Commit e6d3904c authored by Guido van Rossum's avatar Guido van Rossum

Make test_marshal pass. Not my best work. :-(

parent bc14efbd
...@@ -27,18 +27,18 @@ class IntTestCase(unittest.TestCase): ...@@ -27,18 +27,18 @@ class IntTestCase(unittest.TestCase):
# we're running the test on a 32-bit box, of course. # we're running the test on a 32-bit box, of course.
def to_little_endian_string(value, nbytes): def to_little_endian_string(value, nbytes):
bytes = [] b = bytes()
for i in range(nbytes): for i in range(nbytes):
bytes.append(chr(value & 0xff)) b.append(value & 0xff)
value >>= 8 value >>= 8
return ''.join(bytes) return b
maxint64 = (1 << 63) - 1 maxint64 = (1 << 63) - 1
minint64 = -maxint64-1 minint64 = -maxint64-1
for base in maxint64, minint64, -maxint64, -(minint64 >> 1): for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
while base: while base:
s = 'I' + to_little_endian_string(base, 8) s = b'I' + to_little_endian_string(base, 8)
got = marshal.loads(s) got = marshal.loads(s)
self.assertEqual(base, got) self.assertEqual(base, got)
if base == -1: # a fixed-point for shifting right 1 if base == -1: # a fixed-point for shifting right 1
...@@ -128,7 +128,7 @@ class StringTestCase(unittest.TestCase): ...@@ -128,7 +128,7 @@ class StringTestCase(unittest.TestCase):
os.unlink(test_support.TESTFN) os.unlink(test_support.TESTFN)
def test_buffer(self): def test_buffer(self):
for s in ["", "Andr Previn", "abc", " "*10000]: for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]:
b = buffer(s) b = buffer(s)
new = marshal.loads(marshal.dumps(b)) new = marshal.loads(marshal.dumps(b))
self.assertEqual(s, new) self.assertEqual(s, new)
......
...@@ -1014,6 +1014,8 @@ PyObject * ...@@ -1014,6 +1014,8 @@ PyObject *
PyMarshal_WriteObjectToString(PyObject *x, int version) PyMarshal_WriteObjectToString(PyObject *x, int version)
{ {
WFILE wf; WFILE wf;
PyObject *res = NULL;
wf.fp = NULL; wf.fp = NULL;
wf.str = PyString_FromStringAndSize((char *)NULL, 50); wf.str = PyString_FromStringAndSize((char *)NULL, 50);
if (wf.str == NULL) if (wf.str == NULL)
...@@ -1034,7 +1036,8 @@ PyMarshal_WriteObjectToString(PyObject *x, int version) ...@@ -1034,7 +1036,8 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
"too much marshal data for a string"); "too much marshal data for a string");
return NULL; return NULL;
} }
_PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)); if (_PyString_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
return NULL;
} }
if (wf.error) { if (wf.error) {
Py_XDECREF(wf.str); Py_XDECREF(wf.str);
...@@ -1043,7 +1046,12 @@ PyMarshal_WriteObjectToString(PyObject *x, int version) ...@@ -1043,7 +1046,12 @@ PyMarshal_WriteObjectToString(PyObject *x, int version)
:"object too deeply nested to marshal"); :"object too deeply nested to marshal");
return NULL; return NULL;
} }
return wf.str; if (wf.str != NULL) {
/* XXX Quick hack -- need to do this differently */
res = PyBytes_FromObject(wf.str);
Py_DECREF(wf.str);
}
return res;
} }
/* And an interface for Python programs... */ /* And an interface for Python programs... */
...@@ -1092,9 +1100,34 @@ marshal_load(PyObject *self, PyObject *f) ...@@ -1092,9 +1100,34 @@ marshal_load(PyObject *self, PyObject *f)
RFILE rf; RFILE rf;
PyObject *result; PyObject *result;
if (!PyFile_Check(f)) { if (!PyFile_Check(f)) {
PyErr_SetString(PyExc_TypeError, /* XXX Quick hack -- need to do this differently */
"marshal.load() arg must be file"); PyObject *data, *result;
RFILE rf;
data = PyObject_CallMethod(f, "read", "");
if (data == NULL)
return NULL; return NULL;
rf.fp = NULL;
if (PyString_Check(data)) {
rf.ptr = PyString_AS_STRING(data);
rf.end = rf.ptr + PyString_GET_SIZE(data);
}
else if (PyBytes_Check(data)) {
rf.ptr = PyBytes_AS_STRING(data);
rf.end = rf.ptr + PyBytes_GET_SIZE(data);
}
else {
PyErr_Format(PyExc_TypeError,
"f.read() returned neither string "
"nor bytes but %.100s",
data->ob_type->tp_name);
Py_DECREF(data);
return NULL;
}
rf.strings = PyList_New(0);
result = read_object(&rf);
Py_DECREF(rf.strings);
Py_DECREF(data);
return result;
} }
rf.fp = PyFile_AsFile(f); rf.fp = PyFile_AsFile(f);
rf.strings = PyList_New(0); rf.strings = PyList_New(0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment