← index #1685Issue #4527
Related · medium · value 0.272
QUERY · ISSUE

Exception.__init__ raises TypeError if overridden and called by subclass

openby mbueschopened 2015-12-05updated 2024-09-29
bugpy-core
>>> class A(Exception):
...     def __init__(self):
...         Exception.__init__(self)
... 
>>> a = A()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
TypeError: argument should be a 'Exception' not a 'A'

Why is Exception being enforced here, even if A is a subclass of Exception? That makes it hard to subclass from Exception and override __init__.

CANDIDATE · ISSUE

Generator `.throw(e, None)` and `.throw(e, None, None)` mishandle already-instantiated exception

closedby mentalisttraceuropened 2019-02-20updated 2019-05-09

Using the web browser Unicorn emulator, haven't verified against newer or other versions:

>>> def g(): yield
...
>>> g().throw(Exception('koz'), None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in g
TypeError: exceptions must derive from BaseException

The expected (what CPython does) behavior is for it to just raise Exception('koz'), same as it would if we didn't pass the second argument at all. Same with when all three arguments are passed in.

The CPython logic for determining when the first argument is allowed to be an exception seems to be based on the second argument either being not passed in or None.

This is not a significant issue and the workaround in calling code is short and simple, but it can cause unexpected and not-obvious breakages deep inside code that otherwise works, and I presume the fix would be trivial for someone who knows where and how .throw is implemented in MicroPython, and I presume it would have almost no performance impact (one conditional inside just the code path of raising exceptions in a generator).

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied