Python types.ModuleType.__init__() Examples

The following are 30 code examples of types.ModuleType.__init__(). 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 types.ModuleType , or try the search function .
Example #1
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def weakref_segfault():
    # SF 742911
    if verbose:
        print "Testing weakref segfault..."

    import weakref

    class Provoker:
        def __init__(self, referrent):
            self.ref = weakref.ref(referrent)

        def __del__(self):
            x = self.ref()

    class Oops(object):
        pass

    o = Oops()
    o.whatever = Provoker(o)
    del o 
Example #2
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def proxysuper():
    if verbose:
        print "Testing super() for a proxy object..."
    class Proxy(object):
        def __init__(self, obj):
            self.__obj = obj
        def __getattribute__(self, name):
            if name.startswith("_Proxy__"):
                return object.__getattribute__(self, name)
            else:
                return getattr(self.__obj, name)

    class B(object):
        def f(self):
            return "B.f"

    class C(B):
        def f(self):
            return super(C, self).f() + "->C.f"

    obj = C()
    p = Proxy(obj)
    vereq(C.__dict__["f"](p), "B.f->C.f") 
Example #3
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def weakref_segfault():
    # SF 742911
    if verbose:
        print "Testing weakref segfault..."

    import weakref

    class Provoker:
        def __init__(self, referrent):
            self.ref = weakref.ref(referrent)

        def __del__(self):
            x = self.ref()

    class Oops(object):
        pass

    o = Oops()
    o.whatever = Provoker(o)
    del o 
Example #4
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def methods():
    if verbose: print "Testing methods..."
    class C(object):
        def __init__(self, x):
            self.x = x
        def foo(self):
            return self.x
    c1 = C(1)
    vereq(c1.foo(), 1)
    class D(C):
        boo = C.foo
        goo = c1.foo
    d2 = D(2)
    vereq(d2.foo(), 2)
    vereq(d2.boo(), 2)
    vereq(d2.goo(), 1)
    class E(object):
        foo = C.foo
    vereq(E().foo, C.foo) # i.e., unbound
    verify(repr(C.foo.__get__(C(1))).startswith("<bound method ")) 
Example #5
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def funnynew():
    if verbose: print "Testing __new__ returning something unexpected..."
    class C(object):
        def __new__(cls, arg):
            if isinstance(arg, str): return [1, 2, 3]
            elif isinstance(arg, int): return object.__new__(D)
            else: return object.__new__(cls)
    class D(C):
        def __init__(self, arg):
            self.foo = arg
    vereq(C("1"), [1, 2, 3])
    vereq(D("1"), [1, 2, 3])
    d = D(None)
    veris(d.foo, None)
    d = C(1)
    vereq(isinstance(d, D), True)
    vereq(d.foo, 1)
    d = D(1)
    vereq(isinstance(d, D), True)
    vereq(d.foo, 1) 
Example #6
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def proxysuper():
    if verbose:
        print "Testing super() for a proxy object..."
    class Proxy(object):
        def __init__(self, obj):
            self.__obj = obj
        def __getattribute__(self, name):
            if name.startswith("_Proxy__"):
                return object.__getattribute__(self, name)
            else:
                return getattr(self.__obj, name)

    class B(object):
        def f(self):
            return "B.f"

    class C(B):
        def f(self):
            return super(C, self).f() + "->C.f"

    obj = C()
    p = Proxy(obj)
    vereq(C.__dict__["f"](p), "B.f->C.f") 
Example #7
Source File: test_descr.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def weakref_segfault():
    # SF 742911
    if verbose:
        print "Testing weakref segfault..."

    import weakref

    class Provoker:
        def __init__(self, referrent):
            self.ref = weakref.ref(referrent)

        def __del__(self):
            x = self.ref()

    class Oops(object):
        pass

    o = Oops()
    o.whatever = Provoker(o)
    del o 
Example #8
Source File: test_descr.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def proxysuper():
    if verbose:
        print "Testing super() for a proxy object..."
    class Proxy(object):
        def __init__(self, obj):
            self.__obj = obj
        def __getattribute__(self, name):
            if name.startswith("_Proxy__"):
                return object.__getattribute__(self, name)
            else:
                return getattr(self.__obj, name)

    class B(object):
        def f(self):
            return "B.f"

    class C(B):
        def f(self):
            return super(C, self).f() + "->C.f"

    obj = C()
    p = Proxy(obj)
    vereq(C.__dict__["f"](p), "B.f->C.f") 
Example #9
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def methods():
    if verbose: print "Testing methods..."
    class C(object):
        def __init__(self, x):
            self.x = x
        def foo(self):
            return self.x
    c1 = C(1)
    vereq(c1.foo(), 1)
    class D(C):
        boo = C.foo
        goo = c1.foo
    d2 = D(2)
    vereq(d2.foo(), 2)
    vereq(d2.boo(), 2)
    vereq(d2.goo(), 1)
    class E(object):
        foo = C.foo
    vereq(E().foo, C.foo) # i.e., unbound
    verify(repr(C.foo.__get__(C(1))).startswith("<bound method ")) 
Example #10
Source File: test_descr.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def methods():
    if verbose: print "Testing methods..."
    class C(object):
        def __init__(self, x):
            self.x = x
        def foo(self):
            return self.x
    c1 = C(1)
    vereq(c1.foo(), 1)
    class D(C):
        boo = C.foo
        goo = c1.foo
    d2 = D(2)
    vereq(d2.foo(), 2)
    vereq(d2.boo(), 2)
    vereq(d2.goo(), 1)
    class E(object):
        foo = C.foo
    vereq(E().foo, C.foo) # i.e., unbound
    verify(repr(C.foo.__get__(C(1))).startswith("<bound method ")) 
Example #11
Source File: test_descr.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def funnynew():
    if verbose: print "Testing __new__ returning something unexpected..."
    class C(object):
        def __new__(cls, arg):
            if isinstance(arg, str): return [1, 2, 3]
            elif isinstance(arg, int): return object.__new__(D)
            else: return object.__new__(cls)
    class D(C):
        def __init__(self, arg):
            self.foo = arg
    vereq(C("1"), [1, 2, 3])
    vereq(D("1"), [1, 2, 3])
    d = D(None)
    veris(d.foo, None)
    d = C(1)
    vereq(isinstance(d, D), True)
    vereq(d.foo, 1)
    d = D(1)
    vereq(isinstance(d, D), True)
    vereq(d.foo, 1) 
Example #12
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def funnynew():
    if verbose: print "Testing __new__ returning something unexpected..."
    class C(object):
        def __new__(cls, arg):
            if isinstance(arg, str): return [1, 2, 3]
            elif isinstance(arg, int): return object.__new__(D)
            else: return object.__new__(cls)
    class D(C):
        def __init__(self, arg):
            self.foo = arg
    vereq(C("1"), [1, 2, 3])
    vereq(D("1"), [1, 2, 3])
    d = D(None)
    veris(d.foo, None)
    d = C(1)
    vereq(isinstance(d, D), True)
    vereq(d.foo, 1)
    d = D(1)
    vereq(isinstance(d, D), True)
    vereq(d.foo, 1) 
Example #13
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def isinst_isclass():
    if verbose:
        print "Testing proxy isinstance() and isclass()..."
    class Proxy(object):
        def __init__(self, obj):
            self.__obj = obj
        def __getattribute__(self, name):
            if name.startswith("_Proxy__"):
                return object.__getattribute__(self, name)
            else:
                return getattr(self.__obj, name)
    # Test with a classic class
    class C:
        pass
    a = C()
    pa = Proxy(a)
    verify(isinstance(a, C))  # Baseline
    verify(isinstance(pa, C)) # Test
    # Test with a classic subclass
    class D(C):
        pass
    a = D()
    pa = Proxy(a)
    verify(isinstance(a, C))  # Baseline
    verify(isinstance(pa, C)) # Test
    # Test with a new-style class
    class C(object):
        pass
    a = C()
    pa = Proxy(a)
    verify(isinstance(a, C))  # Baseline
    verify(isinstance(pa, C)) # Test
    # Test with a new-style subclass
    class D(C):
        pass
    a = D()
    pa = Proxy(a)
    verify(isinstance(a, C))  # Baseline
    verify(isinstance(pa, C)) # Test 
Example #14
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def slottrash():
    # Deallocating deeply nested slotted trash caused stack overflows
    if verbose:
        print "Testing slot trash..."
    class trash(object):
        __slots__ = ['x']
        def __init__(self, x):
            self.x = x
    o = None
    for i in xrange(50000):
        o = trash(o)
    del o 
Example #15
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def slottrash():
    # Deallocating deeply nested slotted trash caused stack overflows
    if verbose:
        print "Testing slot trash..."
    class trash(object):
        __slots__ = ['x']
        def __init__(self, x):
            self.x = x
    o = None
    for i in xrange(50000):
        o = trash(o)
    del o 
Example #16
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def kwdargs():
    if verbose: print "Testing keyword arguments to __init__, __call__..."
    def f(a): return a
    vereq(f.__call__(a=42), 42)
    a = []
    list.__init__(a, sequence=[0, 1, 2])
    vereq(a, [0, 1, 2]) 
Example #17
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def classic_comparisons():
    if verbose: print "Testing classic comparisons..."
    class classic:
        pass
    for base in (classic, int, object):
        if verbose: print "        (base = %s)" % base
        class C(base):
            def __init__(self, value):
                self.value = int(value)
            def __cmp__(self, other):
                if isinstance(other, C):
                    return cmp(self.value, other.value)
                if isinstance(other, int) or isinstance(other, long):
                    return cmp(self.value, other)
                return NotImplemented
        c1 = C(1)
        c2 = C(2)
        c3 = C(3)
        vereq(c1, 1)
        c = {1: c1, 2: c2, 3: c3}
        for x in 1, 2, 3:
            for y in 1, 2, 3:
                verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
                for op in "<", "<=", "==", "!=", ">", ">=":
                    verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
                           "x=%d, y=%d" % (x, y))
                verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y))
                verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) 
Example #18
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def str_subclass_as_dict_key():
    if verbose:
        print "Testing a str subclass used as dict key .."

    class cistr(str):
        """Sublcass of str that computes __eq__ case-insensitively.

        Also computes a hash code of the string in canonical form.
        """

        def __init__(self, value):
            self.canonical = value.lower()
            self.hashcode = hash(self.canonical)

        def __eq__(self, other):
            if not isinstance(other, cistr):
                other = cistr(other)
            return self.canonical == other.canonical

        def __hash__(self):
            return self.hashcode

    vereq(cistr('ABC'), 'abc')
    vereq('aBc', cistr('ABC'))
    vereq(str(cistr('ABC')), 'ABC')

    d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3}
    vereq(d[cistr('one')], 1)
    vereq(d[cistr('tWo')], 2)
    vereq(d[cistr('THrEE')], 3)
    verify(cistr('ONe') in d)
    vereq(d.get(cistr('thrEE')), 3) 
Example #19
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def compattr():
    if verbose: print "Testing computed attributes..."
    class C(object):
        class computed_attribute(object):
            def __init__(self, get, set=None, delete=None):
                self.__get = get
                self.__set = set
                self.__delete = delete
            def __get__(self, obj, type=None):
                return self.__get(obj)
            def __set__(self, obj, value):
                return self.__set(obj, value)
            def __delete__(self, obj):
                return self.__delete(obj)
        def __init__(self):
            self.__x = 0
        def __get_x(self):
            x = self.__x
            self.__x = x+1
            return x
        def __set_x(self, x):
            self.__x = x
        def __delete_x(self):
            del self.__x
        x = computed_attribute(__get_x, __set_x, __delete_x)
    a = C()
    vereq(a.x, 0)
    vereq(a.x, 1)
    a.x = 10
    vereq(a.x, 10)
    vereq(a.x, 11)
    del a.x
    vereq(hasattr(a, 'x'), 0) 
Example #20
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, first, last):
            self.first = first
            self.last = last 
Example #21
Source File: splitbrain.py    From backdoorme with MIT License 5 votes vote down vote up
def __init__(self):
        ModuleType.__init__(self, "sys", sys.__doc__) 
Example #22
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_init():
    # SF 1155938
    class Foo(object):
        def __init__(self):
            return 10
    try:
        Foo()
    except TypeError:
        pass
    else:
        raise TestFailed, "did not test __init__() for None return" 
Example #23
Source File: test_descr.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def pymods():
    if verbose: print "Testing Python subclass of module..."
    log = []
    from types import ModuleType as MT
    class MM(MT):
        def __init__(self, name):
            MT.__init__(self, name)
        def __getattribute__(self, name):
            log.append(("getattr", name))
            return MT.__getattribute__(self, name)
        def __setattr__(self, name, value):
            log.append(("setattr", name, value))
            MT.__setattr__(self, name, value)
        def __delattr__(self, name):
            log.append(("delattr", name))
            MT.__delattr__(self, name)
    a = MM("a")
    a.foo = 12
    x = a.foo
    del a.foo
    vereq(log, [("setattr", "foo", 12),
                ("getattr", "foo"),
                ("delattr", "foo")])

    # http://python.org/sf/1174712
    try:
        class Module(types.ModuleType, str):
            pass
    except TypeError:
        pass
    else:
        raise TestFailed("inheriting from ModuleType and str at the "
                          "same time should fail") 
Example #24
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def isinst_isclass():
    if verbose:
        print "Testing proxy isinstance() and isclass()..."
    class Proxy(object):
        def __init__(self, obj):
            self.__obj = obj
        def __getattribute__(self, name):
            if name.startswith("_Proxy__"):
                return object.__getattribute__(self, name)
            else:
                return getattr(self.__obj, name)
    # Test with a classic class
    class C:
        pass
    a = C()
    pa = Proxy(a)
    verify(isinstance(a, C))  # Baseline
    verify(isinstance(pa, C)) # Test
    # Test with a classic subclass
    class D(C):
        pass
    a = D()
    pa = Proxy(a)
    verify(isinstance(a, C))  # Baseline
    verify(isinstance(pa, C)) # Test
    # Test with a new-style class
    class C(object):
        pass
    a = C()
    pa = Proxy(a)
    verify(isinstance(a, C))  # Baseline
    verify(isinstance(pa, C)) # Test
    # Test with a new-style subclass
    class D(C):
        pass
    a = D()
    pa = Proxy(a)
    verify(isinstance(a, C))  # Baseline
    verify(isinstance(pa, C)) # Test 
Example #25
Source File: splitbrain.py    From backdoorme with MIT License 5 votes vote down vote up
def __init__(self, realmod):
        ModuleType.__init__(self, realmod.__name__, getattr(realmod, "__doc__", None))
        object.__setattr__(self, "__realmod__", realmod)
        object.__setattr__(self, "__file__", getattr(realmod, "__file__", None)) 
Example #26
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def kwdargs():
    if verbose: print "Testing keyword arguments to __init__, __call__..."
    def f(a): return a
    vereq(f.__call__(a=42), 42)
    a = []
    list.__init__(a, sequence=[0, 1, 2])
    vereq(a, [0, 1, 2]) 
Example #27
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def classic_comparisons():
    if verbose: print "Testing classic comparisons..."
    class classic:
        pass
    for base in (classic, int, object):
        if verbose: print "        (base = %s)" % base
        class C(base):
            def __init__(self, value):
                self.value = int(value)
            def __cmp__(self, other):
                if isinstance(other, C):
                    return cmp(self.value, other.value)
                if isinstance(other, int) or isinstance(other, long):
                    return cmp(self.value, other)
                return NotImplemented
        c1 = C(1)
        c2 = C(2)
        c3 = C(3)
        vereq(c1, 1)
        c = {1: c1, 2: c2, 3: c3}
        for x in 1, 2, 3:
            for y in 1, 2, 3:
                verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
                for op in "<", "<=", "==", "!=", ">", ">=":
                    verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
                           "x=%d, y=%d" % (x, y))
                verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y))
                verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y)) 
Example #28
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def str_subclass_as_dict_key():
    if verbose:
        print "Testing a str subclass used as dict key .."

    class cistr(str):
        """Sublcass of str that computes __eq__ case-insensitively.

        Also computes a hash code of the string in canonical form.
        """

        def __init__(self, value):
            self.canonical = value.lower()
            self.hashcode = hash(self.canonical)

        def __eq__(self, other):
            if not isinstance(other, cistr):
                other = cistr(other)
            return self.canonical == other.canonical

        def __hash__(self):
            return self.hashcode

    vereq(cistr('ABC'), 'abc')
    vereq('aBc', cistr('ABC'))
    vereq(str(cistr('ABC')), 'ABC')

    d = {cistr('one'): 1, cistr('two'): 2, cistr('tHree'): 3}
    vereq(d[cistr('one')], 1)
    vereq(d[cistr('tWo')], 2)
    vereq(d[cistr('THrEE')], 3)
    verify(cistr('ONe') in d)
    vereq(d.get(cistr('thrEE')), 3) 
Example #29
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def compattr():
    if verbose: print "Testing computed attributes..."
    class C(object):
        class computed_attribute(object):
            def __init__(self, get, set=None, delete=None):
                self.__get = get
                self.__set = set
                self.__delete = delete
            def __get__(self, obj, type=None):
                return self.__get(obj)
            def __set__(self, obj, value):
                return self.__set(obj, value)
            def __delete__(self, obj):
                return self.__delete(obj)
        def __init__(self):
            self.__x = 0
        def __get_x(self):
            x = self.__x
            self.__x = x+1
            return x
        def __set_x(self, x):
            self.__x = x
        def __delete_x(self):
            del self.__x
        x = computed_attribute(__get_x, __set_x, __delete_x)
    a = C()
    vereq(a.x, 0)
    vereq(a.x, 1)
    a.x = 10
    vereq(a.x, 10)
    vereq(a.x, 11)
    del a.x
    vereq(hasattr(a, 'x'), 0) 
Example #30
Source File: test_descr.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def pymods():
    if verbose: print "Testing Python subclass of module..."
    log = []
    from types import ModuleType as MT
    class MM(MT):
        def __init__(self, name):
            MT.__init__(self, name)
        def __getattribute__(self, name):
            log.append(("getattr", name))
            return MT.__getattribute__(self, name)
        def __setattr__(self, name, value):
            log.append(("setattr", name, value))
            MT.__setattr__(self, name, value)
        def __delattr__(self, name):
            log.append(("delattr", name))
            MT.__delattr__(self, name)
    a = MM("a")
    a.foo = 12
    x = a.foo
    del a.foo
    vereq(log, [("setattr", "foo", 12),
                ("getattr", "foo"),
                ("delattr", "foo")])

    # http://python.org/sf/1174712
    try:
        class Module(types.ModuleType, str):
            pass
    except TypeError:
        pass
    else:
        raise TestFailed("inheriting from ModuleType and str at the "
                          "same time should fail")