Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I call sys.exc_clear() for another part of a Python program?
    primarykey
    data
    text
    <p>I have a class (see <a href="/questions/5557035/what-is-the-most-pythonic-way-to-pass-objects-between-imported-libraries-of-code">this previous question</a> if you are interested) which tracks, amongst other things, errors. The class is called in a variety of situations, one of them is during an exception. Even though my class calls <code>sys.exc_clear()</code> as part of the regular course of events, the next time the class is called (even when there is no error, such as when I am just throwing some statistical information in one of the class functions) the <code>sys.exc_info()</code> tuple is still full of the original non-<code>None</code> objects.</p> <p>I can call <code>sys.exc_clear()</code> and <code>sys.exc_info()</code> is just a bunch of <code>None</code>s while the thread is executing in that class, but as soon as execution returns to the main program, this ceases to be valid. My reading of the documentation suggests that this is because the execution stack has returned to another frame. This seems to be a situation tangentially mentioned in <a href="/questions/1187102/in-python-how-to-tell-if-being-called-by-exception-handling-code">this</a> question previously.</p> <p>So, my only option appears to be tacking <code>sys.exc_clear()</code> after each <code>except</code> in my main program. I have tried it in a few places and it works. I can do this, but it seems tedious and ugly. Is another way?</p> <p>ADDITION:</p> <p>Imagine the main program as </p> <pre><code>import tracking def Important_Function(): try: something that fails except: myTrack.track(level='warning', technical='Failure in Important_Function' ...) return def Other_Function(): myTrack.track(level='info', technical='Total=0' ...) return myTrack = tracking.Tracking() myTrack.track(level='debug', parties=['operator'], technical='Started the program.') Important_Function() Other_Function() </code></pre> <p>Then the Tracking code as:</p> <pre><code>import sys import inspect import traceback ... lots of initialization stuff def track(self, level='info', technical=None, parties=None, clear=True ...): # What are our errors? errors = {} errortype, errorvalue, errortraceback = sys.exc_info() errortype, errorvalue = sys.exc_info()[:2] errors['type'] = None errors['class'] = errortype errors['value'] = errorvalue errors['arguments'] = None errors['traceback'] = None try: errors['type'] = str(errortype.__name__) try: errors['arguments'] = str(errorvalue.__dict__['args']) except KeyError: pass errors['traceback'] = traceback.format_tb(errortraceback, maxTBlevel) except: pass if clear == True: sys.exc_clear() </code></pre> <p>No multi-threading that I'm aware of. If I print <code>sys.exc_info()</code> right after calling <code>sys.exc_clear()</code>, everything has been cleared. But once I return from the <code>track</code> function and then re-enter it, even without errors, <code>sys.exc_info()</code> is back with a tuple full of the previous, old errors.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload