[capi-sig] Easy way to Return Value from 1 line of Embedded Python?
mal at egenix.com
Sun May 30 23:28:01 CEST 2010
Jerry Krinock wrote:
> On 2010 May 28, at 01:54, M.-A. Lemburg wrote:
>> You can compile the code into code object using Py_CompileString()
>> and then pass this to PyEval_EvalCode() for execution. This
>> will evaluate the code and return the resulting Python object.
> Thank you, Mark-Andre. I had trouble getting that to work. Let's move on to your preferred answer.
>> In your case, it's probably easier to just load the pickle
>> module from C and call the loads() function directly rather
>> than going through an extra layer of Python code.
> I'm not sure if you meant to use PyFunction_New() or PyRun_String(). I got it to work using the latter. See code below.
I was thinking of PyImport_ImportModule() to import the pickle module,
PyModule_GetDict() and PyDict_GetItemString() to get the function
object and finally PyEval_CallFunction() to call the function.
You can also use a short-cut directly from the module object
to the function call by using PyEval_CallMethod(pickle_module,
"loads", ...) - top-level symbols in a module are available
as attributes of the module object, e.g.
pickle_module = PyImport_ImportModule("pickle");
result = PyEval_CallMethod(pickle_module, "loads", "s#", ...);
This avoids having to compile any Python code just to unpickle
Professional Python Services directly from the Source (#1, May 30 2010)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
2010-07-19: EuroPython 2010, Birmingham, UK 49 days to go
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
More information about the capi-sig