summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichele Simionato <michele.simionato@gmail.com>2021-03-31 05:57:58 +0200
committerMichele Simionato <michele.simionato@gmail.com>2021-03-31 05:57:58 +0200
commit2cad696ebb88180c02a7294985f8a81f95e90ff6 (patch)
tree9c83dd1cb181cf2ec5627bff09e796e6570d700d
parentf77bcbce45deb6757ef17d06033ef00a8b85c6df (diff)
downloadpython-decorator-git-2cad696ebb88180c02a7294985f8a81f95e90ff6.tar.gz
Removed support for old Python versions
-rw-r--r--src/decorator.py65
-rw-r--r--src/tests/documentation.py2
-rw-r--r--src/tests/test.py13
3 files changed, 15 insertions, 65 deletions
diff --git a/src/decorator.py b/src/decorator.py
index 45d61a4..a291557 100644
--- a/src/decorator.py
+++ b/src/decorator.py
@@ -32,47 +32,15 @@ Decorator module, see
https://github.com/micheles/decorator/blob/master/docs/documentation.md
for the documentation.
"""
-from __future__ import print_function
-
import re
import sys
import inspect
import operator
import itertools
-import collections
-
-__version__ = '4.4.2'
-
-if sys.version_info >= (3,):
- from inspect import getfullargspec
-
- def get_init(cls):
- return cls.__init__
-else:
- FullArgSpec = collections.namedtuple(
- 'FullArgSpec', 'args varargs varkw defaults '
- 'kwonlyargs kwonlydefaults annotations')
-
- def getfullargspec(f):
- "A quick and dirty replacement for getfullargspec for Python 2.X"
- return FullArgSpec._make(inspect.getargspec(f) + ([], None, {}))
-
- def get_init(cls):
- return cls.__init__.__func__
-
-try:
- iscoroutinefunction = inspect.iscoroutinefunction
-except AttributeError:
- # let's assume there are no coroutine functions in old Python
- def iscoroutinefunction(f):
- return False
-try:
- from inspect import isgeneratorfunction
-except ImportError:
- # assume no generator function in old Python versions
- def isgeneratorfunction(caller):
- return False
+from contextlib import _GeneratorContextManager
+from inspect import getfullargspec, iscoroutinefunction, isgeneratorfunction
+__version__ = '4.5.0'
DEF = re.compile(r'\s*def\s*([_\w][_\w\d]*)\s*\(')
POS = inspect.Parameter.POSITIONAL_OR_KEYWORD
@@ -243,8 +211,7 @@ def decorate(func, caller, extras=()):
fun.__name__ = func.__name__
fun.__signature__ = inspect.signature(func)
fun.__wrapped__ = func
- if hasattr(func, '__qualname__'): # >= Python 3.3
- fun.__qualname__ = func.__qualname__
+ fun.__qualname__ = func.__qualname__
fun.__annotations__ = func.__annotations__
fun.__dict__.update(func.__dict__)
return fun
@@ -272,21 +239,18 @@ def decorator(caller, _func=None):
dec.__name__ = caller.__name__
dec.__doc__ = caller.__doc__
dec.__wrapped__ = caller
- if hasattr(caller, '__qualname__'): # >= Python 3.3
- dec.__qualname__ = caller.__qualname__
+ dec.__qualname__ = caller.__qualname__
dec.__dict__.update(caller.__dict__)
return dec
# ####################### contextmanager ####################### #
-try: # Python >= 3.2
- from contextlib import _GeneratorContextManager
-except ImportError: # Python >= 2.5
- from contextlib import GeneratorContextManager as _GeneratorContextManager
-
class ContextManager(_GeneratorContextManager):
+ def __init__(self, g, *a, **k):
+ return _GeneratorContextManager.__init__(self, g, a, k)
+
def __call__(self, func):
"""Context manager decorator"""
return FunctionMaker.create(
@@ -294,19 +258,6 @@ class ContextManager(_GeneratorContextManager):
dict(_self_=self, _func_=func), __wrapped__=func)
-init = getfullargspec(_GeneratorContextManager.__init__)
-n_args = len(init.args)
-if n_args == 2 and not init.varargs: # (self, genobj) Python 2.7
- def __init__(self, g, *a, **k):
- return _GeneratorContextManager.__init__(self, g(*a, **k))
- ContextManager.__init__ = __init__
-elif n_args == 2 and init.varargs: # (self, gen, *a, **k) Python 3.4
- pass
-elif n_args == 4: # (self, gen, args, kwds) Python 3.5
- def __init__(self, g, *a, **k):
- return _GeneratorContextManager.__init__(self, g, a, k)
- ContextManager.__init__ = __init__
-
_contextmanager = decorator(ContextManager)
diff --git a/src/tests/documentation.py b/src/tests/documentation.py
index 28369ed..efb42f2 100644
--- a/src/tests/documentation.py
+++ b/src/tests/documentation.py
@@ -1298,7 +1298,7 @@ that.
On a similar note, for old versions of the decorator module (< 4.5)
there is a restriction on argument names. For instance,
-if you have an argument cakked ``_call_`` or ``_func_``, you will get a
+if you have an argument called ``_call_`` or ``_func_``, you will get a
``NameError`` when decorating the function. This restriction has been
lifted in version 4.5.
diff --git a/src/tests/test.py b/src/tests/test.py
index 83e54aa..a72b1d7 100644
--- a/src/tests/test.py
+++ b/src/tests/test.py
@@ -126,13 +126,12 @@ class ExtraTestCase(unittest.TestCase):
def f1(x, y, z):
pass
- if sys.version_info < (3, 5):
- self.assertNotEqual(d1.__code__.co_filename,
- d2.__code__.co_filename)
- self.assertNotEqual(f1.__code__.co_filename,
- f2.__code__.co_filename)
- self.assertNotEqual(f1_orig.__code__.co_filename,
- f1.__code__.co_filename)
+ self.assertEqual(d1.__code__.co_filename,
+ d2.__code__.co_filename)
+ self.assertEqual(f1.__code__.co_filename,
+ f2.__code__.co_filename)
+ self.assertEqual(f1_orig.__code__.co_filename,
+ f1.__code__.co_filename)
def test_no_first_arg(self):
@decorator