From e63e0f2add0f152d7d0fbdbdc004c4752df60284 Mon Sep 17 00:00:00 2001 From: fabioz Date: Tue, 21 Aug 2018 14:33:36 -0300 Subject: [PATCH] Fix issue launching with --nodebug. Fixes #745 --- ptvsd/_vendored/pydevd/pydevd.py | 42 ++++++++++--------- .../resources/launch/foo/__main__.py | 2 + .../tests_python/resources/launch/foo/bar.py | 0 .../_vendored/pydevd/tests_python/test_run.py | 35 ++++++++++++++++ 4 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 ptvsd/_vendored/pydevd/tests_python/resources/launch/foo/__main__.py create mode 100644 ptvsd/_vendored/pydevd/tests_python/resources/launch/foo/bar.py create mode 100644 ptvsd/_vendored/pydevd/tests_python/test_run.py diff --git a/ptvsd/_vendored/pydevd/pydevd.py b/ptvsd/_vendored/pydevd/pydevd.py index cc5fca2d5..99f8517ec 100644 --- a/ptvsd/_vendored/pydevd/pydevd.py +++ b/ptvsd/_vendored/pydevd/pydevd.py @@ -1018,6 +1018,7 @@ def run(self, file, globals=None, locals=None, is_module=False, set_trace=True): if os.path.isfile(new_target): file = new_target + m = None if globals is None: m = save_main_module(file, 'pydevd') globals = m.__dict__ @@ -1029,27 +1030,28 @@ def run(self, file, globals=None, locals=None, is_module=False, set_trace=True): if locals is None: locals = globals + # Predefined (writable) attributes: __name__ is the module's name; + # __doc__ is the module's documentation string, or None if unavailable; + # __file__ is the pathname of the file from which the module was loaded, + # if it was loaded from a file. The __file__ attribute is not present for + # C modules that are statically linked into the interpreter; for extension modules + # loaded dynamically from a shared library, it is the pathname of the shared library file. + + + # I think this is an ugly hack, bug it works (seems to) for the bug that says that sys.path should be the same in + # debug and run. + if sys.path[0] != '' and m is not None and m.__file__.startswith(sys.path[0]): + # print >> sys.stderr, 'Deleting: ', sys.path[0] + del sys.path[0] + + if not is_module: + # now, the local directory has to be added to the pythonpath + # sys.path.insert(0, os.getcwd()) + # Changed: it's not the local directory, but the directory of the file launched + # The file being run must be in the pythonpath (even if it was not before) + sys.path.insert(0, os.path.split(rPath(file))[0]) + if set_trace: - # Predefined (writable) attributes: __name__ is the module's name; - # __doc__ is the module's documentation string, or None if unavailable; - # __file__ is the pathname of the file from which the module was loaded, - # if it was loaded from a file. The __file__ attribute is not present for - # C modules that are statically linked into the interpreter; for extension modules - # loaded dynamically from a shared library, it is the pathname of the shared library file. - - - # I think this is an ugly hack, bug it works (seems to) for the bug that says that sys.path should be the same in - # debug and run. - if sys.path[0] != '' and m.__file__.startswith(sys.path[0]): - # print >> sys.stderr, 'Deleting: ', sys.path[0] - del sys.path[0] - - if not is_module: - # now, the local directory has to be added to the pythonpath - # sys.path.insert(0, os.getcwd()) - # Changed: it's not the local directory, but the directory of the file launched - # The file being run must be in the pythonpath (even if it was not before) - sys.path.insert(0, os.path.split(rPath(file))[0]) while not self.ready_to_run: time.sleep(0.1) # busy wait until we receive run command diff --git a/ptvsd/_vendored/pydevd/tests_python/resources/launch/foo/__main__.py b/ptvsd/_vendored/pydevd/tests_python/resources/launch/foo/__main__.py new file mode 100644 index 000000000..fd97604e9 --- /dev/null +++ b/ptvsd/_vendored/pydevd/tests_python/resources/launch/foo/__main__.py @@ -0,0 +1,2 @@ +import bar +print('Worked') \ No newline at end of file diff --git a/ptvsd/_vendored/pydevd/tests_python/resources/launch/foo/bar.py b/ptvsd/_vendored/pydevd/tests_python/resources/launch/foo/bar.py new file mode 100644 index 000000000..e69de29bb diff --git a/ptvsd/_vendored/pydevd/tests_python/test_run.py b/ptvsd/_vendored/pydevd/tests_python/test_run.py new file mode 100644 index 000000000..fefe58f28 --- /dev/null +++ b/ptvsd/_vendored/pydevd/tests_python/test_run.py @@ -0,0 +1,35 @@ +pytest_plugins = [ + str('_pytest.pytester'), +] + +def _run_and_check(testdir, path): + result = testdir.runpython(path) + result.stdout.fnmatch_lines([ + 'Worked' + ]) + +def test_run(testdir): + from tests_python import debugger_unittest + from os.path import os + + foo_dir = debugger_unittest._get_debugger_test_file(os.path.join('resources', 'launch', 'foo')) + pydevd_dir = os.path.dirname(os.path.dirname(__file__)) + assert os.path.exists(os.path.join(pydevd_dir, 'pydevd.py')) + + _run_and_check(testdir, testdir.makepyfile(''' +import sys +sys.path.append(%(pydevd_dir)r) +import pydevd +py_db = pydevd.PyDB() +py_db.ready_to_run = True +py_db.run(%(foo_dir)r) +''' % locals())) + + _run_and_check(testdir, testdir.makepyfile(''' +import sys +sys.path.append(%(pydevd_dir)r) +import pydevd +py_db = pydevd.PyDB() +py_db.run(%(foo_dir)r, set_trace=False) +''' % locals())) +