mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-28 06:20:32 +03:00
Added utility method to return a string in the connection encoding.
In Py2 the result is plain string, in Py3 an unicode decoded in the connection encoding.
This commit is contained in:
parent
60841c6567
commit
d3f3f1caf0
|
@ -119,6 +119,7 @@ typedef struct {
|
|||
} connectionObject;
|
||||
|
||||
/* C-callable functions in connection_int.c and connection_ext.c */
|
||||
HIDDEN PyObject *conn_text_from_chars(connectionObject *pgconn, const char *str);
|
||||
HIDDEN int conn_get_standard_conforming_strings(PGconn *pgconn);
|
||||
HIDDEN int conn_get_isolation_level(PGresult *pgres);
|
||||
HIDDEN int conn_get_protocol_version(PGconn *pgconn);
|
||||
|
|
|
@ -35,6 +35,23 @@
|
|||
#include <string.h>
|
||||
|
||||
|
||||
/* Return a new "string" from a char* from the database.
|
||||
*
|
||||
* On Py2 just get a string, on Py3 decode it in the connection codec.
|
||||
*
|
||||
* Use a fallback if the connection is NULL.
|
||||
*/
|
||||
PyObject *
|
||||
conn_text_from_chars(connectionObject *self, const char *str)
|
||||
{
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
return PyString_FromString(str);
|
||||
#else
|
||||
const char *codec = self ? self->codec : "ascii";
|
||||
return PyUnicode_Decode(str, strlen(str), codec, "replace");
|
||||
#endif
|
||||
}
|
||||
|
||||
/* conn_notice_callback - process notices */
|
||||
|
||||
static void
|
||||
|
@ -76,9 +93,7 @@ conn_notice_process(connectionObject *self)
|
|||
|
||||
while (notice != NULL) {
|
||||
PyObject *msg;
|
||||
/* XXX possible other encode I think */
|
||||
msg = Text_FromUTF8(notice->message);
|
||||
|
||||
msg = conn_text_from_chars(self, notice->message);
|
||||
Dprintf("conn_notice_process: %s", notice->message);
|
||||
|
||||
/* Respect the order in which notices were produced,
|
||||
|
@ -146,9 +161,8 @@ conn_notifies_process(connectionObject *self)
|
|||
(int) pgn->be_pid, pgn->relname);
|
||||
|
||||
if (!(pid = PyInt_FromLong((long)pgn->be_pid))) { goto error; }
|
||||
/* XXX in the connection encoding? */
|
||||
if (!(channel = Text_FromUTF8(pgn->relname))) { goto error; }
|
||||
if (!(payload = Text_FromUTF8(pgn->extra))) { goto error; }
|
||||
if (!(channel = conn_text_from_chars(self, pgn->relname))) { goto error; }
|
||||
if (!(payload = conn_text_from_chars(self, pgn->extra))) { goto error; }
|
||||
|
||||
if (!(notify = PyObject_CallFunctionObjArgs((PyObject *)&NotifyType,
|
||||
pid, channel, payload, NULL))) {
|
||||
|
|
|
@ -498,7 +498,7 @@ psyco_conn_get_parameter_status(connectionObject *self, PyObject *args)
|
|||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
return Text_FromUTF8(val);
|
||||
return conn_text_from_chars(self, val);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -78,8 +78,7 @@ notify_init(NotifyObject *self, PyObject *args, PyObject *kwargs)
|
|||
}
|
||||
|
||||
if (!payload) {
|
||||
/* XXX review encoding */
|
||||
payload = Text_FromUTF8AndSize("", 0);
|
||||
payload = Text_FromUTF8("");
|
||||
}
|
||||
|
||||
Py_CLEAR(self->pid);
|
||||
|
|
|
@ -987,8 +987,7 @@ _pq_fetch_tuples(cursorObject *curs)
|
|||
|
||||
/* 1/ fill the other fields */
|
||||
PyTuple_SET_ITEM(dtitem, 0,
|
||||
/* XXX guaranteed to be ASCII/UTF8? */
|
||||
Text_FromUTF8(PQfname(curs->pgres, i)));
|
||||
conn_text_from_chars(curs->conn, PQfname(curs->pgres, i)));
|
||||
PyTuple_SET_ITEM(dtitem, 1, type);
|
||||
|
||||
/* 2/ display size is the maximum size of this field result tuples. */
|
||||
|
@ -1220,7 +1219,7 @@ pq_fetch(cursorObject *curs)
|
|||
|
||||
/* backend status message */
|
||||
Py_XDECREF(curs->pgstatus);
|
||||
curs->pgstatus = Text_FromUTF8(PQcmdStatus(curs->pgres));
|
||||
curs->pgstatus = conn_text_from_chars(curs->conn, PQcmdStatus(curs->pgres));
|
||||
|
||||
switch(pgstatus) {
|
||||
|
||||
|
|
|
@ -582,26 +582,26 @@ psyco_set_error(PyObject *exc, PyObject *curs, const char *msg,
|
|||
PyObject *err = PyObject_CallFunction(exc, "s", msg);
|
||||
|
||||
if (err) {
|
||||
connectionObject *conn = NULL;
|
||||
if (curs) {
|
||||
PyObject_SetAttrString(err, "cursor", curs);
|
||||
conn = ((cursorObject *)curs)->conn;
|
||||
}
|
||||
|
||||
if (pgerror) {
|
||||
/* XXX is this always ASCII? If not, it needs
|
||||
to be decoded properly for Python 3. */
|
||||
t = Text_FromUTF8(pgerror);
|
||||
t = conn_text_from_chars(conn, pgerror);
|
||||
PyObject_SetAttrString(err, "pgerror", t);
|
||||
Py_DECREF(t);
|
||||
}
|
||||
|
||||
if (pgcode) {
|
||||
/* XXX likewise */
|
||||
t = Text_FromUTF8(pgcode);
|
||||
t = conn_text_from_chars(conn, pgcode);
|
||||
PyObject_SetAttrString(err, "pgcode", t);
|
||||
Py_DECREF(t);
|
||||
}
|
||||
|
||||
if (curs)
|
||||
PyObject_SetAttrString(err, "cursor", curs);
|
||||
|
||||
PyErr_SetObject(exc, err);
|
||||
Py_DECREF(err);
|
||||
Py_DECREF(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user