diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-12-07 09:26:49 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-12-07 09:26:49 (GMT) |
commit | 0c78634d785361dcde0c2c138e8bf5f9bb744d3d (patch) | |
tree | 3b4b16b83fd8b377fa0c61c21be9695890c21250 /Lib/test/test_descr.py | |
parent | 520348e5c0284668738e0ba98b86e7d1a5fd8600 (diff) | |
parent | 5adfac2c1b7fb4f0782d097e7e0e6c5614965634 (diff) | |
download | cpython-0c78634d785361dcde0c2c138e8bf5f9bb744d3d.zip cpython-0c78634d785361dcde0c2c138e8bf5f9bb744d3d.tar.gz cpython-0c78634d785361dcde0c2c138e8bf5f9bb744d3d.tar.bz2 |
Issue #5322: Fixed setting __new__ to a PyCFunction inside Python code.
Original patch by Andreas Stührk.
Diffstat (limited to 'Lib/test/test_descr.py')
-rw-r--r-- | Lib/test/test_descr.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 1e08ed9..ebfcb77 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -7,6 +7,7 @@ import pickle import sys import types import unittest +import warnings import weakref from copy import deepcopy @@ -1661,6 +1662,75 @@ order (MRO) for bases """ self.assertEqual(b.foo, 3) self.assertEqual(b.__class__, D) + 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) + + 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) + + 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) + def test_altmro(self): # Testing mro() and overriding it... class A(object): @@ -3522,6 +3592,24 @@ order (MRO) for bases """ 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)) + def test_imul_bug(self): # Testing for __imul__ problems... # SF bug 544647 |