Commits

Robert Brewer committed 39923f5

Fix for py3k BufferedReader with stats

Comments (0)

Files changed (2)

cheroot/py3makefile.py

 DEFAULT_BUFFER_SIZE = io.DEFAULT_BUFFER_SIZE
 
 
+class BufferedReader(io.BufferedReader):
+    """Faux file object attached to a socket object."""
+
+    def __init__(self, *args, **kwargs):
+        self.bytes_read = 0
+        io.BufferedReader.__init__(self, *args, **kwargs)
+
+    def read(self, *args, **kwargs):
+        output = io.BufferedReader.read(self, *args, **kwargs)
+        self.bytes_read += len(output)
+        return output
+
+
 class BufferedWriter(io.BufferedWriter):
     """Faux file object attached to a socket object."""
 
+    def __init__(self, *args, **kwargs):
+        self.bytes_written = 0
+        io.BufferedWriter.__init__(self, *args, **kwargs)
+
     def write(self, b):
         self._checkClosed()
         if isinstance(b, str):
             raise TypeError("can't write str to binary stream")
-        
+
+        self.bytes_written += len(b)
+
         with self._write_lock:
             self._write_buf.extend(b)
             self._flush_unlocked()
             return len(b)
-    
+
     def _flush_unlocked(self):
         self._checkClosed("flush of closed file")
         while self._write_buf:
 
 def makefile(sock, mode='r', bufsize=DEFAULT_BUFFER_SIZE):
     if 'r' in mode:
-        return io.BufferedReader(socket.SocketIO(sock, mode), bufsize)
+        return BufferedReader(socket.SocketIO(sock, mode), bufsize)
     else:
         return BufferedWriter(socket.SocketIO(sock, mode), bufsize)
 

cheroot/test/helper.py

         cls.log = []
         cls.httpserver.error_log = logsink
 
+        # Turn on stats, mostly for code coverage
+        cls.httpserver.stats['Enabled'] = True
+
         if hasattr(cls, 'setup_server'):
             # Clear the wsgi server so that
             # it can be updated with the new root