This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: marshal.loads accepts unicode strings
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.2, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: guilherme-pg, pitrou, python-dev
Priority: normal Keywords: easy, patch

Created on 2012-03-02 16:25 by pitrou, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
14177-marshal-loads-deny-strings.patch guilherme-pg, 2012-03-02 23:49 review
14177-marshal-loads-deny-strings-2.patch guilherme-pg, 2012-03-03 01:18 review
Messages (8)
msg154778 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-03-02 16:25
>>> marshal.loads('T')
True
>>> marshal.loads(b'T')
True

Contrast with:

>>> marshal.load(io.BytesIO(b'T'))
True
>>> marshal.load(io.StringIO('T'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f.read() returned not bytes but str
msg154809 - (view) Author: Guilherme Gonçalves (guilherme-pg) Date: 2012-03-02 23:49
The attached patch attempts to solve the issue.

It makes sure marshal.loads only accepts objects conforming to the buffer protocol, updates the documentation for the method accordingly, adds a test case and updates existing test cases that rely on this issue.

Please excuse any obvious mistakes in the patch, this is my first contribution and I'll be glad to iterate until it's good.
msg154816 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-03-03 00:59
Thank you. The patch looks almost perfect, just one nit: apparently you changed the documentation for marshal.dumps. Is that intended?
msg154818 - (view) Author: Guilherme Gonçalves (guilherme-pg) Date: 2012-03-03 01:18
Oops, sorry, that was unintended.

I uploaded a new version of the patch with the correct documentation update, making it explicit that loads() expects a bytes object.
msg154819 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-03-03 01:31
Thank you! What is your full name, for credits?
msg154820 - (view) Author: Guilherme Gonçalves (guilherme-pg) Date: 2012-03-03 01:36
Guilherme Gonçalves <guilherme.p.gonc@gmail.com>

Thanks for the quick review.
msg154821 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-03-03 01:44
New changeset 4966907d3661 by Antoine Pitrou in branch '3.2':
Issue #14177: marshal.loads() now raises TypeError when given an unicode string.
http://hg.python.org/cpython/rev/4966907d3661

New changeset e67b3a9bd2dc by Antoine Pitrou in branch 'default':
- Issue #14177: marshal.loads() now raises TypeError when given an unicode
http://hg.python.org/cpython/rev/e67b3a9bd2dc
msg154822 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-03-03 01:47
I've committed the patch in 3.2 and 3.3.
History
Date User Action Args
2022-04-11 14:57:27adminsetgithub: 58385
2012-03-03 01:47:59pitrousetstatus: open -> closed
resolution: fixed
messages: + msg154822

stage: needs patch -> resolved
2012-03-03 01:44:48python-devsetnosy: + python-dev
messages: + msg154821
2012-03-03 01:36:19guilherme-pgsetmessages: + msg154820
2012-03-03 01:31:37pitrousetmessages: + msg154819
2012-03-03 01:18:59guilherme-pgsetfiles: + 14177-marshal-loads-deny-strings-2.patch

messages: + msg154818
2012-03-03 00:59:39pitrousetmessages: + msg154816
2012-03-02 23:49:01guilherme-pgsetfiles: + 14177-marshal-loads-deny-strings.patch

nosy: + guilherme-pg
messages: + msg154809

keywords: + patch
2012-03-02 16:25:12pitroucreate