Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python2....
File: contextlib.py
"""Utilities for with-statement contexts. See PEP 343."""
[0] Fix | Delete
[1] Fix | Delete
import sys
[2] Fix | Delete
from functools import wraps
[3] Fix | Delete
from warnings import warn
[4] Fix | Delete
[5] Fix | Delete
__all__ = ["contextmanager", "nested", "closing"]
[6] Fix | Delete
[7] Fix | Delete
class GeneratorContextManager(object):
[8] Fix | Delete
"""Helper for @contextmanager decorator."""
[9] Fix | Delete
[10] Fix | Delete
def __init__(self, gen):
[11] Fix | Delete
self.gen = gen
[12] Fix | Delete
[13] Fix | Delete
def __enter__(self):
[14] Fix | Delete
try:
[15] Fix | Delete
return self.gen.next()
[16] Fix | Delete
except StopIteration:
[17] Fix | Delete
raise RuntimeError("generator didn't yield")
[18] Fix | Delete
[19] Fix | Delete
def __exit__(self, type, value, traceback):
[20] Fix | Delete
if type is None:
[21] Fix | Delete
try:
[22] Fix | Delete
self.gen.next()
[23] Fix | Delete
except StopIteration:
[24] Fix | Delete
return
[25] Fix | Delete
else:
[26] Fix | Delete
raise RuntimeError("generator didn't stop")
[27] Fix | Delete
else:
[28] Fix | Delete
if value is None:
[29] Fix | Delete
# Need to force instantiation so we can reliably
[30] Fix | Delete
# tell if we get the same exception back
[31] Fix | Delete
value = type()
[32] Fix | Delete
try:
[33] Fix | Delete
self.gen.throw(type, value, traceback)
[34] Fix | Delete
raise RuntimeError("generator didn't stop after throw()")
[35] Fix | Delete
except StopIteration, exc:
[36] Fix | Delete
# Suppress the exception *unless* it's the same exception that
[37] Fix | Delete
# was passed to throw(). This prevents a StopIteration
[38] Fix | Delete
# raised inside the "with" statement from being suppressed
[39] Fix | Delete
return exc is not value
[40] Fix | Delete
except:
[41] Fix | Delete
# only re-raise if it's *not* the exception that was
[42] Fix | Delete
# passed to throw(), because __exit__() must not raise
[43] Fix | Delete
# an exception unless __exit__() itself failed. But throw()
[44] Fix | Delete
# has to raise the exception to signal propagation, so this
[45] Fix | Delete
# fixes the impedance mismatch between the throw() protocol
[46] Fix | Delete
# and the __exit__() protocol.
[47] Fix | Delete
#
[48] Fix | Delete
if sys.exc_info()[1] is not value:
[49] Fix | Delete
raise
[50] Fix | Delete
[51] Fix | Delete
[52] Fix | Delete
def contextmanager(func):
[53] Fix | Delete
"""@contextmanager decorator.
[54] Fix | Delete
[55] Fix | Delete
Typical usage:
[56] Fix | Delete
[57] Fix | Delete
@contextmanager
[58] Fix | Delete
def some_generator(<arguments>):
[59] Fix | Delete
<setup>
[60] Fix | Delete
try:
[61] Fix | Delete
yield <value>
[62] Fix | Delete
finally:
[63] Fix | Delete
<cleanup>
[64] Fix | Delete
[65] Fix | Delete
This makes this:
[66] Fix | Delete
[67] Fix | Delete
with some_generator(<arguments>) as <variable>:
[68] Fix | Delete
<body>
[69] Fix | Delete
[70] Fix | Delete
equivalent to this:
[71] Fix | Delete
[72] Fix | Delete
<setup>
[73] Fix | Delete
try:
[74] Fix | Delete
<variable> = <value>
[75] Fix | Delete
<body>
[76] Fix | Delete
finally:
[77] Fix | Delete
<cleanup>
[78] Fix | Delete
[79] Fix | Delete
"""
[80] Fix | Delete
@wraps(func)
[81] Fix | Delete
def helper(*args, **kwds):
[82] Fix | Delete
return GeneratorContextManager(func(*args, **kwds))
[83] Fix | Delete
return helper
[84] Fix | Delete
[85] Fix | Delete
[86] Fix | Delete
@contextmanager
[87] Fix | Delete
def nested(*managers):
[88] Fix | Delete
"""Combine multiple context managers into a single nested context manager.
[89] Fix | Delete
[90] Fix | Delete
This function has been deprecated in favour of the multiple manager form
[91] Fix | Delete
of the with statement.
[92] Fix | Delete
[93] Fix | Delete
The one advantage of this function over the multiple manager form of the
[94] Fix | Delete
with statement is that argument unpacking allows it to be
[95] Fix | Delete
used with a variable number of context managers as follows:
[96] Fix | Delete
[97] Fix | Delete
with nested(*managers):
[98] Fix | Delete
do_something()
[99] Fix | Delete
[100] Fix | Delete
"""
[101] Fix | Delete
warn("With-statements now directly support multiple context managers",
[102] Fix | Delete
DeprecationWarning, 3)
[103] Fix | Delete
exits = []
[104] Fix | Delete
vars = []
[105] Fix | Delete
exc = (None, None, None)
[106] Fix | Delete
try:
[107] Fix | Delete
for mgr in managers:
[108] Fix | Delete
exit = mgr.__exit__
[109] Fix | Delete
enter = mgr.__enter__
[110] Fix | Delete
vars.append(enter())
[111] Fix | Delete
exits.append(exit)
[112] Fix | Delete
yield vars
[113] Fix | Delete
except:
[114] Fix | Delete
exc = sys.exc_info()
[115] Fix | Delete
finally:
[116] Fix | Delete
while exits:
[117] Fix | Delete
exit = exits.pop()
[118] Fix | Delete
try:
[119] Fix | Delete
if exit(*exc):
[120] Fix | Delete
exc = (None, None, None)
[121] Fix | Delete
except:
[122] Fix | Delete
exc = sys.exc_info()
[123] Fix | Delete
if exc != (None, None, None):
[124] Fix | Delete
# Don't rely on sys.exc_info() still containing
[125] Fix | Delete
# the right information. Another exception may
[126] Fix | Delete
# have been raised and caught by an exit method
[127] Fix | Delete
raise exc[0], exc[1], exc[2]
[128] Fix | Delete
[129] Fix | Delete
[130] Fix | Delete
class closing(object):
[131] Fix | Delete
"""Context to automatically close something at the end of a block.
[132] Fix | Delete
[133] Fix | Delete
Code like this:
[134] Fix | Delete
[135] Fix | Delete
with closing(<module>.open(<arguments>)) as f:
[136] Fix | Delete
<block>
[137] Fix | Delete
[138] Fix | Delete
is equivalent to this:
[139] Fix | Delete
[140] Fix | Delete
f = <module>.open(<arguments>)
[141] Fix | Delete
try:
[142] Fix | Delete
<block>
[143] Fix | Delete
finally:
[144] Fix | Delete
f.close()
[145] Fix | Delete
[146] Fix | Delete
"""
[147] Fix | Delete
def __init__(self, thing):
[148] Fix | Delete
self.thing = thing
[149] Fix | Delete
def __enter__(self):
[150] Fix | Delete
return self.thing
[151] Fix | Delete
def __exit__(self, *exc_info):
[152] Fix | Delete
self.thing.close()
[153] Fix | Delete
[154] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function