Re: problem with PyMapping_SetItemString()

2009-04-21 Thread rahul
On Apr 22, 12:17 am, Stefan Behnel  wrote:
> rahul wrote:
> > tatic PyObject *upadteCheck(PyObject *self,PyObject *args){
> >    PyObject *var_pyvalue=NULL,*newVar_pyvalue=NULL,*dict=NULL;
> >    char *varName;
>
> >    if (!PyArg_ParseTuple(args, "s", &varName)){
> >                 return NULL;
> >       }
> >      dict=PyEval_GetLocals();
> >      var_pyvalue=PyMapping_GetItemString(dict,varName);
>
> >      if(inObject==NULL){
> >            dict=PyEval_GetGlobals();
> >            var_pyvalue=PyMapping_GetItemString(dict,varName);
> >      }
>
> what's "inObject"?
>
> Stefan

Hi Stefan,
   That was due to type error, "inObject" is actually "var_pyvalue"


   rahul
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with PyMapping_SetItemString()

2009-04-21 Thread Aaron Brady
On Apr 21, 2:25 am, rahul  wrote:
> i have a c extension
snip
>           dict=PyEval_GetLocals();
snip
>           PyMapping_SetItemString(dict,varname,newVar_pyvalue);
snip

> than first two test cases runs correctly and gives result for var1
> "value changed"  but 3rd test case not gives correct result and value
> of var1 remains "abcd"
>
> why this happen and how i correct it ??

There's no good way to change the variable space programmatically.
Python has more flexibility than most other languages as it is.
Anyway, your best bet is to use an independent namespace:

class Globals: pass
setattr( Globals, 'itemX', 'value1' )
setattr( Globals, 'itemX', 'value2' )

Your 'updateCheck' function would then have to take the namespace as
an additional parameter.
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with PyMapping_SetItemString()

2009-04-21 Thread John Machin
On Apr 21, 5:25 pm, rahul  wrote:
> i have a c extension
>
> tatic PyObject *upadteCheck(PyObject *self,PyObject *args){
>         PyObject *var_pyvalue=NULL,*newVar_pyvalue=NULL,*dict=NULL;
>         char *varName;
>
>         if (!PyArg_ParseTuple(args, "s", &varName)){
>                 return NULL;
>       }
>           dict=PyEval_GetLocals();
>           var_pyvalue=PyMapping_GetItemString(dict,varName);
>
>           if(inObject==NULL){
>                 dict=PyEval_GetGlobals();
>                 var_pyvalue=PyMapping_GetItemString(dict,varName);
>           }
>
>           printf("\n input value for variable %s is :  %s
> \n",varName,PyString_AsString(var_pyvalue))
>
>           newVar_pyvalue=Py_BuildValue("s","value changed");
>
>           PyMapping_SetItemString(dict,varname,newVar_pyvalue);
>
>           return Py_BuildValue("");
>
> }
>
> and i have three  test cases for this extension
>
> 1.(name test1.py)
>          import upadteCheck
>          var1= "abcd"
>
>          func1():
>              updateCheck.updateCheck("var1")
>              print var1
>
> 2.(name test2.py)
>         import upadteCheck
>          var1= "abcd"
>          updateCheck.updateCheck("var1")
>          print var1
>
> 3.(name test3.py)
>          import upadteCheck
>
>          func1():
>              var1= "abcd"
>              updateCheck.updateCheck("var1")
>              print var1
>
> if i run these three test cases like
>  1.   import test1
>        test1.fun1()
>
>  2.   python test2
>
>  3. import test3
>      test3.func1()
>
> than first two test cases runs correctly and gives result for var1
> "value changed"  but 3rd test case not gives correct result and value
> of var1 remains "abcd"
>
> why this happen and how i correct it ??

Because you are trying to update a function's locals -- see this:
http://docs.python.org/library/functions.html?highlight=locals#locals

CPython's function locals are not stored in a dict, for efficiency
reasons. The dict that you get from locals() in Python or
PyEval_GetLocals() in C is constructed temporarily just for the
occasion. Changes to the dict are not reflected in the function's
locals.

Some general comments:

1. Either you have customised Python and C to forgive typos like
update / upadte and undeclared names like inObject, or you have the
annoying and counter-productive habit of re-typing (badly) what you
thought you ran, instead of using copy/paste.

2. Even if what you are attempting "worked", calling the function in
your extension appears to do no more that writing
   var1 = "changed value"
instead.

3. Why do you think you need to do what you are attempting? What
problem is it trying to solve? What need or requirement is it intended
to meet?
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with PyMapping_SetItemString()

2009-04-21 Thread Stefan Behnel
rahul wrote:
> tatic PyObject *upadteCheck(PyObject *self,PyObject *args){
>   PyObject *var_pyvalue=NULL,*newVar_pyvalue=NULL,*dict=NULL;
>   char *varName;
> 
>   if (!PyArg_ParseTuple(args, "s", &varName)){
> return NULL;
>   }
> dict=PyEval_GetLocals();
> var_pyvalue=PyMapping_GetItemString(dict,varName);
> 
> if(inObject==NULL){
>   dict=PyEval_GetGlobals();
>   var_pyvalue=PyMapping_GetItemString(dict,varName);
> }

what's "inObject"?

Stefan
--
http://mail.python.org/mailman/listinfo/python-list


problem with PyMapping_SetItemString()

2009-04-21 Thread rahul
i have a c extension

tatic PyObject *upadteCheck(PyObject *self,PyObject *args){
PyObject *var_pyvalue=NULL,*newVar_pyvalue=NULL,*dict=NULL;
char *varName;

if (!PyArg_ParseTuple(args, "s", &varName)){
return NULL;
  }
  dict=PyEval_GetLocals();
  var_pyvalue=PyMapping_GetItemString(dict,varName);

  if(inObject==NULL){
dict=PyEval_GetGlobals();
var_pyvalue=PyMapping_GetItemString(dict,varName);
  }

  printf("\n input value for variable %s is :  %s
\n",varName,PyString_AsString(var_pyvalue))

  newVar_pyvalue=Py_BuildValue("s","value changed");

  PyMapping_SetItemString(dict,varname,newVar_pyvalue);

  return Py_BuildValue("");

}

and i have three  test cases for this extension

1.(name test1.py)
 import upadteCheck
 var1= "abcd"

 func1():
 updateCheck.updateCheck("var1")
 print var1

2.(name test2.py)
import upadteCheck
 var1= "abcd"
 updateCheck.updateCheck("var1")
 print var1

3.(name test3.py)
 import upadteCheck

 func1():
 var1= "abcd"
 updateCheck.updateCheck("var1")
 print var1

if i run these three test cases like
 1.   import test1
   test1.fun1()

 2.   python test2

 3. import test3
 test3.func1()

than first two test cases runs correctly and gives result for var1
"value changed"  but 3rd test case not gives correct result and value
of var1 remains "abcd"

why this happen and how i correct it ??
--
http://mail.python.org/mailman/listinfo/python-list


problem with PyMapping_SetItemString()

2009-04-21 Thread rahul
i have a c extension

tatic PyObject *upadteCheck(PyObject *self,PyObject *args){
PyObject *var_pyvalue=NULL,*newVar_pyvalue=NULL,*dict=NULL;
char *varName;

if (!PyArg_ParseTuple(args, "s", &varName)){
return NULL;
  }
  dict=PyEval_GetLocals();
  var_pyvalue=PyMapping_GetItemString(dict,varName);

  if(inObject==NULL){
dict=PyEval_GetGlobals();
var_pyvalue=PyMapping_GetItemString(dict,varName);
  }

  printf("\n input value for variable %s is :  %s
\n",varName,PyString_AsString(var_pyvalue))

  newVar_pyvalue=Py_BuildValue("s","value changed");

  PyMapping_SetItemString(dict,varname,newVar_pyvalue);

  return Py_BuildValue("");
}



and i have three  test cases for this extension

1.(name test1.py)
 import upadteCheck
 var1= "abcd"

 func1():
 updateCheck.updateCheck(var1)
 print var1


2.(name test2.py)
import upadteCheck
 var1= "abcd"
 updateCheck.updateCheck(var1)
 print var1

3.(name test3.py)
 import upadteCheck

 func1():
 var1= "abcd"
 updateCheck.updateCheck(var1)
 print var1

if i run these three test cases like
 1.   import test1
   test1.fun1()

 2.   python test2

 3. import test3
 test3.func1()

than first two test cases runs correctly and gives result for var1
"value changed"  but 3rd test case not gives correct result and value
of var1 remains "abcd"


why this happen and how i correct it ??
--
http://mail.python.org/mailman/listinfo/python-list