Commits

Anonymous committed bedf7d1

Seek the body in Request.body when available

  • Participants
  • Parent commits 971736e
  • Tags 0.9.6.1

Comments (0)

Files changed (3)

 svn trunk
 ---------
 
-* Fixed :meth:`Response.__init__`, which for some content types would
+* Fixed :method:`Response.__init__`, which for some content types would
   raise an exception.
 
+* The ``req.body`` property will not recreate a StringIO object
+  unnecessarily when rereading the body.
+
 0.9.6
 -----
 

tests/test_request.txt

     >>> start, end, length
     (0, 100, 1000)
 
-A quick test of caching the request body:
+A quick test of caching the request body (now that we check for seek()
+this doesn't exercise it well):
 
     >>> from cStringIO import StringIO
     >>> length = Request.request_body_tempfile_limit+10
     >>> len(req.body)
     10250
     >>> req.body_file
-    <open file '<fdopen>', mode 'w+b' at ...>
+    <cStringIO.StringI object at ...>
+    >>> req.body_file.tell()
+    0
 
 Some query tests:
 

webob/__init__.py

         except ValueError:
             return ''
         c = self.body_file.read(length)
-        tempfile_limit = self.request_body_tempfile_limit
-        if tempfile_limit and len(c) > tempfile_limit:
-            fileobj = tempfile.TemporaryFile()
-            fileobj.write(c)
-            fileobj.seek(0)
+        if hasattr(self.body_file, 'seek'):
+            self.body_file.seek(0)
         else:
-            fileobj = StringIO(c)
-        # We don't want/need to lose CONTENT_LENGTH here (as setting
-        # self.body_file would do):
-        self.environ['wsgi.input'] = fileobj
+            tempfile_limit = self.request_body_tempfile_limit
+            if tempfile_limit and len(c) > tempfile_limit:
+                fileobj = tempfile.TemporaryFile()
+                fileobj.write(c)
+                fileobj.seek(0)
+            else:
+                fileobj = StringIO(c)
+            # We don't want/need to lose CONTENT_LENGTH here (as setting
+            # self.body_file would do):
+            self.environ['wsgi.input'] = fileobj
         return c
 
     def _body__set(self, value):