[ python-Bugs-1756389 ] reference count discrepancy, PyErr_Print vs. PyErr_Clear

SourceForge.net noreply at sourceforge.net
Thu Jul 19 22:16:16 CEST 2007


Bugs item #1756389, was opened at 2007-07-18 16:02
Message generated for change (Comment added) made by gagenellina
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1756389&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Jon Klein (jonklein)
Assigned to: Nobody/Anonymous (nobody)
Summary: reference count discrepancy, PyErr_Print vs. PyErr_Clear

Initial Comment:

Possible reference count leak in PyErr_Print?

When a call to PyObject_Call( someObject ) raises an exception, subsequently calling PyErr_Print and PyErr_Clear will result in different behaviors with respect to the reference count of "someObject".  The reference count of "someObject" will be 1 higher with PyErr_Print() than with PyErr_Clear().

Calling PyErr_Print followed by PyErr_Clear will still result in an extra reference count.

This may be the expected behavior, but it is not documented, and I cannot figure out how to "reclaim" the reference lost when using PyErr_Print.

Attached is a short C program which uses an embedded interpreter to illustrate the problem.  Below is the test of the simple Python module I use with the C program to demonstrate the problem.

- - - - 


class ExceptionTest:
        def RaiseException( self ):
                raise Exception()



----------------------------------------------------------------------

Comment By: Gabriel Genellina (gagenellina)
Date: 2007-07-19 17:16

Message:
Logged In: YES 
user_id=479790
Originator: NO

The extra reference is hold in the traceback, which itself is hold in
sys.last_traceback
PyErr_Print calls PyErr_PrintEx with set_sys_last_vars=1, indicating that
sys.last_type/last_value/last_traceback should be set. 

<http://docs.python.org/lib/module-sys.html#l2h-5144> says: "These three
variables are not always defined; they are set when an exception is not
handled and the interpreter prints an error message and a stack traceback.
[...] (Since there is only one interactive thread, thread-safety is not a
concern for these variables, unlike for exc_type etc.)"

So there is no reference leak. The next exception printed will clear the
extra reference. If that's not acceptable, you can: a) avoid calling
PyErr_Print, or use PyErr_Print(0). b) explicitely set those three
attributes to None. c) at least set sys.last_traceback to None.

----------------------------------------------------------------------

Comment By: Jon Klein (jonklein)
Date: 2007-07-18 16:20

Message:
Logged In: YES 
user_id=1103043
Originator: YES

File Added: ExceptionTest.cc

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1756389&group_id=5470


More information about the Python-bugs-list mailing list