Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(159234)

Side by Side Diff: Lib/test/test_mmap.py

Issue 23001: Accept mutable bytes-like objects
Patch Set: Created 4 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 from test.support import (TESTFN, run_unittest, import_module, unlink, 1 from test.support import (TESTFN, run_unittest, import_module, unlink,
2 requires, _2G, _4G, gc_collect, cpython_only) 2 requires, _2G, _4G, gc_collect, cpython_only)
3 import unittest 3 import unittest
4 import os 4 import os
5 import re 5 import re
6 import itertools 6 import itertools
7 import socket 7 import socket
8 import sys 8 import sys
9 import weakref 9 import weakref
10 10
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 f.flush() 275 f.flush()
276 m = mmap.mmap(f.fileno(), n) 276 m = mmap.mmap(f.fileno(), n)
277 f.close() 277 f.close()
278 278
279 self.assertEqual(m.find(b'one'), 0) 279 self.assertEqual(m.find(b'one'), 0)
280 self.assertEqual(m.find(b'ones'), 8) 280 self.assertEqual(m.find(b'ones'), 8)
281 self.assertEqual(m.find(b'one', 0, -1), 0) 281 self.assertEqual(m.find(b'one', 0, -1), 0)
282 self.assertEqual(m.find(b'one', 1), 8) 282 self.assertEqual(m.find(b'one', 1), 8)
283 self.assertEqual(m.find(b'one', 1, -1), 8) 283 self.assertEqual(m.find(b'one', 1, -1), 8)
284 self.assertEqual(m.find(b'one', 1, -2), -1) 284 self.assertEqual(m.find(b'one', 1, -2), -1)
285 self.assertEqual(m.find(bytearray(b'one')), 0)
285 286
286 287
287 def test_rfind(self): 288 def test_rfind(self):
288 # test the new 'end' parameter works as expected 289 # test the new 'end' parameter works as expected
289 f = open(TESTFN, 'wb+') 290 f = open(TESTFN, 'wb+')
290 data = b'one two ones' 291 data = b'one two ones'
291 n = len(data) 292 n = len(data)
292 f.write(data) 293 f.write(data)
293 f.flush() 294 f.flush()
294 m = mmap.mmap(f.fileno(), n) 295 m = mmap.mmap(f.fileno(), n)
295 f.close() 296 f.close()
296 297
297 self.assertEqual(m.rfind(b'one'), 8) 298 self.assertEqual(m.rfind(b'one'), 8)
298 self.assertEqual(m.rfind(b'one '), 0) 299 self.assertEqual(m.rfind(b'one '), 0)
299 self.assertEqual(m.rfind(b'one', 0, -1), 8) 300 self.assertEqual(m.rfind(b'one', 0, -1), 8)
300 self.assertEqual(m.rfind(b'one', 0, -2), 0) 301 self.assertEqual(m.rfind(b'one', 0, -2), 0)
301 self.assertEqual(m.rfind(b'one', 1, -1), 8) 302 self.assertEqual(m.rfind(b'one', 1, -1), 8)
302 self.assertEqual(m.rfind(b'one', 1, -2), -1) 303 self.assertEqual(m.rfind(b'one', 1, -2), -1)
304 self.assertEqual(m.rfind(bytearray(b'one')), 8)
303 305
304 306
305 def test_double_close(self): 307 def test_double_close(self):
306 # make sure a double close doesn't crash on Solaris (Bug# 665913) 308 # make sure a double close doesn't crash on Solaris (Bug# 665913)
307 f = open(TESTFN, 'wb+') 309 f = open(TESTFN, 'wb+')
308 310
309 f.write(2**16 * b'a') # Arbitrary character 311 f.write(2**16 * b'a') # Arbitrary character
310 f.close() 312 f.close()
311 313
312 f = open(TESTFN, 'rb') 314 f = open(TESTFN, 'rb')
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 self.assertRaises(ValueError, m.read_byte) 596 self.assertRaises(ValueError, m.read_byte)
595 # Test read() 597 # Test read()
596 m.seek(3) 598 m.seek(3)
597 self.assertEqual(m.read(3), b"345") 599 self.assertEqual(m.read(3), b"345")
598 self.assertEqual(m.tell(), 6) 600 self.assertEqual(m.tell(), 6)
599 # Test write() 601 # Test write()
600 m.seek(3) 602 m.seek(3)
601 m.write(b"bar") 603 m.write(b"bar")
602 self.assertEqual(m.tell(), 6) 604 self.assertEqual(m.tell(), 6)
603 self.assertEqual(m[:], b"012bar6789") 605 self.assertEqual(m[:], b"012bar6789")
604 m.seek(8) 606 m.write(bytearray(b"baz"))
605 self.assertRaises(ValueError, m.write, b"bar") 607 self.assertEqual(m.tell(), 9)
608 self.assertEqual(m[:], b"012barbaz9")
609 self.assertRaises(ValueError, m.write, b"ba")
606 610
607 def test_non_ascii_byte(self): 611 def test_non_ascii_byte(self):
608 for b in (129, 200, 255): # > 128 612 for b in (129, 200, 255): # > 128
609 m = mmap.mmap(-1, 1) 613 m = mmap.mmap(-1, 1)
610 m.write_byte(b) 614 m.write_byte(b)
611 self.assertEqual(m[0], b) 615 self.assertEqual(m[0], b)
612 m.seek(0) 616 m.seek(0)
613 self.assertEqual(m.read_byte(), b) 617 self.assertEqual(m.read_byte(), b)
614 m.close() 618 m.close()
615 619
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 @unittest.skipUnless(sys.maxsize > _4G, "test cannot run on 32-bit systems") 769 @unittest.skipUnless(sys.maxsize > _4G, "test cannot run on 32-bit systems")
766 def test_around_4GB(self): 770 def test_around_4GB(self):
767 self._test_around_boundary(_4G) 771 self._test_around_boundary(_4G)
768 772
769 773
770 def test_main(): 774 def test_main():
771 run_unittest(MmapTests, LargeMmapTests) 775 run_unittest(MmapTests, LargeMmapTests)
772 776
773 if __name__ == '__main__': 777 if __name__ == '__main__':
774 test_main() 778 test_main()
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+