[Python-checkins] cpython (3.2): Issue #14177: marshal.loads() now raises TypeError when given an unicode string.

antoine.pitrou python-checkins at python.org
Sat Mar 3 02:44:47 CET 2012


http://hg.python.org/cpython/rev/4966907d3661
changeset:   75374:4966907d3661
branch:      3.2
parent:      75369:185a6ae76479
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sat Mar 03 02:35:32 2012 +0100
summary:
  Issue #14177: marshal.loads() now raises TypeError when given an unicode string.
Patch by Guilherme Gonçalves.

files:
  Lib/test/test_exceptions.py |  2 +-
  Lib/test/test_marshal.py    |  7 ++++++-
  Misc/ACKS                   |  1 +
  Misc/NEWS                   |  3 +++
  Python/marshal.c            |  8 ++++----
  5 files changed, 15 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -38,7 +38,7 @@
         try:
             try:
                 import marshal
-                marshal.loads('')
+                marshal.loads(b'')
             except EOFError:
                 pass
         finally:
diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py
--- a/Lib/test/test_marshal.py
+++ b/Lib/test/test_marshal.py
@@ -184,7 +184,7 @@
                 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 @@
             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,
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -341,6 +341,7 @@
 Michael Gilfix
 Christoph Gohlke
 Tim Golden
+Guilherme Gonçalves
 Chris Gonnerman
 David Goodger
 Hans de Graaff
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -130,6 +130,9 @@
 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
diff --git a/Python/marshal.c b/Python/marshal.c
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -1383,7 +1383,7 @@
     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 @@
 }
 
 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[] = {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list