PyRun_String using my module in a def

S

Stuart

I've written my own python modules with the C API, called dlfl. I've
now embedded a python interpreter into my Qt application. I am able to
execute multiline/singleline blocks and it has no problems remembering
definitions from one execute to the next. The problem arises when i
try and use a dlfl method inside a def block. For example

def x( ): print faces(0)

faces is a method in dlfl. If I were to just type "print faces(0)" it
would execute fine. However, this way when I type "x()", it just says
"global name 'faces' is not defined"

If I define x without any of my dlfl methods it works fine:

def x():
y = 2
print y+2

x()
# 4

Here are the relevant parts of my code:

PyObject *main = PyImport_AddModule("__main__");
PyObject *dlfl = PyImport_AddModule("dlfl");
PyObject* main_dict = PyModule_GetDict( main );
PyObject* dlfl_dict = PyModule_GetDict( dlfl );

PyObject *rstring = PyRun_String( cmd, Py_file_input, main_dict,
dlfl_dict );

rstring returns NULL and then PyErr_Fetch( &object, &data,
&traceback ); gets the traceback string as I mentioned above.

Hopefully, there is just a simple solution. Thanks :)
 
A

Alex Martelli

Stuart said:
PyObject *rstring = PyRun_String( cmd, Py_file_input, main_dict,
dlfl_dict );

You're passing difl_dict as the "locals" to PyRun_String -- but a
function has its own locals, so it won't use those locals. Just update
main_dict with difl_dict (that's the equivalent of the "from difl import
*" which you appear to desire) and use main_dict for both globals and
locals in your PyRun_String call.


Alex
 
S

Stuart

What command do you mean when you say "update main_dict with
dlfl_dict"?

I tried PyObject *rstring = PyRun_String( cmd, Py_file_input,
dlfl_dict, dlfl_dict );
This worked, but has the side effect of not allowing other commands
like "execfile"
I was able to type that before, but now it just says "name 'execfile'
is not defined"

Thanks for your help! :)
 
G

Gabriel Genellina

What command do you mean when you say "update main_dict with
dlfl_dict"?

I think Alex Martelly was refering to use main_dict.update(dlfl_dict)
(Python code) or PyDict_Update(main_dict, dlfl_dict) (in C code).
I tried PyObject *rstring = PyRun_String( cmd, Py_file_input,
dlfl_dict, dlfl_dict );
This worked, but has the side effect of not allowing other commands
like "execfile"

The idea is to copy all items from dlfl_dict into main_dict, and use
main_dict for both globals and locals.
 
A

Alex Martelli

Gabriel Genellina said:
I think Alex Martelly was refering to use main_dict.update(dlfl_dict)
(Python code) or PyDict_Update(main_dict, dlfl_dict) (in C code).
Yep.



The idea is to copy all items from dlfl_dict into main_dict, and use
main_dict for both globals and locals.

Exactly.


Alex
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top