本文整理汇总了Python中types.ModuleType.__new__方法的典型用法代码示例。如果您正苦于以下问题:Python ModuleType.__new__方法的具体用法?Python ModuleType.__new__怎么用?Python ModuleType.__new__使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types.ModuleType
的用法示例。
在下文中一共展示了ModuleType.__new__方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_newslots
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
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)
示例2: test_restored_object_new
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
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)
示例3: test_recursions_1
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
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
示例4: test_funny_new
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
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)
示例5: test_object_new_and_init_with_parameters
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
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)
示例6: newslot
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
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)
示例7: funnynew
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
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)
示例8: test_complexes
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
def test_complexes(self):
# Testing complex operations...
self.number_operators(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)
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)
示例9: test_bad_new
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
def test_bad_new(self):
self.assertRaises(TypeError, object.__new__)
self.assertRaises(TypeError, object.__new__, '')
self.assertRaises(TypeError, list.__new__, object)
self.assertRaises(TypeError, object.__new__, list)
class C(object):
__new__ = list.__new__
self.assertRaises(TypeError, C)
class C(list):
__new__ = object.__new__
self.assertRaises(TypeError, C)
示例10: test_uninitialized_modules
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
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})
示例11: test_funny_new
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
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.assertEqual(isinstance(d, D), True)
self.assertEqual(d.foo, 1)
d = D(1)
self.assertEqual(isinstance(d, D), True)
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))
示例12: test_unintialized_modules
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __new__ [as 别名]
def test_unintialized_modules(self):
# Testing uninitialized module objects...
from types import ModuleType as M
m = M.__new__(M)
str(m)
self.assertEqual(hasattr(m, "__name__"), 0)
self.assertEqual(hasattr(m, "__file__"), 0)
self.assertEqual(hasattr(m, "foo"), 0)
self.assertFalse(m.__dict__) # None or {} are both reasonable answers
m.foo = 1
self.assertEqual(m.__dict__, {"foo": 1})