44from test import support
55from test .support import import_helper
66
7- try :
8- import _testlimitedcapi
9- except ImportError :
10- _testlimitedcapi = None
7+ _testlimitedcapi = import_helper .import_module ('_testlimitedcapi' )
8+ _testcapi = import_helper .import_module ('_testcapi' )
119
1210NULL = None
1311
12+
1413class CAPITest (unittest .TestCase ):
1514 # TODO: Test the following functions:
1615 #
@@ -20,7 +19,6 @@ class CAPITest(unittest.TestCase):
2019 maxDiff = None
2120
2221 @support .cpython_only
23- @unittest .skipIf (_testlimitedcapi is None , 'need _testlimitedcapi module' )
2422 def test_sys_getobject (self ):
2523 # Test PySys_GetObject()
2624 getobject = _testlimitedcapi .sys_getobject
@@ -37,8 +35,22 @@ def test_sys_getobject(self):
3735 "'utf-8' codec can't decode" )
3836 # CRASHES getobject(NULL)
3937
38+ def test_sys_getattr (self ):
39+ # Test PySys_GetAttr()
40+ sys_getattr = _testcapi .PySys_GetAttr
41+
42+ self .assertIs (sys_getattr ('stdout' ), sys .stdout )
43+ with support .swap_attr (sys , '\U0001f40d ' , 42 ):
44+ self .assertEqual (sys_getattr ('\U0001f40d ' .encode ()), 42 )
45+
46+ with self .assertRaises (AttributeError ):
47+ sys_getattr (b'nonexisting' )
48+ with self .assertRaises (UnicodeDecodeError ):
49+ self .assertIs (sys_getattr (b'\xff ' ), AttributeError )
50+
51+ # CRASHES sys_getattr(NULL)
52+
4053 @support .cpython_only
41- @unittest .skipIf (_testlimitedcapi is None , 'need _testlimitedcapi module' )
4254 def test_sys_setobject (self ):
4355 # Test PySys_SetObject()
4456 setobject = _testlimitedcapi .sys_setobject
@@ -70,7 +82,6 @@ def test_sys_setobject(self):
7082 # CRASHES setobject(NULL, value)
7183
7284 @support .cpython_only
73- @unittest .skipIf (_testlimitedcapi is None , 'need _testlimitedcapi module' )
7485 def test_sys_getxoptions (self ):
7586 # Test PySys_GetXOptions()
7687 getxoptions = _testlimitedcapi .sys_getxoptions
0 commit comments