How to use the wrapt.BoundFunctionWrapper function in wrapt

To help you get started, we’ve selected a few wrapt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ionelmc / python-lazy-object-proxy / tests / test_function_wrapper.py View on Github external
instance = object()

        _bound_wrapper_1 = _wrapper.__get__(instance, type(instance))

        self.assertTrue(_bound_wrapper_1._self_parent is _wrapper)

        self.assertTrue(isinstance(_bound_wrapper_1,
                wrapt.BoundFunctionWrapper))
        self.assertEqual(_bound_wrapper_1._self_instance, instance)

        _bound_wrapper_2 = _bound_wrapper_1.__get__(instance, type(instance))

        self.assertTrue(_bound_wrapper_2._self_parent is _wrapper)

        self.assertTrue(isinstance(_bound_wrapper_2,
                wrapt.BoundFunctionWrapper))
        self.assertEqual(_bound_wrapper_2._self_instance,
                _bound_wrapper_1._self_instance)

        self.assertTrue(_bound_wrapper_1 is _bound_wrapper_2)
github DataDog / dd-trace-py / tests / contrib / celery / test_task.py View on Github external
def test_patch_task(self):
        """
        When celery.Task is patched
            we patch the __init__, apply, apply_async, and run methods
        """
        # Assert base class methods are patched
        self.assertIsInstance(celery.Task.__init__, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(celery.Task.apply, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(celery.Task.apply_async, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(celery.Task.run, wrapt.BoundFunctionWrapper)

        # Create an instance of a Task
        task = celery.Task()

        # Assert instance methods are patched
        self.assertIsInstance(task.__init__, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(task.apply, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(task.apply_async, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(task.run, wrapt.BoundFunctionWrapper)
github DataDog / dd-trace-py / tests / contrib / celery / test_task.py View on Github external
def test_patch_task(self):
        """
        When celery.Task is patched
            we patch the __init__, apply, apply_async, and run methods
        """
        # Assert base class methods are patched
        self.assertIsInstance(celery.Task.__init__, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(celery.Task.apply, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(celery.Task.apply_async, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(celery.Task.run, wrapt.BoundFunctionWrapper)

        # Create an instance of a Task
        task = celery.Task()

        # Assert instance methods are patched
        self.assertIsInstance(task.__init__, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(task.apply, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(task.apply_async, wrapt.BoundFunctionWrapper)
        self.assertIsInstance(task.run, wrapt.BoundFunctionWrapper)
github ionelmc / python-lazy-object-proxy / tests / test_function_wrapper.py View on Github external
def test_override_bound_type(self):

        class _BoundFunctionWrapper(wrapt.BoundFunctionWrapper):
            ATTRIBUTE = 1

        class _FunctionWrapper(wrapt.FunctionWrapper):
            __bound_function_wrapper__ = _BoundFunctionWrapper

        def function():
            pass

        def wrapper(wrapped, instance, args, kwargs):
            return wrapped(*args, **kwargs)

        _wrapper = _FunctionWrapper(function, wrapper)

        self.assertTrue(isinstance(_wrapper, _FunctionWrapper))

        instance = object()
github reahl / reahl / reahl-component / reahl / component / modelinterface.py View on Github external
if parsed_value:
            arguments = parsed_value.copy()
            fields = StandaloneFieldIndex(arguments)
            fields.update_copies(self.event_argument_fields)
            
            arguments.update(fields.as_input_kwargs())
            input_string='?%s' % urllib_parse.urlencode(arguments)
            if six.PY2:
                return input_string.decode('utf-8')
            else:
                return input_string
        else:
            return '?'
    

class SecuredMethod(BoundFunctionWrapper):
    def __init__(self,  *args, **kwargs):
        super(SecuredMethod, self).__init__(*args, **kwargs)
    def _self_read_check(self, *args, **kwargs):
        return self._self_parent.check_right(self.read_check, self._self_instance, *args, **kwargs)
    def _self_write_check(self, *args, **kwargs):
        return self._self_parent.check_right(self.write_check, self._self_instance, *args, **kwargs)


class SecuredFunction(FunctionWrapper):
    __bound_function_wrapper__ = SecuredMethod

    def __init__(self,  wrapped, read_check, write_check):
        super(SecuredFunction, self).__init__(wrapped, self.check_call_wrapped)
        self.check_and_setup_check(read_check)
        self._self_read_check = self.read_check = read_check