Make Binary(None) work as expected, adapting to NULL

Issue reported by Stefano Dal Pra.
This commit is contained in:
Daniele Varrazzo 2011-02-18 14:19:57 +00:00
parent 8530ef1793
commit 836f8a1aa1
2 changed files with 11 additions and 0 deletions

View File

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

View File

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