Run a setup script in a somewhat controlled environment. This is adapted from code in distutils and our goal here is that is faster to not need to spin up a python interpreter to run it. We had to fork it because the real run_setup swallows errors and KeyboardInterrupt with no way
(script_name: str, script_args: list[str])
| 111 | |
| 112 | |
| 113 | def run_setup(script_name: str, script_args: list[str]) -> bool: |
| 114 | """Run a setup script in a somewhat controlled environment. |
| 115 | |
| 116 | This is adapted from code in distutils and our goal here is that is |
| 117 | faster to not need to spin up a python interpreter to run it. |
| 118 | |
| 119 | We had to fork it because the real run_setup swallows errors |
| 120 | and KeyboardInterrupt with no way to recover them (!). |
| 121 | The real version has some extra features that we removed since |
| 122 | we weren't using them. |
| 123 | |
| 124 | Returns whether the setup succeeded. |
| 125 | """ |
| 126 | save_argv = sys.argv.copy() |
| 127 | g = {"__file__": script_name} |
| 128 | try: |
| 129 | try: |
| 130 | sys.argv[0] = script_name |
| 131 | sys.argv[1:] = script_args |
| 132 | with open(script_name, "rb") as f: |
| 133 | exec(f.read(), g) |
| 134 | finally: |
| 135 | sys.argv = save_argv |
| 136 | except SystemExit as e: |
| 137 | # distutils converts KeyboardInterrupt into a SystemExit with |
| 138 | # "interrupted" as the argument. Convert it back so that |
| 139 | # pytest will exit instead of just failing the test. |
| 140 | if e.code == "interrupted": |
| 141 | raise KeyboardInterrupt from e |
| 142 | |
| 143 | return e.code == 0 or e.code is None |
| 144 | |
| 145 | return True |
| 146 | |
| 147 | |
| 148 | @contextlib.contextmanager |
no test coverage detected
searching dependent graphs…