You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exception ABC doesn't work in Python 3 (but does in Python 2.7) #69634
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2015-10-21.02:32:09.832>created_at=<Date2015-10-20.19:32:31.154>labels= ['interpreter-core', 'type-bug']
title="Exception ABC doesn't work in Python 3 (but does in Python 2.7)"updated_at=<Date2015-10-21.02:32:09.831>user='https://bugs.python.org/merchise'
What are you trying to accomplish that needs an abstract base class?
APIError is not an Exception class, it is a meta-class, and thus it does not match the exception because it is not a *base* class of DriverError (it is DiverError's *meta* class).
Now, it is an interesting question whether we want the try/except statement's behavior to match issubclass, since intuitively we think the except in try except is doing the equivalent of issubclass (it isn't).
I don't know why this works in python2, but the class/meta-class machinery did undergo some significant changes. So, it is possible that this is in fact a bug (ie: an unintentional change in semantics). If so, it is also a doc bug, since the try statement docs clear say the expression must evaluate to a base class, and the virtual class (abc) docs *only* mention issubclass and isinstance.
Thinking about this some more my explanation isn't quite clear (because my thinking wasn't quite clear): the problem isn't that APIError isn't *capable* of being a base class; I was wrong when I said it is itself a metaclass. It is a class that has ABCMeta *as* its metaclass. The problem is what I said in the second part: that APIError isn't a base class *of DriverError*. The operant issue is that 'except' isn't doing issubclass, it is looking at the base class list of DriverError, and APIError isn't in it.
I'd have to see your 2.7 code to figure out why that version "works" :)
In Python 2, PyErr_GivenExceptionMatches 1 calls PyObject_IsSubclass. To handle calling __subclasscheck__ in this case, 2.7 (but not 2.6) temporarily increases the recursion limit by 5. For example:
If the recursion limit weren't temporarily increased, then trying to call __subclasscheck__ in the above case would raise another RuntimeError.
In Python 3, PyErr_GivenExceptionMatches 2 instead calls PyType_IsSubtype. See bpo-2534. In that issue Antoine's reason for the change is that "otherwise there are some nasty issues with recursion checking".
Thanks ~eryksun, you clarified me in where the error is located and possible patches for it.
We are programming a framework where is very important to do mappings between (1) the API and (2) the drivers layers.
We try to make our code available to both branches of Python (2 and 3). This case is complex because a logical exception type definition (like "resource not found") is implemented different by disparate API consumer applications (these implementing the drivers), for example httplib, couchdb or any specific database management system.
So, the API layer have to find a way to uniform disparate exception "fauna" in its definitions at a logical level.
ABCs are the nice solution, only have to work as its conceptual definition said, including exceptions, in all Python versions.
MojoVampire commented on Oct 21, 2015
MojoVampiremannequin
Mannequin
More actions
This looks like a duplicate of bpo-12029. The delays on addressing that bug seem to indicate that initially there was a fairly substantial performance cost for all exception handling paths when ABC friendly checking was performed, but that was at least partially due to a bug in those code paths that didn't bypass the ABC machinery in the simple subclass case.
That said, I'm not sure it wouldn't cause other performance regressions (I made a comment to that effect on the original bug).
merchise commented on Oct 21, 2015
merchisemannequin
MannequinAuthor
More actions
Josh Rosenberg, you are right, it's a duplication. Sorry I didn't find the issue bpo-12029 before.
Activity
merchise commentedon Oct 20, 2015
When I run the next code in all my installed versions of Python 3 (and the equivalent code for PyPy and CPython 2.7):
Runs OK in CPython 2.7 equivalent code (using
ABCMetaas meta-class).Fails in PyPy and all versions of Python 3.
bitdancer commentedon Oct 20, 2015
What are you trying to accomplish that needs an abstract base class?
APIError is not an Exception class, it is a meta-class, and thus it does not match the exception because it is not a *base* class of DriverError (it is DiverError's *meta* class).
Now, it is an interesting question whether we want the try/except statement's behavior to match issubclass, since intuitively we think the except in try except is doing the equivalent of issubclass (it isn't).
I don't know why this works in python2, but the class/meta-class machinery did undergo some significant changes. So, it is possible that this is in fact a bug (ie: an unintentional change in semantics). If so, it is also a doc bug, since the try statement docs clear say the expression must evaluate to a base class, and the virtual class (abc) docs *only* mention issubclass and isinstance.
bitdancer commentedon Oct 20, 2015
Thinking about this some more my explanation isn't quite clear (because my thinking wasn't quite clear): the problem isn't that APIError isn't *capable* of being a base class; I was wrong when I said it is itself a metaclass. It is a class that has ABCMeta *as* its metaclass. The problem is what I said in the second part: that APIError isn't a base class *of DriverError*. The operant issue is that 'except' isn't doing issubclass, it is looking at the base class list of DriverError, and APIError isn't in it.
I'd have to see your 2.7 code to figure out why that version "works" :)
eryksun commentedon Oct 20, 2015
In Python 2, PyErr_GivenExceptionMatches 1 calls PyObject_IsSubclass. To handle calling __subclasscheck__ in this case, 2.7 (but not 2.6) temporarily increases the recursion limit by 5. For example:
>>> sys.getrecursionlimit() 1000 >>> f() recursion limit: 1005 frame: 1000 >>> sys.getrecursionlimit() 1000If the recursion limit weren't temporarily increased, then trying to call __subclasscheck__ in the above case would raise another RuntimeError.
In Python 3, PyErr_GivenExceptionMatches 2 instead calls PyType_IsSubtype. See bpo-2534. In that issue Antoine's reason for the change is that "otherwise there are some nasty issues with recursion checking".
merchise commentedon Oct 21, 2015
Thanks ~eryksun, you clarified me in where the error is located and possible patches for it.
We are programming a framework where is very important to do mappings between (1) the API and (2) the drivers layers.
We try to make our code available to both branches of Python (2 and 3). This case is complex because a logical exception type definition (like "resource not found") is implemented different by disparate API consumer applications (these implementing the drivers), for example
httplib,couchdbor any specific database management system.So, the API layer have to find a way to uniform disparate exception "fauna" in its definitions at a logical level.
ABCs are the nice solution, only have to work as its conceptual definition said, including exceptions, in all Python versions.
MojoVampire commentedon Oct 21, 2015
This looks like a duplicate of bpo-12029. The delays on addressing that bug seem to indicate that initially there was a fairly substantial performance cost for all exception handling paths when ABC friendly checking was performed, but that was at least partially due to a bug in those code paths that didn't bypass the ABC machinery in the simple subclass case.
That said, I'm not sure it wouldn't cause other performance regressions (I made a comment to that effect on the original bug).
merchise commentedon Oct 21, 2015
Josh Rosenberg, you are right, it's a duplication. Sorry I didn't find the issue bpo-12029 before.