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

Issue:
listen(0) in create_server() does not allow client to connect to linux server
## Environment data

- PTVSD version: 4.1.1
- OS and version: linux kernel 4.14.46
- Python version (& distribution if applicable, e.g. Anaconda): 2.7.13, 3.5.3
- Using VS Code or Visual Studio: N/A

## Actual behavior
server on linux never accepts connection, i.e. `client, _ = sock.accept()` in `connect()` in socket.py never returns.  This is due to the `listen(0)` call in `create_server()`.  This was changed from `listen(1)` in 322f6946.  Although `listen(0)` does work correctly on mac, it does not on linux.

## Expected  behavior
the incoming connection to be accepted

## Steps to reproduce:
run:
```python
ptvsd.enable_attach(address=('0.0.0.0', 9876), redirect_output=True)
ptvsd.wait_for_attach()
```
then from the command line, see that `telnet localhost 9876` hangs instead of connecting.  some background history is at https://bugs.python.org/issue8498





Discussion:

Resolving patch:
diff --git a/ptvsd/socket.py b/ptvsd/socket.py
--- a/ptvsd/socket.py
+++ b/ptvsd/socket.py
@@ -75,7 +75,7 @@ def create_server(host, port):
         host = 'localhost'
     server = _new_sock()
     server.bind((host, port))
-    server.listen(0)
+    server.listen(1)
     return server
 
 


Test patch:
Repository: microsoft/ptvsd

Issue:
Debugger doesn't stop at breakpoint
VS, Python 2.7, Just My Code ON or OFF

```
res = raw_input('>')
res = 'hello'
print(res) # < breakpoint on this line
```

F5
Press enter when asked for input in console
Breakpoint on print is not hit



Discussion:
Not seeing a suspended event from `pydevd`. Seems to be specific after `raw_input`.

@fabioz cc
Note: can replicate this using vscode+pydevd (on linux) but not pydev+pydevd (on windows).
This works on 4.1.1 but no longer works on 4.1.2. Same environment as in my original report at the top.
I'll take a look at it.

Resolving patch:
diff --git a/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py b/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py
--- a/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py
+++ b/ptvsd/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py
@@ -1021,7 +1021,7 @@ def make_show_console_message(self, thread_id, frame):
 
     def make_input_requested_message(self, started):
         try:
-            return NetCommand(CMD_INPUT_REQUESTED, 0, started)
+            return NetCommand(CMD_INPUT_REQUESTED, 0, str(started))
         except:
             return self.make_error_message(0, get_exception_traceback_str())
 


Test patch:
diff --git a/ptvsd/_vendored/pydevd/tests_python/test_pydevd_io.py b/ptvsd/_vendored/pydevd/tests_python/test_pydevd_io.py
--- a/ptvsd/_vendored/pydevd/tests_python/test_pydevd_io.py
+++ b/ptvsd/_vendored/pydevd/tests_python/test_pydevd_io.py
@@ -1,4 +1,5 @@
 from _pydevd_bundle.pydevd_io import IORedirector
+from _pydevd_bundle.pydevd_comm import NetCommandFactory
 
 
 def test_io_redirector():
@@ -12,3 +13,42 @@ class MyRedirection2(object):
     # Check that we don't fail creating the IORedirector if the original
     # doesn't have a 'buffer'.
     IORedirector(MyRedirection1(), MyRedirection2(), wrap_buffer=True)
+
+
+class _DummyWriter(object):
+
+    __slots__ = ['commands', 'command_meanings']
+
+    def __init__(self):
+        self.commands = []
+        self.command_meanings = []
+
+    def add_command(self, cmd):
+        from _pydevd_bundle.pydevd_comm import ID_TO_MEANING
+        meaning = ID_TO_MEANING[str(cmd.id)]
+        self.command_meanings.append(meaning)
+        self.commands.append(cmd)
+
+class _DummyPyDb(object):
+    
+    def __init__(self):
+        self.cmd_factory = NetCommandFactory()
+        self.writer = _DummyWriter()
+    
+
+def test_debug_console():
+    from _pydev_bundle.pydev_console_utils import DebugConsoleStdIn
+
+    class OriginalStdin(object):
+
+        def readline(self):
+            return 'read'
+
+    original_stdin = OriginalStdin()
+
+    py_db = _DummyPyDb()
+    debug_console_std_in = DebugConsoleStdIn(py_db, original_stdin)
+    assert debug_console_std_in.readline() == 'read'
+
+    assert py_db.writer.command_meanings == ['CMD_INPUT_REQUESTED', 'CMD_INPUT_REQUESTED']
+
Repository: microsoft/ptvsd

Issue:
test_path_names_uppercase_enabled fails
```
2018-09-18T18:50:20.6706273Z ======================================================================
2018-09-18T18:50:20.6706627Z FAIL [0.001s]: test_path_names_uppercase_enabled (tests.ptvsd.test_pathutils.PathUtilTests)
2018-09-18T18:50:20.6706979Z ----------------------------------------------------------------------
2018-09-18T18:50:20.6707253Z Traceback (most recent call last):
2018-09-18T18:50:20.6707620Z   File "D:\a\1\s\tests\ptvsd\test_pathutils.py", line 78, in test_path_names_uppercase_enabled
2018-09-18T18:50:20.6708077Z     self.assertEqual(result, ACTUAL)
2018-09-18T18:50:20.6708307Z AssertionError: 'D:\\A\\1\\S\\TESTS\\PTVSD\\test_pathutils.py' != 'D:\\a\\1\\s\\tests\\ptvsd\\test_pathutils.py'
2018-09-18T18:50:20.6708746Z - D:\A\1\S\TESTS\PTVSD\test_pathutils.py
2018-09-18T18:50:20.6708945Z + D:\a\1\s\tests\ptvsd\test_pathutils.py
```




Discussion:

Resolving patch:
diff --git a/ptvsd/pathutils.py b/ptvsd/pathutils.py
--- a/ptvsd/pathutils.py
+++ b/ptvsd/pathutils.py
@@ -56,7 +56,8 @@ def _get_actual_filename(self, name):
             dir_names = os.listdir(os.getcwd())
             fragments = list(filter(
                             lambda x: x.upper() == path_fragment1, dir_names))
-        fragments += list(d for d in dirs[1:] if d)
+        fragments += list('{}[{}]'.format(d[:-1], d[-1])
+                          for d in dirs[1:] if d)
         path = glob(sep.join(fragments))
         if not path:
             return name


Test patch:
Repository: microsoft/ptvsd

Issue:
Using sys.exit() with no arguments causes TypeError inside ptvsd
## Environment data

- PTVSD version: 4.1.3
- OS and version: Windows 10
- Python version (& distribution if applicable, e.g. Anaconda): 3.6
- Using VS Code or Visual Studio: VS

## Actual behavior

```
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\ptvsd_launcher.py", line 119, in <module>
    
vspd.debug(filename, port_num, debug_id, debug_options, run_as)

  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\debugger.py", line 37, in debug
    
run(address, filename, *args, **kwargs)

  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_local.py", line 48, in run_file
    
run(argv, addr, **kwargs)

  File "c:\program files (x86)\microsoft visual studio\2017\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_local.py", line 101, in _run
    
daemon.exitcode = int(ex.code)

TypeError
: 
int() argument must be a string, a bytes-like object or a number, not 'NoneType'
```


## Expected  behavior

No error

## Steps to reproduce:
1. Debug this code:

```
sys.exit()
```

https://docs.python.org/3/library/sys.html#sys.exit



Discussion:

Resolving patch:
diff --git a/ptvsd/_local.py b/ptvsd/_local.py
--- a/ptvsd/_local.py
+++ b/ptvsd/_local.py
@@ -139,5 +139,5 @@ def _run(argv, addr, _pydevd=pydevd, _install=install, **kwargs):
     try:
         _pydevd.main()
     except SystemExit as ex:
-        daemon.exitcode = int(ex.code)
+        daemon.exitcode = 0 if ex.code is None else int(ex.code)
         raise


Test patch:
Repository: microsoft/ptvsd

Issue:
pyfile adds an additional new line in every line of code
## Environment data

- PTVSD version: master
- OS and version: any
- Python version (& distribution if applicable, e.g. Anaconda): 3.6
- Using VS Code or Visual Studio:

## Actual behavior

code:
```python
@pyfile
def foo():
    print('one')
    print('two')
```
The file generated by pyfile, `foo.py`:
```python
print('one')

print('two')

```

## Expected  behavior

The file generated by pyfile, `foo.py`:
```python
print('one')
print('two')
```

Having the extra blank lines makes it confusing to set breakpoints in tests.



Discussion:
Can I work on fixing this?


Resolving patch:
diff --git a/pytests/conftest.py b/pytests/conftest.py
--- a/pytests/conftest.py
+++ b/pytests/conftest.py
@@ -84,7 +84,7 @@ def factory(source):
         line = source[0]
         indent = len(line) - len(line.lstrip())
         source = [line[indent:] for line in source]
-        source = '\n'.join(source)
+        source = ''.join(source)
 
         tmpfile = tmpdir.join(name + '.py')
         assert not tmpfile.check()
@@ -104,5 +104,3 @@ def debug_session(request):
         session.wait_for_exit()
     finally:
         session.stop()
-
-


Test patch:
Repository: microsoft/ptvsd

Issue:
Race condition in JsonMessageChannel
```py
    def send_request(self, command, arguments=None):
        d = {'command': command}
        if arguments is not None:
            d['arguments'] = arguments
        seq = self._send_message('request', d)
        request = Request(self, seq)
        with self._lock:
            self._requests[seq] = request
        return request
```
Note that the message is sent before the requests dict is updated. If it goes fast enough, the response handler will receive a response to an "unknown" request.


Discussion:
Caused this test failure.
https://ptvsd.visualstudio.com/ptvsd/_build/results?buildId=862&view=logs

Resolving patch:
diff --git a/ptvsd/messaging.py b/ptvsd/messaging.py
--- a/ptvsd/messaging.py
+++ b/ptvsd/messaging.py
@@ -5,6 +5,7 @@
 from __future__ import print_function, with_statement, absolute_import
 
 import collections
+import contextlib
 import itertools
 import json
 import sys
@@ -222,6 +223,7 @@ def start(self):
     def wait(self):
         self._worker.join()
 
+    @contextlib.contextmanager
     def _send_message(self, type, rest={}):
         with self._lock:
             seq = next(self._seq_iter)
@@ -231,17 +233,16 @@ def _send_message(self, type, rest={}):
         }
         message.update(rest)
         with self._lock:
+            yield seq
             self.stream.write_json(message)
         self.send_callback(self, message)
-        return seq
 
     def send_request(self, command, arguments=None):
         d = {'command': command}
         if arguments is not None:
             d['arguments'] = arguments
-        seq = self._send_message('request', d)
-        request = Request(self, seq)
-        with self._lock:
+        with self._send_message('request', d) as seq:
+            request = Request(self, seq)
             self._requests[seq] = request
         return request
 
@@ -249,7 +250,8 @@ def send_event(self, event, body=None):
         d = {'event': event}
         if body is not None:
             d['body'] = body
-        self._send_message('event', d)
+        with self._send_message('event', d):
+            pass
 
     def send_response(self, request_seq, success, command, error_message=None, body=None):
         d = {
@@ -263,7 +265,8 @@ def send_response(self, request_seq, success, command, error_message=None, body=
         else:
             if error_message is not None:
                 d['message'] = error_message
-        self._send_message('response', d)
+        with self._send_message('response', d):
+            pass
 
     def on_message(self, message):
         self.receive_callback(self, message)


Test patch:
Repository: microsoft/ptvsd

Issue:
Use SIO_LOOPBACK_FAST_PATH
Python 3.6 adds the SIO_LOOPBACK_FAST_PATH flag for sockets, which dramatically increases the bandwidth for loopback connections (I got >4x on my machine with the test code on the linked issue).

We should enable this flag where possible in our debugger. The functionality has existed since Windows 8, but using it on earlier OSs may cause an exception. It is safe to use if the socket eventually gets a connection from another IP.

Moved from https://github.com/Microsoft/PTVS/issues/1350


Discussion:

Resolving patch:
diff --git a/ptvsd/socket.py b/ptvsd/socket.py
--- a/ptvsd/socket.py
+++ b/ptvsd/socket.py
@@ -75,14 +75,14 @@ def create_server(host, port, timeout=None):
     if host is None:
         host = 'localhost'
     server = _new_sock()
-    server.bind((host, port))
-
-    # _, listener_port = server.getsockname()
-    # print('Listening on', listener_port)
-
-    if timeout is not None:
-        server.settimeout(timeout)
-    server.listen(1)
+    try:
+        server.bind((host, port))
+        if timeout is not None:
+            server.settimeout(timeout)
+        server.listen(1)
+    except Exception:
+        server.close()
+        raise
     return server
 
 
@@ -96,6 +96,15 @@ def _new_sock():
                          socket.SOCK_STREAM,
                          socket.IPPROTO_TCP)
     if platform.system() == 'Windows':
+        try:
+            sock.ioctl(socket.SIO_LOOPBACK_FAST_PATH, True)
+        except AttributeError:
+            pass  # Not supported in python 2.* or <3.6
+        except OSError as ose:
+            if ose.winerror == 10045:  # Not supported by OS
+                pass
+            else:
+                raise
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
     else:
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@@ -200,7 +209,6 @@ def close_socket(sock):
     try:
         shut_down(sock)
     except Exception:
-        # TODO: Log errors?
         pass
     sock.close()
 


Test patch:
diff --git a/pytests/test_socket.py b/pytests/test_socket.py
--- a/pytests/test_socket.py
+++ b/pytests/test_socket.py
@@ -3,7 +3,7 @@
 # for license information.
 
 import pytest
-from ptvsd.socket import create_server, shut_down
+from ptvsd.socket import create_server, close_socket
 
 
 class TestSocketServerReuse(object):
@@ -18,7 +18,7 @@ def test_reuse_same_address_port(self):
                 create_server(self.HOST1, self.PORT1)
             assert sock1.getsockname() == (self.HOST1, self.PORT1)
         finally:
-            shut_down(sock1)
+            close_socket(sock1)
 
     def test_reuse_same_port(self):
         try:
@@ -27,5 +27,5 @@ def test_reuse_same_port(self):
             assert sock1.getsockname() == (self.HOST1, self.PORT1)
             assert sock2.getsockname() == (self.HOST2, self.PORT1)
         finally:
-            shut_down(sock1)
-            shut_down(sock2)
+            close_socket(sock1)
+            close_socket(sock2)
Repository: microsoft/ptvsd

Issue:
Debugging Django in VSC with subprocess debugging is very slow
## Environment data

- PTVSD version: Master
- OS and version: Mac
- Python version (& distribution if applicable, e.g. Anaconda): Python 3.7
- Using VS Code or Visual Studio:VSC

## Actual behavior

Debugging django with live reload (subprocess debugging) is very slow.
To the extent, its almost unusable - i thought it wasn't working.

It takes over 30seconds to start django.
Reloading takes around 10 seconds or longer.

## Expected  behavior

Should be fast.

## Steps to reproduce:
Start debugging django with the following config:
```js
        {
            "name": "Django2",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver"
            ],
            "django": true,
            "subProcess": true
        },
```



Discussion:
This repros on windows as well. Starting the new process seems to take a long time. could this be because of #648 ?

Resolving patch:
diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py
--- a/ptvsd/wrapper.py
+++ b/ptvsd/wrapper.py
@@ -1296,7 +1296,7 @@ def close(self):
     def _subprocess_notifier(self):
         while not self.closed:
             try:
-                subprocess_request, subprocess_response = multiproc.subprocess_queue.get(block=False, timeout=0.1)
+                subprocess_request, subprocess_response = multiproc.subprocess_queue.get(timeout=0.1)
             except queue.Empty:
                 continue
 


Test patch:
Repository: microsoft/ptvsd

Issue:
Fetching remote source for a file with a Unicode name fails on 2.7



Discussion:

Resolving patch:
diff --git a/src/ptvsd/pathutils.py b/src/ptvsd/pathutils.py
--- a/src/ptvsd/pathutils.py
+++ b/src/ptvsd/pathutils.py
@@ -2,7 +2,7 @@
 # Licensed under the MIT License. See LICENSE in the project root
 # for license information.
 
-from __future__ import print_function, with_statement, absolute_import
+from __future__ import print_function, with_statement, absolute_import, unicode_literals
 
 from glob import glob
 import os.path
diff --git a/src/ptvsd/wrapper.py b/src/ptvsd/wrapper.py
--- a/src/ptvsd/wrapper.py
+++ b/src/ptvsd/wrapper.py
@@ -200,10 +200,21 @@ def __init__(self, cmdid):
         self.cmdid = cmdid
 
 
-def unquote(s):
-    if s is None:
-        return None
-    return urllib.unquote(s)
+if sys.version_info >= (3,):
+    def unquote(s):
+        return None if s is None else urllib.unquote(s)
+else:
+    # In Python 2, urllib.unquote doesn't handle Unicode strings correctly,
+    # so we need to convert to ASCII first, unquote, and then decode.
+    def unquote(s):
+        if s is None:
+            return None
+        if not isinstance(s, bytes):
+            s = bytes(s)
+        s = urllib.unquote(s)
+        if isinstance(s, bytes):
+            s = s.decode('utf-8')
+        return s
 
 
 def unquote_xml_path(s):
@@ -1605,7 +1616,10 @@ def on_source(self, request, args):
         if source_reference == 0:
             self.send_error_response(request, 'Source unavailable')
         else:
+            if sys.version_info < (3,) and not isinstance(filename, bytes):
+                filename = filename.encode(sys.getfilesystemencoding())
             server_filename = path_to_unicode(pydevd_file_utils.norm_file_to_server(filename))
+
             cmd = pydevd_comm.CMD_LOAD_SOURCE
             _, _, content = yield self.pydevd_request(cmd, server_filename)
             self.send_response(request, content=content)


Test patch:
Repository: microsoft/ptvsd

Issue:
Build wheels for manylinux1



Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -9,13 +9,23 @@
 import subprocess
 import sys
 
-from setuptools import setup
+pure = None
+if '--pure' in sys.argv:
+    pure = True
+    sys.argv.remove('--pure')
+elif '--universal' in sys.argv:
+    pure = True
+elif '--abi' in sys.argv:
+    pure = False
+    sys.argv.remove('--abi')
 
-import versioneer
+
+from setuptools import setup  # noqa
+import versioneer  # noqa
 
 sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src'))
-import ptvsd
-import ptvsd._vendored
+import ptvsd  # noqa
+import ptvsd._vendored  # noqa
 del sys.path[0]
 
 
@@ -23,6 +33,11 @@
 PTVSD_ROOT = os.path.dirname(os.path.abspath(ptvsd.__file__))
 
 
+def get_buildplatform():
+    if '-p' in sys.argv:
+        return sys.argv[sys.argv.index('-p') + 1]
+    return None
+
 def cython_build():
     print('Compiling extension modules (set SKIP_CYTHON_BUILD=1 to omit)')
     subprocess.call([
@@ -46,10 +61,29 @@ def iter_vendored_files():
     long_description = fh.read()
 
 
+try:
+    from wheel.bdist_wheel import bdist_wheel as _bdist_wheel
+
+    class bdist_wheel(_bdist_wheel):
+        def finalize_options(self):
+            _bdist_wheel.finalize_options(self)
+            self.root_is_pure = pure
+
+except ImportError:
+    bdist_wheel = None
+
 if __name__ == '__main__':
     if not os.getenv('SKIP_CYTHON_BUILD'):
         cython_build()
 
+    cmds = versioneer.get_cmdclass()
+    cmds['bdist_wheel'] = bdist_wheel
+
+    extras = {}
+    platforms = get_buildplatform()
+    if platforms is not None:
+        extras['platforms'] = platforms
+
     setup(
         name='ptvsd',
         version=versioneer.get_version(),
@@ -82,5 +116,6 @@ def iter_vendored_files():
             'ptvsd': ['ThirdPartyNotices.txt'],
             'ptvsd._vendored': list(iter_vendored_files()),
         },
-        cmdclass=versioneer.get_cmdclass(),
+        cmdclass=cmds,
+        **extras
     )


Test patch:
Repository: microsoft/ptvsd

Issue:
Launch module from VSC broken in master
## Environment data

- PTVSD version: master
- OS and version: any
- Python version (& distribution if applicable, e.g. Anaconda): any
- Using VS Code or Visual Studio: any

launching modules as specified in this issue is broken. https://github.com/Microsoft/ptvsd/issues/1010#issuecomment-444992883 




Discussion:
It wasn't properly adding '' (i.e. current directory) to `sys.path`, like Python's own `-c` and `-m` do. This wasn't visible in our test runs, since they do not import from current directory, but instead explicitly add the test module directory to path.

Resolving patch:
diff --git a/src/ptvsd/__main__.py b/src/ptvsd/__main__.py
--- a/src/ptvsd/__main__.py
+++ b/src/ptvsd/__main__.py
@@ -256,6 +256,9 @@ def run_module():
     if sys.version_info < (3,) and not isinstance(target, bytes):
         target = target.encode(sys.getfilesystemencoding())
 
+    # 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
@@ -270,6 +273,8 @@ def run_module():
 
 
 def run_code():
+    # Add current directory to path, like Python itself does for -c.
+    sys.path.insert(0, '')
     code = compile(ptvsd.options.target, '<string>', 'exec')
     setup_connection()
     eval(code, {})
@@ -321,7 +326,7 @@ def escape(ch):
 
 def main(argv=sys.argv):
     try:
-        argv[:] = [argv[0]] + list(parse(argv[1:]))
+        sys.argv[:] = [argv[0]] + list(parse(argv[1:]))
     except Exception as ex:
         print(HELP + '\nError: ' + str(ex), file=sys.stderr)
         sys.exit(2)


Test patch: