Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions Lib/test/test_ctypes/test_dlerror.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ctypes
import os
import platform
import re
import sys
import test.support
import unittest
Expand Down Expand Up @@ -133,24 +134,36 @@ def configure_locales(func):
@classmethod
def setUpClass(cls):
cls.libc_filename = find_library("c")
if cls.libc_filename is None:
raise unittest.SkipTest('cannot find libc')

def check_filename_in_dlerror(self, error_message):
if not sys.platform.startswith('linux'):
# On macOS, the filename is not reported by dlerror().
return

if not re.match(r'(i[3-6]86|x86_64)$', platform.machine()):
# On some architectures, the libc file is detected
# to be 'libc.so.6' but is incorrectly reported by
# dlerror() as libc-X.Y.so.
self.skipTest('do not search for filename '
'in dlerror() error message')

self.assertIn(self.libc_filename, error_message)

@configure_locales
def test_localized_error_from_dll(self):
dll = CDLL(self.libc_filename)
with self.assertRaises(AttributeError) as cm:
dll.this_name_does_not_exist
if sys.platform.startswith('linux'):
# On macOS, the filename is not reported by dlerror().
self.assertIn(self.libc_filename, str(cm.exception))
self.check_filename_in_dlerror(str(cm.exception))

@configure_locales
def test_localized_error_in_dll(self):
dll = CDLL(self.libc_filename)
with self.assertRaises(ValueError) as cm:
c_int.in_dll(dll, 'this_name_does_not_exist')
if sys.platform.startswith('linux'):
# On macOS, the filename is not reported by dlerror().
self.assertIn(self.libc_filename, str(cm.exception))
self.check_filename_in_dlerror(str(cm.exception))

@unittest.skipUnless(hasattr(_ctypes, 'dlopen'),
'test requires _ctypes.dlopen()')
Expand All @@ -174,9 +187,7 @@ def test_localized_error_dlsym(self):
dll = _ctypes.dlopen(self.libc_filename)
with self.assertRaises(OSError) as cm:
_ctypes.dlsym(dll, 'this_name_does_not_exist')
if sys.platform.startswith('linux'):
# On macOS, the filename is not reported by dlerror().
self.assertIn(self.libc_filename, str(cm.exception))
self.check_filename_in_dlerror(str(cm.exception))


if __name__ == "__main__":
Expand Down
Loading