summaryrefslogtreecommitdiffstats
path: root/Lib/contextlib.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-04-01 06:16:35 (GMT)
committerGitHub <noreply@github.com>2019-04-01 06:16:35 (GMT)
commit42a139ed88c487f325a241c6ee8b308b3c045975 (patch)
treede56527188eff240b06496c3451e525af959dcad /Lib/contextlib.py
parent5f2c50810a67982b0c80f6d3258fee3647f67005 (diff)
downloadcpython-42a139ed88c487f325a241c6ee8b308b3c045975.zip
cpython-42a139ed88c487f325a241c6ee8b308b3c045975.tar.gz
cpython-42a139ed88c487f325a241c6ee8b308b3c045975.tar.bz2
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.
Diffstat (limited to 'Lib/contextlib.py')
-rw-r--r--Lib/contextlib.py40
1 files changed, 36 insertions, 4 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index c06ec73..ae498a2 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -377,7 +377,8 @@ class _BaseExitStack:
return MethodType(cm_exit, cm)
@staticmethod
- def _create_cb_wrapper(callback, *args, **kwds):
+ def _create_cb_wrapper(*args, **kwds):
+ callback, *args = args
def _exit_wrapper(exc_type, exc, tb):
callback(*args, **kwds)
return _exit_wrapper
@@ -426,11 +427,26 @@ class _BaseExitStack:
self._push_cm_exit(cm, _exit)
return result
- def callback(self, callback, *args, **kwds):
+ def callback(*args, **kwds):
"""Registers an arbitrary callback and arguments.
Cannot suppress exceptions.
"""
+ if len(args) >= 2:
+ self, callback, *args = args
+ elif not args:
+ raise TypeError("descriptor 'callback' of '_BaseExitStack' object "
+ "needs an argument")
+ elif 'callback' in kwds:
+ callback = kwds.pop('callback')
+ self, *args = args
+ import warnings
+ warnings.warn("Passing 'callback' as keyword argument is deprecated",
+ DeprecationWarning, stacklevel=2)
+ else:
+ raise TypeError('callback expected at least 1 positional argument, '
+ 'got %d' % (len(args)-1))
+
_exit_wrapper = self._create_cb_wrapper(callback, *args, **kwds)
# We changed the signature, so using @wraps is not appropriate, but
@@ -536,7 +552,8 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
return MethodType(cm_exit, cm)
@staticmethod
- def _create_async_cb_wrapper(callback, *args, **kwds):
+ def _create_async_cb_wrapper(*args, **kwds):
+ callback, *args = args
async def _exit_wrapper(exc_type, exc, tb):
await callback(*args, **kwds)
return _exit_wrapper
@@ -571,11 +588,26 @@ class AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager):
self._push_async_cm_exit(exit, exit_method)
return exit # Allow use as a decorator
- def push_async_callback(self, callback, *args, **kwds):
+ def push_async_callback(*args, **kwds):
"""Registers an arbitrary coroutine function and arguments.
Cannot suppress exceptions.
"""
+ if len(args) >= 2:
+ self, callback, *args = args
+ elif not args:
+ raise TypeError("descriptor 'push_async_callback' of "
+ "'AsyncExitStack' object needs an argument")
+ elif 'callback' in kwds:
+ callback = kwds.pop('callback')
+ self, *args = args
+ import warnings
+ warnings.warn("Passing 'callback' as keyword argument is deprecated",
+ DeprecationWarning, stacklevel=2)
+ else:
+ raise TypeError('push_async_callback expected at least 1 '
+ 'positional argument, got %d' % (len(args)-1))
+
_exit_wrapper = self._create_async_cb_wrapper(callback, *args, **kwds)
# We changed the signature, so using @wraps is not appropriate, but