PyObject_GetAttrString failing when getting a function pointerfr om PyObject* returned by Py_Compile

B

Borse, Ganesh

Hi,

My following code is failing with an error of "isSizeSmall not function or callable"

//---------------------------------------------------
char szExpr[2048];
memset(szExpr,'\0',sizeof(szExpr));
sprintf(szExpr,"def isSizeSmall(size,vol,ADV,prod):\n if ( (size < 1000) & (vol < (0.001 * ADV)) & (prod==\"Stock\")): return 100\n else: return 11\n\n\n");

PyObject* pyco = Py_CompileString(szExpr,"<stdin>", Py_file_input);

// Get a pointer to this function
char funcname[56];
memset(funcname,'\0',sizeof(funcname));
sprintf(funcname,"isSizeSmall");

PyObject* func = PyObject_GetAttrString(pyco,funcname);
if(!func || !PyCallable_Check(func))
printf("%s not function or callable\n");
//---------------------------------------------------

In above case, Py_CompileString parses & compiles the code successfully.

Can we not use the "PyObject*" returned by Py_CompileString as input to PyObject_GetAttrString?
What could be wrong?

I want to use this function pointer returned by PyObject_GetAttrString as input to PyObject_CallObject.
Can I use the "PyObject*" returned by Py_CompileString directly as is in call to PyObject_CallObject?

Please help.

Thanks and Regards,
Ganesh

==============================================================================
Please access the attached hyperlink for an important electronic communications disclaimer:

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==============================================================================
 
S

sndive

Hi,

My following code is failing with an error of "isSizeSmall not function or callable"

//---------------------------------------------------
char szExpr[2048];
memset(szExpr,'\0',sizeof(szExpr));
sprintf(szExpr,"def isSizeSmall(size,vol,ADV,prod):\n if ( (size < 1000) & (vol < (0.001 * ADV)) & (prod==\"Stock\")): return 100\n else: return 11\n\n\n");

PyObject* pyco = Py_CompileString(szExpr,"<stdin>", Py_file_input);

// Get a pointer to this function
char funcname[56];
memset(funcname,'\0',sizeof(funcname));
sprintf(funcname,"isSizeSmall");

PyObject* func = PyObject_GetAttrString(pyco,funcname);
if(!func || !PyCallable_Check(func))
printf("%s not function or callable\n");
//---------------------------------------------------

In above case, Py_CompileString parses & compiles the code successfully.

Can we not use the "PyObject*" returned by Py_CompileString as input to PyObject_GetAttrString?
What could be wrong?
per documentation
compile string returns None when you use Py_file_input
I want to use this function pointer returned by PyObject_GetAttrString as input to PyObject_CallObject.
Can I use the "PyObject*" returned by Py_CompileString directly as is in call to PyObject_CallObject?
not with Py_file_input
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top