Skip to content

Commit

Permalink
[mypyc] Clean up support for debugging mypyc runtests (#12849)
Browse files Browse the repository at this point in the history
Add a --mypyc-debug option to pytest that compiles a test normally and
then runs the resulting binary in the debugger specified.

The support for debugging runtests was already somewhat there. This just
cleans it up and adds a pytest option to use it.
  • Loading branch information
pranavrajpal authored May 25, 2022
1 parent 87e73fe commit 4024748
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
13 changes: 13 additions & 0 deletions mypy/test/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@

import pytest
from typing import List, Tuple, Set, Optional, Iterator, Any, Dict, NamedTuple, Union, Pattern
from typing_extensions import Final

from mypy.test.config import test_data_prefix, test_temp_dir, PREFIX

root_dir = os.path.normpath(PREFIX)

# Debuggers that we support for debugging mypyc run tests
# implementation of using each of these debuggers is in test_run.py
# TODO: support more debuggers
SUPPORTED_DEBUGGERS: Final = ["gdb", "lldb"]


# File modify/create operation: copy module contents from source_path.
class UpdateFile(NamedTuple):
Expand Down Expand Up @@ -549,6 +555,13 @@ def pytest_addoption(parser: Any) -> None:
help='Set the verbose flag when creating mypy Options')
group.addoption('--mypyc-showc', action='store_true', default=False,
help='Display C code on mypyc test failures')
group.addoption(
"--mypyc-debug",
default=None,
dest="debugger",
choices=SUPPORTED_DEBUGGERS,
help="Run the first mypyc run test with the specified debugger",
)


# This function name is special to pytest. See
Expand Down
22 changes: 11 additions & 11 deletions mypyc/test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ast
import glob
import os.path
import platform
import re
import subprocess
import contextlib
Expand Down Expand Up @@ -272,19 +271,20 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
env = os.environ.copy()
env['MYPYC_RUN_BENCH'] = '1' if bench else '0'

# XXX: This is an ugly hack.
if 'MYPYC_RUN_GDB' in os.environ:
if platform.system() == 'Darwin':
debugger = testcase.config.getoption('debugger')
if debugger:
if debugger == 'lldb':
subprocess.check_call(['lldb', '--', sys.executable, driver_path], env=env)
assert False, ("Test can't pass in lldb mode. (And remember to pass -s to "
"pytest)")
elif platform.system() == 'Linux':
elif debugger == 'gdb':
subprocess.check_call(['gdb', '--args', sys.executable, driver_path], env=env)
assert False, ("Test can't pass in gdb mode. (And remember to pass -s to "
"pytest)")
else:
assert False, 'Unsupported OS'

assert False, 'Unsupported debugger'
# TODO: find a way to automatically disable capturing
# stdin/stdout when in debugging mode
assert False, (
"Test can't pass in debugging mode. "
"(Make sure to pass -s to pytest to interact with the debugger)"
)
proc = subprocess.Popen([sys.executable, driver_path], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, env=env)
output = proc.communicate()[0].decode('utf8')
Expand Down

0 comments on commit 4024748

Please sign in to comment.