Skip to content

Commit

Permalink
Revert addition of enabled to wrap_function_wrapper() as usage doesn'…
Browse files Browse the repository at this point in the history
…t require it.
  • Loading branch information
GrahamDumpleton committed Jul 25, 2023
1 parent 26b1a95 commit 8c60238
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 98 deletions.
12 changes: 6 additions & 6 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Note that version 1.16.0 drops support for Python 2.7 and 3.5. Python version

**New Features**

* The ``wrap_function_wrapper()`` and ``patch_function_wrapper()`` functions now
accept an ``enabled`` argument, which can be a literal boolean value, object
that evaluates as boolean, or a callable object which returns a boolean. In
the case of a callable, determination of whether the wrapper is invoked will
be left until the point of the call. In the other cases, the wrapper will not
be applied if the value evaluates false at the point of applying the wrapper.
* The ``patch_function_wrapper()`` decorator now accepts an ``enabled``
argument, which can be a literal boolean value, object that evaluates as
boolean, or a callable object which returns a boolean. In the case of a
callable, determination of whether the wrapper is invoked will be left until
the point of the call. In the other cases, the wrapper will not be applied if
the value evaluates false at the point of applying the wrapper.

**Features Changed**

Expand Down
4 changes: 2 additions & 2 deletions src/wrapt/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,8 @@ def _wrapper(wrapped, instance, args, kwargs):
return FunctionWrapper(target_wrapped, target_wrapper)
return FunctionWrapper(wrapper, _wrapper)

def wrap_function_wrapper(module, name, wrapper, enabled=None):
return wrap_object(module, name, FunctionWrapper, (wrapper, enabled))
def wrap_function_wrapper(module, name, wrapper):
return wrap_object(module, name, FunctionWrapper, (wrapper,))

def patch_function_wrapper(module, name, enabled=None):
def _wrapper(wrapper):
Expand Down
90 changes: 0 additions & 90 deletions tests/test_monkey_patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ def global_function_1(*args, **kwargs):
def global_function_2(*args, **kwargs):
return args, kwargs

def global_function_2_enabled_literal_false(*args, **kwargs):
return args, kwargs

def global_function_2_enabled_literal_true(*args, **kwargs):
return args, kwargs

def global_function_2_enabled_callable(*args, **kwargs):
return args, kwargs

def global_function_3(*args, **kwargs):
return args, kwargs

Expand Down Expand Up @@ -180,87 +171,6 @@ def wrapper(wrapped, instance, args, kwargs):
self.assertEqual(result, (_args, _kwargs))
self.assertEqual(called[0], (_args, _kwargs))

def test_wrap_function_module_enabled_literal_false(self):

_args = (1, 2)
_kwargs = {'one': 1, 'two': 2}

called = []

def wrapper(wrapped, instance, args, kwargs):
called.append((args, kwargs))
self.assertEqual(instance, None)
self.assertEqual(args, _args)
self.assertEqual(kwargs, _kwargs)
return wrapped(*args, **kwargs)

module = sys.modules[__name__]

wrapt.wrap_function_wrapper(module, 'global_function_2_enabled_literal_false', wrapper, enabled=False)

result = global_function_2_enabled_literal_false(*_args, **_kwargs)

self.assertEqual(result, (_args, _kwargs))
self.assertEqual(called, [])

def test_wrap_function_module_enabled_literal_true(self):

_args = (1, 2)
_kwargs = {'one': 1, 'two': 2}

called = []

def wrapper(wrapped, instance, args, kwargs):
called.append((args, kwargs))
self.assertEqual(instance, None)
self.assertEqual(args, _args)
self.assertEqual(kwargs, _kwargs)
return wrapped(*args, **kwargs)

module = sys.modules[__name__]

wrapt.wrap_function_wrapper(module, 'global_function_2_enabled_literal_true', wrapper, enabled=True)

result = global_function_2_enabled_literal_true(*_args, **_kwargs)

self.assertEqual(result, (_args, _kwargs))
self.assertEqual(called[0], (_args, _kwargs))

def test_wrap_function_module_enabled_callable(self):

_args = (1, 2)
_kwargs = {'one': 1, 'two': 2}

called = []

def wrapper(wrapped, instance, args, kwargs):
called.append((args, kwargs))
self.assertEqual(instance, None)
self.assertEqual(args, _args)
self.assertEqual(kwargs, _kwargs)
return wrapped(*args, **kwargs)

module = sys.modules[__name__]

enable = False

def enabled():
return enable

wrapt.wrap_function_wrapper(module, 'global_function_2_enabled_callable', wrapper, enabled=enabled)

result = global_function_2_enabled_callable(*_args, **_kwargs)

self.assertEqual(result, (_args, _kwargs))
self.assertEqual(called, [])

enable = True

result = global_function_2_enabled_callable(*_args, **_kwargs)

self.assertEqual(result, (_args, _kwargs))
self.assertEqual(called[0], (_args, _kwargs))

def test_wrap_instance_method_module_name(self):

_args = (1, 2)
Expand Down

0 comments on commit 8c60238

Please sign in to comment.