From 96156727c0fff7795f0163e9134ced0e93ce13c4 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 19 Oct 2019 16:28:10 +0100 Subject: [PATCH] Allow parsing boolean both upper and lowercase Reportedly useful on H2 databases. Close #965 --- psycopg/typecast_basic.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/psycopg/typecast_basic.c b/psycopg/typecast_basic.c index 0f0fb929..51ba2458 100644 --- a/psycopg/typecast_basic.c +++ b/psycopg/typecast_basic.c @@ -114,16 +114,27 @@ typecast_UNICODE_cast(const char *s, Py_ssize_t len, PyObject *curs) static PyObject * typecast_BOOLEAN_cast(const char *s, Py_ssize_t len, PyObject *curs) { - PyObject *res; + PyObject *res = NULL; if (s == NULL) { Py_RETURN_NONE; } - if (s[0] == 't') - res = Py_True; - else - res = Py_False; + switch (s[0]) { + case 't': + case 'T': + res = Py_True; + break; - Py_INCREF(res); + case 'f': + case 'F': + res = Py_False; + break; + + default: + PyErr_Format(InterfaceError, "can't parse boolean: '%s'", s); + break; + } + + Py_XINCREF(res); return res; }