diff --git a/Lib/imp.py b/Lib/imp.py new file mode 100644 --- /dev/null +++ b/Lib/imp.py @@ -0,0 +1,100 @@ +"""This module provides the components needed to build your own __import__ +function. Undocumented functions are obsolete.""" +# XXX Importlib +from _imp import cache_from_source, source_from_cache +# XXX Live here +from _imp import find_module, load_module, reload +# XXX Needs to stay in _imp +from _imp import (lock_held, acquire_lock, release_lock, _fix_co_filename, + get_frozen_object, is_frozen_package, init_builtin, + is_builtin, is_frozen, load_dynamic) +# XXX Can die a horrible death: +from _imp import load_compiled, load_source, load_package +# load_compiled (documented through 3.1) +# load_source (document through 3.1) +# load_package (seemingly never documented) + + +import itertools +import os +import sys + + +# XXX Can live in importlib +def get_magic(): + """Return the magic number for .pyc or .pyo files.""" + return b'\x8a\x0c\r\n' + + +def get_tag(): + """Return the magic tag for .pyc or .pyo files.""" + return 'cpython-{}{}'.format(*sys.version_info[:2]) + + +# XXX Kind of lives here, kind of live in importlib +def get_suffixes(): + """Return a list of (suffix, mode, type) tuples describing the files that + find_module() looks for.""" + ext_suffixes = ['.cpython-{}{}m.so'.format(*sys.version_info[:2]), + '.abi{}.so'.format(sys.version_info[0]), '.so'] + source_suffixes = ['.py'] + compiled_suffixes = ['.pyc' if __debug__ else '.pyo'] + extensions = ((suffix, 'rb', C_EXTENSION) for suffix in ext_suffixes) + source = ((suffix, 'U', PY_SOURCE) for suffix in source_suffixes) + compiled = ((suffix, 'rb', PY_COMPILED) for suffix in compiled_suffixes) + return list(itertools.chain(extensions, source, compiled)) + + +def new_module(name): + """Create a new module. + + Do not enter it in sys.modules. The module name must include the full + package name, if any. + + """ + return type(sys)(name) + + +def cache_from_source(path, debug_override=None): + """Given the path to a .py file, return the path to its .pyc/.pyo file. + + The .py file does not need to exist; this simply returns the path to the + .pyc/.pyo file calculated as if the .py file were imported. The extension + will be .pyc unless __debug__ is not defined, then it will be .pyo. + + If debug_override is not None, then it must be a boolean and is taken as + the value of __debug__ instead. + + """ + pass + + +# XXX Stays here + +class NullImporter: + + """Null importer object""" + + def __init__(self, name): + if len(name) == 0: + raise ImportError('empty pathname') + elif os.path.isdir(name): + raise ImportError('existing directory') + + def find_module(self, fullname, path=None): + """Always return None""" + return None + + +# Constants +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 + diff --git a/Modules/config.c.in b/Modules/config.c.in --- a/Modules/config.c.in +++ b/Modules/config.c.in @@ -39,7 +39,7 @@ {"marshal", PyMarshal_Init}, /* This lives in import.c */ - {"imp", PyInit_imp}, + {"_imp", PyInit_imp}, /* This lives in Python/Python-ast.c */ {"_ast", PyInit__ast}, diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -4258,7 +4258,7 @@ static struct PyModuleDef impmodule = { PyModuleDef_HEAD_INIT, - "imp", + "_imp", doc_imp, 0, imp_methods,