471,344 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

PyRun_String using my module in a def

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 :)

Jul 5 '07 #1
4 3840
Stuart <st*********@gmail.comwrote:
...
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
Jul 5 '07 #2
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! :)

On Jul 4, 10:23 pm, a...@mac.com (Alex Martelli) wrote:
Stuart <stuart.t...@gmail.comwrote:

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

Jul 5 '07 #3
En Thu, 05 Jul 2007 01:19:32 -0300, Stuart <st*********@gmail.com>
escribió:
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.

--
Gabriel Genellina

Jul 5 '07 #4
Gabriel Genellina <ga*******@yahoo.com.arwrote:
En Thu, 05 Jul 2007 01:19:32 -0300, Stuart <st*********@gmail.com>
escribió:
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).
Yep.

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.
Exactly.
Alex
Jul 5 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Matthias | last post: by
5 posts views Thread by pyapplico | last post: by
2 posts views Thread by stuart.tett | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.