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

Commit

Permalink
Fix issue initializing matplotlib and error when thread is not found. #…
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz authored and karthiknadig committed Jan 24, 2019
1 parent 40cbf49 commit 92218e7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/ptvsd/_vendored/pydevd/.travis_install_python_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if [ "$PYDEVD_PYTHON_VERSION" = "3.6" ]; then
fi

if [ "$PYDEVD_PYTHON_VERSION" = "3.7" ]; then
conda install --yes pyqt=5
conda install --yes pyqt=5 matplotlib
# Note: track the latest django
pip install "django"
fi
Expand Down
4 changes: 2 additions & 2 deletions src/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,8 +1155,8 @@ def pydevd_find_thread_by_id(thread_id):
return i

# This can happen when a request comes for a thread which was previously removed.
pydevd_log(1, "Could not find thread %s\n" % thread_id)
pydevd_log(1, "Available: %s\n" % [get_thread_id(t) for t in threads] % thread_id)
pydevd_log(1, "Could not find thread %s\n" % (thread_id,))
pydevd_log(1, "Available: %s\n" % ([get_thread_id(t) for t in threads],))
except:
traceback.print_exc()

Expand Down
7 changes: 7 additions & 0 deletions src/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ def save_main_module(file, module_name):
return m


def is_current_thread_main_thread():
if hasattr(threading, 'main_thread'):
return threading.current_thread() is threading.main_thread()
else:
return isinstance(threading.current_thread(), threading._MainThread)


def to_number(x):
if is_string(x):
try:
Expand Down
24 changes: 14 additions & 10 deletions src/ptvsd/_vendored/pydevd/pydevd.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from _pydevd_bundle.pydevd_net_command_factory_xml import NetCommandFactory
from _pydevd_bundle.pydevd_trace_dispatch import (
trace_dispatch as _trace_dispatch, global_cache_skips, global_cache_frame_skips, fix_top_level_trace_and_get_trace_func)
from _pydevd_bundle.pydevd_utils import save_main_module
from _pydevd_bundle.pydevd_utils import save_main_module, is_current_thread_main_thread
from _pydevd_frame_eval.pydevd_frame_eval_main import (
frame_eval_func, dummy_trace_dispatch)
import pydev_ipython # @UnusedImport
Expand Down Expand Up @@ -734,8 +734,9 @@ def check_output_redirect(self):
def init_matplotlib_in_debug_console(self):
# import hook and patches for matplotlib support in debug console
from _pydev_bundle.pydev_import_hook import import_hook_manager
for module in dict_keys(self.mpl_modules_for_patching):
import_hook_manager.add_module_name(module, self.mpl_modules_for_patching.pop(module))
if is_current_thread_main_thread():
for module in dict_keys(self.mpl_modules_for_patching):
import_hook_manager.add_module_name(module, self.mpl_modules_for_patching.pop(module))

def init_matplotlib_support(self):
# prepare debugger for integration with matplotlib GUI event loop
Expand Down Expand Up @@ -763,11 +764,12 @@ def return_control():

def _activate_mpl_if_needed(self):
if len(self.mpl_modules_for_patching) > 0:
for module in dict_keys(self.mpl_modules_for_patching):
if module in sys.modules:
activate_function = self.mpl_modules_for_patching.pop(module)
activate_function()
self.mpl_in_use = True
if is_current_thread_main_thread():
for module in dict_keys(self.mpl_modules_for_patching):
if module in sys.modules:
activate_function = self.mpl_modules_for_patching.pop(module)
activate_function()
self.mpl_in_use = True

def _call_mpl_hook(self):
try:
Expand Down Expand Up @@ -1158,11 +1160,13 @@ def _do_wait_suspend(self, thread, frame, event, arg, suspend_type, from_this_th
info = thread.additional_info

if info.pydev_state == STATE_SUSPEND and not self._finish_debugging_session:
in_main_thread = is_current_thread_main_thread()
# before every stop check if matplotlib modules were imported inside script code
self._activate_mpl_if_needed()
if in_main_thread:
self._activate_mpl_if_needed()

while info.pydev_state == STATE_SUSPEND and not self._finish_debugging_session:
if self.mpl_in_use:
if in_main_thread and self.mpl_in_use:
# call input hooks if only matplotlib is in use
self._call_mpl_hook()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from concurrent.futures import ThreadPoolExecutor
import matplotlib
import matplotlib.pyplot as plt

processed = []


def double(nmbr):
doubled = nmbr * 2 # break here
processed.append(1)
return doubled


with ThreadPoolExecutor(max_workers=2) as pool:
futures = []

for number in range(3):
future = pool.submit(double, number)
futures.append(future)

pool.shutdown()
assert len(processed) == 3
print('TEST SUCEEDED!')
16 changes: 16 additions & 0 deletions src/ptvsd/_vendored/pydevd/tests_python/test_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2828,6 +2828,22 @@ def test_step_over_my_code(case_setup):
writer.write_step_over_my_code(hit.thread_id)
writer.finished_ok = True


def test_matplotlib_activation(case_setup):
try:
import matplotlib
except ImportError:
return

with case_setup.test_file('_debugger_case_matplotlib.py') as writer:
writer.write_add_breakpoint(writer.get_line_index_with_content('break here'))
writer.write_make_initial_run()
for _ in range(3):
hit = writer.wait_for_breakpoint_hit()
writer.write_run_thread(hit.thread_id)

writer.finished_ok = True

# Jython needs some vars to be set locally.
# set JAVA_HOME=c:\bin\jdk1.8.0_172
# set PATH=%PATH%;C:\bin\jython2.7.0\bin
Expand Down
25 changes: 25 additions & 0 deletions src/ptvsd/_vendored/pydevd/tests_python/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import threading
from _pydevd_bundle.pydevd_comm import pydevd_find_thread_by_id


def test_is_main_thread():
from _pydevd_bundle.pydevd_utils import is_current_thread_main_thread
assert is_current_thread_main_thread()

class NonMainThread(threading.Thread):

def run(self):
self.is_main_thread = is_current_thread_main_thread()

non_main_thread = NonMainThread()
non_main_thread.start()
non_main_thread.join()
assert not non_main_thread.is_main_thread


def test_find_thread():
from _pydevd_bundle.pydevd_constants import get_current_thread_id
assert pydevd_find_thread_by_id('123') is None

assert pydevd_find_thread_by_id(
get_current_thread_id(threading.current_thread())) is threading.current_thread()

0 comments on commit 92218e7

Please sign in to comment.