GitHub CopilotWrite better code with AI | MCP RegistryIntegrate external tools | ActionsAutomate any workflow | CodespacesInstant dev environments | IssuesPlan and track work | Code ReviewManage code changes | Code QualityEnforce quality at merge | Why GitHub | Marketplace | View all features | Enterprises | Small and medium teams | Startups | View all use cases | View all industries | View all solutions | AI | Software Development | DevOps | Security | View all topics | Customer stories | Events & webinars | Ebooks & reports | Business insights | Trust center | Partners | View all resources
@@ -19,6 +19,68 @@ class CAPITest(unittest.TestCase):
19192020maxDiff=None
[email protected](_testlimitedcapiisNone, 'need _testlimitedcapi module')
23+deftest_sys_getattr(self):
24+# Test PySys_GetAttr()
25+sys_getattr=_testlimitedcapi.sys_getattr
26+27+self.assertIs(sys_getattr('stdout'), sys.stdout)
28+withsupport.swap_attr(sys, '\U0001f40d', 42):
29+self.assertEqual(sys_getattr('\U0001f40d'), 42)
30+31+withself.assertRaisesRegex(RuntimeError, r'lost sys\.nonexistent'):
32+sys_getattr('nonexistent')
33+withself.assertRaisesRegex(RuntimeError, r'lost sys\.\U0001f40d'):
34+sys_getattr('\U0001f40d')
35+self.assertRaises(TypeError, sys_getattr, 1)
36+self.assertRaises(TypeError, sys_getattr, [])
37+# CRASHES sys_getattr(NULL)
[email protected](_testlimitedcapiisNone, 'need _testlimitedcapi module')
40+deftest_sys_getattrstring(self):
41+# Test PySys_GetAttrString()
42+getattrstring=_testlimitedcapi.sys_getattrstring
43+44+self.assertIs(getattrstring(b'stdout'), sys.stdout)
45+withsupport.swap_attr(sys, '\U0001f40d', 42):
46+self.assertEqual(getattrstring('\U0001f40d'.encode()), 42)
47+48+withself.assertRaisesRegex(RuntimeError, r'lost sys\.nonexistent'):
49+getattrstring(b'nonexistent')
50+withself.assertRaisesRegex(RuntimeError, r'lost sys\.\U0001f40d'):
51+getattrstring('\U0001f40d'.encode())
52+self.assertRaises(UnicodeDecodeError, getattrstring, b'\xff')
53+# CRASHES getattrstring(NULL)
[email protected](_testlimitedcapiisNone, 'need _testlimitedcapi module')
56+deftest_sys_getoptionalattr(self):
57+# Test PySys_GetOptionalAttr()
58+getoptionalattr=_testlimitedcapi.sys_getoptionalattr
59+60+self.assertIs(getoptionalattr('stdout'), sys.stdout)
61+withsupport.swap_attr(sys, '\U0001f40d', 42):
62+self.assertEqual(getoptionalattr('\U0001f40d'), 42)
63+64+self.assertIs(getoptionalattr('nonexistent'), AttributeError)
65+self.assertIs(getoptionalattr('\U0001f40d'), AttributeError)
66+self.assertRaises(TypeError, getoptionalattr, 1)
67+self.assertRaises(TypeError, getoptionalattr, [])
68+# CRASHES getoptionalattr(NULL)
[email protected](_testlimitedcapiisNone, 'need _testlimitedcapi module')
71+deftest_sys_getoptionalattrstring(self):
72+# Test PySys_GetOptionalAttrString()
73+getoptionalattrstring=_testlimitedcapi.sys_getoptionalattrstring
74+75+self.assertIs(getoptionalattrstring(b'stdout'), sys.stdout)
76+withsupport.swap_attr(sys, '\U0001f40d', 42):
77+self.assertEqual(getoptionalattrstring('\U0001f40d'.encode()), 42)
78+79+self.assertIs(getoptionalattrstring(b'nonexistent'), AttributeError)
80+self.assertIs(getoptionalattrstring('\U0001f40d'.encode()), AttributeError)
81+self.assertRaises(UnicodeDecodeError, getoptionalattrstring, b'\xff')
82+# CRASHES getoptionalattrstring(NULL)
[email protected]_only
[email protected](_testlimitedcapiisNone, 'need _testlimitedcapi module')
2486deftest_sys_getobject(self):
@@ -29,7 +91,7 @@ def test_sys_getobject(self):
2991withsupport.swap_attr(sys, '\U0001f40d', 42):
3092self.assertEqual(getobject('\U0001f40d'.encode()), 42)
319332-self.assertIs(getobject(b'nonexisting'), AttributeError)
94+self.assertIs(getobject(b'nonexistent'), AttributeError)
3395withsupport.catch_unraisable_exception() ascm:
3496self.assertIs(getobject(b'\xff'), AttributeError)
3597self.assertEqual(cm.unraisable.exc_type, UnicodeDecodeError)