From ce4360fd8eeba0a263a5dd11699100ac17ced88e Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 11 Dec 2024 09:02:02 +0100 Subject: [PATCH 1/4] Drop Python 2 remaining logic --- docs/testing.rst | 7 ------ src/wrapt/decorators.py | 40 ++------------------------------ src/wrapt/importer.py | 12 ++-------- src/wrapt/patches.py | 9 +------ src/wrapt/wrappers.py | 21 +++++------------ tests/compat.py | 25 -------------------- tests/conftest.py | 2 -- tests/test_adapter.py | 7 ++---- tests/test_adapter_py3.py | 7 +----- tests/test_adapter_py33.py | 7 +----- tests/test_class.py | 4 +--- tests/test_class_py37.py | 2 -- tests/test_function.py | 4 ++-- tests/test_function_wrapper.py | 2 -- tests/test_object_proxy.py | 31 +++++++++---------------- tests/test_outer_classmethod.py | 4 ++-- tests/test_outer_staticmethod.py | 4 ++-- tests/test_post_import_hooks.py | 6 +---- tests/test_update_attributes.py | 33 +++++++------------------- 19 files changed, 42 insertions(+), 185 deletions(-) diff --git a/docs/testing.rst b/docs/testing.rst index 8a859d92..c15866e4 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -15,21 +15,18 @@ without the C extensions. :: - py27-without-extensions py35-without-extensions py36-without-extensions py37-without-extensions py38-without-extensions py39-without-extensions - py27-install-extensions py35-install-extensions py36-install-extensions py37-install-extensions py38-install-extensions py39-install-extensions - py27-disable-extensions py35-disable-extensions py36-disable-extensions py37-disable-extensions @@ -45,10 +42,6 @@ If wishing to run tests for a specific Python combination you can run tox -e py39-install-extensions -If adding more tests and you need to add a test which is Python 2 or -Python 3 specific, then end the name of the Python code file as -``_py2.py`` or ``_py3.py`` appropriately. - For further options refer to the documentation for ``tox``. Coverage diff --git a/src/wrapt/decorators.py b/src/wrapt/decorators.py index c80a4bb7..37b95206 100644 --- a/src/wrapt/decorators.py +++ b/src/wrapt/decorators.py @@ -3,33 +3,6 @@ """ -import sys - -PY2 = sys.version_info[0] == 2 - -if PY2: - string_types = basestring, - - def exec_(_code_, _globs_=None, _locs_=None): - """Execute code in a namespace.""" - if _globs_ is None: - frame = sys._getframe(1) - _globs_ = frame.f_globals - if _locs_ is None: - _locs_ = frame.f_locals - del frame - elif _locs_ is None: - _locs_ = _globs_ - exec("""exec _code_ in _globs_, _locs_""") - -else: - string_types = str, - - import builtins - - exec_ = getattr(builtins, "exec") - del builtins - from functools import partial from inspect import isclass from threading import Lock, RLock @@ -102,9 +75,6 @@ def __signature__(self): else: return signature(self._self_adapter) - if PY2: - func_code = __code__ - func_defaults = __defaults__ class _BoundAdapterWrapper(BoundFunctionWrapper): @@ -120,8 +90,6 @@ def __signature__(self): else: return signature(self._self_parent._self_adapter) - if PY2: - im_func = __func__ class AdapterWrapper(FunctionWrapper): @@ -146,10 +114,6 @@ def __defaults__(self): def __kwdefaults__(self): return self._self_surrogate.__kwdefaults__ - if PY2: - func_code = __code__ - func_defaults = __defaults__ - @property def __signature__(self): return self._self_surrogate.__signature__ @@ -220,13 +184,13 @@ def _build(wrapped, wrapper, enabled=None, adapter=None): annotations = {} - if not isinstance(adapter, string_types): + if not isinstance(adapter, str): if len(adapter) == 7: annotations = adapter[-1] adapter = adapter[:-1] adapter = formatargspec(*adapter) - exec_('def adapter{}: pass'.format(adapter), ns, ns) + exec('def adapter{}: pass'.format(adapter), ns, ns) adapter = ns['adapter'] # Override the annotations for the manufactured diff --git a/src/wrapt/importer.py b/src/wrapt/importer.py index 23fcbd2f..3eece68e 100644 --- a/src/wrapt/importer.py +++ b/src/wrapt/importer.py @@ -5,15 +5,7 @@ import sys import threading - -PY2 = sys.version_info[0] == 2 - -if PY2: - string_types = basestring, - find_spec = None -else: - string_types = str, - from importlib.util import find_spec +from importlib.util import find_spec from .__wrapt__ import ObjectProxy @@ -49,7 +41,7 @@ def register_post_import_hook(hook, name): # Create a deferred import hook if hook is a string name rather than # a callable function. - if isinstance(hook, string_types): + if isinstance(hook, str): hook = _create_import_hook_from_string(hook) with _post_import_hooks_lock: diff --git a/src/wrapt/patches.py b/src/wrapt/patches.py index e22adf7c..863c368e 100644 --- a/src/wrapt/patches.py +++ b/src/wrapt/patches.py @@ -1,19 +1,12 @@ import inspect import sys -PY2 = sys.version_info[0] == 2 - -if PY2: - string_types = basestring, -else: - string_types = str, - from .__wrapt__ import FunctionWrapper # Helper functions for applying wrappers to existing functions. def resolve_path(module, name): - if isinstance(module, string_types): + if isinstance(module, str): __import__(module) module = sys.modules[module] diff --git a/src/wrapt/wrappers.py b/src/wrapt/wrappers.py index 62da8a30..2ffe784c 100644 --- a/src/wrapt/wrappers.py +++ b/src/wrapt/wrappers.py @@ -2,13 +2,6 @@ import operator import inspect -PY2 = sys.version_info[0] == 2 - -if PY2: - string_types = basestring, -else: - string_types = str, - def with_metaclass(meta, *bases): """Create a base class with a metaclass.""" return meta("NewBase", bases, {}) @@ -116,9 +109,8 @@ def __dir__(self): def __str__(self): return str(self.__wrapped__) - if not PY2: - def __bytes__(self): - return bytes(self.__wrapped__) + def __bytes__(self): + return bytes(self.__wrapped__) def __repr__(self): return '<{} at 0x{:x} for {} at 0x{:x}>'.format( @@ -132,9 +124,8 @@ def __format__(self, format_spec): def __reversed__(self): return reversed(self.__wrapped__) - if not PY2: - def __round__(self, ndigits=None): - return round(self.__wrapped__, ndigits) + def __round__(self, ndigits=None): + return round(self.__wrapped__, ndigits) if sys.hexversion >= 0x03070000: def __mro_entries__(self, bases): @@ -482,7 +473,7 @@ def _unpack_self(self, *args): return self, args self, args = _unpack_self(*args) - + _args = self._self_args + args _kwargs = dict(self._self_kwargs) @@ -536,7 +527,7 @@ def __get__(self, instance, owner): if self._self_binding == 'builtin': return self - + if self._self_binding == "class": return self diff --git a/tests/compat.py b/tests/compat.py index 83b61f94..7d80c152 100644 --- a/tests/compat.py +++ b/tests/compat.py @@ -1,32 +1,7 @@ import sys -PY2 = sys.version_info[0] < 3 -PY3 = sys.version_info[0] >= 3 - PYXY = tuple(sys.version_info[:2]) -if PY3: - import builtins - exec_ = getattr(builtins, "exec") - del builtins - -else: - def exec_(_code_, _globs_=None, _locs_=None): - """Execute code in a namespace.""" - if _globs_ is None: - frame = sys._getframe(1) - _globs_ = frame.f_globals - if _locs_ is None: - _locs_ = frame.f_locals - del frame - elif _locs_ is None: - _locs_ = _globs_ - exec("""exec _code_ in _globs_, _locs_""") - - exec_("""def reraise(tp, value, tb=None): - raise tp, value, tb -""") - try: from inspect import getfullargspec except ImportError: diff --git a/tests/conftest.py b/tests/conftest.py index 318d2263..d2749bd4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -41,5 +41,3 @@ def pytest_pycollect_makemodule(path, parent): return construct_dummy(path, parent) if '_py3' in path.basename and version < (3, 0): return construct_dummy(path, parent) - if '_py2' in path.basename and version >= (3, 0): - return construct_dummy(path, parent) diff --git a/tests/test_adapter.py b/tests/test_adapter.py index 85063c28..b96b2ce7 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -6,7 +6,7 @@ import wrapt -from compat import PY2, exec_, getfullargspec +from compat import getfullargspec DECORATORS_CODE = """ import wrapt @@ -19,7 +19,7 @@ def adapter1(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) def function1(arg1, arg2): '''documentation''' @@ -88,9 +88,6 @@ def test_signature(self): # actually needs to match that of the adapter function the # prototype of which was supplied via the dummy function. - if PY2: - return - def _adapter(arg1, arg2, arg3=None, *args, **kwargs): pass function1a_signature = str(inspect.signature(_adapter)) diff --git a/tests/test_adapter_py3.py b/tests/test_adapter_py3.py index b6d48f17..6b4e5091 100644 --- a/tests/test_adapter_py3.py +++ b/tests/test_adapter_py3.py @@ -8,8 +8,6 @@ import wrapt -from compat import PY2, exec_ - DECORATORS_CODE = """ import wrapt from typing import Iterable @@ -28,7 +26,7 @@ def adapter2(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) def function1(arg1, arg2) -> Iterable: '''documentation''' @@ -88,9 +86,6 @@ def test_signature(self): # actually needs to match that of the adapter function the # prototype of which was supplied via the dummy function. - if PY2: - return - def _adapter(arg1, arg2, arg3=None, *args, **kwargs) -> Iterable: pass function1a_signature = str(inspect.signature(_adapter)) diff --git a/tests/test_adapter_py33.py b/tests/test_adapter_py33.py index 0cc8b2eb..61fd4acf 100644 --- a/tests/test_adapter_py33.py +++ b/tests/test_adapter_py33.py @@ -6,8 +6,6 @@ import wrapt -from compat import PY2, PY3, exec_ - DECORATORS_CODE = """ import wrapt @@ -19,7 +17,7 @@ def adapter1(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) def function1(arg1, arg2): '''documentation''' @@ -52,9 +50,6 @@ def test_signature(self): # actually needs to match that of the adapter function the # prototype of which was supplied via the dummy function. - if PY2: - return - def _adapter(arg1, arg2, *, arg3=None, **kwargs): pass function1a_signature = str(inspect.signature(_adapter)) diff --git a/tests/test_class.py b/tests/test_class.py index d0f83845..d8d943b1 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -6,8 +6,6 @@ import wrapt -from compat import PY2, PY3, exec_ - DECORATORS_CODE = """ import wrapt @@ -17,7 +15,7 @@ def passthru_decorator(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) class class1(object): pass diff --git a/tests/test_class_py37.py b/tests/test_class_py37.py index cdab1b6d..0ccbdb16 100644 --- a/tests/test_class_py37.py +++ b/tests/test_class_py37.py @@ -6,8 +6,6 @@ import wrapt -from compat import PY2, PY3, exec_ - class TestInheritance(unittest.TestCase): def test_single_inheritance(self): diff --git a/tests/test_function.py b/tests/test_function.py index cb77b35c..7da01b95 100644 --- a/tests/test_function.py +++ b/tests/test_function.py @@ -6,7 +6,7 @@ import wrapt -from compat import exec_, getfullargspec +from compat import getfullargspec DECORATORS_CODE = """ import wrapt @@ -17,7 +17,7 @@ def passthru_decorator(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) def function1(arg): '''documentation''' diff --git a/tests/test_function_wrapper.py b/tests/test_function_wrapper.py index ac7df47f..2a71d601 100644 --- a/tests/test_function_wrapper.py +++ b/tests/test_function_wrapper.py @@ -4,8 +4,6 @@ import wrapt -from compat import PY2, PY3, exec_ - class TestClassInheritence(unittest.TestCase): def test_function_type_inheritence(self): diff --git a/tests/test_object_proxy.py b/tests/test_object_proxy.py index a8a079be..f6d122e7 100644 --- a/tests/test_object_proxy.py +++ b/tests/test_object_proxy.py @@ -9,8 +9,6 @@ import wrapt -from compat import PY2, PY3, exec_ - OBJECTS_CODE = """ class TargetBaseClass(object): "documentation" @@ -24,7 +22,7 @@ def target(): """ objects = types.ModuleType('objects') -exec_(OBJECTS_CODE, objects.__dict__, objects.__dict__) +exec(OBJECTS_CODE, objects.__dict__, objects.__dict__) class TestAttributeAccess(unittest.TestCase): @@ -69,8 +67,7 @@ def function1(*args, **kwargs): self.assertEqual(function2.__wrapped__, function1) self.assertEqual(function2.__name__, function1.__name__) - if PY3: - self.assertEqual(function2.__qualname__, function1.__qualname__) + self.assertEqual(function2.__qualname__, function1.__qualname__) function2.__wrapped__ = None @@ -80,8 +77,7 @@ def function1(*args, **kwargs): self.assertEqual(function2.__wrapped__, None) self.assertFalse(hasattr(function2, '__name__')) - if PY3: - self.assertFalse(hasattr(function2, '__qualname__')) + self.assertFalse(hasattr(function2, '__qualname__')) def function3(*args, **kwargs): return args, kwargs @@ -92,8 +88,7 @@ def function3(*args, **kwargs): self.assertEqual(function2.__wrapped__, function3) self.assertEqual(function2.__name__, function3.__name__) - if PY3: - self.assertEqual(function2.__qualname__, function3.__qualname__) + self.assertEqual(function2.__qualname__, function3.__qualname__) def test_delete_wrapped(self): def function1(*args, **kwargs): @@ -876,9 +871,6 @@ def test_int(self): self.assertEqual(int(one), 1) - if not PY3: - self.assertEqual(long(one), 1) - def test_float(self): one = wrapt.ObjectProxy(1) @@ -1804,15 +1796,14 @@ def test_callable_proxy_is_callable(self): class SpecialMethods(unittest.TestCase): def test_class_bytes(self): - if PY3: - class Class(object): - def __bytes__(self): - return b'BYTES' - instance = Class() + class Class(object): + def __bytes__(self): + return b'BYTES' + instance = Class() - proxy = wrapt.ObjectProxy(instance) + proxy = wrapt.ObjectProxy(instance) - self.assertEqual(bytes(instance), bytes(proxy)) + self.assertEqual(bytes(instance), bytes(proxy)) def test_str_format(self): instance = 'abcd' @@ -1854,7 +1845,7 @@ def test_fractions_round(self): self.assertEqual(round(instance), round(proxy)) self.assertEqual(round(instance, 3), round(proxy, 3)) self.assertEqual(round(instance, ndigits=3), round(proxy, ndigits=3)) - + class TestArgumentUnpacking(unittest.TestCase): def test_self_keyword_argument_on_dict(self): diff --git a/tests/test_outer_classmethod.py b/tests/test_outer_classmethod.py index c08d34a5..c65f7645 100644 --- a/tests/test_outer_classmethod.py +++ b/tests/test_outer_classmethod.py @@ -5,7 +5,7 @@ import wrapt -from compat import PYXY, exec_, getfullargspec +from compat import PYXY, getfullargspec DECORATORS_CODE = """ import wrapt @@ -16,7 +16,7 @@ def passthru_decorator(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) class Class(object): @classmethod diff --git a/tests/test_outer_staticmethod.py b/tests/test_outer_staticmethod.py index 959c7ffd..a1609a4b 100644 --- a/tests/test_outer_staticmethod.py +++ b/tests/test_outer_staticmethod.py @@ -5,7 +5,7 @@ import wrapt -from compat import exec_, getfullargspec +from compat import getfullargspec DECORATORS_CODE = """ import wrapt @@ -16,7 +16,7 @@ def passthru_decorator(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) class Class(object): @staticmethod diff --git a/tests/test_post_import_hooks.py b/tests/test_post_import_hooks.py index 5113e8d2..53e651a4 100644 --- a/tests/test_post_import_hooks.py +++ b/tests/test_post_import_hooks.py @@ -7,7 +7,6 @@ import wrapt from wrapt.importer import _post_import_hooks -from compat import PY2, PY3 class TestPostImportHooks(unittest.TestCase): @@ -148,9 +147,6 @@ def test_import_deadlock_3(self): # there is a module import lock per named module and so we do not have # this problem. - if PY2: - return - hooks_called = [] @wrapt.when_imported('this') @@ -158,7 +154,7 @@ def hook_this(module): hooks_called.append('this') self.assertFalse('wsgiref' in sys.modules) - + @wrapt.when_imported('wsgiref') def hook_wsgiref(module): hooks_called.append('wsgiref') diff --git a/tests/test_update_attributes.py b/tests/test_update_attributes.py index 9850c1b1..ec0f5ae1 100644 --- a/tests/test_update_attributes.py +++ b/tests/test_update_attributes.py @@ -4,7 +4,6 @@ import wrapt -from compat import PY2, PY3, exec_ @wrapt.decorator def passthru_decorator(wrapped, instance, args, kwargs): @@ -45,10 +44,9 @@ def test_update_qualname(self): def function(): pass - if PY3: - method = self.test_update_qualname - self.assertEqual(function.__qualname__, - (method.__qualname__ + '..function')) + method = self.test_update_qualname + self.assertEqual(function.__qualname__, + (method.__qualname__ + '..function')) function.__qualname__ = 'override_qualname' @@ -63,10 +61,9 @@ def wrapper(wrapped, instance, args, kwargs): instance = wrapt.FunctionWrapper(function, wrapper) - if PY3: - method = self.test_update_qualname_modified_on_original - self.assertEqual(instance.__qualname__, - (method.__qualname__ + '..function')) + method = self.test_update_qualname_modified_on_original + self.assertEqual(instance.__qualname__, + (method.__qualname__ + '..function')) instance.__qualname__ = 'override_qualname' @@ -134,14 +131,7 @@ def test_update_annotations(self): def function(): pass - if PY3: - self.assertEqual(function.__annotations__, {}) - - else: - def run(*args): - function.__annotations__ - - self.assertRaises(AttributeError, run, ()) + self.assertEqual(function.__annotations__, {}) override_annotations = {'override_annotations': ''} function.__annotations__ = override_annotations @@ -158,14 +148,7 @@ def wrapper(wrapped, instance, args, kwargs): instance = wrapt.FunctionWrapper(function, wrapper) - if PY3: - self.assertEqual(instance.__annotations__, {}) - - else: - def run(*args): - instance.__annotations__ - - self.assertRaises(AttributeError, run, ()) + self.assertEqual(instance.__annotations__, {}) override_annotations = {'override_annotations': ''} instance.__annotations__ = override_annotations From a8a3b99ddec061bce4e69a020564ab2197a7dc1b Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 11 Dec 2024 09:03:53 +0100 Subject: [PATCH 2/4] Remove comment on docs --- docs/testing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/testing.rst b/docs/testing.rst index c15866e4..71783e7a 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -10,7 +10,7 @@ run the command: tox -By default tests are run for Python 2.7, 3.5-3.9 and PyPy, with and +By default tests are run for Python 3.5-3.9 and PyPy, with and without the C extensions. :: From 787f643ca3650bae83401166d4ee2b3e0c7fefea Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 11 Dec 2024 09:09:43 +0100 Subject: [PATCH 3/4] Fix tests --- tests/test_inner_classmethod.py | 4 ++-- tests/test_inner_staticmethod.py | 4 ++-- tests/test_instancemethod.py | 4 ++-- tests/test_nested_function.py | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/test_inner_classmethod.py b/tests/test_inner_classmethod.py index e2127ab9..5a642f88 100644 --- a/tests/test_inner_classmethod.py +++ b/tests/test_inner_classmethod.py @@ -5,7 +5,7 @@ import wrapt -from compat import exec_, getfullargspec +from compat import getfullargspec DECORATORS_CODE = """ import wrapt @@ -16,7 +16,7 @@ def passthru_decorator(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) class Class(object): @classmethod diff --git a/tests/test_inner_staticmethod.py b/tests/test_inner_staticmethod.py index bd3756f4..706d9520 100644 --- a/tests/test_inner_staticmethod.py +++ b/tests/test_inner_staticmethod.py @@ -5,7 +5,7 @@ import wrapt -from compat import exec_, getfullargspec +from compat import getfullargspec DECORATORS_CODE = """ import wrapt @@ -16,7 +16,7 @@ def passthru_decorator(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) class Class(object): @staticmethod diff --git a/tests/test_instancemethod.py b/tests/test_instancemethod.py index 90e13ad7..83a60a3e 100644 --- a/tests/test_instancemethod.py +++ b/tests/test_instancemethod.py @@ -6,7 +6,7 @@ import wrapt -from compat import exec_, getfullargspec +from compat import getfullargspec DECORATORS_CODE = """ import wrapt @@ -17,7 +17,7 @@ def passthru_decorator(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) class OldClass1(): def function(self, arg): diff --git a/tests/test_nested_function.py b/tests/test_nested_function.py index ca65263b..667e14bc 100644 --- a/tests/test_nested_function.py +++ b/tests/test_nested_function.py @@ -5,7 +5,7 @@ import wrapt -from compat import exec_, getfullargspec +from compat import getfullargspec DECORATORS_CODE = """ import wrapt @@ -16,7 +16,7 @@ def passthru_decorator(wrapped, instance, args, kwargs): """ decorators = types.ModuleType('decorators') -exec_(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) +exec(DECORATORS_CODE, decorators.__dict__, decorators.__dict__) def function1(): def inner(arg): From 99da42bdf708279bf0189f9bd9b03ce8b0bb2f93 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Wed, 11 Dec 2024 09:20:53 +0100 Subject: [PATCH 4/4] Remove Python 2 conditionals from C --- src/wrapt/_wrappers.c | 293 +----------------------------------------- 1 file changed, 2 insertions(+), 291 deletions(-) diff --git a/src/wrapt/_wrappers.c b/src/wrapt/_wrappers.c index 7f85855d..43493c23 100644 --- a/src/wrapt/_wrappers.c +++ b/src/wrapt/_wrappers.c @@ -79,19 +79,11 @@ static int WraptObjectProxy_raw_init(WraptObjectProxyObject *self, self->wrapped = wrapped; if (!module_str) { -#if PY_MAJOR_VERSION >= 3 module_str = PyUnicode_InternFromString("__module__"); -#else - module_str = PyString_InternFromString("__module__"); -#endif } if (!doc_str) { -#if PY_MAJOR_VERSION >= 3 doc_str = PyUnicode_InternFromString("__doc__"); -#else - doc_str = PyString_InternFromString("__doc__"); -#endif } object = PyObject_GetAttr(wrapped, module_str); @@ -182,23 +174,13 @@ static PyObject *WraptObjectProxy_repr(WraptObjectProxyObject *self) return NULL; } -#if PY_MAJOR_VERSION >= 3 return PyUnicode_FromFormat("<%s at %p for %s at %p>", Py_TYPE(self)->tp_name, self, Py_TYPE(self->wrapped)->tp_name, self->wrapped); -#else - return PyString_FromFormat("<%s at %p for %s at %p>", - Py_TYPE(self)->tp_name, self, - Py_TYPE(self->wrapped)->tp_name, self->wrapped); -#endif } /* ------------------------------------------------------------------------- */ -#if PY_MAJOR_VERSION < 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 3) -typedef long Py_hash_t; -#endif - static Py_hash_t WraptObjectProxy_hash(WraptObjectProxyObject *self) { if (!self->wrapped) { @@ -299,33 +281,6 @@ static PyObject *WraptObjectProxy_multiply(PyObject *o1, PyObject *o2) /* ------------------------------------------------------------------------- */ -#if PY_MAJOR_VERSION < 3 -static PyObject *WraptObjectProxy_divide(PyObject *o1, PyObject *o2) -{ - if (PyObject_IsInstance(o1, (PyObject *)&WraptObjectProxy_Type)) { - if (!((WraptObjectProxyObject *)o1)->wrapped) { - PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized"); - return NULL; - } - - o1 = ((WraptObjectProxyObject *)o1)->wrapped; - } - - if (PyObject_IsInstance(o2, (PyObject *)&WraptObjectProxy_Type)) { - if (!((WraptObjectProxyObject *)o2)->wrapped) { - PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized"); - return NULL; - } - - o2 = ((WraptObjectProxyObject *)o2)->wrapped; - } - - return PyNumber_Divide(o1, o2); -} -#endif - -/* ------------------------------------------------------------------------- */ - static PyObject *WraptObjectProxy_remainder(PyObject *o1, PyObject *o2) { if (PyObject_IsInstance(o1, (PyObject *)&WraptObjectProxy_Type)) { @@ -585,19 +540,6 @@ static PyObject *WraptObjectProxy_or(PyObject *o1, PyObject *o2) return PyNumber_Or(o1, o2); } -/* ------------------------------------------------------------------------- */ - -#if PY_MAJOR_VERSION < 3 -static PyObject *WraptObjectProxy_int(WraptObjectProxyObject *self) -{ - if (!self->wrapped) { - PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized"); - return NULL; - } - - return PyNumber_Int(self->wrapped); -} -#endif /* ------------------------------------------------------------------------- */ @@ -625,52 +567,6 @@ static PyObject *WraptObjectProxy_float(WraptObjectProxyObject *self) /* ------------------------------------------------------------------------- */ -#if PY_MAJOR_VERSION < 3 -static PyObject *WraptObjectProxy_oct(WraptObjectProxyObject *self) -{ - PyNumberMethods *nb; - - if (!self->wrapped) { - PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized"); - return NULL; - } - - if ((nb = self->wrapped->ob_type->tp_as_number) == NULL || - nb->nb_oct == NULL) { - PyErr_SetString(PyExc_TypeError, - "oct() argument can't be converted to oct"); - return NULL; - } - - return (*nb->nb_oct)(self->wrapped); -} -#endif - -/* ------------------------------------------------------------------------- */ - -#if PY_MAJOR_VERSION < 3 -static PyObject *WraptObjectProxy_hex(WraptObjectProxyObject *self) -{ - PyNumberMethods *nb; - - if (!self->wrapped) { - PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized"); - return NULL; - } - - if ((nb = self->wrapped->ob_type->tp_as_number) == NULL || - nb->nb_hex == NULL) { - PyErr_SetString(PyExc_TypeError, - "hex() argument can't be converted to hex"); - return NULL; - } - - return (*nb->nb_hex)(self->wrapped); -} -#endif - -/* ------------------------------------------------------------------------- */ - static PyObject *WraptObjectProxy_inplace_add(WraptObjectProxyObject *self, PyObject *other) { @@ -752,35 +648,6 @@ static PyObject *WraptObjectProxy_inplace_multiply( /* ------------------------------------------------------------------------- */ -#if PY_MAJOR_VERSION < 3 -static PyObject *WraptObjectProxy_inplace_divide( - WraptObjectProxyObject *self, PyObject *other) -{ - PyObject *object = NULL; - - if (!self->wrapped) { - PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized"); - return NULL; - } - - if (PyObject_IsInstance(other, (PyObject *)&WraptObjectProxy_Type)) - other = ((WraptObjectProxyObject *)other)->wrapped; - - object = PyNumber_InPlaceDivide(self->wrapped, other); - - if (!object) - return NULL; - - Py_DECREF(self->wrapped); - self->wrapped = object; - - Py_INCREF(self); - return (PyObject *)self; -} -#endif - -/* ------------------------------------------------------------------------- */ - static PyObject *WraptObjectProxy_inplace_remainder( WraptObjectProxyObject *self, PyObject *other) { @@ -1146,13 +1013,8 @@ static PyObject *WraptObjectProxy_self_setattr( PyObject *name = NULL; PyObject *value = NULL; -#if PY_MAJOR_VERSION >= 3 if (!PyArg_ParseTuple(args, "UO:__self_setattr__", &name, &value)) return NULL; -#else - if (!PyArg_ParseTuple(args, "SO:__self_setattr__", &name, &value)) - return NULL; -#endif if (PyObject_GenericSetAttr((PyObject *)self, name, value) != 0) { return NULL; @@ -1316,7 +1178,6 @@ static PyObject *WraptObjectProxy_reversed( /* ------------------------------------------------------------------------- */ -#if PY_MAJOR_VERSION >= 3 static PyObject *WraptObjectProxy_round( WraptObjectProxyObject *self, PyObject *args, PyObject *kwds) { @@ -1361,7 +1222,6 @@ static PyObject *WraptObjectProxy_round( return result; } -#endif /* ------------------------------------------------------------------------- */ @@ -1605,11 +1465,7 @@ static PyObject *WraptObjectProxy_getattro( PyErr_Clear(); if (!getattr_str) { -#if PY_MAJOR_VERSION >= 3 getattr_str = PyUnicode_InternFromString("__getattr__"); -#else - getattr_str = PyString_InternFromString("__getattr__"); -#endif } object = PyObject_GenericGetAttr((PyObject *)self, getattr_str); @@ -1631,13 +1487,8 @@ static PyObject *WraptObjectProxy_getattr( { PyObject *name = NULL; -#if PY_MAJOR_VERSION >= 3 if (!PyArg_ParseTuple(args, "U:__getattr__", &name)) return NULL; -#else - if (!PyArg_ParseTuple(args, "S:__getattr__", &name)) - return NULL; -#endif if (!self->wrapped) { PyErr_SetString(PyExc_ValueError, "wrapper has not been initialized"); @@ -1659,19 +1510,11 @@ static int WraptObjectProxy_setattro( PyObject *match = NULL; if (!startswith_str) { -#if PY_MAJOR_VERSION >= 3 startswith_str = PyUnicode_InternFromString("startswith"); -#else - startswith_str = PyString_InternFromString("startswith"); -#endif } if (!self_str) { -#if PY_MAJOR_VERSION >= 3 self_str = PyUnicode_InternFromString("_self_"); -#else - self_str = PyString_InternFromString("_self_"); -#endif } match = PyObject_CallMethodObjArgs(name, startswith_str, self_str, NULL); @@ -1687,11 +1530,7 @@ static int WraptObjectProxy_setattro( Py_XDECREF(match); if (!wrapped_str) { -#if PY_MAJOR_VERSION >= 3 wrapped_str = PyUnicode_InternFromString("__wrapped__"); -#else - wrapped_str = PyString_InternFromString("__wrapped__"); -#endif } if (PyObject_HasAttr((PyObject *)Py_TYPE(self), name)) @@ -1736,9 +1575,6 @@ static PyNumberMethods WraptObjectProxy_as_number = { (binaryfunc)WraptObjectProxy_add, /*nb_add*/ (binaryfunc)WraptObjectProxy_subtract, /*nb_subtract*/ (binaryfunc)WraptObjectProxy_multiply, /*nb_multiply*/ -#if PY_MAJOR_VERSION < 3 - (binaryfunc)WraptObjectProxy_divide, /*nb_divide*/ -#endif (binaryfunc)WraptObjectProxy_remainder, /*nb_remainder*/ (binaryfunc)WraptObjectProxy_divmod, /*nb_divmod*/ (ternaryfunc)WraptObjectProxy_power, /*nb_power*/ @@ -1752,27 +1588,12 @@ static PyNumberMethods WraptObjectProxy_as_number = { (binaryfunc)WraptObjectProxy_and, /*nb_and*/ (binaryfunc)WraptObjectProxy_xor, /*nb_xor*/ (binaryfunc)WraptObjectProxy_or, /*nb_or*/ -#if PY_MAJOR_VERSION < 3 - 0, /*nb_coerce*/ -#endif -#if PY_MAJOR_VERSION < 3 - (unaryfunc)WraptObjectProxy_int, /*nb_int*/ - (unaryfunc)WraptObjectProxy_long, /*nb_long*/ -#else (unaryfunc)WraptObjectProxy_long, /*nb_int*/ 0, /*nb_long/nb_reserved*/ -#endif (unaryfunc)WraptObjectProxy_float, /*nb_float*/ -#if PY_MAJOR_VERSION < 3 - (unaryfunc)WraptObjectProxy_oct, /*nb_oct*/ - (unaryfunc)WraptObjectProxy_hex, /*nb_hex*/ -#endif (binaryfunc)WraptObjectProxy_inplace_add, /*nb_inplace_add*/ (binaryfunc)WraptObjectProxy_inplace_subtract, /*nb_inplace_subtract*/ (binaryfunc)WraptObjectProxy_inplace_multiply, /*nb_inplace_multiply*/ -#if PY_MAJOR_VERSION < 3 - (binaryfunc)WraptObjectProxy_inplace_divide, /*nb_inplace_divide*/ -#endif (binaryfunc)WraptObjectProxy_inplace_remainder, /*nb_inplace_remainder*/ (ternaryfunc)WraptObjectProxy_inplace_power, /*nb_inplace_power*/ (binaryfunc)WraptObjectProxy_inplace_lshift, /*nb_inplace_lshift*/ @@ -1825,12 +1646,10 @@ static PyMethodDef WraptObjectProxy_methods[] = { { "__bytes__", (PyCFunction)WraptObjectProxy_bytes, METH_NOARGS, 0 }, { "__format__", (PyCFunction)WraptObjectProxy_format, METH_VARARGS, 0 }, { "__reversed__", (PyCFunction)WraptObjectProxy_reversed, METH_NOARGS, 0 }, -#if PY_MAJOR_VERSION >= 3 { "__round__", (PyCFunction)WraptObjectProxy_round, METH_VARARGS | METH_KEYWORDS, 0 }, -#endif { "__complex__", (PyCFunction)WraptObjectProxy_complex, METH_NOARGS, 0 }, -#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 7) +#if PY_MINOR_VERSION >= 7 { "__mro_entries__", (PyCFunction)WraptObjectProxy_mro_entries, METH_VARARGS | METH_KEYWORDS, 0 }, #endif @@ -1876,13 +1695,8 @@ PyTypeObject WraptObjectProxy_Type = { (getattrofunc)WraptObjectProxy_getattro, /*tp_getattro*/ (setattrofunc)WraptObjectProxy_setattro, /*tp_setattro*/ 0, /*tp_as_buffer*/ -#if PY_MAJOR_VERSION < 3 - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ -#else Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ -#endif 0, /*tp_doc*/ (traverseproc)WraptObjectProxy_traverse, /*tp_traverse*/ (inquiry)WraptObjectProxy_clear, /*tp_clear*/ @@ -1949,11 +1763,7 @@ PyTypeObject WraptCallableObjectProxy_Type = { 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ -#if PY_MAJOR_VERSION < 3 - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ -#else Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ -#endif 0, /*tp_doc*/ 0, /*tp_traverse*/ 0, /*tp_clear*/ @@ -2193,13 +2003,8 @@ PyTypeObject WraptPartialCallableObjectProxy_Type = { 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ -#if PY_MAJOR_VERSION < 3 - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ -#else Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ -#endif 0, /*tp_doc*/ (traverseproc)WraptPartialCallableObjectProxy_traverse, /*tp_traverse*/ (inquiry)WraptPartialCallableObjectProxy_clear, /*tp_clear*/ @@ -2305,11 +2110,7 @@ static int WraptFunctionWrapperBase_init(WraptFunctionWrapperObject *self, "enabled", "binding", "parent", "owner", NULL }; if (!callable_str) { -#if PY_MAJOR_VERSION >= 3 callable_str = PyUnicode_InternFromString("callable"); -#else - callable_str = PyString_InternFromString("callable"); -#endif } if (!PyArg_ParseTupleAndKeywords(args, kwds, @@ -2384,17 +2185,10 @@ static PyObject *WraptFunctionWrapperBase_call( static PyObject *instancemethod_str = NULL; if (!function_str) { -#if PY_MAJOR_VERSION >= 3 function_str = PyUnicode_InternFromString("function"); callable_str = PyUnicode_InternFromString("callable"); classmethod_str = PyUnicode_InternFromString("classmethod"); instancemethod_str = PyUnicode_InternFromString("instancemethod"); -#else - function_str = PyString_InternFromString("function"); - callable_str = PyString_InternFromString("callable"); - classmethod_str = PyString_InternFromString("classmethod"); - instancemethod_str = PyString_InternFromString("instancemethod"); -#endif } if (self->enabled != Py_None) { @@ -2477,29 +2271,16 @@ static PyObject *WraptFunctionWrapperBase_descr_get( static PyObject *instancemethod_str = NULL; if (!bound_type_str) { -#if PY_MAJOR_VERSION >= 3 bound_type_str = PyUnicode_InternFromString( "__bound_function_wrapper__"); -#else - bound_type_str = PyString_InternFromString( - "__bound_function_wrapper__"); -#endif } if (!function_str) { -#if PY_MAJOR_VERSION >= 3 function_str = PyUnicode_InternFromString("function"); callable_str = PyUnicode_InternFromString("callable"); builtin_str = PyUnicode_InternFromString("builtin"); class_str = PyUnicode_InternFromString("class"); instancemethod_str = PyUnicode_InternFromString("instancemethod"); -#else - function_str = PyString_InternFromString("function"); - callable_str = PyString_InternFromString("callable"); - builtin_str = PyString_InternFromString("builtin"); - class_str = PyString_InternFromString("class"); - instancemethod_str = PyString_InternFromString("instancemethod"); -#endif } if (self->parent == Py_None) { @@ -2561,11 +2342,7 @@ static PyObject *WraptFunctionWrapperBase_descr_get( static PyObject *wrapped_str = NULL; if (!wrapped_str) { -#if PY_MAJOR_VERSION >= 3 wrapped_str = PyUnicode_InternFromString("__wrapped__"); -#else - wrapped_str = PyString_InternFromString("__wrapped__"); -#endif } wrapped = PyObject_GetAttr(self->parent, wrapped_str); @@ -2847,13 +2624,8 @@ PyTypeObject WraptFunctionWrapperBase_Type = { 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ -#if PY_MAJOR_VERSION < 3 - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ -#else Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ -#endif 0, /*tp_doc*/ (traverseproc)WraptFunctionWrapperBase_traverse, /*tp_traverse*/ (inquiry)WraptFunctionWrapperBase_clear, /*tp_clear*/ @@ -2914,16 +2686,11 @@ static PyObject *WraptBoundFunctionWrapper_call( } if (!function_str) { -#if PY_MAJOR_VERSION >= 3 function_str = PyUnicode_InternFromString("function"); callable_str = PyUnicode_InternFromString("callable"); -#else - function_str = PyString_InternFromString("function"); - callable_str = PyString_InternFromString("callable"); -#endif } - /* + /* * We need to do things different depending on whether we are likely * wrapping an instance method vs a static method or class method. */ @@ -3102,11 +2869,7 @@ PyTypeObject WraptBoundFunctionWrapper_Type = { 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ -#if PY_MAJOR_VERSION < 3 - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ -#else Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ -#endif 0, /*tp_doc*/ 0, /*tp_traverse*/ 0, /*tp_clear*/ @@ -3158,59 +2921,31 @@ static int WraptFunctionWrapper_init(WraptFunctionWrapperObject *self, } if (!function_str) { -#if PY_MAJOR_VERSION >= 3 function_str = PyUnicode_InternFromString("function"); -#else - function_str = PyString_InternFromString("function"); -#endif } if (!classmethod_str) { -#if PY_MAJOR_VERSION >= 3 classmethod_str = PyUnicode_InternFromString("classmethod"); -#else - classmethod_str = PyString_InternFromString("classmethod"); -#endif } if (!staticmethod_str) { -#if PY_MAJOR_VERSION >= 3 staticmethod_str = PyUnicode_InternFromString("staticmethod"); -#else - staticmethod_str = PyString_InternFromString("staticmethod"); -#endif } if (!callable_str) { -#if PY_MAJOR_VERSION >= 3 callable_str = PyUnicode_InternFromString("callable"); -#else - callable_str = PyString_InternFromString("callable"); -#endif } if (!builtin_str) { -#if PY_MAJOR_VERSION >= 3 builtin_str = PyUnicode_InternFromString("builtin"); -#else - builtin_str = PyString_InternFromString("builtin"); -#endif } if (!class_str) { -#if PY_MAJOR_VERSION >= 3 class_str = PyUnicode_InternFromString("class"); -#else - class_str = PyString_InternFromString("class"); -#endif } if (!instancemethod_str) { -#if PY_MAJOR_VERSION >= 3 instancemethod_str = PyUnicode_InternFromString("instancemethod"); -#else - instancemethod_str = PyString_InternFromString("instancemethod"); -#endif } if (PyObject_IsInstance(wrapped, (PyObject *)&WraptFunctionWrapperBase_Type)) { @@ -3234,16 +2969,9 @@ static int WraptFunctionWrapper_init(WraptFunctionWrapperObject *self, binding = staticmethod_str; } else if ((instance = PyObject_GetAttrString(wrapped, "__self__")) != 0) { - #if PY_MAJOR_VERSION < 3 - if (PyObject_IsInstance(instance, (PyObject *)&PyClass_Type) || - PyObject_IsInstance(instance, (PyObject *)&PyType_Type)) { - binding = classmethod_str; - } - #else if (PyObject_IsInstance(instance, (PyObject *)&PyType_Type)) { binding = classmethod_str; } - #endif else if (PyObject_IsInstance(wrapped, (PyObject *)&PyMethod_Type)) { binding = instancemethod_str; } @@ -3296,11 +3024,7 @@ PyTypeObject WraptFunctionWrapper_Type = { 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ -#if PY_MAJOR_VERSION < 3 - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ -#else Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ -#endif 0, /*tp_doc*/ 0, /*tp_traverse*/ 0, /*tp_clear*/ @@ -3325,7 +3049,6 @@ PyTypeObject WraptFunctionWrapper_Type = { /* ------------------------------------------------------------------------- */ -#if PY_MAJOR_VERSION >= 3 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "_wrappers", /* m_name */ @@ -3337,18 +3060,13 @@ static struct PyModuleDef moduledef = { NULL, /* m_clear */ NULL, /* m_free */ }; -#endif static PyObject * moduleinit(void) { PyObject *module; -#if PY_MAJOR_VERSION >= 3 module = PyModule_Create(&moduledef); -#else - module = Py_InitModule3("_wrappers", NULL, NULL); -#endif if (module == NULL) return NULL; @@ -3401,16 +3119,9 @@ moduleinit(void) return module; } -#if PY_MAJOR_VERSION < 3 -PyMODINIT_FUNC init_wrappers(void) -{ - moduleinit(); -} -#else PyMODINIT_FUNC PyInit__wrappers(void) { return moduleinit(); } -#endif /* ------------------------------------------------------------------------- */