Breaking ground for PEP 3137 implementation:

Get rid of buffer().  Use memoryview() in its place where possible.
In a few places, do things a bit different, because memoryview()
can't slice (yet).
This commit is contained in:
Guido van Rossum 2007-10-08 02:46:15 +00:00
parent 85c1ba5d74
commit bae07c9baf
24 changed files with 72 additions and 199 deletions

View file

@ -6,7 +6,7 @@ import re
def dump(obj):
# helper function to dump memory contents in hex, with a hyphen
# between the bytes.
h = str(hexlify(buffer(obj)))
h = str(hexlify(memoryview(obj)))
return re.sub(r"(..)", r"\1-", h)[:-1]

View file

@ -4,7 +4,7 @@ from binascii import hexlify
from ctypes import *
def bin(s):
return str(hexlify(buffer(s))).upper()
return str(hexlify(memoryview(s))).upper()
# Each *simple* type that supports different byte orders has an
# __ctype_be__ attribute that specifies the same type in BIG ENDIAN

View file

@ -30,17 +30,17 @@ class StringArrayTestCase(unittest.TestCase):
buf.value = "Hello, World"
self.failUnlessEqual(buf.value, "Hello, World")
self.failUnlessRaises(TypeError, setattr, buf, "value", buffer("Hello, World"))
self.assertRaises(TypeError, setattr, buf, "value", buffer("abc"))
self.assertRaises(ValueError, setattr, buf, "raw", buffer("x" * 100))
self.failUnlessRaises(TypeError, setattr, buf, "value", memoryview(b"Hello, World"))
self.assertRaises(TypeError, setattr, buf, "value", memoryview(b"abc"))
self.assertRaises(ValueError, setattr, buf, "raw", memoryview(b"x" * 100))
def test_c_buffer_raw(self):
buf = c_buffer(32)
buf.raw = buffer(b"Hello, World")
buf.raw = memoryview(b"Hello, World")
self.failUnlessEqual(buf.value, "Hello, World")
self.assertRaises(TypeError, setattr, buf, "value", buffer("abc"))
self.assertRaises(ValueError, setattr, buf, "raw", buffer("x" * 100))
self.assertRaises(TypeError, setattr, buf, "value", memoryview(b"abc"))
self.assertRaises(ValueError, setattr, buf, "raw", memoryview(b"x" * 100))
def test_param_1(self):
BUF = c_char * 4