Skip to content

Instantly share code, notes, and snippets.

@ykxpb
Created November 22, 2011 09:29
Show Gist options
  • Select an option

  • Save ykxpb/1385291 to your computer and use it in GitHub Desktop.

Select an option

Save ykxpb/1385291 to your computer and use it in GitHub Desktop.
python with_statement
# from PEP-343
mgr=statement
exit=type(mgr).__exit__ #not calling it yet
value = type(mgr).__enter__(mgr)
exc = True
try:
try:
var=value #only if "as var " is present
block
except:
exc = False
if not exit(mgr, *sys.exc_info()): #exit返回True将不抛出如何异常
raise
finally:
# The normal and non-local-goto cases are handled here
if exc:
exit(mgr, None, None, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment