contextlib2.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. """contextlib2 - backports and enhancements to the contextlib module"""
  2. import abc
  3. import sys
  4. import warnings
  5. from collections import deque
  6. from functools import wraps
  7. __all__ = ["contextmanager", "closing", "nullcontext",
  8. "AbstractContextManager",
  9. "ContextDecorator", "ExitStack",
  10. "redirect_stdout", "redirect_stderr", "suppress"]
  11. # Backwards compatibility
  12. __all__ += ["ContextStack"]
  13. # Backport abc.ABC
  14. if sys.version_info[:2] >= (3, 4):
  15. _abc_ABC = abc.ABC
  16. else:
  17. _abc_ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
  18. # Backport classic class MRO
  19. def _classic_mro(C, result):
  20. if C in result:
  21. return
  22. result.append(C)
  23. for B in C.__bases__:
  24. _classic_mro(B, result)
  25. return result
  26. # Backport _collections_abc._check_methods
  27. def _check_methods(C, *methods):
  28. try:
  29. mro = C.__mro__
  30. except AttributeError:
  31. mro = tuple(_classic_mro(C, []))
  32. for method in methods:
  33. for B in mro:
  34. if method in B.__dict__:
  35. if B.__dict__[method] is None:
  36. return NotImplemented
  37. break
  38. else:
  39. return NotImplemented
  40. return True
  41. class AbstractContextManager(_abc_ABC):
  42. """An abstract base class for context managers."""
  43. def __enter__(self):
  44. """Return `self` upon entering the runtime context."""
  45. return self
  46. @abc.abstractmethod
  47. def __exit__(self, exc_type, exc_value, traceback):
  48. """Raise any exception triggered within the runtime context."""
  49. return None
  50. @classmethod
  51. def __subclasshook__(cls, C):
  52. """Check whether subclass is considered a subclass of this ABC."""
  53. if cls is AbstractContextManager:
  54. return _check_methods(C, "__enter__", "__exit__")
  55. return NotImplemented
  56. class ContextDecorator(object):
  57. """A base class or mixin that enables context managers to work as decorators."""
  58. def refresh_cm(self):
  59. """Returns the context manager used to actually wrap the call to the
  60. decorated function.
  61. The default implementation just returns *self*.
  62. Overriding this method allows otherwise one-shot context managers
  63. like _GeneratorContextManager to support use as decorators via
  64. implicit recreation.
  65. DEPRECATED: refresh_cm was never added to the standard library's
  66. ContextDecorator API
  67. """
  68. warnings.warn("refresh_cm was never added to the standard library",
  69. DeprecationWarning)
  70. return self._recreate_cm()
  71. def _recreate_cm(self):
  72. """Return a recreated instance of self.
  73. Allows an otherwise one-shot context manager like
  74. _GeneratorContextManager to support use as
  75. a decorator via implicit recreation.
  76. This is a private interface just for _GeneratorContextManager.
  77. See issue #11647 for details.
  78. """
  79. return self
  80. def __call__(self, func):
  81. @wraps(func)
  82. def inner(*args, **kwds):
  83. with self._recreate_cm():
  84. return func(*args, **kwds)
  85. return inner
  86. class _GeneratorContextManager(ContextDecorator):
  87. """Helper for @contextmanager decorator."""
  88. def __init__(self, func, args, kwds):
  89. self.gen = func(*args, **kwds)
  90. self.func, self.args, self.kwds = func, args, kwds
  91. # Issue 19330: ensure context manager instances have good docstrings
  92. doc = getattr(func, "__doc__", None)
  93. if doc is None:
  94. doc = type(self).__doc__
  95. self.__doc__ = doc
  96. # Unfortunately, this still doesn't provide good help output when
  97. # inspecting the created context manager instances, since pydoc
  98. # currently bypasses the instance docstring and shows the docstring
  99. # for the class instead.
  100. # See http://bugs.python.org/issue19404 for more details.
  101. def _recreate_cm(self):
  102. # _GCM instances are one-shot context managers, so the
  103. # CM must be recreated each time a decorated function is
  104. # called
  105. return self.__class__(self.func, self.args, self.kwds)
  106. def __enter__(self):
  107. try:
  108. return next(self.gen)
  109. except StopIteration:
  110. raise RuntimeError("generator didn't yield")
  111. def __exit__(self, type, value, traceback):
  112. if type is None:
  113. try:
  114. next(self.gen)
  115. except StopIteration:
  116. return
  117. else:
  118. raise RuntimeError("generator didn't stop")
  119. else:
  120. if value is None:
  121. # Need to force instantiation so we can reliably
  122. # tell if we get the same exception back
  123. value = type()
  124. try:
  125. self.gen.throw(type, value, traceback)
  126. raise RuntimeError("generator didn't stop after throw()")
  127. except StopIteration as exc:
  128. # Suppress StopIteration *unless* it's the same exception that
  129. # was passed to throw(). This prevents a StopIteration
  130. # raised inside the "with" statement from being suppressed.
  131. return exc is not value
  132. except RuntimeError as exc:
  133. # Don't re-raise the passed in exception
  134. if exc is value:
  135. return False
  136. # Likewise, avoid suppressing if a StopIteration exception
  137. # was passed to throw() and later wrapped into a RuntimeError
  138. # (see PEP 479).
  139. if _HAVE_EXCEPTION_CHAINING and exc.__cause__ is value:
  140. return False
  141. raise
  142. except:
  143. # only re-raise if it's *not* the exception that was
  144. # passed to throw(), because __exit__() must not raise
  145. # an exception unless __exit__() itself failed. But throw()
  146. # has to raise the exception to signal propagation, so this
  147. # fixes the impedance mismatch between the throw() protocol
  148. # and the __exit__() protocol.
  149. #
  150. if sys.exc_info()[1] is not value:
  151. raise
  152. def contextmanager(func):
  153. """@contextmanager decorator.
  154. Typical usage:
  155. @contextmanager
  156. def some_generator(<arguments>):
  157. <setup>
  158. try:
  159. yield <value>
  160. finally:
  161. <cleanup>
  162. This makes this:
  163. with some_generator(<arguments>) as <variable>:
  164. <body>
  165. equivalent to this:
  166. <setup>
  167. try:
  168. <variable> = <value>
  169. <body>
  170. finally:
  171. <cleanup>
  172. """
  173. @wraps(func)
  174. def helper(*args, **kwds):
  175. return _GeneratorContextManager(func, args, kwds)
  176. return helper
  177. class closing(object):
  178. """Context to automatically close something at the end of a block.
  179. Code like this:
  180. with closing(<module>.open(<arguments>)) as f:
  181. <block>
  182. is equivalent to this:
  183. f = <module>.open(<arguments>)
  184. try:
  185. <block>
  186. finally:
  187. f.close()
  188. """
  189. def __init__(self, thing):
  190. self.thing = thing
  191. def __enter__(self):
  192. return self.thing
  193. def __exit__(self, *exc_info):
  194. self.thing.close()
  195. class _RedirectStream(object):
  196. _stream = None
  197. def __init__(self, new_target):
  198. self._new_target = new_target
  199. # We use a list of old targets to make this CM re-entrant
  200. self._old_targets = []
  201. def __enter__(self):
  202. self._old_targets.append(getattr(sys, self._stream))
  203. setattr(sys, self._stream, self._new_target)
  204. return self._new_target
  205. def __exit__(self, exctype, excinst, exctb):
  206. setattr(sys, self._stream, self._old_targets.pop())
  207. class redirect_stdout(_RedirectStream):
  208. """Context manager for temporarily redirecting stdout to another file.
  209. # How to send help() to stderr
  210. with redirect_stdout(sys.stderr):
  211. help(dir)
  212. # How to write help() to a file
  213. with open('help.txt', 'w') as f:
  214. with redirect_stdout(f):
  215. help(pow)
  216. """
  217. _stream = "stdout"
  218. class redirect_stderr(_RedirectStream):
  219. """Context manager for temporarily redirecting stderr to another file."""
  220. _stream = "stderr"
  221. class suppress(object):
  222. """Context manager to suppress specified exceptions
  223. After the exception is suppressed, execution proceeds with the next
  224. statement following the with statement.
  225. with suppress(FileNotFoundError):
  226. os.remove(somefile)
  227. # Execution still resumes here if the file was already removed
  228. """
  229. def __init__(self, *exceptions):
  230. self._exceptions = exceptions
  231. def __enter__(self):
  232. pass
  233. def __exit__(self, exctype, excinst, exctb):
  234. # Unlike isinstance and issubclass, CPython exception handling
  235. # currently only looks at the concrete type hierarchy (ignoring
  236. # the instance and subclass checking hooks). While Guido considers
  237. # that a bug rather than a feature, it's a fairly hard one to fix
  238. # due to various internal implementation details. suppress provides
  239. # the simpler issubclass based semantics, rather than trying to
  240. # exactly reproduce the limitations of the CPython interpreter.
  241. #
  242. # See http://bugs.python.org/issue12029 for more details
  243. return exctype is not None and issubclass(exctype, self._exceptions)
  244. # Context manipulation is Python 3 only
  245. _HAVE_EXCEPTION_CHAINING = sys.version_info[0] >= 3
  246. if _HAVE_EXCEPTION_CHAINING:
  247. def _make_context_fixer(frame_exc):
  248. def _fix_exception_context(new_exc, old_exc):
  249. # Context may not be correct, so find the end of the chain
  250. while 1:
  251. exc_context = new_exc.__context__
  252. if exc_context is old_exc:
  253. # Context is already set correctly (see issue 20317)
  254. return
  255. if exc_context is None or exc_context is frame_exc:
  256. break
  257. new_exc = exc_context
  258. # Change the end of the chain to point to the exception
  259. # we expect it to reference
  260. new_exc.__context__ = old_exc
  261. return _fix_exception_context
  262. def _reraise_with_existing_context(exc_details):
  263. try:
  264. # bare "raise exc_details[1]" replaces our carefully
  265. # set-up context
  266. fixed_ctx = exc_details[1].__context__
  267. raise exc_details[1]
  268. except BaseException:
  269. exc_details[1].__context__ = fixed_ctx
  270. raise
  271. else:
  272. # No exception context in Python 2
  273. def _make_context_fixer(frame_exc):
  274. return lambda new_exc, old_exc: None
  275. # Use 3 argument raise in Python 2,
  276. # but use exec to avoid SyntaxError in Python 3
  277. def _reraise_with_existing_context(exc_details):
  278. exc_type, exc_value, exc_tb = exc_details
  279. exec("raise exc_type, exc_value, exc_tb")
  280. # Handle old-style classes if they exist
  281. try:
  282. from types import InstanceType
  283. except ImportError:
  284. # Python 3 doesn't have old-style classes
  285. _get_type = type
  286. else:
  287. # Need to handle old-style context managers on Python 2
  288. def _get_type(obj):
  289. obj_type = type(obj)
  290. if obj_type is InstanceType:
  291. return obj.__class__ # Old-style class
  292. return obj_type # New-style class
  293. # Inspired by discussions on http://bugs.python.org/issue13585
  294. class ExitStack(object):
  295. """Context manager for dynamic management of a stack of exit callbacks
  296. For example:
  297. with ExitStack() as stack:
  298. files = [stack.enter_context(open(fname)) for fname in filenames]
  299. # All opened files will automatically be closed at the end of
  300. # the with statement, even if attempts to open files later
  301. # in the list raise an exception
  302. """
  303. def __init__(self):
  304. self._exit_callbacks = deque()
  305. def pop_all(self):
  306. """Preserve the context stack by transferring it to a new instance"""
  307. new_stack = type(self)()
  308. new_stack._exit_callbacks = self._exit_callbacks
  309. self._exit_callbacks = deque()
  310. return new_stack
  311. def _push_cm_exit(self, cm, cm_exit):
  312. """Helper to correctly register callbacks to __exit__ methods"""
  313. def _exit_wrapper(*exc_details):
  314. return cm_exit(cm, *exc_details)
  315. _exit_wrapper.__self__ = cm
  316. self.push(_exit_wrapper)
  317. def push(self, exit):
  318. """Registers a callback with the standard __exit__ method signature
  319. Can suppress exceptions the same way __exit__ methods can.
  320. Also accepts any object with an __exit__ method (registering a call
  321. to the method instead of the object itself)
  322. """
  323. # We use an unbound method rather than a bound method to follow
  324. # the standard lookup behaviour for special methods
  325. _cb_type = _get_type(exit)
  326. try:
  327. exit_method = _cb_type.__exit__
  328. except AttributeError:
  329. # Not a context manager, so assume its a callable
  330. self._exit_callbacks.append(exit)
  331. else:
  332. self._push_cm_exit(exit, exit_method)
  333. return exit # Allow use as a decorator
  334. def callback(self, callback, *args, **kwds):
  335. """Registers an arbitrary callback and arguments.
  336. Cannot suppress exceptions.
  337. """
  338. def _exit_wrapper(exc_type, exc, tb):
  339. callback(*args, **kwds)
  340. # We changed the signature, so using @wraps is not appropriate, but
  341. # setting __wrapped__ may still help with introspection
  342. _exit_wrapper.__wrapped__ = callback
  343. self.push(_exit_wrapper)
  344. return callback # Allow use as a decorator
  345. def enter_context(self, cm):
  346. """Enters the supplied context manager
  347. If successful, also pushes its __exit__ method as a callback and
  348. returns the result of the __enter__ method.
  349. """
  350. # We look up the special methods on the type to match the with statement
  351. _cm_type = _get_type(cm)
  352. _exit = _cm_type.__exit__
  353. result = _cm_type.__enter__(cm)
  354. self._push_cm_exit(cm, _exit)
  355. return result
  356. def close(self):
  357. """Immediately unwind the context stack"""
  358. self.__exit__(None, None, None)
  359. def __enter__(self):
  360. return self
  361. def __exit__(self, *exc_details):
  362. received_exc = exc_details[0] is not None
  363. # We manipulate the exception state so it behaves as though
  364. # we were actually nesting multiple with statements
  365. frame_exc = sys.exc_info()[1]
  366. _fix_exception_context = _make_context_fixer(frame_exc)
  367. # Callbacks are invoked in LIFO order to match the behaviour of
  368. # nested context managers
  369. suppressed_exc = False
  370. pending_raise = False
  371. while self._exit_callbacks:
  372. cb = self._exit_callbacks.pop()
  373. try:
  374. if cb(*exc_details):
  375. suppressed_exc = True
  376. pending_raise = False
  377. exc_details = (None, None, None)
  378. except:
  379. new_exc_details = sys.exc_info()
  380. # simulate the stack of exceptions by setting the context
  381. _fix_exception_context(new_exc_details[1], exc_details[1])
  382. pending_raise = True
  383. exc_details = new_exc_details
  384. if pending_raise:
  385. _reraise_with_existing_context(exc_details)
  386. return received_exc and suppressed_exc
  387. # Preserve backwards compatibility
  388. class ContextStack(ExitStack):
  389. """Backwards compatibility alias for ExitStack"""
  390. def __init__(self):
  391. warnings.warn("ContextStack has been renamed to ExitStack",
  392. DeprecationWarning)
  393. super(ContextStack, self).__init__()
  394. def register_exit(self, callback):
  395. return self.push(callback)
  396. def register(self, callback, *args, **kwds):
  397. return self.callback(callback, *args, **kwds)
  398. def preserve(self):
  399. return self.pop_all()
  400. class nullcontext(AbstractContextManager):
  401. """Context manager that does no additional processing.
  402. Used as a stand-in for a normal context manager, when a particular
  403. block of code is only sometimes used with a normal context manager:
  404. cm = optional_cm if condition else nullcontext()
  405. with cm:
  406. # Perform operation, using optional_cm if condition is True
  407. """
  408. def __init__(self, enter_result=None):
  409. self.enter_result = enter_result
  410. def __enter__(self):
  411. return self.enter_result
  412. def __exit__(self, *excinfo):
  413. pass