Merge branch 'master' into python3
[pdm.git] / pdm / sshsock.py
index dd53e6a..7ee26e8 100644 (file)
@@ -9,31 +9,31 @@ class sshsocket(object):
         if port is not None:
             args += ["-p", str(int(port))]
         args += [host]
-        args += ["python", "-m", "pdm.sshsock", path]
+        args += ["python3", "-m", "pdm.sshsock", path]
         self.proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
         fcntl.fcntl(self.proc.stdout, fcntl.F_SETFL, fcntl.fcntl(self.proc.stdout, fcntl.F_GETFL) | os.O_NONBLOCK)
         head = self.recv(5)
-        if head != "SSOCK":
+        if head != b"SSOCK":
             raise socket.error("unexpected reply from %s: %r" % (host, head))
         head = self.recv(1)
-        if head == "+":
-            buf = ""
+        if head == b"+":
+            buf = b""
             while True:
                 r = self.recv(1)
-                if r == "":
+                if r == b"":
                     raise socket.error("unexpected EOF in SSH socket stream")
-                elif r == "\n":
+                elif r == b"\n":
                     break
                 buf += r
             return
-        elif head == "-":
-            buf = ""
+        elif head == b"-":
+            buf = b""
             while True:
                 r = self.recv(1)
-                if r in ("\n", ""):
+                if r in {b"\n", b""}:
                     break
                 buf += r
-            raise socket.error(buf)
+            raise socket.error(buf.decode("utf-8"))
         else:
             raise socket.error("unexpected reply from %s: %r" % (host, head))
 
@@ -60,7 +60,7 @@ class sshsocket(object):
         self.close()
 
 def cli():
-    fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK)
+    fcntl.fcntl(sys.stdin.buffer, fcntl.F_SETFL, fcntl.fcntl(sys.stdin.buffer, fcntl.F_GETFL) | os.O_NONBLOCK)
     sk = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     try:
         try:
@@ -71,32 +71,32 @@ def cli():
             return
         sys.stdout.write("SSOCK+\n")
         sys.stdout.flush()
-        buf1 = ""
-        buf2 = ""
+        buf1 = b""
+        buf2 = b""
         while True:
             wfd = []
             if buf1: wfd.append(sk)
-            if buf2: wfd.append(sys.stdout)
-            rfd, wfd, efd = select.select([sk, sys.stdin], wfd, [])
+            if buf2: wfd.append(sys.stdout.buffer)
+            rfd, wfd, efd = select.select([sk, sys.stdin.buffer], wfd, [])
             if sk in rfd:
                 ret = sk.recv(65536)
-                if ret == "":
+                if ret == b"":
                     break
                 else:
                     buf2 += ret
-            if sys.stdin in rfd:
-                ret = sys.stdin.read()
-                if ret == "":
+            if sys.stdin.buffer in rfd:
+                ret = sys.stdin.buffer.read()
+                if ret == b"":
                     break
                 else:
                     buf1 = ret
             if sk in wfd:
                 ret = sk.send(buf1)
                 buf1 = buf1[ret:]
-            if sys.stdout in wfd:
-                sys.stdout.write(buf2)
-                sys.stdout.flush()
-                buf2 = ""
+            if sys.stdout.buffer in wfd:
+                sys.stdout.buffer.write(buf2)
+                sys.stdout.buffer.flush()
+                buf2 = b""
     finally:
         sk.close()