Merge pull request #1661 from mattspitz/super_len_compatibility

Increasing super_len compatibilty to include BytesIO and cStringIO objects.
This commit is contained in:
2013-10-10 12:11:14 -07:00
3 changed files with 35 additions and 2 deletions
+1
View File
@@ -137,3 +137,4 @@ Patches and Suggestions
- Alexander Karpinsky @homm86
- Marc Schlaich @schlamar
- Park Ilsu <daftonshady@gmail.com> @daftshady
- Matt Spitz @mattspitz
+13 -2
View File
@@ -12,6 +12,7 @@ that are also useful for external consumption.
import cgi
import codecs
import collections
import io
import os
import platform
import re
@@ -46,11 +47,21 @@ def dict_to_sequence(d):
def super_len(o):
if hasattr(o, '__len__'):
return len(o)
if hasattr(o, 'len'):
return o.len
if hasattr(o, 'fileno'):
return os.fstat(o.fileno()).st_size
if hasattr(o, 'fileno'):
try:
fileno = o.fileno()
except io.UnsupportedOperation:
pass
else:
return os.fstat(fileno).st_size
if hasattr(o, 'getvalue'):
# e.g. BytesIO, cStringIO.StringI
return len(o.getvalue())
def get_netrc_auth(url):
"""Returns the Requests tuple auth for a given url from netrc."""
+21
View File
@@ -873,5 +873,26 @@ class TestCaseInsensitiveDict(unittest.TestCase):
self.assertEqual(frozenset(cid), keyset)
class UtilsTestCase(unittest.TestCase):
def test_super_len_io_streams(self):
""" Ensures that we properly deal with different kinds of IO streams. """
# uses StringIO or io.StringIO (see import above)
from io import BytesIO
from requests.utils import super_len
self.assertEqual(super_len(StringIO.StringIO()), 0)
self.assertEqual(super_len(StringIO.StringIO('with so much drama in the LBC')), 29)
self.assertEqual(super_len(BytesIO()), 0)
self.assertEqual(super_len(BytesIO(b"it's kinda hard bein' snoop d-o-double-g")), 40)
try:
import cStringIO
except ImportError:
pass
else:
self.assertEqual(super_len(cStringIO.StringIO('but some how, some way...')), 25)
if __name__ == '__main__':
unittest.main()