mirror of
				https://github.com/psycopg/psycopg2.git
				synced 2025-11-04 01:37:31 +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;
 | 
					} connectionObject;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* C-callable functions in connection_int.c and connection_ext.c */
 | 
					/* 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_standard_conforming_strings(PGconn *pgconn);
 | 
				
			||||||
HIDDEN int  conn_get_isolation_level(PGresult *pgres);
 | 
					HIDDEN int  conn_get_isolation_level(PGresult *pgres);
 | 
				
			||||||
HIDDEN int  conn_get_protocol_version(PGconn *pgconn);
 | 
					HIDDEN int  conn_get_protocol_version(PGconn *pgconn);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -35,6 +35,23 @@
 | 
				
			||||||
#include <string.h>
 | 
					#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 */
 | 
					/* conn_notice_callback - process notices */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void
 | 
					static void
 | 
				
			||||||
| 
						 | 
					@ -76,9 +93,7 @@ conn_notice_process(connectionObject *self)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while (notice != NULL) {
 | 
					    while (notice != NULL) {
 | 
				
			||||||
        PyObject *msg;
 | 
					        PyObject *msg;
 | 
				
			||||||
        /* XXX possible other encode I think */
 | 
					        msg = conn_text_from_chars(self, notice->message);
 | 
				
			||||||
        msg = Text_FromUTF8(notice->message);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        Dprintf("conn_notice_process: %s", notice->message);
 | 
					        Dprintf("conn_notice_process: %s", notice->message);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* Respect the order in which notices were produced,
 | 
					        /* Respect the order in which notices were produced,
 | 
				
			||||||
| 
						 | 
					@ -146,9 +161,8 @@ conn_notifies_process(connectionObject *self)
 | 
				
			||||||
                (int) pgn->be_pid, pgn->relname);
 | 
					                (int) pgn->be_pid, pgn->relname);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!(pid = PyInt_FromLong((long)pgn->be_pid))) { goto error; }
 | 
					        if (!(pid = PyInt_FromLong((long)pgn->be_pid))) { goto error; }
 | 
				
			||||||
        /* XXX in the connection encoding? */
 | 
					        if (!(channel = conn_text_from_chars(self, pgn->relname))) { goto error; }
 | 
				
			||||||
        if (!(channel = Text_FromUTF8(pgn->relname))) { goto error; }
 | 
					        if (!(payload = conn_text_from_chars(self, pgn->extra))) { goto error; }
 | 
				
			||||||
        if (!(payload = Text_FromUTF8(pgn->extra))) { goto error; }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!(notify = PyObject_CallFunctionObjArgs((PyObject *)&NotifyType,
 | 
					        if (!(notify = PyObject_CallFunctionObjArgs((PyObject *)&NotifyType,
 | 
				
			||||||
                pid, channel, payload, NULL))) {
 | 
					                pid, channel, payload, NULL))) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -498,7 +498,7 @@ psyco_conn_get_parameter_status(connectionObject *self, PyObject *args)
 | 
				
			||||||
        Py_INCREF(Py_None);
 | 
					        Py_INCREF(Py_None);
 | 
				
			||||||
        return 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) {
 | 
					    if (!payload) {
 | 
				
			||||||
        /* XXX review encoding */
 | 
					        payload = Text_FromUTF8("");
 | 
				
			||||||
        payload = Text_FromUTF8AndSize("", 0);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Py_CLEAR(self->pid);
 | 
					    Py_CLEAR(self->pid);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -987,8 +987,7 @@ _pq_fetch_tuples(cursorObject *curs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* 1/ fill the other fields */
 | 
					        /* 1/ fill the other fields */
 | 
				
			||||||
        PyTuple_SET_ITEM(dtitem, 0,
 | 
					        PyTuple_SET_ITEM(dtitem, 0,
 | 
				
			||||||
                         /* XXX guaranteed to be ASCII/UTF8? */
 | 
					             conn_text_from_chars(curs->conn, PQfname(curs->pgres, i)));
 | 
				
			||||||
                         Text_FromUTF8(PQfname(curs->pgres, i)));
 | 
					 | 
				
			||||||
        PyTuple_SET_ITEM(dtitem, 1, type);
 | 
					        PyTuple_SET_ITEM(dtitem, 1, type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /* 2/ display size is the maximum size of this field result tuples. */
 | 
					        /* 2/ display size is the maximum size of this field result tuples. */
 | 
				
			||||||
| 
						 | 
					@ -1220,7 +1219,7 @@ pq_fetch(cursorObject *curs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* backend status message */
 | 
					    /* backend status message */
 | 
				
			||||||
    Py_XDECREF(curs->pgstatus);
 | 
					    Py_XDECREF(curs->pgstatus);
 | 
				
			||||||
    curs->pgstatus = Text_FromUTF8(PQcmdStatus(curs->pgres));
 | 
					    curs->pgstatus = conn_text_from_chars(curs->conn, PQcmdStatus(curs->pgres));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    switch(pgstatus) {
 | 
					    switch(pgstatus) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -582,24 +582,24 @@ psyco_set_error(PyObject *exc, PyObject *curs, const char *msg,
 | 
				
			||||||
    PyObject *err = PyObject_CallFunction(exc, "s", msg);
 | 
					    PyObject *err = PyObject_CallFunction(exc, "s", msg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (err) {
 | 
					    if (err) {
 | 
				
			||||||
 | 
					        connectionObject *conn = NULL;
 | 
				
			||||||
 | 
					        if (curs) {
 | 
				
			||||||
 | 
					            PyObject_SetAttrString(err, "cursor", curs);
 | 
				
			||||||
 | 
					            conn = ((cursorObject *)curs)->conn;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (pgerror) {
 | 
					        if (pgerror) {
 | 
				
			||||||
            /* XXX is this always ASCII? If not, it needs
 | 
					            t = conn_text_from_chars(conn, pgerror);
 | 
				
			||||||
               to be decoded properly for Python 3. */
 | 
					 | 
				
			||||||
            t = Text_FromUTF8(pgerror);
 | 
					 | 
				
			||||||
            PyObject_SetAttrString(err, "pgerror", t);
 | 
					            PyObject_SetAttrString(err, "pgerror", t);
 | 
				
			||||||
            Py_DECREF(t);
 | 
					            Py_DECREF(t);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (pgcode) {
 | 
					        if (pgcode) {
 | 
				
			||||||
            /* XXX likewise */
 | 
					            t = conn_text_from_chars(conn, pgcode);
 | 
				
			||||||
            t = Text_FromUTF8(pgcode);
 | 
					 | 
				
			||||||
            PyObject_SetAttrString(err, "pgcode", t);
 | 
					            PyObject_SetAttrString(err, "pgcode", t);
 | 
				
			||||||
            Py_DECREF(t);
 | 
					            Py_DECREF(t);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (curs)
 | 
					 | 
				
			||||||
            PyObject_SetAttrString(err, "cursor", curs);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        PyErr_SetObject(exc, err);
 | 
					        PyErr_SetObject(exc, err);
 | 
				
			||||||
        Py_DECREF(err);
 | 
					        Py_DECREF(err);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user