|
| 1 | +import marshal |
| 2 | +import os.path |
| 3 | +import unittest |
| 4 | + |
| 5 | +from test import support |
| 6 | +from test.support import import_helper |
| 7 | +from test.support import os_helper |
| 8 | +from test.test_marshal import HelperMixin, omit_last_byte |
| 9 | + |
| 10 | + |
| 11 | +# Skip this test if _testcapi is are not available. |
| 12 | +_testcapi = import_helper.import_module('_testcapi') |
| 13 | + |
| 14 | + |
| 15 | +@support.cpython_only |
| 16 | +class CAPI_TestCase(unittest.TestCase, HelperMixin): |
| 17 | + |
| 18 | + def test_read_from_file_error(self): |
| 19 | + # A read error is reported as OSError, not EOFError. |
| 20 | + # A directory cannot be read (on some platforms it cannot even |
| 21 | + # be opened, which is reported as OSError as well). |
| 22 | + os.mkdir(os_helper.TESTFN) |
| 23 | + self.addCleanup(os_helper.rmdir, os_helper.TESTFN) |
| 24 | + for func in (_testcapi.pymarshal_read_short_from_file, |
| 25 | + _testcapi.pymarshal_read_long_from_file, |
| 26 | + _testcapi.pymarshal_read_object_from_file, |
| 27 | + _testcapi.pymarshal_read_last_object_from_file): |
| 28 | + with self.subTest(func=func.__name__): |
| 29 | + self.assertRaises(OSError, func, os_helper.TESTFN) |
| 30 | + |
| 31 | + @unittest.skipUnless(os.path.exists('/dev/full'), 'requires /dev/full') |
| 32 | + def test_write_to_file_error(self): |
| 33 | + # A write error is reported as OSError. |
| 34 | + # The data is large enough to not fit in the stdio buffer, so that |
| 35 | + # the error is detected before the file is closed. |
| 36 | + obj = b'x' * 100000 |
| 37 | + with self.assertRaises(OSError): |
| 38 | + _testcapi.pymarshal_write_object_to_file(obj, '/dev/full', |
| 39 | + marshal.version) |
| 40 | + |
| 41 | + def test_write_unmarshallable_to_file(self): |
| 42 | + self.addCleanup(os_helper.unlink, os_helper.TESTFN) |
| 43 | + with self.assertRaisesRegex(ValueError, 'unmarshallable object'): |
| 44 | + _testcapi.pymarshal_write_object_to_file(object(), os_helper.TESTFN, |
| 45 | + marshal.version) |
| 46 | + |
| 47 | + def test_write_long_to_file(self): |
| 48 | + for v in range(marshal.version + 1): |
| 49 | + _testcapi.pymarshal_write_long_to_file(0x12345678, os_helper.TESTFN, v) |
| 50 | + with open(os_helper.TESTFN, 'rb') as f: |
| 51 | + data = f.read() |
| 52 | + os_helper.unlink(os_helper.TESTFN) |
| 53 | + self.assertEqual(data, b'\x78\x56\x34\x12') |
| 54 | + |
| 55 | + def test_write_object_to_file(self): |
| 56 | + obj = ('\u20ac', b'abc', 123, 45.6, 7+8j, 'long line '*1000) |
| 57 | + for v in range(marshal.version + 1): |
| 58 | + _testcapi.pymarshal_write_object_to_file(obj, os_helper.TESTFN, v) |
| 59 | + with open(os_helper.TESTFN, 'rb') as f: |
| 60 | + data = f.read() |
| 61 | + os_helper.unlink(os_helper.TESTFN) |
| 62 | + self.assertEqual(marshal.loads(data), obj) |
| 63 | + |
| 64 | + def test_read_short_from_file(self): |
| 65 | + with open(os_helper.TESTFN, 'wb') as f: |
| 66 | + f.write(b'\x34\x12xxxx') |
| 67 | + r, p = _testcapi.pymarshal_read_short_from_file(os_helper.TESTFN) |
| 68 | + os_helper.unlink(os_helper.TESTFN) |
| 69 | + self.assertEqual(r, 0x1234) |
| 70 | + self.assertEqual(p, 2) |
| 71 | + |
| 72 | + with open(os_helper.TESTFN, 'wb') as f: |
| 73 | + f.write(b'\x12') |
| 74 | + with self.assertRaises(EOFError): |
| 75 | + _testcapi.pymarshal_read_short_from_file(os_helper.TESTFN) |
| 76 | + os_helper.unlink(os_helper.TESTFN) |
| 77 | + |
| 78 | + def test_read_long_from_file(self): |
| 79 | + with open(os_helper.TESTFN, 'wb') as f: |
| 80 | + f.write(b'\x78\x56\x34\x12xxxx') |
| 81 | + r, p = _testcapi.pymarshal_read_long_from_file(os_helper.TESTFN) |
| 82 | + os_helper.unlink(os_helper.TESTFN) |
| 83 | + self.assertEqual(r, 0x12345678) |
| 84 | + self.assertEqual(p, 4) |
| 85 | + |
| 86 | + with open(os_helper.TESTFN, 'wb') as f: |
| 87 | + f.write(b'\x56\x34\x12') |
| 88 | + with self.assertRaises(EOFError): |
| 89 | + _testcapi.pymarshal_read_long_from_file(os_helper.TESTFN) |
| 90 | + os_helper.unlink(os_helper.TESTFN) |
| 91 | + |
| 92 | + def test_read_last_object_from_file(self): |
| 93 | + obj = ('\u20ac', b'abc', 123, 45.6, 7+8j) |
| 94 | + for v in range(marshal.version + 1): |
| 95 | + data = marshal.dumps(obj, v) |
| 96 | + with open(os_helper.TESTFN, 'wb') as f: |
| 97 | + f.write(data + b'xxxx') |
| 98 | + r, p = _testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN) |
| 99 | + os_helper.unlink(os_helper.TESTFN) |
| 100 | + self.assertEqual(r, obj) |
| 101 | + |
| 102 | + with open(os_helper.TESTFN, 'wb') as f: |
| 103 | + f.write(omit_last_byte(data)) |
| 104 | + with self.assertRaises(EOFError): |
| 105 | + _testcapi.pymarshal_read_last_object_from_file(os_helper.TESTFN) |
| 106 | + os_helper.unlink(os_helper.TESTFN) |
| 107 | + |
| 108 | + def test_read_object_from_file(self): |
| 109 | + obj = ('\u20ac', b'abc', 123, 45.6, 7+8j) |
| 110 | + for v in range(marshal.version + 1): |
| 111 | + data = marshal.dumps(obj, v) |
| 112 | + with open(os_helper.TESTFN, 'wb') as f: |
| 113 | + f.write(data + b'xxxx') |
| 114 | + r, p = _testcapi.pymarshal_read_object_from_file(os_helper.TESTFN) |
| 115 | + os_helper.unlink(os_helper.TESTFN) |
| 116 | + self.assertEqual(r, obj) |
| 117 | + self.assertEqual(p, len(data)) |
| 118 | + |
| 119 | + with open(os_helper.TESTFN, 'wb') as f: |
| 120 | + f.write(omit_last_byte(data)) |
| 121 | + with self.assertRaises(EOFError): |
| 122 | + _testcapi.pymarshal_read_object_from_file(os_helper.TESTFN) |
| 123 | + os_helper.unlink(os_helper.TESTFN) |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + unittest.main() |
0 commit comments