Skip to content

Commit 12b655d

Browse files
committed
Merge pull request #12 from msabramo/buffer_memoryview_py3
Deal with lack of `buffer` in py3
2 parents f523a11 + 52fba74 commit 12b655d

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

smmap/mman.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
is_64_bit,
77
align_to_mmap,
88
string_types,
9+
buffer,
910
)
1011

1112
from weakref import ref

smmap/test/test_tutorial.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def test_example(self):
4444
# its recommended not to create big slices when feeding the buffer
4545
# into consumers (e.g. struct or zlib).
4646
# Instead, either give the buffer directly, or use pythons buffer command.
47+
from smmap.util import buffer
4748
buffer(c.buffer(), 1, 9) # first 9 bytes without copying them
4849

4950
# you can query absolute offsets, and check whether an offset is included

smmap/util.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@
1212
from mmap import PAGESIZE as ALLOCATIONGRANULARITY
1313
#END handle pythons missing quality assurance
1414

15-
__all__ = [ "align_to_mmap", "is_64_bit",
15+
__all__ = [ "align_to_mmap", "is_64_bit", "buffer",
1616
"MapWindow", "MapRegion", "MapRegionList", "ALLOCATIONGRANULARITY"]
1717

1818
#{ Utilities
1919

20+
try:
21+
# Python 2
22+
buffer = buffer
23+
except NameError:
24+
# Python 3 has no `buffer`; only `memoryview`
25+
def buffer(obj, offset, size):
26+
return memoryview(obj[offset:offset+size])
27+
28+
2029
def string_types():
2130
if sys.version_info[0] >= 3:
2231
return str

0 commit comments

Comments
 (0)