Issue 7827: recv_into() argument 1 must be pinned buffer, not bytearray (original) (raw)

In Python 2.6 and Python 2.7a2+, I can't socket.recv_into(a byte array instance).

I get a TypeError which complains about a "pinned buffer". I have only an inkling of what that means. Since an array.array("b") works there, and since it works in Python 3.1.1, and since I thought the point of a bytearray was to make things like recv_into easier, I think this exception is a bug in Python 2.6 and 2.7.

Here's my reproducibles:

Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import socket sock = socket.socket() sock.connect( ("python.org", 80) ) sock.send(b"GET / HTTP/1.0\r\n\r\n") 18 buf = bytearray(b" " * 10) sock.recv_into(buf)

Traceback (most recent call last): File "", line 1, in TypeError: recv_into() argument 1 must be pinned buffer, not bytearray

I expected a bytearray to work there. In fact, I thought the point of bytearray was to allow this to work. By comparison, an array of bytes does work:

import array arr = array.array("b") arr.extend(map(ord, "This is a test")) len(arr) 14 sock.recv_into(arr) 14 arr array('b', [72, 84, 84, 80, 47, 49, 46, 49, 32, 51, 48, 50, 32, 70]) "".join(map(chr, arr)) 'HTTP/1.1 302 F'

I don't even know what a "pinned buffer" means, and searching python.org isn't helpful.

Using a bytearray in Python 3.1.1 does work: Python 3.1.1 (r311:74480, Jan 31 2010, 23:07:16) [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import socket sock = socket.socket() sock.connect( ("python.org", 80) ) sock.send(b"GET / HTTP/1.0\r\n\r\n") 18 buf = bytearray(b" " * 10) sock.recv_into(buf) 10 buf bytearray(b'HTTP/1.1 3')

For reference, here's an example with 2.7a2+ (freshly built out of version control) showing that it does not work there.

Python 2.7a2+ (trunk:74969:77901M, Feb 1 2010, 02:44:24) [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import socket sock = socket.socket() sock.connect( ("python.org", 80) ) b = bytearray(b" " * 10) sock.recv_into(b) Traceback (most recent call last): File "", line 1, in TypeError: recv_into() argument 1 must be pinned buffer, not bytearray

Seems to be fixed in 2.7, although I'm not sure when exactly :

Python 2.7.2 (default, Oct 21 2011, 22:13:39) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import socket sock = socket.socket() sock.connect( ("python.org", 80) ) sock.send(b"GET / HTTP/1.0\r\n\r\n") 18 buf = bytearray(b" " * 10) sock.recv_into(buf) 10 print buf HTTP/1.1 3