×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Super Contributor
    Join Date
    Mar 2004
    Location
    Czech Republic
    Posts
    2,037

    Embedding Pyhton in C++

    Hi,

    is there any way how to execute the Python module function from char* bufer? Now I'm using the PyImport_Import to load the '.py' module but I would prefer to provide the char* buffer to the interpreter instead. I have found only the PyRun_SimpleString but it does not give me the flexibility of providing the input parameters and checking the result...

    Thanks a lot,
    BR
    STeN

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Location
    UK
    Posts
    125

    Re: Embedding Pyhton in C++

    Quote Originally Posted by stenlik View Post
    is there any way how to execute the Python module function from char* bufer? Now I'm using the PyImport_Import to load the '.py' module but I would prefer to provide the char* buffer to the interpreter instead. I have found only the PyRun_SimpleString but it does not give me the flexibility of providing the input parameters and checking the result...
    Do you mean you want to import a module, then call a function from that module? If so, you want something like...

    Code:
    Py_Initialize();
    
    PyObject* module = PyImport_ImportModule("mymodule");
    PyObject* func = PyObject_GetAttrString(module, "myfunction");
    PyObject* result = PyObject_CallObject(func, NULL);
    
    // Do something with 'result'
    
    Py_DECREF(result);
    Py_DECREF(func);
    Py_DECREF(module);
    
    Py_Finalize();
    There's a more complete example at http://docs.python.org/extending/emb...pure-embedding.

    If you meant something else, then please clarify.

  3. #3
    Super Contributor
    Join Date
    Mar 2004
    Location
    Czech Republic
    Posts
    2,037

    Re: Embedding Pyhton in C++

    Hi,

    thanks for reply! |But I do not want to import module from file - this is already working for me. The problem is that when I use the PyImport_Import I need to have saved the Python script in the .py file. What I need is to construct the script body dynamically in C++ and provide it to the interpreter as a char* buffer... (of course I can save it to some tepmorary file, but this is not too nice solution). The only method I have found was PyRun_SimpleString - but it does not allow to handle complex scenarios with passing paramateres, readign result, etc.

    BR
    STeN

  4. #4
    Regular Contributor
    Join Date
    Mar 2003
    Location
    UK
    Posts
    125

    Re: Embedding Pyhton in C++

    Quote Originally Posted by stenlik View Post
    The only method I have found was PyRun_SimpleString - but it does not allow to handle complex scenarios with passing paramateres, readign result, etc.
    I'm not sure I understand. If you want to generate a fixed char* buffer with the code, and then execute it, I don't see where the 'passing parameters' applies.

    Unless you mean you want the Python module to call back into C functions, in which case you need to use a combination of embedding and extending.

Similar Threads

  1. embedding Open C in a SIS file
    By pzul_wisner in forum Open C/C++
    Replies: 4
    Last Post: 2008-01-23, 19:56
  2. Application embedding in 3rd edition
    By symbianyucca in forum Symbian
    Replies: 13
    Last Post: 2007-04-27, 04:55
  3. Application Embedding
    By skumar_rao in forum Symbian
    Replies: 0
    Last Post: 2007-04-27, 04:51
  4. Client/server-framework, embedding, 3rd ed.
    By tsippa in forum Symbian User Interface
    Replies: 4
    Last Post: 2007-01-26, 11:07
  5. Application embedding using Ecom plugin (S60 3rd MR)
    By akrapacs in forum Symbian User Interface
    Replies: 0
    Last Post: 2006-09-20, 16:14

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×