473,378 Members | 1,403 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,378 software developers and data experts.

PyRun_String

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 like

Marker 1
Hi!
Hi!
Marker 2

Obviously Hi! is printed twice. I've extracted the above example of a more
complex piece of code where you can clearly see that the statement is
actually executed twice (not only printed). You can also see this with

Log("Marker 1");
Py_XDECREF( PyRun_String( "def func():\n\ti = 0\n\twhile 1:\n\t\tyield
i\n\t\ti += 1\no=func()", Py_file_input, Dict, Dict) );
Py_XDECREF( PyRun_String( "print o.next()", Py_file_input, Dict, Dict) );
Log("Marker 2");
Here the output goes

Marker 1
0
1
Marker 2
What is the reason for this very strange behaviour? Did I do something
wrong?

-Matthias
Mar 9 '06 #1
2 5071
Matthias wrote:
Log("Marker 1");
Py_XDECREF( PyRun_String( "print 'Hi!'", Py_single_input, Dict, Dict) );
Log("Marker 2");

The output looks like

Marker 1
Hi!
Hi!
Marker 2

Obviously Hi! is printed twice.


Py_XDECREF is a C macro. If you check its definition you will see that it
does indeed evaluate the argument twice. This is a not uncommon problem
when using macros in C: if in doubt, do not call a macro with anything
as a parameter which has side effects.

#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
Mar 9 '06 #2
Am Thu, 09 Mar 2006 20:06:54 +0100 hat Duncan Booth
<du**********@invalid.invalid> geschrieben:
Matthias wrote:
Log("Marker 1");
Py_XDECREF( PyRun_String( "print 'Hi!'", Py_single_input, Dict, Dict) );
Log("Marker 2");

The output looks like

Marker 1
Hi!
Hi!
Marker 2

Obviously Hi! is printed twice.


Py_XDECREF is a C macro. If you check its definition you will see that it
does indeed evaluate the argument twice. This is a not uncommon problem
when using macros in C: if in doubt, do not call a macro with anything
as a parameter which has side effects.

#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)


Arrrghh, I've seen the evil side today; C macros are surely devil-sent. I
spent hours tracking down this bug, I didn't even consider the Py_XDECREF
and concentrated on PyRun_String. It's a pity C doesn't support inline
functions. Thanks a lot for pointing this out before I lost my head,
Duncan!

Maybe Py_XDECREF(op) could be replaced with

#define Py_XDECREF(op) {PyObject* obj = (op); if (obj == NULL) ; else
Py_DECREF(obj)}

but I guess this eats one or two clock cycles too much for other people
who use Py_XDECREF.

Thanks again!

-Matthias
Mar 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kim | last post by:
Hi everyone, I'm writing a embeded python program, and I want to evaluate some expression by calling function: PyRun_SimpleString("print 'hello'") I don't want to output it to stdout but...
1
by: David Faden | last post by:
Hi, Given arbitrary Python source code input by the user, I want to get the result of executing that source as an expression if it is an expression and otherwise execute the source as a...
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);...
0
by: DPhelps | last post by:
I have a multithreaded Python shell server (base on the sample code 'pysvr.py') that uses a C-based extension class. The trouble I'm having is that I cannot figure out a way to create a Python...
1
by: John Dean | last post by:
Hi Is it possible to execute a whole script using the C API function PyRun_String? At moment I load the script into a buffer. Then I get each line of the script and pass it PyRun_String. This...
6
by: John Dean | last post by:
Hi I spent the whole of yesterday trying the get the following C code to execute PyRun_String("def title();", Py_file_input, dict, dict); PyRun_String("\treturn 'Foo Bar'", Py_file_input,...
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...
4
by: Stuart | last post by:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.