Fix #1272: Launch a SubModule (#1290)

Add sys.path entry for current directory before using runpy/importlib to resolve the module.
This commit is contained in:
Pavel Minaev 2019-04-01 17:16:34 -07:00 committed by Karthik Nadig
parent 079eb3eaff
commit 41d189c197
5 changed files with 29 additions and 4 deletions

View file

@ -8,5 +8,11 @@
"python.linting.enabled": true,
"python.linting.pylintEnabled": false,
"python.envFile": "${workspaceFolder}/.env",
}
"files.exclude": {
"**/*.pyc": true,
"**/__pycache__": true,
".tox": true,
"src/ptvsd.egg-info": true,
}
},
}

View file

@ -213,6 +213,10 @@ def setup_connection():
elif opts.target_kind == 'file':
sys.argv[0] = opts.target
elif opts.target_kind == 'module':
# Add current directory to path, like Python itself does for -m. This must
# be in place before trying to use find_spec below to resolve submodules.
sys.path.insert(0, '')
# We want to do the same thing that run_module() would do here, without
# actually invoking it. On Python 3, it's exposed as a public API, but
# on Python 2, we have to invoke a private function in runpy for this.
@ -280,9 +284,6 @@ def run_module():
ptvsd.log.info('Running module {0}', target)
# Add current directory to path, like Python itself does for -m.
sys.path.insert(0, '')
# Docs say that runpy.run_module is equivalent to -m, but it's not actually
# the case for packages - -m sets __name__ to '__main__', but run_module sets
# it to `pkg.__main__`. This breaks everything that uses the standard pattern

View file

@ -11,6 +11,7 @@ import re
import ptvsd
from tests.helpers import print
from tests.helpers.pathutils import get_test_root
from tests.helpers.pattern import ANY, Regex
from tests.helpers.session import DebugSession
from tests.helpers.timeline import Event
@ -49,6 +50,20 @@ def test_run(pyfile, run_as, start_method):
session.wait_for_exit()
def test_run_submodule():
cwd = get_test_root('testpkgs')
with DebugSession() as session:
session.initialize(
target=('module', 'pkg1.sub'),
start_method='launch',
ignore_unobserved=[Event('continued')],
cwd=cwd,
)
session.start_debugging()
session.wait_for_next(Event('output', ANY.dict_with({'category': 'stdout', 'output': 'three'})))
session.wait_for_exit()
@pytest.mark.parametrize('run_as', ['file', 'module', 'code'])
def test_nodebug(pyfile, run_as):
@pyfile

View file

@ -0,0 +1,3 @@
print('one')
print('two')
print('three')