PYTHON-675 python 2/3 single-source for the gridfs module

This commit is contained in:
Luke Lovett 2014-04-23 17:59:22 +00:00
parent d494105aa4
commit 69ce42c5a0
4 changed files with 13 additions and 19 deletions

View File

@ -97,7 +97,7 @@ from bson.objectid import ObjectId
from bson.regex import Regex
from bson.timestamp import Timestamp
from bson.py3compat import PY3, binary_type, iteritems, string_types, text_type
from bson.py3compat import PY3, binary_type, iteritems, text_type
_RE_OPT_TABLE = {

View File

@ -55,11 +55,6 @@ if PY3:
text_type = str
string_type = str
integer_types = int
next_item = "__next__"
# TODO: remove when gridfs module is made single-source
next_item = '__next__'
string_types = (bytes, text_type)
else:
try:
from cStringIO import StringIO
@ -96,7 +91,3 @@ else:
string_type = basestring
text_type = unicode
integer_types = (int, long)
# TODO: remove when gridfs module is made single-source
next_item = 'next'
string_types = (bytes, text_type)

View File

@ -207,7 +207,7 @@ class GridFS(object):
else:
cursor.limit(-1).skip(version).sort("uploadDate", ASCENDING)
try:
grid_file = cursor.next()
grid_file = next(cursor)
return GridOut(self.__collection, file_document=grid_file)
except StopIteration:
raise NoFile("no version %d for filename %r" % (version, filename))

View File

@ -20,8 +20,7 @@ import os
from bson.binary import Binary
from bson.objectid import ObjectId
from bson.py3compat import (b, binary_type, next_item,
string_types, text_type, StringIO)
from bson.py3compat import binary_type, text_type, StringIO
from gridfs.errors import (CorruptGridFile,
FileExists,
NoFile,
@ -41,8 +40,8 @@ except AttributeError:
_SEEK_CUR = 1
_SEEK_END = 2
EMPTY = b("")
NEWLN = b("\n")
EMPTY = b""
NEWLN = b"\n"
"""Default chunk size, in bytes."""
# Slightly under a power of 2, to work well with server's record allocations.
@ -317,9 +316,9 @@ class GridIn(object):
read = data.read
except AttributeError:
# string
if not isinstance(data, string_types):
if not isinstance(data, (text_type, bytes)):
raise TypeError("can only write strings or file-like objects")
if isinstance(data, unicode):
if isinstance(data, text_type):
try:
data = data.encode(self.encoding)
except AttributeError:
@ -500,7 +499,7 @@ class GridOut(object):
.. versionadded:: 1.9
"""
if size == 0:
return b('')
return b''
remainder = int(self.length) - self.__position
if size < 0 or size > remainder:
@ -607,6 +606,8 @@ class GridOutIterator(object):
self.__current_chunk += 1
return binary_type(chunk["data"])
__next__ = next
class GridFile(object):
"""No longer supported.
@ -651,9 +652,11 @@ class GridOutCursor(Cursor):
"""Get next GridOut object from cursor.
"""
# Work around "super is not iterable" issue in Python 3.x
next_file = getattr(super(GridOutCursor, self), next_item)()
next_file = super(GridOutCursor, self).next()
return GridOut(self.__root_collection, file_document=next_file)
__next__ = next
def add_option(self, *args, **kwargs):
raise NotImplementedError("Method does not exist for GridOutCursor")