Issue #14177: marshal.loads() now raises TypeError when given an unicode string.
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 3 Mar 2012 01:35:32 +0000 (02:35 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 3 Mar 2012 01:35:32 +0000 (02:35 +0100)
Patch by Guilherme Gonçalves.

Lib/test/test_exceptions.py
Lib/test/test_marshal.py
Misc/ACKS
Misc/NEWS
Python/marshal.c

index 0a7ddd4c5b1b27b186bbb7b41d441ba3054708a9..7a2dd0c8f17fa86d0c6a0c366a142c4938cb66f3 100644 (file)
@@ -38,7 +38,7 @@ class ExceptionTests(unittest.TestCase):
         try:
             try:
                 import marshal
-                marshal.loads('')
+                marshal.loads(b'')
             except EOFError:
                 pass
         finally:
index 9a250120ed81283ddad38d792c36e9f75dc2686d..96a70ecc2abc5272ad67ddd373e9ed8960be4c5c 100644 (file)
@@ -184,7 +184,7 @@ class BugsTestCase(unittest.TestCase):
                 pass
 
     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)
 
     def test_recursion_limit(self):
@@ -257,6 +257,11 @@ class BugsTestCase(unittest.TestCase):
             finally:
                 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():
     support.run_unittest(IntTestCase,
index 3f9cff936a5d4881c38c708902045cbed5adb7ee..00288772b24456aab1e4f6f14758407aaeacd18a 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -341,6 +341,7 @@ Johannes Gijsbers
 Michael Gilfix
 Christoph Gohlke
 Tim Golden
+Guilherme Gonçalves
 Chris Gonnerman
 David Goodger
 Hans de Graaff
index 9c854a2aa54b7f54182576d89f8a6469bc4ea1b6..d6298400b40fdd006191fad7ce334f8dca384358 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -130,6 +130,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14177: marshal.loads() now raises TypeError when given an unicode
+  string.  Patch by Guilherme Gonçalves.
+
 - Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary,
   WeakValueDictionary) to return a better approximation when some objects
   are dead or dying.  Moreover, the implementation is now O(1) rather than
index 7b1af44f9985340260e586a1f105f31f2f37aa65..3e2fbeb499f7b727f07a16fc82057d9c49c2bf69 100644 (file)
@@ -1383,7 +1383,7 @@ marshal_loads(PyObject *self, PyObject *args)
     char *s;
     Py_ssize_t n;
     PyObject* result;
-    if (!PyArg_ParseTuple(args, "s*:loads", &p))
+    if (!PyArg_ParseTuple(args, "y*:loads", &p))
         return NULL;
     s = p.buf;
     n = p.len;
@@ -1400,10 +1400,10 @@ marshal_loads(PyObject *self, PyObject *args)
 }
 
 PyDoc_STRVAR(loads_doc,
-"loads(string)\n\
+"loads(bytes)\n\
 \n\
-Convert the string to a value. If no valid value is found, raise\n\
-EOFError, ValueError or TypeError. Extra characters in the string are\n\
+Convert the bytes object to a value. If no valid value is found, raise\n\
+EOFError, ValueError or TypeError. Extra characters in the input are\n\
 ignored.");
 
 static PyMethodDef marshal_methods[] = {