From a446c68522f016fb2cc110553f1a6308ad98eb78 Mon Sep 17 00:00:00 2001 From: mattip Date: Wed, 2 Dec 2026 19:18:10 +0200 Subject: [PATCH 1/2] remove _imp_emulation --- src/cffi/_imp_emulation.py | 83 ----------------------------------- src/cffi/vengine_cpy.py | 38 ++++++++++------ testing/cffi0/test_verify.py | 4 +- testing/cffi1/test_verify1.py | 4 +- testing/support.py | 2 +- 5 files changed, 30 insertions(+), 101 deletions(-) delete mode 100644 src/cffi/_imp_emulation.py diff --git a/src/cffi/_imp_emulation.py b/src/cffi/_imp_emulation.py deleted file mode 100644 index 65b0bc6b..00000000 --- a/src/cffi/_imp_emulation.py +++ /dev/null @@ -1,83 +0,0 @@ - -try: - # this works on Python < 3.12 - from imp import * - -except ImportError: - # this is a limited emulation for Python >= 3.12. - # Note that this is used only for tests or for the old ffi.verify(). - # This is copied from the source code of Python 3.11. - - from _imp import (acquire_lock, release_lock, - is_builtin, is_frozen) - - from importlib._bootstrap import _load - - from importlib import machinery - import os - import sys - import tokenize - - SEARCH_ERROR = 0 - PY_SOURCE = 1 - PY_COMPILED = 2 - C_EXTENSION = 3 - PY_RESOURCE = 4 - PKG_DIRECTORY = 5 - C_BUILTIN = 6 - PY_FROZEN = 7 - PY_CODERESOURCE = 8 - IMP_HOOK = 9 - - def get_suffixes(): - extensions = [(s, 'rb', C_EXTENSION) - for s in machinery.EXTENSION_SUFFIXES] - source = [(s, 'r', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES] - bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES] - return extensions + source + bytecode - - def find_module(name, path=None): - if not isinstance(name, str): - raise TypeError(f"'name' must be a str, not {type(name)}") - elif not isinstance(path, (type(None), list)): - # Backwards-compatibility - raise RuntimeError("'path' must be None or a list, " - f"not {type(path)}") - - if path is None: - if is_builtin(name): - return None, None, ('', '', C_BUILTIN) - elif is_frozen(name): - return None, None, ('', '', PY_FROZEN) - else: - path = sys.path - - for entry in path: - package_directory = os.path.join(entry, name) - for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]: - package_file_name = '__init__' + suffix - file_path = os.path.join(package_directory, package_file_name) - if os.path.isfile(file_path): - return None, package_directory, ('', '', PKG_DIRECTORY) - for suffix, mode, type_ in get_suffixes(): - file_name = name + suffix - file_path = os.path.join(entry, file_name) - if os.path.isfile(file_path): - break - else: - continue - break # Break out of outer loop when breaking out of inner loop. - else: - raise ImportError(name, name=name) - - encoding = None - if 'b' not in mode: - with open(file_path, 'rb') as file: - encoding = tokenize.detect_encoding(file.readline)[0] - file = open(file_path, mode, encoding=encoding) - return file, file_path, (suffix, mode, type_) - - def load_dynamic(name, path, file=None): - loader = machinery.ExtensionFileLoader(name, path) - spec = machinery.ModuleSpec(name=name, loader=loader, origin=path) - return _load(spec) diff --git a/src/cffi/vengine_cpy.py b/src/cffi/vengine_cpy.py index 0c38e951..afd97bc0 100644 --- a/src/cffi/vengine_cpy.py +++ b/src/cffi/vengine_cpy.py @@ -2,9 +2,24 @@ # DEPRECATED: implementation for ffi.verify() # import sys +import _imp +import importlib.machinery +import importlib.util from . import model from .error import VerificationError -from . import _imp_emulation as imp + + +def _load_dynamic(name, path): + loader = importlib.machinery.ExtensionFileLoader(name, path) + spec = importlib.util.spec_from_file_location(name, path, loader=loader) + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + try: + loader.exec_module(module) + except BaseException: + del sys.modules[name] + raise + return module class VCPythonEngine: @@ -21,18 +36,15 @@ def patch_extension_kwds(self, kwds): pass def find_module(self, module_name, path, so_suffixes): - try: - f, filename, descr = imp.find_module(module_name, path) - except ImportError: + spec = importlib.machinery.PathFinder.find_spec(module_name, path) + if spec is None or not spec.has_location: return None - if f is not None: - f.close() # Note that after a setuptools installation, there are both .py # and .so files with the same basename. The code here relies on - # imp.find_module() locating the .so in priority. - if descr[0] not in so_suffixes: + # the path finder locating the .so in priority. + if not spec.origin.endswith(tuple(so_suffixes)): return None - return filename + return spec.origin def collect_types(self): self._typesdict = {} @@ -132,15 +144,15 @@ def write_source_to_f(self): def load_library(self, flags=None): # XXX review all usages of 'self' here! # import it as a new extension module - imp.acquire_lock() + _imp.acquire_lock() try: if hasattr(sys, "getdlopenflags"): previous_flags = sys.getdlopenflags() try: if hasattr(sys, "setdlopenflags") and flags is not None: sys.setdlopenflags(flags) - module = imp.load_dynamic(self.verifier.get_module_name(), - self.verifier.modulefilename) + module = _load_dynamic(self.verifier.get_module_name(), + self.verifier.modulefilename) except ImportError as e: error = f"importing {self.verifier.modulefilename!r}: {e}" raise VerificationError(error) @@ -148,7 +160,7 @@ def load_library(self, flags=None): if hasattr(sys, "setdlopenflags"): sys.setdlopenflags(previous_flags) finally: - imp.release_lock() + _imp.release_lock() # # call loading_cpy_struct() to get the struct layout inferred by # the C compiler diff --git a/testing/cffi0/test_verify.py b/testing/cffi0/test_verify.py index 40bde4f7..b1a63605 100644 --- a/testing/cffi0/test_verify.py +++ b/testing/cffi0/test_verify.py @@ -1594,10 +1594,10 @@ def test_callback_in_thread(): if sys.platform == 'win32': pytest.skip("pthread only") import os, subprocess - from cffi import _imp_emulation as imp + import cffi arg = os.path.join(os.path.dirname(__file__), 'callback_in_thread.py') g = subprocess.Popen([sys.executable, arg, - os.path.dirname(imp.find_module('cffi')[1])]) + os.path.dirname(os.path.dirname(cffi.__file__))]) result = g.wait() assert result == 0 diff --git a/testing/cffi1/test_verify1.py b/testing/cffi1/test_verify1.py index bdd6c3ee..a188aa46 100644 --- a/testing/cffi1/test_verify1.py +++ b/testing/cffi1/test_verify1.py @@ -1556,10 +1556,10 @@ def test_callback_in_thread(): if sys.platform == 'win32': pytest.skip("pthread only") import os, subprocess - from cffi import _imp_emulation as imp + import cffi arg = os.path.join(os.path.dirname(__file__), 'callback_in_thread.py') g = subprocess.Popen([sys.executable, arg, - os.path.dirname(imp.find_module('cffi')[1])]) + os.path.dirname(os.path.dirname(cffi.__file__))]) result = g.wait() assert result == 0 diff --git a/testing/support.py b/testing/support.py index 96b10cb6..f4868f30 100644 --- a/testing/support.py +++ b/testing/support.py @@ -2,7 +2,7 @@ import site, subprocess from pathlib import Path import pytest -from cffi._imp_emulation import load_dynamic +from cffi.vengine_cpy import _load_dynamic as load_dynamic __all__ = ['load_dynamic'] From 7b3ff7f6917538eaa96adad61cf07b78c7e656cc Mon Sep 17 00:00:00 2001 From: mattip Date: Wed, 2 Dec 2026 22:55:02 +0200 Subject: [PATCH 2/2] retrigger CI