exec a string in an embedded environment

T

Tommy R

Hi all pythonistas!

I have a problem with my embedding of python inside a c app.

very short background
I work on a safety critical embedded application that runs on VxWorks.
I have successfully ported the interpreter to VW. In my solution I have
restricted other "classes" to use python through my simplified api. Its
a really complex architecture that force me to work this way, I have
encapsulated the entire interpreter with its own memspace and so on.
Redirected all IO signals and such.

To the problem
I need some way to execute a string and pass arguments to the functions
inside the string. We have discussed a solution where we first load the
string (containing some funcs) and then run something similar to
Py_RunString("foo(1.0, 'str')"); We need to do this in a generic way
so we can send in arbitrary arguments.

Can I use Py_CompileString to get a PyObject to that 'module' and then
in some magic way call a function within that module?

I am totaly out of ideas so please help me anyone.


//Tommy
 
G

Graham Fawcett

Tommy said:
I need some way to execute a string and pass arguments to the functions
inside the string. We have discussed a solution where we first load the
string (containing some funcs) and then run something similar to
Py_RunString("foo(1.0, 'str')"); We need to do this in a generic way
so we can send in arbitrary arguments.

You could use

foo_ptr = Py_RunString("foo", Py_eval_input, globals_ptr,
locals_ptr)

to get a reference to the foo function, and then use

result_ptr = PyObject_Call(foo_ptr, args_ptr, kwargs_ptr)

to call it with arbitrary arguments and keyword arguments. Of course,
you're responsible for setting up the globals and locals, as well as
the args list and kwargs dictionary objects.

The PyObject_Call signature, from <abstract.h>, is:

PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable_object,
PyObject *args, PyObject *kw);

Does this help?

Graham
 
G

Greg Copeland

I work on a safety critical embedded application that runs on VxWorks.
I have successfully ported the interpreter to VW. In my solution I have

Sure wish you would of asked...I ported Python to VxWorks
some time back. I've been using it for some time now.

Greg
 
J

jon

Greg said:
Sure wish you would of asked...I ported Python to VxWorks
some time back. I've been using it for some time now.

Greg,

I'm very interested to hear more about your experience in porting
Python to VxWorks. Prior to seeing your VxWorks-related postings as
well as Tommy's, the last VxWorks port related information I found was
back in '99-'00 timeframe.

So, do you have any recipes that you can post somewhere? Any help
pointers would be greatly appreciated.

Thanks,
Jon Wahlmann
 
G

Greg Copeland

I would be happy to share my point with you. In fact, I'm fixing a
minor memory leak (socket module; vxWorks specific) in Python 2.3.4
(ported version) today. My port is actually on BE XScale.

Email me at g t copeland2002@@ya hoo...com and I'll be happy to talk
more with you.
 
T

Tommy Ryding

No it didn't help me :(

the foo_ptr that is returned from PyRun_String is not a callable
object. Therefor I can't call it from PyObject_Call.


I would like to do the exact same procedure as I do when the code is
located in a module (file).

pModule = PyImport_Import(pName);
Py_DECREF(pName);

if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, inArg->funcName);
/* pFunc is a new reference */

if (pFunc && PyCallable_Check(pFunc)) {


It must be possible to import a module written inside a string with a
similar approach. The ultimate solution would be to change the
PyImport_Import() call to something that creates an object from a
string

Now some of you think why not have the code inside a file. We need to
create encrypted script files, thats why.

I hope someone can help me //Tommy
 
T

Tommy Ryding

I can't post that much of what I have done but some questions might
answer if you e-mail me the question to my gmail.com address.
tommy.ryding@...

//Tommy
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top