Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3....
File: contextlib.py
[500] Fix | Delete
# Callbacks are invoked in LIFO order to match the behaviour of
[501] Fix | Delete
# nested context managers
[502] Fix | Delete
suppressed_exc = False
[503] Fix | Delete
pending_raise = False
[504] Fix | Delete
while self._exit_callbacks:
[505] Fix | Delete
is_sync, cb = self._exit_callbacks.pop()
[506] Fix | Delete
assert is_sync
[507] Fix | Delete
try:
[508] Fix | Delete
if cb(*exc_details):
[509] Fix | Delete
suppressed_exc = True
[510] Fix | Delete
pending_raise = False
[511] Fix | Delete
exc_details = (None, None, None)
[512] Fix | Delete
except:
[513] Fix | Delete
new_exc_details = sys.exc_info()
[514] Fix | Delete
# simulate the stack of exceptions by setting the context
[515] Fix | Delete
_fix_exception_context(new_exc_details[1], exc_details[1])
[516] Fix | Delete
pending_raise = True
[517] Fix | Delete
exc_details = new_exc_details
[518] Fix | Delete
if pending_raise:
[519] Fix | Delete
try:
[520] Fix | Delete
# bare "raise exc_details[1]" replaces our carefully
[521] Fix | Delete
# set-up context
[522] Fix | Delete
fixed_ctx = exc_details[1].__context__
[523] Fix | Delete
raise exc_details[1]
[524] Fix | Delete
except BaseException:
[525] Fix | Delete
exc_details[1].__context__ = fixed_ctx
[526] Fix | Delete
raise
[527] Fix | Delete
return received_exc and suppressed_exc
[528] Fix | Delete
[529] Fix | Delete
def close(self):
[530] Fix | Delete
"""Immediately unwind the context stack."""
[531] Fix | Delete
self.__exit__(None, None, None)
[532] Fix | Delete
[533] Fix | Delete
[534] Fix | Delete
# Inspired by discussions on https://bugs.python.org/issue29302
[535] Fix | Delete
class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
[536] Fix | Delete
"""Async context manager for dynamic management of a stack of exit
[537] Fix | Delete
callbacks.
[538] Fix | Delete
[539] Fix | Delete
For example:
[540] Fix | Delete
async with AsyncExitStack() as stack:
[541] Fix | Delete
connections = [await stack.enter_async_context(get_connection())
[542] Fix | Delete
for i in range(5)]
[543] Fix | Delete
# All opened connections will automatically be released at the
[544] Fix | Delete
# end of the async with statement, even if attempts to open a
[545] Fix | Delete
# connection later in the list raise an exception.
[546] Fix | Delete
"""
[547] Fix | Delete
[548] Fix | Delete
@staticmethod
[549] Fix | Delete
def _create_async_exit_wrapper(cm, cm_exit):
[550] Fix | Delete
return MethodType(cm_exit, cm)
[551] Fix | Delete
[552] Fix | Delete
@staticmethod
[553] Fix | Delete
def _create_async_cb_wrapper(callback, /, *args, **kwds):
[554] Fix | Delete
async def _exit_wrapper(exc_type, exc, tb):
[555] Fix | Delete
await callback(*args, **kwds)
[556] Fix | Delete
return _exit_wrapper
[557] Fix | Delete
[558] Fix | Delete
async def enter_async_context(self, cm):
[559] Fix | Delete
"""Enters the supplied async context manager.
[560] Fix | Delete
[561] Fix | Delete
If successful, also pushes its __aexit__ method as a callback and
[562] Fix | Delete
returns the result of the __aenter__ method.
[563] Fix | Delete
"""
[564] Fix | Delete
_cm_type = type(cm)
[565] Fix | Delete
_exit = _cm_type.__aexit__
[566] Fix | Delete
result = await _cm_type.__aenter__(cm)
[567] Fix | Delete
self._push_async_cm_exit(cm, _exit)
[568] Fix | Delete
return result
[569] Fix | Delete
[570] Fix | Delete
def push_async_exit(self, exit):
[571] Fix | Delete
"""Registers a coroutine function with the standard __aexit__ method
[572] Fix | Delete
signature.
[573] Fix | Delete
[574] Fix | Delete
Can suppress exceptions the same way __aexit__ method can.
[575] Fix | Delete
Also accepts any object with an __aexit__ method (registering a call
[576] Fix | Delete
to the method instead of the object itself).
[577] Fix | Delete
"""
[578] Fix | Delete
_cb_type = type(exit)
[579] Fix | Delete
try:
[580] Fix | Delete
exit_method = _cb_type.__aexit__
[581] Fix | Delete
except AttributeError:
[582] Fix | Delete
# Not an async context manager, so assume it's a coroutine function
[583] Fix | Delete
self._push_exit_callback(exit, False)
[584] Fix | Delete
else:
[585] Fix | Delete
self._push_async_cm_exit(exit, exit_method)
[586] Fix | Delete
return exit # Allow use as a decorator
[587] Fix | Delete
[588] Fix | Delete
def push_async_callback(*args, **kwds):
[589] Fix | Delete
"""Registers an arbitrary coroutine function and arguments.
[590] Fix | Delete
[591] Fix | Delete
Cannot suppress exceptions.
[592] Fix | Delete
"""
[593] Fix | Delete
if len(args) >= 2:
[594] Fix | Delete
self, callback, *args = args
[595] Fix | Delete
elif not args:
[596] Fix | Delete
raise TypeError("descriptor 'push_async_callback' of "
[597] Fix | Delete
"'AsyncExitStack' object needs an argument")
[598] Fix | Delete
elif 'callback' in kwds:
[599] Fix | Delete
callback = kwds.pop('callback')
[600] Fix | Delete
self, *args = args
[601] Fix | Delete
import warnings
[602] Fix | Delete
warnings.warn("Passing 'callback' as keyword argument is deprecated",
[603] Fix | Delete
DeprecationWarning, stacklevel=2)
[604] Fix | Delete
else:
[605] Fix | Delete
raise TypeError('push_async_callback expected at least 1 '
[606] Fix | Delete
'positional argument, got %d' % (len(args)-1))
[607] Fix | Delete
[608] Fix | Delete
_exit_wrapper = self._create_async_cb_wrapper(callback, *args, **kwds)
[609] Fix | Delete
[610] Fix | Delete
# We changed the signature, so using @wraps is not appropriate, but
[611] Fix | Delete
# setting __wrapped__ may still help with introspection.
[612] Fix | Delete
_exit_wrapper.__wrapped__ = callback
[613] Fix | Delete
self._push_exit_callback(_exit_wrapper, False)
[614] Fix | Delete
return callback # Allow use as a decorator
[615] Fix | Delete
push_async_callback.__text_signature__ = '($self, callback, /, *args, **kwds)'
[616] Fix | Delete
[617] Fix | Delete
async def aclose(self):
[618] Fix | Delete
"""Immediately unwind the context stack."""
[619] Fix | Delete
await self.__aexit__(None, None, None)
[620] Fix | Delete
[621] Fix | Delete
def _push_async_cm_exit(self, cm, cm_exit):
[622] Fix | Delete
"""Helper to correctly register coroutine function to __aexit__
[623] Fix | Delete
method."""
[624] Fix | Delete
_exit_wrapper = self._create_async_exit_wrapper(cm, cm_exit)
[625] Fix | Delete
self._push_exit_callback(_exit_wrapper, False)
[626] Fix | Delete
[627] Fix | Delete
async def __aenter__(self):
[628] Fix | Delete
return self
[629] Fix | Delete
[630] Fix | Delete
async def __aexit__(self, *exc_details):
[631] Fix | Delete
received_exc = exc_details[0] is not None
[632] Fix | Delete
[633] Fix | Delete
# We manipulate the exception state so it behaves as though
[634] Fix | Delete
# we were actually nesting multiple with statements
[635] Fix | Delete
frame_exc = sys.exc_info()[1]
[636] Fix | Delete
def _fix_exception_context(new_exc, old_exc):
[637] Fix | Delete
# Context may not be correct, so find the end of the chain
[638] Fix | Delete
while 1:
[639] Fix | Delete
exc_context = new_exc.__context__
[640] Fix | Delete
if exc_context is old_exc:
[641] Fix | Delete
# Context is already set correctly (see issue 20317)
[642] Fix | Delete
return
[643] Fix | Delete
if exc_context is None or exc_context is frame_exc:
[644] Fix | Delete
break
[645] Fix | Delete
new_exc = exc_context
[646] Fix | Delete
# Change the end of the chain to point to the exception
[647] Fix | Delete
# we expect it to reference
[648] Fix | Delete
new_exc.__context__ = old_exc
[649] Fix | Delete
[650] Fix | Delete
# Callbacks are invoked in LIFO order to match the behaviour of
[651] Fix | Delete
# nested context managers
[652] Fix | Delete
suppressed_exc = False
[653] Fix | Delete
pending_raise = False
[654] Fix | Delete
while self._exit_callbacks:
[655] Fix | Delete
is_sync, cb = self._exit_callbacks.pop()
[656] Fix | Delete
try:
[657] Fix | Delete
if is_sync:
[658] Fix | Delete
cb_suppress = cb(*exc_details)
[659] Fix | Delete
else:
[660] Fix | Delete
cb_suppress = await cb(*exc_details)
[661] Fix | Delete
[662] Fix | Delete
if cb_suppress:
[663] Fix | Delete
suppressed_exc = True
[664] Fix | Delete
pending_raise = False
[665] Fix | Delete
exc_details = (None, None, None)
[666] Fix | Delete
except:
[667] Fix | Delete
new_exc_details = sys.exc_info()
[668] Fix | Delete
# simulate the stack of exceptions by setting the context
[669] Fix | Delete
_fix_exception_context(new_exc_details[1], exc_details[1])
[670] Fix | Delete
pending_raise = True
[671] Fix | Delete
exc_details = new_exc_details
[672] Fix | Delete
if pending_raise:
[673] Fix | Delete
try:
[674] Fix | Delete
# bare "raise exc_details[1]" replaces our carefully
[675] Fix | Delete
# set-up context
[676] Fix | Delete
fixed_ctx = exc_details[1].__context__
[677] Fix | Delete
raise exc_details[1]
[678] Fix | Delete
except BaseException:
[679] Fix | Delete
exc_details[1].__context__ = fixed_ctx
[680] Fix | Delete
raise
[681] Fix | Delete
return received_exc and suppressed_exc
[682] Fix | Delete
[683] Fix | Delete
[684] Fix | Delete
class nullcontext(AbstractContextManager):
[685] Fix | Delete
"""Context manager that does no additional processing.
[686] Fix | Delete
[687] Fix | Delete
Used as a stand-in for a normal context manager, when a particular
[688] Fix | Delete
block of code is only sometimes used with a normal context manager:
[689] Fix | Delete
[690] Fix | Delete
cm = optional_cm if condition else nullcontext()
[691] Fix | Delete
with cm:
[692] Fix | Delete
# Perform operation, using optional_cm if condition is True
[693] Fix | Delete
"""
[694] Fix | Delete
[695] Fix | Delete
def __init__(self, enter_result=None):
[696] Fix | Delete
self.enter_result = enter_result
[697] Fix | Delete
[698] Fix | Delete
def __enter__(self):
[699] Fix | Delete
return self.enter_result
[700] Fix | Delete
[701] Fix | Delete
def __exit__(self, *excinfo):
[702] Fix | Delete
pass
[703] Fix | Delete
[704] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function