Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6d3904

Browse files
committed
Make test_marshal pass. Not my best work. :-(
1 parent bc14efb commit e6d3904

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

Lib/test/test_marshal.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ def test_int64(self):
2727
# we're running the test on a 32-bit box, of course.
2828

2929
def to_little_endian_string(value, nbytes):
30-
bytes = []
30+
b = bytes()
3131
for i in range(nbytes):
32-
bytes.append(chr(value & 0xff))
32+
b.append(value & 0xff)
3333
value >>= 8
34-
return ''.join(bytes)
34+
return b
3535

3636
maxint64 = (1 << 63) - 1
3737
minint64 = -maxint64-1
3838

3939
for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
4040
while base:
41-
s = 'I' + to_little_endian_string(base, 8)
41+
s = b'I' + to_little_endian_string(base, 8)
4242
got = marshal.loads(s)
4343
self.assertEqual(base, got)
4444
if base == -1: # a fixed-point for shifting right 1
@@ -128,7 +128,7 @@ def test_string(self):
128128
os.unlink(test_support.TESTFN)
129129

130130
def test_buffer(self):
131-
for s in ["", "Andrè Previn", "abc", " "*10000]:
131+
for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]:
132132
b = buffer(s)
133133
new = marshal.loads(marshal.dumps(b))
134134
self.assertEqual(s, new)

Python/marshal.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,8 @@ PyObject *
10141014
PyMarshal_WriteObjectToString(PyObject *x, int version)
10151015
{
10161016
WFILE wf;
1017+
PyObject *res = NULL;
1018+
10171019
wf.fp = NULL;
10181020
wf.str = PyString_FromStringAndSize((char *)NULL, 50);
10191021
if (wf.str == NULL)
@@ -1034,7 +1036,8 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
10341036
"too much marshal data for a string");
10351037
return NULL;
10361038
}
1037-
_PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base));
1039+
if (_PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
1040+
return NULL;
10381041
}
10391042
if (wf.error) {
10401043
Py_XDECREF(wf.str);
@@ -1043,7 +1046,12 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
10431046
:"object too deeply nested to marshal");
10441047
return NULL;
10451048
}
1046-
return wf.str;
1049+
if (wf.str != NULL) {
1050+
/* XXX Quick hack -- need to do this differently */
1051+
res = PyBytes_FromObject(wf.str);
1052+
Py_DECREF(wf.str);
1053+
}
1054+
return res;
10471055
}
10481056

10491057
/* And an interface for Python programs... */
@@ -1092,9 +1100,34 @@ marshal_load(PyObject *self, PyObject *f)
10921100
RFILE rf;
10931101
PyObject *result;
10941102
if (!PyFile_Check(f)) {
1095-
PyErr_SetString(PyExc_TypeError,
1096-
"marshal.load() arg must be file");
1097-
return NULL;
1103+
/* XXX Quick hack -- need to do this differently */
1104+
PyObject *data, *result;
1105+
RFILE rf;
1106+
data = PyObject_CallMethod(f, "read", "");
1107+
if (data == NULL)
1108+
return NULL;
1109+
rf.fp = NULL;
1110+
if (PyString_Check(data)) {
1111+
rf.ptr = PyString_AS_STRING(data);
1112+
rf.end = rf.ptr + PyString_GET_SIZE(data);
1113+
}
1114+
else if (PyBytes_Check(data)) {
1115+
rf.ptr = PyBytes_AS_STRING(data);
1116+
rf.end = rf.ptr + PyBytes_GET_SIZE(data);
1117+
}
1118+
else {
1119+
PyErr_Format(PyExc_TypeError,
1120+
"f.read() returned neither string "
1121+
"nor bytes but %.100s",
1122+
data->ob_type->tp_name);
1123+
Py_DECREF(data);
1124+
return NULL;
1125+
}
1126+
rf.strings = PyList_New(0);
1127+
result = read_object(&rf);
1128+
Py_DECREF(rf.strings);
1129+
Py_DECREF(data);
1130+
return result;
10981131
}
10991132
rf.fp = PyFile_AsFile(f);
11001133
rf.strings = PyList_New(0);

0 commit comments

Comments
 (0)