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 **/
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 *

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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);