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: binascii.a2b_hex raises binascii.Error and ValueError, not TypeError
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Lennart Grahl, docs@python, luiz.poleto, martin.panter, python-dev, steven.daprano
Priority: normal Keywords: patch

Created on 2016-05-25 15:42 by Lennart Grahl, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue27124.patch luiz.poleto, 2016-05-26 03:04 Patch for the documentation. review
Messages (6)
msg266368 - (view) Author: Lennart Grahl (Lennart Grahl) Date: 2016-05-25 15:42
When using binascii.a2b_hex (or binascii.unhexlify) and the argument is invalid, the doc states that it should raise a TypeError for invalid arguments (e.g. passing an odd-length string).

However, it does raise binascii.Error instead of TypeError:

try:
    binascii.a2b_hex('a')
except Exception as exc:
    print(type(exc))
msg266370 - (view) Author: Lennart Grahl (Lennart Grahl) Date: 2016-05-25 15:55
When using binascii.a2b_hex (or binascii.unhexlify) and the argument is invalid, the doc states that it should raise a TypeError for invalid arguments (e.g. passing an odd-length string).

However, it does raise binascii.Error instead of TypeError:

try:
    binascii.a2b_hex('a')
except Exception as exc:
    print(type(exc))

What surprised me even more was that it raises ValueError (although binascii.Error inherits ValueError) in case a unicode string has been passed as an argument:

try:
    binascii.a2b_hex('ä')
except binascii.Error:
    print('binascii.Error')
except ValueError:
    print('ValueError')
msg266371 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-05-25 16:23
In Python 3.x, 'a' is just as much a Unicode string as 'ä'. I agree that binascii.a2b_hex should raise ValueError for argument 'ä', or ';' for that matter, as they are invalid values. There's an inconsistency in why some invalid characters raise ValueError and some raise binascii.Error. But since binascii.Error is a subclass of ValueError, it probably doesn't matter that much, so long as the documentation is clear.
msg266404 - (view) Author: Luiz Poleto (luiz.poleto) * Date: 2016-05-26 03:04
binascii.Error is used throughout the module for most of the validations, with only a few of them using TypeError/ValueError. I guess it would make more sense to update these to raise binascii.Error (unless they were left on purpose) to make it consistent with the rest of the module, although changing from TypeError to binascii.Error might break existing code.

Anyway, since a2b_hex is currently raising binascii.Error for an odd-length input, the attached patch fixes the documentation to reflect it.
msg266419 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-05-26 09:03
The TypeError → binascii.Error changeover was made in revision eb45f85c4c79, to this function, along with various decoding functions in the base64 module. The base64 documenation was only slowly updated in a series of bug fixes, and it looks like unhexlify() is the only one left.

At the top of the binascii documentation, it says that the decoding functions accept ASCII strings. The general check for non-ASCII code points (raising ValueError) would be done before the specific checks for hexadecimal digits and having the right length.

Luiz’s patch looks okay to me.
msg266582 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-05-29 01:10
New changeset ef89ecb6debc by Martin Panter in branch '3.5':
Issue #27124: Fix documentation of exception raised by a2b_hex()
https://hg.python.org/cpython/rev/ef89ecb6debc

New changeset 85e6da63d73f by Martin Panter in branch 'default':
Issue #27124: Merge binascii doc from 3.5
https://hg.python.org/cpython/rev/85e6da63d73f
History
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71311
2016-05-29 03:44:20martin.pantersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2016-05-29 01:10:37python-devsetnosy: + python-dev
messages: + msg266582
2016-05-26 09:03:13martin.pantersetversions: + Python 3.5, Python 3.6, - Python 3.4
nosy: + martin.panter

messages: + msg266419

stage: patch review
2016-05-26 03:04:25luiz.poletosetfiles: + issue27124.patch

nosy: + luiz.poleto
messages: + msg266404

keywords: + patch
2016-05-25 16:23:49steven.dapranosetnosy: + steven.daprano
messages: + msg266371
2016-05-25 15:55:58Lennart Grahlsetmessages: + msg266370
title: binascii.a2b_hex raises binascii.Error, not TypeError -> binascii.a2b_hex raises binascii.Error and ValueError, not TypeError
2016-05-25 15:42:31Lennart Grahlcreate