[New-bugs-announce] [issue41707] Builtins like int() and float() should not blindly treat buffer protocol bytes as string characters.

Alex Mohr report at bugs.python.org
Thu Sep 3 16:51:59 EDT 2020


New submission from Alex Mohr <alexjmohr at gmail.com>:

Instead they should request and take into consideration the buffer object's data format.  For example, surely we don't want to treat floating point binary representations as string characters:

>>> from array import array
>>> a = array('f', [1.2, 2.3, 3.4])
>>> int(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: b'\x9a\x99\x99?33\x13@\x9a\x99Y@'

It's even a little dangerous, since you can get "lucky" with certain binary representations:

>>> a = array('I', [875770417, 875770420])
>>> int(a)
12344234

----------
components: Interpreter Core
messages: 376317
nosy: amohr
priority: normal
severity: normal
status: open
title: Builtins like int() and float() should not blindly treat buffer protocol bytes as string characters.
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41707>
_______________________________________


More information about the New-bugs-announce mailing list