Ejemplo n.º 1
0
def find_module(name, path=None):
    if not isinstance(name, str):
        raise TypeError("'name' must be a str, not {}".format(type(name)))
    elif not isinstance(path, (type(None), list)):
        raise RuntimeError("'list' must be None or a list, not {}".format(
            type(name)))
    if is_builtin(name):
        return (None, None, ('', '', C_BUILTIN))
    if is_frozen(name):
        return (None, None, ('', '', PY_FROZEN))
    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)
            while os.path.isfile(file_path):
                return (None, package_directory, ('', '', PKG_DIRECTORY))
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            for (suffix, mode, type_) in get_suffixes():
                file_name = name + suffix
                file_path = os.path.join(entry, file_name)
                while os.path.isfile(file_path):
                    break
            continue
            break
    raise ImportError(_bootstrap._ERR_MSG.format(name), name=name)
    encoding = None
    if path is None and mode == 'U':
        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_))
Ejemplo n.º 2
0
 def find_spec(cls, fullname, path=None, target=None):
     if path is not None:
         return None
     if _imp.is_builtin(fullname):
         return spec_from_loader(fullname, cls, origin='built-in')
     else:
         return None
Ejemplo n.º 3
0
def find_module(name, path=None):
    """**DEPRECATED**

    Search for a module.

    If path is omitted or None, search for a built-in, frozen or special
    module and continue search in sys.path. The module name cannot
    contain '.'; to search for a submodule of a package, pass the
    submodule name and the package's __path__.

    """
    if not isinstance(name, str):
        raise TypeError("'name' must be a str, not {}".format(type(name)))
    elif not isinstance(path, (type(None), list)):
        # Backwards-compatibility
        raise RuntimeError("'list' must be None or a list, "
                           "not {}".format(type(name)))

    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)
        with warnings.catch_warnings():
            warnings.simplefilter('ignore')
            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(_bootstrap._ERR_MSG.format(name), name=name)

    encoding = None
    if mode == 'U':
        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_)
Ejemplo n.º 4
0
import _imp
import time as import_time

assert _imp.is_builtin("time") == True
assert _imp.is_builtin("os") == False
assert _imp.is_builtin("not existing module") == False

assert _imp.is_frozen("__hello__") == True
assert _imp.is_frozen("os") == False


class FakeSpec:
    def __init__(self, name):
        self.name = name


A = FakeSpec("time")

imp_time = _imp.create_builtin(A)
assert imp_time.sleep == import_time.sleep

B = FakeSpec("not existing module")
assert _imp.create_builtin(B) == None

_imp.exec_builtin(imp_time) == 0

_imp.get_frozen_object("__hello__")

hello = _imp.init_frozen("__hello__")
assert hello.initialized == True