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

Skip to content

Commit 4a90ef0

Browse files
committed
Issue #14177: marshal.loads() now raises TypeError when given an unicode string.
Patch by Guilherme Gonçalves.
1 parent 679e9d3 commit 4a90ef0

5 files changed

Lines changed: 15 additions & 6 deletions

File tree

Lib/test/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def testRaising(self):
3838
try:
3939
try:
4040
import marshal
41-
marshal.loads('')
41+
marshal.loads(b'')
4242
except EOFError:
4343
pass
4444
finally:

Lib/test/test_marshal.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def test_fuzz(self):
184184
pass
185185

186186
def test_loads_recursion(self):
187-
s = 'c' + ('X' * 4*4) + '{' * 2**20
187+
s = b'c' + (b'X' * 4*4) + b'{' * 2**20
188188
self.assertRaises(ValueError, marshal.loads, s)
189189

190190
def test_recursion_limit(self):
@@ -257,6 +257,11 @@ def test_multiple_dumps_and_loads(self):
257257
finally:
258258
support.unlink(support.TESTFN)
259259

260+
def test_loads_reject_unicode_strings(self):
261+
# Issue #14177: marshal.loads() should not accept unicode strings
262+
unicode_string = 'T'
263+
self.assertRaises(TypeError, marshal.loads, unicode_string)
264+
260265

261266
def test_main():
262267
support.run_unittest(IntTestCase,

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ Johannes Gijsbers
341341
Michael Gilfix
342342
Christoph Gohlke
343343
Tim Golden
344+
Guilherme Gonçalves
344345
Chris Gonnerman
345346
David Goodger
346347
Hans de Graaff

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ Core and Builtins
130130
Library
131131
-------
132132

133+
- Issue #14177: marshal.loads() now raises TypeError when given an unicode
134+
string. Patch by Guilherme Gonçalves.
135+
133136
- Issue #14159: Fix the len() of weak containers (WeakSet, WeakKeyDictionary,
134137
WeakValueDictionary) to return a better approximation when some objects
135138
are dead or dying. Moreover, the implementation is now O(1) rather than

Python/marshal.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ marshal_loads(PyObject *self, PyObject *args)
13831383
char *s;
13841384
Py_ssize_t n;
13851385
PyObject* result;
1386-
if (!PyArg_ParseTuple(args, "s*:loads", &p))
1386+
if (!PyArg_ParseTuple(args, "y*:loads", &p))
13871387
return NULL;
13881388
s = p.buf;
13891389
n = p.len;
@@ -1400,10 +1400,10 @@ marshal_loads(PyObject *self, PyObject *args)
14001400
}
14011401

14021402
PyDoc_STRVAR(loads_doc,
1403-
"loads(string)\n\
1403+
"loads(bytes)\n\
14041404
\n\
1405-
Convert the string to a value. If no valid value is found, raise\n\
1406-
EOFError, ValueError or TypeError. Extra characters in the string are\n\
1405+
Convert the bytes object to a value. If no valid value is found, raise\n\
1406+
EOFError, ValueError or TypeError. Extra characters in the input are\n\
14071407
ignored.");
14081408

14091409
static PyMethodDef marshal_methods[] = {

0 commit comments

Comments
 (0)