Python types.ModuleType.__new__() Examples
The following are 30
code examples of types.ModuleType.__new__().
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 |
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 #2
Source File: test_descr.py From BinderFilter with MIT License | 6 votes |
def test_newslots(self): # Testing __new__ slot override... class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() self.assertEqual(a.foo, 3) self.assertEqual(a.__class__, C) class D(C): pass b = D() self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, D)
Example #3
Source File: test_descr.py From oss-ftp with MIT License | 6 votes |
def test_recursions_1(self): # Testing recursion checks ... class Letter(str): def __new__(cls, letter): if letter == 'EPS': return str.__new__(cls) return str.__new__(cls, letter) def __str__(self): if not self: return 'EPS' return self # sys.stdout needs to be the original to trigger the recursion bug test_stdout = sys.stdout sys.stdout = test_support.get_original_stdout() try: # nothing should actually be printed, this should raise an exception print Letter('w') except RuntimeError: pass else: self.fail("expected a RuntimeError for print recursion") finally: sys.stdout = test_stdout
Example #4
Source File: test_descr.py From oss-ftp with MIT License | 6 votes |
def test_newslots(self): # Testing __new__ slot override... class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() self.assertEqual(a.foo, 3) self.assertEqual(a.__class__, C) class D(C): pass b = D() self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, D)
Example #5
Source File: test_descr.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_recursions_1(self): # Testing recursion checks ... class Letter(str): def __new__(cls, letter): if letter == 'EPS': return str.__new__(cls) return str.__new__(cls, letter) def __str__(self): if not self: return 'EPS' return self # sys.stdout needs to be the original to trigger the recursion bug test_stdout = sys.stdout sys.stdout = test_support.get_original_stdout() try: # nothing should actually be printed, this should raise an exception print Letter('w') except RuntimeError: pass else: self.fail("expected a RuntimeError for print recursion") finally: sys.stdout = test_stdout
Example #6
Source File: test_descr.py From BinderFilter with MIT License | 6 votes |
def test_recursions_1(self): # Testing recursion checks ... class Letter(str): def __new__(cls, letter): if letter == 'EPS': return str.__new__(cls) return str.__new__(cls, letter) def __str__(self): if not self: return 'EPS' return self # sys.stdout needs to be the original to trigger the recursion bug test_stdout = sys.stdout sys.stdout = test_support.get_original_stdout() try: # nothing should actually be printed, this should raise an exception print Letter('w') except RuntimeError: pass else: self.fail("expected a RuntimeError for print recursion") finally: sys.stdout = test_stdout
Example #7
Source File: test_descr.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_restored_object_new(self): class A(object): def __new__(cls, *args, **kwargs): raise AssertionError self.assertRaises(AssertionError, A) class B(A): __new__ = object.__new__ def __init__(self, foo): self.foo = foo with warnings.catch_warnings(): warnings.simplefilter('error', DeprecationWarning) b = B(3) self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, B) del B.__new__ self.assertRaises(AssertionError, B) del A.__new__ with warnings.catch_warnings(): warnings.simplefilter('error', DeprecationWarning) b = B(3) self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, B)
Example #8
Source File: test_descr.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_newslots(self): # Testing __new__ slot override... class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() self.assertEqual(a.foo, 3) self.assertEqual(a.__class__, C) class D(C): pass b = D() self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, D)
Example #9
Source File: test_descr.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_funny_new(self): # 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 self.assertEqual(C("1"), [1, 2, 3]) self.assertEqual(D("1"), [1, 2, 3]) d = D(None) self.assertEqual(d.foo, None) d = C(1) self.assertIsInstance(d, D) self.assertEqual(d.foo, 1) d = D(1) self.assertIsInstance(d, D) self.assertEqual(d.foo, 1)
Example #10
Source File: test_descr.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_newslots(self): # Testing __new__ slot override... class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() self.assertEqual(a.foo, 3) self.assertEqual(a.__class__, C) class D(C): pass b = D() self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, D)
Example #11
Source File: test_descr.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_funny_new(self): # 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 self.assertEqual(C("1"), [1, 2, 3]) self.assertEqual(D("1"), [1, 2, 3]) d = D(None) self.assertEqual(d.foo, None) d = C(1) self.assertIsInstance(d, D) self.assertEqual(d.foo, 1) d = D(1) self.assertIsInstance(d, D) self.assertEqual(d.foo, 1)
Example #12
Source File: test_descr.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_newslots(self): # Testing __new__ slot override... class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() self.assertEqual(a.foo, 3) self.assertEqual(a.__class__, C) class D(C): pass b = D() self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, D)
Example #13
Source File: test_descr.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_newslots(self): # Testing __new__ slot override... class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() self.assertEqual(a.foo, 3) self.assertEqual(a.__class__, C) class D(C): pass b = D() self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, D)
Example #14
Source File: test_descr.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_newslots(self): # Testing __new__ slot override... class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() self.assertEqual(a.foo, 3) self.assertEqual(a.__class__, C) class D(C): pass b = D() self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, D)
Example #15
Source File: test_descr.py From CTFCrackTools with GNU General Public License v3.0 | 6 votes |
def newslot(): if verbose: print "Testing __new__ slot override..." class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() vereq(a.foo, 3) verify(a.__class__ is C) class D(C): pass b = D() vereq(b.foo, 3) verify(b.__class__ is D)
Example #16
Source File: test_descr.py From android_universal with MIT License | 6 votes |
def test_object_new_and_init_with_parameters(self): # See issue #1683368 class OverrideNeither: pass self.assertRaises(TypeError, OverrideNeither, 1) self.assertRaises(TypeError, OverrideNeither, kw=1) class OverrideNew: def __new__(cls, foo, kw=0, *args, **kwds): return object.__new__(cls, *args, **kwds) class OverrideInit: def __init__(self, foo, kw=0, *args, **kwargs): return object.__init__(self, *args, **kwargs) class OverrideBoth(OverrideNew, OverrideInit): pass for case in OverrideNew, OverrideInit, OverrideBoth: case(1) case(1, kw=2) self.assertRaises(TypeError, case, 1, 2, 3) self.assertRaises(TypeError, case, 1, 2, foo=3)
Example #17
Source File: test_descr.py From android_universal with MIT License | 6 votes |
def test_newslots(self): # Testing __new__ slot override... class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() self.assertEqual(a.foo, 3) self.assertEqual(a.__class__, C) class D(C): pass b = D() self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, D)
Example #18
Source File: test_descr.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
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 #19
Source File: test_descr.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def newslot(): if verbose: print "Testing __new__ slot override..." class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() vereq(a.foo, 3) verify(a.__class__ is C) class D(C): pass b = D() vereq(b.foo, 3) verify(b.__class__ is D)
Example #20
Source File: test_descr.py From medicare-demo with Apache License 2.0 | 6 votes |
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 #21
Source File: test_descr.py From medicare-demo with Apache License 2.0 | 6 votes |
def newslot(): if verbose: print "Testing __new__ slot override..." class C(list): def __new__(cls): self = list.__new__(cls) self.foo = 1 return self def __init__(self): self.foo = self.foo + 2 a = C() vereq(a.foo, 3) verify(a.__class__ is C) class D(C): pass b = D() vereq(b.foo, 3) verify(b.__class__ is D)
Example #22
Source File: test_descr.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_object_new_and_init_with_parameters(self): # See issue #1683368 class OverrideNeither: pass self.assertRaises(TypeError, OverrideNeither, 1) self.assertRaises(TypeError, OverrideNeither, kw=1) class OverrideNew: def __new__(cls, foo, kw=0, *args, **kwds): return object.__new__(cls, *args, **kwds) class OverrideInit: def __init__(self, foo, kw=0, *args, **kwargs): return object.__init__(self, *args, **kwargs) class OverrideBoth(OverrideNew, OverrideInit): pass for case in OverrideNew, OverrideInit, OverrideBoth: case(1) case(1, kw=2) self.assertRaises(TypeError, case, 1, 2, 3) self.assertRaises(TypeError, case, 1, 2, foo=3)
Example #23
Source File: test_descr.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_restored_object_new(self): class A(object): def __new__(cls, *args, **kwargs): raise AssertionError self.assertRaises(AssertionError, A) class B(A): __new__ = object.__new__ def __init__(self, foo): self.foo = foo with warnings.catch_warnings(): warnings.simplefilter('error', DeprecationWarning) b = B(3) self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, B) del B.__new__ self.assertRaises(AssertionError, B) del A.__new__ with warnings.catch_warnings(): warnings.simplefilter('error', DeprecationWarning) b = B(3) self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, B)
Example #24
Source File: test_descr.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_recursions_1(self): # Testing recursion checks ... class Letter(str): def __new__(cls, letter): if letter == 'EPS': return str.__new__(cls) return str.__new__(cls, letter) def __str__(self): if not self: return 'EPS' return self # sys.stdout needs to be the original to trigger the recursion bug test_stdout = sys.stdout sys.stdout = test_support.get_original_stdout() try: # nothing should actually be printed, this should raise an exception print Letter('w') except RuntimeError: pass else: self.fail("expected a RuntimeError for print recursion") finally: sys.stdout = test_stdout
Example #25
Source File: test_descr.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_object_new(self): class A(object): pass object.__new__(A) self.assertRaises(TypeError, object.__new__, A, 5) object.__init__(A()) self.assertRaises(TypeError, object.__init__, A(), 5) class A(object): def __init__(self, foo): self.foo = foo object.__new__(A) object.__new__(A, 5) object.__init__(A(3)) self.assertRaises(TypeError, object.__init__, A(3), 5) class A(object): def __new__(cls, foo): return object.__new__(cls) object.__new__(A) self.assertRaises(TypeError, object.__new__, A, 5) object.__init__(A(3)) object.__init__(A(3), 5) class A(object): def __new__(cls, foo): return object.__new__(cls) def __init__(self, foo): self.foo = foo object.__new__(A) self.assertRaises(TypeError, object.__new__, A, 5) object.__init__(A(3)) self.assertRaises(TypeError, object.__init__, A(3), 5)
Example #26
Source File: test_descr.py From android_universal with MIT License | 5 votes |
def test_uninitialized_modules(self): # Testing uninitialized module objects... from types import ModuleType as M m = M.__new__(M) str(m) self.assertNotHasAttr(m, "__name__") self.assertNotHasAttr(m, "__file__") self.assertNotHasAttr(m, "foo") self.assertFalse(m.__dict__) # None or {} are both reasonable answers m.foo = 1 self.assertEqual(m.__dict__, {"foo": 1})
Example #27
Source File: test_descr.py From medicare-demo with Apache License 2.0 | 5 votes |
def complexes(): if verbose: print "Testing complex operations..." numops(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge', 'int', 'long', 'float']) class Number(complex): __slots__ = ['prec'] def __new__(cls, *args, **kwds): result = complex.__new__(cls, *args) result.prec = kwds.get('prec', 12) return result def __repr__(self): prec = self.prec if self.imag == 0.0: return "%.*g" % (prec, self.real) if self.real == 0.0: return "%.*gj" % (prec, self.imag) return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag) __str__ = __repr__ a = Number(3.14, prec=6) vereq(repr(a), "3.14") vereq(a.prec, 6) a = Number(a, prec=2) vereq(repr(a), "3.1") vereq(a.prec, 2) a = Number(234.5) vereq(repr(a), "234.5") vereq(a.prec, 12)
Example #28
Source File: test_descr.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def __new__(mcls, name, bases, attrs): if attrs.get('__doc__') is None: attrs['__doc__'] = name # helps when debugging with gdb return type.__new__(mcls, name, bases, attrs)
Example #29
Source File: test_descr.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_complexes(self): # Testing complex operations... self.number_operators(100.0j, 3.0j, skip=['lt', 'le', 'gt', 'ge', 'int', 'float', 'floordiv', 'divmod', 'mod']) class Number(complex): __slots__ = ['prec'] def __new__(cls, *args, **kwds): result = complex.__new__(cls, *args) result.prec = kwds.get('prec', 12) return result def __repr__(self): prec = self.prec if self.imag == 0.0: return "%.*g" % (prec, self.real) if self.real == 0.0: return "%.*gj" % (prec, self.imag) return "(%.*g+%.*gj)" % (prec, self.real, prec, self.imag) __str__ = __repr__ a = Number(3.14, prec=6) self.assertEqual(repr(a), "3.14") self.assertEqual(a.prec, 6) a = Number(a, prec=2) self.assertEqual(repr(a), "3.1") self.assertEqual(a.prec, 2) a = Number(234.5) self.assertEqual(repr(a), "234.5") self.assertEqual(a.prec, 12)
Example #30
Source File: test_descr.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_funny_new(self): # 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 self.assertEqual(C("1"), [1, 2, 3]) self.assertEqual(D("1"), [1, 2, 3]) d = D(None) self.assertEqual(d.foo, None) d = C(1) self.assertIsInstance(d, D) self.assertEqual(d.foo, 1) d = D(1) self.assertIsInstance(d, D) self.assertEqual(d.foo, 1) class C(object): @staticmethod def __new__(*args): return args self.assertEqual(C(1, 2), (C, 1, 2)) class D(C): pass self.assertEqual(D(1, 2), (D, 1, 2)) class C(object): @classmethod def __new__(*args): return args self.assertEqual(C(1, 2), (C, C, 1, 2)) class D(C): pass self.assertEqual(D(1, 2), (D, D, 1, 2))