Run script/command inside the defined interpreter and retrieve the JSON encoded output :param command: command/path to script to execute :param interpreter: command/path to the Python interpreter used for execution :param stdin: stdin to pass to the execute program :return: jso
(*, command: List[str], interpreter: str, stdin=None, native_callback: Optional[Callable]=None)
| 67 | |
| 68 | |
| 69 | def execute_interpreter(*, command: List[str], interpreter: str, stdin=None, native_callback: Optional[Callable]=None): |
| 70 | """ |
| 71 | Run script/command inside the defined interpreter and retrieve the JSON encoded output |
| 72 | |
| 73 | :param command: command/path to script to execute |
| 74 | :param interpreter: command/path to the Python interpreter used for execution |
| 75 | :param stdin: stdin to pass to the execute program |
| 76 | :return: json decoded stdout |
| 77 | """ |
| 78 | if interpreter == "native": |
| 79 | try: |
| 80 | return native_callback(command) |
| 81 | except Exception as exc: |
| 82 | raise PythonExecutorError(f"Native interpreter failed") from exc |
| 83 | |
| 84 | full_args = [interpreter] + command |
| 85 | proc = subprocess.run( |
| 86 | args=full_args, |
| 87 | stdout=subprocess.PIPE, |
| 88 | stderr=subprocess.PIPE, |
| 89 | shell=False, |
| 90 | input=stdin |
| 91 | ) |
| 92 | if proc.returncode == 0: |
| 93 | payload = None |
| 94 | try: |
| 95 | payload = proc.stdout |
| 96 | return loads(payload) |
| 97 | except JSONDecodeError: |
| 98 | LOGGER.exception(f"Error decoding interpreter JSON: {repr(payload)}") |
| 99 | new_exception = PythonExecutorError("Error decoding python interpreter JSON") |
| 100 | new_exception.stdout = payload |
| 101 | new_exception.stderr = proc.stderr |
| 102 | raise new_exception |
| 103 | else: |
| 104 | exc = PythonExecutorError(f"Interpreter exited with non-zero status code: {proc.returncode}") |
| 105 | exc.stderr = proc.stderr |
| 106 | exc.stdout = proc.stdout |
| 107 | raise exc |
| 108 | |
| 109 | |
| 110 | def get_native_source_code(command): |
no test coverage detected