From 69ce42c5a0206b461bc27eaeb70d92c36e4ec1e8 Mon Sep 17 00:00:00 2001 From: Luke Lovett Date: Wed, 23 Apr 2014 17:59:22 +0000 Subject: [PATCH] PYTHON-675 python 2/3 single-source for the gridfs module --- bson/json_util.py | 2 +- bson/py3compat.py | 9 --------- gridfs/__init__.py | 2 +- gridfs/grid_file.py | 19 +++++++++++-------- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/bson/json_util.py b/bson/json_util.py index da251dc40..0b3a06b69 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -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 = { diff --git a/bson/py3compat.py b/bson/py3compat.py index 9474dd568..0f5b3db7a 100644 --- a/bson/py3compat.py +++ b/bson/py3compat.py @@ -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) diff --git a/gridfs/__init__.py b/gridfs/__init__.py index 2d915359a..3eac49c79 100644 --- a/gridfs/__init__.py +++ b/gridfs/__init__.py @@ -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)) diff --git a/gridfs/grid_file.py b/gridfs/grid_file.py index 48482322e..2464833a9 100644 --- a/gridfs/grid_file.py +++ b/gridfs/grid_file.py @@ -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")