mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-01-31 17:34:08 +03:00
Redefining the microprotocol on Py3 as returning bytes.
This commit is contained in:
parent
014b6a6d5b
commit
56e4c2bd55
|
@ -38,7 +38,7 @@ static PyObject *
|
||||||
asis_str(asisObject *self)
|
asis_str(asisObject *self)
|
||||||
{
|
{
|
||||||
if (self->wrapped == Py_None) {
|
if (self->wrapped == Py_None) {
|
||||||
return Text_FromUTF8("NULL");
|
return Bytes_FromString("NULL");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return PyObject_Str(self->wrapped);
|
return PyObject_Str(self->wrapped);
|
||||||
|
|
|
@ -62,6 +62,7 @@ binary_quote(binaryObject *self)
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
|| PyBuffer_Check(self->wrapped)
|
|| PyBuffer_Check(self->wrapped)
|
||||||
#else
|
#else
|
||||||
|
|| PyByteArray_Check(self->wrapped)
|
||||||
|| PyMemoryView_Check(self->wrapped)
|
|| PyMemoryView_Check(self->wrapped)
|
||||||
#endif
|
#endif
|
||||||
) {
|
) {
|
||||||
|
@ -78,11 +79,11 @@ binary_quote(binaryObject *self)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
self->buffer = PyString_FromFormat(
|
self->buffer = Bytes_FromFormat(
|
||||||
(self->conn && ((connectionObject*)self->conn)->equote)
|
(self->conn && ((connectionObject*)self->conn)->equote)
|
||||||
? "E'%s'::bytea" : "'%s'::bytea" , to);
|
? "E'%s'::bytea" : "'%s'::bytea" , to);
|
||||||
else
|
else
|
||||||
self->buffer = Text_FromUTF8("''::bytea");
|
self->buffer = Bytes_FromString("''::bytea");
|
||||||
|
|
||||||
PQfreemem(to);
|
PQfreemem(to);
|
||||||
}
|
}
|
||||||
|
@ -97,15 +98,21 @@ 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_str(binaryObject *self)
|
||||||
{
|
{
|
||||||
if (self->buffer == NULL) {
|
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;
|
return self->buffer;
|
||||||
|
#else
|
||||||
|
return PyUnicode_FromEncodedObject(self->buffer, "ascii", "replace");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
|
|
@ -53,7 +53,7 @@ list_quote(listObject *self)
|
||||||
PyObject *quoted;
|
PyObject *quoted;
|
||||||
PyObject *wrapped = PyList_GET_ITEM(self->wrapped, i);
|
PyObject *wrapped = PyList_GET_ITEM(self->wrapped, i);
|
||||||
if (wrapped == Py_None)
|
if (wrapped == Py_None)
|
||||||
quoted = Text_FromUTF8("NULL");
|
quoted = Bytes_FromString("NULL");
|
||||||
else
|
else
|
||||||
quoted = microprotocol_getquoted(wrapped,
|
quoted = microprotocol_getquoted(wrapped,
|
||||||
(connectionObject*)self->connection);
|
(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
|
/* now that we have a tuple of adapted objects we just need to join them
|
||||||
and put "ARRAY[] around the result */
|
and put "ARRAY[] around the result */
|
||||||
str = Text_FromUTF8(", ");
|
str = Bytes_FromString(", ");
|
||||||
joined = PyObject_CallMethod(str, "join", "(O)", tmp);
|
joined = PyObject_CallMethod(str, "join", "(O)", tmp);
|
||||||
if (joined == NULL) goto error;
|
if (joined == NULL) goto error;
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION < 3
|
res = Bytes_FromFormat("ARRAY[%s]", Bytes_AsString(joined));
|
||||||
res = PyString_FromFormat("ARRAY[%s]", PyString_AsString(joined));
|
|
||||||
#else
|
|
||||||
res = PyUnicode_FromFormat("ARRAY[%U]", joined);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
error:
|
error:
|
||||||
Py_XDECREF(tmp);
|
Py_XDECREF(tmp);
|
||||||
|
|
|
@ -96,8 +96,7 @@ qstring_quote(qstringObject *self)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* XXX need to decode in connection's encoding in 3.0 */
|
self->buffer = Bytes_FromStringAndSize(buffer, qlen);
|
||||||
self->buffer = Text_FromUTF8AndSize(buffer, qlen);
|
|
||||||
PyMem_Free(buffer);
|
PyMem_Free(buffer);
|
||||||
Py_DECREF(str);
|
Py_DECREF(str);
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,7 @@ _mogrify(PyObject *var, PyObject *fmt, connectionObject *conn, PyObject **new)
|
||||||
optimization over the adapting code and can go away in
|
optimization over the adapting code and can go away in
|
||||||
the future if somebody finds a None adapter usefull. */
|
the future if somebody finds a None adapter usefull. */
|
||||||
if (value == Py_None) {
|
if (value == Py_None) {
|
||||||
t = Text_FromUTF8("NULL");
|
t = Bytes_FromString("NULL");
|
||||||
PyDict_SetItem(n, key, t);
|
PyDict_SetItem(n, key, t);
|
||||||
/* t is a new object, refcnt = 1, key is at 2 */
|
/* 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;
|
d = c+1;
|
||||||
|
|
||||||
if (value == Py_None) {
|
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++;
|
while (*d && !isalpha(*d)) d++;
|
||||||
if (*d) *d = 's';
|
if (*d) *d = 's';
|
||||||
Py_DECREF(value);
|
Py_DECREF(value);
|
||||||
|
@ -950,7 +950,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
sql[sl-2] = ')';
|
sql[sl-2] = ')';
|
||||||
sql[sl-1] = '\0';
|
sql[sl-1] = '\0';
|
||||||
|
|
||||||
operation = Text_FromUTF8(sql);
|
operation = Bytes_FromString(sql);
|
||||||
PyMem_Free((void*)sql);
|
PyMem_Free((void*)sql);
|
||||||
|
|
||||||
if (_psyco_curs_execute(self, operation, parameters, self->conn->async)) {
|
if (_psyco_curs_execute(self, operation, parameters, self->conn->async)) {
|
||||||
|
|
|
@ -203,7 +203,10 @@ microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
|
||||||
return NULL;
|
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 *
|
PyObject *
|
||||||
microprotocol_getquoted(PyObject *obj, connectionObject *conn)
|
microprotocol_getquoted(PyObject *obj, connectionObject *conn)
|
||||||
|
@ -241,6 +244,16 @@ microprotocol_getquoted(PyObject *obj, connectionObject *conn)
|
||||||
adapted to the right protocol) */
|
adapted to the right protocol) */
|
||||||
res = PyObject_CallMethod(adapted, "getquoted", NULL);
|
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:
|
exit:
|
||||||
Py_XDECREF(adapted);
|
Py_XDECREF(adapted);
|
||||||
Py_XDECREF(prepare);
|
Py_XDECREF(prepare);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user