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

@ -50,7 +50,7 @@ def TimestampFromTicks(ticks):
version_info = tuple([int(x) for x in version.split(".")])
sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")])
Binary = buffer
Binary = memoryview
def register_adapters_and_converters():
def adapt_date(val):

View file

@ -593,7 +593,7 @@ class ConstructorTests(unittest.TestCase):
ts = sqlite.TimestampFromTicks(42)
def CheckBinary(self):
b = sqlite.Binary(chr(0) + "'")
b = sqlite.Binary(b"\0'")
class ExtensionTests(unittest.TestCase):
def CheckScriptStringSql(self):

View file

@ -62,7 +62,7 @@ class SqliteTypeTests(unittest.TestCase):
self.failUnlessEqual(row[0], val)
def CheckBlob(self):
val = buffer(b"Guglhupf")
val = memoryview(b"Guglhupf")
self.cur.execute("insert into test(b) values (?)", (val,))
self.cur.execute("select b from test")
row = self.cur.fetchone()
@ -203,7 +203,7 @@ class DeclTypesTests(unittest.TestCase):
def CheckBlob(self):
# default
val = buffer(b"Guglhupf")
val = memoryview(b"Guglhupf")
self.cur.execute("insert into test(bin) values (?)", (val,))
self.cur.execute("select bin from test")
row = self.cur.fetchone()
@ -305,7 +305,7 @@ class BinaryConverterTests(unittest.TestCase):
def CheckBinaryInputForConverter(self):
testdata = b"abcdefg" * 10
result = self.con.execute('select ? as "x [bin]"', (buffer(bz2.compress(testdata)),)).fetchone()[0]
result = self.con.execute('select ? as "x [bin]"', (memoryview(bz2.compress(testdata)),)).fetchone()[0]
self.failUnlessEqual(testdata, result)
class DateTimeTests(unittest.TestCase):

View file

@ -36,7 +36,7 @@ def func_returnfloat():
def func_returnnull():
return None
def func_returnblob():
return buffer(b"blob")
return b"blob"
def func_raiseexception():
5/0
@ -49,7 +49,7 @@ def func_isfloat(v):
def func_isnone(v):
return type(v) is type(None)
def func_isblob(v):
return type(v) is buffer
return isinstance(v, (bytes, memoryview))
class AggrNoStep:
def __init__(self):
@ -100,7 +100,8 @@ class AggrCheckType:
self.val = None
def step(self, whichType, val):
theType = {"str": str, "int": int, "float": float, "None": type(None), "blob": buffer}
theType = {"str": str, "int": int, "float": float, "None": type(None),
"blob": bytes}
self.val = int(theType[whichType] is type(val))
def finalize(self):
@ -196,8 +197,8 @@ class FunctionTests(unittest.TestCase):
cur = self.con.cursor()
cur.execute("select returnblob()")
val = cur.fetchone()[0]
self.failUnlessEqual(type(val), buffer)
self.failUnlessEqual(val, buffer(b"blob"))
self.failUnlessEqual(type(val), bytes)
self.failUnlessEqual(val, memoryview(b"blob"))
def CheckFuncException(self):
cur = self.con.cursor()
@ -234,7 +235,7 @@ class FunctionTests(unittest.TestCase):
def CheckParamBlob(self):
cur = self.con.cursor()
cur.execute("select isblob(?)", (buffer(b"blob"),))
cur.execute("select isblob(?)", (memoryview(b"blob"),))
val = cur.fetchone()[0]
self.failUnlessEqual(val, 1)
@ -252,7 +253,7 @@ class AggregateTests(unittest.TestCase):
)
""")
cur.execute("insert into test(t, i, f, n, b) values (?, ?, ?, ?, ?)",
("foo", 5, 3.14, None, buffer(b"blob"),))
("foo", 5, 3.14, None, memoryview(b"blob"),))
self.con.create_aggregate("nostep", 1, AggrNoStep)
self.con.create_aggregate("nofinalize", 1, AggrNoFinalize)
@ -344,7 +345,7 @@ class AggregateTests(unittest.TestCase):
def CheckAggrCheckParamBlob(self):
cur = self.con.cursor()
cur.execute("select checkType('blob', ?)", (buffer(b"blob"),))
cur.execute("select checkType('blob', ?)", (memoryview(b"blob"),))
val = cur.fetchone()[0]
self.failUnlessEqual(val, 1)