vendredi 27 novembre 2020

sys.exc_info() in Python 2 vs Python 3

I am having some difficulties with exception handling in Python 3. In both Python 2.7 and 3.8, I have this snippet that gives you an idea of how I handle errors/exceptions:

type_except, value, traceback = sys.exc_info()
print("This is the exception type:", type_except)
if not type_except:
    self.info("This was successful")
elif type_except is self.failureException:
    self.warning(self.failure_message(type_except, value, tb))
    self.info("This failed")
else:
    self.error(self.error_message(type_except, value, tb))
    self.info("This resulted in an ERROR")

In Python 2, everything works as expected, and when I have an exception, I get the error message, and I get this when I print what type_except is:

This is the exception type:

<type 'exceptions.AssertionError'>

In Python 3, when running the same test, that fails for the same reason, I get a tuple of Nones in sys.exc_info() instead. Thus, my failed tests pass.

Python 3:

This is the exception type: None

Does anyone know why this might be happening? I looked at the definitions of sys.exc_info() for both 2.7 and 3.8, but they seem the same.

Aucun commentaire:

Enregistrer un commentaire