Permalink
Browse files

Fallback to StringIO if cStringIO doesn't exist.

  • Loading branch information...
1 parent 5581811 commit c188836d649f8a868dc346ca68acb82d421b7b57 @behackett behackett committed Aug 3, 2012
Showing with 11 additions and 19 deletions.
  1. +8 −0 bson/py3compat.py
  2. +1 −9 gridfs/grid_file.py
  3. +1 −5 test/test_grid_file.py
  4. +1 −5 test/test_gridfs.py
View
8 bson/py3compat.py
@@ -20,6 +20,9 @@
if PY3:
import codecs
+
+ from io import BytesIO as StringIO
+
def b(s):
# BSON and socket operations deal in binary data. In
# python 3 that means instances of `bytes`. In python
@@ -37,6 +40,11 @@ def bytes_from_hex(h):
text_type = str
else:
+ try:
+ from cStringIO import StringIO
+ except ImportError:
+ from StringIO import StringIO
+
def b(s):
# See comments above. In python 2.x b('foo') is just 'foo'.
return s
View
10 gridfs/grid_file.py
@@ -17,18 +17,10 @@
import datetime
import math
import os
-import sys
-
-if sys.version_info[0] == 3:
- from io import BytesIO as StringIO
-else:
- # 2to3 will turn cStringIO into io. That's okay
- # since we'll never get here under python3.
- from cStringIO import StringIO
from bson.binary import Binary
from bson.objectid import ObjectId
-from bson.py3compat import b, binary_type, string_types, text_type
+from bson.py3compat import b, binary_type, string_types, text_type, StringIO
from gridfs.errors import (CorruptGridFile,
FileExists,
NoFile,
View
6 test/test_grid_file.py
@@ -17,10 +17,6 @@
"""Tests for the grid_file module.
"""
-try:
- from io import BytesIO as StringIO
-except ImportError:
- from cStringIO import StringIO
import datetime
import os
import sys
@@ -30,7 +26,7 @@
from nose.plugins.skip import SkipTest
from bson.objectid import ObjectId
-from bson.py3compat import b
+from bson.py3compat import b, StringIO
from gridfs.grid_file import (DEFAULT_CHUNK_SIZE,
_SEEK_CUR,
_SEEK_END,
View
6 test/test_gridfs.py
@@ -22,10 +22,6 @@
from pymongo.read_preferences import ReadPreference
from test.test_replica_set_connection import TestConnectionReplicaSetBase
-try:
- from io import BytesIO as StringIO
-except ImportError:
- from cStringIO import StringIO
import datetime
import unittest
import threading
@@ -35,7 +31,7 @@
import gridfs
-from bson.py3compat import b
+from bson.py3compat import b, StringIO
from gridfs.errors import (FileExists,
NoFile)
from test.test_connection import get_connection

0 comments on commit c188836

Please sign in to comment.