diff options
Diffstat (limited to 'psycopg/pqpath.c')
-rw-r--r-- | psycopg/pqpath.c | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index c8d9c46..328a2b2 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -167,6 +167,7 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres) const char *err2 = NULL; const char *code = NULL; PyObject *pyerr = NULL; + PyObject *pgerror = NULL, *pgcode = NULL; if (conn == NULL) { PyErr_SetString(DatabaseError, @@ -221,19 +222,37 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres) err2 = strip_severity(err); Dprintf("pq_raise: err2=%s", err2); + /* decode now the details of the error, because after psyco_set_error + * decoding will fail. + */ + if (!(pgerror = conn_text_from_chars(conn, err))) { + /* we can't really handle an exception while handling this error + * so just print it. */ + PyErr_Print(); + PyErr_Clear(); + } + + if (!(pgcode = conn_text_from_chars(conn, code))) { + PyErr_Print(); + PyErr_Clear(); + } + pyerr = psyco_set_error(exc, curs, err2); if (pyerr && PyObject_TypeCheck(pyerr, &errorType)) { errorObject *perr = (errorObject *)pyerr; - PyMem_Free(perr->pyenc); - psycopg_strdup(&perr->pyenc, conn->pyenc, -1); + Py_CLEAR(perr->pydecoder); + Py_XINCREF(conn->pydecoder); + perr->pydecoder = conn->pydecoder; Py_CLEAR(perr->pgerror); - perr->pgerror = error_text_from_chars(perr, err); + perr->pgerror = pgerror; + pgerror = NULL; Py_CLEAR(perr->pgcode); - perr->pgcode = error_text_from_chars(perr, code); + perr->pgcode = pgcode; + pgcode = NULL; CLEARPGRES(perr->pgres); if (pgres && *pgres) { @@ -241,6 +260,9 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres) *pgres = NULL; } } + + Py_XDECREF(pgerror); + Py_XDECREF(pgcode); } /* pq_set_critical, pq_resolve_critical - manage critical errors @@ -1332,8 +1354,7 @@ _pq_copy_in_v3(cursorObject *curs) /* a file may return unicode if implements io.TextIOBase */ if (PyUnicode_Check(o)) { PyObject *tmp; - Dprintf("_pq_copy_in_v3: encoding in %s", curs->conn->pyenc); - if (!(tmp = PyUnicode_AsEncodedString(o, curs->conn->pyenc, NULL))) { + if (!(tmp = conn_encode(curs->conn, o))) { Dprintf("_pq_copy_in_v3: encoding() failed"); error = 1; break; @@ -1488,7 +1509,7 @@ _pq_copy_out_v3(cursorObject *curs) if (len > 0 && buffer) { if (is_text) { - obj = PyUnicode_Decode(buffer, len, curs->conn->pyenc, NULL); + obj = conn_decode(curs->conn, buffer, len); } else { obj = Bytes_FromStringAndSize(buffer, len); } @@ -1638,7 +1659,7 @@ retry: Dprintf("pq_read_replication_message: >>%.*s<<", data_size, buffer + hdr); if (repl->decode) { - str = PyUnicode_Decode(buffer + hdr, data_size, conn->pyenc, NULL); + str = conn_decode(conn, buffer + hdr, data_size); } else { str = Bytes_FromStringAndSize(buffer + hdr, data_size); } |