From 836f8a1aa1b9ae5b1406218f697d385cbb3c3b92 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 18 Feb 2011 14:19:57 +0000 Subject: [PATCH] Make Binary(None) work as expected, adapting to NULL Issue reported by Stefano Dal Pra. --- psycopg/adapter_binary.c | 6 ++++++ tests/types_basic.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/psycopg/adapter_binary.c b/psycopg/adapter_binary.c index 00bdb9ee..2574c603 100644 --- a/psycopg/adapter_binary.c +++ b/psycopg/adapter_binary.c @@ -113,6 +113,12 @@ exit: if (got_view) { PyBuffer_Release(&view); } #endif + /* Allow Binary(None) to work */ + if (self->wrapped == Py_None) { + Py_INCREF(psyco_null); + rv = psyco_null; + } + /* if the wrapped object is not bytes or a buffer, this is an error */ if (!rv && !PyErr_Occurred()) { PyErr_Format(PyExc_TypeError, "can't escape %s to binary", diff --git a/tests/types_basic.py b/tests/types_basic.py index f956f90a..5321a0c8 100755 --- a/tests/types_basic.py +++ b/tests/types_basic.py @@ -128,6 +128,11 @@ class TypesBasicTests(unittest.TestCase): buf = self.execute("SELECT %s::bytea AS foo", (b,)) self.assertEqual(s, buf) + def testBinaryNone(self): + b = psycopg2.Binary(None) + buf = self.execute("SELECT %s::bytea AS foo", (b,)) + self.assertEqual(buf, None) + def testBinaryEmptyString(self): # test to make sure an empty Binary is converted to an empty string if sys.version_info[0] < 3: