[issue2291] Catching all exceptions with 'object'
Alexander Belopolsky
report at bugs.python.org
Sun Mar 16 17:33:50 CET 2008
Alexander Belopolsky <belopolsky at users.sourceforge.net> added the comment:
I thought some more about this issue and the current behavior seems
wrong and potentially dangerous. Consider the following code:
class x:
pass
class y(x):
pass
try:
raise y
except y:
print "a"
except:
print "b"
It prints 'b'. Now, suppose in preparation for 3.0 transition someone
adds "__metaclass__ = type" to the module with that code. The result:
it prints 'a'. Since the difference in behavior is in error handling
code, which in my experience is often not thoroughly tested, the bug
introduced by a seemingly innocuous move from old to new style classes
is likely to trigger in the worst possible moment. (For example a wrong
roll-back logic is applied after a failed database commit.)
My understanding is that the current logic of bypassing the subclass
check in PyErr_GivenExceptionMatches in the case of new style class
which is not a subclass of BaseException, is designed to support string
exceptions. Maybe a better choice would be to exclude only string
subclasses from a subclass check.
I will submit a patch if others agree that this approach is worth
considering.
__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2291>
__________________________________
More information about the Python-bugs-list
mailing list