Commits

James Lan committed 837eff0

fix an issue in test case: on Windows, recv returns None instead of empty str; handles close() on none connected socket

Comments (0)

Files changed (2)

pypesocketTest.py

     try:
         while s:
             s = sock.recv(65536)
-            cs.write(s)
+            if s:
+                cs.write(s)
     finally:
         try:
             sock.shutdown(socket.SHUT_RD)
     """Use windows named pipe to simulate unix domain socket"""
 
     def accept(self):
-        pipe = self.conn.get()
+        pipe = self._conn.get()
         if not pipe:
             raise socket.error(socket.EBADF, 'Bad file descriptor')
         sock = _pipesocket()
         sock._handle = pipe
-        sock._server = False
         return sock, ''
 
     def bind(self, address):
         self._path = '\\\\.\\pipe\\' + os.path.abspath(address)
 
     def close(self):
-        if self._server:
+        if hasattr(self, '_handle'):
+            _kernel32.CloseHandle(self._handle)
+        elif hasattr(self, '_conn'):
             timeout = 0.01
             for retry in xrange(10):
                 self.closing = True
             else:
                 raise socket.error("failed to close socket " + self._path)
             self.listener.join()
-        else:
-            _kernel32.CloseHandle(self._handle)
 
     def connect(self, address):
-        self._server = False
         self.calc_pipename(address)
         timeout = 0.01
         for retry in xrange(10):
                     self.closed = True
                     _kernel32.CloseHandle(self.event)
                     _kernel32.CloseHandle(pipe)
-                    self.conn.put(None)
+                    self._conn.put(None)
                     break
                 else:
-                    self.conn.put(pipe)
+                    self._conn.put(pipe)
                     _kernel32.SetEvent(self.event)
             print "listening thread finished"
 
-        self._server = True
         self.closing = False
         self.closed = False
         self.event = _kernel32.CreateEventA(None, _FALSE, _FALSE, None)
-        self.conn = Queue.Queue(backlog)
+        self._conn = Queue.Queue(backlog)
         self.listener = Thread(target = run)
         self.listener.start()