[New-bugs-announce] [issue29178] Adding bytes.frombuffer(byteslike) constructor

INADA Naoki report at bugs.python.org
Fri Jan 6 06:16:17 EST 2017


New submission from INADA Naoki:

# Summary

## 1. Making bytes from slice of bytearray easy and efficient.

bs = bytes(memoryview(bytelike)[start:end]) works fine on CPython,
but it will cause issue on PyPy.

Since memoryview is not closed explicitly, exception like
"BufferError: Existing exports of data: object cannot be re-sized"
will be raised after it.
Where exception raised can be far from where unclosed memoryview is leaked.


## 2. bytes(x) constructor is too overloaded.

It has undocumented corner cases. See PEP 467 and #29159


# ML threads

https://mail.python.org/pipermail/python-dev/2016-October/146668.html
https://mail.python.org/pipermail/python-dev/2017-January/147109.html

+1 from: Nathaniel Smith, Alexander Belopolsky, Yury Selivanov
-1 from: Nick Coghlan

Nick proposed put it on separated module, instead of adding it as builtin method.


# First draft patch

bytes-frombuffer.patch is first draft patch.  It implements frombuffer to only bytes,
with signature proposed first. Only C-contiguous buffer is supported for now.

  frombuffer(byteslike, length=-1, offset=0) method of builtins.type instance
    Create a bytes object from bytes-like object.

    Examples:
        bytes.frombuffer(b'abcd') -> b'abcd'
        bytes.frombuffer(b'abcd', 2) -> b'ab'
        bytes.frombuffer(b'abcd', 8) -> b'abcd'
        bytes.frombuffer(b'abcd', offset=2) -> b'cd'
        bytes.frombuffer(b'abcd', 1, 2) -> b'c'

----------
components: Interpreter Core
files: bytes-frombuffer.patch
keywords: patch
messages: 284813
nosy: Yury.Selivanov, belopolsky, haypo, inada.naoki, ncoghlan
priority: normal
severity: normal
status: open
title: Adding bytes.frombuffer(byteslike) constructor
type: enhancement
versions: Python 3.7
Added file: http://bugs.python.org/file46173/bytes-frombuffer.patch

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


More information about the New-bugs-announce mailing list