how to know argument name with which a function of extended c called

rahul rahul03535 at gmail.com
Wed Apr 15 10:13:06 CEST 2009


On Apr 14, 6:24 pm, John Machin <sjmac... at lexicon.net> wrote:
> On Apr 14, 10:35 pm, rahul <rahul03... at gmail.com> wrote:
>
> > Hi,
> >   i need to write a 'c extension function' in this function i need to
> > change argument value with  which this function called.
>
> The appropriate way for a function to give output is to return a
> value, or a tuple of values.
>
> example:
>
> def get_next_token(input_buffer, offset):
>    """get next lexical token, starting at offset
>       return (the_token, new offset)"""
>    length = find_len_of_token_somehow(input_buffer, offset)
>    new_offset = offset + length
>    return input_buffer[offset:new_offset], new_offset
>
> and you can call it by
>    token, pos = get_next_token(buff, pos)
>    return input
>
> >   ie,
> >          if a python code like
> >             import changeValue as c
> >             arg="old value"
> >             c.changeValue(arg)
> >             print arg
>
> Fortunately, you can't construct such a thing in Python or in a C
> extension. Consider the following:
>
> print "two", 2
> c.changeValue(2)
> print "two maybe", 2
>
> What would you want to it to print the second time?
> two maybe new value?
>
>
>
> >  then it print "new value"
>
> >  i write code like this..
>
> > static PyObject *changeValue(PyObject *self,PyObject *args){
> >         PyObject *sampleObj, *m ;
> >         char *argName;
>
> >       if (!PyArg_ParseTuple(args, "O", &sampleObj)){
> >                 return NULL;
> >       }
>
> >    m = PyImport_AddModule("__main__");
>
> This means you are assuming/hoping this function will be called only
> from the main script ...
>
> >    PyObject_SetAttrString(m, argName, "new value");
>
> Even if you know the name, you have the problem that it is changing
> the __main__ module's globals ... but the arg could be local or it
> could be an expression ...
>
> >    return Py_BuildValue("");
>
> > }
>
> > But for this i need to know the argument name with which this function
> > called .
> > Is this possible to know argument name in extended c function? if yes,
> > than how i can do it???
>
> No, it's not possible to know the argument name (without help from the
> caller e.g. keyword args), it may not even have a name, it may have
> multiple names ... this is just another variation of the old "what is
> the name of my object" FAQ.
>
> Why don't you tell us what you are trying to achieve (a higher-level
> goal than "I need to poke some value at/into some variable of doubtful
> name and unknowable location"), and then we might be able to give you
> some ideas.
>
> HTH,
> John




Hi John,
   thanks for your great full information,
     But, In my project anyhow i have to change argument value in some
cases. can we pass pointer  of an variable in extended c function.

  like
     import changeValue as c
            arg="old value"
            c.changeValue(&arg)
            print arg

if yes, then how pointer of this variable handle through extended c
function. and how we can change the value through pointer.

----
Rahul priyadarshi



More information about the Python-list mailing list