Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5230327

Browse files
ambvJukkaL
authored andcommitted
Remove API's reliance on overwriting sys.argv (#2908)
* Remove API's reliance on overwriting sys.argv Also, original `sys.stdout` and `sys.stderr` are now recovered also in case of internal exceptions. Test plan: - mypy command-line still works - all tests still pass
1 parent 7f97f6e commit 5230327

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

mypy/api.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
from mypy.main import main
4242

4343

44-
def run(params: List[str]) -> Tuple[str, str, int]:
45-
sys.argv = [''] + params
46-
44+
def run(args: List[str]) -> Tuple[str, str, int]:
4745
old_stdout = sys.stdout
4846
new_stdout = StringIO()
4947
sys.stdout = new_stdout
@@ -53,12 +51,12 @@ def run(params: List[str]) -> Tuple[str, str, int]:
5351
sys.stderr = new_stderr
5452

5553
try:
56-
main(None)
54+
main(None, args=args)
5755
exit_status = 0
5856
except SystemExit as system_exit:
5957
exit_status = system_exit.code
60-
61-
sys.stdout = old_stdout
62-
sys.stderr = old_stderr
58+
finally:
59+
sys.stdout = old_stdout
60+
sys.stderr = old_stderr
6361

6462
return new_stdout.getvalue(), new_stderr.getvalue(), exit_status

mypy/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@
2424
PY_EXTENSIONS = tuple(PYTHON_EXTENSIONS)
2525

2626

27-
def main(script_path: str) -> None:
27+
def main(script_path: str, args: List[str] = None) -> None:
2828
"""Main entry point to the type checker.
2929
3030
Args:
3131
script_path: Path to the 'mypy' script (used for finding data files).
32+
args: Custom command-line arguments. If not given, sys.argv[1:] will
33+
be used.
3234
"""
3335
t0 = time.time()
3436
if script_path:
3537
bin_dir = find_bin_directory(script_path)
3638
else:
3739
bin_dir = None
3840
sys.setrecursionlimit(2 ** 14)
39-
sources, options = process_options(sys.argv[1:])
41+
if args is None:
42+
args = sys.argv[1:]
43+
sources, options = process_options(args)
4044
serious = False
4145
try:
4246
res = type_check_only(sources, bin_dir, options)

0 commit comments

Comments
 (0)