summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-12-25 12:03:15 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-12-31 03:18:27 +0100
commit3214c23f51c8effa7e78f9a7f59735c5b3e10868 (patch)
treee11d356c8ee4a70c7ab80cd6dbebe1b943a87693
parent2e22eef727fc3b57e63c7a6a1b5cbae9533cd632 (diff)
downloadpsycopg2-3214c23f51c8effa7e78f9a7f59735c5b3e10868.tar.gz
Fixed adaptation in several adapters.
The getquoted methods always return bytes. The str() convert this representation to string on the fly.
-rw-r--r--psycopg/adapter_asis.c21
-rw-r--r--psycopg/adapter_binary.c12
-rw-r--r--psycopg/adapter_datetime.c6
-rw-r--r--psycopg/adapter_list.c2
-rw-r--r--psycopg/adapter_pboolean.c14
-rw-r--r--psycopg/adapter_pdecimal.c20
-rw-r--r--psycopg/adapter_pfloat.c27
-rw-r--r--psycopg/adapter_qstring.c6
-rw-r--r--psycopg/microprotocols.c2
9 files changed, 69 insertions, 41 deletions
diff --git a/psycopg/adapter_asis.c b/psycopg/adapter_asis.c
index 7c7cb81..bc64aa7 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 6fa7b59..ccfaf24 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 12547b0..56be0c9 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 522c49d..b7e1f96 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 cdd3ef4..4e2c446 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 fa4acfa..9b57346 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 3260d2c..2753737 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 34ed410..a9d4ec6 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 45b26f8..2173815 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);