Commit Graph

75 Commits

Author SHA1 Message Date
Irit Katriel e65e587f93
gh-92118: Add test for traceback when exception is modified by (Async)ExitStack.__exit__ (GH-92339) 2022-05-05 17:39:33 +01:00
Irit Katriel e61330b44f
gh-92118: fix traceback of exceptions propagated from inside a contextlib.contextmanager (GH-92202) 2022-05-04 19:40:47 +01:00
Nikita Sobolev 22f73bd9f1
bpo-46425: Fix direct invocation of `test_contextlib` (GH-30681) 2022-01-21 09:36:19 +02:00
Filipe Laíns 3592980f91
bpo-25625: add contextlib.chdir (GH-28271)
Added non parallel-safe :func:`~contextlib.chdir` context manager to change
the current working directory and then restore it on exit. Simple wrapper
around :func:`~os.chdir`.

Signed-off-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-10-20 00:19:27 +02:00
John Belmonte e6d1aa1ac6
bpo-44594: fix (Async)ExitStack handling of __context__ (gh-27089)
* bpo-44594: fix (Async)ExitStack handling of __context__

Make enter_context(foo()) / enter_async_context(foo()) equivalent to
`[async] with foo()` regarding __context__ when an exception is raised.

Previously exceptions would be caught and re-raised with the wrong
context when explicitly overriding __context__ with None.
2021-10-03 23:49:55 -07:00
Nick Coghlan a2c45e5bcf
bpo-44515: handle non-refcounted GC in contextlib tests (GH-26910)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-07-26 22:57:17 +02:00
Thomas Grainger 7f1c330da3
bpo-44566: resolve differences between asynccontextmanager and contextmanager (#27024) 2021-07-20 20:15:07 +02:00
Serhiy Storchaka 6cb145d23f
bpo-44471: Change error type for bad objects in ExitStack.enter_context() (GH-26820)
A TypeError is now raised instead of an AttributeError in
ExitStack.enter_context() and AsyncExitStack.enter_async_context()
for objects which do not support the context manager or
asynchronous context manager protocols correspondingly.
2021-06-29 11:28:15 +03:00
Serhiy Storchaka 20a88004ba
bpo-12022: Change error type for bad objects in "with" and "async with" (GH-26809)
A TypeError is now raised instead of an AttributeError in
"with" and "async with" statements for objects which do not
support the context manager or asynchronous context manager
protocols correspondingly.
2021-06-29 11:27:04 +03:00
Inada Naoki 35715d1e72
bpo-43651: PEP 597: Fix EncodingWarning in some tests (GH-25145)
* test_asyncio
* test_bz2
* test_math
* test_cmath
* test_cmd_line
* test_cmd_line_script
* test_compile
* test_contextlib
* test_profile
* ctypes/test/test_find
* test_multiprocessing
* test_configparser
* test_csv
* test_dbm_dumb
* test_decimal
* test_difflib
* os.fdopen() calls io.text_encoding() to emit EncodingWarning for right place.
2021-04-04 09:01:23 +09:00
Hai Shi 96a6a6d42b
bpo-40275: Use new test.support helper submodules in tests (GH-21412) 2020-07-09 15:25:10 +02:00
Serhiy Storchaka 142566c028
[3.9] bpo-37116: Use PEP 570 syntax for positional-only parameters. (GH-12620)
Turn deprecation warnings added in 3.8 into TypeError.
2019-06-05 18:22:31 +03:00
Serhiy Storchaka 42a139ed88
bpo-36492: Deprecate passing some arguments as keyword arguments. (GH-12637)
Deprecated passing the following arguments as keyword arguments:

- "func" in functools.partialmethod(), weakref.finalize(),
  profile.Profile.runcall(), cProfile.Profile.runcall(),
  bdb.Bdb.runcall(), trace.Trace.runfunc() and
  curses.wrapper().
- "function" in unittest.addModuleCleanup() and
  unittest.TestCase.addCleanup().
- "fn" in the submit() method of concurrent.futures.ThreadPoolExecutor
  and concurrent.futures.ProcessPoolExecutor.
- "callback" in contextlib.ExitStack.callback(),
  contextlib.AsyncExitStack.callback() and
  contextlib.AsyncExitStack.push_async_callback().
- "c" and "typeid" in the create() method of multiprocessing.managers.Server
  and multiprocessing.managers.SharedMemoryServer.
- "obj" in weakref.finalize().

Also allowed to pass arbitrary keyword arguments (even "self" and "func")
if the above arguments are passed as positional argument.
2019-04-01 09:16:35 +03:00
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి) 70f61b933f bpo-35202: Remove unused imports in Lib directory (GH-10450) 2018-11-15 21:59:19 +09:00
Martin Teichmann dd0e087edc bpo-30306: release arguments of contextmanager (GH-1500)
The arguments to a generator function which is declared as a
contextmanager are stored inside the context manager, and
thus are kept alive, even when it is used as a regular context
manager, and not as a function decorator (where it needs
the original arguments to recreate the generator on each
call).

This is a possible unnecessary memory leak, so this changes
contextmanager.__enter__ to release the saved arguments,
as that method being called means that particular CM instance
isn't going to need to recreate the underlying generator.

Patch by Martin Teichmann.
2018-01-28 14:17:46 +10:00
Ilya Kulakov 1aa094f740 bpo-29302: Implement contextlib.AsyncExitStack. (#4790) 2018-01-25 15:51:18 -05:00
Jesse-Bakker 0784a2e5b1 bpo-10049: Add a "no-op" (null) context manager to contextlib (GH-4464)
Adds a simpler and faster alternative to ExitStack for handling
single optional context managers without having to change the
lexical structure of your code.
2017-11-23 10:23:28 +10:00
Antoine Pitrou a6a4dc816d bpo-31370: Remove support for threads-less builds (#3385)
* Remove Setup.config
* Always define WITH_THREAD for compatibility.
2017-09-07 18:56:24 +02:00
Jelle Zijlstra 57161aac5e bpo-30266: support "= None" pattern in AbstractContextManager (#1448)
contextlib.AbstractContextManager now supports anti-registration
by setting __enter__ = None or __exit__ = None, following the pattern
introduced in bpo-25958.
2017-06-09 08:21:47 -07:00
svelankar 00c75e9a45 bpo-29692: contextlib.contextmanager may incorrectly unchain RuntimeError (GH-949)
contextlib._GeneratorContextManager.__exit__ includes a special case to deal with
PEP 479 RuntimeErrors created when `StopIteration` is thrown into the context
manager body.

Previously this check was too permissive, and undid one level of chaining on *all*
RuntimeError instances, not just those that wrapped a StopIteration instance.
2017-04-11 19:11:13 +10:00
Serhiy Storchaka 9a573a0c23 Added more tests for issue #27122. 2016-06-20 05:30:31 +03:00
Serhiy Storchaka ce1a9f309f Added more tests for issue #27122. 2016-06-20 05:29:54 +03:00
Gregory P. Smith 881aa38972 Issue #27123: When an exception is raised within the context being
managed by a contextlib.ExitStack() and one of the exit stack
generators catches and raises it in a chain, do not re-raise the
original exception when exiting, let the new chained one through.
This avoids the PEP 479 bug described in issue25782.
2016-06-14 09:24:31 -07:00
Gregory P. Smith ba2ecd6841 Issue #27123: When an exception is raised within the context being
managed by a contextlib.ExitStack() and one of the exit stack
generators catches and raises it in a chain, do not re-raise the
original exception when exiting, let the new chained one through.
This avoids the PEP 479 bug described in issue25782.
2016-06-14 09:19:20 -07:00
Brett Cannon 9e080e0e74 Issue #25609: Introduce contextlib.AbstractContextManager and
typing.ContextManager.
2016-04-08 12:15:27 -07:00
Martin Panter 7e3a91a5fc Issue #26136: Upgrade the generator_stop warning to DeprecationWarning
Patch by Anish Shah.
2016-02-10 04:40:48 +00:00
Martin Panter 05da525e55 Issue #25322: Merge contextlib.suppress test fix from 3.4 into 3.5 2015-10-10 11:05:47 +00:00
Martin Panter 7c6420a552 Issue #25322: Fix test for nested contextlib.suppress 2015-10-10 11:04:44 +00:00
Serhiy Storchaka eab7704044 Issue #24336: The contextmanager decorator now works with functions with
keyword arguments called "func" and "self".  Patch by Martin Panter.
2015-06-28 17:08:35 +03:00
Serhiy Storchaka 101ff3541c Issue #24336: The contextmanager decorator now works with functions with
keyword arguments called "func" and "self".  Patch by Martin Panter.
2015-06-28 17:06:07 +03:00
Yury Selivanov 683333955a Issue 24237: Raise PendingDeprecationWarning per PEP 479
Raise PendingDeprecationWarning when generator raises StopIteration
and no __future__ import is used.  Fix offenders in the stdlib
and tests.

See also issue 22906.
Thanks to Nick Coghlan and Berker Peksag for reviews.
2015-05-22 11:16:47 -04:00
Yury Selivanov 8170e8c0d1 PEP 479: Change StopIteration handling inside generators.
Closes issue #22906.
2015-05-09 11:44:30 -04:00
Berker Peksag bb44fe0a0b Issue #22389: Add contextlib.redirect_stderr(). 2014-11-28 23:28:06 +02:00
Nick Coghlan b3c0f4067d Merge removal of issue 20317 debugging code from 3.3 2014-01-24 23:07:16 +10:00
Nick Coghlan add94c9d82 Issue 20317: Remove debugging code from contextlib
- Alex J Burke noticed a debugging raise in the commit that
  fixed the original bug reported in issue 20317
- this showed that multiple iterations through the affected
  loop wasn't actually being tested
2014-01-24 23:05:45 +10:00
Nick Coghlan d58831e688 Merge #20317 from 3.3 2014-01-22 23:04:37 +10:00
Nick Coghlan 09761e7c9c Issue #20317: Don't create a reference loop in ExitStack 2014-01-22 22:24:46 +10:00
Nick Coghlan 8e113b418d Close #19403: make contextlib.redirect_stdout reentrant 2013-11-03 17:00:51 +10:00
Nick Coghlan 561eb5cbe4 Issue #19330: Handle the no-docstrings case in tests 2013-10-26 22:20:43 +10:00
Nick Coghlan 059def58a5 Close #19330 by using public classes in contextlib
- added test cases to ensure docstrings are reasonable
- also updates various comments in contextlib for accuracy
- identifed #19404 as an issue making it difficult to provide
  good help output on generator based context manager instances
2013-10-26 18:08:15 +10:00
Nick Coghlan 0ddaed3456 Close #19396: make test_contextlib tolerate -S 2013-10-26 16:37:47 +10:00
Nick Coghlan 8608d26e81 contextlib doc updates and refactoring
- explain single use, reusable and reentrant in docs
- converted suppress to a reentrant class based impl
- converted redirect_stdout to a reusable impl
- moved both suppress and redirect_stdout behind a functional
  facade
- added reentrancy tests for the updated suppress
- added reusability tests for the updated redirect_stdio
- slightly cleaned up an exception from contextmanager
2013-10-20 00:30:51 +10:00
Nick Coghlan 240f86d7dd Close #19266: contextlib.ignore -> contextlib.suppress
Patch by Zero Piraeus.
2013-10-17 23:40:57 +10:00
Raymond Hettinger 1254b407ac Rename contextlib.ignored() to contextlib.ignore(). 2013-10-10 22:39:39 -07:00
Raymond Hettinger 088cbf2d39 Issue #15805: Add contextlib.redirect_stdout() 2013-10-10 00:46:57 -07:00
Nick Coghlan e6f4631f08 Merge #19092 from 3.3 2013-10-01 23:28:00 +10:00
Nick Coghlan 1a33b2f35b Close #19092: ExitStack now reraises exceptions from __exit__
Report and patch by Hrvoje Nikšić
2013-10-01 23:24:56 +10:00
Brett Cannon 3e9a9ae09d Update various test modules to use unittest.main() for test discovery
instead of manually listing tests for test.support.run_unittest().
2013-06-12 21:25:59 -04:00
Raymond Hettinger e318a883fe Issue #15806: Add contextlib.ignored(). 2013-03-10 22:26:51 -07:00
Nick Coghlan 77452fc121 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack 2012-06-01 22:48:32 +10:00