Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Fix #1272: Launch a SubModule (#1290)
Browse files Browse the repository at this point in the history
Add sys.path entry for current directory before using runpy/importlib to resolve the module.
  • Loading branch information
int19h authored and karthiknadig committed Apr 2, 2019
1 parent 079eb3e commit 41d189c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
8 changes: 7 additions & 1 deletion ptvsd.code-workspace
Expand Up @@ -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,
}
},
}
7 changes: 4 additions & 3 deletions src/ptvsd/__main__.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/func/test_run.py
Expand Up @@ -11,6 +11,7 @@
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
Expand Down Expand Up @@ -49,6 +50,20 @@ def code_to_debug():
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
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions tests/func/testfiles/testpkgs/pkg1/sub/__main__.py
@@ -0,0 +1,3 @@
print('one')
print('two')
print('three')

0 comments on commit 41d189c

Please sign in to comment.