mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-01-31 09:24:07 +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)
|
||||
{
|
||||
if (self->wrapped == Py_None) {
|
||||
return Text_FromUTF8("NULL");
|
||||
return Bytes_FromString("NULL");
|
||||
}
|
||||
else {
|
||||
return PyObject_Str(self->wrapped);
|
||||
|
|
|
@ -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 *
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user