Thanks to visit codestin.com
Credit goes to github.com

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

Lines changed: 72 additions & 199 deletions

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):

0 commit comments

Comments
 (0)