Skip to content

Commit 4bfe2a8

Browse files
author
James William Pye
committed
And promptly get rid of it, Nested.
After some minor refactoring, there were no dependents.
1 parent 2ebd7cf commit 4bfe2a8

2 files changed

Lines changed: 1 addition & 188 deletions

File tree

postgresql/python/contextlib.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,57 +24,3 @@ def __context__(typ):
2424
@staticmethod
2525
def __exit__(typ, val, tb):
2626
pass
27-
28-
class Nested(object):
29-
"""
30-
cause they deprecated it in 3.1
31-
32-
Implemented with a class instead of a contextlib.contextmanager. A generator
33-
CM is probably a better choice as it would likely make it easier to properly
34-
preserve __context__.
35-
36-
WARNING: Uses __cause__ to reference exception contexts when possible.
37-
"""
38-
__slots__ = ('cm', 'exits')
39-
40-
def __init__(self, *cm):
41-
self.cm = tuple([
42-
x.__context__() if hasattr(x, '__context__') else x for x in cm
43-
])
44-
45-
def __enter__(self):
46-
if hasattr(self, 'exits'):
47-
raise RuntimeError("context manager already ran")
48-
self.exits = []
49-
r = []
50-
try:
51-
for x in self.cm:
52-
r.append(x.__enter__())
53-
self.exits.append(x.__exit__)
54-
except:
55-
if self.__exit__(*sys.exc_info()):
56-
raise RuntimeError("cannot suppress exceptions raised during entry")
57-
raise
58-
return tuple(r)
59-
60-
def __exit__(self, typ, val, tb):
61-
# if there are no exits, there's nothing to be done.
62-
oval = val
63-
for x in reversed(self.exits):
64-
try:
65-
if x(typ, val, tb):
66-
typ = val = tb = None
67-
except:
68-
newtyp, newval, newtb = sys.exc_info()
69-
if newval.__cause__ is None:
70-
newval.__cause__ = newval.__context__
71-
typ = newtyp
72-
val = newval
73-
tb = newtb
74-
self.exits = ()
75-
if val is not None and val is not oval:
76-
raise val
77-
return val is None
78-
79-
def __context__(self):
80-
return self

postgresql/test/test_python.py

Lines changed: 1 addition & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def testFindAvailable(self):
150150
else:
151151
self.fail("got a connection to an available port: " + str(portnum))
152152

153-
class contextlib(unittest.TestCase):
153+
class test_contextlib(unittest.TestCase):
154154
def testNoCM(self):
155155
with NoCM as foo:
156156
pass
@@ -161,139 +161,6 @@ def testNoCM(self):
161161
pass
162162
self.failUnlessEqual(foo, None)
163163

164-
def testNested(self):
165-
class SomeExc(Exception):
166-
pass
167-
@contextmanager
168-
def just_one():
169-
yield 1
170-
@contextmanager
171-
def just_two():
172-
try:
173-
yield 2
174-
except:
175-
print("wtf")
176-
@contextmanager
177-
def raise_exc():
178-
raise SomeExc("foo")
179-
yield 1
180-
@contextmanager
181-
def finally_exc():
182-
try:
183-
yield 1
184-
finally:
185-
raise SomeExc("foo")
186-
@contextmanager
187-
def suppress():
188-
try:
189-
yield None
190-
except SomeExc:
191-
pass
192-
@contextmanager
193-
def expect_and_raise(exc, rexc):
194-
try:
195-
yield None
196-
except exc:
197-
raise rexc("bleh")
198-
199-
with Nested() as foo:
200-
pass
201-
self.failUnlessEqual(foo, ())
202-
try:
203-
with Nested() as foo:
204-
self.failUnlessEqual(foo, ())
205-
raise SomeExc("bar")
206-
except SomeExc:
207-
pass
208-
else:
209-
self.fail("empty Nested did not raise exception")
210-
211-
with Nested(just_one()) as foo:
212-
pass
213-
self.failUnlessEqual(foo, (1,))
214-
215-
with Nested(just_one(), just_one()) as foo:
216-
pass
217-
self.failUnlessEqual(foo, (1,1))
218-
219-
# NoCM won't raise a RuntimeError on re-use.
220-
N=Nested(NoCM)
221-
with N:
222-
pass
223-
try:
224-
with N:
225-
pass
226-
except RuntimeError:
227-
pass
228-
else:
229-
self.fail("exhausted Nested() failed to raise RuntimeError")
230-
231-
# unsuppressed exceptions on entry
232-
try:
233-
with Nested(raise_exc()):
234-
pass
235-
except SomeExc:
236-
pass
237-
else:
238-
self.fail("nested didn't raise SomeExc")
239-
try:
240-
with Nested(just_one(), raise_exc()):
241-
pass
242-
except SomeExc:
243-
pass
244-
else:
245-
self.fail("nested didn't raise SomeExc")
246-
247-
# suppressed SomeExc during entry--disallowed.
248-
try:
249-
with Nested(suppress(), raise_exc()):
250-
pass
251-
except RuntimeError:
252-
pass
253-
else:
254-
self.fail("nested didn't raise RuntimeError on suppressed partial entry")
255-
256-
# suppress should stop it, and that's okay because we're already
257-
# inside the block.
258-
with Nested(suppress(), finally_exc()):
259-
raise SomeExc("foo")
260-
261-
# This test case validates that the context is being
262-
# properly set.
263-
class ThisExc(Exception):
264-
pass
265-
try:
266-
with Nested(expect_and_raise(ThisExc, SomeExc)):
267-
raise ThisExc("FOO")
268-
except SomeExc as e:
269-
self.failUnlessEqual(type(e.__context__), ThisExc)
270-
else:
271-
self.fail("failed to raise exception")
272-
273-
class ThatExc(Exception):
274-
pass
275-
try:
276-
with Nested(expect_and_raise(ThisExc, SomeExc), expect_and_raise(ThatExc, ThisExc)):
277-
raise ThatExc("BAFOON")
278-
except SomeExc as e:
279-
# ThatExc -> ThisExc -> SomeExc
280-
self.failUnlessEqual(type(e.__cause__), ThisExc)
281-
self.failUnlessEqual(type(e.__cause__.__cause__), ThatExc)
282-
283-
try:
284-
with Nested(just_one()) as foo:
285-
self.failUnlessEqual(foo, (1,))
286-
raise SomeExc("inside the block")
287-
except SomeExc as e:
288-
pass
289-
else:
290-
self.fail("Nested didn't pass up exception raised in block")
291-
292-
# Slightly more complex suppression.
293-
with Nested(just_two(), suppress(), expect_and_raise(ThisExc, SomeExc), expect_and_raise(ThatExc, ThisExc)) as foo:
294-
raise ThatExc("MOOF")
295-
self.failUnlessEqual(foo[0], 2)
296-
297164
if __name__ == '__main__':
298165
from types import ModuleType
299166
this = ModuleType("this")

0 commit comments

Comments
 (0)