summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichele Simionato <michele.simionato@gmail.com>2021-03-31 07:12:08 +0200
committerMichele Simionato <michele.simionato@gmail.com>2021-03-31 07:12:08 +0200
commit3bbdb6dd1fb04c8b8379c751e5e0e0afe04c1d3f (patch)
tree00e776f332d4b654b42b3be42b88e75220463702
parent95132174ed34089ec620e928597fb8f4d2dd0e92 (diff)
downloadpython-decorator-git-3bbdb6dd1fb04c8b8379c751e5e0e0afe04c1d3f.tar.gz
Not using the FunctionMaker in ContextManager
-rw-r--r--src/decorator.py22
-rw-r--r--src/tests/documentation.py13
2 files changed, 13 insertions, 22 deletions
diff --git a/src/decorator.py b/src/decorator.py
index 4aa312b..2293c6e 100644
--- a/src/decorator.py
+++ b/src/decorator.py
@@ -1,6 +1,6 @@
# ######################### LICENSE ############################ #
-# Copyright (c) 2005-2020, Michele Simionato
+# Copyright (c) 2005-2021, Michele Simionato
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
@@ -47,7 +47,7 @@ POS = inspect.Parameter.POSITIONAL_OR_KEYWORD
EMPTY = inspect.Parameter.empty
-# basic functionality
+# this is not used anymore in the core, but kept for backward compatibility
class FunctionMaker(object):
"""
An object with the ability to create functions with a given signature.
@@ -114,7 +114,9 @@ class FunctionMaker(object):
raise TypeError('You are decorating a non function: %s' % func)
def update(self, func, **kw):
- "Update the signature of func with the data in self"
+ """
+ Update the signature of func with the data in self
+ """
func.__name__ = self.name
func.__doc__ = getattr(self, 'doc', None)
func.__dict__ = getattr(self, 'dict', {})
@@ -131,7 +133,9 @@ class FunctionMaker(object):
func.__dict__.update(kw)
def make(self, src_templ, evaldict=None, addsource=False, **attrs):
- "Make a new function from a given template and update the signature"
+ """
+ Make a new function from a given template and update the signature
+ """
src = src_templ % vars(self) # expand name and signature
evaldict = evaldict or {}
mo = DEF.search(src)
@@ -219,7 +223,7 @@ def decorate(func, caller, extras=()):
def decorator(caller, _func=None):
- """decorator(caller) converts a caller function into a decorator"""
+ "decorator(caller) converts a caller function into a decorator"
if _func is not None: # return a decorated function
# this is obsolete behavior; you should use decorate instead
return decorate(_func, caller)
@@ -254,10 +258,10 @@ class ContextManager(_GeneratorContextManager):
return _GeneratorContextManager.__init__(self, g, a, k)
def __call__(self, func):
- """Context manager decorator"""
- return FunctionMaker.create(
- func, "with _self_: return _func_(%(shortsignature)s)",
- dict(_self_=self, _func_=func), __wrapped__=func)
+ def caller(f, *a, **k):
+ with self:
+ return f(*a, **k)
+ return decorate(func, caller)
_contextmanager = decorator(ContextManager)
diff --git a/src/tests/documentation.py b/src/tests/documentation.py
index b827c1a..ec04c6d 100644
--- a/src/tests/documentation.py
+++ b/src/tests/documentation.py
@@ -1282,19 +1282,6 @@ notice that lately I have come to believe that decorating functions with
keyword arguments is not such a good idea, and you may want not to do
that.
-On a similar note, there is a restriction on argument names. For instance,
-if you name an argument ``_call_`` or ``_func_``, you will get a ``NameError``:
-
-```python
->>> @trace
-... def f(_func_): print(f)
-...
-Traceback (most recent call last):
- ...
-NameError: _func_ is overridden in
-def f(_func_):
- return _call_(_func_, _func_)
-
Finally, the implementation is such that the decorated function makes
a (shallow) copy of the original function dictionary: