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.
This commit is contained in:
Daniele Varrazzo 2012-09-21 00:54:37 +01:00
parent 526e270934
commit 469b6f8aff
2 changed files with 10 additions and 4 deletions

View File

@ -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,

View File

@ -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]],))