From d73782a3321e08f3d703335908a7a46bb3bee535 Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Mon, 24 Aug 2009 12:07:03 -0400 Subject: [PATCH] fix for gridfs seek on Python < 2.5 --- gridfs/grid_file.py | 11 +++++++---- pymongo/__init__.py | 9 +++++++++ test/test_grid_file.py | 11 ++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/gridfs/grid_file.py b/gridfs/grid_file.py index 9d25a3aca..3b9f6872e 100644 --- a/gridfs/grid_file.py +++ b/gridfs/grid_file.py @@ -20,6 +20,9 @@ import math import os from threading import Condition +from pymongo import _SEEK_SET +from pymongo import _SEEK_CUR +from pymongo import _SEEK_END from pymongo.son import SON from pymongo.database import Database from pymongo.objectid import ObjectId @@ -312,7 +315,7 @@ class GridFile(object): self.__assert_open("r") return self.__position - def seek(self, pos, whence=os.SEEK_SET): + def seek(self, pos, whence=_SEEK_SET): """Set the current position of the GridFile (read-mode files only). :Parameters: @@ -322,11 +325,11 @@ class GridFile(object): position, os.SEEK_END (2) to seek relative to the file's end. """ self.__assert_open("r") - if whence == os.SEEK_SET: + if whence == _SEEK_SET: new_pos = pos - elif whence == os.SEEK_CUR: + elif whence == _SEEK_CUR: new_pos = self.__position + pos - elif whence == os.SEEK_END: + elif whence == _SEEK_END: new_pos = int(self.length) + pos else: raise IOError(22, "Invalid argument") diff --git a/pymongo/__init__.py b/pymongo/__init__.py index 8a33f6da2..09b6929fe 100644 --- a/pymongo/__init__.py +++ b/pymongo/__init__.py @@ -16,6 +16,7 @@ import types import sys +import os from pymongo.connection import Connection as PyMongo_Connection from pymongo.son import SON @@ -38,6 +39,14 @@ version = "0.14.1" Connection = PyMongo_Connection """Alias for pymongo.connection.Connection.""" +try: + _SEEK_SET = os.SEEK_SET + _SEEK_CUR = os.SEEK_CUR + _SEEK_END = os.SEEK_END +except AttributeError: # before 2.5 + _SEEK_SET = 0 + _SEEK_CUR = 1 + _SEEK_END = 2 def _index_list(key_or_list, direction): """Helper to generate a list of (key, direction) pairs. diff --git a/test/test_grid_file.py b/test/test_grid_file.py index 11763f26f..61fc8e7d3 100644 --- a/test/test_grid_file.py +++ b/test/test_grid_file.py @@ -24,6 +24,7 @@ sys.path[0:0] = [""] import qcheck from test_connection import get_connection from gridfs.grid_file import GridFile +from pymongo import _SEEK_END, _SEEK_CUR class TestGridFile(unittest.TestCase): @@ -330,16 +331,16 @@ class TestGridFile(unittest.TestCase): self.assertEqual(file.read(), "ello world") self.assertRaises(IOError, file.seek, -1) - file.seek(-3, os.SEEK_END) + file.seek(-3, _SEEK_END) self.assertEqual(file.read(), "rld") - file.seek(0, os.SEEK_END) + file.seek(0, _SEEK_END) self.assertEqual(file.read(), "") - self.assertRaises(IOError, file.seek, -100, os.SEEK_END) + self.assertRaises(IOError, file.seek, -100, _SEEK_END) file.seek(3) - file.seek(3, os.SEEK_CUR) + file.seek(3, _SEEK_CUR) self.assertEqual(file.read(), "world") - self.assertRaises(IOError, file.seek, -100, os.SEEK_CUR) + self.assertRaises(IOError, file.seek, -100, _SEEK_CUR) file.close()