This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add sys.exc_clear() to clear current exception
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: gvanrossum, jacobs99, nnorwitz
Priority: normal Keywords: patch

Created on 2003-02-25 21:26 by jacobs99, last changed 2022-04-10 16:07 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
exc_clear.diff jacobs99, 2003-02-25 21:28 diff to add sys.exc_clear, unit tests, and documentation
exc_clear2.diff jacobs99, 2003-02-26 12:39 diff to add sys.exc_clear, unit tests, and documentation (v2)
Messages (6)
msg42893 - (view) Author: Kevin Jacobs (jacobs99) Date: 2003-02-25 21:26
There is no way to clear the "current" exception, which is
available via the sys.exc_info() function.  There are a few
(obscure) easons why one would want to be able to do so,
and mainly due to the implementation details of how
exception information is stored.  

Specifically, sys.exc_info()  will return information
on the last 
exception even outside of an 'except:' block that
caught the 
exception.  So an exception and all of the frame
objects on the 
stack, and all local variables stored in those frames
are kept 
alive in the last exception traceback until either 1)
another 
exception is thrown, or 2) the stack returns to a frame
that is 
handling another exception (thrown before the "current" 
exception).

Thus, it is sometimes useful to be able to clear the
"current"
exception.  e.g.:

1) Some error handling and logging handlers will report
    on the current or last exception (as a hint about
what may
    have gone wrong).  Once that information is handled, 
    additional error handling or logging calls should
not report 
    it again. 

2) Sometimes resources are not released when an exception
     is raised until the next exception is raised. 
This causes
     problems for programs that rely on object
finalization to
     release resources (like memory, locks, file
descriptors, etc.).
     Such code is suboptimal, but it exists and there
are few
     easy alternatives other than creating many
'finally:' clauses
     (which can violate encapsulation and abstraction layer
     boundries and is syntactically hairy at times). 
Anyhow,
     such programs may want to clear the current exception
     and trigger garbage collection at certain
synchronization
     points, in order to flush pending object
finalization.  Clearly,
     this is a somewhat hit-or-miss strategy, though it
works
     fairly well on practice, though no sane developer
should
     ever rely on it.

Anyhow, I've implemented a trivial patch to sysmodule.c
to add a 'exc_clear()' function that clears the current 
or last exception.  I've also added a test case and
updated the documentation.
msg42894 - (view) Author: Kevin Jacobs (jacobs99) Date: 2003-02-25 21:39
Logged In: YES 
user_id=459565

Before someone else says it -- yes, technically there is a way
to "clear" the current exception -- by raising another
exception.
However that leaves a bogus excepton in the thread state, which
still stores at least one Python stack frame.
msg42895 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-02-26 03:32
Logged In: YES 
user_id=33168

I have a couple of minor things.  The prototype should be: 
sys_exc_clear(PyObject *self, PyObject *noargs).  This will
remove the (PyCFunction) cast.  (I realize there are other
places in the file you copied, but they are wrong too. :-)

The doc for exc_clear should have a \versionadded{2.3}
before the \end.  Should the doc for exc_info() also be
updated, since the docstring was updated?
msg42896 - (view) Author: Kevin Jacobs (jacobs99) Date: 2003-02-26 12:39
Logged In: YES 
user_id=459565

I've updated my patchj based on Neil's feedback:

  1) sys_exc_clear and sys_exc_info now use the 
       recommended prototype and the cast to
       PyCFunction was removed.
  2) \versionadded was added to the exc_clear docs.
  3)  The exc_info docs were slightly modified to better
        match the updated doc string.
msg42897 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-03-01 02:00
Logged In: YES 
user_id=6380

Grabbing this for review.
msg42898 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-03-01 03:31
Logged In: YES 
user_id=6380

All checked in, thanks. I changed the docstring for
sys.exc_info() again, to:

"Return information about the most recent exception caught
by an except clause in the current stack frame or in an
older stack frame."

I think this is accurate and concise.
History
Date User Action Args
2022-04-10 16:07:07adminsetgithub: 38050
2003-02-25 21:26:45jacobs99create