- Issue #14177: marshal.loads() now raises TypeError when given an unicode

string.  Patch by Guilherme Gonçalves.
This commit is contained in:
Antoine Pitrou 2012-03-03 02:38:37 +01:00
commit 0d3a003f24
5 changed files with 15 additions and 6 deletions

View File

@ -38,7 +38,7 @@ class ExceptionTests(unittest.TestCase):
try: try:
try: try:
import marshal import marshal
marshal.loads('') marshal.loads(b'')
except EOFError: except EOFError:
pass pass
finally: finally:

View File

@ -201,7 +201,7 @@ class BugsTestCase(unittest.TestCase):
pass pass
def test_loads_recursion(self): def test_loads_recursion(self):
s = 'c' + ('X' * 4*4) + '{' * 2**20 s = b'c' + (b'X' * 4*4) + b'{' * 2**20
self.assertRaises(ValueError, marshal.loads, s) self.assertRaises(ValueError, marshal.loads, s)
def test_recursion_limit(self): def test_recursion_limit(self):
@ -274,6 +274,11 @@ class BugsTestCase(unittest.TestCase):
finally: finally:
support.unlink(support.TESTFN) support.unlink(support.TESTFN)
def test_loads_reject_unicode_strings(self):
# Issue #14177: marshal.loads() should not accept unicode strings
unicode_string = 'T'
self.assertRaises(TypeError, marshal.loads, unicode_string)
def test_main(): def test_main():
support.run_unittest(IntTestCase, support.run_unittest(IntTestCase,

View File

@ -371,6 +371,7 @@ Yannick Gingras
Michael Goderbauer Michael Goderbauer
Christoph Gohlke Christoph Gohlke
Tim Golden Tim Golden
Guilherme Gonçalves
Tiago Gonçalves Tiago Gonçalves
Chris Gonnerman Chris Gonnerman
David Goodger David Goodger

View File

@ -511,6 +511,9 @@ Core and Builtins
Library Library
------- -------
- Issue #14177: marshal.loads() now raises TypeError when given an unicode
string. Patch by Guilherme Gonçalves.
- Issue #13550: Remove the debug machinery from the threading module: remove - Issue #13550: Remove the debug machinery from the threading module: remove
verbose arguments from all threading classes and functions. verbose arguments from all threading classes and functions.

View File

@ -1384,7 +1384,7 @@ marshal_loads(PyObject *self, PyObject *args)
char *s; char *s;
Py_ssize_t n; Py_ssize_t n;
PyObject* result; PyObject* result;
if (!PyArg_ParseTuple(args, "s*:loads", &p)) if (!PyArg_ParseTuple(args, "y*:loads", &p))
return NULL; return NULL;
s = p.buf; s = p.buf;
n = p.len; n = p.len;
@ -1400,10 +1400,10 @@ marshal_loads(PyObject *self, PyObject *args)
} }
PyDoc_STRVAR(loads_doc, PyDoc_STRVAR(loads_doc,
"loads(string)\n\ "loads(bytes)\n\
\n\ \n\
Convert the string to a value. If no valid value is found, raise\n\ Convert the bytes object to a value. If no valid value is found, raise\n\
EOFError, ValueError or TypeError. Extra characters in the string are\n\ EOFError, ValueError or TypeError. Extra characters in the input are\n\
ignored."); ignored.");
static PyMethodDef marshal_methods[] = { static PyMethodDef marshal_methods[] = {