Skip to content

Commit d15bb5f

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
bpo-32011: Revert "Issue GH-15480: Remove the deprecated and unused TYPE_INT64 code from marshal." (GH-4381) (#4405)
Simplify the reverted code. This reverts commit e9bbe8b. (cherry picked from commit 00987f6)
1 parent 3864248 commit d15bb5f

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

‎Lib/test/test_marshal.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ def test_ints(self):
3434
self.helper(expected)
3535
n = n >> 1
3636

37+
def test_int64(self):
38+
# Simulate int marshaling with TYPE_INT64.
39+
maxint64 = (1 << 63) - 1
40+
minint64 = -maxint64-1
41+
for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
42+
while base:
43+
s = b'I' + int.to_bytes(base, 8, 'little', signed=True)
44+
got = marshal.loads(s)
45+
self.assertEqual(base, got)
46+
if base == -1: # a fixed-point for shifting right 1
47+
base = 0
48+
else:
49+
base >>= 1
50+
51+
got = marshal.loads(b'I\xfe\xdc\xba\x98\x76\x54\x32\x10')
52+
self.assertEqual(got, 0x1032547698badcfe)
53+
got = marshal.loads(b'I\x01\x23\x45\x67\x89\xab\xcd\xef')
54+
self.assertEqual(got, -0x1032547698badcff)
55+
got = marshal.loads(b'I\x08\x19\x2a\x3b\x4c\x5d\x6e\x7f')
56+
self.assertEqual(got, 0x7f6e5d4c3b2a1908)
57+
got = marshal.loads(b'I\xf7\xe6\xd5\xc4\xb3\xa2\x91\x80')
58+
self.assertEqual(got, -0x7f6e5d4c3b2a1909)
59+
3760
def test_bool(self):
3861
for b in (True, False):
3962
self.helper(b)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Restored support of loading marshal files with the TYPE_INT64 code. These
2+
files can be produced in Python 2.7.

‎Python/marshal.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#define TYPE_STOPITER 'S'
3333
#define TYPE_ELLIPSIS '.'
3434
#define TYPE_INT 'i'
35+
/* TYPE_INT64 is not generated anymore.
36+
Supported for backward compatibility only. */
37+
#define TYPE_INT64 'I'
3538
#define TYPE_FLOAT 'f'
3639
#define TYPE_BINARY_FLOAT 'g'
3740
#define TYPE_COMPLEX 'x'
@@ -777,6 +780,19 @@ r_long(RFILE *p)
777780
return x;
778781
}
779782

783+
/* r_long64 deals with the TYPE_INT64 code. */
784+
static PyObject *
785+
r_long64(RFILE *p)
786+
{
787+
const unsigned char *buffer = (const unsigned char *) r_string(8, p);
788+
if (buffer == NULL) {
789+
return NULL;
790+
}
791+
return _PyLong_FromByteArray(buffer, 8,
792+
1 /* little endian */,
793+
1 /* signed */);
794+
}
795+
780796
static PyObject *
781797
r_PyLong(RFILE *p)
782798
{
@@ -976,6 +992,11 @@ r_object(RFILE *p)
976992
R_REF(retval);
977993
break;
978994

995+
case TYPE_INT64:
996+
retval = r_long64(p);
997+
R_REF(retval);
998+
break;
999+
9791000
case TYPE_LONG:
9801001
retval = r_PyLong(p);
9811002
R_REF(retval);

0 commit comments

Comments
 (0)