Skip to content

Commit d63cb85

Browse files
committed
Mmap module uses PosixSupportLibrary
1 parent 9c6f74b commit d63cb85

File tree

13 files changed

+1009
-587
lines changed

13 files changed

+1009
-587
lines changed

graalpython/com.oracle.graal.python.cext/posix/posix.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include <sys/utsname.h>
6363
#include <sys/wait.h>
6464
#include <sys/file.h>
65+
#include <sys/mman.h>
6566
#include <unistd.h>
6667

6768

@@ -505,6 +506,37 @@ int32_t call_system(const char *pathname) {
505506
return system(pathname);
506507
}
507508

509+
void *call_mmap(int64_t length, int32_t prot, int32_t flags, int32_t fd, int64_t offset) {
510+
void *result = mmap(NULL, length, prot, flags, fd, offset);
511+
return result == MAP_FAILED ? 0 : result;
512+
}
513+
514+
int32_t call_munmap(void* address, int64_t length) {
515+
return munmap(address, length);
516+
}
517+
518+
void call_msync(void* address, int64_t offset, int64_t length) {
519+
// TODO: can be generalized to also accept different flags,
520+
// but MS_SYNC and such seem to be defined to different values across systems
521+
msync(address + offset, length, MS_SYNC);
522+
}
523+
524+
int8_t read_byte(int8_t *address, int64_t index) {
525+
return address[index];
526+
}
527+
528+
void write_bytes(int8_t *address, int8_t* buffer, int64_t index, int32_t length) {
529+
for (int64_t i = 0; i < length; ++i) {
530+
address[index + i] = buffer[i];
531+
}
532+
}
533+
534+
void read_bytes(int8_t *address, int8_t* buffer, int64_t index, int32_t length) {
535+
for (int64_t i = 0; i < length; ++i) {
536+
buffer[i] = address[index + i];
537+
}
538+
}
539+
508540
int32_t get_errno() {
509541
return errno;
510542
}

graalpython/com.oracle.graal.python.test/src/tests/test_mmap.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22
# Copyright (C) 1996-2017 Python Software Foundation
33
#
44
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
5-
from test.support import (TESTFN, run_unittest, import_module, unlink,
6-
requires, _2G, _4G, gc_collect, cpython_only)
7-
import unittest
85
import os
96
import re
10-
import itertools
11-
import socket
12-
import sys
13-
import weakref
7+
import unittest
8+
from test.support import (TESTFN, run_unittest, import_module)
149

1510
# Skip test if we can't import mmap.
1611
mmap = import_module('mmap')
@@ -179,6 +174,38 @@ def test_context_manager_exception(self):
179174
self.assertTrue(m.closed, "context manager failed")
180175

181176

177+
FIND_BUFFER_SIZE = 1024 # keep in sync with FindNode#BUFFER_SIZE
178+
def test_find():
179+
cases = [
180+
# (size, needle_pos)
181+
(FIND_BUFFER_SIZE * 3 + 1, FIND_BUFFER_SIZE * 3 - 2),
182+
(FIND_BUFFER_SIZE * 3 + 3, FIND_BUFFER_SIZE * 3),
183+
(FIND_BUFFER_SIZE * 2, FIND_BUFFER_SIZE),
184+
(FIND_BUFFER_SIZE * 2, FIND_BUFFER_SIZE - 1),
185+
(11, 1),
186+
]
187+
for (size, needle_pos) in cases:
188+
m = mmap.mmap(-1, size)
189+
m[needle_pos] = b'a'[0]
190+
m[needle_pos + 1] = b'b'[0]
191+
m[needle_pos + 2] = b'c'[0]
192+
assert m.find(b'abc') == needle_pos
193+
assert m.find(b'abc', 0, needle_pos) == -1
194+
assert m.find(b'abc', 0, needle_pos+2) == -1
195+
assert m.find(b'abc', 0, needle_pos+3) == needle_pos
196+
assert m.find(b'abc', needle_pos) == needle_pos
197+
assert m.find(b'abc', needle_pos+1) == -1
198+
assert m.find(b'abc', needle_pos-1) == needle_pos
199+
m.close()
200+
201+
202+
def test_getitem():
203+
m = mmap.mmap(-1, 12)
204+
for i in range(0, 12):
205+
m[i] = i
206+
assert m[slice(-10, 100)] == b'\x02\x03\x04\x05\x06\x07\x08\t\n\x0b'
207+
208+
182209

183210
def test_main():
184211
#run_unittest(MmapTests, LargeMmapTests)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
*graalpython.lib-python.3.test.test_mmap.LargeMmapTests.test_around_2GB
22
*graalpython.lib-python.3.test.test_mmap.LargeMmapTests.test_around_4GB
3+
*graalpython.lib-python.3.test.test_mmap.LargeMmapTests.test_large_offset
34
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_anonymous
45
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_bad_file_desc
6+
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_basic
57
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_concat_repeat_exception
68
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_context_manager
79
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_context_manager_exception
810
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_crasher_on_windows
911
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_double_close
12+
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_empty_file
1013
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_entire_file
1114
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_find_end
1215
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_invalid_descriptor
16+
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_length_0_large_offset
17+
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_length_0_offset
1318
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_madvise
19+
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_prot_readonly
1420
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_read_all
1521
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_read_invalid_arg
22+
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_resize_past_pos
1623
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_sizeof
1724
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_subclass
1825
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_tagname
26+
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_tougher_find
1927
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_weakref
2028
*graalpython.lib-python.3.test.test_mmap.MmapTests.test_write_returning_the_number_of_bytes_written

0 commit comments

Comments
 (0)