diff --git a/gridfs/__init__.py b/gridfs/__init__.py index f291e8c9c..ab8a5a02b 100644 --- a/gridfs/__init__.py +++ b/gridfs/__init__.py @@ -20,7 +20,14 @@ The :mod:`gridfs` package is an implementation of GridFS on top of .. mongodoc:: gridfs """ -from bson.py3compat import abc +from collections import abc + +from pymongo import (ASCENDING, + DESCENDING) +from pymongo.common import UNAUTHORIZED_CODES, validate_string +from pymongo.database import Database +from pymongo.errors import ConfigurationError, OperationFailure + from gridfs.errors import NoFile from gridfs.grid_file import (GridIn, GridOut, @@ -28,12 +35,6 @@ from gridfs.grid_file import (GridIn, DEFAULT_CHUNK_SIZE, _clear_entity_type_registry, _disallow_transactions) -from pymongo import (ASCENDING, - DESCENDING) -from pymongo.common import UNAUTHORIZED_CODES, validate_string -from pymongo.database import Database -from pymongo.errors import ConfigurationError, OperationFailure - class GridFS(object): """An instance of GridFS on top of a single Database. diff --git a/gridfs/grid_file.py b/gridfs/grid_file.py index 23c629be6..e49ca472a 100644 --- a/gridfs/grid_file.py +++ b/gridfs/grid_file.py @@ -23,8 +23,6 @@ from bson.int64 import Int64 from bson.son import SON from bson.binary import Binary from bson.objectid import ObjectId -from bson.py3compat import text_type, StringIO -from gridfs.errors import CorruptGridFile, FileExists, NoFile from pymongo import ASCENDING from pymongo.collection import Collection from pymongo.cursor import Cursor @@ -35,6 +33,8 @@ from pymongo.errors import (ConfigurationError, OperationFailure) from pymongo.read_preferences import ReadPreference +from gridfs.errors import CorruptGridFile, FileExists, NoFile + try: _SEEK_SET = os.SEEK_SET _SEEK_CUR = os.SEEK_CUR @@ -195,7 +195,7 @@ class GridIn(object): object.__setattr__(self, "_coll", coll) object.__setattr__(self, "_chunks", coll.chunks) object.__setattr__(self, "_file", kwargs) - object.__setattr__(self, "_buffer", StringIO()) + object.__setattr__(self, "_buffer", io.BytesIO()) object.__setattr__(self, "_position", 0) object.__setattr__(self, "_chunk_number", 0) object.__setattr__(self, "_closed", False) @@ -297,7 +297,7 @@ class GridIn(object): """ self.__flush_data(self._buffer.getvalue()) self._buffer.close() - self._buffer = StringIO() + self._buffer = io.BytesIO() def __flush(self): """Flush the file to the database. @@ -369,15 +369,15 @@ class GridIn(object): read = data.read except AttributeError: # string - if not isinstance(data, (text_type, bytes)): + if not isinstance(data, (str, bytes)): raise TypeError("can only write strings or file-like objects") - if isinstance(data, text_type): + if isinstance(data, str): try: data = data.encode(self.encoding) except AttributeError: raise TypeError("must specify an encoding for file in " - "order to write %s" % (text_type.__name__,)) - read = StringIO(data).read + "order to write str") + read = io.BytesIO(data).read if self._buffer.tell() > 0: # Make sure to flush only when _buffer is complete @@ -560,7 +560,7 @@ class GridOut(object): return EMPTY received = 0 - data = StringIO() + data = io.BytesIO() while received < size: chunk_data = self.readchunk() received += len(chunk_data) @@ -595,7 +595,7 @@ class GridOut(object): return EMPTY received = 0 - data = StringIO() + data = io.BytesIO() while received < size: chunk_data = self.readchunk() pos = chunk_data.find(NEWLN, 0, size)