|
7 | 7 | import os |
8 | 8 | import types |
9 | 9 |
|
| 10 | +try: |
| 11 | + import _testcapi |
| 12 | +except ImportError: |
| 13 | + _testcapi = None |
| 14 | + |
10 | 15 | class HelperMixin: |
11 | 16 | def helper(self, sample, *extra): |
12 | 17 | new = marshal.loads(marshal.dumps(sample, *extra)) |
@@ -434,18 +439,88 @@ def testNoIntern(self): |
434 | 439 | s2 = sys.intern(s) |
435 | 440 | self.assertNotEqual(id(s2), id(s)) |
436 | 441 |
|
| 442 | +@support.cpython_only |
| 443 | +@unittest.skipUnless(_testcapi, 'requires _testcapi') |
| 444 | +class CAPI_TestCase(unittest.TestCase, HelperMixin): |
| 445 | + |
| 446 | + def test_write_long_to_file(self): |
| 447 | + for v in range(marshal.version + 1): |
| 448 | + _testcapi.pymarshal_write_long_to_file(0x12345678, support.TESTFN, v) |
| 449 | + with open(support.TESTFN, 'rb') as f: |
| 450 | + data = f.read() |
| 451 | + support.unlink(support.TESTFN) |
| 452 | + self.assertEqual(data, b'\x78\x56\x34\x12') |
| 453 | + |
| 454 | + def test_write_object_to_file(self): |
| 455 | + obj = ('\u20ac', b'abc', 123, 45.6, 7+8j, 'long line '*1000) |
| 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) |
437 | 523 |
|
438 | | -def test_main(): |
439 | | - support.run_unittest(IntTestCase, |
440 | | - FloatTestCase, |
441 | | - StringTestCase, |
442 | | - CodeTestCase, |
443 | | - ContainerTestCase, |
444 | | - ExceptionTestCase, |
445 | | - BufferTestCase, |
446 | | - BugsTestCase, |
447 | | - LargeValuesTestCase, |
448 | | - ) |
449 | 524 |
|
450 | 525 | if __name__ == "__main__": |
451 | | - test_main() |
| 526 | + unittest.main() |
0 commit comments