11import _ctypes
22import os
33import platform
4+ import re
45import sys
56import test .support
67import unittest
@@ -133,24 +134,36 @@ def configure_locales(func):
133134 @classmethod
134135 def setUpClass (cls ):
135136 cls .libc_filename = find_library ("c" )
137+ if cls .libc_filename is None :
138+ raise unittest .SkipTest ('cannot find libc' )
139+
140+ def check_filename_in_dlerror (self , error_message ):
141+ if not sys .platform .startswith ('linux' ):
142+ # On macOS, the filename is not reported by dlerror().
143+ return
144+
145+ if not re .match (r'(i[3-6]86|x86_64)$' , platform .machine ()):
146+ # On some architectures, the libc file is detected
147+ # to be 'libc.so.6' but is incorrectly reported by
148+ # dlerror() as libc-X.Y.so.
149+ self .skipTest ('do not search for filename '
150+ 'in dlerror() error message' )
151+
152+ self .assertIn (self .libc_filename , error_message )
136153
137154 @configure_locales
138155 def test_localized_error_from_dll (self ):
139156 dll = CDLL (self .libc_filename )
140157 with self .assertRaises (AttributeError ) as cm :
141158 dll .this_name_does_not_exist
142- if sys .platform .startswith ('linux' ):
143- # On macOS, the filename is not reported by dlerror().
144- self .assertIn (self .libc_filename , str (cm .exception ))
159+ self .check_filename_in_dlerror (str (cm .exception ))
145160
146161 @configure_locales
147162 def test_localized_error_in_dll (self ):
148163 dll = CDLL (self .libc_filename )
149164 with self .assertRaises (ValueError ) as cm :
150165 c_int .in_dll (dll , 'this_name_does_not_exist' )
151- if sys .platform .startswith ('linux' ):
152- # On macOS, the filename is not reported by dlerror().
153- self .assertIn (self .libc_filename , str (cm .exception ))
166+ self .check_filename_in_dlerror (str (cm .exception ))
154167
155168 @unittest .skipUnless (hasattr (_ctypes , 'dlopen' ),
156169 'test requires _ctypes.dlopen()' )
@@ -174,9 +187,7 @@ def test_localized_error_dlsym(self):
174187 dll = _ctypes .dlopen (self .libc_filename )
175188 with self .assertRaises (OSError ) as cm :
176189 _ctypes .dlsym (dll , 'this_name_does_not_exist' )
177- if sys .platform .startswith ('linux' ):
178- # On macOS, the filename is not reported by dlerror().
179- self .assertIn (self .libc_filename , str (cm .exception ))
190+ self .check_filename_in_dlerror (str (cm .exception ))
180191
181192
182193if __name__ == "__main__" :
0 commit comments