diff options
author | Meador Inge <meadori@gmail.com> | 2011-11-25 22:37:44 -0600 |
---|---|---|
committer | Meador Inge <meadori@gmail.com> | 2011-11-25 22:37:44 -0600 |
commit | 06f7816558ca216ae66587d2d94a4e96d1405cfe (patch) | |
tree | fb6b17e5f7547ff2081ceb8c14020a0a7acc8511 /Lib/ctypes | |
parent | 4fb23f90eaa04cb91e3ee949fe9c77edbd9526cb (diff) | |
parent | d757d4e44849ce3f66ca61c1f3ba2cffaa2ca7fd (diff) | |
download | cpython-06f7816558ca216ae66587d2d94a4e96d1405cfe.tar.gz |
Issue #13380: add an internal function for resetting the ctypes caches
Diffstat (limited to 'Lib/ctypes')
-rw-r--r-- | Lib/ctypes/test/test_callbacks.py | 2 | ||||
-rw-r--r-- | Lib/ctypes/test/test_memfunctions.py | 2 | ||||
-rw-r--r-- | Lib/ctypes/test/test_parameters.py | 9 | ||||
-rw-r--r-- | Lib/ctypes/test/test_python_api.py | 3 | ||||
-rw-r--r-- | Lib/ctypes/test/test_refcounts.py | 3 | ||||
-rw-r--r-- | Lib/ctypes/test/test_stringptr.py | 2 | ||||
-rw-r--r-- | Lib/ctypes/util.py | 46 |
7 files changed, 30 insertions, 37 deletions
diff --git a/Lib/ctypes/test/test_callbacks.py b/Lib/ctypes/test/test_callbacks.py index c7207eab9d..5600b437c1 100644 --- a/Lib/ctypes/test/test_callbacks.py +++ b/Lib/ctypes/test/test_callbacks.py @@ -140,7 +140,7 @@ class Callbacks(unittest.TestCase): def __del__(self): gc.collect() CFUNCTYPE(None)(lambda x=Nasty(): None) - + try: WINFUNCTYPE diff --git a/Lib/ctypes/test/test_memfunctions.py b/Lib/ctypes/test/test_memfunctions.py index aa2113b885..aec4aaadac 100644 --- a/Lib/ctypes/test/test_memfunctions.py +++ b/Lib/ctypes/test/test_memfunctions.py @@ -1,4 +1,5 @@ import sys +from test import support import unittest from ctypes import * @@ -49,6 +50,7 @@ class MemFunctionsTest(unittest.TestCase): self.assertEqual(cast(a, POINTER(c_byte))[:7:7], [97]) + @support.refcount_test def test_string_at(self): s = string_at(b"foo bar") # XXX The following may be wrong, depending on how Python diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py index e83fd9a6fc..9762fb915d 100644 --- a/Lib/ctypes/test/test_parameters.py +++ b/Lib/ctypes/test/test_parameters.py @@ -73,13 +73,10 @@ class SimpleTypesTestCase(unittest.TestCase): except ImportError: ## print "(No c_wchar_p)" return - s = "123" - if sys.platform == "win32": - self.assertTrue(c_wchar_p.from_param(s)._obj is s) - self.assertRaises(TypeError, c_wchar_p.from_param, 42) - # new in 0.9.1: convert (decode) ascii to unicode - self.assertEqual(c_wchar_p.from_param("123")._obj, "123") + c_wchar_p.from_param("123") + + self.assertRaises(TypeError, c_wchar_p.from_param, 42) self.assertRaises(TypeError, c_wchar_p.from_param, b"123\377") pa = c_wchar_p.from_param(c_wchar_p("123")) diff --git a/Lib/ctypes/test/test_python_api.py b/Lib/ctypes/test/test_python_api.py index 1f4c6039dc..9de3980ed4 100644 --- a/Lib/ctypes/test/test_python_api.py +++ b/Lib/ctypes/test/test_python_api.py @@ -1,5 +1,6 @@ from ctypes import * import unittest, sys +from test import support from ctypes.test import is_resource_enabled ################################################################ @@ -25,6 +26,7 @@ class PythonAPITestCase(unittest.TestCase): self.assertEqual(PyBytes_FromStringAndSize(b"abcdefghi", 3), b"abc") + @support.refcount_test def test_PyString_FromString(self): pythonapi.PyBytes_FromString.restype = py_object pythonapi.PyBytes_FromString.argtypes = (c_char_p,) @@ -56,6 +58,7 @@ class PythonAPITestCase(unittest.TestCase): del res self.assertEqual(grc(42), ref42) + @support.refcount_test def test_PyObj_FromPtr(self): s = "abc def ghi jkl" ref = grc(s) diff --git a/Lib/ctypes/test/test_refcounts.py b/Lib/ctypes/test/test_refcounts.py index 35a81aa40b..5613e7a387 100644 --- a/Lib/ctypes/test/test_refcounts.py +++ b/Lib/ctypes/test/test_refcounts.py @@ -1,4 +1,5 @@ import unittest +from test import support import ctypes import gc @@ -10,6 +11,7 @@ dll = ctypes.CDLL(_ctypes_test.__file__) class RefcountTestCase(unittest.TestCase): + @support.refcount_test def test_1(self): from sys import getrefcount as grc @@ -34,6 +36,7 @@ class RefcountTestCase(unittest.TestCase): self.assertEqual(grc(callback), 2) + @support.refcount_test def test_refcount(self): from sys import getrefcount as grc def func(*args): diff --git a/Lib/ctypes/test/test_stringptr.py b/Lib/ctypes/test/test_stringptr.py index 3d25fa5360..95cd1614c6 100644 --- a/Lib/ctypes/test/test_stringptr.py +++ b/Lib/ctypes/test/test_stringptr.py @@ -1,4 +1,5 @@ import unittest +from test import support from ctypes import * import _ctypes_test @@ -7,6 +8,7 @@ lib = CDLL(_ctypes_test.__file__) class StringPtrTestCase(unittest.TestCase): + @support.refcount_test def test__POINTER_c_char(self): class X(Structure): _fields_ = [("str", POINTER(c_char))] diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 1881e89688..97d0c2f3fa 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -1,5 +1,6 @@ import sys, os import contextlib +import subprocess # find_library(name) returns the pathname of a library, or None. if os.name == "nt": @@ -136,16 +137,12 @@ elif os.name == "posix": rv = f.close() if rv == 10: raise OSError('objdump command not found') - with contextlib.closing(os.popen(cmd)) as f: - data = f.read() - res = re.search(r'\sSONAME\s+([^\s]+)', data) + res = re.search(r'\sSONAME\s+([^\s]+)', dump) if not res: return None return res.group(1) - if (sys.platform.startswith("freebsd") - or sys.platform.startswith("openbsd") - or sys.platform.startswith("dragonfly")): + if sys.platform.startswith(("freebsd", "openbsd", "dragonfly")): def _num_version(libname): # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ] @@ -171,22 +168,6 @@ elif os.name == "posix": else: - def _findLib_ldconfig(name): - # XXX assuming GLIBC's ldconfig (with option -p) - expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) - with contextlib.closing(os.popen('/sbin/ldconfig -p 2>/dev/null')) as f: - data = f.read() - res = re.search(expr, data) - if not res: - # Hm, this works only for libs needed by the python executable. - cmd = 'ldd %s 2>/dev/null' % sys.executable - with contextlib.closing(os.popen(cmd)) as f: - data = f.read() - res = re.search(expr, data) - if not res: - return None - return res.group(0) - def _findSoname_ldconfig(name): import struct if struct.calcsize('l') == 4: @@ -203,14 +184,19 @@ elif os.name == "posix": abi_type = mach_map.get(machine, 'libc6') # XXX assuming GLIBC's ldconfig (with option -p) - expr = r'(\S+)\s+\((%s(?:, OS ABI:[^\)]*)?)\)[^/]*(/[^\(\)\s]*lib%s\.[^\(\)\s]*)' \ - % (abi_type, re.escape(name)) - with contextlib.closing(os.popen('LC_ALL=C LANG=C /sbin/ldconfig -p 2>/dev/null')) as f: - data = f.read() - res = re.search(expr, data) - if not res: - return None - return res.group(1) + regex = os.fsencode( + '\s+(lib%s\.[^\s]+)\s+\(%s' % (re.escape(name), abi_type)) + try: + with subprocess.Popen(['/sbin/ldconfig', '-p'], + stdin=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + stdout=subprocess.PIPE, + env={'LC_ALL': 'C', 'LANG': 'C'}) as p: + res = re.search(regex, p.stdout.read()) + if res: + return os.fsdecode(res.group(1)) + except OSError: + pass def find_library(name): return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name)) |