Title: socket.py WHY Author: waterapple Pastebin link: http://pastebin.com/5T5zAbQL First Edit: Monday 30th of March 2015 12:08:01 AM CDT Last Edit: Monday 30th of March 2015 12:08:01 AM CDT     def read(self, size=-1):         # Use max, disallow tiny reads in a loop as they are very inefficient.         # We never leave read() with any leftover data from a new recv() call         # in our internal buffer.         rbufsize = max(self._rbufsize, self.default_bufsize)         # Our use of StringIO rather than lists of string objects returned by         # recv() minimizes memory usage and fragmentation that occurs when         # rbufsize is large compared to the typical return value of recv().         buf = self._rbuf         buf.seek(0, 2)  # seek end         if size < 0:             # Read until EOF             self._rbuf = StringIO()  # reset _rbuf.  we consume it via buf.             while True:                 try:                     data = self._sock.recv(rbufsize)                 except error, e:                     if e.args[0] == EINTR:                         continue                     raise                 if not data:                     break                 buf.write(data)             return buf.getvalue()         else:             # Read until size bytes or EOF seen, whichever comes first             buf_len = buf.tell()             if buf_len >= size:                 # Already have size bytes in our buffer?  Extract and return.                 buf.seek(0)                 rv = buf.read(size)                 self._rbuf = StringIO()                 self._rbuf.write(buf.read())                 return rv               self._rbuf = StringIO()  # reset _rbuf.  we consume it via buf.             while True:                 left = size - buf_len                 # recv() will malloc the amount of memory given as its                 # parameter even though it often returns much less data                 # than that.  The returned data string is short lived                 # as we copy it into a StringIO and free it.  This avoids                 # fragmentation issues on many platforms.                 try:                     data = self._sock.recv(left)                 except error, e:                     if e.args[0] == EINTR:                         continue                     raise                 if not data:                     break                 n = len(data)                 if n == size and not buf_len:                     # Shortcut.  Avoid buffer data copies when:                     # - We have no data in our buffer.                     # AND                     # - Our call to recv returned exactly the                     #   number of bytes we were asked to read.                     return data                 if n == left:                     buf.write(data)                     del data  # explicit free                     break                 assert n <= left, "recv(%d) returned %d bytes" % (left, n)                 buf.write(data)                 buf_len += n                 del data  # explicit free                 #assert buf_len == buf.tell()             return buf.getvalue()