Updating StringIO, BytesIO, and FileIO imports and usage. (Using six)
diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py
index cdbf140..56d182e 100644
--- a/googleapiclient/discovery.py
+++ b/googleapiclient/discovery.py
@@ -28,9 +28,9 @@
'key2param',
]
+from six import StringIO
# Standard library imports
-import StringIO
import copy
from email.generator import Generator
from email.mime.multipart import MIMEMultipart
@@ -752,7 +752,7 @@
msgRoot.attach(msg)
# encode the body: note that we can't use `as_string`, because
# it plays games with `From ` lines.
- fp = StringIO.StringIO()
+ fp = StringIO()
g = Generator(fp, mangle_from_=False)
g.flatten(msgRoot, unixfrom=False)
body = fp.getvalue()
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index f0e9133..64d3f3c 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -24,7 +24,8 @@
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
-import StringIO
+from six import BytesIO, StringIO
+
import base64
import copy
import gzip
@@ -262,7 +263,7 @@
Note that the Python file object is compatible with io.Base and can be used
with this class also.
- fh = io.BytesIO('...Some data to upload...')
+ fh = BytesIO('...Some data to upload...')
media = MediaIoBaseUpload(fh, mimetype='image/png',
chunksize=1024*1024, resumable=True)
farm.animals().insert(
@@ -468,7 +469,7 @@
resumable: bool, True if this is a resumable upload. False means upload
in a single request.
"""
- fd = StringIO.StringIO(body)
+ fd = BytesIO(body)
super(MediaInMemoryUpload, self).__init__(fd, mimetype, chunksize=chunksize,
resumable=resumable)
@@ -1113,7 +1114,7 @@
msg['content-length'] = str(len(request.body))
# Serialize the mime message.
- fp = StringIO.StringIO()
+ fp = StringIO()
# maxheaderlen=0 means don't line wrap headers.
g = Generator(fp, maxheaderlen=0)
g.flatten(msg, unixfrom=False)
@@ -1236,7 +1237,7 @@
# encode the body: note that we can't use `as_string`, because
# it plays games with `From ` lines.
- fp = StringIO.StringIO()
+ fp = StringIO()
g = Generator(fp, mangle_from_=False)
g.flatten(message, unixfrom=False)
body = fp.getvalue()
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index ccc69c2..70bd248 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -25,6 +25,8 @@
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
+from six import BytesIO, StringIO
+
import copy
import datetime
import httplib2
@@ -35,7 +37,6 @@
import sys
import unittest2 as unittest
import urlparse
-import StringIO
try:
@@ -857,62 +858,48 @@
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', http=self.http)
- try:
- import io
+ # Set up a seekable stream and try to upload in single chunk.
+ fd = BytesIO('01234"56789"')
+ media_upload = MediaIoBaseUpload(
+ fd=fd, mimetype='text/plain', chunksize=-1, resumable=True)
- # Set up a seekable stream and try to upload in single chunk.
- fd = io.BytesIO('01234"56789"')
- media_upload = MediaIoBaseUpload(
- fd=fd, mimetype='text/plain', chunksize=-1, resumable=True)
+ request = zoo.animals().insert(media_body=media_upload, body=None)
- request = zoo.animals().insert(media_body=media_upload, body=None)
+ # The single chunk fails, restart at the right point.
+ http = HttpMockSequence([
+ ({'status': '200',
+ 'location': 'http://upload.example.com'}, ''),
+ ({'status': '308',
+ 'location': 'http://upload.example.com/2',
+ 'range': '0-4'}, ''),
+ ({'status': '200'}, 'echo_request_body'),
+ ])
- # The single chunk fails, restart at the right point.
- http = HttpMockSequence([
- ({'status': '200',
- 'location': 'http://upload.example.com'}, ''),
- ({'status': '308',
- 'location': 'http://upload.example.com/2',
- 'range': '0-4'}, ''),
- ({'status': '200'}, 'echo_request_body'),
- ])
-
- body = request.execute(http=http)
- self.assertEqual('56789', body)
-
- except ImportError:
- pass
-
+ body = request.execute(http=http)
+ self.assertEqual('56789', body)
def test_media_io_base_stream_chunksize_resume(self):
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', http=self.http)
+ # Set up a seekable stream and try to upload in chunks.
+ fd = BytesIO('0123456789')
+ media_upload = MediaIoBaseUpload(
+ fd=fd, mimetype='text/plain', chunksize=5, resumable=True)
+
+ request = zoo.animals().insert(media_body=media_upload, body=None)
+
+ # The single chunk fails, pull the content sent out of the exception.
+ http = HttpMockSequence([
+ ({'status': '200',
+ 'location': 'http://upload.example.com'}, ''),
+ ({'status': '400'}, 'echo_request_body'),
+ ])
+
try:
- import io
-
- # Set up a seekable stream and try to upload in chunks.
- fd = io.BytesIO('0123456789')
- media_upload = MediaIoBaseUpload(
- fd=fd, mimetype='text/plain', chunksize=5, resumable=True)
-
- request = zoo.animals().insert(media_body=media_upload, body=None)
-
- # The single chunk fails, pull the content sent out of the exception.
- http = HttpMockSequence([
- ({'status': '200',
- 'location': 'http://upload.example.com'}, ''),
- ({'status': '400'}, 'echo_request_body'),
- ])
-
- try:
- body = request.execute(http=http)
- except HttpError as e:
- self.assertEqual('01234', e.content)
-
- except ImportError:
- pass
-
+ body = request.execute(http=http)
+ except HttpError as e:
+ self.assertEqual('01234', e.content)
def test_resumable_media_handle_uploads_of_unknown_size(self):
http = HttpMockSequence([
@@ -1021,7 +1008,7 @@
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', http=self.http)
- fd = StringIO.StringIO('data goes here')
+ fd = BytesIO('data goes here')
# Create an upload that doesn't know the full size of the media.
upload = MediaIoBaseUpload(
@@ -1045,7 +1032,7 @@
zoo = build('zoo', 'v1', http=self.http)
# Create an upload that doesn't know the full size of the media.
- fd = StringIO.StringIO('data goes here')
+ fd = BytesIO('data goes here')
upload = MediaIoBaseUpload(
fd=fd, mimetype='image/png', chunksize=500, resumable=True)
diff --git a/tests/test_http.py b/tests/test_http.py
index a3ff0ac..0608c92 100644
--- a/tests/test_http.py
+++ b/tests/test_http.py
@@ -23,6 +23,9 @@
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
+from six import BytesIO, StringIO
+from io import FileIO
+
# Do not remove the httplib2 import
import httplib2
import logging
@@ -30,7 +33,6 @@
import unittest2 as unittest
import urllib
import random
-import StringIO
import time
from googleapiclient.discovery import build
@@ -196,19 +198,14 @@
class TestMediaIoBaseUpload(unittest.TestCase):
def test_media_io_base_upload_from_file_io(self):
- try:
- import io
-
- fd = io.FileIO(datafile('small.png'), 'r')
- upload = MediaIoBaseUpload(
- fd=fd, mimetype='image/png', chunksize=500, resumable=True)
- self.assertEqual('image/png', upload.mimetype())
- self.assertEqual(190, upload.size())
- self.assertEqual(True, upload.resumable())
- self.assertEqual(500, upload.chunksize())
- self.assertEqual('PNG', upload.getbytes(1, 3))
- except ImportError:
- pass
+ fd = FileIO(datafile('small.png'), 'r')
+ upload = MediaIoBaseUpload(
+ fd=fd, mimetype='image/png', chunksize=500, resumable=True)
+ self.assertEqual('image/png', upload.mimetype())
+ self.assertEqual(190, upload.size())
+ self.assertEqual(True, upload.resumable())
+ self.assertEqual(500, upload.chunksize())
+ self.assertEqual('PNG', upload.getbytes(1, 3))
def test_media_io_base_upload_from_file_object(self):
f = open(datafile('small.png'), 'r')
@@ -233,7 +230,7 @@
def test_media_io_base_upload_from_string_io(self):
f = open(datafile('small.png'), 'r')
- fd = StringIO.StringIO(f.read())
+ fd = StringIO(f.read())
f.close()
upload = MediaIoBaseUpload(
@@ -246,52 +243,32 @@
f.close()
def test_media_io_base_upload_from_bytes(self):
- try:
- import io
-
- f = open(datafile('small.png'), 'r')
- fd = io.BytesIO(f.read())
- upload = MediaIoBaseUpload(
- fd=fd, mimetype='image/png', chunksize=500, resumable=True)
- self.assertEqual('image/png', upload.mimetype())
- self.assertEqual(190, upload.size())
- self.assertEqual(True, upload.resumable())
- self.assertEqual(500, upload.chunksize())
- self.assertEqual('PNG', upload.getbytes(1, 3))
- except ImportError:
- pass
+ f = open(datafile('small.png'), 'r')
+ fd = BytesIO(f.read())
+ upload = MediaIoBaseUpload(
+ fd=fd, mimetype='image/png', chunksize=500, resumable=True)
+ self.assertEqual('image/png', upload.mimetype())
+ self.assertEqual(190, upload.size())
+ self.assertEqual(True, upload.resumable())
+ self.assertEqual(500, upload.chunksize())
+ self.assertEqual('PNG', upload.getbytes(1, 3))
def test_media_io_base_upload_raises_on_invalid_chunksize(self):
- try:
- import io
-
- f = open(datafile('small.png'), 'r')
- fd = io.BytesIO(f.read())
- self.assertRaises(InvalidChunkSizeError, MediaIoBaseUpload,
- fd, 'image/png', chunksize=-2, resumable=True)
- except ImportError:
- pass
+ f = open(datafile('small.png'), 'r')
+ fd = BytesIO(f.read())
+ self.assertRaises(InvalidChunkSizeError, MediaIoBaseUpload,
+ fd, 'image/png', chunksize=-2, resumable=True)
def test_media_io_base_upload_streamable(self):
- try:
- import io
-
- fd = io.BytesIO('stuff')
- upload = MediaIoBaseUpload(
- fd=fd, mimetype='image/png', chunksize=500, resumable=True)
- self.assertEqual(True, upload.has_stream())
- self.assertEqual(fd, upload.stream())
- except ImportError:
- pass
+ fd = BytesIO('stuff')
+ upload = MediaIoBaseUpload(
+ fd=fd, mimetype='image/png', chunksize=500, resumable=True)
+ self.assertEqual(True, upload.has_stream())
+ self.assertEqual(fd, upload.stream())
def test_media_io_base_next_chunk_retries(self):
- try:
- import io
- except ImportError:
- return
-
f = open(datafile('small.png'), 'r')
- fd = io.BytesIO(f.read())
+ fd = BytesIO(f.read())
upload = MediaIoBaseUpload(
fd=fd, mimetype='image/png', chunksize=500, resumable=True)
@@ -333,7 +310,7 @@
http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', http=http)
self.request = zoo.animals().get_media(name='Lion')
- self.fd = StringIO.StringIO()
+ self.fd = BytesIO()
def test_media_io_base_download(self):
self.request.http = HttpMockSequence([
@@ -963,7 +940,7 @@
"""Test _StreamSlice."""
def setUp(self):
- self.stream = StringIO.StringIO('0123456789')
+ self.stream = BytesIO('0123456789')
def test_read(self):
s = _StreamSlice(self.stream, 0, 4)
diff --git a/tests/test_schema.py b/tests/test_schema.py
index 8d07fed..c1216a5 100644
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -20,7 +20,6 @@
import json
import os
import unittest2 as unittest
-import StringIO
from googleapiclient.schema import Schemas