Skip to content

Commit

Permalink
Fallback to StringIO if cStringIO doesn't exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
behackett committed Aug 3, 2012
1 parent 5581811 commit c188836
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
8 changes: 8 additions & 0 deletions bson/py3compat.py
Expand Up @@ -20,6 +20,9 @@


if PY3: if PY3:
import codecs import codecs

from io import BytesIO as StringIO

def b(s): def b(s):
# BSON and socket operations deal in binary data. In # BSON and socket operations deal in binary data. In
# python 3 that means instances of `bytes`. In python # python 3 that means instances of `bytes`. In python
Expand All @@ -37,6 +40,11 @@ def bytes_from_hex(h):
text_type = str text_type = str


else: else:
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO

def b(s): def b(s):
# See comments above. In python 2.x b('foo') is just 'foo'. # See comments above. In python 2.x b('foo') is just 'foo'.
return s return s
Expand Down
10 changes: 1 addition & 9 deletions gridfs/grid_file.py
Expand Up @@ -17,18 +17,10 @@
import datetime import datetime
import math import math
import os 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.binary import Binary
from bson.objectid import ObjectId 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, from gridfs.errors import (CorruptGridFile,
FileExists, FileExists,
NoFile, NoFile,
Expand Down
6 changes: 1 addition & 5 deletions test/test_grid_file.py
Expand Up @@ -17,10 +17,6 @@
"""Tests for the grid_file module. """Tests for the grid_file module.
""" """


try:
from io import BytesIO as StringIO
except ImportError:
from cStringIO import StringIO
import datetime import datetime
import os import os
import sys import sys
Expand All @@ -30,7 +26,7 @@
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest


from bson.objectid import ObjectId from bson.objectid import ObjectId
from bson.py3compat import b from bson.py3compat import b, StringIO
from gridfs.grid_file import (DEFAULT_CHUNK_SIZE, from gridfs.grid_file import (DEFAULT_CHUNK_SIZE,
_SEEK_CUR, _SEEK_CUR,
_SEEK_END, _SEEK_END,
Expand Down
6 changes: 1 addition & 5 deletions test/test_gridfs.py
Expand Up @@ -22,10 +22,6 @@
from pymongo.read_preferences import ReadPreference from pymongo.read_preferences import ReadPreference
from test.test_replica_set_connection import TestConnectionReplicaSetBase from test.test_replica_set_connection import TestConnectionReplicaSetBase


try:
from io import BytesIO as StringIO
except ImportError:
from cStringIO import StringIO
import datetime import datetime
import unittest import unittest
import threading import threading
Expand All @@ -35,7 +31,7 @@


import gridfs import gridfs


from bson.py3compat import b from bson.py3compat import b, StringIO
from gridfs.errors import (FileExists, from gridfs.errors import (FileExists,
NoFile) NoFile)
from test.test_connection import get_connection from test.test_connection import get_connection
Expand Down

0 comments on commit c188836

Please sign in to comment.