1
1
import _ctypes
2
2
import os
3
3
import platform
4
+ import re
4
5
import sys
5
6
import test .support
6
7
import unittest
@@ -133,24 +134,36 @@ def configure_locales(func):
133
134
@classmethod
134
135
def setUpClass (cls ):
135
136
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 )
136
153
137
154
@configure_locales
138
155
def test_localized_error_from_dll (self ):
139
156
dll = CDLL (self .libc_filename )
140
157
with self .assertRaises (AttributeError ) as cm :
141
158
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 ))
145
160
146
161
@configure_locales
147
162
def test_localized_error_in_dll (self ):
148
163
dll = CDLL (self .libc_filename )
149
164
with self .assertRaises (ValueError ) as cm :
150
165
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 ))
154
167
155
168
@unittest .skipUnless (hasattr (_ctypes , 'dlopen' ),
156
169
'test requires _ctypes.dlopen()' )
@@ -174,9 +187,7 @@ def test_localized_error_dlsym(self):
174
187
dll = _ctypes .dlopen (self .libc_filename )
175
188
with self .assertRaises (OSError ) as cm :
176
189
_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 ))
180
191
181
192
182
193
if __name__ == "__main__" :
0 commit comments