From 56e4c2bd55239dc30d4feee6adafe851f13b8e24 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 24 Dec 2010 15:43:27 +0100 Subject: [PATCH] Redefining the microprotocol on Py3 as returning bytes. --- psycopg/adapter_asis.c | 2 +- psycopg/adapter_binary.c | 17 ++++++++++++----- psycopg/adapter_list.c | 10 +++------- psycopg/adapter_qstring.c | 5 ++--- psycopg/cursor_type.c | 6 +++--- psycopg/microprotocols.c | 15 ++++++++++++++- 6 files changed, 35 insertions(+), 20 deletions(-) diff --git a/psycopg/adapter_asis.c b/psycopg/adapter_asis.c index 1aa1d0da..7c7cb811 100644 --- a/psycopg/adapter_asis.c +++ b/psycopg/adapter_asis.c @@ -38,7 +38,7 @@ static PyObject * asis_str(asisObject *self) { if (self->wrapped == Py_None) { - return Text_FromUTF8("NULL"); + return Bytes_FromString("NULL"); } else { return PyObject_Str(self->wrapped); diff --git a/psycopg/adapter_binary.c b/psycopg/adapter_binary.c index 22105070..6fa7b590 100644 --- a/psycopg/adapter_binary.c +++ b/psycopg/adapter_binary.c @@ -62,6 +62,7 @@ binary_quote(binaryObject *self) #if PY_MAJOR_VERSION < 3 || PyBuffer_Check(self->wrapped) #else + || PyByteArray_Check(self->wrapped) || PyMemoryView_Check(self->wrapped) #endif ) { @@ -78,11 +79,11 @@ binary_quote(binaryObject *self) } if (len > 0) - self->buffer = PyString_FromFormat( + self->buffer = Bytes_FromFormat( (self->conn && ((connectionObject*)self->conn)->equote) ? "E'%s'::bytea" : "'%s'::bytea" , to); else - self->buffer = Text_FromUTF8("''::bytea"); + self->buffer = Bytes_FromString("''::bytea"); PQfreemem(to); } @@ -97,15 +98,21 @@ 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) { if (self->buffer == NULL) { - binary_quote(self); + if (!(binary_quote(self))) { + return NULL; + } } - Py_XINCREF(self->buffer); +#if PY_MAJOR_VERSION < 3 + Py_INCREF(self->buffer); return self->buffer; +#else + return PyUnicode_FromEncodedObject(self->buffer, "ascii", "replace"); +#endif } static PyObject * diff --git a/psycopg/adapter_list.c b/psycopg/adapter_list.c index 946dd221..522c49dd 100644 --- a/psycopg/adapter_list.c +++ b/psycopg/adapter_list.c @@ -53,7 +53,7 @@ list_quote(listObject *self) PyObject *quoted; PyObject *wrapped = PyList_GET_ITEM(self->wrapped, i); if (wrapped == Py_None) - quoted = Text_FromUTF8("NULL"); + quoted = Bytes_FromString("NULL"); else quoted = microprotocol_getquoted(wrapped, (connectionObject*)self->connection); @@ -67,15 +67,11 @@ list_quote(listObject *self) /* now that we have a tuple of adapted objects we just need to join them and put "ARRAY[] around the result */ - str = Text_FromUTF8(", "); + str = Bytes_FromString(", "); joined = PyObject_CallMethod(str, "join", "(O)", tmp); if (joined == NULL) goto error; -#if PY_MAJOR_VERSION < 3 - res = PyString_FromFormat("ARRAY[%s]", PyString_AsString(joined)); -#else - res = PyUnicode_FromFormat("ARRAY[%U]", joined); -#endif + res = Bytes_FromFormat("ARRAY[%s]", Bytes_AsString(joined)); error: Py_XDECREF(tmp); diff --git a/psycopg/adapter_qstring.c b/psycopg/adapter_qstring.c index c1082d8e..34ed4101 100644 --- a/psycopg/adapter_qstring.c +++ b/psycopg/adapter_qstring.c @@ -95,9 +95,8 @@ qstring_quote(qstringObject *self) Py_DECREF(str); return NULL; } - - /* XXX need to decode in connection's encoding in 3.0 */ - self->buffer = Text_FromUTF8AndSize(buffer, qlen); + + self->buffer = Bytes_FromStringAndSize(buffer, qlen); PyMem_Free(buffer); Py_DECREF(str); diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 922288b5..5290c238 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -144,7 +144,7 @@ _mogrify(PyObject *var, PyObject *fmt, connectionObject *conn, PyObject **new) optimization over the adapting code and can go away in the future if somebody finds a None adapter usefull. */ if (value == Py_None) { - t = Text_FromUTF8("NULL"); + t = Bytes_FromString("NULL"); PyDict_SetItem(n, key, t); /* t is a new object, refcnt = 1, key is at 2 */ @@ -220,7 +220,7 @@ _mogrify(PyObject *var, PyObject *fmt, connectionObject *conn, PyObject **new) d = c+1; if (value == Py_None) { - PyTuple_SET_ITEM(n, index, Text_FromUTF8("NULL")); + PyTuple_SET_ITEM(n, index, Bytes_FromString("NULL")); while (*d && !isalpha(*d)) d++; if (*d) *d = 's'; Py_DECREF(value); @@ -950,7 +950,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args, PyObject *kwargs) sql[sl-2] = ')'; sql[sl-1] = '\0'; - operation = Text_FromUTF8(sql); + operation = Bytes_FromString(sql); PyMem_Free((void*)sql); if (_psyco_curs_execute(self, operation, parameters, self->conn->async)) { diff --git a/psycopg/microprotocols.c b/psycopg/microprotocols.c index 067729f2..45b26f85 100644 --- a/psycopg/microprotocols.c +++ b/psycopg/microprotocols.c @@ -203,7 +203,10 @@ microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt) return NULL; } -/* microprotocol_getquoted - utility function that adapt and call getquoted */ +/* microprotocol_getquoted - utility function that adapt and call getquoted. + * + * Return a bytes string, NULL on error. + */ PyObject * microprotocol_getquoted(PyObject *obj, connectionObject *conn) @@ -241,6 +244,16 @@ microprotocol_getquoted(PyObject *obj, connectionObject *conn) adapted to the right protocol) */ res = PyObject_CallMethod(adapted, "getquoted", NULL); + /* Convert to bytes. */ + if (res && PyUnicode_CheckExact(res)) { + PyObject *b; + const char *codec; + codec = (conn && conn->codec) ? conn->codec : "utf8"; + b = PyUnicode_AsEncodedString(res, codec, NULL); + Py_DECREF(res); + res = b; + } + exit: Py_XDECREF(adapted); Py_XDECREF(prepare);