473,226 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 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 4130
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Heather Korns | last post by:
Here's a snippet of code that demonstrates my problem: result = PyRun_String ("import math", Py_file_input, pdict,pdict); result = PyRun_String ("math.sqrt(-1)", Py_file_input, pdict,pdict);...
1
by: orit | last post by:
I have the following xml file: <?xml version="1.0" encoding="utf-8" ?> <course id="2555" title="Developing Microsoft .NET Applications for Windows (Visual C# .NET)" length="5 days"...
2
by: Matthias | last post by:
Hello, I encountered a strange problem today. I try to do this: Log("Marker 1"); Py_XDECREF( PyRun_String( "print 'Hi!'", Py_single_input, Dict, Dict) ); Log("Marker 2"); The output looks...
5
by: pyapplico | last post by:
Is there any possible way that I can place a .py file on the internet, and use that source code in an .py file on my computer?
1
by: joeedh | last post by:
Hi I'm getting extremely odd behavior. First of all, why isn't PyEval_EvalCode documented anywhere? Anyway, I'm working on blender's python integration (it embeds python, as opposed to python...
2
by: stuart.tett | last post by:
I'm using PyRun_String with Py_single_input for a python interpreter embedded in my application. I'm using Py_single_input. Py_single input is what I want, but it seems to output to stdout. Before...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
3
by: Tony Johansson | last post by:
Hello! You can set target Module for AttributeUsage. I just wonder what does it mean with module ? //Tony
3
ram09
by: ram09 | last post by:
After deploying our site in the iis7 server, we encountered this error... ModuleName AspNetInitializationExceptionModule Notification 1 HttpStatus 500 HttpReason Internal Server Error ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.