File imp.py of Package failed_ibus-cangjie

# Compatibility shim for Python versions that no longer provide the
# deprecated built-in 'imp' module (Python 3.10+). Some upstream
# Makefile.install / automake-generated snippets still do:
#   import sys, os, py_compile, imp
# which fails on newer Python because 'imp' was removed.
#
# This tiny shim provides a minimal subset of imp API that is typically
# used by installation helpers:
#   - find_module(name, path=None)
#   - load_module(name, file, pathname, description)
#
# It delegates to importlib under the hood. It lives in the package
# source root so that "import imp" executed from the source dir will
# pick up this module on sys.path (current directory is in sys.path).
#
# Implement only what is needed to satisfy common usage patterns in
# generated install snippets. Keep the implementation intentionally
# small and conservative.

from __future__ import annotations
import importlib.util
import importlib.machinery
import importlib
import sys
import types
from typing import Optional, Tuple, Any

def find_module(name: str, path: Optional[object]=None) -> Tuple[Optional[Any], Optional[str], Optional[Any]]:
    """
    Emulate imp.find_module by using importlib to locate a module spec.
    Returns a tuple similar to the old imp API: (file, pathname, description).
    We return (None, origin, None) when a spec is found, and raise ImportError
    when not found (matching the old behavior).
    """
    # Use importlib.util.find_spec which accepts a module name and an optional path.
    spec = importlib.util.find_spec(name, package=None)
    if spec is None or spec.origin is None:
        raise ImportError(name)
    return None, spec.origin, None

def load_module(name: str, file: Optional[Any], pathname: str, description: Optional[Any]) -> types.ModuleType:
    """
    Emulate imp.load_module by loading a module given a filesystem pathname.
    This attempts to load the module from the provided pathname. If a module
    with the given name is already present in sys.modules it is returned.
    """
    if name in sys.modules:
        return sys.modules[name]

    # Create a spec from the given file location; if that fails, try to use
    # importlib.import_module as a fallback (in case pathname is not a file).
    try:
        spec = importlib.util.spec_from_file_location(name, pathname)
        if spec is None or spec.loader is None:
            # Fallback: try normal import
            module = importlib.import_module(name)
            return module
        module = importlib.util.module_from_spec(spec)
        # Insert into sys.modules before executing to handle recursive imports.
        sys.modules[name] = module
        spec.loader.exec_module(module)
        return module
    except Exception:
        # Clean up on failure
        if name in sys.modules:
            del sys.modules[name]
        raise

# Provide a minimal new_module to mimic imp.new_module used rarely.
def new_module(name: str) -> types.ModuleType:
    module = types.ModuleType(name)
    return module

# Provide a minimal fallback for get_suffixes (not always used, but harmless).
def get_suffixes():
    """
    Return a list of tuples (suffix, mode, type) similar to imp.get_suffixes.
    We derive this from importlib.machinery.
    """
    suffixes = []
    for suffix in importlib.machinery.SOURCE_SUFFIXES:
        suffixes.append((suffix, 'r', importlib.machinery.SOURCE))
    for suffix in importlib.machinery.BYTECODE_SUFFIXES:
        suffixes.append((suffix, 'rb', importlib.machinery.BYTECODE))
    for suffix in importlib.machinery.EXTENSION_SUFFIXES:
        suffixes.append((suffix, 'rb', importlib.machinery.EXTENSION))
    return suffixes

# Expose names commonly referenced by old code.
__all__ = ["find_module", "load_module", "new_module", "get_suffixes"]
openSUSE Build Service is sponsored by