473,503 Members | 10,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Undocumented Python 2.6 change: Py_None vs NULL when C implementationraises exception

I was debugging M2Crypto function written in C which changed behavior
between Python 2.6 and earlier Python versions. In an error condition
the function was supposed to raise exception type A, but with 2.6 it
raised type B, and further, there was no string value for the exception.

I tracked this down to the C code incorrectly returning Py_None when it
should have returned NULL. Changing the C code to return NULL made it
behave correctly in 2.6.

I don't know how common a mistake it is to return Py_None when NULL
should have been returned, but it might be worth a note in the list of
changes for 2.6 that this behavior changed, don't you think?

--
Heikki Toivonen
Jul 23 '08 #1
4 2209
Heikki Toivonen wrote:
I was debugging M2Crypto function written in C which changed behavior
between Python 2.6 and earlier Python versions. In an error condition
the function was supposed to raise exception type A, but with 2.6 it
raised type B, and further, there was no string value for the exception.

I tracked this down to the C code incorrectly returning Py_None when it
should have returned NULL. Changing the C code to return NULL made it
behave correctly in 2.6.

I don't know how common a mistake it is to return Py_None when NULL
should have been returned, but it might be worth a note in the list of
changes for 2.6 that this behavior changed, don't you think?
the behaviour when setting the exception status without returning NULL
has never been well-defined; it pretty much depends on what kind of
error checking the code that runs later happens to use.

(this has been an entertaining source of obscure errors over the years;
code that uses PyErr_Clear may mask errors, and code that uses
PyErr_Occurred to check if something went wrong might raise the wrong
error, often at an unexpected location).

</F>

Jul 23 '08 #2
Heikki Toivonen wrote:
I was debugging M2Crypto function written in C which changed behavior
between Python 2.6 and earlier Python versions. In an error condition
the function was supposed to raise exception type A, but with 2.6 it
raised type B, and further, there was no string value for the exception.

I tracked this down to the C code incorrectly returning Py_None when it
should have returned NULL. Changing the C code to return NULL made it
behave correctly in 2.6.
Can you please be specific what function you are talking about?
I don't know how common a mistake it is to return Py_None when NULL
should have been returned, but it might be worth a note in the list of
changes for 2.6 that this behavior changed, don't you think?
Perhaps. OTOH, perhaps the change is completely erroneous. In that case,
rather than documenting it, it should be reverted.

Unfortunately, as you keep the specific issue secret, none of this will
happen, as we have no clue what you are talking about.

I'm sure there are tons of silent changes, in this release, all past
releases, and all future releases, not only in Python, but in any
software.

Regards,
Martin
Jul 23 '08 #3
Martin v. Löwis wrote:
Heikki Toivonen wrote:
>I tracked this down to the C code incorrectly returning Py_None when it
should have returned NULL. Changing the C code to return NULL made it
behave correctly in 2.6.

Can you please be specific what function you are talking about?
Perhaps it wasn't clear that I was referring to the C code in an
extension, M2Crypto. I assumed that this affected all extension code
like this, which is why I didn't mention the actual lines. It appears
from Fredrik's comment that this might not be just a 2.6 issue, but that
this problem has cropped up in the past as well more or less randomly.
>I don't know how common a mistake it is to return Py_None when NULL
should have been returned, but it might be worth a note in the list of
changes for 2.6 that this behavior changed, don't you think?

Perhaps. OTOH, perhaps the change is completely erroneous. In that case,
rather than documenting it, it should be reverted.

Unfortunately, as you keep the specific issue secret, none of this will
happen, as we have no clue what you are talking about.
I don't know what change in Python caused the change in M2Crypto
behavior. I can only point you to the change I made in M2Crypto if you
are interested:
http://viewcvs.osafoundation.org/m2c...&r1=611&r2=531

If you revert that change and run the M2Crypto unit tests you will see
the single error in the tests.
I'm sure there are tons of silent changes, in this release, all past
releases, and all future releases, not only in Python, but in any
software.
Given that there is a long document showing the changes in each Python
release, I would hope all intended changes of significance would be
listed. Of course mistakes can happen, which was why I posted in the
first place.

I have no plans to track down the exact change in Python code that
caused this. There does not seem to be much point, since according to
Fredrik this seems to be an area that is practically undefined and the
M2Crypto code was clearly buggy.

--
Heikki Toivonen
Jul 24 '08 #4
I have no plans to track down the exact change in Python code that
caused this. There does not seem to be much point, since according to
Fredrik this seems to be an area that is practically undefined and the
M2Crypto code was clearly buggy.
I see, and I agree with Fredrik's analysis. It might actually be that
there was *no* change in 2.6 causing this change in behavior, but just
a difference in data returned in the actual application. E.g. if a
PyInt_FromLong returns -1, the caller also needs to check for
PyErr_Occurred, which would then detect an earlier exception. So it
might be that if you have -1 in your data, you see the exception, but
if you have -2 instead, you won't see it.

Regards,
Martin
Jul 24 '08 #5

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

Similar topics

2
1411
by: tomas.bouda | last post by:
Hi, I'm facing a problem which seems to be a Python bug... I've got an application written in C and having Python embedded inside. The situation is the following: 1) C object is wrapped by...
2
1374
by: Donnie Leen | last post by:
I wrote a program to test calling c function from python code embedding in c as following, it cause error after running a while(about 398 circle). I test it in msvc6, python2.3, windows 2k, could...
3
3354
by: Travis Berg | last post by:
I'm running into a problem when trying to perform a callback to a Python function from a C extension. Specifically, the callback is being made by a pthread that seems to cause the problem. If I...
2
4427
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
4
1869
by: Luke Miller | last post by:
Hello, I am working on my first python module based on a c program. The module builds and installs OK using dist-utils, and imports fine into python. However, when I try and use the one wrapper...
2
1344
by: Ben Sizer | last post by:
I have Python embedded in a C++ application (yes, yes, I know, I'd prefer it the other way around too) and essentially need to expose some read-only values and functions to Python so it can be used...
5
3796
by: Stefan Bellon | last post by:
Hi all! I am embedding Python into a GUI application in a way that the GUI is scriptable using Python. Now I have come to a problem that when the user puts a "sys.exit(0)" into his script to...
0
1097
by: Tim Spens | last post by:
The following is a simple complete example using the c python api to generate callbacks from c to python. But when I run the c code I get a segfault in PyInt_FromLong () (see below). Most of this...
0
2091
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
0
7294
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
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7015
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...
1
5026
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...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.