microsoft / ptvsd

Python debugger package for use with Visual Studio and Visual Studio Code.
Other
550 stars 68 forks source link

During debugging instead of context menu on hover the debugger crash #2076

Closed jensmalmgren closed 4 years ago

jensmalmgren commented 4 years ago

Hi I am using Visual Studio Code, full version see below. I am using the extension for Python version 2020.1.58038.

Expected behavior:

Not crashing when about to display a context menu in the debugger.

Actual behavior:

While hovering over a specific variable the debugger stops with an error message (full message below). Curious as I am I looked into the problem and found that there is an uncaught exception in get_names of pydevd_resolver.py.

At line 167 I found this original code:

    def get_names(self, var):
        used___dict__ = False
        try:
            names = dir(var)
        except TypeError:
            names = []
        if not names:
            if hasattr(var, '__dict__'):
                names = dict_keys(var.__dict__)
                used___dict__ = True
        return names, used___dict__

When I change it to the following the debugger stops crashing:

    def get_names(self, var):
        used___dict__ = False
        try:
            names = dir(var)
        except TypeError:
            names = []
        if not names:
            try:
                if hasattr(var, '__dict__'):
                    names = dict_keys(var.__dict__)
                    used___dict__ = True
            except TypeError:
                pass
        return names, used___dict__

This is my version of Visual Studio Code

Version: 1.42.0 (user setup) Commit: ae08d5460b5a45169385ff3fd44208f431992451 Date: 2020-02-06T10:51:34.058Z Electron: 6.1.6 Chrome: 76.0.3809.146 Node.js: 12.4.0 V8: 7.6.303.31-electron.0 OS: Windows_NT x64 10.0.18363

I am creating a program converting DOCX to HTML in Python and I am using python-docx for this. python-docx version 0.8.10

In the code at this line when hovering over child_Element the debugger crasched: for child_Element in doc_DocX.element.body: if (child_Element.tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tbl'): ...

LOGS Here is the full list of the error message:

Traceback (most recent call last):
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 606, in trace_dispatch
    self.do_wait_suspend(thread, frame, event, arg)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 97, in do_wait_suspend
    self._args[0].do_wait_suspend(*args, **kwargs)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1616, in do_wait_suspend
    keep_suspended = self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1644, in _do_wait_suspend
    self.process_internal_commands()
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1350, in process_internal_commands
    int_cmd.do_it(self)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 502, in do_it
    self.method(dbg, *self.args, **self.kwargs)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 675, in internal_get_variable_json
    for child_var in variable.get_children_variables(fmt=fmt):
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_suspended_frames.py", line 111, in get_children_variables
    lst = resolver.get_contents_debug_adapter_protocol(self.value, fmt=fmt)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_resolver.py", line 95, in get_contents_debug_adapter_protocol
    dct, used___dict__ = self._get_py_dictionary(obj)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_resolver.py", line 192, in _get_py_dictionary
    names, used___dict__ = self.get_names(var)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_resolver.py", line 174, in get_names
    if hasattr(var, '__dict__'):
TypeError: descriptor '__dict__' for '_OxmlElementBase' objects doesn't apply to a 'CT_Tbl' object
Traceback (most recent call last):
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Users\jens\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 263, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\jens\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\jens\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\jens\OneDrive\PythonPrograms\ExportDocxToHTML.py", line 376, in <module>
    file.write(ProduceMarkupOfTable(table_Element))
  File "c:\Users\jens\OneDrive\PythonPrograms\ExportDocxToHTML.py", line 376, in <module>
    file.write(ProduceMarkupOfTable(table_Element))
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 606, in trace_dispatch
    self.do_wait_suspend(thread, frame, event, arg)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 97, in do_wait_suspend
    self._args[0].do_wait_suspend(*args, **kwargs)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1616, in do_wait_suspend
    keep_suspended = self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\pydevd.py", line 1644, in _do_wait_suspend
    self.process_internal_commands()
", line 1350, in process_internal_commands
    int_cmd.do_it(self)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 502, in do_it
    self.method(dbg, *self.args, **self.kwargs)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 675, in internal_get_variable_json
    for child_var in variable.get_children_variables(fmt=fmt):
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_suspended_frames.py", line 111, in get_children_variables
    lst = resolver.get_contents_debug_adapter_protocol(self.value, fmt=fmt)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_resolver.py", line 95, in get_contents_debug_adapter_protocol
    dct, used___dict__ = self._get_py_dictionary(obj)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_resolver.py", line 192, in _get_py_dictionary
    names, used___dict__ = self.get_names(var)
  File "c:\Users\jens\.vscode\extensions\ms-python.python-2020.1.58038\pythonFiles\lib\python\old_ptvsd\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_resolver.py", line 174, in get_names
    if hasattr(var, '__dict__'):
TypeError: descriptor '__dict__' for '_OxmlElementBase' objects doesn't apply to a 'CT_Tbl' object

(end of error message)

If this issue was reported at the wrong place or the wrong way please bear with me because I am new to this. Have a nice day.

int19h commented 4 years ago

I never thought that hasattr() must be guarded as well, but per its definition, that's true - it is defined as getattr() + except AttributeError, so if getattr() raises something else, it propagates. @fabioz, I guess all uses of hasattr() in eval/children code has to be guarded.

fabioz commented 4 years ago

I'll take a look at that.