Example #1
0
def _get_code_from_file(run_name,
                        fname=None,
                        hy_src_check=lambda x: x.endswith('.hy')):
    """A patch of `runpy._get_code_from_file` that will also run and cache Hy
    code.
    """
    if fname is None and run_name is not None:
        fname = run_name

    # Check for bytecode first.  (This is what the `runpy` version does!)
    with open(fname, "rb") as f:
        code = pkgutil.read_code(f)

    if code is None:
        if hy_src_check(fname):
            code = _hy_code_from_file(fname, loader_type=HyLoader)
        else:
            # Try normal source
            with open(fname, "rb") as f:
                # This code differs from `runpy`'s only in that we
                # force decoding into UTF-8.
                source = f.read().decode('utf-8')
            code = compile(source, fname, 'exec')

    return (code, fname)
Example #2
0
def _get_code_from_file(run_name, fname=None):
    """A patch of `runpy._get_code_from_file` that will also compile Hy
    code.

    This version will read and cache bytecode for Hy files.  It operates
    normally otherwise.
    """
    if fname is None and run_name is not None:
        fname = run_name

    if fname.endswith('.hy'):
        full_fname = os.path.abspath(fname)
        fname_path, fname_file = os.path.split(full_fname)
        modname = os.path.splitext(fname_file)[0]
        sys.path.insert(0, fname_path)
        try:
            loader = pkgutil.get_loader(modname)
            code = loader.get_code(modname)
        finally:
            sys.path.pop(0)
    else:
        with open(fname, "rb") as f:
            code = pkgutil.read_code(f)
        if code is None:
            with open(fname, "rb") as f:
                source = f.read().decode('utf-8')
            code = compile(source, fname, 'exec')

    return (code, fname) if PY3 else code
Example #3
0
def _get_code_from_file(fname):
    with open(fname, 'rb') as f:
        code = read_code(f)
    if code is None:
        with open(fname, 'rU') as f:
            code = compile(f.read(), fname, 'exec')
    return code
Example #4
0
def _get_code_from_file(fname):
    with open(fname, "rb") as f:
        code = read_code(f)
    if code is None:
        with open(fname, "rU") as f:
            code = compile(f.read(), fname, "exec")
    return code
Example #5
0
def _get_code_from_file(fname):
    with open(fname, 'rb') as f:
        code = read_code(f)
    if code is None:
        with open(fname, 'rU') as f:
            code = compile(f.read(), fname, 'exec')
    return code
Example #6
0
    def _LoadModule(self, name, fp, path, info, deferredImports, parent=None):
        """Load the module, given the information acquired by the finder."""
        suffix, mode, type = info
        if type == imp.PKG_DIRECTORY:
            return self._LoadPackage(name, path, parent, deferredImports)
        module = self._AddModule(name, file_name=path, parent=parent)

        if type == imp.PY_SOURCE:
            logging.debug("Adding module [%s] [PY_SOURCE]", name)
            # Load & compile Python source code
            # if file opened, it already use good encoding; else detect it manually
            if not fp:
                with open(path, "rb") as f:
                    encoding = tokenize.detect_encoding(f.readline)[0]
                fp = open(path, "r", encoding=encoding)
            codeString = fp.read()
            if codeString and codeString[-1] != "\n":
                codeString = codeString + "\n"
            try:
                module.code = compile(codeString,
                                      path,
                                      "exec",
                                      optimize=self.optimizeFlag)
            except SyntaxError:
                raise ImportError("Invalid syntax in %s" % path)

        elif type == imp.PY_COMPILED:
            logging.debug("Adding module [%s] [PY_COMPILED]", name)
            # Load Python bytecode
            if isinstance(fp, bytes):
                fp = io.BytesIO(fp)
                module.source_is_zip_file = True
            module.code = pkgutil.read_code(fp)
            if module.code is None:
                raise ImportError("Bad magic number in %s" % path)

        elif type == imp.C_EXTENSION:
            logging.debug("Adding module [%s] [C_EXTENSION]", name)

        # If there's a custom hook for this module, run it.
        self._RunHook("load", module.name, module)

        if module.code is not None:
            if self.replace_paths:
                topLevelModule = module
                while topLevelModule.parent is not None:
                    topLevelModule = topLevelModule.parent
                module.code = self._ReplacePathsInCode(topLevelModule,
                                                       module.code)

            # Scan the module code for import statements
            self._ScanCode(module.code, module, deferredImports)

            # Verify __package__ in use
            self._ReplacePackageInCode(module)

        module.in_import = False
        return module
Example #7
0
def _get_code_from_file(fname):
    # Check for a compiled file first
    with open(fname, "rb") as f:
        code = read_code(f)
    if code is None:
        # That didn't work, so try it as normal source code
        with open(fname, "rb") as f:
            code = compile(f.read(), fname, 'exec')
    return code
Example #8
0
def _get_code_from_file(fname):
    # Check for a compiled file first
    with open(fname, "rb") as f:
        code = read_code(f)
    if code is None:
        # That didn't work, so try it as normal source code
        with open(fname, "rU") as f:
            code = compile(f.read(), fname, 'exec')
    return code
Example #9
0
def _get_code_from_file(run_name, fname):
    with open(fname, 'rb') as f:
        code = read_code(f)
    if code is None:
        with open(fname, 'rb') as f:
            code = compile(f.read(), fname, 'exec')
            loader = importlib.machinery.SourceFileLoader(run_name, fname)
    else:
        loader = importlib.machinery.SourcelessFileLoader(run_name, fname)
    return (code, loader)
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
Example #11
0
def _get_code_from_file(run_name, fname):
    with open(fname, 'rb') as f:
        code = read_code(f)
    if code is None:
        with open(fname, 'rb') as f:
            code = compile(f.read(), fname, 'exec')
            loader = importlib.machinery.SourceFileLoader(run_name, fname)
    else:
        loader = importlib.machinery.SourcelessFileLoader(run_name, fname)
    return (code, loader)
Example #12
0
def _get_code_from_file(run_name, fname):
    # Check for a compiled file first
    with open(fname, "rb") as f:
        code = read_code(f)
    if code is None:
        # That didn't work, so try it as normal source code
        with open(fname, "rb") as f:
            code = compile(f.read(), fname, 'exec')
            loader = importlib.machinery.SourceFileLoader(run_name, fname)
    else:
        loader = importlib.machinery.SourcelessFileLoader(run_name, fname)
    return code, loader
Example #13
0
def run_path(filename):
    try:
        # runpy.run_path:
        import io
        from pkgutil import get_importer, read_code

        try:
            open_code = io.open_code
        except AttributeError:
            # The function is missing in Python 3.7
            def open_code(path):
                return open(path, "rb")

        importer = get_importer(filename)
        is_NullImporter = False
        if type(importer).__module__ == "imp":
            if type(importer).__name__ == "NullImporter":
                is_NullImporter = True
        if isinstance(importer, type(None)) or is_NullImporter:
            # runpy._get_code_from_file:
            with open_code(filename) as f:
                code = read_code(f)
            if code is None:
                # That didn't work, so try it as normal source code
                with open_code(filename) as f:
                    code = compile(f.read(), filename, "exec")
        else:
            # Run directory.
            code = functools.partial(runpy.run_path,
                                     filename,
                                     run_name="__main__")
    except FileNotFoundError as err:
        show_error(f"Can't open file: {err}")
        sys.exit(2)
    except (OverflowError, SyntaxError, ValueError):
        show_syntax_error(filename)
    else:
        run_code(code, filename)
Example #14
0
def _get_code_from_file(run_name, fname=None,
                        hy_src_check=lambda x: x.endswith('.hy')):
    """A patch of `runpy._get_code_from_file` that will also run and cache Hy
    code.
    """
    if fname is None and run_name is not None:
        fname = run_name

    # Check for bytecode first.  (This is what the `runpy` version does!)
    with open(fname, "rb") as f:
        code = pkgutil.read_code(f)

    if code is None:
        if hy_src_check(fname):
            code = _hy_code_from_file(fname, loader_type=HyLoader)
        else:
            # Try normal source
            with open(fname, "rb") as f:
                # This code differs from `runpy`'s only in that we
                # force decoding into UTF-8.
                source = f.read().decode('utf-8')
            code = compile(source, fname, 'exec')

    return (code, fname) if PY3 else code
Example #15
0
def open_compiled_python_script(session, stream, file_name, argv=None):
    """Execute compiled Python script in a ChimeraX context

    Each script is opened in a uniquely named importable sandbox
    (see timeit example above).  And the current ChimeraX session
    is available as a global variable named **session**.

    Parameters
    ----------
    session : a ChimeraX :py:class:`~chimerax.core.session.Session`
    stream : open data stream
    file_name : how to identify the file
    """
    import pkgutil
    try:
        code = pkgutil.read_code(stream)
        if code is None:
            from .errors import UserError
            raise UserError(
                "Python code was compiled for a different version of Python")
        _exec_python(session, code, argv)
    finally:
        stream.close()
    return [], "executed %s" % file_name
Example #16
0
def execscript(path: str, *args: str) -> None:
    """
    Execute a script.

    The script is executed in the same context as the caller: currently defined
    globals are available to the script, and globals defined by the script are
    added back to the calling context.

    This is most useful for executing scripts from interactive mode. For
    example, you could have a script named ``exe.py``:

    .. code-block:: python3

        \"\"\"Get all tasks executing a given file.\"\"\"

        import sys

        from drgn.helpers.linux.fs import d_path
        from drgn.helpers.linux.pid import find_task

        def task_exe_path(task):
            if task.mm:
                return d_path(task.mm.exe_file.f_path).decode()
            else:
                return None

        tasks = [
            task for task in for_each_task(prog)
            if task_exe_path(task) == sys.argv[1]
        ]

    Then, you could execute it and use the defined variables and functions:

    >>> execscript('exe.py', '/usr/bin/bash')
    >>> tasks[0].pid
    (pid_t)358442
    >>> task_exe_path(find_task(prog, 357954))
    '/usr/bin/vim'

    :param path: File path of the script.
    :param args: Zero or more additional arguments to pass to the script. This
        is a :ref:`variable argument list <python:tut-arbitraryargs>`.
    """
    # This is based on runpy.run_code, which we can't use because we want to
    # update globals even if the script throws an exception.
    saved_module = []
    try:
        saved_module.append(sys.modules["__main__"])
    except KeyError:
        pass
    saved_argv = sys.argv
    try:
        module = types.ModuleType("__main__")
        sys.modules["__main__"] = module
        sys.argv = [path]
        sys.argv.extend(args)

        with _open_code(path) as f:
            code = pkgutil.read_code(f)  # type: ignore[attr-defined]
        if code is None:
            with _open_code(path) as f:
                code = compile(f.read(), path, "exec")
        module.__spec__ = None
        module.__file__ = path
        module.__cached__ = None  # type: ignore[attr-defined]

        caller_globals = sys._getframe(1).f_globals
        caller_special_globals = {
            name: caller_globals[name]
            for name in _special_globals if name in caller_globals
        }
        for name, value in caller_globals.items():
            if name not in _special_globals:
                setattr(module, name, value)

        try:
            exec(code, vars(module))
        finally:
            caller_globals.clear()
            caller_globals.update(caller_special_globals)
            for name, value in vars(module).items():
                if name not in _special_globals:
                    caller_globals[name] = value
    finally:
        sys.argv = saved_argv
        if saved_module:
            sys.modules["__main__"] = saved_module[0]
        else:
            del sys.modules["__main__"]
Example #17
0
def xreload(mod):
    """Reload a module in place, updating classes, methods and functions.

    Args:
      mod: a module object

    Returns:
      The (updated) input object itself.
    """
    # Get the module name, e.g. 'foo.bar.whatever'
    modname = mod.__name__
    # Get the module namespace (dict) early; this is part of the type check
    modns = mod.__dict__
    # Parse it into package name and module name, e.g. 'foo.bar' and 'whatever'
    i = modname.rfind(".")
    if i >= 0:
        pkgname, modname = modname[:i], modname[i + 1:]
    else:
        pkgname = None

    # Compute the search path
    if pkgname:
        # We're not reloading the package, only the module in it
        path = sys.modules[pkgname].__path__  # Search inside the package
    else:
        # Search the top-level module path
        path = None  # Make find_module() uses the default search path

    # Find the module; may raise ImportError
    # TODO: port to new import logic using importlib.util.find_spec and loader
    (stream, filename, (suffix, mode, kind)) = imp.find_module(modname, path)
    # Turn it into a code object
    try:
        # Is it Python source code or byte code read from a file?
        if kind not in (imp.PY_COMPILED, imp.PY_SOURCE):
            # Fall back to built-in reload()
            return importlib.reload(mod)
        if kind == imp.PY_SOURCE:
            source = stream.read()
            code = compile(source, filename, "exec")
        else:
            code = pkgutil.read_code(stream)
    finally:
        if stream:
            stream.close()

    # Execute the code.  We copy the module dict to a temporary; then
    # clear the module dict; then execute the new code in the module
    # dict; then swap things back and around.  This trick (due to
    # Glyph Lefkowitz) ensures that the (readonly) __globals__
    # attribute of methods and functions is set to the correct dict
    # object.
    tmpns = modns.copy()
    modns.clear()
    modns["__name__"] = tmpns["__name__"]
    for attr in ("__file__", "__path__", "__spec__", "__package__"):
        if attr in tmpns:
            modns[attr] = tmpns[attr]

    exec(code, modns)

    # Now we get to the hard part
    oldnames = set(tmpns)
    newnames = set(modns)

    # Update attributes in place
    for name in oldnames & newnames:
        _update(tmpns[name], modns[name])

    return mod
Example #18
0
 def update_event(self, inp=-1):
     self.set_output_val(0, pkgutil.read_code(self.input(0)))
Example #19
0
def execscript(path: str, *args: str) -> None:
    """
    Execute a script.

    The script is executed in the same context as the caller: currently defined
    globals are available to the script, and globals defined by the script are
    added back to the calling context.

    This is most useful for executing scripts from interactive mode. For
    example, you could have a script named ``tasks.py``:

    .. code-block:: python3

        import sys

        \"\"\"
        Get all tasks in a given state.
        \"\"\"

        # From include/linux/sched.h.
        def task_state_index(task):
            task_state = task.state.value_()
            if task_state == 0x402:  # TASK_IDLE
                return 8
            else:
                state = (task_state | task.exit_state.value_()) & 0x7f
                return state.bit_length()

        def task_state_to_char(task):
            return 'RSDTtXZPI'[task_state_index(task)]

        tasks = [
            task for task in for_each_task(prog)
            if task_state_to_char(task) == sys.argv[1]
        ]

    Then, you could execute it and use the defined variables and functions:

    >>> execscript('tasks.py', 'R')
    >>> tasks[0].comm
    (char [16])"python3"
    >>> task_state_to_char(find_task(prog, 1))
    'S'

    :param path: File path of the script.
    :param args: Zero or more additional arguments to pass to the script. This
        is a :ref:`variable argument list <python:tut-arbitraryargs>`.
    """
    # This is based on runpy.run_code, which we can't use because we want to
    # update globals even if the script throws an exception.
    saved_module = []
    try:
        saved_module.append(sys.modules["__main__"])
    except KeyError:
        pass
    saved_argv = sys.argv
    try:
        module = types.ModuleType("__main__")
        sys.modules["__main__"] = module
        sys.argv = [path]
        sys.argv.extend(args)

        with _open_code(path) as f:
            code = pkgutil.read_code(f)
        if code is None:
            with _open_code(path) as f:
                code = compile(f.read(), path, "exec")
        module.__spec__ = None
        module.__file__ = path
        module.__cached__ = None

        caller_globals = sys._getframe(1).f_globals
        caller_special_globals = {
            name: caller_globals[name]
            for name in _special_globals
            if name in caller_globals
        }
        for name, value in caller_globals.items():
            if name not in _special_globals:
                setattr(module, name, value)

        try:
            exec(code, vars(module))
        finally:
            caller_globals.clear()
            caller_globals.update(caller_special_globals)
            for name, value in vars(module).items():
                if name not in _special_globals:
                    caller_globals[name] = value
    finally:
        sys.argv = saved_argv
        if saved_module:
            sys.modules["__main__"] = saved_module[0]
        else:
            del sys.modules["__main__"]
Example #20
0
import sys
from pathlib import Path
from pkgutil import read_code

# Remove the name of the runner
sys.argv.pop(0)

# Read the input file
script = Path(sys.argv[0])

with script.open('rb') as script_file:
    # If we received a source file, compile it (and leave bytecode as is)
    if script.suffix != ".pyc":
        code = compile(script_file.read(), str(script), "exec")
    else:
        code = read_code(script_file)

# Run the code and convert exceptions to error codes
try:
    try:
        exec(code)
    except:
        import traceback
        traceback.print_exc()
        raise
except AssertionError:
    sys.exit(101)
except TypeError:
    sys.exit(102)
except NameError:
    sys.exit(103)