[issue10976] json.loads() throws TypeError on bytes object

hhas report at bugs.python.org
Fri Jan 21 20:01:49 CET 2011


New submission from hhas <hhas at users.sourceforge.net>:

json.loads() accepts strings but errors on bytes objects. Documentation and API indicate that both should work. Review of json/__init__.py code shows that the loads() function's 'encoding' arg is ignored and no decoding takes place before the object is passed to JSONDecoder.decode()

Tested on Python 3.1.2 and Python 3.2rc1; fails on both. 

Example:

#################################################

#!/usr/local/bin/python3.2

import json

print(json.loads('123'))
# 123

print(json.loads(b'123'))
# /Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/json/decoder.py:325:  
#   TypeError: can't use a string pattern on a bytes-like object

print(json.loads(b'123', encoding='utf-8'))
# /Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/json/decoder.py:325:  
#   TypeError: can't use a string pattern on a bytes-like object

#################################################

Patch attached.

----------
components: Library (Lib)
files: json.diff
keywords: patch
messages: 126772
nosy: hhas
priority: normal
severity: normal
status: open
title: json.loads() throws TypeError on bytes object
type: behavior
versions: Python 3.1, Python 3.2
Added file: http://bugs.python.org/file20481/json.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10976>
_______________________________________


More information about the Python-bugs-list mailing list