def _get_code_from_file(run_name, fname):
    # Check for a compiled file first
    with io.open_code(fname) as f:
        code = read_code(f)
    if code is None:
        # That didn't work, so try it as normal source code
        with io.open_code(fname) as f:
            code = compile(f.read(), fname, 'exec')
    return code, fname
Exemplo n.º 2
0
def _get_code_from_file(run_name, fname):
    # Check for a compiled file first
    decoded_path = os.path.abspath(os.fsdecode(fname))
    with io.open_code(decoded_path) as f:
        code = read_code(f)
    if code is None:
        # That didn't work, so try it as normal source code
        with io.open_code(decoded_path) as f:
            code = compile(f.read(), fname, "exec")
    return code, fname
Exemplo n.º 3
0
def initialize_pyc_bytes():
    dirname = os.path.dirname(__file__)
    for filename in os.listdir(dirname):
        if filename.endswith(".py"):
            filepath = os.path.join(dirname, filename)
            with io.open_code(filepath) as f:
                PYC_BYTES.append(marshal.dumps(compile(f.read(), filename, "exec")))
Exemplo n.º 4
0
def _find_module(
    name: str,
    path: Optional[List[str]] = None
) -> Tuple[Optional[IO[bytes]], Optional[str], Tuple[str, str, int]]:
    """An importlib reimplementation of imp.find_module (for our purposes)."""

    # It's necessary to clear the caches for our Finder first, in case any
    # modules are being added/deleted/modified at runtime. In particular,
    # test_modulefinder.py changes file tree contents in a cache-breaking way:

    importlib.machinery.PathFinder.invalidate_caches()

    spec = importlib.machinery.PathFinder.find_spec(name, path)

    if spec is None:
        raise ImportError("No module named {name!r}".format(name=name),
                          name=name)

    # Some special cases:

    if spec.loader is importlib.machinery.BuiltinImporter:
        return None, None, ("", "", _C_BUILTIN)

    if spec.loader is importlib.machinery.FrozenImporter:
        return None, None, ("", "", _PY_FROZEN)

    if spec.loader is None and spec.submodule_search_locations:
        return (
            None,
            os.path.dirname(str(spec.submodule_search_locations)),
            ("", "", _NSP_DIRECTORY),
        )

    file_path = spec.origin

    assert file_path is not None

    if spec.loader.is_package(name):  # type: ignore[union-attr]
        return None, os.path.dirname(file_path), ("", "", _PKG_DIRECTORY)

    if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
        kind = _PY_SOURCE

    elif isinstance(spec.loader, importlib.machinery.ExtensionFileLoader):
        kind = _C_EXTENSION

    elif isinstance(spec.loader, importlib.machinery.SourcelessFileLoader):
        kind = _PY_COMPILED

    else:  # Should never happen.
        return None, None, ("", "", _SEARCH_ERROR)

    file = io.open_code(file_path)
    suffix = os.path.splitext(file_path)[-1]

    return file, file_path, (suffix, "rb", kind)
Exemplo n.º 5
0
Arquivo: site.py Projeto: za/cpython
def addpackage(sitedir, name, known_paths):
    """Process a .pth file within the site-packages directory:
       For each line in the file, either combine it with sitedir to a path
       and add that to known_paths, or execute it if it starts with 'import '.
    """
    if known_paths is None:
        known_paths = _init_pathinfo()
        reset = True
    else:
        reset = False
    fullname = os.path.join(sitedir, name)
    _trace(f"Processing .pth file: {fullname!r}")
    try:
        # locale encoding is not ideal especially on Windows. But we have used
        # it for a long time. setuptools uses the locale encoding too.
        f = io.TextIOWrapper(io.open_code(fullname), encoding="locale")
    except OSError:
        return
    with f:
        for n, line in enumerate(f):
            if line.startswith("#"):
                continue
            if line.strip() == "":
                continue
            try:
                if line.startswith(("import ", "import\t")):
                    exec(line)
                    continue
                line = line.rstrip()
                dir, dircase = makepath(sitedir, line)
                if not dircase in known_paths and os.path.exists(dir):
                    sys.path.append(dir)
                    known_paths.add(dircase)
            except Exception:
                print("Error processing line {:d} of {}:\n".format(
                    n + 1, fullname),
                      file=sys.stderr)
                import traceback
                for record in traceback.format_exception(*sys.exc_info()):
                    for line in record.splitlines():
                        print('  ' + line, file=sys.stderr)
                print("\nRemainder of file ignored", file=sys.stderr)
                break
    if reset:
        known_paths = None
    return known_paths
Exemplo n.º 6
0
def addpackage(sitedir, name, known_paths):
    """Process a .pth file within the site-packages directory:
       For each line in the file, either combine it with sitedir to a path
       and add that to known_paths, or execute it if it starts with 'import '.
    """
    if known_paths is None:
        known_paths = _init_pathinfo()
        reset = True
    else:
        reset = False
    fullname = os.path.join(sitedir, name)
    _trace(f"Processing .pth file: {fullname!r}")
    try:
        f = io.TextIOWrapper(io.open_code(fullname))
    except OSError:
        return
    with f:
        for n, line in enumerate(f):
            if line.startswith("#"):
                continue
            if line.strip() == "":
                continue
            try:
                if line.startswith(("import ", "import\t")):
                    exec(line)
                    continue
                line = line.rstrip()
                dir, dircase = makepath(sitedir, line)
                if not dircase in known_paths and os.path.exists(dir):
                    sys.path.append(dir)
                    known_paths.add(dircase)
            except Exception:
                sys.stderr.write(
                    "Error processing line {:d} of {}:\x0a".format(
                        n + 1, fullname))
                import traceback
                for record in traceback.format_exception(*sys.exc_info()):
                    for line in record.splitlines():
                        sys.stderr.write('  ' + line + '\x0a')
                sys.stderr.write("\nRemainder of file ignored\x0a")
                break
    if reset:
        known_paths = None
    return known_paths
Exemplo n.º 7
0
LOCALE_ENCODING = locale.getlocale()[1]
with open(FILENAME, encoding=LOCALE_ENCODING) as f:
    pass

with open(FILENAME) as f:  # [unspecified-encoding]
    pass

with open(FILENAME, encoding=None) as f:  # [unspecified-encoding]
    pass

LOCALE_ENCODING = None
with open(FILENAME, encoding=LOCALE_ENCODING) as f:  # [unspecified-encoding]
    pass

io.open(FILENAME, "w+b")
io.open_code(FILENAME)
io.open(FILENAME)  # [unspecified-encoding]
io.open(FILENAME, "wt")  # [unspecified-encoding]
io.open(FILENAME, "w+")  # [unspecified-encoding]
io.open(FILENAME, "w", encoding=None)  # [unspecified-encoding]

with io.open(FILENAME, encoding="utf8", errors="surrogateescape") as f:
    pass

LOCALE_ENCODING = locale.getlocale()[1]
with io.open(FILENAME, encoding=LOCALE_ENCODING) as f:
    pass

with io.open(FILENAME) as f:  # [unspecified-encoding]
    pass
Exemplo n.º 8
0
 def load_file(self, pathname):
     dir, name = os.path.split(pathname)
     name, ext = os.path.splitext(name)
     with io.open_code(pathname) as fp:
         stuff = (ext, "rb", _PY_SOURCE)
         self.load_module(name, fp, pathname, stuff)
Exemplo n.º 9
0
 def run_script(self, pathname):
     self.msg(2, "run_script", pathname)
     with io.open_code(pathname) as fp:
         stuff = ("", "rb", _PY_SOURCE)
         self.load_module('__main__', fp, pathname, stuff)
Exemplo n.º 10
0
 def get_data(self, path):
     with io.open_code(str(path)) as fd:
         return tokenize.untokenize(translate_tokens(fd.readline))
Exemplo n.º 11
0
 def update_event(self, inp=-1):
     self.set_output_val(0, io.open_code(self.input(0)))