[issue12029] Allow catching virtual subclasses in except clauses

2022-02-07 Thread Michael McCoy


Michael McCoy  added the comment:

Checking my comment history here, a past me was terribly bad at linking the
correct PR on github.This is the correct link:
https://github.com/python/cpython/pull/6461

On Mon, Feb 7, 2022 at 10:12 AM Guido van Rossum 
wrote:

>
> Guido van Rossum  added the comment:
>
> Fixing the version field. Since it's a feature, this could not go into any
> version before 3.11.
>
> Maybe Irit can look through the discussion and patch and see if there's
> value to doing this? (Feel free to decline!)
>
> --
> nosy: +iritkatriel
> versions: +Python 3.11 -Python 3.4, Python 3.5, Python 3.6, Python 3.7,
> Python 3.8, Python 3.9
>
> ___
> Python tracker 
> <https://bugs.python.org/issue12029>
> ___
>

--

___
Python tracker 
<https://bugs.python.org/issue12029>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33271] Exception handling matches subtypes, not subclasses

2018-04-13 Thread Michael McCoy

New submission from Michael McCoy :

Exception handling matches subtypes, not subclasses


# Example


from abc import ABC

class MyException(Exception, ABC):
pass

class OtherException(Exception):
pass

MyException.register(OtherException)

try:
raise OtherException
except MyException:
print("Correct: Caught MyException")
except Exception:
print("Wrong: Caught something else")

# "Wrong: Caught something else"


# Background and evidence of bug-ness

Issue 2534 [1] (10 years ago!) introduced the behavior, but only in the Python 
3 patch [2]. During code review, the correct function call was used [3], but 
the function's name got switched in the final python3 patch without any comment.

The current Python 2 code uses `PyObject_IsSubclass`, and produces the correct 
behavior in the example above (using `__metaclass__ = ABCMeta`, of course). 
This leads me to strongly suspect that this is a bug, not a feature. The note 
below regarding unittest for further evidence that this code has eight legs.

Given the ancient nature of this bug, it affects all versions of python3.

[1] https://bugs.python.org/issue2534
[2] https://bugs.python.org/file11257/isinstance3k-2.patch
[3] https://codereview.appspot.com/483/diff/1/21#newcode114
[4] https://github.com/python/cpython/blob/2.7/Python/errors.c#L119


# Solution

Coming very soon in a PR on Github, but in short, we do the following:

  1. Switch `PyType_IsSubtype` to  `PyObject_IsSubclass`.
  2. Revert the changes made to remove “dead code” in 
https://bugs.python.org/issue31091. The code was dead because the wrong 
function was used—the PR left only the bug.
  3. Add tests. Note that `unittest`’s `self.assertRaises` function uses 
`issubclass` and does not alert to this bug. (Different symptom, same cause.)


# Note

This bug has nothing to do with the `abc` package, beyond being a simple way to 
generate the error.


-Mike

Gitub: mbmccoy

--
components: Interpreter Core
messages: 315241
nosy: Michael McCoy
priority: normal
severity: normal
status: open
title: Exception handling matches subtypes, not subclasses
versions: Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8

___
Python tracker 
<https://bugs.python.org/issue33271>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33271] Exception handling matches subclasses, not subtypes

2018-04-13 Thread Michael McCoy

Change by Michael McCoy :


--
title: Exception handling matches subtypes, not subclasses -> Exception 
handling matches subclasses, not subtypes

___
Python tracker 
<https://bugs.python.org/issue33271>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33271] Exception handling should match subclasses, not subtypes

2018-04-13 Thread Michael McCoy

Change by Michael McCoy :


--
title: Exception handling matches subclasses, not subtypes -> Exception 
handling should match subclasses, not subtypes

___
Python tracker 
<https://bugs.python.org/issue33271>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33271] Exception handling should match subclasses, not subtypes

2018-04-13 Thread Michael McCoy

Michael McCoy  added the comment:

Adding Serhiy because this relates to issue31091, and specifically claims that 
there _was_ a bug in the old code (msg299585).

--
nosy: +serhiy.storchaka

___
Python tracker 
<https://bugs.python.org/issue33271>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33271] Exception handling should match subclasses, not subtypes

2018-04-13 Thread Michael McCoy

Michael McCoy  added the comment:

Serhiy, it sure is. I'll note that the issue is open. Moreover, reading through 
the history, Guido says it's a bug (msg160418), and there was agreement that it 
should be fixed. It's unclear why it was dropped.

Can you help me get this in? I'm not sure the correct protocol, since this is 
my first contrib.

Note that if this is not a bug, then there are probably bugs anywhere 
issubclass(...) is used to emulate exception handling. For example, unittest's 
self.assertRaises is inconsistent with the current behavior:

from abc import ABC
import unittest

class TestExceptionABC(unittest.TestCase):

def test_exception(self):
class A(Exception, ABC):
pass
class B(Exception):
pass
A.register(B)
with self.assertRaises(A, msg="Wrong exception raised"):
raise B

Test passes in python3, but not in python2.

--

___
Python tracker 
<https://bugs.python.org/issue33271>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12029] Allow catching virtual subclasses in except clauses

2018-04-13 Thread Michael McCoy

Change by Michael McCoy :


--
pull_requests: +6160

___
Python tracker 
<https://bugs.python.org/issue12029>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12029] Allow catching virtual subclasses in except clauses

2018-04-13 Thread Michael McCoy

Michael McCoy  added the comment:

Amalgamating the patch history here, I've updated the tests on Github (PR6160) 
to include tests for both the recursive case and ensure the correct error is 
propagated up if an exception occurs during the subclass check. 

I've also added a check to ensure that unittest's assertRaises behaves as 
expected. (The test currently passes on master, which is a bug if this doesn't 
get merged.)

Finally, the PR updates the documentation for try/except.

--
nosy: +Michael McCoy

___
Python tracker 
<https://bugs.python.org/issue12029>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12029] Allow catching virtual subclasses in except clauses

2018-04-13 Thread Michael McCoy

Michael McCoy  added the comment:

Sorry, my last message referred to Github PR6460 / pull_request6160.

--
versions: +Python 3.4, Python 3.6, Python 3.7, Python 3.8

___
Python tracker 
<https://bugs.python.org/issue12029>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com