exec a string in an embedded environment

Discussion in 'Python' started by Tommy R, Jan 11, 2006.

  1. Tommy R

    Tommy R Guest

    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
     
    Tommy R, Jan 11, 2006
    #1
    1. Advertising

  2. 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
     
    Graham Fawcett, Jan 11, 2006
    #2
    1. Advertising

  3. 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
     
    Greg Copeland, Jan 11, 2006
    #3
  4. Tommy R

    Guest

    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
     
    , Jan 12, 2006
    #4
  5. Tommy R

    Jon Guest

    Tommy, same question to you... :)

    -Jon
     
    Jon, Jan 12, 2006
    #5
  6. 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.
     
    Greg Copeland, Jan 12, 2006
    #6
  7. Tommy R

    Tommy Ryding Guest

    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
     
    Tommy Ryding, Jan 17, 2006
    #7
  8. Tommy R

    Tommy Ryding Guest

    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
     
    Tommy Ryding, Jan 17, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Vaughan
    Replies:
    11
    Views:
    1,157
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,128
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    479
    Duncan Booth
    Jul 22, 2004
  4. Guillermo Riojas
    Replies:
    0
    Views:
    185
    Guillermo Riojas
    Nov 26, 2010
  5. Random Task
    Replies:
    12
    Views:
    681
    Joe Smith
    Dec 4, 2005
Loading...

Share This Page