classification
Title: binascii.a2b_* functions could accept unicode strings
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, haypo, luke-jr, pitrou, python-dev, r.david.murray, vadmium, vajrasky
Priority: normal Keywords: patch

Created on 2011-12-19 16:30 by pitrou, last changed 2013-11-05 00:08 by vadmium.

Files
File name Uploaded Description Edit
binasciistr.patch pitrou, 2011-12-20 08:57 review
fix_doc_binascii_unhexlify.patch vajrasky, 2013-11-02 04:42 review
better_error_message_binascii.patch vajrasky, 2013-11-02 13:43 review
Messages (11)
msg149876 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-12-19 16:30
a2b_hex and friends accept only byte strings:

>>> binascii.a2b_hex(b'00')
b'\x00'
>>> binascii.a2b_hex('00')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface

But they could just as well accept ASCII-only unicode strings. Also, with PEP 393, accessing the 8-bit ASCII data doesn't even need a conversion.
msg149905 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-12-20 08:57
Here is a patch.
msg149911 - (view) Author: Roundup Robot (python-dev) Date: 2011-12-20 13:00
New changeset eb8d62706d5f by Antoine Pitrou in branch 'default':
Issue #13637: "a2b" functions in the binascii module now accept ASCII-only unicode strings.
http://hg.python.org/cpython/rev/eb8d62706d5f
msg149913 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-12-20 13:08
Committed now.
msg153829 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-02-21 01:11
I disagree with this feature.  Reopening pending discussion on python-dev.
msg154265 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-02-25 15:40
Discussion resolved in favor of patch.
msg154964 - (view) Author: Luke-Jr (luke-jr) Date: 2012-03-05 17:19
Has this been fixed in 3.2 yet? Somehow it seems to have been "reclassified" as an enhancement when it's really a regression. str worked fine in these functions in 3.1.
msg155001 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * Date: 2012-03-06 05:47
I confirm that it works in Python 3.1 and doesn't work in Python 3.2.
msg201963 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-11-02 04:42
Antoine, I think you forgot to update the doc.

http://docs.python.org/3.4/library/binascii.html#binascii.unhexlify

Changed in version 3.2: Accept only bytestring or bytearray objects as input.

Attached the patch to update the doc to reflect the changes in this ticket.
msg201972 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2013-11-02 13:43
Here is another patch to better the error message. Right now, the error message when wrong input sent to unhexlify is a little bit funny.

>>> import binascii
>>> binascii.unhexlify(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument should be bytes, buffer or ASCII string, not <class 'int'>

After patch:
>>> import binascii
>>> binascii.unhexlify(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument should be bytes, buffer or ASCII string, not int
msg202181 - (view) Author: Martin Panter (vadmium) * Date: 2013-11-05 00:08
The a2b_qp() function also documents a byte string restriction for 3.2, and now 3.3 also seems to support ASCII-compatible text strings. Maybe the documentation should reflect this also?
History
Date User Action Args
2013-11-05 00:08:44vadmiumsetnosy: + vadmium
messages: + msg202181
2013-11-02 13:43:42vajraskysetfiles: + better_error_message_binascii.patch

messages: + msg201972
2013-11-02 04:42:35vajraskysetfiles: + fix_doc_binascii_unhexlify.patch
nosy: + vajrasky
messages: + msg201963

2012-03-06 05:47:37Arfreversetstatus: closed -> open

type: enhancement -> behavior

nosy: + Arfrever
messages: + msg155001
resolution: fixed ->
stage: resolved ->
2012-03-05 17:19:25luke-jrsetnosy: + luke-jr
messages: + msg154964
2012-02-25 15:40:46r.david.murraysetstatus: open -> closed

messages: + msg154265
2012-02-21 01:11:27r.david.murraysetstatus: closed -> open
nosy: + r.david.murray
messages: + msg153829

2011-12-20 13:09:24pitrousetstatus: open -> closed
2011-12-20 13:08:10pitrousetresolution: fixed
messages: + msg149913
stage: patch review -> resolved
2011-12-20 13:00:54python-devsetnosy: + python-dev
messages: + msg149911
2011-12-20 08:57:35pitrousetfiles: + binasciistr.patch
keywords: + patch
messages: + msg149905

stage: needs patch -> patch review
2011-12-19 16:30:44pitroucreate