8000 Breaking ground for PEP 3137 implementation: · python/cpython@bae07c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit bae07c9

Browse files
committed
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).
1 parent 85c1ba5 commit bae07c9

24 files changed

+72
-199
lines changed

Lib/_abcoll.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ def count(self, value):
491491

492492
Sequence.register(tuple)
493493
Sequence.register(basestring)
494-
Sequence.register(buffer)
494+
Sequence.register(memoryview)
495495

496496

497497
class MutableSequence(Sequence):

Lib/ctypes/test/test_array_in_pointer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def dump(obj):
77
# helper function to dump memory contents in hex, with a hyphen
88
# between the bytes.
9-
h = str(hexlify(buffer(obj)))
9+
h = str(hexlify(memoryview(obj)))
1010
return re.sub(r"(..)", r"\1-", h)[:-1]
1111

1212

Lib/ctypes/test/test_byteswap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from ctypes import *
55

66
def bin(s):
7-
return str(hexlify(buffer(s))).upper()
7+
return str(hexlify(memoryview(s))).upper()
88

99
# Each *simple* type that supports different byte orders has an
1010
# __ctype_be__ attribute that specifies the same type in BIG ENDIAN

Lib/ctypes/test/test_strings.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ def test_c_buffer_value(self):
3030
buf.value = "Hello, World"
3131
self.failUnlessEqual(buf.value, "Hello, World")
3232

33-
self.failUnlessRaises(TypeError, setattr, buf, "value", buffer("Hello, World"))
34-
self.assertRaises(TypeError, setattr, buf, "value", buffer("abc"))
35-
self.assertRaises(ValueError, setattr, buf, "raw", buffer("x" * 100))
33+
self.failUnlessRaises(TypeError, setattr, buf, "value", memoryview(b"Hello, World"))
34+
self.assertRaises(TypeError, setattr, buf, "value", memoryview(b"abc"))
35+
self.assertRaises(ValueError, setattr, buf, "raw", memoryview(b"x" * 100))
3636

3737
def test_c_buffer_raw(self):
3838
buf = c_buffer(32)
3939

40-
buf.raw = buffer(b"Hello, World")
40+
buf.raw = memoryview(b"Hello, World")
4141
self.failUnlessEqual(buf.value, "Hello, World")
42-
self.assertRaises(TypeError, setattr, buf, "value", buffer("abc"))
43-
self.assertRaises(ValueError, setattr, buf, "raw", buffer("x" * 100))
42+
self.assertRaises(TypeError, setattr, buf, "value", memoryview(b"abc"))
43+
self.assertRaises(ValueError, setattr, buf, "raw", memoryview(b"x" * 100))
4444

4545
def test_param_1(self):
4646
BUF = c_char * 4

Lib/sqlite3/dbapi2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def TimestampFromTicks(ticks):
5050
version_info = tuple([int(x) for x in version.split(".")])
5151
sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
5252

53-
Binary = buffer
53+
Binary = memoryview
5454

5555
def register_adapters_and_converters():
5656
def adapt_date(val):

Lib/sqlite3/test/dbapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ def CheckTimestampFromTicks(self):
593593
ts = sqlite.TimestampFromTicks(42)
594594

595595
def CheckBinary(self):
596-
b = sqlite.Binary(chr(0) + "'")
596+
b = sqlite.Binary(b"\0'")
597597

598598
class ExtensionTests(unittest.TestCase):
599599
def CheckScriptStringSql(self):

Lib/sqlite3/test/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def CheckFloat(self):
6262
self.failUnlessEqual(row[0], val)
6363

6464
def CheckBlob(self):
65-
val = buffer(b"Guglhupf")
65+
val = memoryview(b"Guglhupf")
6666
self.cur.execute("insert into test(b) values (?)", (val,))
6767
self.cur.execute("select b from test")
6868
row = self.cur.fetchone()
@@ -203,7 +203,7 @@ class Bar: pass
203203

204204
def CheckBlob(self):
205205
# default
206-
val = buffer(b"Guglhupf")
206+
val = memoryview(b"Guglhupf")
207207
self.cur.execute("insert into test(bin) values (?)", (val,))
208208
self.cur.execute("select bin from test")
209209
row = self.cur.fetchone()
@@ -305,7 +305,7 @@ def tearDown(self):
305305

306306
def CheckBinaryInputForConverter(self):
307307
testdata = b"abcdefg" * 10
308-
result = self.con.execute('select ? as "x [bin]"', (buffer(bz2.compress(testdata)),)).fetchone()[0]
308+
result = self.con.execute('select ? as "x [bin]"', (memoryview(bz2.compress(testdata)),)).fetchone()[0]
309309
self.failUnlessEqual(testdata, result)
310310

311311
class DateTimeTests(unittest.TestCase):

Lib/sqlite3/test/userfunctions.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def func_returnfloat():
3636
def func_returnnull():
3737
return None
3838
def func_returnblob():
39-
return buffer(b"blob")
39+
return b"blob"
4040
def func_raiseexception():
4141
5/0
4242

@@ -49,7 +49,7 @@ def func_isfloat(v):
4949
def func_isnone(v):
5050
return type(v) is type(None)
5151
def func_isblob(v):
52-
return type(v) is buffer
52+
return isinstance(v, (bytes, memoryview))
5353

5454
class AggrNoStep:
5555
def __init__(self):
@@ -100,7 +100,8 @@ def __init__(self):
100100
self.val = None
101101

102102
def step(self, whichType, val):
103-
theType = {"str": str, "int": int, "float": float, "None": type(None), "blob": buffer}
103+
theType = {"str": str, "int": int, "float": float, "None": type(None),
104+
"blob": bytes}
104105
self.val = int(theType[whichType] is type(val))
105106

106107
def finalize(self):
@@ -196,8 +197,8 @@ def CheckFuncReturnBlob(self):
196197
cur = self.con.cursor()
197198
cur.execute("select returnblob()")
198199
val = cur.fetchone()[0]
199-
self.failUnlessEqual(type(val), buffer)
200-
self.failUnlessEqual(val, buffer(b"blob"))
200+
self.failUnlessEqual(type(val), bytes)
201+
self.failUnlessEqual(val, memoryview(b"blob"))
201202

202203
def CheckFuncException(self):
203204
cur = self.con.cursor()
@@ -234,7 +235,7 @@ def CheckParamNone(self):
234235

235236
def CheckParamBlob(self):
236237
cur = self.con.cursor()
237-
cur.execute("select isblob(?)", (buffer(b"blob"),))
238+
cur.execute("select isblob(?)", (memoryview(b"blob"),))
238239
val = cur.fetchone()[0]
239240
self.failUnlessEqual(val, 1)
240241

@@ -252,7 +253,7 @@ def setUp(self):
252253
)
253254
""")
254255
cur.execute("insert into test(t, i, f, n, b) values (?, ?, ?, ?, ?)",
255-
("foo", 5, 3.14, None, buffer(b"blob"),))
256+
("foo", 5, 3.14, None, memoryview(b"blob"),))
256257

257258
self.con.create_aggregate("nostep", 1, AggrNoStep)
258259
self.con.create_aggregate("nofinalize", 1, AggrNoFinalize)
@@ -344,7 +345,7 @@ def CheckAggrCheckParamNone(self):
344345

345346
def CheckAggrCheckParamBlob(self):
346347
cur = self.con.cursor()
347-
cur.execute("select checkType('blob', ?)", (buffer(b"blob"),))
348+
cur.execute("select checkType('blob', ?)", (memoryview(b"blob"),))
348349
val = cur.fetchone()[0]
349350
self.failUnlessEqual(val, 1)
350351

Lib/subprocess.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,11 @@ def wait(self):
10411041

10421042

10431043
def _communicate(self, input):
1044-
if isinstance(input, str): # Unicode
1045-
input = input.encode("utf-8") # XXX What else?
1044+
if self.stdin:
1045+
if isinstance(input, str): # Unicode
1046+
input = input.encode("utf-8") # XXX What else?
1047+
if not isinstance(input, (bytes, str8)):
1048+
input = bytes(input)
10461049
read_set = []
10471050
write_set = []
10481051
stdout = None # Return
@@ -1071,7 +1074,8 @@ def _communicate(self, input):
10711074
# When select has indicated that the file is writable,
10721075
# we can write up to PIPE_BUF bytes without risk
10731076
# blocking. POSIX defines PIPE_BUF >= 512
1074-
bytes_written = os.write(self.stdin.fileno(), buffer(input, input_offset, 512))
1077+
chunk = input[input_offset : input_offset + 512]
1078+
bytes_written = os.write(self.stdin.fileno(), chunk)
10751079
input_offset += bytes_written
10761080
if input_offset >= len(input):
10771081
self.stdin.close()

Lib/test/test_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ def test_coveritertraverse(self):
708708

709709
def test_buffer(self):
710710
a = array.array(self.typecode, self.example)
711-
b = bytes(buffer(a))
711+
b = bytes(memoryview(a))
712712
self.assertEqual(b[0], a.tostring()[0])
713713

714714
def test_weakref(self):

Lib/test/test_buffer.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

Lib/test/test_bytes.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def test_decode(self):
343343

344344
def test_from_buffer(self):
345345
sample = str8("Hello world\n\x80\x81\xfe\xff")
346-
buf = buffer(sample)
346+
buf = memoryview(sample)
347347
b = bytes(buf)
348348
self.assertEqual(b, bytes(map(ord, sample)))
349349

@@ -456,8 +456,8 @@ def test_fromhex(self):
456456
b = bytes([0x1a, 0x2b, 0x30])
457457
self.assertEquals(bytes.fromhex('1a2B30'), b)
458458
self.assertEquals(bytes.fromhex(' 1A 2B 30 '), b)
459-
self.assertEquals(bytes.fromhex(buffer(b'')), bytes())
460-
self.assertEquals(bytes.fromhex(buffer(b'0000')), bytes([0, 0]))
459+
self.assertEquals(bytes.fromhex(memoryview(b'')), bytes())
460+
self.assertEquals(bytes.fromhex(memoryview(b'0000')), bytes([0, 0]))
461461
self.assertRaises(ValueError, bytes.fromhex, 'a')
462462
self.assertRaises(ValueError, bytes.fromhex, 'rt')
463463
self.assertRaises(ValueError, bytes.fromhex, '1a b cd')
@@ -630,7 +630,7 @@ def test_split_whitespace(self):
630630
self.assertEqual(b' a bb c '.split(None, 3), [b'a', b'bb', b'c'])
631631

632632
def test_split_buffer(self):
633-
self.assertEqual(b'a b'.split(buffer(b' ')), [b'a', b'b'])
633+
self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b'])
634634

635635
def test_split_string_error(self):
636636
self.assertRaises(TypeError, b'a b'.split, ' ')
@@ -653,7 +653,7 @@ def test_rsplit_whitespace(self):
653653
self.assertEqual(b' a bb c '.rsplit(None, 3), [b'a', b'bb', b'c'])
654654

655655
def test_rplit_buffer(self):
656-
self.assertEqual(b'a b'.rsplit(buffer(b' ')), [b'a', b'b'])
656+
self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b'])
657657

658658
def test_rplit_string_error(self):
659659
self.assertRaises(TypeError, b'a b'.rsplit, ' ')
@@ -707,9 +707,9 @@ def test_strip_whitespace(self):
707707
self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc')
708708

709709
def test_strip_buffer(self):
710-
self.assertEqual(b'abc'.strip(buffer(b'ac')), b'b')
711-
self.assertEqual(b'abc'.lstrip(buffer(b'ac')), b'bc')
712-
self.assertEqual(b'abc'.rstrip(buffer(b'ac')), b'ab')
710+
self.assertEqual(b'abc'.strip(memoryview(b'ac')), b'b')
711+
self.assertEqual(b'abc'.lstrip(memoryview(b'ac')), b'bc')
712+
self.assertEqual(b'abc'.rstrip(memoryview(b'ac')), b'ab')
713713

714714
def test_strip_string_error(self):
715715
self.assertRaises(TypeError, b'abc'.strip, 'b')

Lib/test/test_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def test_close_flushes(self):
251251

252252
def test_array_writes(self):
253253
a = array.array('i', range(10))
254-
n = len(buffer(a))
254+
n = len(memoryview(a))
255255
f = io.open(test_support.TESTFN, "wb", 0)
256256
self.assertEqual(f.write(a), n)
257257
f.close()

Lib/test/test_marshal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ def test_string(self):
9898
for s in ["", "Andr\xe8 Previn", "abc", " "*10000]:
9999
self.helper(s)
100100

101-
def test_buffer(self):
101+
def test_bytes(self):
102102
for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]:
103-
self.helper(buffer(s))
103+
self.helper(s)
104104

105105
class ExceptionTestCase(unittest.TestCase):
106106
def test_exceptions(self):

Lib/test/test_repr.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,6 @@ def test_nesting(self):
163163
eq(r([[[[[[{}]]]]]]), "[[[[[[{}]]]]]]")
164164
eq(r([[[[[[[{}]]]]]]]), "[[[[[[[...]]]]]]]")
165165

166-
def test_buffer(self):
167-
# XXX doesn't test buffers with no b_base or read-write buffers (see
168-
# bufferobject.c). The test is fairly incomplete too. Sigh.
169-
x = buffer('foo')
170-
self.failUnless(repr(x).startswith('<read-only buffer for 0x'))
171-
172166
def test_cell(self):
173167
# XXX Hmm? How to get at a cell object?
174168
pass

Lib/test/test_struct.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def test_1530559():
541541
test_1530559()
542542

543543
###########################################################################
544-
# Packing and unpacking to/from buffers.
544+
# Packing and unpacking to/from memory views.
545545

546546
# Copied and modified from unittest.
547547
def assertRaises(excClass, callableObj, *args, **kwargs):
@@ -556,7 +556,7 @@ def test_unpack_from():
556556
test_string = b'abcd01234'
557557
fmt = '4s'
558558
s = struct.Struct(fmt)
559-
for cls in (str, str8, buffer, bytes):
559+
for cls in (str, str8, bytes): # XXX + memoryview
560560
if verbose:
561561
print("test_unpack_from using", cls.__name__)
562562
data = cls(test_string)
@@ -567,7 +567,7 @@ def test_unpack_from():
567567
vereq(s.unpack_from(data, i), (data[i:i+4],))
568568
for i in range(6, len(test_string) + 1):
569569
simple_err(s.unpack_from, data, i)
570-
for cls in (str, buffer):
570+
for cls in (str, str8, bytes): # XXX + memoryview
571571
data = cls(test_string)
572572
vereq(struct.unpack_from(fmt, data), ('abcd',))
573573
vereq(struct.unpack_from(fmt, data, 2), ('cd01',))
@@ -619,19 +619,19 @@ def test_pack_into_fn():
619619
assertRaises(struct.error, pack_into, small_buf, 0, test_string)
620620
assertRaises(struct.error, pack_into, small_buf, 2, test_string)
621621

622-
def test_unpack_with_buffer():
622+
def test_unpack_with_memoryview():
623623
# SF bug 1563759: struct.unpack doens't support buffer protocol objects
624624
data1 = array.array('B', b'\x12\x34\x56\x78')
625-
data2 = buffer(b'......\x12\x34\x56\x78......', 6, 4)
625+
data2 = memoryview(b'\x12\x34\x56\x78') # XXX b'......XXXX......', 6, 4
626626
for data in [data1, data2]:
627627
value, = struct.unpack('>I', data)
628628
vereq(value, 0x12345678)
629629

630-
# Test methods to pack and unpack from buffers rather than strings.
630+
# Test methods to pack and unpack from memoryviews rather than strings.
631631
test_unpack_from()
632632
test_pack_into()
633633
test_pack_into_fn()
634-
test_unpack_with_buffer()
634+
test_unpack_with_memoryview()
635635

636636
def test_bool():
637637
for prefix in tuple("<>!=")+('',):

0 commit comments

Comments
 (0)
0