Exception that calls `super().__init__` breaks `copy.copy`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug report
Bug description:
For some reason calling Exception.__init__ breaks copy.copys ability to detect args. The following code
import copy
class MyException(Exception):
def __init__(self, x):
super().__init__()
self.x = x
my_exc = MyException(5)
copy.copy(my_exc)
gives
Traceback (most recent call last):
File "./foo.py", line 9, in <module>
copy.copy(my_exc)
File "/usr/lib/python3.12/copy.py", line 97, in copy
return _reconstruct(x, None, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/copy.py", line 253, in _reconstruct
y = func(*args)
^^^^^^^^^^^
TypeError: MyException.__init__() missing 1 required positional argument: 'x'
Removing the super().__init__() line makes everything work as expected, but given that it's generally seen as good practice to call super().__init__() in child classes, and you may be working with a more complex class structure, this seems like pretty bad behavior.
I haven't dug deep enough into copy.copy or BaseException.__init__ to decide which one deserves the blame.
For the reason why I want to copy exceptions, see https://github.com/python-trio/flake8-async/issues/298
CPython versions tested on:
3.9, 3.10, 3.11, 3.12, 3.13, 3.14
Operating systems tested on:
Linux
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere das Beispiel mit copy.copy und untersuche das Verhalten in /usr/lib/python3.12/copy.py zusammen mit BaseException.init. Bestimme das erwartete Kopierverhalten für eine Exception, deren Initialisierer super().init() aufruft, und füge anschließend einen Regressionstest hinzu, der zeigt, dass das Kopieren den erforderlichen Konstruktorzustand bewahrt, ohne TypeError auszulösen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100