-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Implement support for with-blocks.
The basic idea here is rewrite the ast of the with-block into two statements and try-except, which we already know how to oneline-ify In general, we can rewrite a with block of the form: with <expr> as <ctxname>: <body> as __anonymous = <expr> <ctxname> = __anonymous.__enter__() try: <body> except: if not __anonymous.__exit__(*sys.exc_info()): raise else: __anonymous.__exit__(None, None, None)
- Loading branch information
1 parent
167dc20
commit 942b45d
Showing
4 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class ContextManager(object): | ||
|
||
def __enter__(self): | ||
print "Entering" | ||
return "__enter__ value" | ||
|
||
def __exit__(self, type, value, traceback): | ||
print "Exiting" | ||
|
||
print "Before" | ||
with ContextManager() as c: | ||
print "In Body" | ||
print c | ||
|
||
# This currently fails for reasons I don't quite understand. | ||
# print "After" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class ContextManager(object): | ||
|
||
def __enter__(self): | ||
print "Entering" | ||
|
||
def __exit__(self, type, value, traceback): | ||
print "Exiting" | ||
|
||
|
||
print "Before" | ||
with ContextManager() as c: | ||
print "In Body" | ||
raise ValueError("Raise") | ||
|
||
print "After" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class ContextManager(object): | ||
|
||
def __enter__(self): | ||
print "Entering" | ||
return "__enter__ value" | ||
|
||
def __exit__(self, type, value, traceback): | ||
print "Exiting" | ||
# Suppress the exception. | ||
return True | ||
|
||
|
||
print "Before" | ||
with ContextManager() as c: | ||
print "Before Raise" | ||
raise ValueError("Raise") | ||
|
||
# This currently fails for reasons I don't quite understand. | ||
# print "After" |