diff --git a/ChangeLog b/ChangeLog index f3a1d875..5d89c7b7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2006-09-01 Federico Di Gregorio + * psycopg/adapter_binary.c: applied patch from jdahlin (#119) + to fix the segfault on empty binary buffers. + * psycopg/connection_type.c: added .status attribute to expose the internal status. diff --git a/psycopg/adapter_binary.c b/psycopg/adapter_binary.c index e380b2be..dcff6ca6 100644 --- a/psycopg/adapter_binary.c +++ b/psycopg/adapter_binary.c @@ -136,11 +136,14 @@ binary_quote(binaryObject *self) const char *buffer; int buffer_len; size_t len = 0; - + + if (self->buffer == NULL) + self->buffer = PyString_FromString(""); /* if we got a plain string or a buffer we escape it and save the buffer */ - if (PyString_Check(self->wrapped) || PyBuffer_Check(self->wrapped)) { + else if (PyString_Check(self->wrapped) || PyBuffer_Check(self->wrapped)) { /* escape and build quoted buffer */ PyObject_AsCharBuffer(self->wrapped, &buffer, &buffer_len); + to = (char *)binary_escape((unsigned char*)buffer, buffer_len, &len, self->conn ? ((connectionObject*)self->conn)->pgconn : NULL); if (to == NULL) { diff --git a/tests/types_basic.py b/tests/types_basic.py index 3abaf927..8a867df0 100644 --- a/tests/types_basic.py +++ b/tests/types_basic.py @@ -63,6 +63,9 @@ class TypesBasicTests(TestCase): r = str(self.execute("SELECT %s::bytea AS foo", (b,))) self.failUnless(r == s, "wrong binary quoting") + b = psycopg2.Binary('') + self.assertEqual(str(b), '') + def testArray(self): s = self.execute("SELECT %s AS foo", ([[1,2],[3,4]],)) self.failUnless(s == [[1,2],[3,4]], "wrong array quoting " + str(s))