From 3214c23f51c8effa7e78f9a7f59735c5b3e10868 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 25 Dec 2010 12:03:15 +0100 Subject: [PATCH] Fixed adaptation in several adapters. The getquoted methods always return bytes. The str() convert this representation to string on the fly. --- psycopg/adapter_asis.c | 21 ++++++++++++++++----- psycopg/adapter_binary.c | 12 ++++-------- psycopg/adapter_datetime.c | 6 +++--- psycopg/adapter_list.c | 2 +- psycopg/adapter_pboolean.c | 14 +++++++------- psycopg/adapter_pdecimal.c | 20 ++++++++++++++------ psycopg/adapter_pfloat.c | 27 ++++++++++++++++++++------- psycopg/adapter_qstring.c | 6 +++--- psycopg/microprotocols.c | 2 +- 9 files changed, 69 insertions(+), 41 deletions(-) diff --git a/psycopg/adapter_asis.c b/psycopg/adapter_asis.c index 7c7cb811..bc64aa74 100644 --- a/psycopg/adapter_asis.c +++ b/psycopg/adapter_asis.c @@ -35,20 +35,31 @@ /** the AsIs object **/ static PyObject * -asis_str(asisObject *self) +asis_getquoted(asisObject *self, PyObject *args) { + PyObject *rv; if (self->wrapped == Py_None) { - return Bytes_FromString("NULL"); + rv = Bytes_FromString("NULL"); } else { - return PyObject_Str(self->wrapped); + rv = PyObject_Str(self->wrapped); +#if PY_MAJOR_VERSION > 2 + /* unicode to bytes in Py3 */ + if (rv) { + PyObject *tmp = PyUnicode_AsUTF8String(rv); + Py_DECREF(rv); + rv = tmp; + } +#endif } + + return rv; } static PyObject * -asis_getquoted(asisObject *self, PyObject *args) +asis_str(asisObject *self) { - return asis_str(self); + return psycopg_ensure_text(asis_getquoted(self, NULL)); } static PyObject * diff --git a/psycopg/adapter_binary.c b/psycopg/adapter_binary.c index 6fa7b590..ccfaf240 100644 --- a/psycopg/adapter_binary.c +++ b/psycopg/adapter_binary.c @@ -98,27 +98,23 @@ binary_quote(binaryObject *self) } /* binary_str, binary_getquoted - return result of quoting */ -/* XXX what is the point of this method? */ + static PyObject * -binary_str(binaryObject *self) +binary_getquoted(binaryObject *self, PyObject *args) { if (self->buffer == NULL) { if (!(binary_quote(self))) { return NULL; } } -#if PY_MAJOR_VERSION < 3 Py_INCREF(self->buffer); return self->buffer; -#else - return PyUnicode_FromEncodedObject(self->buffer, "ascii", "replace"); -#endif } static PyObject * -binary_getquoted(binaryObject *self, PyObject *args) +binary_str(binaryObject *self) { - return binary_str(self); + return psycopg_ensure_text(binary_getquoted(self, NULL)); } static PyObject * diff --git a/psycopg/adapter_datetime.c b/psycopg/adapter_datetime.c index 12547b05..56be0c9b 100644 --- a/psycopg/adapter_datetime.c +++ b/psycopg/adapter_datetime.c @@ -119,7 +119,7 @@ _pydatetime_string_delta(pydatetimeObject *self) } static PyObject * -pydatetime_str(pydatetimeObject *self) +pydatetime_getquoted(pydatetimeObject *self, PyObject *args) { if (self->type <= PSYCO_DATETIME_TIMESTAMP) { return _pydatetime_string_date_time(self); @@ -130,9 +130,9 @@ pydatetime_str(pydatetimeObject *self) } static PyObject * -pydatetime_getquoted(pydatetimeObject *self, PyObject *args) +pydatetime_str(pydatetimeObject *self) { - return pydatetime_str(self); + return psycopg_ensure_text(pydatetime_getquoted(self, NULL)); } static PyObject * diff --git a/psycopg/adapter_list.c b/psycopg/adapter_list.c index 522c49dd..b7e1f967 100644 --- a/psycopg/adapter_list.c +++ b/psycopg/adapter_list.c @@ -83,7 +83,7 @@ list_quote(listObject *self) static PyObject * list_str(listObject *self) { - return list_quote(self); + return psycopg_ensure_text(list_quote(self)); } static PyObject * diff --git a/psycopg/adapter_pboolean.c b/psycopg/adapter_pboolean.c index cdd3ef4e..4e2c4464 100644 --- a/psycopg/adapter_pboolean.c +++ b/psycopg/adapter_pboolean.c @@ -35,29 +35,29 @@ /** the Boolean object **/ static PyObject * -pboolean_str(pbooleanObject *self) +pboolean_getquoted(pbooleanObject *self, PyObject *args) { #ifdef PSYCOPG_NEW_BOOLEAN if (PyObject_IsTrue(self->wrapped)) { - return Text_FromUTF8("true"); + return Bytes_FromString("true"); } else { - return Text_FromUTF8("false"); + return Bytes_FromString("false"); } #else if (PyObject_IsTrue(self->wrapped)) { - return Text_FromUTF8("'t'"); + return Bytes_FromString("'t'"); } else { - return Text_FromUTF8("'f'"); + return Bytes_FromString("'f'"); } #endif } static PyObject * -pboolean_getquoted(pbooleanObject *self, PyObject *args) +pboolean_str(pbooleanObject *self) { - return pboolean_str(self); + return psycopg_ensure_text(pboolean_getquoted(self, NULL)); } static PyObject * diff --git a/psycopg/adapter_pdecimal.c b/psycopg/adapter_pdecimal.c index fa4acfab..9b573467 100644 --- a/psycopg/adapter_pdecimal.c +++ b/psycopg/adapter_pdecimal.c @@ -36,7 +36,7 @@ /** the Decimal object **/ static PyObject * -pdecimal_str(pdecimalObject *self) +pdecimal_getquoted(pdecimalObject *self, PyObject *args) { PyObject *check, *res = NULL; check = PyObject_CallMethod(self->wrapped, "is_finite", NULL); @@ -45,7 +45,7 @@ pdecimal_str(pdecimalObject *self) goto end; } else if (check) { - res = Text_FromUTF8("'NaN'::numeric"); + res = Bytes_FromString("'NaN'::numeric"); goto end; } @@ -57,7 +57,7 @@ pdecimal_str(pdecimalObject *self) goto end; } if (PyObject_IsTrue(check)) { - res = Text_FromUTF8("'NaN'::numeric"); + res = Bytes_FromString("'NaN'::numeric"); goto end; } @@ -66,11 +66,19 @@ pdecimal_str(pdecimalObject *self) goto end; } if (PyObject_IsTrue(check)) { - res = Text_FromUTF8("'NaN'::numeric"); + res = Bytes_FromString("'NaN'::numeric"); goto end; } res = PyObject_Str(self->wrapped); +#if PY_MAJOR_VERSION > 2 + /* unicode to bytes in Py3 */ + if (res) { + PyObject *tmp = PyUnicode_AsUTF8String(res); + Py_DECREF(res); + res = tmp; + } +#endif end: Py_XDECREF(check); @@ -78,9 +86,9 @@ end: } static PyObject * -pdecimal_getquoted(pdecimalObject *self, PyObject *args) +pdecimal_str(pdecimalObject *self) { - return pdecimal_str(self); + return psycopg_ensure_text(pdecimal_getquoted(self, NULL)); } static PyObject * diff --git a/psycopg/adapter_pfloat.c b/psycopg/adapter_pfloat.c index 3260d2cd..2753737a 100644 --- a/psycopg/adapter_pfloat.c +++ b/psycopg/adapter_pfloat.c @@ -36,21 +36,34 @@ /** the Float object **/ static PyObject * -pfloat_str(pfloatObject *self) +pfloat_getquoted(pfloatObject *self, PyObject *args) { + PyObject *rv; double n = PyFloat_AsDouble(self->wrapped); if (isnan(n)) - return Text_FromUTF8("'NaN'::float"); + rv = Bytes_FromString("'NaN'::float"); else if (isinf(n)) - return Text_FromUTF8("'Infinity'::float"); - else - return PyObject_Repr(self->wrapped); + rv = Bytes_FromString("'Infinity'::float"); + else { + rv = PyObject_Repr(self->wrapped); + +#if PY_MAJOR_VERSION > 2 + /* unicode to bytes in Py3 */ + if (rv) { + PyObject *tmp = PyUnicode_AsUTF8String(rv); + Py_DECREF(rv); + rv = tmp; + } +#endif + } + + return rv; } static PyObject * -pfloat_getquoted(pfloatObject *self, PyObject *args) +pfloat_str(pfloatObject *self) { - return pfloat_str(self); + return psycopg_ensure_text(pfloat_getquoted(self, NULL)); } static PyObject * diff --git a/psycopg/adapter_qstring.c b/psycopg/adapter_qstring.c index 34ed4101..a9d4ec6e 100644 --- a/psycopg/adapter_qstring.c +++ b/psycopg/adapter_qstring.c @@ -106,7 +106,7 @@ qstring_quote(qstringObject *self) /* qstring_str, qstring_getquoted - return result of quoting */ static PyObject * -qstring_str(qstringObject *self) +qstring_getquoted(qstringObject *self, PyObject *args) { if (self->buffer == NULL) { qstring_quote(self); @@ -116,9 +116,9 @@ qstring_str(qstringObject *self) } static PyObject * -qstring_getquoted(qstringObject *self, PyObject *args) +qstring_str(qstringObject *self) { - return qstring_str(self); + return psycopg_ensure_text(qstring_getquoted(self, NULL)); } static PyObject * diff --git a/psycopg/microprotocols.c b/psycopg/microprotocols.c index 45b26f85..2173815d 100644 --- a/psycopg/microprotocols.c +++ b/psycopg/microprotocols.c @@ -138,7 +138,7 @@ microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt) /* None is always adapted to NULL */ if (obj == Py_None) - return Text_FromUTF8("NULL"); + return Bytes_FromString("NULL"); Dprintf("microprotocols_adapt: trying to adapt %s", Py_TYPE(obj)->tp_name);