Tags: app, application, backgroundi, critical, embedded, embedding, environment, exec, inside, programming, python, pythonistasi, safety, string

exec a string in an embedded environment

On Programmer » Python

4,574 words with 7 Comments; publish: Wed, 02 Jan 2008 18:45:00 GMT; (20093.75, « »)

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

All Comments

Leave a comment...

  • 7 Comments
    • Tommy R wrote:

      > 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

      #1; Wed, 02 Jan 2008 18:47:00 GMT
    • On Wed, 11 Jan 2006 04:29:32 -0800, Tommy R wrote:

      > 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

      #2; Wed, 02 Jan 2008 18:48:00 GMT
    • Greg Copeland wrote:

      > On Wed, 11 Jan 2006 04:29:32 -0800, Tommy R wrote:

      > > 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,

      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

      #3; Wed, 02 Jan 2008 18:49:00 GMT
    • Tommy, same question to you... :-)

      -Jon

      #4; Wed, 02 Jan 2008 18:50:00 GMT
    • 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.python.itags.org..python.itags.org.ya hoo...com and I'll be happy to talk

      more with you.

      #5; Wed, 02 Jan 2008 18:51:00 GMT
    • 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

      #6; Wed, 02 Jan 2008 18:52:00 GMT
    • 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.python.itags.org....

      //Tommy

      #7; Wed, 02 Jan 2008 18:53:00 GMT