From: Finn B. <bc...@us...> - 2000-12-05 19:25:51
|
Update of /cvsroot/jython/jython/Lib In directory slayer.i.sourceforge.net:/tmp/cvs-serv16081 Modified Files: socket.py Log Message: Fix bug 122816. A socket now keeps track of the number of makefile() calls and only closes the connection when noth the socket and the last makefile() file is closed. Index: socket.py =================================================================== RCS file: /cvsroot/jython/jython/Lib/socket.py,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -r1.7 -r1.8 *** socket.py 2000/11/26 16:18:40 1.7 --- socket.py 2000/12/05 19:25:48 1.8 *************** *** 57,60 **** --- 57,61 ---- addr = None server = 0 + file_count = 0 def bind(self, addr, port=None): *************** *** 143,157 **** def makefile(self, mode="r", bufsize=-1): if self.istream: if self.ostream: ! return org.python.core.PyFile(self.istream, self.ostream, "<socket>", mode) else: ! return org.python.core.PyFile(self.istream, "<socket>", mode) elif self.ostream: ! return org.python.core.PyFile(self.ostream, "<socket>", mode) else: raise IOError, "both istream and ostream have been shut down" def shutdown(self, how): assert how in (0, 1, 2) --- 144,198 ---- def makefile(self, mode="r", bufsize=-1): + file = None if self.istream: if self.ostream: ! file = org.python.core.PyFile(self.istream, self.ostream, "<socket>", mode) else: ! file = org.python.core.PyFile(self.istream, "<socket>", mode) elif self.ostream: ! file = org.python.core.PyFile(self.ostream, "<socket>", mode) else: raise IOError, "both istream and ostream have been shut down" + if file: + return _tcpsocket.FileWrapper(self, file) + class FileWrapper: + def __init__(self, socket, file): + self.socket = socket + self.sock = socket.sock + self.istream = socket.istream + self.ostream = socket.ostream + + self.file = file + self.read = file.read + self.readline = file.readline + self.readlines = file.readlines + self.write = file.write + self.writelines = file.writelines + self.flush = file.flush + self.seek = file.seek + self.tell = file.tell + + self.socket.file_count += 1 + + def close(self): + if self.file.closed: + # Already closed + return + + self.socket.file_count -= 1 + self.file.close() + + if self.socket.file_count == 0 and self.socket.sock == 0: + # This is the last file Only close the socket and streams + # if there are no outstanding files left. + if self.sock: + self.sock.close() + if self.istream: + self.istream.close() + if self.ostream: + self.ostream.close() + def shutdown(self, how): assert how in (0, 1, 2) *************** *** 169,178 **** self.istream = 0 self.ostream = 0 ! if istream: ! istream.close() ! if ostream: ! ostream.close() ! if sock: ! sock.close() --- 210,222 ---- self.istream = 0 self.ostream = 0 ! # Only close the socket and streams if there are no ! # outstanding files left. ! if self.file_count == 0: ! if istream: ! istream.close() ! if ostream: ! ostream.close() ! if sock: ! sock.close() |