[New-bugs-announce] [issue26759] PyBytes_FromObject accepts arbitrary iterable

Serhiy Storchaka report at bugs.python.org
Thu Apr 14 14:13:43 EDT 2016

New submission from Serhiy Storchaka:

PyBytes_FromObject creates a bytes object from an object that implements the buffer or the iterable protocol. But only using the buffer protocol is documented.

We should either document the current behavior (the documentation of int.from_bytes() can be used as a sample), or change the behavior to match the documentation.

For now PyBytes_FromObject() is used in the stdlib only for converting FS paths to str (besides using internally in bytes). When called from PyUnicode_FSDecoder(), this leads to accepting arbitrary iterables as filenames, that looks at leas strange (issue26754). In the posix module it is called only for objects that support the buffer protocol.

Thus the support of the iterable protocol is not used or misused in the stdlib. I don't know if it is used correctly in third party code, I suspect that this is rather misused. Note that there is alternative API function PyObject_Bytes(), that accepts same arguments as the bytes() constructor, except an integer, and supports the buffer protocol, the iterable protocol, and in additional supports the __bytes__() special method.

messages: 263423
nosy: haypo, martin.panter, pitrou, serhiy.storchaka
priority: normal
severity: normal
status: open
title: PyBytes_FromObject accepts arbitrary iterable
type: behavior
versions: Python 3.5, Python 3.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list