fix for gridfs seek on Python < 2.5
This commit is contained in:
parent
d4c3eed06d
commit
d73782a332
@ -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")
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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()
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user