summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Thiel <byronimo@gmail.com>2014-05-16 08:23:01 +0200
committerSebastian Thiel <byronimo@gmail.com>2014-05-16 08:23:01 +0200
commit84124a347c6b7cd5f8b30b170325e77204f170fd (patch)
tree7012656115c13412270d7fe70557ba50eae3cb8c
parentcf1cf469f315df00ce7b9d47693008cd2fa1b56a (diff)
parent2d3b5a303a65d6f80b84e63a1b3cf4b670c81f9a (diff)
downloadsmmap-84124a347c6b7cd5f8b30b170325e77204f170fd.tar.gz
Merge pull request #6 from dbaxa/python_3_support
Initial work for supporting python 3 (>= 3.3).
-rw-r--r--smmap/__init__.py4
-rw-r--r--smmap/buf.py8
-rw-r--r--smmap/mman.py20
-rw-r--r--smmap/test/test_buf.py6
-rw-r--r--smmap/test/test_mman.py8
-rw-r--r--smmap/test/test_tutorial.py2
-rw-r--r--smmap/test/test_util.py8
-rw-r--r--smmap/util.py13
8 files changed, 39 insertions, 30 deletions
diff --git a/smmap/__init__.py b/smmap/__init__.py
index a10cd5c..879ebea 100644
--- a/smmap/__init__.py
+++ b/smmap/__init__.py
@@ -7,5 +7,5 @@ version_info = (0, 8, 2)
__version__ = '.'.join(str(i) for i in version_info)
# make everything available in root package for convenience
-from mman import *
-from buf import *
+from .mman import *
+from .buf import *
diff --git a/smmap/buf.py b/smmap/buf.py
index 255c6b5..3917ee8 100644
--- a/smmap/buf.py
+++ b/smmap/buf.py
@@ -1,5 +1,5 @@
"""Module with a simple buffer implementation using the memory manager"""
-from mman import WindowCursor
+from .mman import WindowCursor
import sys
@@ -21,7 +21,7 @@ class SlidingWindowMapBuffer(object):
)
- def __init__(self, cursor = None, offset = 0, size = sys.maxint, flags = 0):
+ def __init__(self, cursor = None, offset = 0, size = sys.maxsize, flags = 0):
"""Initalize the instance to operate on the given cursor.
:param cursor: if not None, the associated cursor to the file you want to access
If None, you have call begin_access before using the buffer and provide a cursor
@@ -61,7 +61,7 @@ class SlidingWindowMapBuffer(object):
assert c.is_valid()
if i < 0:
i = self._size + i
- if j == sys.maxint:
+ if j == sys.maxsize:
j = self._size
if j < 0:
j = self._size + j
@@ -86,7 +86,7 @@ class SlidingWindowMapBuffer(object):
# END fast or slow path
#{ Interface
- def begin_access(self, cursor = None, offset = 0, size = sys.maxint, flags = 0):
+ def begin_access(self, cursor = None, offset = 0, size = sys.maxsize, flags = 0):
"""Call this before the first use of this instance. The method was already
called by the constructor in case sufficient information was provided.
diff --git a/smmap/mman.py b/smmap/mman.py
index 97c42c5..9cc251f 100644
--- a/smmap/mman.py
+++ b/smmap/mman.py
@@ -1,15 +1,17 @@
"""Module containnig a memory memory manager which provides a sliding window on a number of memory mapped files"""
-from util import (
+from .util import (
MapWindow,
MapRegion,
MapRegionList,
is_64_bit,
- align_to_mmap
+ align_to_mmap,
+ string_types,
)
from weakref import ref
import sys
from sys import getrefcount
+from functools import reduce
__all__ = ["StaticWindowMapManager", "SlidingWindowMapManager", "WindowCursor"]
#{ Utilities
@@ -218,7 +220,7 @@ class WindowCursor(object):
**Note:** it is not required to be valid anymore
:raise ValueError: if the mapping was not created by a file descriptor"""
- if isinstance(self._rlist.path_or_fd(), basestring):
+ if isinstance(self._rlist.path_or_fd(), string_types()):
raise ValueError("File descriptor queried although mapping was generated from path")
#END handle type
return self._rlist.path_or_fd()
@@ -256,7 +258,7 @@ class StaticWindowMapManager(object):
_MB_in_bytes = 1024 * 1024
- def __init__(self, window_size = 0, max_memory_size = 0, max_open_handles = sys.maxint):
+ def __init__(self, window_size = 0, max_memory_size = 0, max_open_handles = sys.maxsize):
"""initialize the manager with the given parameters.
:param window_size: if -1, a default window size will be chosen depending on
the operating system's architechture. It will internally be quantified to a multiple of the page size
@@ -306,7 +308,7 @@ class StaticWindowMapManager(object):
while (size == 0) or (self._memory_size + size > self._max_memory_size):
lru_region = None
lru_list = None
- for regions in self._fdict.itervalues():
+ for regions in self._fdict.values():
for region in regions:
# check client count - consider that we keep one reference ourselves !
if (region.client_count()-2 == 0 and
@@ -343,7 +345,7 @@ class StaticWindowMapManager(object):
r = a[0]
else:
try:
- r = self.MapRegionCls(a.path_or_fd(), 0, sys.maxint, flags)
+ r = self.MapRegionCls(a.path_or_fd(), 0, sys.maxsize, flags)
except Exception:
# apparently we are out of system resources or hit a limit
# As many more operations are likely to fail in that condition (
@@ -405,7 +407,7 @@ class StaticWindowMapManager(object):
def num_open_files(self):
"""Amount of opened files in the system"""
- return reduce(lambda x,y: x+y, (1 for rlist in self._fdict.itervalues() if len(rlist) > 0), 0)
+ return reduce(lambda x,y: x+y, (1 for rlist in self._fdict.values() if len(rlist) > 0), 0)
def window_size(self):
""":return: size of each window when allocating new regions"""
@@ -445,7 +447,7 @@ class StaticWindowMapManager(object):
#END early bailout
num_closed = 0
- for path, rlist in self._fdict.iteritems():
+ for path, rlist in self._fdict.items():
if path.startswith(base_path):
for region in rlist:
region._mf.close()
@@ -473,7 +475,7 @@ class SlidingWindowMapManager(StaticWindowMapManager):
__slots__ = tuple()
- def __init__(self, window_size = -1, max_memory_size = 0, max_open_handles = sys.maxint):
+ def __init__(self, window_size = -1, max_memory_size = 0, max_open_handles = sys.maxsize):
"""Adjusts the default window size to -1"""
super(SlidingWindowMapManager, self).__init__(window_size, max_memory_size, max_open_handles)
diff --git a/smmap/test/test_buf.py b/smmap/test/test_buf.py
index 4bdcb76..d40da14 100644
--- a/smmap/test/test_buf.py
+++ b/smmap/test/test_buf.py
@@ -1,4 +1,4 @@
-from lib import TestBase, FileCreator
+from .lib import TestBase, FileCreator
from smmap.mman import SlidingWindowMapManager, StaticWindowMapManager
from smmap.buf import *
@@ -22,8 +22,8 @@ class TestBuf(TestBase):
# invalid paths fail upon construction
c = man_optimal.make_cursor(fc.path)
- self.failUnlessRaises(ValueError, SlidingWindowMapBuffer, type(c)()) # invalid cursor
- self.failUnlessRaises(ValueError, SlidingWindowMapBuffer, c, fc.size) # offset too large
+ self.assertRaises(ValueError, SlidingWindowMapBuffer, type(c)()) # invalid cursor
+ self.assertRaises(ValueError, SlidingWindowMapBuffer, c, fc.size) # offset too large
buf = SlidingWindowMapBuffer() # can create uninitailized buffers
assert buf.cursor() is None
diff --git a/smmap/test/test_mman.py b/smmap/test/test_mman.py
index 46429a4..0929583 100644
--- a/smmap/test/test_mman.py
+++ b/smmap/test/test_mman.py
@@ -1,4 +1,4 @@
-from lib import TestBase, FileCreator
+from .lib import TestBase, FileCreator
from smmap.mman import *
from smmap.mman import WindowCursor
@@ -66,7 +66,7 @@ class TestMMan(TestBase):
man._collect_lru_region(10)
# doesn't fail if we overallocate
- assert man._collect_lru_region(sys.maxint) == 0
+ assert man._collect_lru_region(sys.maxsize) == 0
# use a region, verify most basic functionality
fc = FileCreator(self.k_window_test_size, "manager_test")
@@ -80,9 +80,9 @@ class TestMMan(TestBase):
assert c.buffer()[:] == open(fc.path, 'rb').read(20)[10:]
if isinstance(item, int):
- self.failUnlessRaises(ValueError, c.path)
+ self.assertRaises(ValueError, c.path)
else:
- self.failUnlessRaises(ValueError, c.fd)
+ self.assertRaises(ValueError, c.fd)
#END handle value error
#END for each input
os.close(fd)
diff --git a/smmap/test/test_tutorial.py b/smmap/test/test_tutorial.py
index 4e1a576..ad1a9c0 100644
--- a/smmap/test/test_tutorial.py
+++ b/smmap/test/test_tutorial.py
@@ -1,4 +1,4 @@
-from lib import TestBase
+from .lib import TestBase
class TestTutorial(TestBase):
diff --git a/smmap/test/test_util.py b/smmap/test/test_util.py
index 2df0660..a009bd9 100644
--- a/smmap/test/test_util.py
+++ b/smmap/test/test_util.py
@@ -1,4 +1,4 @@
-from lib import TestBase, FileCreator
+from .lib import TestBase, FileCreator
from smmap.util import *
@@ -38,7 +38,7 @@ class TestMMan(TestBase):
assert wc.ofs == 1 and wc.size == maxsize
# without maxsize
- wc.extend_right_to(wr, sys.maxint)
+ wc.extend_right_to(wr, sys.maxsize)
assert wc.ofs_end() == wr.ofs and wc.ofs == 1
# extend left
@@ -46,7 +46,7 @@ class TestMMan(TestBase):
wr.extend_left_to(wc2, maxsize)
assert wr.size == maxsize
- wr.extend_left_to(wc2, sys.maxint)
+ wr.extend_left_to(wc2, sys.maxsize)
assert wr.ofs == wc2.ofs_end()
wc.align()
@@ -68,7 +68,7 @@ class TestMMan(TestBase):
assert rhalfsize.ofs_begin() == 0 and rhalfsize.size() == half_size
assert rfull.includes_ofs(0) and rfull.includes_ofs(fc.size-1) and rfull.includes_ofs(half_size)
- assert not rfull.includes_ofs(-1) and not rfull.includes_ofs(sys.maxint)
+ assert not rfull.includes_ofs(-1) and not rfull.includes_ofs(sys.maxsize)
# with the values we have, this test only works on windows where an alignment
# size of 4096 is assumed.
# We only test on linux as it is inconsitent between the python versions
diff --git a/smmap/util.py b/smmap/util.py
index c6710b3..fee9ad5 100644
--- a/smmap/util.py
+++ b/smmap/util.py
@@ -19,6 +19,13 @@ __all__ = [ "align_to_mmap", "is_64_bit",
#{ Utilities
+def string_types():
+ if sys.version_info[0] >= 3:
+ return str
+ else:
+ return basestring
+
+
def align_to_mmap(num, round_up):
"""
Align the given integer number to the closest page offset, which usually is 4096 bytes.
@@ -34,7 +41,7 @@ def align_to_mmap(num, round_up):
def is_64_bit():
""":return: True if the system is 64 bit. Otherwise it can be assumed to be 32 bit"""
- return sys.maxint > (1<<32) - 1
+ return sys.maxsize > (1<<32) - 1
#}END utilities
@@ -154,7 +161,7 @@ class MapRegion(object):
self._mfb = buffer(self._mf, ofs, self._size)
#END handle buffer wrapping
finally:
- if isinstance(path_or_fd, basestring):
+ if isinstance(path_or_fd, string_types()):
os.close(fd)
#END only close it if we opened it
#END close file handle
@@ -258,7 +265,7 @@ class MapRegionList(list):
def file_size(self):
""":return: size of file we manager"""
if self._file_size is None:
- if isinstance(self._path_or_fd, basestring):
+ if isinstance(self._path_or_fd, string_types()):
self._file_size = os.stat(self._path_or_fd).st_size
else:
self._file_size = os.fstat(self._path_or_fd).st_size