internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: microsoft/ptvsd

Issue:
Process event is not sent back by PTVSD
The process event is sent after PTVSD sends the CMD_RUN command request to pydevd.
In `wrapper.py` the `on_configurationDone` method sends the `CMD_RUN` request and waits for a response. However pydevd doesn't send any response for the `CMD_RUN` request.

Solution - remove the `yield`





Discussion:

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -454,7 +454,7 @@ def on_initialize(self, request, args):
     def on_configurationDone(self, request, args):
         # TODO: docstring
         self.send_response(request)
-        yield self.pydevd_request(pydevd_comm.CMD_RUN, '')
+        self.pydevd_request(pydevd_comm.CMD_RUN, '')
         self.send_process_event(self.start_reason)
 
     def on_disconnect(self, request, args):


Test patch:
Repository: microsoft/ptvsd

Issue:
Breaking on uncaught exceptions does not behave similar to breaking on raised exceptions
```python
def one():
    try:
        raise ArithmeticError()
    except ArithmeticError:
        print("handled")
    raise ArithmeticError()
one()
```

**Scenario 1**
* Ensure `Raised Exceptions` is selected
* Debug and VS Code will break on line 3 and a Red message "PAUSED ON EXCEPTION" is displayed
* User now knows there's an exception

**Message received by VS Code:**
```javascript
{"body": {"reason": "exception", "threadId": 3}, "type": "event", "event": "stopped", "seq": 6}
```

![scenario1](https://user-images.githubusercontent.com/1948812/36325768-c069a882-130d-11e8-897e-9817cd6f9fda.PNG)


**Scenario 2**
* Ensure `Raised Exceptions` is not selected and `Uncaught Exceptions` is selected
* Debug and VS Code will break on line 5 and a message "PAUSED ON PAUSE is displayed
* Not a meaningful break reason (it should indicate it broke due to an exception)

**Message received by VS Code:**
```javascript
{"body": {"reason": "pause", "threadId": 3}, "type": "event", "event": "stopped", "seq": 6}
```

![scenario2](https://user-images.githubusercontent.com/1948812/36325802-cf41213c-130d-11e8-8b4d-16f5ff4842f8.PNG)




Discussion:

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -788,9 +788,13 @@ def on_pydevd_thread_suspend(self, seq, args):
                 pydevd_comm.CMD_STEP_OVER,
                 pydevd_comm.CMD_STEP_RETURN,
         }
+        EXCEPTION_REASONS = {
+            pydevd_comm.CMD_STEP_CAUGHT_EXCEPTION,
+            pydevd_comm.CMD_ADD_EXCEPTION_BREAK
+        }
         if reason in STEP_REASONS:
             reason = 'step'
-        elif reason == pydevd_comm.CMD_STEP_CAUGHT_EXCEPTION:
+        elif reason in EXCEPTION_REASONS:
             reason = 'exception'
         elif reason == pydevd_comm.CMD_SET_BREAK:
             reason = 'breakpoint'


Test patch:
Repository: microsoft/ptvsd

Issue:
Conditional breakpoints don't work due to pydevd issue
`_pydevd_frame_eval.handle_breakpoint` always returns `True`. For conditional breakpoints to work it should return eval result from `_pydevd_bundle.pydevd_frame.handle_breakpoint_condition`. The issue repros in **LiClipse**.


Discussion:

Resolving patch:
diff --git a/ptvsd/pydevd/_pydevd_bundle/pydevd_frame.py b/ptvsd/pydevd/_pydevd_bundle/pydevd_frame.py
--- a/ptvsd/pydevd/_pydevd_bundle/pydevd_frame.py
+++ b/ptvsd/pydevd/_pydevd_bundle/pydevd_frame.py
@@ -36,12 +36,10 @@ def send_signature_call_trace(*args, **kwargs):
 get_file_type = DONT_TRACE.get
 
 
-def handle_breakpoint_condition(py_db, info, breakpoint, new_frame, default_return_value):
+def handle_breakpoint_condition(py_db, info, breakpoint, new_frame):
     condition = breakpoint.condition
     try:
-        val = eval(condition, new_frame.f_globals, new_frame.f_locals)
-        if not val:
-            return default_return_value
+        return eval(condition, new_frame.f_globals, new_frame.f_locals)
 
     except:
         if type(condition) != type(''):
@@ -559,10 +557,9 @@ def trace_dispatch(self, frame, event, arg):
                     if stop or exist_result:
                         condition = breakpoint.condition
                         if condition is not None:
-                            result = handle_breakpoint_condition(main_debugger, info, breakpoint, new_frame,
-                                                                 self.trace_dispatch)
-                            if result is not None:
-                                return result
+                            result = handle_breakpoint_condition(main_debugger, info, breakpoint, new_frame)
+                            if not result:
+                                return self.trace_dispatch
 
                         if breakpoint.expression is not None:
                             handle_breakpoint_expression(breakpoint, info, new_frame)
diff --git a/ptvsd/pydevd/_pydevd_frame_eval/pydevd_frame_tracing.py b/ptvsd/pydevd/_pydevd_frame_eval/pydevd_frame_tracing.py
--- a/ptvsd/pydevd/_pydevd_frame_eval/pydevd_frame_tracing.py
+++ b/ptvsd/pydevd/_pydevd_frame_eval/pydevd_frame_tracing.py
@@ -28,7 +28,9 @@ def handle_breakpoint(frame, thread, global_debugger, breakpoint):
     condition = breakpoint.condition
     info = thread.additional_info
     if condition is not None:
-        handle_breakpoint_condition(global_debugger, info, breakpoint, new_frame, False)
+        result = handle_breakpoint_condition(global_debugger, info, breakpoint, new_frame)
+        if not result:
+            return False
 
     if breakpoint.expression is not None:
         handle_breakpoint_expression(breakpoint, info, new_frame)


Test patch:
Repository: microsoft/ptvsd

Issue:
Pip Installing PTVSD fails 
* Python2.7
* Pip install ptvsd from local source fails with the following error:
```
running build_ext
    building 'ptvsd.pydevd._pydevd_bundle.pydevd_cython' extension
    error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
```


Discussion:
Can you update setuptools and try again to see if it's a problem there? The extension is optional, so failure shouldn't break the rest of the install.

(Alternatively, confirm whether it still installed even though the extension didn't build.)
> (Alternatively, confirm whether it still installed even though the extension didn't build

No, it didn't get installed
It installed for me, I have setuptools 38.5.1 (but of course I have VC++ for Python redist)
```
Processing \\rtvs-devserver1\libs\ptvsd
Installing collected packages: ptvsd
  Running setup.py install for ptvsd: started
    Running setup.py install for ptvsd: finished with status 'done'
Successfully installed ptvsd-4.0.0a1
```


Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -6,6 +6,7 @@
 
 import os
 import os.path
+import sys
 from setuptools import setup, Extension
 
 ROOT = os.path.dirname(os.path.abspath(__file__))
@@ -25,6 +26,18 @@ def get_pydevd_package_data():
         for f in files:
             yield os.path.join(root[len(ptvsd_prefix) + 1:], f)
 
+cmdclass = {}
+
+if sys.version_info[0] == 2:
+    from setuptools.command.build_ext import build_ext
+    class build_optional_ext(build_ext):
+        def build_extension(self, ext):
+            try:
+                super(build_optional_ext, self).build_extension(ext)
+            except:
+                pass
+    cmdclass = { 'build_ext': build_optional_ext }
+
 setup(name='ptvsd',
       version='4.0.0a1',
       description='Visual Studio remote debugging server for Python',
@@ -43,4 +56,5 @@ def get_pydevd_package_data():
       ext_modules=[Extension('ptvsd.pydevd._pydevd_bundle.pydevd_cython',
                              ['ptvsd/pydevd/_pydevd_bundle/pydevd_cython.c'],
                              optional=True)],
+      cmdclass=cmdclass,
       )


Test patch:
Repository: microsoft/ptvsd

Issue:
PVSD should kill process when SIGHUP event is received
@brettcannon @int19h @karthiknadig 

* Debug a program with an input statement
* Program waits for user input
* Kill terminal

**Observed behaviour:**
* Socket connection in debug adapter throws an exception
* The debug adapter (VS Code) reacts to this error and terminates the debug session
* Part of this includes killing the python process and manually sending the `termainted` event

**Proposed solution**
* As the program is launched for debugging we can handle the SIGHUP signal from PTVSD and send the `terminated` event from within and exit the process.

The reason I'm proposing this is because its better for PTVSD to send messages directly to VS Code where possible (avoid need for VS Code adapter to do the dirty work of figuring out when to send terminated events, etc).

@karthiknadig how does VS handle such situations (closing terminal)?
Is this where you too are handling the `error` and `close` events of the socket connection?


Discussion:
Wait, SIGHUP doesn't run atexit handlers?
Also, I think the answer for VS would be that it doesn't get into such situations, because Windows doesn't have SIGHUP. I guess there's still a question of what remote debugging from VS would do, but I don't think we've ever tried that. 
> because Windows doesn't have SIGHUP

Aaah yes, it doesn't have ANY signals.

>Wait, SIGHUP doesn't run atexit handlers? 

Nope, from what I've read atexit handlers don't work for most signals.

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -8,6 +8,7 @@
 import errno
 import os
 import platform
+import signal
 import socket
 import sys
 import threading
@@ -969,6 +970,10 @@ def exit_handler(proc, server_thread):
     if server_thread.is_alive():
         server_thread.join(WAIT_FOR_THREAD_FINISH_TIMEOUT)
 
+def signal_handler(signum, frame, proc):
+    proc.close()
+    sys.exit(0)
+
 def start_server(port):
     """Return a socket to a (new) local pydevd-handling daemon.
 
@@ -981,6 +986,8 @@ def start_server(port):
     client, _ = server.accept()
     pydevd, proc, server_thread = _start(client, server)
     atexit.register(lambda: exit_handler(proc, server_thread))
+    if platform.system() != 'Windows':
+        signal.signal(signal.SIGHUP, lambda signum, frame: signal_handler(signum, frame, proc))
     return pydevd
 
 
@@ -996,6 +1003,8 @@ def start_client(host, port):
     client.connect((host, port))
     pydevd, proc, server_thread = _start(client, None)
     atexit.register(lambda: exit_handler(proc, server_thread))
+    if platform.system() != 'Windows':
+        signal.signal(signal.SIGHUP, lambda signum, frame: signal_handler(signum, frame, proc))
     return pydevd
 
 


Test patch:
Repository: microsoft/ptvsd

Issue:
Threads are never cleared.
ptvsd tracks known threads in.  They are added in the `threads` request handler and the PyDevd `CMD_THREAD_CREATE` event handler.  However, they are never removed.  I'd expect the `CMD_THREAD_KILL` event handler to removed the thread from the list.

Note that I'm not sure this is causing any problems.  However, the fix is simple.


Discussion:

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -838,12 +838,13 @@ def on_pydevd_thread_create(self, seq, args):
     @pydevd_events.handler(pydevd_comm.CMD_THREAD_KILL)
     def on_pydevd_thread_kill(self, seq, args):
         # TODO: docstring
+        pyd_tid = args.strip()
         try:
-            pyd_tid = args.strip()
             vsc_tid = self.thread_map.to_vscode(pyd_tid, autogen=False)
         except KeyError:
             pass
         else:
+            self.thread_map.remove(pyd_tid, vsc_tid)
             self.send_event('thread', reason='exited', threadId=vsc_tid)
 
     @pydevd_events.handler(pydevd_comm.CMD_THREAD_SUSPEND)
@@ -922,7 +923,12 @@ def on_pydevd_send_curr_exception_trace(self, seq, args):
     @pydevd_events.handler(pydevd_comm.CMD_SEND_CURR_EXCEPTION_TRACE_PROCEEDED)
     def on_pydevd_send_curr_exception_trace_proceeded(self, seq, args):
         # TODO: docstring
-        pass
+        pyd_tid = args.strip()
+        with self.active_exceptions_lock:
+            try:
+                del self.active_exceptions[pyd_tid]
+            except KeyError:
+                pass
 
 
 ########################


Test patch:
Repository: microsoft/ptvsd

Issue:
Ability to redirect stdout and stderr (tee program output)
The functionality provided by the `RedirectOutput` debug option in hew older version of PTVSD doesn't seem to be supported by the PyDev debugger.

From what I can tell this functionality is only available in `attach` scenarios. I guess this is because the debugger (PyDev) is always launched inside a terminal.

FYI - We're also missing the corresponding protocol implementation in the wrapper. I tried implementing this, however it was a moot point as there's no way to launch a program with redirection enabled as there's no way to do this in PyDev (I could be mistaken).


Discussion:

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -419,6 +419,7 @@ def __init__(self, socket, pydevd, logfile=None):
         self.loop = futures.EventLoop()
         self.exceptions_mgr = ExceptionsManager(self)
         self.disconnect_request = None
+        self.launch_arguments = None
         self.disconnect_request_event = threading.Event()
         pydevd._vscprocessor = self
         self._closed = False
@@ -539,8 +540,17 @@ def on_initialize(self, request, args):
     def on_configurationDone(self, request, args):
         # TODO: docstring
         self.send_response(request)
+        self.process_launch_arguments()
         self.pydevd_request(pydevd_comm.CMD_RUN, '')
         self.send_process_event(self.start_reason)
+    
+    def process_launch_arguments(self):
+        """Process the launch arguments to configure the debugger"""
+        if self.launch_arguments is None:
+            return
+
+        redirect_stdout = 'STDOUT\tSTDERR' if self.launch_arguments.get('redirectOutput', False) == True else ''
+        self.pydevd_request(pydevd_comm.CMD_REDIRECT_OUTPUT, redirect_stdout)
 
     def on_disconnect(self, request, args):
         # TODO: docstring
@@ -564,6 +574,7 @@ def on_attach(self, request, args):
     def on_launch(self, request, args):
         # TODO: docstring
         self.start_reason = 'launch'
+        self.launch_arguments = request.get('arguments', None)
         self.send_response(request)
 
     def send_process_event(self, start_method):
@@ -981,6 +992,15 @@ def on_pydevd_send_curr_exception_trace_proceeded(self, seq, args):
             except KeyError:
                 pass
 
+    @pydevd_events.handler(pydevd_comm.CMD_WRITE_TO_CONSOLE)
+    def on_pydevd_cmd_write_to_console2(self, seq, args):
+        """Handle console output"""
+        xml = untangle.parse(args).xml
+        ctx = xml.io['ctx']
+        category = 'stdout' if ctx == '1' else 'stderr'
+        content = unquote(xml.io['s'])
+        self.send_event('output', category=category, output=content)
+        
 
 ########################
 # lifecycle


Test patch:
Repository: microsoft/ptvsd

Issue:
Error reading integer
From VS (might not be a ptvsd bug, not sure at this point):
Create new python application
Add new item, python unit test
Set the unit test as startup file
F5

Result:
```
---------------------------
Microsoft Visual Studio
---------------------------
Error reading integer. Unexpected token: Boolean. Path 'exitCode'.
---------------------------
OK   
---------------------------
```


Discussion:
It's parsing 'true' as an integer

```
 1> [DebugAdapter] <--   E (process): {"type": "event", "seq": 5, "event": "process", "body": {"name": "C:\\Users\\huvalo\\source\\repos\\PythonApplication67\\PythonApplication67\\test1.py", "systemProcessId": 16620, "isLocalProcess": true, "startMethod": "launch"}}
 1> [DebugAdapter] <--   E (exited): {"type": "event", "seq": 6, "event": "exited", "body": {"exitCode": true}}
 1> ERROR: Protocol error

JsonReaderException: Error reading integer. Unexpected token: Boolean. Path 'exitCode'.

 1> ERROR: Error reading integer. Unexpected token: Boolean. Path 'exitCode'.
 1> Debug adapter non-protocol standard output: Content-Length: 62

{"type": "event", "seq": 7, "event": "terminated", "body": {}}
```


Resolving patch:
diff --git a/ptvsd/debugger.py b/ptvsd/debugger.py
--- a/ptvsd/debugger.py
+++ b/ptvsd/debugger.py
@@ -32,5 +32,5 @@ def debug(filename, port_num, debug_id, debug_options, run_as):
     try:
         pydevd.main()
     except SystemExit as ex:
-        ptvsd.wrapper.ptvsd_sys_exit_code = ex.code
+        ptvsd.wrapper.ptvsd_sys_exit_code = int(ex.code)
         raise


Test patch:
Repository: microsoft/ptvsd

Issue:
Column number in stackTrace should not be '0'
Currently column number in the handler `on_stackTrace` is hardcoded to `0`.
Based on the protocol, if the value is `0`, then its an indication that the column number is to be ignored:
[/** The column within the line. If source is null or doesn't exist, column is 0 and must be ignored. */](https://github.com/Microsoft/vscode-debugadapter-node/blob/master/protocol/src/debugProtocol.ts#L1223)

Here's the hardcoding:
['line': line, 'column': 0,](https://github.com/Microsoft/ptvsd/blob/master/ptvsd/wrapper.py#L684)

Why does this matter?
* When the value is `0`, the exception widget is not displayed in VS Code
Here's the widget I'm referring to:
<img width="575" alt="screen shot 2018-03-04 at 10 50 14 pm" src="https://user-images.githubusercontent.com/1948812/36961047-6efdbf8a-1ffe-11e8-824c-52be0f95cba1.png">

Column number in stackTrace should not be '0'
Currently column number in the handler `on_stackTrace` is hardcoded to `0`.
Based on the protocol, if the value is `0`, then its an indication that the column number is to be ignored:
[/** The column within the line. If source is null or doesn't exist, column is 0 and must be ignored. */](https://github.com/Microsoft/vscode-debugadapter-node/blob/master/protocol/src/debugProtocol.ts#L1223)

Here's the hardcoding:
['line': line, 'column': 0,](https://github.com/Microsoft/ptvsd/blob/master/ptvsd/wrapper.py#L684)

Why does this matter?
* When the value is `0`, the exception widget is not displayed in VS Code
Here's the widget I'm referring to:
<img width="575" alt="screen shot 2018-03-04 at 10 50 14 pm" src="https://user-images.githubusercontent.com/1948812/36961047-6efdbf8a-1ffe-11e8-824c-52be0f95cba1.png">



Discussion:

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -681,7 +681,7 @@ def on_stackTrace(self, request, args):
                 'id': fid,
                 'name': name,
                 'source': {'path': file},
-                'line': line, 'column': 0,
+                'line': line, 'column': 1,
             })
 
         self.send_response(request,


Test patch:
Repository: microsoft/ptvsd

Issue:
Problems with breakpoints across multiple files.
It appears that we are removing *all* breakpoints when handling `setBreakpoints` requests.  We should only be removing all breakpoints for the specified file.

spam.py:

    from eggs import go_spam
    go_spam()  # set breakpoint here

eggs.py:

    def go_spam():
        print('here')  # set breakpoint here


Discussion:

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -849,9 +849,10 @@ def on_setBreakpoints(self, request, args):
         # First, we must delete all existing breakpoints in that source.
         cmd = pydevd_comm.CMD_REMOVE_BREAK
         for pyd_bpid, vsc_bpid in self.bp_map.pairs():
-            msg = 'python-line\t{}\t{}'.format(path, vsc_bpid)
-            self.pydevd_notify(cmd, msg)
-            self.bp_map.remove(pyd_bpid, vsc_bpid)
+            if pyd_bpid[0] == path:
+                msg = 'python-line\t{}\t{}'.format(path, vsc_bpid)
+                self.pydevd_notify(cmd, msg)
+                self.bp_map.remove(pyd_bpid, vsc_bpid)
 
         cmd = pydevd_comm.CMD_SET_BREAK
         msgfmt = '{}\tpython-line\t{}\t{}\tNone\t{}\tNone'


Test patch:
Repository: microsoft/ptvsd

Issue:
Add watch from QuickWatch window watches wrong expression
Visual Studio:
```
class MyClass:
    a = 12
    text = 'hello'


c_obj = MyClass()
c_obj.a = 15

import os
r = os.urandom(5100111)
print('done')
```

I selected `text` and clicked on Add Watch, and see what it added to Watch window.

![image](https://user-images.githubusercontent.com/1696845/36324589-c8fcc460-1309-11e8-89c1-b38d4ea89446.png)




Discussion:

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -8,6 +8,7 @@
 import errno
 import os
 import platform
+import re
 import signal
 import socket
 import sys
@@ -46,7 +47,6 @@
 WAIT_FOR_DISCONNECT_REQUEST_TIMEOUT = 2
 WAIT_FOR_THREAD_FINISH_TIMEOUT = 1
 
-
 class UnsupportedPyDevdCommandError(Exception):
 
     def __init__(self, cmdid):
@@ -788,9 +788,49 @@ def on_variables(self, request, args):
                 pyd_child = pyd_var + (var_name,)
                 var['variablesReference'] = self.var_map.to_vscode(
                     pyd_child, autogen=True)
+
+            eval_name = self.__get_variable_evaluate_name(pyd_var, var_name)
+            if eval_name:
+                var['evaluateName'] = eval_name
+
             variables.append(var)
         self.send_response(request, variables=variables.get_sorted_variables())
 
+    def __get_variable_evaluate_name(self, pyd_var_parent, var_name):
+        # TODO: docstring
+        eval_name = None
+        if len(pyd_var_parent) > 3:
+            # This means the current variable has a parent i.e, it is not a
+            # FRAME variable. These require evaluateName to work in VS 
+            # watch window
+            var = pyd_var_parent + (var_name,)
+            eval_name = var[3]
+            for s in var[4:]:
+                try:
+                    # Check and get the dictionary key or list index.
+                    # Note: this is best effort, keys that are object references
+                    # will not work
+                    i = self.__get_index_or_key(s)
+                    eval_name += '[{}]'.format(i)
+                except:
+                    eval_name += '.' + s
+
+        return eval_name
+
+    def __get_index_or_key(self, text):
+        # Dictionary resolver in pydevd provides key in '<repr> (<hash>)' format
+        result = re.match(r"(.*)\ \(([0-9]*)\)", text, re.IGNORECASE | re.UNICODE)
+        if result and len(result.groups()) == 2:
+            try:
+                # check if group 2 is a hash
+                int(result.group(2))
+                return result.group(1)
+            except:
+                pass
+        # In the result XML from pydevd list indexes appear 
+        # as names. If the name is a number then it is a index.
+        return int(text)
+
     @async_handler
     def on_setVariable(self, request, args):
         vsc_var = int(args['variablesReference'])


Test patch: