11import _ctypes
22import os
33import platform
4- import subprocess
54import sys
6- import tempfile
75import test .support
86import unittest
97from ctypes import CDLL , c_int
10- from test . support . os_helper import create_empty_file , temp_dir
8+ from ctypes . util import find_library
119
1210
1311FOO_C = r"""
3230"""
3331
3432
35- def has_gcc ():
36- return subprocess .call (["gcc" , "--version" ],
37- stdout = subprocess .DEVNULL ,
38- stderr = subprocess .DEVNULL ) == 0
39-
40-
4133@unittest .skipUnless (sys .platform .startswith ('linux' ),
4234 'test requires GNU IFUNC support' )
4335class TestNullDlsym (unittest .TestCase ):
@@ -63,7 +55,13 @@ class TestNullDlsym(unittest.TestCase):
6355 """
6456
6557 def test_null_dlsym (self ):
66- if not has_gcc ():
58+ import subprocess
59+ import tempfile
60+
61+ retcode = subprocess .call (["gcc" , "--version" ],
62+ stdout = subprocess .DEVNULL ,
63+ stderr = subprocess .DEVNULL )
64+ if retcode != 0 :
6765 self .skipTest ("gcc is missing" )
6866
6967 pipe_r , pipe_w = os .pipe ()
@@ -132,39 +130,25 @@ def configure_locales(func):
132130
133131 @classmethod
134132 def setUpClass (cls ):
135- if not has_gcc ():
136- raise unittest .SkipTest ("gcc is missing" )
137-
138- def make_empty_lib (self , outdir , so_libname ):
139- srcname = os .path .join (outdir , 'empty.c' )
140- dstname = os .path .join (outdir , so_libname )
141- create_empty_file (srcname )
142- args = ['gcc' , '-shared' , '-o' , dstname , srcname ]
143- p = subprocess .run (args , capture_output = True )
144- p .check_returncode ()
145- return dstname
133+ cls .libc_file = find_library ("c" )
146134
147135 @configure_locales
148136 def test_localized_error_from_dll (self ):
149- with temp_dir () as outdir :
150- dstname = self .make_empty_lib (outdir , 'test_from_dll.so' )
151- dll = CDLL (dstname )
152- with self .assertRaises (AttributeError ) as cm :
153- dll .foo
154- if sys .platform .startswith ('linux' ):
155- # On macOS, the filename is not reported by dlerror().
156- self .assertIn ('test_from_dll.so' , str (cm .exception ))
137+ dll = CDLL (self .libc_file )
138+ with self .assertRaises (AttributeError ) as cm :
139+ dll .foo
140+ if sys .platform .startswith ('linux' ):
141+ # On macOS, the filename is not reported by dlerror().
142+ self .assertIn (self .libc_file , str (cm .exception ))
157143
158144 @configure_locales
159145 def test_localized_error_in_dll (self ):
160- with temp_dir () as outdir :
161- dstname = self .make_empty_lib (outdir , 'test_in_dll.so' )
162- dll = CDLL (dstname )
163- with self .assertRaises (ValueError ) as cm :
164- c_int .in_dll (dll , 'foo' )
165- if sys .platform .startswith ('linux' ):
166- # On macOS, the filename is not reported by dlerror().
167- self .assertIn ('test_in_dll.so' , str (cm .exception ))
146+ dll = CDLL (self .libc_file )
147+ with self .assertRaises (ValueError ) as cm :
148+ c_int .in_dll (dll , 'foo' )
149+ if sys .platform .startswith ('linux' ):
150+ # On macOS, the filename is not reported by dlerror().
151+ self .assertIn (self .libc_file , str (cm .exception ))
168152
169153 @unittest .skipUnless (hasattr (_ctypes , 'dlopen' ),
170154 'test requires _ctypes.dlopen()' )
@@ -185,14 +169,12 @@ def test_localized_error_dlopen(self):
185169 'test requires _ctypes.dlsym()' )
186170 @configure_locales
187171 def test_localized_error_dlsym (self ):
188- with temp_dir () as outdir :
189- dstname = self .make_empty_lib (outdir , 'test_dlsym.so' )
190- dll = _ctypes .dlopen (dstname )
191- with self .assertRaises (OSError ) as cm :
192- _ctypes .dlsym (dll , 'foo' )
193- if sys .platform .startswith ('linux' ):
194- # On macOS, the filename is not reported by dlerror().
195- self .assertIn ('test_dlsym.so' , str (cm .exception ))
172+ dll = _ctypes .dlopen (self .libc_file )
173+ with self .assertRaises (OSError ) as cm :
174+ _ctypes .dlsym (dll , 'foo' )
175+ if sys .platform .startswith ('linux' ):
176+ # On macOS, the filename is not reported by dlerror().
177+ self .assertIn (self .libc_file , str (cm .exception ))
196178
197179
198180if __name__ == "__main__" :
0 commit comments