contextlib.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. """Utilities for with-statement contexts. See PEP 343."""
  2. import sys
  3. from collections import deque
  4. from functools import wraps
  5. __all__ = ["contextmanager", "closing", "ContextDecorator", "ExitStack",
  6. "redirect_stdout", "redirect_stderr", "suppress"]
  7. class ContextDecorator(object):
  8. "A base class or mixin that enables context managers to work as decorators."
  9. def _recreate_cm(self):
  10. """Return a recreated instance of self.
  11. Allows an otherwise one-shot context manager like
  12. _GeneratorContextManager to support use as
  13. a decorator via implicit recreation.
  14. This is a private interface just for _GeneratorContextManager.
  15. See issue #11647 for details.
  16. """
  17. return self
  18. def __call__(self, func):
  19. @wraps(func)
  20. def inner(*args, **kwds):
  21. with self._recreate_cm():
  22. return func(*args, **kwds)
  23. return inner
  24. class _GeneratorContextManager(ContextDecorator):
  25. """Helper for @contextmanager decorator."""
  26. def __init__(self, func, args, kwds):
  27. self.gen = func(*args, **kwds)
  28. self.func, self.args, self.kwds = func, args, kwds
  29. # Issue 19330: ensure context manager instances have good docstrings
  30. doc = getattr(func, "__doc__", None)
  31. if doc is None:
  32. doc = type(self).__doc__
  33. self.__doc__ = doc
  34. # Unfortunately, this still doesn't provide good help output when
  35. # inspecting the created context manager instances, since pydoc
  36. # currently bypasses the instance docstring and shows the docstring
  37. # for the class instead.
  38. # See http://bugs.python.org/issue19404 for more details.
  39. def _recreate_cm(self):
  40. # _GCM instances are one-shot context managers, so the
  41. # CM must be recreated each time a decorated function is
  42. # called
  43. return self.__class__(self.func, self.args, self.kwds)
  44. def __enter__(self):
  45. try:
  46. return next(self.gen)
  47. except StopIteration:
  48. raise RuntimeError("generator didn't yield") from None
  49. def __exit__(self, type, value, traceback):
  50. if type is None:
  51. try:
  52. next(self.gen)
  53. except StopIteration:
  54. return
  55. else:
  56. raise RuntimeError("generator didn't stop")
  57. else:
  58. if value is None:
  59. # Need to force instantiation so we can reliably
  60. # tell if we get the same exception back
  61. value = type()
  62. try:
  63. self.gen.throw(type, value, traceback)
  64. raise RuntimeError("generator didn't stop after throw()")
  65. except StopIteration as exc:
  66. # Suppress StopIteration *unless* it's the same exception that
  67. # was passed to throw(). This prevents a StopIteration
  68. # raised inside the "with" statement from being suppressed.
  69. return exc is not value
  70. except RuntimeError as exc:
  71. # Likewise, avoid suppressing if a StopIteration exception
  72. # was passed to throw() and later wrapped into a RuntimeError
  73. # (see PEP 479).
  74. if exc.__cause__ is value:
  75. return False
  76. raise
  77. except:
  78. # only re-raise if it's *not* the exception that was
  79. # passed to throw(), because __exit__() must not raise
  80. # an exception unless __exit__() itself failed. But throw()
  81. # has to raise the exception to signal propagation, so this
  82. # fixes the impedance mismatch between the throw() protocol
  83. # and the __exit__() protocol.
  84. #
  85. if sys.exc_info()[1] is not value:
  86. raise
  87. def contextmanager(func):
  88. """@contextmanager decorator.
  89. Typical usage:
  90. @contextmanager
  91. def some_generator(<arguments>):
  92. <setup>
  93. try:
  94. yield <value>
  95. finally:
  96. <cleanup>
  97. This makes this:
  98. with some_generator(<arguments>) as <variable>:
  99. <body>
  100. equivalent to this:
  101. <setup>
  102. try:
  103. <variable> = <value>
  104. <body>
  105. finally:
  106. <cleanup>
  107. """
  108. @wraps(func)
  109. def helper(*args, **kwds):
  110. return _GeneratorContextManager(func, args, kwds)
  111. return helper
  112. class closing(object):
  113. """Context to automatically close something at the end of a block.
  114. Code like this:
  115. with closing(<module>.open(<arguments>)) as f:
  116. <block>
  117. is equivalent to this:
  118. f = <module>.open(<arguments>)
  119. try:
  120. <block>
  121. finally:
  122. f.close()
  123. """
  124. def __init__(self, thing):
  125. self.thing = thing
  126. def __enter__(self):
  127. return self.thing
  128. def __exit__(self, *exc_info):
  129. self.thing.close()
  130. class _RedirectStream:
  131. _stream = None
  132. def __init__(self, new_target):
  133. self._new_target = new_target
  134. # We use a list of old targets to make this CM re-entrant
  135. self._old_targets = []
  136. def __enter__(self):
  137. self._old_targets.append(getattr(sys, self._stream))
  138. setattr(sys, self._stream, self._new_target)
  139. return self._new_target
  140. def __exit__(self, exctype, excinst, exctb):
  141. setattr(sys, self._stream, self._old_targets.pop())
  142. class redirect_stdout(_RedirectStream):
  143. """Context manager for temporarily redirecting stdout to another file.
  144. # How to send help() to stderr
  145. with redirect_stdout(sys.stderr):
  146. help(dir)
  147. # How to write help() to a file
  148. with open('help.txt', 'w') as f:
  149. with redirect_stdout(f):
  150. help(pow)
  151. """
  152. _stream = "stdout"
  153. class redirect_stderr(_RedirectStream):
  154. """Context manager for temporarily redirecting stderr to another file."""
  155. _stream = "stderr"
  156. class suppress:
  157. """Context manager to suppress specified exceptions
  158. After the exception is suppressed, execution proceeds with the next
  159. statement following the with statement.
  160. with suppress(FileNotFoundError):
  161. os.remove(somefile)
  162. # Execution still resumes here if the file was already removed
  163. """
  164. def __init__(self, *exceptions):
  165. self._exceptions = exceptions
  166. def __enter__(self):
  167. pass
  168. def __exit__(self, exctype, excinst, exctb):
  169. # Unlike isinstance and issubclass, CPython exception handling
  170. # currently only looks at the concrete type hierarchy (ignoring
  171. # the instance and subclass checking hooks). While Guido considers
  172. # that a bug rather than a feature, it's a fairly hard one to fix
  173. # due to various internal implementation details. suppress provides
  174. # the simpler issubclass based semantics, rather than trying to
  175. # exactly reproduce the limitations of the CPython interpreter.
  176. #
  177. # See http://bugs.python.org/issue12029 for more details
  178. return exctype is not None and issubclass(exctype, self._exceptions)
  179. # Inspired by discussions on http://bugs.python.org/issue13585
  180. class ExitStack(object):
  181. """Context manager for dynamic management of a stack of exit callbacks
  182. For example:
  183. with ExitStack() as stack:
  184. files = [stack.enter_context(open(fname)) for fname in filenames]
  185. # All opened files will automatically be closed at the end of
  186. # the with statement, even if attempts to open files later
  187. # in the list raise an exception
  188. """
  189. def __init__(self):
  190. self._exit_callbacks = deque()
  191. def pop_all(self):
  192. """Preserve the context stack by transferring it to a new instance"""
  193. new_stack = type(self)()
  194. new_stack._exit_callbacks = self._exit_callbacks
  195. self._exit_callbacks = deque()
  196. return new_stack
  197. def _push_cm_exit(self, cm, cm_exit):
  198. """Helper to correctly register callbacks to __exit__ methods"""
  199. def _exit_wrapper(*exc_details):
  200. return cm_exit(cm, *exc_details)
  201. _exit_wrapper.__self__ = cm
  202. self.push(_exit_wrapper)
  203. def push(self, exit):
  204. """Registers a callback with the standard __exit__ method signature
  205. Can suppress exceptions the same way __exit__ methods can.
  206. Also accepts any object with an __exit__ method (registering a call
  207. to the method instead of the object itself)
  208. """
  209. # We use an unbound method rather than a bound method to follow
  210. # the standard lookup behaviour for special methods
  211. _cb_type = type(exit)
  212. try:
  213. exit_method = _cb_type.__exit__
  214. except AttributeError:
  215. # Not a context manager, so assume its a callable
  216. self._exit_callbacks.append(exit)
  217. else:
  218. self._push_cm_exit(exit, exit_method)
  219. return exit # Allow use as a decorator
  220. def callback(self, callback, *args, **kwds):
  221. """Registers an arbitrary callback and arguments.
  222. Cannot suppress exceptions.
  223. """
  224. def _exit_wrapper(exc_type, exc, tb):
  225. callback(*args, **kwds)
  226. # We changed the signature, so using @wraps is not appropriate, but
  227. # setting __wrapped__ may still help with introspection
  228. _exit_wrapper.__wrapped__ = callback
  229. self.push(_exit_wrapper)
  230. return callback # Allow use as a decorator
  231. def enter_context(self, cm):
  232. """Enters the supplied context manager
  233. If successful, also pushes its __exit__ method as a callback and
  234. returns the result of the __enter__ method.
  235. """
  236. # We look up the special methods on the type to match the with statement
  237. _cm_type = type(cm)
  238. _exit = _cm_type.__exit__
  239. result = _cm_type.__enter__(cm)
  240. self._push_cm_exit(cm, _exit)
  241. return result
  242. def close(self):
  243. """Immediately unwind the context stack"""
  244. self.__exit__(None, None, None)
  245. def __enter__(self):
  246. return self
  247. def __exit__(self, *exc_details):
  248. received_exc = exc_details[0] is not None
  249. # We manipulate the exception state so it behaves as though
  250. # we were actually nesting multiple with statements
  251. frame_exc = sys.exc_info()[1]
  252. def _fix_exception_context(new_exc, old_exc):
  253. # Context may not be correct, so find the end of the chain
  254. while 1:
  255. exc_context = new_exc.__context__
  256. if exc_context is old_exc:
  257. # Context is already set correctly (see issue 20317)
  258. return
  259. if exc_context is None or exc_context is frame_exc:
  260. break
  261. new_exc = exc_context
  262. # Change the end of the chain to point to the exception
  263. # we expect it to reference
  264. new_exc.__context__ = old_exc
  265. # Callbacks are invoked in LIFO order to match the behaviour of
  266. # nested context managers
  267. suppressed_exc = False
  268. pending_raise = False
  269. while self._exit_callbacks:
  270. cb = self._exit_callbacks.pop()
  271. try:
  272. if cb(*exc_details):
  273. suppressed_exc = True
  274. pending_raise = False
  275. exc_details = (None, None, None)
  276. except:
  277. new_exc_details = sys.exc_info()
  278. # simulate the stack of exceptions by setting the context
  279. _fix_exception_context(new_exc_details[1], exc_details[1])
  280. pending_raise = True
  281. exc_details = new_exc_details
  282. if pending_raise:
  283. try:
  284. # bare "raise exc_details[1]" replaces our carefully
  285. # set-up context
  286. fixed_ctx = exc_details[1].__context__
  287. raise exc_details[1]
  288. except BaseException:
  289. exc_details[1].__context__ = fixed_ctx
  290. raise
  291. return received_exc and suppressed_exc