Exemplo n.º 1
0
    def test_contextmanager(self):
        """Test unwrapping a context manager."""

        self.assertEqual(
            unwrap_function(contextmanager(my_function)),
            my_function
        )
    def test_wraps(self):
        """Test unwrapping a function from a decorator using wraps."""

        if sys.version_info < (3, 0):
            # In Python 2, functools doesn't set enough information on the
            # wrapped function
            raise unittest.SkipTest(
                "Cannot unwrap @wraps in general case on Python 2.")

        @wraps(my_function)
        def decorated():
            """A decorated function."""
            return my_function()

        self.assertEqual(unwrap_function(decorated), my_function)
Exemplo n.º 3
0
    def test_wraps(self):
        """Test unwrapping a function from a decorator using wraps."""

        if sys.version_info < (3, 0):
            # In Python 2, functools doesn't set enough information on the
            # wrapped function
            raise unittest.SkipTest(
                "Cannot unwrap @wraps in general case on Python 2.")

        @wraps(my_function)
        def decorated():
            """A decorated function."""
            return my_function()

        self.assertEqual(unwrap_function(decorated), my_function)
Exemplo n.º 4
0
    def _function_id(cls, func):
        """
        A unique identifier of a function to prevent adding the same hook
        twice.

        To support dynamically generated functions, take the variables from
        the function closure into account.
        """

        func = unwrap_function(func)

        return (
            func.__code__.co_filename,
            func.__code__.co_firstlineno,
            # variables in the closure might not be hashable
            tuple(str(c.cell_contents) for c in func.__closure__ or ()),
        )
Exemplo n.º 5
0
    def _function_id(cls, func):
        """
        A unique identifier of a function to prevent adding the same hook
        twice.

        To support dynamically generated functions, take the variables from
        the function closure into account.
        """

        func = unwrap_function(func)

        return (
            func.__code__.co_filename,
            func.__code__.co_firstlineno,
            # variables in the closure might not be hashable
            tuple(str(c.cell_contents) for c in func.__closure__ or ()),
        )
    def test_plain_function(self):
        """Test unwrapping the raw function."""

        self.assertEqual(unwrap_function(my_function), my_function)
Exemplo n.º 7
0
    def test_plain_function(self):
        """Test unwrapping the raw function."""

        self.assertEqual(unwrap_function(my_function), my_function)