Page 1 of 1

Contextlib.contextManager erhöht keine Ausnahmen, wenn die Rücksendung in schließlich Klausel ist

Posted: 02 Jun 2025, 18:44
by Anonymous
Ich habe gerade ein wirklich seltsames Verhalten von Pythons contextlib.contextManager gestoßen. wird nicht mehr erhöht.

Code: Select all

from contextlib import contextmanager

@contextmanager
def test():
try:
print("inside the contextmanager")
yield
finally:
print("exiting contextmanager")
return # This shomehow shadows any exception raised within the contextmanager

with test():
try:
raise RuntimeError("ASDF")
except Exception as ex:
print(f"there's an {ex} exception... lets raise it again!")
raise ex
print("i don't care about exceptions")
< /code>
Wenn ich dies ausführe, erhalte ich Folgendes: < /p>
>>> inside the contextmanager
>>> there's an ASDF exception... lets raise it again!
>>> exiting contextmanager
>>> i don't care about exceptions
Ich hätte erwartet, dass die Ausnahme erhöht wird.