Skip to content

Commit 56e4c2b

Browse files
committed
Redefining the microprotocol on Py3 as returning bytes.
1 parent 014b6a6 commit 56e4c2b

File tree

6 files changed

+35
-20
lines changed

6 files changed

+35
-20
lines changed

psycopg/adapter_asis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static PyObject *
3838
asis_str(asisObject *self)
3939
{
4040
if (self->wrapped == Py_None) {
41-
return Text_FromUTF8("NULL");
41+
return Bytes_FromString("NULL");
4242
}
4343
else {
4444
return PyObject_Str(self->wrapped);

psycopg/adapter_binary.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ binary_quote(binaryObject *self)
6262
#if PY_MAJOR_VERSION < 3
6363
|| PyBuffer_Check(self->wrapped)
6464
#else
65+
|| PyByteArray_Check(self->wrapped)
6566
|| PyMemoryView_Check(self->wrapped)
6667
#endif
6768
) {
@@ -78,11 +79,11 @@ binary_quote(binaryObject *self)
7879
}
7980

8081
if (len > 0)
81-
self->buffer = PyString_FromFormat(
82+
self->buffer = Bytes_FromFormat(
8283
(self->conn && ((connectionObject*)self->conn)->equote)
8384
? "E'%s'::bytea" : "'%s'::bytea" , to);
8485
else
85-
self->buffer = Text_FromUTF8("''::bytea");
86+
self->buffer = Bytes_FromString("''::bytea");
8687

8788
PQfreemem(to);
8889
}
@@ -97,15 +98,21 @@ binary_quote(binaryObject *self)
9798
}
9899

99100
/* binary_str, binary_getquoted - return result of quoting */
100-
101+
/* XXX what is the point of this method? */
101102
static PyObject *
102103
binary_str(binaryObject *self)
103104
{
104105
if (self->buffer == NULL) {
105-
binary_quote(self);
106+
if (!(binary_quote(self))) {
107+
return NULL;
108+
}
106109
}
107-
Py_XINCREF(self->buffer);
110+
#if PY_MAJOR_VERSION < 3
111+
Py_INCREF(self->buffer);
108112
return self->buffer;
113+
#else
114+
return PyUnicode_FromEncodedObject(self->buffer, "ascii", "replace");
115+
#endif
109116
}
110117

111118
static PyObject *

psycopg/adapter_list.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ list_quote(listObject *self)
5353
PyObject *quoted;
5454
PyObject *wrapped = PyList_GET_ITEM(self->wrapped, i);
5555
if (wrapped == Py_None)
56-
quoted = Text_FromUTF8("NULL");
56+
quoted = Bytes_FromString("NULL");
5757
else
5858
quoted = microprotocol_getquoted(wrapped,
5959
(connectionObject*)self->connection);
@@ -67,15 +67,11 @@ list_quote(listObject *self)
6767

6868
/* now that we have a tuple of adapted objects we just need to join them
6969
and put "ARRAY[] around the result */
70-
str = Text_FromUTF8(", ");
70+
str = Bytes_FromString(", ");
7171
joined = PyObject_CallMethod(str, "join", "(O)", tmp);
7272
if (joined == NULL) goto error;
7373

74-
#if PY_MAJOR_VERSION < 3
75-
res = PyString_FromFormat("ARRAY[%s]", PyString_AsString(joined));
76-
#else
77-
res = PyUnicode_FromFormat("ARRAY[%U]", joined);
78-
#endif
74+
res = Bytes_FromFormat("ARRAY[%s]", Bytes_AsString(joined));
7975

8076
error:
8177
Py_XDECREF(tmp);

psycopg/adapter_qstring.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ qstring_quote(qstringObject *self)
9595
Py_DECREF(str);
9696
return NULL;
9797
}
98-
99-
/* XXX need to decode in connection's encoding in 3.0 */
100-
self->buffer = Text_FromUTF8AndSize(buffer, qlen);
98+
99+
self->buffer = Bytes_FromStringAndSize(buffer, qlen);
101100
PyMem_Free(buffer);
102101
Py_DECREF(str);
103102

psycopg/cursor_type.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ _mogrify(PyObject *var, PyObject *fmt, connectionObject *conn, PyObject **new)
144144
optimization over the adapting code and can go away in
145145
the future if somebody finds a None adapter usefull. */
146146
if (value == Py_None) {
147-
t = Text_FromUTF8("NULL");
147+
t = Bytes_FromString("NULL");
148148
PyDict_SetItem(n, key, t);
149149
/* t is a new object, refcnt = 1, key is at 2 */
150150

@@ -220,7 +220,7 @@ _mogrify(PyObject *var, PyObject *fmt, connectionObject *conn, PyObject **new)
220220
d = c+1;
221221

222222
if (value == Py_None) {
223-
PyTuple_SET_ITEM(n, index, Text_FromUTF8("NULL"));
223+
PyTuple_SET_ITEM(n, index, Bytes_FromString("NULL"));
224224
while (*d && !isalpha(*d)) d++;
225225
if (*d) *d = 's';
226226
Py_DECREF(value);
@@ -950,7 +950,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args, PyObject *kwargs)
950950
sql[sl-2] = ')';
951951
sql[sl-1] = '\0';
952952

953-
operation = Text_FromUTF8(sql);
953+
operation = Bytes_FromString(sql);
954954
PyMem_Free((void*)sql);
955955

956956
if (_psyco_curs_execute(self, operation, parameters, self->conn->async)) {

psycopg/microprotocols.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt)
203203
return NULL;
204204
}
205205

206-
/* microprotocol_getquoted - utility function that adapt and call getquoted */
206+
/* microprotocol_getquoted - utility function that adapt and call getquoted.
207+
*
208+
* Return a bytes string, NULL on error.
209+
*/
207210

208211
PyObject *
209212
microprotocol_getquoted(PyObject *obj, connectionObject *conn)
@@ -241,6 +244,16 @@ microprotocol_getquoted(PyObject *obj, connectionObject *conn)
241244
adapted to the right protocol) */
242245
res = PyObject_CallMethod(adapted, "getquoted", NULL);
243246

247+
/* Convert to bytes. */
248+
if (res && PyUnicode_CheckExact(res)) {
249+
PyObject *b;
250+
const char *codec;
251+
codec = (conn && conn->codec) ? conn->codec : "utf8";
252+
b = PyUnicode_AsEncodedString(res, codec, NULL);
253+
Py_DECREF(res);
254+
res = b;
255+
}
256+
244257
exit:
245258
Py_XDECREF(adapted);
246259
Py_XDECREF(prepare);

0 commit comments

Comments
 (0)