From 469b6f8aff4cafe203d851b19bedfab0128e795a Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 21 Sep 2012 00:54:37 +0100 Subject: [PATCH] Return memoryview object of type "c" instead of "B" from bytea In Python 3.3 items are returned as int instead of chars. I'm not sure the way I did it is correct: worth asking some hardcore Python dev. Fixed tests after the stricter memview comparison rules in Py 3.3. --- psycopg/typecast_binary.c | 10 ++++++++-- tests/test_types_basic.py | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/psycopg/typecast_binary.c b/psycopg/typecast_binary.c index 49eb547f..322d1495 100644 --- a/psycopg/typecast_binary.c +++ b/psycopg/typecast_binary.c @@ -55,7 +55,6 @@ chunk_repr(chunkObject *self) #if PY_MAJOR_VERSION < 3 -/* XXX support 3.0 buffer protocol */ static Py_ssize_t chunk_getreadbuffer(chunkObject *self, Py_ssize_t segment, void **ptr) { @@ -90,9 +89,16 @@ static PyBufferProcs chunk_as_buffer = /* 3.0 buffer interface */ int chunk_getbuffer(PyObject *_self, Py_buffer *view, int flags) { + int rv; chunkObject *self = (chunkObject*)_self; - return PyBuffer_FillInfo(view, _self, self->base, self->len, 1, flags); + rv = PyBuffer_FillInfo(view, _self, self->base, self->len, 1, flags); + if (rv == 0) { + /* TODO: is this really the way to do it? */ + view->format = "c"; + } + return rv; } + static PyBufferProcs chunk_as_buffer = { chunk_getbuffer, diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index e442fc62..c2a47e93 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -126,7 +126,7 @@ class TypesBasicTests(unittest.TestCase): s = bytes(range(256)) b = psycopg2.Binary(s) buf = self.execute("SELECT %s::bytea AS foo", (b,)) - self.assertEqual(s, buf) + self.assertEqual(s, buf.tobytes()) def testBinaryNone(self): b = psycopg2.Binary(None) @@ -154,7 +154,7 @@ class TypesBasicTests(unittest.TestCase): s = bytes(range(256)) buf = self.execute("SELECT %s::bytea AS foo", (psycopg2.Binary(s),)) buf2 = self.execute("SELECT %s::bytea AS foo", (buf,)) - self.assertEqual(s, buf2) + self.assertEqual(s, buf2.tobytes()) def testArray(self): s = self.execute("SELECT %s AS foo", ([[1,2],[3,4]],))