Python six.create_unbound_method() Examples

The following are 7 code examples of six.create_unbound_method(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six , or try the search function .
Example #1
Source File: utils.py    From designate with Apache License 2.0 5 votes vote down vote up
def parameterized_class(cls):
    """A class decorator for running parameterized test cases.
    Mark your class with @parameterized_class.
    Mark your test cases with @parameterized.
    """
    test_functions = {
        k: v for k, v in vars(cls).items() if k.startswith('test')
    }
    for name, f in test_functions.items():
        if not hasattr(f, '_test_data'):
            continue

        # remove the original test function from the class
        delattr(cls, name)

        # add a new test function to the class for each entry in f._test_data
        for tag, args in f._test_data.items():
            new_name = "{0}_{1}".format(f.__name__, tag)
            if hasattr(cls, new_name):
                raise Exception(
                    "Parameterized test case '{0}.{1}' created from '{0}.{2}' "
                    "already exists".format(cls.__name__, new_name, name))

            # Using `def new_method(self): f(self, **args)` is not sufficient
            # (all new_methods use the same args value due to late binding).
            # Instead, use this factory function.
            new_method = def_method(f, **args)

            # To add a method to a class, available for all instances:
            #   MyClass.method = types.MethodType(f, None, MyClass)
            setattr(cls, new_name, six.create_unbound_method(new_method, cls))
    return cls 
Example #2
Source File: test_six.py    From six with MIT License 5 votes vote down vote up
def test_create_unbound_method():
    class X(object):
        pass

    def f(self):
        return self
    u = six.create_unbound_method(f, X)
    pytest.raises(TypeError, u)
    if six.PY2:
        assert isinstance(u, types.MethodType)
    x = X()
    assert f(x) is x 
Example #3
Source File: display.py    From python-xlib with GNU Lesser General Public License v2.1 5 votes vote down vote up
def extension_add_method(self, object, name, function):
        """extension_add_method(object, name, function)

        Add an X extension module method.  OBJECT is the type of
        object to add the function to, a string from this list:

            display
            resource
            drawable
            window
            pixmap
            fontable
            font
            gc
            colormap
            cursor

        NAME is the name of the method, a string.  FUNCTION is a
        normal function whose first argument is a 'self'.
        """

        if object == 'display':
            if hasattr(self, name):
                raise AssertionError('attempting to replace display method: %s' % name)

            self.display_extension_methods[name] = function

        else:
            class_list = (object, ) + _resource_hierarchy.get(object, ())
            for class_name in class_list:
                cls = _resource_baseclasses[class_name]
                if hasattr(cls, name):
                    raise AssertionError('attempting to replace %s method: %s' % (class_name, name))

                method = create_unbound_method(function, cls)

                # Maybe should check extension overrides too
                try:
                    self.class_extension_dicts[class_name][name] = method
                except KeyError:
                    self.class_extension_dicts[class_name] = { name: method } 
Example #4
Source File: display.py    From MIA-Dictionary-Addon with GNU General Public License v3.0 5 votes vote down vote up
def extension_add_method(self, object, name, function):
        """extension_add_method(object, name, function)

        Add an X extension module method.  OBJECT is the type of
        object to add the function to, a string from this list:

            display
            resource
            drawable
            window
            pixmap
            fontable
            font
            gc
            colormap
            cursor

        NAME is the name of the method, a string.  FUNCTION is a
        normal function whose first argument is a 'self'.
        """

        if object == 'display':
            if hasattr(self, name):
                raise AssertionError('attempting to replace display method: %s' % name)

            self.display_extension_methods[name] = function

        else:
            class_list = (object, ) + _resource_hierarchy.get(object, ())
            for class_name in class_list:
                cls = _resource_baseclasses[class_name]
                if hasattr(cls, name):
                    raise AssertionError('attempting to replace %s method: %s' % (class_name, name))

                method = create_unbound_method(function, cls)

                # Maybe should check extension overrides too
                try:
                    self.class_extension_dicts[class_name][name] = method
                except KeyError:
                    self.class_extension_dicts[class_name] = { name: method } 
Example #5
Source File: test_six.py    From c4ddev with MIT License 5 votes vote down vote up
def test_create_unbound_method():
    class X(object):
        pass

    def f(self):
        return self
    u = six.create_unbound_method(f, X)
    py.test.raises(TypeError, u)
    if six.PY2:
        assert isinstance(u, types.MethodType)
    x = X()
    assert f(x) is x 
Example #6
Source File: test_six.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def test_create_unbound_method():
    class X(object):
        pass

    def f(self):
        return self
    u = six.create_unbound_method(f, X)
    py.test.raises(TypeError, u)
    if six.PY2:
        assert isinstance(u, types.MethodType)
    x = X()
    assert f(x) is x 
Example #7
Source File: test_six.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def test_create_unbound_method():
    class X(object):
        pass

    def f(self):
        return self
    u = six.create_unbound_method(f, X)
    py.test.raises(TypeError, u)
    if six.PY2:
        assert isinstance(u, types.MethodType)
    x = X()
    assert f(x) is x