Commits

John Dickinson committed d08ebbf Merge

Merge pull request #48 from djoume/master

buffer strings in storage_object.py so that we don't send them 1 byte at a time.

  • Participants
  • Parent commits 469a780, de5883c

Comments (0)

Files changed (3)

File cloudfiles/storage_object.py

             except IOError:
                 pass  # If the file descriptor is read-only this will fail
             self.size = int(os.fstat(data.fileno())[6])
-        else:
+        elif isinstance(data, basestring):
             data = StringIO.StringIO(data)
             self.size = data.len
+        elif isinstance(data, StringIO.StringIO):
+            self.size = data.len
+        else:
+            self.size = len(data)
 
         # If override is set (and _etag is not None), then the etag has
         # been manually assigned and we will not calculate our own.
         """
         self._name_check()
 
+        if isinstance(iterable, basestring):
+            # use write to buffer the string and avoid sending it 1 byte at a time
+            self.write(iterable)
+
         if hasattr(iterable, 'read'):
 
             def file_iterator(file):

File tests/fakehttp.py

     def connect(self):
         self.sock = TrackerSocket()
 
+    def send(self, data):
+        self._wbuffer = data
+        connbase.send(self, data)
 
 if __name__ == '__main__':
     conn = CustomHTTPConnection('localhost', 8000)

File tests/object_test.py

 from misc              import printdoc
 from tempfile          import mktemp
 import os
+from StringIO import StringIO
 
 
 class ObjectTest(unittest.TestCase):
         Simple sanity test of Object.write()
         """
         self.storage_object.write('the rain in spain ...')
+        self.assertEqual('the rain in spain ...', self.conn.connection._wbuffer)
+
+    @printdoc
+    def test_write_with_stringio(self):
+        """
+        Ensure write() can deal with a StringIO instance
+        """
+        self.storage_object.write(StringIO('the rain in spain ...'))
+        self.assertEqual('the rain in spain ...', self.conn.connection._wbuffer)
+
+    @printdoc
+    def test_write_with_file(self):
+        """
+        Ensure write() can deal with a file instance
+        """
+        tmpnam = mktemp()
+        try:
+            fp = open(tmpnam, 'w')
+            fp.write('the rain in spain ...')
+            fp.close()
+            fp = open(tmpnam, 'r')
+            self.storage_object.write(fp)
+            fp.close()
+            self.assertEqual('the rain in spain ...', self.conn.connection._wbuffer)
+        finally:
+            os.unlink(tmpnam)
 
     @printdoc
     def test_send(self):