Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1ef17954
Commit
1ef17954
authored
May 31, 2012
by
Stefan Krah
Browse files
Options
Browse Files
Download
Plain Diff
Merge.
parents
5ddbcfc5
a5bd2a18
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
27 deletions
+28
-27
Lib/contextlib.py
Lib/contextlib.py
+15
-26
Lib/test/test_contextlib.py
Lib/test/test_contextlib.py
+6
-0
Misc/NEWS
Misc/NEWS
+7
-1
No files found.
Lib/contextlib.py
View file @
1ef17954
...
...
@@ -225,32 +225,21 @@ class ExitStack(object):
return
self
def
__exit__
(
self
,
*
exc_details
):
if
not
self
.
_exit_callbacks
:
return
# This looks complicated, but it is really just
# setting up a chain of try-expect statements to ensure
# that outer callbacks still get invoked even if an
# inner one throws an exception
def
_invoke_next_callback
(
exc_details
):
# Callbacks are removed from the list in FIFO order
# but the recursion means they're invoked in LIFO order
cb
=
self
.
_exit_callbacks
.
popleft
()
if
not
self
.
_exit_callbacks
:
# Innermost callback is invoked directly
return
cb
(
*
exc_details
)
# More callbacks left, so descend another level in the stack
# Callbacks are invoked in LIFO order to match the behaviour of
# nested context managers
suppressed_exc
=
False
while
self
.
_exit_callbacks
:
cb
=
self
.
_exit_callbacks
.
pop
()
try
:
suppress_exc
=
_invoke_next_callback
(
exc_details
)
if
cb
(
*
exc_details
):
suppressed_exc
=
True
exc_details
=
(
None
,
None
,
None
)
except
:
suppress_exc
=
cb
(
*
sys
.
exc_info
())
# Check if this cb suppressed the inner exception
if
not
suppress_exc
:
new_exc_details
=
sys
.
exc_info
()
if
exc_details
!=
(
None
,
None
,
None
):
# simulate the stack of exceptions by setting the context
new_exc_details
[
1
].
__context__
=
exc_details
[
1
]
if
not
self
.
_exit_callbacks
:
raise
else
:
# Check if inner cb suppressed the original exception
if
suppress_exc
:
exc_details
=
(
None
,
None
,
None
)
suppress_exc
=
cb
(
*
exc_details
)
or
suppress_exc
return
suppress_exc
# Kick off the recursive chain
return
_invoke_next_callback
(
exc_details
)
exc_details
=
new_exc_details
return
suppressed_exc
Lib/test/test_contextlib.py
View file @
1ef17954
...
...
@@ -572,6 +572,12 @@ class TestExitStack(unittest.TestCase):
stack
.
push
(
lambda
*
exc
:
1
/
0
)
stack
.
push
(
lambda
*
exc
:
{}[
1
])
def
test_excessive_nesting
(
self
):
# The original implementation would die with RecursionError here
with
ExitStack
()
as
stack
:
for
i
in
range
(
10000
):
stack
.
callback
(
int
)
def
test_instance_bypass
(
self
):
class
Example
(
object
):
pass
cm
=
Example
()
...
...
Misc/NEWS
View file @
1ef17954
...
...
@@ -7,11 +7,17 @@ What's New in Python 3.3.0 Beta 1?
*Release date: TBD*
Library
-------
- Issue #14963: Convert contextlib.ExitStack.__exit__ to use an iterative
algorithm (Patch by Alon Horev)
Tests
-----
- Issue #14963 (partial): Add test cases for exception handling behaviour
in contextlib.
Contex
tStack (Initial patch by Alon Horev)
in contextlib.
Exi
tStack (Initial patch by Alon Horev)
What'
s
New
in
Python
3.3.0
Alpha
4
?
===================================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment