mirror of https://github.com/python/cpython
gh-107735: Move just added C API tests to better place (GH-107743)
This commit is contained in:
parent
0e6e32fb84
commit
0191af97a6
|
@ -992,6 +992,46 @@ class CAPITest(unittest.TestCase):
|
|||
del d.extra
|
||||
self.assertIsNone(d.extra)
|
||||
|
||||
def test_sys_getobject(self):
|
||||
getobject = _testcapi.sys_getobject
|
||||
|
||||
self.assertIs(getobject(b'stdout'), sys.stdout)
|
||||
with support.swap_attr(sys, '\U0001f40d', 42):
|
||||
self.assertEqual(getobject('\U0001f40d'.encode()), 42)
|
||||
|
||||
self.assertIs(getobject(b'nonexisting'), AttributeError)
|
||||
self.assertIs(getobject(b'\xff'), AttributeError)
|
||||
# CRASHES getobject(NULL)
|
||||
|
||||
def test_sys_setobject(self):
|
||||
setobject = _testcapi.sys_setobject
|
||||
|
||||
value = ['value']
|
||||
value2 = ['value2']
|
||||
try:
|
||||
self.assertEqual(setobject(b'newattr', value), 0)
|
||||
self.assertIs(sys.newattr, value)
|
||||
self.assertEqual(setobject(b'newattr', value2), 0)
|
||||
self.assertIs(sys.newattr, value2)
|
||||
self.assertEqual(setobject(b'newattr', NULL), 0)
|
||||
self.assertFalse(hasattr(sys, 'newattr'))
|
||||
self.assertEqual(setobject(b'newattr', NULL), 0)
|
||||
finally:
|
||||
with contextlib.suppress(AttributeError):
|
||||
del sys.newattr
|
||||
try:
|
||||
self.assertEqual(setobject('\U0001f40d'.encode(), value), 0)
|
||||
self.assertIs(getattr(sys, '\U0001f40d'), value)
|
||||
self.assertEqual(setobject('\U0001f40d'.encode(), NULL), 0)
|
||||
self.assertFalse(hasattr(sys, '\U0001f40d'))
|
||||
finally:
|
||||
with contextlib.suppress(AttributeError):
|
||||
delattr(sys, '\U0001f40d')
|
||||
|
||||
with self.assertRaises(UnicodeDecodeError):
|
||||
setobject(b'\xff', value)
|
||||
# CRASHES setobject(NULL, value)
|
||||
|
||||
|
||||
@requires_limited_api
|
||||
class TestHeapTypeRelative(unittest.TestCase):
|
||||
|
@ -2578,46 +2618,6 @@ class TestUops(unittest.TestCase):
|
|||
with self.assertRaises(StopIteration):
|
||||
next(it)
|
||||
|
||||
def test_sys_getobject(self):
|
||||
getobject = _testcapi.sys_getobject
|
||||
|
||||
self.assertIs(getobject(b'stdout'), sys.stdout)
|
||||
with support.swap_attr(sys, '\U0001f40d', 42):
|
||||
self.assertEqual(getobject('\U0001f40d'.encode()), 42)
|
||||
|
||||
self.assertIs(getobject(b'nonexisting'), AttributeError)
|
||||
self.assertIs(getobject(b'\xff'), AttributeError)
|
||||
# CRASHES getobject(NULL)
|
||||
|
||||
def test_sys_setobject(self):
|
||||
setobject = _testcapi.sys_setobject
|
||||
|
||||
value = ['value']
|
||||
value2 = ['value2']
|
||||
try:
|
||||
self.assertEqual(setobject(b'newattr', value), 0)
|
||||
self.assertIs(sys.newattr, value)
|
||||
self.assertEqual(setobject(b'newattr', value2), 0)
|
||||
self.assertIs(sys.newattr, value2)
|
||||
self.assertEqual(setobject(b'newattr', NULL), 0)
|
||||
self.assertFalse(hasattr(sys, 'newattr'))
|
||||
self.assertEqual(setobject(b'newattr', NULL), 0)
|
||||
finally:
|
||||
with contextlib.suppress(AttributeError):
|
||||
del sys.newattr
|
||||
try:
|
||||
self.assertEqual(setobject('\U0001f40d'.encode(), value), 0)
|
||||
self.assertIs(getattr(sys, '\U0001f40d'), value)
|
||||
self.assertEqual(setobject('\U0001f40d'.encode(), NULL), 0)
|
||||
self.assertFalse(hasattr(sys, '\U0001f40d'))
|
||||
finally:
|
||||
with contextlib.suppress(AttributeError):
|
||||
delattr(sys, '\U0001f40d')
|
||||
|
||||
with self.assertRaises(UnicodeDecodeError):
|
||||
setobject(b'\xff', value)
|
||||
# CRASHES setobject(NULL, value)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in New Issue