Skip to content

Commit 84124a3

Browse files
committed
Merge pull request #6 from dbaxa/python_3_support
Initial work for supporting python 3 (>= 3.3).
2 parents cf1cf46 + 2d3b5a3 commit 84124a3

8 files changed

Lines changed: 39 additions & 30 deletions

File tree

smmap/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
__version__ = '.'.join(str(i) for i in version_info)
88

99
# make everything available in root package for convenience
10-
from mman import *
11-
from buf import *
10+
from .mman import *
11+
from .buf import *

smmap/buf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Module with a simple buffer implementation using the memory manager"""
2-
from mman import WindowCursor
2+
from .mman import WindowCursor
33

44
import sys
55

@@ -21,7 +21,7 @@ class SlidingWindowMapBuffer(object):
2121
)
2222

2323

24-
def __init__(self, cursor = None, offset = 0, size = sys.maxint, flags = 0):
24+
def __init__(self, cursor = None, offset = 0, size = sys.maxsize, flags = 0):
2525
"""Initalize the instance to operate on the given cursor.
2626
:param cursor: if not None, the associated cursor to the file you want to access
2727
If None, you have call begin_access before using the buffer and provide a cursor
@@ -61,7 +61,7 @@ def __getslice__(self, i, j):
6161
assert c.is_valid()
6262
if i < 0:
6363
i = self._size + i
64-
if j == sys.maxint:
64+
if j == sys.maxsize:
6565
j = self._size
6666
if j < 0:
6767
j = self._size + j
@@ -86,7 +86,7 @@ def __getslice__(self, i, j):
8686
# END fast or slow path
8787
#{ Interface
8888

89-
def begin_access(self, cursor = None, offset = 0, size = sys.maxint, flags = 0):
89+
def begin_access(self, cursor = None, offset = 0, size = sys.maxsize, flags = 0):
9090
"""Call this before the first use of this instance. The method was already
9191
called by the constructor in case sufficient information was provided.
9292

smmap/mman.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
"""Module containnig a memory memory manager which provides a sliding window on a number of memory mapped files"""
2-
from util import (
2+
from .util import (
33
MapWindow,
44
MapRegion,
55
MapRegionList,
66
is_64_bit,
7-
align_to_mmap
7+
align_to_mmap,
8+
string_types,
89
)
910

1011
from weakref import ref
1112
import sys
1213
from sys import getrefcount
14+
from functools import reduce
1315

1416
__all__ = ["StaticWindowMapManager", "SlidingWindowMapManager", "WindowCursor"]
1517
#{ Utilities
@@ -218,7 +220,7 @@ def fd(self):
218220
219221
**Note:** it is not required to be valid anymore
220222
:raise ValueError: if the mapping was not created by a file descriptor"""
221-
if isinstance(self._rlist.path_or_fd(), basestring):
223+
if isinstance(self._rlist.path_or_fd(), string_types()):
222224
raise ValueError("File descriptor queried although mapping was generated from path")
223225
#END handle type
224226
return self._rlist.path_or_fd()
@@ -256,7 +258,7 @@ class StaticWindowMapManager(object):
256258

257259
_MB_in_bytes = 1024 * 1024
258260

259-
def __init__(self, window_size = 0, max_memory_size = 0, max_open_handles = sys.maxint):
261+
def __init__(self, window_size = 0, max_memory_size = 0, max_open_handles = sys.maxsize):
260262
"""initialize the manager with the given parameters.
261263
:param window_size: if -1, a default window size will be chosen depending on
262264
the operating system's architechture. It will internally be quantified to a multiple of the page size
@@ -306,7 +308,7 @@ def _collect_lru_region(self, size):
306308
while (size == 0) or (self._memory_size + size > self._max_memory_size):
307309
lru_region = None
308310
lru_list = None
309-
for regions in self._fdict.itervalues():
311+
for regions in self._fdict.values():
310312
for region in regions:
311313
# check client count - consider that we keep one reference ourselves !
312314
if (region.client_count()-2 == 0 and
@@ -343,7 +345,7 @@ def _obtain_region(self, a, offset, size, flags, is_recursive):
343345
r = a[0]
344346
else:
345347
try:
346-
r = self.MapRegionCls(a.path_or_fd(), 0, sys.maxint, flags)
348+
r = self.MapRegionCls(a.path_or_fd(), 0, sys.maxsize, flags)
347349
except Exception:
348350
# apparently we are out of system resources or hit a limit
349351
# As many more operations are likely to fail in that condition (
@@ -405,7 +407,7 @@ def num_file_handles(self):
405407

406408
def num_open_files(self):
407409
"""Amount of opened files in the system"""
408-
return reduce(lambda x,y: x+y, (1 for rlist in self._fdict.itervalues() if len(rlist) > 0), 0)
410+
return reduce(lambda x,y: x+y, (1 for rlist in self._fdict.values() if len(rlist) > 0), 0)
409411

410412
def window_size(self):
411413
""":return: size of each window when allocating new regions"""
@@ -445,7 +447,7 @@ def force_map_handle_removal_win(self, base_path):
445447
#END early bailout
446448

447449
num_closed = 0
448-
for path, rlist in self._fdict.iteritems():
450+
for path, rlist in self._fdict.items():
449451
if path.startswith(base_path):
450452
for region in rlist:
451453
region._mf.close()
@@ -473,7 +475,7 @@ class SlidingWindowMapManager(StaticWindowMapManager):
473475

474476
__slots__ = tuple()
475477

476-
def __init__(self, window_size = -1, max_memory_size = 0, max_open_handles = sys.maxint):
478+
def __init__(self, window_size = -1, max_memory_size = 0, max_open_handles = sys.maxsize):
477479
"""Adjusts the default window size to -1"""
478480
super(SlidingWindowMapManager, self).__init__(window_size, max_memory_size, max_open_handles)
479481

smmap/test/test_buf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lib import TestBase, FileCreator
1+
from .lib import TestBase, FileCreator
22

33
from smmap.mman import SlidingWindowMapManager, StaticWindowMapManager
44
from smmap.buf import *
@@ -22,8 +22,8 @@ def test_basics(self):
2222

2323
# invalid paths fail upon construction
2424
c = man_optimal.make_cursor(fc.path)
25-
self.failUnlessRaises(ValueError, SlidingWindowMapBuffer, type(c)()) # invalid cursor
26-
self.failUnlessRaises(ValueError, SlidingWindowMapBuffer, c, fc.size) # offset too large
25+
self.assertRaises(ValueError, SlidingWindowMapBuffer, type(c)()) # invalid cursor
26+
self.assertRaises(ValueError, SlidingWindowMapBuffer, c, fc.size) # offset too large
2727

2828
buf = SlidingWindowMapBuffer() # can create uninitailized buffers
2929
assert buf.cursor() is None

smmap/test/test_mman.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lib import TestBase, FileCreator
1+
from .lib import TestBase, FileCreator
22

33
from smmap.mman import *
44
from smmap.mman import WindowCursor
@@ -66,7 +66,7 @@ def test_memory_manager(self):
6666
man._collect_lru_region(10)
6767

6868
# doesn't fail if we overallocate
69-
assert man._collect_lru_region(sys.maxint) == 0
69+
assert man._collect_lru_region(sys.maxsize) == 0
7070

7171
# use a region, verify most basic functionality
7272
fc = FileCreator(self.k_window_test_size, "manager_test")
@@ -80,9 +80,9 @@ def test_memory_manager(self):
8080
assert c.buffer()[:] == open(fc.path, 'rb').read(20)[10:]
8181

8282
if isinstance(item, int):
83-
self.failUnlessRaises(ValueError, c.path)
83+
self.assertRaises(ValueError, c.path)
8484
else:
85-
self.failUnlessRaises(ValueError, c.fd)
85+
self.assertRaises(ValueError, c.fd)
8686
#END handle value error
8787
#END for each input
8888
os.close(fd)

smmap/test/test_tutorial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lib import TestBase
1+
from .lib import TestBase
22

33
class TestTutorial(TestBase):
44

smmap/test/test_util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lib import TestBase, FileCreator
1+
from .lib import TestBase, FileCreator
22

33
from smmap.util import *
44

@@ -38,15 +38,15 @@ def test_window(self):
3838
assert wc.ofs == 1 and wc.size == maxsize
3939

4040
# without maxsize
41-
wc.extend_right_to(wr, sys.maxint)
41+
wc.extend_right_to(wr, sys.maxsize)
4242
assert wc.ofs_end() == wr.ofs and wc.ofs == 1
4343

4444
# extend left
4545
wr.extend_left_to(wc2, maxsize)
4646
wr.extend_left_to(wc2, maxsize)
4747
assert wr.size == maxsize
4848

49-
wr.extend_left_to(wc2, sys.maxint)
49+
wr.extend_left_to(wc2, sys.maxsize)
5050
assert wr.ofs == wc2.ofs_end()
5151

5252
wc.align()
@@ -68,7 +68,7 @@ def test_region(self):
6868
assert rhalfsize.ofs_begin() == 0 and rhalfsize.size() == half_size
6969

7070
assert rfull.includes_ofs(0) and rfull.includes_ofs(fc.size-1) and rfull.includes_ofs(half_size)
71-
assert not rfull.includes_ofs(-1) and not rfull.includes_ofs(sys.maxint)
71+
assert not rfull.includes_ofs(-1) and not rfull.includes_ofs(sys.maxsize)
7272
# with the values we have, this test only works on windows where an alignment
7373
# size of 4096 is assumed.
7474
# We only test on linux as it is inconsitent between the python versions

smmap/util.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
#{ Utilities
2121

22+
def string_types():
23+
if sys.version_info[0] >= 3:
24+
return str
25+
else:
26+
return basestring
27+
28+
2229
def align_to_mmap(num, round_up):
2330
"""
2431
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):
3441

3542
def is_64_bit():
3643
""":return: True if the system is 64 bit. Otherwise it can be assumed to be 32 bit"""
37-
return sys.maxint > (1<<32) - 1
44+
return sys.maxsize > (1<<32) - 1
3845

3946
#}END utilities
4047

@@ -154,7 +161,7 @@ def __init__(self, path_or_fd, ofs, size, flags = 0):
154161
self._mfb = buffer(self._mf, ofs, self._size)
155162
#END handle buffer wrapping
156163
finally:
157-
if isinstance(path_or_fd, basestring):
164+
if isinstance(path_or_fd, string_types()):
158165
os.close(fd)
159166
#END only close it if we opened it
160167
#END close file handle
@@ -258,7 +265,7 @@ def path_or_fd(self):
258265
def file_size(self):
259266
""":return: size of file we manager"""
260267
if self._file_size is None:
261-
if isinstance(self._path_or_fd, basestring):
268+
if isinstance(self._path_or_fd, string_types()):
262269
self._file_size = os.stat(self._path_or_fd).st_size
263270
else:
264271
self._file_size = os.fstat(self._path_or_fd).st_size

0 commit comments

Comments
 (0)