Debian Codegraph @

rpython/rlib/test/test_rmmap.py

raw symbols SCIP
1 from rpython.tool.udir import udir
2 import os, sys, pytest
3 from rpython.rtyper.test.test_llinterp import interpret
4 from rpython.rtyper.lltypesystem import rffi, lltype
5 from rpython.rlib.rarithmetic import intmask
6 from rpython.rlib import rmmap as mmap
7 from rpython.rlib.rmmap import RTypeError, RValueError, alloc, free
8 from rpython.rlib.rmmap import madvise_free
9
10
11 class TestMMap:
12 def setup_class(cls):
13 cls.tmpname = str(udir.join('mmap-'))
14
15 def test_page_size(self):
16 def f():
17 assert mmap.PAGESIZE > 0
18 assert isinstance(mmap.PAGESIZE, int)
19
20 interpret(f, [])
21
22 def test_attributes(self):
23 def f():
24 assert isinstance(mmap.ACCESS_READ, int)
25 assert isinstance(mmap.ACCESS_WRITE, int)
26 assert isinstance(mmap.ACCESS_COPY, int)
27 if os.name == "posix":
28 assert isinstance(mmap.MAP_ANON, int)
29 assert isinstance(mmap.MAP_ANONYMOUS, int)
30 assert isinstance(mmap.MAP_PRIVATE, int)
31 assert isinstance(mmap.MAP_SHARED, int)
32 assert isinstance(mmap.PROT_EXEC, int)
33 assert isinstance(mmap.PROT_READ, int)
34 assert isinstance(mmap.PROT_WRITE, int)
35
36 interpret(f, [])
37
38 def test_file_size(self):
39 def func(no):
40
41 try:
42 mmap.mmap(no, 123)
43 except RValueError:
44 pass
45 else:
46 raise Exception("didn't raise")
47
48 f = open(self.tmpname + "a", "w+")
49
50 f.write("c")
51 f.flush()
52
53 interpret(func, [f.fileno()])
54 f.close()
55
56 def test_create(self):
57 f = open(self.tmpname + "b", "w+")
58
59 f.write("c")
60 f.flush()
61
62 def func(no):
63 m = mmap.mmap(no, 1)
64 assert m.read(99) == "c"
65
66 interpret(func, [f.fileno()])
67
68 f.close()
69
70 @pytest.mark.skipif("os.name != 'posix'")
71 def test_unmap_range(self):
72 f = open(self.tmpname + "-unmap-range", "w+")
73 left, right, size = 100, 200, 500 # in pages
74
75 f.write(size*4096*"c")
76 f.flush()
77
78 def func(no):
79 m = mmap.mmap(no, size*4096)
80 m.unmap_range(left*4096, (right-left)*4096)
81 m.read(1)
82 m.seek(right*4096)
83 m.read(1)
84
85 def in_map(m, offset):
86 return rffi.ptradd(m.data, offset)
87 def as_num(ptr):
88 return rffi.cast(lltype.Unsigned, ptr)
89 res = mmap.alloc_hinted(in_map(m, (left+right)/2 * 4096), 4096)
90 assert as_num(in_map(m, left*4096)) <= as_num(res) < as_num(in_map(m, right*4096))
91 interpret(func, [f.fileno()])
92 f.close()
93
94 def test_close(self):
95 f = open(self.tmpname + "c", "w+")
96
97 f.write("c")
98 f.flush()
99
100 def func(no):
101 m = mmap.mmap(no, 1)
102 m.close()
103 try:
104 m.check_valid()
105 except RValueError:
106 pass
107 else:
108 raise Exception("Did not raise")
109 interpret(func, [f.fileno()])
110 f.close()
111
112 def test_read_byte(self):
113 f = open(self.tmpname + "d", "w+")
114
115 f.write("c")
116 f.flush()
117
118 def func(no):
119 m = mmap.mmap(no, 1)
120 assert m.read_byte() == "c"
121 try:
122 m.read_byte()
123 except RValueError:
124 pass
125 else:
126 raise Exception("Did not raise")
127 m.close()
128 interpret(func, [f.fileno()])
129 f.close()
130
131 def test_readline(self):
132 import os
133 f = open(self.tmpname + "e", "w+")
134
135 f.write("foo\n")
136 f.flush()
137
138 def func(no):
139 m = mmap.mmap(no, 4)
140 if os.name == "nt":
141 # windows replaces \n with \r. it's time to change to \n only MS!
142 assert m.readline() == "foo\r"
143 elif os.name == "posix":
144 assert m.readline() == "foo\n"
145 assert m.readline() == ""
146 m.close()
147
148 interpret(func, [f.fileno()])
149 f.close()
150
151 def test_read(self):
152 f = open(self.tmpname + "f", "w+")
153
154 f.write("foobar")
155 f.flush()
156
157 def func(no):
158 m = mmap.mmap(no, 6)
159 assert m.read(1) == "f"
160 assert m.read(6) == "oobar"
161 assert m.read(1) == ""
162 m.close()
163
164 interpret(func, [f.fileno()])
165 f.close()
166
167 def test_find_rfind(self):
168 f = open(self.tmpname + "g", "w+")
169 f.write("foobarfoobar\0")
170 f.flush()
171 m = mmap.mmap(f.fileno(), 13)
172
173 for s1 in range(-20, 20):
174 for e1 in range(-20, 20):
175 expected = "foobarfoobar\0".find("ob", s1, e1)
176 assert m.find("ob", s1, e1, False) == expected
177 expected = "foobarfoobar\0".rfind("ob", s1, e1)
178 assert m.find("ob", s1, e1, True) == expected
179
180 m.close()
181 f.close()
182
183 def test_find(self):
184 f = open(self.tmpname + "g", "w+")
185 f.write("foobarfoobar\0")
186 f.flush()
187
188 def func(no):
189 m = mmap.mmap(no, 12)
190 assert m.find("\0", 0, 13) == -1 # no searching past the stop
191 assert m.find("\0", 0, 13, True) == -1
192 m.close()
193 #
194 m = mmap.mmap(no, 13)
195 assert m.find("b", 0, 7) == 3
196 assert m.find("z", 0, 7) == -1
197 assert m.find("o", 11, 13) == -1
198 assert m.find("ob", 0, 7) == 2
199 assert m.find("\0", 0, 13) == 12
200 assert m.find("o", 1, 4) == 1
201 assert m.find("o", 2, 4) == 2
202 assert m.find("o", 2, -4) == 2
203 assert m.find("o", 8, -5) == -1
204 m.close()
205
206 func(f.fileno())
207 interpret(func, [f.fileno()])
208 f.close()
209
210 def test_is_modifiable(self):
211 f = open(self.tmpname + "h", "w+")
212
213 f.write("foobar")
214 f.flush()
215
216 def func(no):
217 m = mmap.mmap(no, 6, access=mmap.ACCESS_READ)
218 try:
219 m.check_writeable()
220 except RTypeError:
221 pass
222 else:
223 assert False
224 try:
225 m.check_resizeable()
226 except RTypeError:
227 pass
228 else:
229 assert False
230 m.close()
231 interpret(func, [f.fileno()])
232 f.close()
233
234 def test_seek(self):
235 f = open(self.tmpname + "i", "w+")
236
237 f.write("foobar")
238 f.flush()
239
240 def func(no):
241 m = mmap.mmap(no, 6)
242 m.seek(0)
243 assert m.tell() == 0
244 m.read(1)
245 m.seek(1, 1)
246 assert m.tell() == 2
247 m.seek(0)
248 m.seek(-1, 2)
249 assert m.tell() == 5
250 m.close()
251 interpret(func, [f.fileno()])
252 f.close()
253
254 def test_write(self):
255 f = open(self.tmpname + "j", "w+")
256
257 f.write("foobar")
258 f.flush()
259 def func(no):
260 m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE)
261 assert m.write("ciao\n") == 5
262 m.seek(0)
263 assert m.read(6) == "ciao\nr"
264 m.close()
265 interpret(func, [f.fileno()])
266 f.close()
267
268 def test_write_byte(self):
269 f = open(self.tmpname + "k", "w+")
270
271 f.write("foobar")
272 f.flush()
273
274 def func(no):
275 m = mmap.mmap(no, 6, access=mmap.ACCESS_READ)
276 m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE)
277 m.write_byte("x")
278 m.seek(0)
279 assert m.read(6) == "xoobar"
280 m.close()
281 interpret(func, [f.fileno()])
282 f.close()
283
284 def test_write_readonly(self):
285 if os.name == "nt":
286 pytest.skip("Needs PROT_READ")
287 f = open(self.tmpname + "l", "w+")
288 f.write("foobar")
289 f.flush()
290 m = mmap.mmap(f.fileno(), 6, prot=mmap.PROT_READ)
291 pytest.raises(RTypeError, m.check_writeable)
292 m.close()
293 f.close()
294
295 def test_write_without_protwrite(self):
296 if sys.platform in ("win32", "darwin"):
297 pytest.skip("Needs PROT_WRITE")
298 f = open(self.tmpname + "l2", "w+")
299 f.write("foobar")
300 f.flush()
301 m = mmap.mmap(f.fileno(), 6, prot=mmap.PROT_READ|mmap.PROT_EXEC)
302 pytest.raises(RTypeError, m.check_writeable)
303 pytest.raises(RTypeError, m.check_writeable)
304 m.close()
305 f.close()
306
307 def test_size(self):
308 f = open(self.tmpname + "l3", "w+")
309
310 f.write("foobar")
311 f.flush()
312
313 def func(no):
314 m = mmap.mmap(no, 5)
315 assert m.file_size() == 6 # size of the underline file, not the mmap
316 m.close()
317
318 interpret(func, [f.fileno()])
319 f.close()
320
321 def test_tell(self):
322 f = open(self.tmpname + "m", "w+")
323
324 f.write("c")
325 f.flush()
326
327 def func(no):
328 m = mmap.mmap(no, 1)
329 assert m.tell() >= 0
330 m.close()
331
332 interpret(func, [f.fileno()])
333 f.close()
334
335 def test_move(self):
336 f = open(self.tmpname + "o", "w+")
337
338 f.write("foobar")
339 f.flush()
340
341 def func(no):
342 m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE)
343 m.move(1, 3, 3)
344 assert m.read(6) == "fbarar"
345 m.seek(0)
346 m.move(1, 3, 2)
347 a = m.read(6)
348 assert a == "frarar"
349 m.close()
350
351 interpret(func, [f.fileno()])
352 f.close()
353
354 def test_resize(self):
355 if ("darwin" in sys.platform) or ("freebsd" in sys.platform):
356 pytest.skip("resize does not work under OSX or FreeBSD")
357
358 import os
359
360 f = open(self.tmpname + "p", "w+")
361 f.write("foobar")
362 f.flush()
363
364 def func(no):
365 m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE)
366 f_size = os.fstat(no).st_size
367 assert intmask(m.file_size()) == f_size == 6
368 m.resize(10)
369 f_size = os.fstat(no).st_size
370 assert intmask(m.file_size()) == f_size == 10
371 m.close()
372
373 interpret(func, [f.fileno()])
374 f.close()
375
376 def test_len(self):
377
378 f = open(self.tmpname + "q", "w+")
379 f.write("foobar")
380 f.flush()
381
382 def func(no):
383 m = mmap.mmap(no, 6)
384 assert m.len() == 6
385 m.close()
386
387 interpret(func, [f.fileno()])
388 f.close()
389
390 def test_get_item(self):
391
392 f = open(self.tmpname + "r", "w+")
393 f.write("foobar")
394 f.flush()
395
396 def func(no):
397 m = mmap.mmap(no, 6)
398 assert m.getitem(0) == 'f'
399 assert m.getitem(-1) == 'r'
400 # sl = slice(1, 2)
401 # assert m.get_item(sl) == 'o'
402 m.close()
403
404 interpret(func, [f.fileno()])
405 f.close()
406
407 def test_set_item(self):
408 f = open(self.tmpname + "s", "w+")
409 f.write("foobar")
410 f.flush()
411
412 def func(no):
413 m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE)
414
415 # def f(m): m[1:3] = u'xx'
416 # pytest.raises(IndexError, f, m)
417 # def f(m): m[1:4] = "zz"
418 # pytest.raises(IndexError, f, m)
419 # def f(m): m[1:6] = "z" * 6
420 # pytest.raises(IndexError, f, m)
421 # def f(m): m[:2] = "z" * 5
422 # m[1:3] = 'xx'
423 # assert m.read(6) == "fxxbar"
424 # m.seek(0)
425 m.setitem(0, 'x')
426 assert m.getitem(0) == 'x'
427 m.setitem(-6, 'y')
428 data = m.read(6)
429 assert data == "yoobar" # yxxbar with slice's stuff
430 m.close()
431
432 interpret(func, [f.fileno()])
433 f.close()
434
435 def test_double_close(self):
436 f = open(self.tmpname + "s", "w+")
437 f.write("foobar")
438 f.flush()
439
440 def func(no):
441 m = mmap.mmap(no, 6, access=mmap.ACCESS_WRITE)
442 m.close()
443 m.close() # didn't explode
444
445 interpret(func, [f.fileno()])
446 f.close()
447
448 def test_translated(self):
449 from rpython.translator.c.test.test_genc import compile
450
451 def func(no):
452 m = mmap.mmap(no, 1)
453 r = m.read_byte()
454 m.close()
455 return r
456
457 compile(func, [int], gcpolicy='boehm')
458
459 @pytest.mark.skipif("not mmap.has_madvise")
460 def test_translated_madvise_bug(self):
461 from rpython.translator.c.test.test_genc import compile
462
463 def func():
464 m = mmap.mmap(-1, 8096)
465 m.madvise(mmap.MADV_NORMAL, 0, 8096)
466 m.close()
467
468 compile(func, [], gcpolicy='boehm')
469
470 def test_windows_crasher_1(self):
471 if sys.platform != "win32":
472 pytest.skip("Windows-only test")
473 def func():
474 m = mmap.mmap(-1, 1000, tagname="foo")
475 # same tagname, but larger size
476 try:
477 m2 = mmap.mmap(-1, 5000, tagname="foo")
478 m2.getitem(4500)
479 except WindowsError:
480 pass
481 m.close()
482 interpret(func, [])
483
484 def test_windows_crasher_2(self):
485 if sys.platform != "win32":
486 pytest.skip("Windows-only test")
487
488 f = open(self.tmpname + "t", "w+")
489 f.write("foobar")
490 f.flush()
491
492 f = open(self.tmpname + "t", "r+b")
493 m = mmap.mmap(f.fileno(), 0)
494 f.close()
495 pytest.raises(WindowsError, m.resize, 0)
496 pytest.raises(RValueError, m.getitem, 0)
497 m.close()
498
499 @pytest.mark.skipif("not mmap.has_madvise")
500 def test_madvise(self):
501 m = mmap.mmap(-1, 8096)
502 m.madvise(mmap.MADV_NORMAL, 0, 8096)
503 m.close()
504
505
506 @pytest.mark.skipif("sys.platform == 'darwin'", reason="needs entitlement")
507 def test_alloc_free():
508 map_size = 65536
509 data = alloc(map_size)
510
511 for i in range(0, map_size, 171):
512 data[i] = chr(i & 0xff)
513 for i in range(0, map_size, 171):
514 assert data[i] == chr(i & 0xff)
515 madvise_free(data, map_size)
516 free(data, map_size)
517
518 @pytest.mark.skipif("sys.platform == 'darwin'", reason="needs entitlement")
519 def test_compile_alloc_free():
520 from rpython.translator.c.test.test_genc import compile
521
522 fn = compile(test_alloc_free, [], gcpolicy='boehm')
523 fn()