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

Skip to content

Commit 0d3a003

Browse files
committed
- Issue #14177: marshal.loads() now raises TypeError when given an unicode
string. Patch by Guilherme Gonçalves.
2 parents 135b6d8 + 4a90ef0 commit 0d3a003

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
@@ -201,7 +201,7 @@ def test_fuzz(self):
201201
pass
202202

203203
def test_loads_recursion(self):
204-
s = 'c' + ('X' * 4*4) + '{' * 2**20
204+
s = b'c' + (b'X' * 4*4) + b'{' * 2**20
205205
self.assertRaises(ValueError, marshal.loads, s)
206206

207207
def test_recursion_limit(self):
@@ -274,6 +274,11 @@ def test_multiple_dumps_and_loads(self):
274274
finally:
275275
support.unlink(support.TESTFN)
276276

277+
def test_loads_reject_unicode_strings(self):
278+
# Issue #14177: marshal.loads() should not accept unicode strings
279+
unicode_string = 'T'
280+
self.assertRaises(TypeError, marshal.loads, unicode_string)
281+
277282

278283
def test_main():
279284
support.run_unittest(IntTestCase,

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Yannick Gingras
371371
Michael Goderbauer
372372
Christoph Gohlke
373373
Tim Golden
374+
Guilherme Gonçalves
374375
Tiago Gonçalves
375376
Chris Gonnerman
376377
David Goodger

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,9 @@ Core and Builtins
511511
Library
512512
-------
513513

514+
- Issue #14177: marshal.loads() now raises TypeError when given an unicode
515+
string. Patch by Guilherme Gonçalves.
516+
514517
- Issue #13550: Remove the debug machinery from the threading module: remove
515518
verbose arguments from all threading classes and functions.
516519

Python/marshal.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ marshal_loads(PyObject *self, PyObject *args)
13841384
char *s;
13851385
Py_ssize_t n;
13861386
PyObject* result;
1387-
if (!PyArg_ParseTuple(args, "s*:loads", &p))
1387+
if (!PyArg_ParseTuple(args, "y*:loads", &p))
13881388
return NULL;
13891389
s = p.buf;
13901390
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)