Fixed adaptation in several adapters.

The getquoted methods always return bytes. The str() convert this
representation to string on the fly.
This commit is contained in:
Daniele Varrazzo 2010-12-25 12:03:15 +01:00
parent 2e22eef727
commit 3214c23f51
9 changed files with 69 additions and 41 deletions

View File

@ -35,20 +35,31 @@
/** the AsIs object **/ /** the AsIs object **/
static PyObject * static PyObject *
asis_str(asisObject *self) asis_getquoted(asisObject *self, PyObject *args)
{ {
PyObject *rv;
if (self->wrapped == Py_None) { if (self->wrapped == Py_None) {
return Bytes_FromString("NULL"); rv = Bytes_FromString("NULL");
} }
else { 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 * 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 * static PyObject *

View File

@ -98,27 +98,23 @@ binary_quote(binaryObject *self)
} }
/* binary_str, binary_getquoted - return result of quoting */ /* binary_str, binary_getquoted - return result of quoting */
/* XXX what is the point of this method? */
static PyObject * static PyObject *
binary_str(binaryObject *self) binary_getquoted(binaryObject *self, PyObject *args)
{ {
if (self->buffer == NULL) { if (self->buffer == NULL) {
if (!(binary_quote(self))) { if (!(binary_quote(self))) {
return NULL; return NULL;
} }
} }
#if PY_MAJOR_VERSION < 3
Py_INCREF(self->buffer); Py_INCREF(self->buffer);
return self->buffer; return self->buffer;
#else
return PyUnicode_FromEncodedObject(self->buffer, "ascii", "replace");
#endif
} }
static PyObject * 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 * static PyObject *

View File

@ -119,7 +119,7 @@ _pydatetime_string_delta(pydatetimeObject *self)
} }
static PyObject * static PyObject *
pydatetime_str(pydatetimeObject *self) pydatetime_getquoted(pydatetimeObject *self, PyObject *args)
{ {
if (self->type <= PSYCO_DATETIME_TIMESTAMP) { if (self->type <= PSYCO_DATETIME_TIMESTAMP) {
return _pydatetime_string_date_time(self); return _pydatetime_string_date_time(self);
@ -130,9 +130,9 @@ pydatetime_str(pydatetimeObject *self)
} }
static PyObject * 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 * static PyObject *

View File

@ -83,7 +83,7 @@ list_quote(listObject *self)
static PyObject * static PyObject *
list_str(listObject *self) list_str(listObject *self)
{ {
return list_quote(self); return psycopg_ensure_text(list_quote(self));
} }
static PyObject * static PyObject *

View File

@ -35,29 +35,29 @@
/** the Boolean object **/ /** the Boolean object **/
static PyObject * static PyObject *
pboolean_str(pbooleanObject *self) pboolean_getquoted(pbooleanObject *self, PyObject *args)
{ {
#ifdef PSYCOPG_NEW_BOOLEAN #ifdef PSYCOPG_NEW_BOOLEAN
if (PyObject_IsTrue(self->wrapped)) { if (PyObject_IsTrue(self->wrapped)) {
return Text_FromUTF8("true"); return Bytes_FromString("true");
} }
else { else {
return Text_FromUTF8("false"); return Bytes_FromString("false");
} }
#else #else
if (PyObject_IsTrue(self->wrapped)) { if (PyObject_IsTrue(self->wrapped)) {
return Text_FromUTF8("'t'"); return Bytes_FromString("'t'");
} }
else { else {
return Text_FromUTF8("'f'"); return Bytes_FromString("'f'");
} }
#endif #endif
} }
static PyObject * 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 * static PyObject *

View File

@ -36,7 +36,7 @@
/** the Decimal object **/ /** the Decimal object **/
static PyObject * static PyObject *
pdecimal_str(pdecimalObject *self) pdecimal_getquoted(pdecimalObject *self, PyObject *args)
{ {
PyObject *check, *res = NULL; PyObject *check, *res = NULL;
check = PyObject_CallMethod(self->wrapped, "is_finite", NULL); check = PyObject_CallMethod(self->wrapped, "is_finite", NULL);
@ -45,7 +45,7 @@ pdecimal_str(pdecimalObject *self)
goto end; goto end;
} }
else if (check) { else if (check) {
res = Text_FromUTF8("'NaN'::numeric"); res = Bytes_FromString("'NaN'::numeric");
goto end; goto end;
} }
@ -57,7 +57,7 @@ pdecimal_str(pdecimalObject *self)
goto end; goto end;
} }
if (PyObject_IsTrue(check)) { if (PyObject_IsTrue(check)) {
res = Text_FromUTF8("'NaN'::numeric"); res = Bytes_FromString("'NaN'::numeric");
goto end; goto end;
} }
@ -66,11 +66,19 @@ pdecimal_str(pdecimalObject *self)
goto end; goto end;
} }
if (PyObject_IsTrue(check)) { if (PyObject_IsTrue(check)) {
res = Text_FromUTF8("'NaN'::numeric"); res = Bytes_FromString("'NaN'::numeric");
goto end; goto end;
} }
res = PyObject_Str(self->wrapped); 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: end:
Py_XDECREF(check); Py_XDECREF(check);
@ -78,9 +86,9 @@ end:
} }
static PyObject * 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 * static PyObject *

View File

@ -36,21 +36,34 @@
/** the Float object **/ /** the Float object **/
static PyObject * static PyObject *
pfloat_str(pfloatObject *self) pfloat_getquoted(pfloatObject *self, PyObject *args)
{ {
PyObject *rv;
double n = PyFloat_AsDouble(self->wrapped); double n = PyFloat_AsDouble(self->wrapped);
if (isnan(n)) if (isnan(n))
return Text_FromUTF8("'NaN'::float"); rv = Bytes_FromString("'NaN'::float");
else if (isinf(n)) else if (isinf(n))
return Text_FromUTF8("'Infinity'::float"); rv = Bytes_FromString("'Infinity'::float");
else else {
return PyObject_Repr(self->wrapped); 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 * 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 * static PyObject *

View File

@ -106,7 +106,7 @@ qstring_quote(qstringObject *self)
/* qstring_str, qstring_getquoted - return result of quoting */ /* qstring_str, qstring_getquoted - return result of quoting */
static PyObject * static PyObject *
qstring_str(qstringObject *self) qstring_getquoted(qstringObject *self, PyObject *args)
{ {
if (self->buffer == NULL) { if (self->buffer == NULL) {
qstring_quote(self); qstring_quote(self);
@ -116,9 +116,9 @@ qstring_str(qstringObject *self)
} }
static PyObject * 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 * static PyObject *

View File

@ -138,7 +138,7 @@ microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
/* None is always adapted to NULL */ /* None is always adapted to NULL */
if (obj == Py_None) if (obj == Py_None)
return Text_FromUTF8("NULL"); return Bytes_FromString("NULL");
Dprintf("microprotocols_adapt: trying to adapt %s", Dprintf("microprotocols_adapt: trying to adapt %s",
Py_TYPE(obj)->tp_name); Py_TYPE(obj)->tp_name);