A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2011-August/112919.html below:

[Python-Dev] memoryview: "B", "c", "b" format specifiers

[Python-Dev] memoryview: "B", "c", "b" format specifiersStefan Krah stefan at bytereef.org
Thu Aug 18 18:22:54 CEST 2011
Hello,

during my work on PEP-3118 fixes I noticed that memoryview does not handle
the "B" format specifier according to the struct module documentation:


Here's what struct does:

>>> b = bytearray([1,2,3])
>>> struct.pack_into('B', b, 0, b'X')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: required argument is not an integer
>>> struct.pack_into('c', b, 0, b'X')
>>> b
bytearray(b'X\x02\x03')


Here's what memoryview does:

>>> b = bytearray([1,2,3])
>>> m = memoryview(b)
>>> m.format
'B'
>>> m[0] = b'X'
>>> m[0] = 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' does not support the buffer interface


So, memoryview does exactly the opposite of what is specified. It should
reject the bytes object but accept the integer.


I would like to fix this in the features/pep-3118 repository as follows:

  - memoryview should respect the format specifiers.

  - bytearray and friends should set the format specifier to "c"
    in their getbuffer() methods.

  - Introduce a new function PyMemoryView_FromBytes() that can be used
    instead of PyMemoryView_FromBuffer(). PyMemoryView_FromBuffer()
    is usually used in conjunction with PyBuffer_FillInfo(), which
    sets the format specifier to "B".


Are there any general objections to this?


Stefan Krah


More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4