mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.12] gh-107735: Add C API tests for PySys_GetObject() and PySys_SetObject() (GH-107736) (#107740)
[3.12] gh-107735: Add C API tests for PySys_GetObject() and PySys_SetObject() (GH-107736).
(cherry picked from commit bea5f93196
)
This commit is contained in:
parent
9864f9a7c7
commit
72534ca85c
2 changed files with 83 additions and 0 deletions
|
@ -51,6 +51,8 @@ _testcapi = import_helper.import_module('_testcapi')
|
|||
import _testinternalcapi
|
||||
|
||||
|
||||
NULL = None
|
||||
|
||||
def decode_stderr(err):
|
||||
return err.decode('utf-8', 'replace').replace('\r', '')
|
||||
|
||||
|
@ -1121,6 +1123,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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue