473,405 Members | 2,187 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,405 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 4169
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 ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.