contextlib2.py 14 KB

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