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

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

Issue 23392: Add tests for marshal FILE* API
Patch Set: Created 4 years, 5 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
« no previous file with comments | « no previous file | Modules/_testcapimodule.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 from test import support 1 from test import support
2 import array 2 import array
3 import io 3 import io
4 import marshal 4 import marshal
5 import sys 5 import sys
6 import unittest 6 import unittest
7 import os 7 import os
8 import types 8 import types
9
10 try:
11 import _testcapi
12 except ImportError:
13 _testcapi = None
9 14
10 class HelperMixin: 15 class HelperMixin:
11 def helper(self, sample, *extra): 16 def helper(self, sample, *extra):
12 new = marshal.loads(marshal.dumps(sample, *extra)) 17 new = marshal.loads(marshal.dumps(sample, *extra))
13 self.assertEqual(sample, new) 18 self.assertEqual(sample, new)
14 try: 19 try:
15 with open(support.TESTFN, "wb") as f: 20 with open(support.TESTFN, "wb") as f:
16 marshal.dump(sample, f, *extra) 21 marshal.dump(sample, f, *extra)
17 with open(support.TESTFN, "rb") as f: 22 with open(support.TESTFN, "rb") as f:
18 new = marshal.load(f) 23 new = marshal.load(f)
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 s2 = sys.intern(s) 432 s2 = sys.intern(s)
428 self.assertEqual(id(s2), id(s)) 433 self.assertEqual(id(s2), id(s))
429 434
430 def testNoIntern(self): 435 def testNoIntern(self):
431 s = marshal.loads(marshal.dumps(self.strobj, 2)) 436 s = marshal.loads(marshal.dumps(self.strobj, 2))
432 self.assertEqual(s, self.strobj) 437 self.assertEqual(s, self.strobj)
433 self.assertNotEqual(id(s), id(self.strobj)) 438 self.assertNotEqual(id(s), id(self.strobj))
434 s2 = sys.intern(s) 439 s2 = sys.intern(s)
435 self.assertNotEqual(id(s2), id(s)) 440 self.assertNotEqual(id(s2), id(s))
436 441
442 @support.cpython_only
443 @unittest.skipUnless(_testcapi, 'requires _testcapi')
444 class CFILE_TestCase(unittest.TestCase, HelperMixin):
437 445
438 def test_main(): 446 def test_write_long_to_file(self):
439 support.run_unittest(IntTestCase, 447 for v in range(marshal.version + 1):
440 FloatTestCase, 448 _testcapi.pymarshal_write_long_to_file(0x12345678, support.TESTFN, v )
441 StringTestCase, 449 with open(support.TESTFN, 'rb') as f:
442 CodeTestCase, 450 data = f.read()
443 ContainerTestCase, 451 support.unlink(support.TESTFN)
444 ExceptionTestCase, 452 self.assertEqual(data, b'\x78\x56\x34\x12')
445 BufferTestCase, 453
446 BugsTestCase, 454 def test_write_object_to_file(self):
447 LargeValuesTestCase, 455 obj = ('\u20ac', b'abc', 123, 45.6, 7+8j, 'long line '*1000)
448 ) 456 for v in range(marshal.version + 1):
457 _testcapi.pymarshal_write_object_to_file(obj, support.TESTFN, v)
458 with open(support.TESTFN, 'rb') as f:
459 data = f.read()
460 support.unlink(support.TESTFN)
461 self.assertEqual(marshal.loads(data), obj)
462
463 def test_read_short_from_file(self):
464 with open(support.TESTFN, 'wb') as f:
465 f.write(b'\x34\x12xxxx')
466 r, p = _testcapi.pymarshal_read_short_from_file(support.TESTFN)
467 support.unlink(support.TESTFN)
468 self.assertEqual(r, 0x1234)
469 self.assertEqual(p, 2)
470
471 with open(support.TESTFN, 'wb') as f:
472 f.write(b'\x12')
473 with self.assertRaises(EOFError):
474 _testcapi.pymarshal_read_short_from_file(support.TESTFN)
475 support.unlink(support.TESTFN)
476
477 def test_read_long_from_file(self):
478 with open(support.TESTFN, 'wb') as f:
479 f.write(b'\x78\x56\x34\x12xxxx')
480 r, p = _testcapi.pymarshal_read_long_from_file(support.TESTFN)
481 support.unlink(support.TESTFN)
482 self.assertEqual(r, 0x12345678)
483 self.assertEqual(p, 4)
484
485 with open(support.TESTFN, 'wb') as f:
486 f.write(b'\x56\x34\x12')
487 with self.assertRaises(EOFError):
488 _testcapi.pymarshal_read_long_from_file(support.TESTFN)
489 support.unlink(support.TESTFN)
490
491 def test_read_last_object_from_file(self):
492 obj = ('\u20ac', b'abc', 123, 45.6, 7+8j)
493 for v in range(marshal.version + 1):
494 data = marshal.dumps(obj, v)
495 with open(support.TESTFN, 'wb') as f:
496 f.write(data + b'xxxx')
497 r, p = _testcapi.pymarshal_read_last_object_from_file(support.TESTFN )
498 support.unlink(support.TESTFN)
499 self.assertEqual(r, obj)
500
501 with open(support.TESTFN, 'wb') as f:
502 f.write(data[:1])
503 with self.assertRaises(EOFError):
504 _testcapi.pymarshal_read_last_object_from_file(support.TESTFN)
505 support.unlink(support.TESTFN)
506
507 def test_read_object_from_file(self):
508 obj = ('\u20ac', b'abc', 123, 45.6, 7+8j)
509 for v in range(marshal.version + 1):
510 data = marshal.dumps(obj, v)
511 with open(support.TESTFN, 'wb') as f:
512 f.write(data + b'xxxx')
513 r, p = _testcapi.pymarshal_read_object_from_file(support.TESTFN)
514 support.unlink(support.TESTFN)
515 self.assertEqual(r, obj)
516 self.assertEqual(p, len(data))
517
518 with open(support.TESTFN, 'wb') as f:
519 f.write(data[:1])
520 with self.assertRaises(EOFError):
521 _testcapi.pymarshal_read_object_from_file(support.TESTFN)
522 support.unlink(support.TESTFN)
523
449 524
450 if __name__ == "__main__": 525 if __name__ == "__main__":
451 test_main() 526 unittest.main()
OLDNEW
« no previous file with comments | « no previous file | Modules/_testcapimodule.c » ('j') | no next file with comments »

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