11import _ctypes
2-
32import os
43import platform
54import subprocess
5+ import sys
66import tempfile
77import test .support
88import unittest
99from ctypes import CDLL , c_int
10+ from test .support .os_helper import create_empty_file
1011
1112
1213FOO_C = r"""
@@ -116,7 +117,7 @@ def test_null_dlsym(self):
116117 _ctypes .dlsym (L , "foo" )
117118
118119
119- class TestLinuxLocalization (unittest .TestCase ):
120+ class TestLocalization (unittest .TestCase ):
120121
121122 @staticmethod
122123 def configure_locales (func ):
@@ -132,11 +133,10 @@ def setUpClass(cls):
132133 if not has_gcc ():
133134 raise unittest .SkipTest ("gcc is missing" )
134135
135- def make_libfoo (self , outdir , source , so_libname ):
136- srcname = os .path .join (outdir , 'source .c' )
136+ def make_libfoo (self , outdir , so_libname ):
137+ srcname = os .path .join (outdir , 'empty .c' )
137138 dstname = os .path .join (outdir , so_libname )
138- with open (srcname , 'w' ) as f :
139- f .write (source )
139+ create_empty_file (srcname )
140140 args = ['gcc' , '-fPIC' , '-shared' , '-o' , dstname , srcname ]
141141 p = subprocess .run (args , capture_output = True )
142142 p .check_returncode ()
@@ -145,17 +145,21 @@ def make_libfoo(self, outdir, source, so_libname):
145145 @configure_locales
146146 def test_localized_error_from_dll (self ):
147147 with tempfile .TemporaryDirectory () as outdir :
148- dstname = self .make_libfoo (outdir , 'int x = 0;' , 'test_in_dll .so' )
148+ dstname = self .make_libfoo (outdir , 'test_from_dll .so' )
149149 dll = CDLL (dstname )
150- with self .assertRaisesRegex (AttributeError , r'test_in_dll\.so:.+' ):
150+ # on macOS, the filename is not reported by dlerror()
151+ pat = '.+' if sys .platform == 'darwin' else r'test_from_dll\.so'
152+ with self .assertRaisesRegex (AttributeError , pat ):
151153 dll .foo
152154
153155 @configure_locales
154156 def test_localized_error_in_dll (self ):
155157 with tempfile .TemporaryDirectory () as outdir :
156- dstname = self .make_libfoo (outdir , 'int x = 0;' , ' test_in_dll.so' )
158+ dstname = self .make_libfoo (outdir , 'test_in_dll.so' )
157159 dll = CDLL (dstname )
158- with self .assertRaisesRegex (ValueError , r'test_in_dll\.so:.+' ):
160+ # on macOS, the filename is not reported by dlerror()
161+ pat = '.+' if sys .platform == 'darwin' else r'test_in_dll\.so'
162+ with self .assertRaisesRegex (ValueError , pat ):
159163 c_int .in_dll (dll , 'foo' )
160164
161165 @unittest .skipUnless (hasattr (_ctypes , 'dlopen' ),
@@ -167,7 +171,7 @@ def test_localized_error_dlopen(self):
167171 # but we are only interested in avoiding a UnicodeDecodeError
168172 # when reporting the dlerror() error message which contains
169173 # the localized filename.
170- filename_pattern = r'missing.*?\.so:.+ '
174+ filename_pattern = r'missing.*?\.so'
171175 with self .assertRaisesRegex (OSError , filename_pattern ):
172176 _ctypes .dlopen (missing_filename , 2 )
173177
@@ -178,9 +182,11 @@ def test_localized_error_dlopen(self):
178182 @configure_locales
179183 def test_localized_error_dlsym (self ):
180184 with tempfile .TemporaryDirectory () as outdir :
181- dstname = self .make_libfoo (outdir , 'int x = 0;' , ' test_dlsym.so' )
185+ dstname = self .make_libfoo (outdir , 'test_dlsym.so' )
182186 dll = _ctypes .dlopen (dstname )
183- with self .assertRaisesRegex (OSError , r'test_dlsym\.so:.+' ):
187+ # on macOS, the filename is not reported by dlerror()
188+ pat = '.+' if sys .platform == 'darwin' else r'test_dlsym\.so'
189+ with self .assertRaisesRegex (OSError , pat ):
184190 _ctypes .dlsym (dll , 'foo' )
185191
186192
0 commit comments