diff --git a/ChangeLog b/ChangeLog index 12cc6ab3..cac156d8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2010-05-03 Daniele Varrazzo + + * psycopg/adapter_binary.c: Adapt buffer objects using an explicit cast on + the string literal (bug reported by Peter Eisentraut) + 2010-04-20 Daniele Varrazzo * lib/pqpath.c: Fixed reference leak in notify reception. diff --git a/psycopg/adapter_binary.c b/psycopg/adapter_binary.c index ba39ebcb..36372bca 100644 --- a/psycopg/adapter_binary.c +++ b/psycopg/adapter_binary.c @@ -157,9 +157,9 @@ binary_quote(binaryObject *self) if (len > 0) self->buffer = PyString_FromFormat( (self->conn && ((connectionObject*)self->conn)->equote) - ? "E'%s'" : "'%s'" , to); + ? "E'%s'::bytea" : "'%s'::bytea" , to); else - self->buffer = PyString_FromString("''"); + self->buffer = PyString_FromString("''::bytea"); PQfreemem(to); } diff --git a/tests/types_basic.py b/tests/types_basic.py index aaca2374..f59e4cf5 100755 --- a/tests/types_basic.py +++ b/tests/types_basic.py @@ -96,7 +96,7 @@ class TypesBasicTests(unittest.TestCase): def testBinaryEmptyString(self): # test to make sure an empty Binary is converted to an empty string b = psycopg2.Binary('') - self.assertEqual(str(b), "''") + self.assertEqual(str(b), "''::bytea") def testBinaryRoundTrip(self): # test to make sure buffers returned by psycopg2 are @@ -113,6 +113,21 @@ class TypesBasicTests(unittest.TestCase): self.failUnless(s == ['one', 'two', 'three'], "wrong array quoting " + str(s)) + def testTypeRoundtripBinary(self): + o1 = buffer("".join(map(chr, range(256)))) + o2 = self.execute("select %s;", (o1,)) + self.assertEqual(type(o1), type(o2)) + + # Test with an empty buffer + o1 = buffer("") + o2 = self.execute("select %s;", (o1,)) + self.assertEqual(type(o1), type(o2)) + + def testTypeRoundtripBinaryArray(self): + o1 = buffer("".join(map(chr, range(256)))) + o1 = [o1] + o2 = self.execute("select %s;", (o1,)) + self.assertEqual(type(o1[0]), type(o2[0])) def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__)