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

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

Post 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.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post