Skip to content

Commit

Permalink
pythongh-107735: Move just added C API tests to better place (pythonG…
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka authored Aug 7, 2023
1 parent 0e6e32f commit 0191af9
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,46 @@ class Data(_testcapi.ObjExtraData):
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):
Expand Down Expand Up @@ -2578,46 +2618,6 @@ def testfunc(it):
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()

0 comments on commit 0191af9

Please sign in to comment.