← index #1685PR #18760
Off-topic · high · value 1.570
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 · PULL REQUEST

py/objtype: Add type validation for super() first argument

closedby bob10042opened 2026-01-30updated 2026-02-20
py-core

Fixes #17728

Root cause:
ative_base_init_wrapper()\ casts ^Grgs[0]\ (self) to \mp_obj_instance_t\ without validation. When _init_\ corrupts self (e.g., \self = integer), \super().init()\ dereferences an invalid pointer causing segfault.

Fix: Add validation before accessing self:

  • Check \mp_obj_is_obj()\ to ensure self is a heap object, not a small int
  • Check \mp_obj_is_instance_type()\ to verify self is an instance type
  • Raise \TypeError\ with clear message if validation fails

Impact: Prevents segfault from improper self assignment in _init_. Now raises proper \TypeError\ instead of crashing, matching CPython behavior.

Test: Added test case in \ ests/basics/subclass_native_init.py\ that verifies corrupted self raises \TypeError.

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