using python interpreters per thread in C++ program

2009-09-06 Thread grbgooglefan
Hi

I've a multi-threaded C++ program, in which I want to use embedded
python interpreter for each thread. I am using Python 2.6.2 on Linux
for this.

When I tried to embed python interpreter per thread, I got crash when
the threads were calling Python's C APIs.

Can we not use python interpreters even private to each multiple
thread?

What is best way to embed python in multi-threaded C++ application?

Please guide.
-- 
http://mail.python.org/mailman/listinfo/python-list


Python interpreters in threads crash the application

2009-04-15 Thread grbgooglefan
I have C application in which I have instantiated Python interpreter
in each worker thread.

When I start the program it crashes at different places in Python code
but program never finishes normally.

One such core dump is given below.

Can you please help me in following queries?
1) Can we not use Python interpreters per thread instead of having a
common shared Python interpreter at global level/ scope?
2) Why is this crash happening? Do I need to use locks in Python
interpreter?
 Will that make the multiple threads work in serialized way?


pflags core:-
data model = _ILP32  flags = MSACCT|MSFORK
 /1:flags = STOPPED
why = PR_SUSPENDED
 /2:flags = STOPPED
why = PR_SUSPENDED
 /3:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /4:flags = 0
sigmask = 0xbefc,0x  cursig = SIGSEGV
 /5:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /6:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /7:flags = STOPPED  lwp_mutex_timedlock(0xff3f0a10,0x0)
why = PR_SUSPENDED
 /8:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /9:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /10:   flags = STOPPED
why = PR_SUSPENDED
 /11:   flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED

pstack core:
-  lwp# 1 / thread# 1  
 000110e8 main (1, ffbff2d4, ffbff2dc, 21400, feec0200, feec0240)
+ 50
 00010c10 _start   (0, 0, 0, 0, 0, 0) + 108
-  lwp# 2 / thread# 2  
 ff20877c string_dealloc (fed7bbfc, 53bc, ff2f6ba0, e40e0, ff2f6ba0,
ff2dec08)
 ff213b44 PyString_InternFromString (ff316e68, 0, 0, ff316e68, e40c0,
2000) + 20
 ff2262f8 init_slotdefs (b8964, 0, 2000, 2204, 0, 0) + 58
 ff2266b8 add_operators (ff2f76c0, 1c00, b8568, 5000, 2000, 21b0) + 1c
 ff21fe28 PyType_Ready (ff2f76c0, bef10, 0, 0, 5124, 0) + 134
 ff21fd88 PyType_Ready (ff2f7438, 1, de358, ff3b3904, 5124, 5284) + 94
 ff2008c8 _Py_ReadyTypes (0, ff308ea4, 121c20, fefc6964, 21788,
ff06b3d4) + 1c
 ff287cc8 Py_InitializeEx (0, 0, 4000, 0, 0, ff2dec08) + 234
 00010c4c __1cMinterpthread6Fpv_0_ (0, fed7c000, 0, 0, 10c38, 0) + 14
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 3 / thread# 3  
 ff040154 __lwp_park (0, 0, ff06c348, 0, 0, 1) + 14
 ff02415c _flockget (ff06c348, 21878, 441b4, ff3b3904, ff06c308,
21878) + 94
 ff018364 printf   (112a4, 21878, 0, 21896, ff068284, ff2dec08) + 5c
 00010c8c __1cMinterpthread6Fpv_0_ (0, fec7c000, 0, 0, 10c38, 0) + 54
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 4 / thread# 4  
 fefb0c90 strlen   (ff2bdee8, feafbcf4, 4000, 73, 2e, 4000) + 50
 ff274924 PyErr_Format (ff2ebb2c, ff2bdee8, 0, 1400, 1780, ff2dec08) +
18
 ff1feeb8 PyObject_HashNotImplemented (e4020, 0, 5000, 53bc, ff2f6ba0,
0) + 34
 ff1f8420 PyDict_GetItem (23a50, e4020, 2000, cb28c, ff2dec08, 0)
+ 64
 ff213a2c PyString_InternInPlace (feafbe3c, 53bc, ff2f6ba0, e4020,
ff2f6ba0, ff2dec08) + b4
 ff1fb6b4 PyDict_SetItemString (239c0, 114a3, e3030, 21896, ff2dec08,
1b) + 20
 00010cd8 __1cMinterpthread6Fpv_0_ (0, feafc000, 0, 0, 10c38, 0) + a0
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 5 / thread# 5  
 ff040154 __lwp_park (0, 0, ff06c348, 0, 0, 1) + 14
 ff02415c _flockget (ff06c348, 21878, 441b4, 0, ff06c308, 21878) + 94
 ff018364 printf   (112a4, 21878, 0, 21896, ff068284, ff2dec08) + 5c
 00010c8c __1cMinterpthread6Fpv_0_ (0, fe9fc000, 0, 0, 10c38, 0) + 54
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 6 / thread# 6  
 ff040154 __lwp_park (0, 0, ff06c348, 0, 0, 1) + 14
 ff02415c _flockget (ff06c348, 21878, 441b4, ff29afcc, ff06c308,
21878) + 94
 ff018364 printf   (112a4, 21878, 0, 21896, ff068284, ff2dec08) + 5c
 00010c8c __1cMinterpthread6Fpv_0_ (0, fe8fc000, 0, 0, 10c38, 0) + 54
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 7 / thread# 7  
 ff3d7e68 ___lwp_mutex_timedlock (0, 1, ff3c3f98, ff3ee980, 2ae64,
ff3ee268) + c
 ff3cc60c elf_bndr (ff3f1b98, 132c, ff1f0840, e3030, ff3f06d0, 0) + 28
 ff3b3904 elf_rtbndr (ff1f0840, 3d, 1, 92492400, ff2f339c, 8) + 10
 ff2e4060  (e3020, ff2f2314, 0, 0, 0, 0)
 ff1f0840 PyLong_FromLong (0, 21878, ff06c348, 21896, ff2dec08, 1b) +
44
 00010cc8 __1cMinterpthread6Fpv_0_ (0, fe7fc000, 0, 0, 10c38, 0) + 90
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 8 / thread# 8  
 ff040154 __lwp_park (0, 0, ff06c348, 0, 0, 1) + 14
 ff038db0 mutex_lock_internal (ff06c348, 0, 1, 0, 10, ff06cbc0) + 5d0
 ff02415c _flockget (ff06c348, 21878, 441b4, ff29afcc, ff06c308,
21878) + 94
 ff018364 printf   (112a4, 21878, 0, 

building release - assert-free python library

2009-04-07 Thread grbgooglefan
How can I build a release and not the debug version of libpython.a?
I have seen that there are assert, abort statements in lot many
functions in Python code. I would like to avoid those when compiling
the libpython.a library because when this libpython gets used for
production purpose and it aborts, the application goes down without
any means of graceful handling that error condition.
How can we handle this?
Please help
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to free /destroy object created by PyTuple_New

2009-04-05 Thread grbgooglefan
Regarding PyTuple_New, when I pass this tuple with variable values set
to some evaluation function like PyObject_CallObject, do I need to
increment reference for this tuple & then decrement again after the
call returns?

If I've not configured my libpython with threads when compiling, will
PyGILState_Release, etc. calls still work?
For PyGILState_Release/Acquire calls do I need to create the lock
explicitely or it will get created internally by Interpreter when
Interpreter is initialized?
Please guide.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to free /destroy object created by PyTuple_New

2009-04-04 Thread grbgooglefan
On Apr 4, 10:27 pm, Andrew Svetlov  wrote:
> To destroy every python object you need to call Py_DECREF.
> To call python code fron you C thread you need to use pair
> PyGILState_Ensure/PyGILState_Release.

In my case, my C application has multiple threads & they are accessing
a single Python Interpreter which was initialized by 1st main thread.
In that case also, do I need to use PyGILState_Ensure/
PyGILState_Release APIs?

I am not using Python interpreter level threads.

Can I have a Python interpreter per thread so that I won't have issues
of some data inside Python interpreters being gettint accidently
overwritten/ freed by other thread?

Will Py_DECREF release the items set in that tuple using call to
PyTuple_SetItem? Or do I need to separately call Py_DECREF for them
also?
What is correct sequence for calling Py_DECREF & Py_INCREF?
If I dont call Py_DECREF, will that object (PyTuple) not get freeed at
all, causing memory leak?

Please guide.
--
http://mail.python.org/mailman/listinfo/python-list


How to free /destroy object created by PyTuple_New

2009-04-04 Thread grbgooglefan

I am using PyTuple_New to pass function arguments to
PyObject_CallObject for execution of a Python function.

How can I free up the memory and object allocated by the PyTuple_New
function?

I am using Python interpreter in my multi-threaded C application & at
load condition my application crashes in Python's evaluation function.
I am not explicitely releasing the PyTuple_New object.

Please guide how can I destory object created by PyTuple_New?

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


Call to PyEval_EvalCodeEx crashes in PyFrame_New with abort

2009-03-30 Thread grbgooglefan
I've emabedded Python(2.6) in my C++ application and using on Solaris
5.10.
When the application calls Py_Eval, it causes an abort & application
core dumps. But this is at random times & no pattern for this. Crash
is as below:

Can you please help to know, what can cause this abort in PyFrame_New?

 fd7c642c _lwp_kill (6, 0, fd7f3018, fd7a8df8, , 6) + 8
 fd741198 abort(fe523660, 1, 35a7c4, ad260, fd7f12d8, 0) + 110
 fe4d869c PyFrame_New (9d1668, 4621140, 9ecc00, fffd, 4621150, 4)
+ 328
 fe48b654 PyEval_EvalCodeEx (9e07a0, 9ecc00, 0, 4815dac, 3, fe543800)
+ 2c
 fe4d9f5c function_call (9e0770, 4815da0, 0, 0, 4815d90, 0) + 1ac
 fe4c9054 PyObject_Call (9e0770, 4815da0, 0, fe54f784, fffe, 2) +
1c
 fe48d2dc PyEval_CallObjectWithKeywords (9e0770, 4815da0, 0, 0,
4815da0, 9e0770) + f0
 fe4aebf0 Py_Eval  (9e0770, 4815da0, 0, 3, 3c, fe5395a4) + 94
--
http://mail.python.org/mailman/listinfo/python-list


How to modify meaning of builtin function "not" to "!"?

2008-05-09 Thread grbgooglefan
I am creating functions, the return result of which I am using to make
decisions in combined expressions.
In some expressions, I would like to inverse the return result of
function.

E.g. function contains(source,search) will return true if "search"
string is found in source string.
I want to make reverse of this by putting it as:
if ( ! contains(s1,s2) ):
 return 1

I found that "!" is not accepted by Python & compile fails with
"invalid syntax".
Corresponding to this Boolean Operator we've "not" in Python.

How can I make "not" as "!"?
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to pass C++ object to another C++ function via Python function

2008-05-09 Thread grbgooglefan
Yes, that worked. I "protected" the variable which I did not want to
get freed. I incremented reference for that variable & it started
working.

Thanks for all your guidance.

On May 3, 5:23 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> Python will do nothing with the pointer inside a PyCObject - unless you  
> want to, and pass a cleanup function as the second argument to the  
> PyCObject constructor.
>
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -

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


Re: how to pass C++ object to another C++ function via Python function

2008-05-01 Thread grbgooglefan
On Apr 22, 7:54 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
>
> If you have a C function that receives a PyCObject, just include the  
> relevant headers (cobject.h) and you can retrieve the original pointer  
> using PyCObject_AsVoidPtr:
>
> void foo(PyObject *pyobj)
> {
>    TOriginalType *porig;
>    porig = (TOriginalType *)PyCObject_AsVoidPtr(pyobj);
>    // do something with porig
> }
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -

This works. But only once, means if I try to use this object second
time in Python function it causes crash.

What I am doing in my program is that I am putting STL map in a
structure & passing that structure as object to a Python function
alongwith other agurments of that Python function. This briefly as
below:

// In pyinterface.h file:---
typedef hash_map Elements;
typedef hash_map
PerExchGroupsElementsTable;
typedef struct capsule {
PerExchGroupsElementsTable* refgrps;
} *pcapsule;

// In pyinterface.cpp file:---
numvars = // value set depending on number of variables of that python
function
PyObject *pTuple = PyTuple_New(numvars);

// Set variables as below:
for(nCtr = 0; nCtr < numvars; nCtr++){
 slot = prul->pVarLst[nCtr].slot;
 ndtyp = ordvars[slot].dtype;
 switch(ndtyp){
   case(INT_T):
 
PyTuple_SetItem(pTuple,nCtr,PyInt_FromLong(ordvars[slot].nvalue));
   break;
   case(FLOAT_T):
 
PyTuple_SetItem(pTuple,nCtr,PyFloat_FromDouble(ordvars[slot].fvalue));
   break;
   case(STRING_T):
 
PyTuple_SetItem(pTuple,nCtr,PyString_FromString(ordvars[slot].cvalue));
   break;
   default:
   printf("\nUnknown data type [%d] for %s\n",ndtyp,prul-
>pVarLst[nCtr].var);
   bUnknownDataType = true;
   break;
 }
 if(bUnknownDataType){
ret = -1;
break;
 }
}

// Then set the C++ object as below:
if(ret == 0){
capsule grpob;
if(pGroups){
  grpob.refgrps = pGroups; // pGroups is pointer to
PerExchGroupsElementsTable map & is global.
  int ret = PyTuple_SetItem(pTuple,
(numvars-1),PyCObject_FromVoidPtr((void *)&grpob, NULL));
}
PyObject *pResult = PyObject_CallObject(pfunc,pTuple);
if(PyErr_Occurred()){
   printf("error occured in PyObject_CallObject for %s\n",prul-
>pyobj.szPyRouteName);
   PyErr_Print();
} else {
printf("Python function passed, use its result for other
purposes as designed\n");
}
Py_XDECREF(pResult);
Py_XDECREF(pTuple);
}

//-- My Pythong module & functions in it ---
//PyObject* pfunc is a Python function which I compile dynamically &
add to my Python module as below:
// One of such dynamically compiled function is as below:
char pyfunction[]=\
"def TSE581(t22,t52,t1012,ob):
   if(co1(ob,t22,\"TSE_FUTURE_GRP\") and
like1(ob,t52,\"TSE_SECID_LST2\")):\n\
  print \"result is pass\"\n\
  return 1\n\
   else:\n\
  print \"result is fail\"\n\
  return 0\n";

// function parameter "ob" in this function definition is the one
which Im passing as CObject.

PyObject *result = NULL;
result =
PyRun_String(pyfunction,Py_file_input,_pPyDictionary,_pPyDictionary);
if(PyErr_Occurred() || result == NULL){
printf("Failed to compile function [%s]\n",func);
PyErr_Print();
return;
}
Py_XDECREF(result);
result = NULL;
PyObject *ptr = PyObject_GetAttrString(_pPyModule,fname);
if(PyErr_Occurred() || *ptr == NULL){
printf("PyObject_GetAttrString failed:%s",fname);
return;
}
if(!PyCallable_Check(*ptr)){
   printf("%s not a callable Python code\n",fname);
   Py_XDECREF(*ptr);
   *ptr = NULL;
   return;
}

// I've created dynamically loadble Python module & multiple functions
similar to above gets added dynamically to this module.
// Module has functions "co1" & "like1" in module's .cpp file. These
functions then use CObject - the struct capsule & map pointer in it.
static PyObject* merorderrouter_co1(PyObject* self, PyObject* args)
{
printf("Contains function\n");
int ret=0;
char *arg1=NULL, *arg2=NULL;
PyObject* voidcap=NULL;
if(!PyArg_ParseTuple(args, "Oss", &voidcap,&arg1,&arg2)){
   printf("failed to get args\n");
   if(PyErr_Occurred())
 PyErr_Print();
   return(PyInt_FromLong(ret));
}
printf("key=%s, grpname=[%s]\n",arg1,arg2);
if(voidcap && PyCObject_Check(voidcap))
   printf("valid Py-C-Object\n");
else
   printf("NOT a valid Py-C-Object\n");
pcapsule pobj = (pcapsule)PyCObject_AsVoidPtr(voidcap);
if(pobj){
   PerExchGroupsElementsTable grpmap = *pobj->refgrps;
   if(grpmap.count(arg2)){
 PerExchGroupsElementsTable::iterator grpmi =
grpmap.find(arg2);
 Elements grpelems = *(Elements*)grpmi->second;
 Elements::iterator ei = grpelems.find(arg1);
 if(ei != grpelems.end()){
printf("has elm.\n");
ret=

Re: how to pass C++ object to another C++ function via Python function

2008-04-21 Thread grbgooglefan
On Apr 21, 10:17 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Mon, 21 Apr 2008 10:24:15 -0300, grbgooglefan <[EMAIL PROTECTED]> escribió:
>
> > I am trying to pass a C++ object to Python function. This Python
> > function then calls another C++ function which then uses this C++
> > object to call methods of that object's class.
>
> > I tried something like this, but it did not work, gave core dump.
>
> You can't pass any arbitrary C object to a Python function.
> In this case you can use a PyCObject, a Python box around a void* pointer.
> Seehttp://docs.python.org/api/cObjects.html

Yup, I looked at http://www.python.org/doc/ext/using-cobjects.html
also. I could not find in this example where is CObject used or
converted back from Python to C.
Is there any tutorial which I can use for this?
--
http://mail.python.org/mailman/listinfo/python-list


how to pass C++ object to another C++ function via Python function

2008-04-21 Thread grbgooglefan
I am trying to pass a C++ object to Python function. This Python
function then calls another C++ function which then uses this C++
object to call methods of that object's class.

I tried something like this, but it did not work, gave core dump.

class myclass {
  public:
   myclass(){};
   ~myclass(){};
   void printmyname() { printf("I am myclass, num=%d\n",num); };
};

main(){
myclass myobj

   char funcbody[]=\
"def pyfunction(t167,classobj):\n\
 onemorefunc(t167,\"NONAMe\")\n\
 return 10\n\
\n\n";

// compile this Python function using PyRun_String & get its pointer
by PyObject_GetAttrString.
// Later call it as below:
   myclass myobj(23);
   PyObject *pTuple = 0;
   pTuple = PyTuple_New(2);
   PyTuple_SetItem(pTuple,0,PyString_FromString("NAME")); // for t167
parameter
   PyObject *inputarg = Py_BuildValue("OO&",pTuple,myobj); // for
classobj parameter

   result = PyObject_CallObject(pPyEvalFunction,inputarg);
}

How can I pass this class object to Python function?
Is it possible to set it in tuple using PyTuple_SetItem, because I may
have varying number of arguments for my Python functions & that's why
I can't use Py_BuildValue.
-- 
http://mail.python.org/mailman/listinfo/python-list


is hash map data structure available in Python?

2008-03-19 Thread grbgooglefan
Hi,
I have a situation that I need to search a name in a big list of names
in my Python embedded interpreter. I am planning to use hash map for
quicker search.
How do I create hash map in Python?
Can you please guide me to some documentation or tutorial which
provides information on creating, editing, searching through hash map
in Python?
Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyImport_ImportModule("cStringIO") failure with undefined symbol

2008-01-31 Thread grbgooglefan
On Jan 11, 9:31 am, "Borse, Ganesh" <[EMAIL PROTECTED]>
wrote:
> Hi,
> Can you please guide me for the following problem?
> The call to "PyImport_ImportModule("cStringIO");" is failing with an error of 
> "undefined symbol:PyObject_SelfIter".
>
> Before importing this module, I am importing only the sys module.
>
>    Py_SetProgramName("/usr/bin/python");
>    Py_Initialize();
>    char* argv[] = { "python","-v",""};
>    PySys_SetArgv(2,argv);
>    PyRun_SimpleString("import sys");
>
>    PyObject *modStringIO = NULL;
>    // Import cStringIO module
>    modStringIO = PyImport_ImportModule("cStringIO");
>
> Should I be importing any other additional module(s) to make this import work?
>
> Please help.
>
> I am trying to use the function GetPythonErrorMessage provided in this 
> post:http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> Thanks in advance for your help.
> Regards.
==

Function "PyObject_SelfIter" is part of libPython.a. But when library
which is linked with libPython.a tries to import cStringIO (which is
nothing but dlopen("lib-dynload/cStringIO.so"), ld.so does not find
this symbol.
So, as a workaround, we can get the cStringIO.so also linked with the
library which has linked libPython.a.
By this we ensure that PyObject_SelfIter is already resolved in the
library.
Then at the time of importing cStringIO at runtime, this symbol is
already referenced & won't cause problems.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Import of cStringIO failing with "undefined symbol: PyObject_SelfIter" on python-2.3.3-88.9

2008-01-31 Thread grbgooglefan
On Jan 10, 6:07 pm, grbgooglefan <[EMAIL PROTECTED]> wrote:
> I am importing cStringIO module in my PythonC++ embedded program.
>
> The import is failing with the following error:
> ImportError: /usr/lib/python2.3/lib-dynload/cStringIO.so: undefined
> symbol:PyObject_SelfIter
>
> I have python-2.3.3-88.9.x86 installed on my machine.
> Why is this error coming? how can I resolve this undefined symbol?
> Do I need to import anything before this?

Workaround to avoid this problem is in another post named
"PyImport_ImportModule("cStringIO") failure with undefined symbol
Options".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get string printed by PyErr_Print( )?

2008-01-31 Thread grbgooglefan
On Jan 9, 8:01 pm, grbgooglefan <[EMAIL PROTECTED]> wrote:
> On Dec 19 2007, 5:55 pm, Christian Heimes <[EMAIL PROTECTED]> wrote:
> >grbgooglefanwrote:
> > > PythonC API functionPyErr_Print( ) prints an error string onto stderr
> > > if PyErr_Occurred() is true.
> > > I don't want to print this to stderr because my Python+C code is
> > > running daemon mode & won't have terminal / stderr.
> > > So, I want to retrieve the string whichPyErr_Print( ) will print.
> > > E.g.,PyErr_Print() printed following string when I tried to call
> > > setTuple with one extra argument
> > > Traceback (most recent call last):
> > >   File "", line 2, in isTacticSafe
> > > IndexError: tuple assignment index out of range
>
> > I suggest a different approach. A daemon must have a stdin, stdout and
> > stderr connected to a terminal. You can use freopen() to redirect stderr
> > and stdout to a log file and fclose() to close stdin.
>
> >http://www.gnu.org/software/libc/manual/html_mono/libc.html#Opening-S...
>
> > Christian
>
> I do not want to redirect anything to file. Because I do not want to
> avoid the disk access completely - for reading as well as writing.
> I liked the 1st option suggested by Robert Kern.
>
> Can you please explain a little bit how can I replace sys.stderr with
> StringIO or my char* buffer?
> I have to show the error message of Python code compilation &
> execution to the user on the GUI & therefore I want to capture the
> error message directly instead of logging it to file.
>
> Thanks in advance for all your help.- Hide quoted text -
>
> - Show quoted text -

Well, I managed to do it myself. Here is what I am doing now to do
this. In Python-C/C++ world, if someone wants to do the same thing,
may use this.
Note: For this, am assigning the cStringIO object to sys.stderr once I
do Py_Initialize & then use cStringIO.getvalue() everytime I get
error.
While importing cStringIO module with Python-2.3.3, I faced the
problem of "undefined symbol:PyObject_SelfIter". I could resolve that
also. I have put that note in the post which I had opened for that.
/+++
PyObject *_pPyModule;
PyObject *_pPyDictionary;
PyObject *_pPyGetValFunc;
PyObject *_pPyobStringIO;

Init(){
// Py_Initialize should have been done by now.
   PyObject *modStringIO = NULL;
   PyObject *obFuncStringIO = NULL;

   // Import cStringIO module
   modStringIO = PyImport_ImportModule("cStringIO");
   if(PyErr_Occurred() || modStringIO == NULL){
 printf("pyParserEvaluator::Init::PyImport cStringIO failed:");
 PyErr_Print();
 goto PY_INIT_ERR;
   }
   // get StringIO constructor
   obFuncStringIO = PyObject_GetAttrString(modStringIO, "StringIO");
   if(PyErr_Occurred() || obFuncStringIO == NULL){
 printf("pyParserEvaluator::Init: cant find cStringIO.StringIO:");
 PyErr_Print();
 goto PY_INIT_ERR;
   }
   // Construct cStringIO object
   _pPyobStringIO = PyObject_CallObject(obFuncStringIO, NULL);
   if(PyErr_Occurred() || _pPyobStringIO==NULL){
 printf("pyParserEvaluator::Init: cStringIO.StringIO() failed:");
 PyErr_Print();
 goto PY_INIT_ERR;
   }
   // get getvalue() method in StringIO instance
   _pPyGetValFunc = PyObject_GetAttrString(_pPyobStringIO,
"getvalue");
   if(PyErr_Occurred() || _pPyGetValFunc==NULL){
 printf("pyParserEvaluator::Init: cant find getvalue function:");
 PyErr_Print();
 goto PY_INIT_ERR;
   }
   // try assigning this object to sys.stderr
   ret = PySys_SetObject("stderr", _pPyobStringIO);
   if(ret != 0){
  printf("failed to assign _pPyobStringIO to stderr\n");
  goto PY_INIT_ERR;
   }
   return ret;

PY_INIT_ERR:
   Py_XDECREF(modStringIO);
   Py_XDECREF(obFuncStringIO);
   Py_XDECREF(_pPyobStringIO);
   Py_XDECREF(_pPyGetValFunc);
}

int _getPythonErrorMessage()
{
// call getvalue() method in StringIO instance
int ret = 0;
PyObject *obResult=NULL;
char *sresult = NULL;
obResult = PyObject_CallObject(_pPyGetValFunc, NULL);
if(PyErr_Occurred() || obResult==NULL){
   printf("getvalue() failed\n");
   ret = -1;
   goto CLEAN_AND_RETURN;
}
// did getvalue return a string?
if(!PyString_Check(obResult)){
   printf("getvalue() did not return error string\n");
   ret = -1;
   goto CLEAN_AND_RETURN;
}
// retrieve error message string from this object
if(NULL != (sresult = PyString_AsString(obResult))){
   pErrorString = strdup(sresult);
} else {
   ret = -1;
   goto CLEAN_AND_RETURN;
}
return(ret);

CLEAN_AND_RETURN:
   Py_XDECREF(obResult);
   return(ret);
}
=
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replacing call to PyObject_CallObject with PyEval_CallFunction

2008-01-29 Thread grbgooglefan
On Jan 30, 1:58 pm, Gabriel Genellina <[EMAIL PROTECTED]> wrote:
> On 30 ene, 01:58, grbgooglefan <[EMAIL PROTECTED]> wrote:
>
> > How do I pass the elements populated in struct variables of this
> > vector dynamically to PyEval_CallFunction, in the fashion somewhat
> > like below?
> > PyEval_CallFunction(obj, "iii", 
> > vector[0].ioparam->nionum,vector[1].ioparam->nionum,vector[2].ioparam->nion­um);
>
> > PyEval_CallFunction(obj, "di", 
> > vector[0].ioparam->fionum,vector[1].ioparam->nionum);
>
> > PyEval_CallFunction(obj, "diiisis", 
> > vector[0].ioparam->fionum,vector[1].ioparam->nionum,vector[2].ioparam-
> > >nionum,vector[3].ioparam->nionum,vector[4].ioparam-
> > >ioString,vector[5].ioparam->nionum,vector[6].ioparam->ioString);
>
> I didn't know of PyEval_CallFunction until now, but aparently the
> lines above are OK. Can't you use it that way? What's your problem?
>
> --
> Gabriel Genellina

slowness of current code - vector is to parsed one by one & switch
adds another time for checks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Replacing call to PyObject_CallObject with PyEval_CallFunction

2008-01-29 Thread grbgooglefan
Hello Python Experts
May you please help me on this change?
I have following code:
//==
typedef struct IOParams {
char   *ioString;
long   lionum;
double dionum;
float  fionum;
intnionum;
} *pIOParams;
   // iterate thru list of variables, check if all req data is set
   int nCtr, ndtyp, ret = 0;
   IOParams inputVar;
   PyObject *pTuple = 0;
   pTuple = PyTuple_New(numofVars);
   for(nCtr = 0; nCtr < numofVars; nCtr++){
  ndtyp = pEvalFunc->pExprVarsArray[nCtr].nDataType;
  switch(ndtyp){
case(INT_T):
   printf("%s=%d ",pEvalFunc-
>pExprVarsArray[nCtr].szVarName,inputVar.nionum);
 
PyTuple_SetItem(pTuple,nCtr,PyInt_FromLong(inputVar.nionum));
   break;
case(LONG_T):
   printf("%s=%ld ",pEvalFunc-
>pExprVarsArray[nCtr].szVarName,inputVar.lionum);
 
PyTuple_SetItem(pTuple,nCtr,PyLong_FromLong(inputVar.lionum));
   break;
case(FLOAT_T):
   printf("%s=%f ",pEvalFunc-
>pExprVarsArray[nCtr].szVarName,inputVar.fionum);
 
PyTuple_SetItem(pTuple,nCtr,PyFloat_FromDouble(inputVar.fionum));
   break;
case(DOUBLE_T):
   printf("%s=%f ",pEvalFunc-
>pExprVarsArray[nCtr].szVarName,inputVar.dionum);
 
PyTuple_SetItem(pTuple,nCtr,PyFloat_FromDouble(inputVar.dionum));
   break;
case(STRING_T):
   printf("%s=%s ",pEvalFunc-
>pExprVarsArray[nCtr].szVarName,inputVar.ioString);
 
PyTuple_SetItem(pTuple,nCtr,PyString_FromString(inputVar.ioString));
   break;
default:
   printf("\nUnknown data type [%d] for %s
\n",ndtyp,pEvalFunc->pExprVarsArray[nCtr].szVarName);
   bUnknownDataType = true;
   break;
  }
  if(bUnknownDataType){ // got an unknown data type for a variable
ret = -1;
break;
  }
   }
   // all variables are set, call Python function
   if(ret == 0){
 printf("\n");
 PyObject *pResult = PyObject_CallObject(pEvalFunc-
>pPyEvalFunction,pTuple);
//==
In this, I am using IOParams struct to set values for the pTuple. The
number & type of values to be set in pTuple change at runtime and
therefore I had to put this for loop to set the value in this pTuple.

Then I came to know about another function call
PyEval_CallFunction(PyObject *obj, char *format, ...);
This function call accepts variable number of arguments, like:
PyEval_CallFunction(obj, "iii", a, b, c);

Limitation with this is that "a", "b" and "c" are individual variables
& are all known at compilation time.
But, in may case, the variables with the values are in a vector of
IOParams structs.

How do I pass the elements populated in struct variables of this
vector dynamically to PyEval_CallFunction, in the fashion somewhat
like below?
PyEval_CallFunction(obj, "iii", vector[0].ioparam-
>nionum,vector[1].ioparam->nionum,vector[2].ioparam->nionum);
PyEval_CallFunction(obj, "di", vector[0].ioparam-
>fionum,vector[1].ioparam->nionum);
PyEval_CallFunction(obj, "diiisis", vector[0].ioparam-
>fionum,vector[1].ioparam->nionum,vector[2].ioparam-
>nionum,vector[3].ioparam->nionum,vector[4].ioparam-
>ioString,vector[5].ioparam->nionum,vector[6].ioparam->ioString);
and so on.

These function calls are just few samples & not all of them.
Any suggestions on this.
Thanks & Regards.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to set sys.stderr to object of cStringIO type

2008-01-15 Thread grbgooglefan
On Jan 15, 5:45 pm, grbgooglefan <[EMAIL PROTECTED]> wrote:
> I am in a perculiar situation. I want to use PyRun_SimpleString for
> creating Python functions in embedded Python in C++.
> But there could be cases when Python function code compilation could
> fail & PyRun_SimpleString will return -1 as return status. At this
> time, it prints the error message to sys.stderr.
> So, we do not have any control or cannot use that message to show to
> user to correct the errors in the function code.
> I could assign an object of cStringIO type to sys.stderr at Python
> command prompt as below:
>
> >>> import sys
> >>> import cStringIO
> >>> obj=cStringIO.StringIO()
> >>> sys.stderr=obj
>
> And then using the obj.getvalue(), I could print exceptions printed to
> sys.stderr.
>
> But, I am not able to figure out, how can I do this same thing in
> Python/C API in my program using Py* functions?
>
> How do we set the "sys.stderr" to cStringIO object from Python
> embedded in C++ or C?
>
> Should I be using PyFile_FromFile for convert the cStringIO object?
> Please help.

I have got a solution for this. Would like to know if this is the
correct way or will it cause any problems if program runs for long
time?

/***/
/--1st stage is assign object of cStringIO to sys.stderr at
initialization
/-- once that is done, we can call getvalue() on that object on every
error.
/-/
  PyObject *_pPyobStringIO;
  PyObject *_pPyGetValFunc;
  _pPyobStringIO=NULL;
  _pPyGetValFunc=NULL;
  PyObject *obFuncStringIO = NULL;
  int ret1 = 0;

  // Import cStringIO module
  PyObject * modStringIO = PyImport_ImportModule("cStringIO");
  if(PyErr_Occurred() || modStringIO == NULL){
 printf("pyParserEvaluator::Init::PyImport cStringIO
failed:");
 PyErr_Print();
 goto PY_INIT_ERR;
  }
  // get StringIO constructor
  obFuncStringIO = PyObject_GetAttrString(modStringIO,
"StringIO");
  if(PyErr_Occurred() || obFuncStringIO == NULL){
 printf("pyParserEvaluator::Init: cant find
cStringIO.StringIO:");
 PyErr_Print();
 goto PY_INIT_ERR;
  }
  // Construct cStringIO object
  _pPyobStringIO = PyObject_CallObject(obFuncStringIO, NULL);
  if(PyErr_Occurred() || _pPyobStringIO==NULL){
printf("pyParserEvaluator::Init: cStringIO.StringIO()
failed:");
PyErr_Print();
goto PY_INIT_ERR;
  }
  // get the getvalue function ptr
  _pPyGetValFunc = PyObject_GetAttrString(_pPyobStringIO,
"getvalue");
  if(PyErr_Occurred() || _pPyGetValFunc==NULL){
 printf("pyParserEvaluator::Init: cant find getvalue
function:");
 PyErr_Print();
 goto PY_INIT_ERR;
 }
  // try assigning this object to sys.stderr
  ret1 = PySys_SetObject("stderr", _pPyobStringIO);
  if(ret1 != 0){
printf("failed to assign _pPyobStringIO to stderr\n");
  } else
 printf("assigned _pPyobStringIO to stderr\n");
PY_INIT_ERR:
 printf("pyParseEvaluator::pyParseEvaluator failed\n");
/
**/
  int ret = PyRun_SimpleString(strFunction);
  if(ret != 0){
// call getvalue() method in StringIO instance
PyObject *obResult=NULL;
obResult = PyObject_CallObject(_pPyGetValFunc, NULL);
if(PyErr_Occurred() || obResult==NULL){
   printf("getvalue() failed\n");
   return -1;
} elseprintf("PyObject_CallObject on getvalue is ok\n");
   // did getvalue return a string?
if(!PyString_Check(obResult)){
 printf("getvalue() did not return error string\n");
 return -1;
} else  printf("getvalue returned string object\n");
// retrieve error message string from this object
char *sresult = NULL;
if(NULL != (sresult = PyString_AsString(obResult))){
  printf("result string was [%s]\n",sresult);
}
  }
/**/
-- 
http://mail.python.org/mailman/listinfo/python-list


[help request] how to set sys.stderr to object of cStringIO type

2008-01-15 Thread grbgooglefan
I am in a perculiar situation. I want to use PyRun_SimpleString for
creating Python functions in embedded Python in C++.
But there could be cases when Python function code compilation could
fail & PyRun_SimpleString will return -1 as return status. At this
time, it prints the error message to sys.stderr.
So, we do not have any control or cannot use that message to show to
user to correct the errors in the function code.
I could assign an object of cStringIO type to sys.stderr at Python
command prompt as below:
>>> import sys
>>> import cStringIO
>>> obj=cStringIO.StringIO()
>>> sys.stderr=obj

And then using the obj.getvalue(), I could print exceptions printed to
sys.stderr.

But, I am not able to figure out, how can I do this same thing in
Python/C API in my program using Py* functions?

How do we set the "sys.stderr" to cStringIO object from Python
embedded in C++ or C?

Should I be using PyFile_FromFile for convert the cStringIO object?
Please help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Import of cStringIO failing with "undefined symbol: PyObject_SelfIter" on python-2.3.3-88.9

2008-01-10 Thread grbgooglefan
I am importing cStringIO module in my PythonC++ embedded program.

The import is failing with the following error:
ImportError: /usr/lib/python2.3/lib-dynload/cStringIO.so: undefined
symbol: PyObject_SelfIter

I have python-2.3.3-88.9.x86 installed on my machine.
Why is this error coming? how can I resolve this undefined symbol?
Do I need to import anything before this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get string printed by PyErr_Print( )?

2008-01-09 Thread grbgooglefan
On Dec 19 2007, 5:55 pm, Christian Heimes <[EMAIL PROTECTED]> wrote:
> grbgooglefan wrote:
> > PythonC API functionPyErr_Print( ) prints an error string onto stderr
> > if PyErr_Occurred() is true.
> > I don't want to print this to stderr because my Python+C code is
> > running daemon mode & won't have terminal / stderr.
> > So, I want to retrieve the string whichPyErr_Print( ) will print.
> > E.g.,PyErr_Print() printed following string when I tried to call
> > setTuple with one extra argument
> > Traceback (most recent call last):
> >   File "", line 2, in isTacticSafe
> > IndexError: tuple assignment index out of range
>
> I suggest a different approach. A daemon must have a stdin, stdout and
> stderr connected to a terminal. You can use freopen() to redirect stderr
> and stdout to a log file and fclose() to close stdin.
>
> http://www.gnu.org/software/libc/manual/html_mono/libc.html#Opening-S...
>
> Christian

I do not want to redirect anything to file. Because I do not want to
avoid the disk access completely - for reading as well as writing.
I liked the 1st option suggested by Robert Kern.

Can you please explain a little bit how can I replace sys.stderr with
StringIO or my char* buffer?
I have to show the error message of Python code compilation &
execution to the user on the GUI & therefore I want to capture the
error message directly instead of logging it to file.

Thanks in advance for all your help.
-- 
http://mail.python.org/mailman/listinfo/python-list


Python modules - how to create & which are better

2008-01-08 Thread grbgooglefan
I have embedded Python in my C++ application & creating Python
function from the expression. I am then evaluating those Python
compiled function (byte code) using PyObject_CallObject.

I want to create a Python module which will have functions called by
my user defined functions from the embedded Python interpreter.

What will be best approach in this case? Should I be creating
normal .py modules or .so modules (python extended with C code)?
For me, most important is the executiong speed of the code in these
modules. I want them to be fast, as those will be executed lot many
times & in time bound fashion.

Any suggestions on this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyObject_CallObject code dump after calling 4 times

2008-01-03 Thread grbgooglefan
On Jan 3, 8:49 pm, grbgooglefan <[EMAIL PROTECTED]> wrote:
> On Jan 3, 8:02 pm, Phil Thompson <[EMAIL PROTECTED]>
> wrote:
>
>
>
>
>
> > On Thursday 03 January 2008, grbgooglefan wrote:
>
> > > I have a following C++ code which uses PyObject_CallObject to evaluate
> > > expressions dynamically. This code sets the input parameters for the
> > > function also dynamically. After calling this function 4 times (with
> > > these shown values), PyObject_CallObject  causes application to crash
> > > in frame_dealloc.
> > > 1) Can some one please help me understand why is this crash happening
> > > in frame_dealloc & how to solve it?
> > > 2) Is there anything wrong I am doing about incrementing or
> > > decrementing the reference counts of the object passed to
> > > PyObject_CallObject?
>
> > Yes.
>
> > > 3) Is it because of the big value (2299265.50) I am trying to
> > > convert from double to float using PyFloat_FromDouble?
>
> > > //= code reduced for readability
> > > ===
> > > switch(ndtyp){
> > >      case(INT_T):
> > >          {
> > >          printf("PyInt_FromLong val %d, var %s
> > > \n",inputVar.nionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> > >          val = PyInt_FromLong(inputVar.nionum);
> > >          break;
> > >          }
> > >       case(LONG_T):
> > >          {
> > >          printf("PyLong_FromLong val %ld, var %s
> > > \n",inputVar.lionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> > >          val = PyLong_FromLong(inputVar.lionum);
> > >          break;
> > >          }
> > >       case(FLOAT_T):
> > >          {
> > >          printf("PyFloat_FromDouble val %f, var %s
> > > \n",inputVar.fionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> > >          val = PyFloat_FromDouble(inputVar.fionum);
> > >          break;
> > >          }
> > >       case(DOUBLE_T):
> > >          {
> > >          printf("PyFloat_FromDouble val %f, var %s
> > > \n",inputVar.dionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> > >          val = PyFloat_FromDouble(inputVar.dionum);
> > >          break;
> > >          }
> > >        case(STRING_T):
> > >          {
> > >          printf("PyString_FromString val %s, var %s
> > > \n",inputVar.ioString,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> > >          val = PyString_FromString(inputVar.ioString);
> > >          break;
> > >          }
> > >        default:
> > >          printf("Unknown data type [%d] for %s\n",ndtyp,pEvalFunc-
>
> > > >pExprVarsArray[nCtr].szVarName);
>
> > > }
> > > if(!val){
> > >    ret = -1;
> > >    printf("Failed to convert %d %s to PyObject\n",ndtyp,pEvalFunc-
>
> > > >pExprVarsArray[nCtr].szVarName); fflush(stdout);
>
> > >    Py_XDECREF(pTuple);
> > >    break;
> > > }
> > >   PyTuple_SetItem(pTuple, nCtr, val);
> > >   Py_XDECREF(val);
>
> > Don't do this - PyTuple_SetItem() steals a reference to val.
>
> > > }
> > > // all variables are set, call Python function
> > > Py_XINCREF(pTuple);
>
> > Why do this?
>
> > >   PyObject *pResult = PyObject_CallObject(pEvalFunc-
>
> > > >pPyEvalFunction,pTuple);
>
> > > Py_XDECREF(pTuple);
>
> > Why do this?
>
> > > if(PyErr_Occurred()){
> > >  PyErr_Print();
> > > } else {
> > >       char* plevel = NULL;
> > >       if(NULL != (plevel = PyString_AsString(pResult))){
> > >         ret = 0;
> > >         sprintf(szEvalResult,"%s",plevel);
> > >       }
> > > }
> > > Py_XDECREF(pResult);
>
> > pTuple will now have the same number of references as when you started the
> > above code, so you may want to Py_DECREF() it.
>
> > Phil- Hide quoted text -
>
> > - Show quoted text -
>
> Thanks for all the responses.
> These help me.
> I could simulate this crash in my small test program & I think (I
> could be wrong also) it is because of extraneous Py_XDECREF of
> "PyObject *val" which I am using to convert variables to tuple.
> When I change the code to simple do like this, it works fine.
>             PyTuple_SetItem(pytuple,0,PyLong_FromLong(size));
>    

Re: PyObject_CallObject code dump after calling 4 times

2008-01-03 Thread grbgooglefan
On Jan 3, 8:02 pm, Phil Thompson <[EMAIL PROTECTED]>
wrote:
> On Thursday 03 January 2008, grbgooglefan wrote:
>
> > I have a following C++ code which uses PyObject_CallObject to evaluate
> > expressions dynamically. This code sets the input parameters for the
> > function also dynamically. After calling this function 4 times (with
> > these shown values), PyObject_CallObject  causes application to crash
> > in frame_dealloc.
> > 1) Can some one please help me understand why is this crash happening
> > in frame_dealloc & how to solve it?
> > 2) Is there anything wrong I am doing about incrementing or
> > decrementing the reference counts of the object passed to
> > PyObject_CallObject?
>
> Yes.
>
>
>
>
>
> > 3) Is it because of the big value (2299265.50) I am trying to
> > convert from double to float using PyFloat_FromDouble?
>
> > //= code reduced for readability
> > ===
> > switch(ndtyp){
> >      case(INT_T):
> >          {
> >          printf("PyInt_FromLong val %d, var %s
> > \n",inputVar.nionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> >          val = PyInt_FromLong(inputVar.nionum);
> >          break;
> >          }
> >       case(LONG_T):
> >          {
> >          printf("PyLong_FromLong val %ld, var %s
> > \n",inputVar.lionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> >          val = PyLong_FromLong(inputVar.lionum);
> >          break;
> >          }
> >       case(FLOAT_T):
> >          {
> >          printf("PyFloat_FromDouble val %f, var %s
> > \n",inputVar.fionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> >          val = PyFloat_FromDouble(inputVar.fionum);
> >          break;
> >          }
> >       case(DOUBLE_T):
> >          {
> >          printf("PyFloat_FromDouble val %f, var %s
> > \n",inputVar.dionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> >          val = PyFloat_FromDouble(inputVar.dionum);
> >          break;
> >          }
> >        case(STRING_T):
> >          {
> >          printf("PyString_FromString val %s, var %s
> > \n",inputVar.ioString,pEvalFunc->pExprVarsArray[nCtr].szVarName);
> >          val = PyString_FromString(inputVar.ioString);
> >          break;
> >          }
> >        default:
> >          printf("Unknown data type [%d] for %s\n",ndtyp,pEvalFunc-
>
> > >pExprVarsArray[nCtr].szVarName);
>
> > }
> > if(!val){
> >    ret = -1;
> >    printf("Failed to convert %d %s to PyObject\n",ndtyp,pEvalFunc-
>
> > >pExprVarsArray[nCtr].szVarName); fflush(stdout);
>
> >    Py_XDECREF(pTuple);
> >    break;
> > }
> >   PyTuple_SetItem(pTuple, nCtr, val);
> >   Py_XDECREF(val);
>
> Don't do this - PyTuple_SetItem() steals a reference to val.
>
> > }
> > // all variables are set, call Python function
> > Py_XINCREF(pTuple);
>
> Why do this?
>
> >   PyObject *pResult = PyObject_CallObject(pEvalFunc-
>
> > >pPyEvalFunction,pTuple);
>
> > Py_XDECREF(pTuple);
>
> Why do this?
>
> > if(PyErr_Occurred()){
> >  PyErr_Print();
> > } else {
> >       char* plevel = NULL;
> >       if(NULL != (plevel = PyString_AsString(pResult))){
> >         ret = 0;
> >         sprintf(szEvalResult,"%s",plevel);
> >       }
> > }
> > Py_XDECREF(pResult);
>
> pTuple will now have the same number of references as when you started the
> above code, so you may want to Py_DECREF() it.
>
> Phil- Hide quoted text -
>
> - Show quoted text -

Thanks for all the responses.
These help me.
I could simulate this crash in my small test program & I think (I
could be wrong also) it is because of extraneous Py_XDECREF of
"PyObject *val" which I am using to convert variables to tuple.
When I change the code to simple do like this, it works fine.
PyTuple_SetItem(pytuple,0,PyLong_FromLong(size));
PyTuple_SetItem(pytuple,1,PyLong_FromLong(maxvol));
PyTuple_SetItem(pytuple,2,PyFloat_FromDouble(adv));
-- 
http://mail.python.org/mailman/listinfo/python-list


PyObject_CallObject code dump after calling 4 times

2008-01-03 Thread grbgooglefan
I have a following C++ code which uses PyObject_CallObject to evaluate
expressions dynamically. This code sets the input parameters for the
function also dynamically. After calling this function 4 times (with
these shown values), PyObject_CallObject  causes application to crash
in frame_dealloc.
1) Can some one please help me understand why is this crash happening
in frame_dealloc & how to solve it?
2) Is there anything wrong I am doing about incrementing or
decrementing the reference counts of the object passed to
PyObject_CallObject?
3) Is it because of the big value (2299265.50) I am trying to
convert from double to float using PyFloat_FromDouble?

//= code reduced for readability
===
switch(ndtyp){
 case(INT_T):
 {
 printf("PyInt_FromLong val %d, var %s
\n",inputVar.nionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
 val = PyInt_FromLong(inputVar.nionum);
 break;
 }
  case(LONG_T):
 {
 printf("PyLong_FromLong val %ld, var %s
\n",inputVar.lionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
 val = PyLong_FromLong(inputVar.lionum);
 break;
 }
  case(FLOAT_T):
 {
 printf("PyFloat_FromDouble val %f, var %s
\n",inputVar.fionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
 val = PyFloat_FromDouble(inputVar.fionum);
 break;
 }
  case(DOUBLE_T):
 {
 printf("PyFloat_FromDouble val %f, var %s
\n",inputVar.dionum,pEvalFunc->pExprVarsArray[nCtr].szVarName);
 val = PyFloat_FromDouble(inputVar.dionum);
 break;
 }
   case(STRING_T):
 {
 printf("PyString_FromString val %s, var %s
\n",inputVar.ioString,pEvalFunc->pExprVarsArray[nCtr].szVarName);
 val = PyString_FromString(inputVar.ioString);
 break;
 }
   default:
 printf("Unknown data type [%d] for %s\n",ndtyp,pEvalFunc-
>pExprVarsArray[nCtr].szVarName);
}
if(!val){
   ret = -1;
   printf("Failed to convert %d %s to PyObject\n",ndtyp,pEvalFunc-
>pExprVarsArray[nCtr].szVarName); fflush(stdout);
   Py_XDECREF(pTuple);
   break;
}
  PyTuple_SetItem(pTuple, nCtr, val);
  Py_XDECREF(val);
}
// all variables are set, call Python function
Py_XINCREF(pTuple);
  PyObject *pResult = PyObject_CallObject(pEvalFunc-
>pPyEvalFunction,pTuple);
Py_XDECREF(pTuple);

if(PyErr_Occurred()){
 PyErr_Print();
} else {
  char* plevel = NULL;
  if(NULL != (plevel = PyString_AsString(pResult))){
ret = 0;
sprintf(szEvalResult,"%s",plevel);
  }
}
Py_XDECREF(pResult);

Following crash (back trace) appears when I run this in GDB. The
expression that was getting evaluated at this time is:
def DangerousQuantity(size,maxvol,adv):
 if((size<1000 and (maxvol<1) and (size<0.001*adv))):
   return "Dangerous"
 else:
   return "FAIL"

//--- Crash dump information & values of variables passed
to this expression
Categorizing the order
pyParserEvaluator evaluating category function DangerousTactic
PyString_FromString val R, var aestactic
PyLong_FromLong val 1139, var endtime
PyLong_FromLong val 599, var starttime
PyLong_FromLong val 23, var Aggr
PyObject_CallObject done, do Py_XDECREF-pTuple
Final result FAIL
doing Py_XDECREF(pResult
pyParserEvaluator evaluating category function MediumTactic
PyString_FromString val R, var aestactic
PyLong_FromLong val 1139, var endtime
PyLong_FromLong val 599, var starttime
PyLong_FromLong val 23, var Aggr
PyObject_CallObject done, do Py_XDECREF-pTuple
Final result FAIL
doing Py_XDECREF(pResult
pyParserEvaluator evaluating category function SafeTactic
PyString_FromString val R, var aestactic
PyLong_FromLong val 1139, var endtime
PyLong_FromLong val 599, var starttime
PyLong_FromLong val 23, var Aggr
PyObject_CallObject done, do Py_XDECREF-pTuple
Final result FAIL
doing Py_XDECREF(pResult
pyParserEvaluator evaluating category function DangerousQuantity
PyLong_FromLong val 1, var size
PyLong_FromLong val 0, var maxvol
PyFloat_FromDouble val 2299265.50, var adv

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 82336688 (LWP 27652)]
0xc000 in ?? ()
(gdb) where
#0  0xc000 in ?? ()
#1  0x0285e59e in frame_dealloc (f=0xf5a2f68c) at Objects/
frameobject.c:106
#2  0x0281a4b1 in PyEval_EvalCodeEx (co=0xf5a69990, globals=0x2884120,
locals=0x0, args=0x288bca0, argcount=3, kws=0x0, kwcount=0,
defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:2609
#3  0x0285f551 in function_call (func=0xf5a694c4, arg=0xf5a47c3c,
kw=0x0) at Objects/funcobject.c:476
#4  0x027e1e04 in PyObject_Call (func=0xf5a2003c, arg=0xf5a47c3c,
kw=0x0) at Objects/abstract.c:1688
#5  0x0281b3eb in PyEval_CallObjectWithKeywords (func=0xf5a694c4,
arg=0xf5a47c3c, kw=0x0) at Python/ceval.c:3058
#6  0x027e1de3 in PyObject_CallObject (o=0xf5a694c4, a=0xf5a47c3c) at
Objects/abstract.c:1679
#7  0x027dd6fd in pyParser

how to get string printed by PyErr_Print( )?

2007-12-19 Thread grbgooglefan
PythonC API function PyErr_Print( ) prints an error string onto stderr
if PyErr_Occurred() is true.
I don't want to print this to stderr because my Python+C code is
running daemon mode & won't have terminal / stderr.
So, I want to retrieve the string which PyErr_Print( ) will print.
E.g., PyErr_Print() printed following string when I tried to call
setTuple with one extra argument
Traceback (most recent call last):
  File "", line 2, in isTacticSafe
IndexError: tuple assignment index out of range

How do I get this error message in a local char* & use it for further
error handling?

Also, is there any way to get an error number associated for these
error conditions using some getError function on the object returned
by PyErr_Occurred()?

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


releasing the reference returned by PyLong_FromLong, PyString_FromString & friends

2007-12-13 Thread grbgooglefan
I am having a object tuple created at application startup. This tuple
I pass on to a python function in call to:
PyObject_CallObject(pcatInfo->pPyEvalFunction,pTuple);

For setting the values in this tuple, I am using PyLong_FromLong,
PyString_FromString & friends functions.
PyTuple_SetItem(pTuple,nCtr,PyString_FromString(szOrdValue));

My worry is that I will be doing this operation lot many times in the
active duration of my application. During this time I do not want to
delete the tuple (pTuple) object passed to PyObject_CallObject, to
avoid the time taken for creating the object every time.

But this poses another problem. Every call I make to PyLong_FromLong,
PyString_FromString & friends & functions, they return references.
How do we release the references or objects returned by these
functions?
What is the optimum strategy in this?
Please guide.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Erros when compiling a CPP program which uses CPython API functions

2007-12-07 Thread grbgooglefan
On Dec 7, 5:15 pm, Christian Heimes <[EMAIL PROTECTED]> wrote:
> grbgooglefan wrote:
> > I am compiling CPP program which uses CPython API functions from
> > Python 2.5.1 source code
>
> > First I compiled with this commanline, that time I got "pyconfig.h"
> > not found.
>
> On Unix you have to run ./configure and make before you can use a source
> tree. It creates a valid pyconfig.h for you. You may want to use the
> ./configure option --enable-shared.
>
> > So, I added "-I../../PC" to my cmd line to get pyconfig.h, like:
>
> PC/pyconfig.h is for Windows only!
>
> Christian

Thanks for this guidance.
I just now compiled Python 2.5.1 in my home directory. The Linux
machine has Python 2.2.3 already installed.
When I run my above mentioned program which is linked with
libpython2.5.1.a, I get an error as below:

[EMAIL PROTECTED]:~/prgs/bakup/Python-2.5.1/Demo/embed>./a.out ~/pxm/
parserroot/
'import site' failed; use -v for traceback
['/home/gborse/prgs/bakup/Python-2.5.1/lib/python25.zip', '/home/
gborse/prgs/bakup/Python-2.5.1/lib/python2.5/', '/home/gborse/prgs/
bakup/Python-2.5.1/lib/python2.5/plat-linux2', '/home/gborse/prgs/
bakup/Python-2.5.1/lib/python2.5/lib-tk', '/home/gborse/prgs/bakup/
Python-2.5.1/lib/python2.5/lib-dynload']

I have set the PYTHONHOME to /home/gborse/prgs/bakup/Python-2.5.1 and
PYTHON_LIB to /home/gborse/prgs/bakup/Python-2.5.1/Lib/

But if I run this simialr program compiled with Python2.2.3, it does
not give this error of "import site" failed.
I don't understand how come the "python2.5" is getting appended to
PYTHON_LIB when this "import site" error is coming?

How do we resolve this error?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Erros when compiling a CPP program which uses CPython API functions

2007-12-07 Thread grbgooglefan
On Dec 7, 2:17 pm, grbgooglefan <[EMAIL PROTECTED]> wrote:
> On Dec 7, 12:32 pm, grbgooglefan <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > I am compiling CPP program which uses CPython API functions from
> > Python 2.5.1 source code
>
> > First I compiled with this commanline, that time I got "pyconfig.h"
> > not found.
> > g++ -Os -I../../Include ../../libpython2.5.a -lm -ldl -lpthread -lutil
> > testeval.cpp
> > In file included from testeval.cpp:1:
> > ../../Include/Python.h:8:22: pyconfig.h: No such file or directory
> > In file included from ../../Include/Python.h:57,  from
> > testeval.cpp:1:
> > ../../Include/pyport.h:4:48: pyconfig.h: No such file or directory
> > ../../Include/pyport.h:734:2: #error "LONG_BIT definition appears
> > wrong for platform (bad gcc/glibc config?)."
>
> > So, I added "-I../../PC" to my cmd line to get pyconfig.h, like:
> > g++ -Os -I../../Include -I../../PC/ ../../libpython2.5.a -lm -ldl -
> > lpthread -lutil testeval.cpp
>
> > As soon as I ran this command I got a long list of errors as shown
> > below.
> > Why the dllimport errors are coming? I did not get these errors when I
> > did same compilation with Python 2.2.3.
> > Please help. Thanks.
>
> > In file included from ../../Include/Python.h:76,  from
> > testeval.cpp:1:
> > ../../Include/pymem.h:50: `dllimport' was not declared in this scope
> > ../../Include/pymem.h:50: ISO C++ forbids declaration of `__declspec'
> > with notype
> > ../../Include/pymem.h:50: syntax error before `void'
> > ../../Include/pymem.h:51: `dllimport' was not declared in this scope
> > ../../Include/pymem.h:51: ISO C++ forbids declaration of `__declspec'
> > with notype
> > ../../Include/pymem.h:51: redefinition of `int __declspec'
> > ../../Include/pymem.h:50: `int __declspec' previously defined here
> > ../../Include/pymem.h:51: syntax error before `void'
> > ../../Include/pymem.h:52: `dllimport' was not declared in this scope
> > ../../Include/pymem.h:52: ISO C++ forbids declaration of `__declspec'
> > with notype
> > ../../Include/pymem.h:52: redefinition of `int __declspec'
> > ../../Include/pymem.h:51: `int __declspec' previously defined here
> > ../../Include/pymem.h:52: syntax error before `void'
> > In file included from ../../Include/Python.h:78,  from
> > testeval.cpp:1:
> > ../../Include/object.h:371: `dllimport' was not declared in this scope
> > ../../Include/object.h:371: ISO C++ forbids declaration of
> > `__declspec' with notype
> > ../../Include/object.h:371: redefinition of `int __declspec'
> > ..
> > ../../Include/Python.h:134: redefinition of `int __declspec'
> > ../../Include/pystrtod.h:11: `int __declspec' previously defined here
> > ../../Include/Python.h:134: syntax error before `*' token
> > testeval.cpp: In function `int main(int, char**)':
> > testeval.cpp:94: `Py_Initialize' undeclared (first use this function)
> > testeval.cpp:94: (Each undeclared identifier is reported only once for
> > each
> >function it appears in.)
> > testeval.cpp:95: `PyRun_SimpleStringFlags' undeclared (first use this
> > function)
>
> I am doing this on following Linux OS config:
> Linux njl36a-7003 2.4.21-9.0.1.ELsmp #1 SMP Mon Feb 9 22:26:51 EST
> 2004 i686 i686 i386 GNU/Linux>cat /etc/redhat-release
>
> Red Hat Enterprise Linux AS release 3 (Taroon Update 1)- Hide quoted text -
>
> - Show quoted text -

This is resolved when I used the pyconfig.h file not from Python-2.5.1/
PC/pyconfig.h but the top level which is Python-2.5.1/pyconfig.h
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I embed Windows Python in C# or VC++?

2007-12-07 Thread grbgooglefan
On Dec 7, 3:07 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> En Fri, 07 Dec 2007 01:24:57 -0300, grbgooglefan <[EMAIL PROTECTED]>  
> escribió:
>
>
>
>
>
> > On Dec 7, 12:17 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
> > wrote:
> >> En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <[EMAIL PROTECTED]>
> >> escribió:
>
> >> > I want to use Python's Windows (python25.dll) version to embed in my
> >> > C# (or atleast VC++) program for performing syntax checks on the
> >> > Python expressions which are later supposed to be evaluated at runtime
> >> > by another C++ program [...]> Can I start doing the development using  
> >> the include, lib & the
> >> > python25.dll files availale after installing this MSI?
>
> >> Yes. You don't require the source package to embed Python and use the  
> >> API in your programs.
>
> > Does it mean, I can embed Python in C# as well with the same APIs?
>
> No; you can use the Python API in a native C++ application (the Python  
> code is plain C, but all the include files have the 'extern "C" {}'  
> declarations). For .NET there are IronPython and PythonNet, but I cannot  
> comment on them, surely someone else may help. See  
> http://www.python.org/about/
>
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -

Hello, Anybody else out there has used Python from C#?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: weird embedding problem

2007-12-07 Thread grbgooglefan
On Dec 7, 2:01 pm, DavidM <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I'm embedding python in a C prog which is built as a linux shared lib.
>
> The prog is linked against libpython, and on startup, it calls
> Py_Initialize().
>
> The prog imports a pure-python script. The script starts up ok, but when
> it imports the 'math' module, the import fails with:
>
> Traceback (most recent call last):
>   File "/home/david/work/video/myprogs/dvedit/test/frei0rfx1.py", line 10, in 
> 
> import math
> ImportError: /usr/lib/python2.5/lib-dynload/math.so: undefined symbol: 
> PyExc_ValueError
> Failed to import math
>
> Any ideas of how to work around this?
>
> Please note - the program I'm writing *must* be built as a shared lib, so
> the usual practice of 'extend, don't embed' is just not an option here.
>
> Thoughts?
>
> Cheers
> D

the math module above is failing to load because of: "undefined
symbol: PyExc_ValueError"
You may have to import some other module before loading math.
Try doing nm on libraries in /usr/lib/python2.5/lib-dynload & see
which one has this symbol (a function) in defined state ("T").

Also, verify if your script works fine out of the C program - may be
at python prompt.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Erros when compiling a CPP program which uses CPython API functions

2007-12-06 Thread grbgooglefan
On Dec 7, 12:32 pm, grbgooglefan <[EMAIL PROTECTED]> wrote:
> I am compiling CPP program which uses CPython API functions from
> Python 2.5.1 source code
>
> First I compiled with this commanline, that time I got "pyconfig.h"
> not found.
> g++ -Os -I../../Include ../../libpython2.5.a -lm -ldl -lpthread -lutil
> testeval.cpp
> In file included from testeval.cpp:1:
> ../../Include/Python.h:8:22: pyconfig.h: No such file or directory
> In file included from ../../Include/Python.h:57,  from
> testeval.cpp:1:
> ../../Include/pyport.h:4:48: pyconfig.h: No such file or directory
> ../../Include/pyport.h:734:2: #error "LONG_BIT definition appears
> wrong for platform (bad gcc/glibc config?)."
>
> So, I added "-I../../PC" to my cmd line to get pyconfig.h, like:
> g++ -Os -I../../Include -I../../PC/ ../../libpython2.5.a -lm -ldl -
> lpthread -lutil testeval.cpp
>
> As soon as I ran this command I got a long list of errors as shown
> below.
> Why the dllimport errors are coming? I did not get these errors when I
> did same compilation with Python 2.2.3.
> Please help. Thanks.
>
> In file included from ../../Include/Python.h:76,  from
> testeval.cpp:1:
> ../../Include/pymem.h:50: `dllimport' was not declared in this scope
> ../../Include/pymem.h:50: ISO C++ forbids declaration of `__declspec'
> with notype
> ../../Include/pymem.h:50: syntax error before `void'
> ../../Include/pymem.h:51: `dllimport' was not declared in this scope
> ../../Include/pymem.h:51: ISO C++ forbids declaration of `__declspec'
> with notype
> ../../Include/pymem.h:51: redefinition of `int __declspec'
> ../../Include/pymem.h:50: `int __declspec' previously defined here
> ../../Include/pymem.h:51: syntax error before `void'
> ../../Include/pymem.h:52: `dllimport' was not declared in this scope
> ../../Include/pymem.h:52: ISO C++ forbids declaration of `__declspec'
> with notype
> ../../Include/pymem.h:52: redefinition of `int __declspec'
> ../../Include/pymem.h:51: `int __declspec' previously defined here
> ../../Include/pymem.h:52: syntax error before `void'
> In file included from ../../Include/Python.h:78,  from
> testeval.cpp:1:
> ../../Include/object.h:371: `dllimport' was not declared in this scope
> ../../Include/object.h:371: ISO C++ forbids declaration of
> `__declspec' with notype
> ../../Include/object.h:371: redefinition of `int __declspec'
> ..
> ../../Include/Python.h:134: redefinition of `int __declspec'
> ../../Include/pystrtod.h:11: `int __declspec' previously defined here
> ../../Include/Python.h:134: syntax error before `*' token
> testeval.cpp: In function `int main(int, char**)':
> testeval.cpp:94: `Py_Initialize' undeclared (first use this function)
> testeval.cpp:94: (Each undeclared identifier is reported only once for
> each
>function it appears in.)
> testeval.cpp:95: `PyRun_SimpleStringFlags' undeclared (first use this
> function)

I am doing this on following Linux OS config:
Linux njl36a-7003 2.4.21-9.0.1.ELsmp #1 SMP Mon Feb 9 22:26:51 EST
2004 i686 i686 i386 GNU/Linux
>cat /etc/redhat-release
Red Hat Enterprise Linux AS release 3 (Taroon Update 1)
-- 
http://mail.python.org/mailman/listinfo/python-list


Erros when compiling a CPP program which uses CPython API functions

2007-12-06 Thread grbgooglefan
I am compiling CPP program which uses CPython API functions from
Python 2.5.1 source code

First I compiled with this commanline, that time I got "pyconfig.h"
not found.
g++ -Os -I../../Include ../../libpython2.5.a -lm -ldl -lpthread -lutil
testeval.cpp
In file included from testeval.cpp:1:
../../Include/Python.h:8:22: pyconfig.h: No such file or directory
In file included from ../../Include/Python.h:57,  from
testeval.cpp:1:
../../Include/pyport.h:4:48: pyconfig.h: No such file or directory
../../Include/pyport.h:734:2: #error "LONG_BIT definition appears
wrong for platform (bad gcc/glibc config?)."

So, I added "-I../../PC" to my cmd line to get pyconfig.h, like:
g++ -Os -I../../Include -I../../PC/ ../../libpython2.5.a -lm -ldl -
lpthread -lutil testeval.cpp

As soon as I ran this command I got a long list of errors as shown
below.
Why the dllimport errors are coming? I did not get these errors when I
did same compilation with Python 2.2.3.
Please help. Thanks.

In file included from ../../Include/Python.h:76,  from
testeval.cpp:1:
../../Include/pymem.h:50: `dllimport' was not declared in this scope
../../Include/pymem.h:50: ISO C++ forbids declaration of `__declspec'
with notype
../../Include/pymem.h:50: syntax error before `void'
../../Include/pymem.h:51: `dllimport' was not declared in this scope
../../Include/pymem.h:51: ISO C++ forbids declaration of `__declspec'
with notype
../../Include/pymem.h:51: redefinition of `int __declspec'
../../Include/pymem.h:50: `int __declspec' previously defined here
../../Include/pymem.h:51: syntax error before `void'
../../Include/pymem.h:52: `dllimport' was not declared in this scope
../../Include/pymem.h:52: ISO C++ forbids declaration of `__declspec'
with notype
../../Include/pymem.h:52: redefinition of `int __declspec'
../../Include/pymem.h:51: `int __declspec' previously defined here
../../Include/pymem.h:52: syntax error before `void'
In file included from ../../Include/Python.h:78,  from
testeval.cpp:1:
../../Include/object.h:371: `dllimport' was not declared in this scope
../../Include/object.h:371: ISO C++ forbids declaration of
`__declspec' with notype
../../Include/object.h:371: redefinition of `int __declspec'
..
../../Include/Python.h:134: redefinition of `int __declspec'
../../Include/pystrtod.h:11: `int __declspec' previously defined here
../../Include/Python.h:134: syntax error before `*' token
testeval.cpp: In function `int main(int, char**)':
testeval.cpp:94: `Py_Initialize' undeclared (first use this function)
testeval.cpp:94: (Each undeclared identifier is reported only once for
each
   function it appears in.)
testeval.cpp:95: `PyRun_SimpleStringFlags' undeclared (first use this
function)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I embed Windows Python in C# or VC++?

2007-12-06 Thread grbgooglefan
On Dec 7, 12:17 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 06 Dec 2007 23:27:15 -0300, grbgooglefan <[EMAIL PROTECTED]>  
> escribió:
>
> > I want to use Python's Windows (python25.dll) version to embed in my
> > C# (or atleast VC++) program for performing syntax checks on the
> > Python expressions which are later supposed to be evaluated at runtime
> > by another C++ program
>
> > For this, I would like to use CPython API functions such as
> > Py_Initialize, PyModule_New, PyModule_GetDict, PyRun_String,
> > PyObject_GetAttrString, PyObject_CallObject, PyTuple_SetItem & other
> > similar functions from my C#/ VC++ program on Windows.
>
> > I have installed Python251.msi on my Desktop.
> > Can I start doing the development using the include, lib & the
> > python25.dll files availale after installing this MSI?
>
> Yes. You don't require the source package to embed Python and use the API  
> in your programs.
>
> --
> Gabriel Genellina

Does it mean, I can embed Python in C# as well with the same APIs?
-- 
http://mail.python.org/mailman/listinfo/python-list


Can I embed Windows Python in C# or VC++?

2007-12-06 Thread grbgooglefan
I want to use Python's Windows (python25.dll) version to embed in my
C# (or atleast VC++) program for performing syntax checks on the
Python expressions which are later supposed to be evaluated at runtime
by another C++ program

For this, I would like to use CPython API functions such as
Py_Initialize, PyModule_New, PyModule_GetDict, PyRun_String,
PyObject_GetAttrString, PyObject_CallObject, PyTuple_SetItem & other
similar functions from my C#/ VC++ program on Windows.

I have installed Python251.msi on my Desktop.
Can I start doing the development using the include, lib & the
python25.dll files availale after installing this MSI?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on compiled code when embedding Python in C++

2007-12-03 Thread grbgooglefan
On Dec 4, 12:36 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Tue, 04 Dec 2007 00:54:57 -0300, Borse, Ganesh  
> <[EMAIL PROTECTED]> escribió:
>
> > When we compile a Python code using functions such as Py_CompileString  
> > or any other similar compile function, what will be the resulting  
> > executable code?
> > Will it be byte code somewhat like the byte code of Java?
> > Or it will be the same binary code like the once generated by  
> > C/C++ compilers from the source code?
>
> For CPython, the resulting bytecode consist of instructions for a virtual  
> machine, like Java.
> I believe Jython compiles Python code into Java bytecode and executes  
> using the Java VM.
>
> > Are these code evaluations recommended for the performance intensive  
> > actions in C++ program?
>
> If you only care about speed, maybe Python isn't the right tool to use in  
> the first place.
>
> There are some performance tips here:  
>  and you can use  
> tools like Psyco, Pyrex and ShedSkin to improve performance dramatically.
>
> --
> Gabriel Genellina

Thanks for these tips. These are eye openers.
The link (http://wingware.com/doc/howtos/performance-profiling-python-
code) given on wiki not accessible.
Anybody who knows updating or correct link, can you please mind
correcting it on ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the function to evaluate code object returned byPyParser _SimpleParseString function?

2007-11-21 Thread grbgooglefan
On Nov 16, 7:11 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote:
> "Borse, Ganesh" <[EMAIL PROTECTED]> wrote in message
>
> news:[EMAIL PROTECTED]
> | Py_CompileString takes the sourcecodefrom file, isn't it?
>
> No.
>
> | As can be seen from the syntax of thisfunction: "PyObject*
> Py_CompileString(char *str, char *filename, int start)"
>
> I am rather sure that the filename param is the same as for the
> Python-level builtinfunctioncompile:
>   compile( string, filename, kind[, flags[, dont_inherit]])
>
> Compile the string into acodeobject.Codeobjects can be executed by an
> exec statement or evaluated by acallto eval(). The filename argument
> should give the file from which thecodewas read; pass some recognizable
> value if it wasn't read from a file ('' is commonly used).
>
> The filename is used for exception traceback messages.

Hi,
I want to put this code in a function & call it.
I am now facing a problem that, I am able to compile a function using
Py_CompileString. That generates a "*definition*" of that function.
I am stuck at a road block & not able to execute this function
definition which is part of "*codeObject*" returned by
Py_CompileString.

What should I do to transform this 'code' object to 'callable' or
'function' object, so that I can call it?

Please help.
Regards.
-- 
http://mail.python.org/mailman/listinfo/python-list


[ Please help ] how to create Python functions in C++ at runtime & call them

2007-11-20 Thread grbgooglefan
I want to compile following type of python function in my C++ program
at runtime.
def isSizeSmall(size,vol,ADV,prod):
   if ( (size < 1000) & (vol < (0.001 * ADV)) & (prod=="Stock")):
print "OK"; return 10
   else: print "NOK"; return 11

Using Py_CompileString, I compiled a code object from this function,
like:
   char szExpr[2048];
   sprintf(szExpr,"def isSizeSmall(size,vol,ADV,prod):\n  if ( (size <
1000) & (vol < (0.001 * ADV)) & (prod==\"Stock\")): print \"OK\";
return 10\n  else: print \"NOK\"; return 11\n\n\n");

   PyObject* result = Py_CompileString(szExpr,"",
Py_file_input);

Then, I tried to call this function in 3 different ways: 1)
PyEval_EvalCode, 2) PyObject_CallObject.
But both failed.

For using PyObject_CallObject, I did something like this.

   PyObject* tuple = PyTuple_New(4);
   PyObject* val = 0;

   val = PyInt_FromLong(ordval.size);
   PyTuple_SetItem(tuple,0,val);

   val = PyInt_FromLong(ordval.vol);
   PyTuple_SetItem(tuple,1,val);

   val = PyInt_FromLong(ordval.ADV);
   PyTuple_SetItem(tuple,2,val);

   val = PyString_FromString(ordval.prod);
   PyTuple_SetItem(tuple,3,val);

   PyObject *glb = PyDict_New();
   PyDict_SetItemString(glb, "__builtins__", PyEval_GetBuiltins());

 PyObject* func = PyFunction_New(result,glb);
 if(!func || PyErr_Occurred()){
   printf("Failed to get Function..\n");
   PyErr_Print();
 } else {
 printf("Calling PyObject_CallObject\n");
 if(PyCallable_Check(func))
   printf("func is callable\n");
 PyObject* ret = PyObject_CallObject(func, tuple);
 if(!ret || PyErr_Occurred())
   PyErr_Print();
 else
   printf("PyObject_CallObject evaluated..\n");
 }

I got this output. Why is it so? Why is this function not taking any
parameters? Please help.

Expression to eval =
[def isSizeSmall(size,vol,ADV,prod):
  if ( (size < 1000) & (vol < (0.001 * ADV)) & (prod=="Stock")): print
"OK"; return 10
  else: print "NOK"; return 11

]
func is callable
TypeError: ?() takes no arguments (4 given)

Thanks for all help & guidance
-- 
http://mail.python.org/mailman/listinfo/python-list