diff options
| author | James Henstridge <james@jamesh.id.au> | 2007-12-19 14:29:03 +0000 |
|---|---|---|
| committer | James Henstridge <james@jamesh.id.au> | 2007-12-19 14:29:03 +0000 |
| commit | fd5d2d52384068ad7ad30aafd9d2ea525d7909fa (patch) | |
| tree | 835d3d5ca297d8db91d303664de26080518b5b3d | |
| parent | 67afd678b0d5e84aeb743c5e45b04d141701f4f0 (diff) | |
| download | psycopg2-fd5d2d52384068ad7ad30aafd9d2ea525d7909fa.tar.gz | |
* psycopg/pqpath.c (pq_raise): only remove the first 8 characters
of the exception message if it actually gives the severity.
* psycopg/pqpath.h (pq_resolve_critical): add prototype, since
this function is being used from connection_int.c.
* psycopg/psycopg.h: update psyco_set_error() prototype.
* psycopg/psycopgmodule.c (psyco_errors_init): set pgerror, pgcode
and cursor class attributes to None on psycopg2.Error so that the
attributes will always be available (simplifies error handling).
(psyco_set_error): add const qualifiers to msg, pgerror and pgcode
arguments.
Don't bother setting pgerror, pgcode or cursor to None if they are
not provided -- the class defaults take care of this.
| -rw-r--r-- | ChangeLog | 18 | ||||
| -rw-r--r-- | psycopg/pqpath.c | 30 | ||||
| -rw-r--r-- | psycopg/pqpath.h | 1 | ||||
| -rw-r--r-- | psycopg/psycopg.h | 4 | ||||
| -rw-r--r-- | psycopg/psycopgmodule.c | 29 |
5 files changed, 58 insertions, 24 deletions
@@ -1,3 +1,21 @@ +2007-12-20 James Henstridge <james@jamesh.id.au> + + * psycopg/pqpath.c (pq_raise): only remove the first 8 characters + of the exception message if it actually gives the severity. + + * psycopg/pqpath.h (pq_resolve_critical): add prototype, since + this function is being used from connection_int.c. + + * psycopg/psycopg.h: update psyco_set_error() prototype. + + * psycopg/psycopgmodule.c (psyco_errors_init): set pgerror, pgcode + and cursor class attributes to None on psycopg2.Error so that the + attributes will always be available (simplifies error handling). + (psyco_set_error): add const qualifiers to msg, pgerror and pgcode + arguments. + Don't bother setting pgerror, pgcode or cursor to None if they are + not provided -- the class defaults take care of this. + 2007-11-11 Daniele Varrazzo <daniele.varrazzo@gmail.com> * Use escape string syntax for string escape if connected to a diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 8c2c8a1..c33a3b8 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -40,18 +40,35 @@ #include "psycopg/pgtypes.h" #include "psycopg/pgversion.h" + +/* Strip off the severity from a Postgres error message. */ +static const char * +strip_severity(const char *msg) +{ + if (!msg) + return NULL; + + if (strlen(msg) > 8 && (!strncmp(msg, "ERROR: ", 8) || + !strncmp(msg, "FATAL: ", 8) || + !strncmp(msg, "PANIC: ", 8))) + return &msg[8]; + else + return msg; +} + /* pq_raise - raise a python exception of the right kind This function should be called while holding the GIL. */ -void -pq_raise(connectionObject *conn, cursorObject *curs, PyObject *exc, char *msg) +static void +pq_raise(connectionObject *conn, cursorObject *curs, PyObject *exc, + const char *msg) { PyObject *pgc = (PyObject*)curs; - char *err = NULL; - char *err2 = NULL; - char *code = NULL; + const char *err = NULL; + const char *err2 = NULL; + const char *code = NULL; char *buf = NULL; if ((conn == NULL && curs == NULL) || (curs != NULL && conn == NULL)) { @@ -108,8 +125,7 @@ pq_raise(connectionObject *conn, cursorObject *curs, PyObject *exc, char *msg) } /* try to remove the initial "ERROR: " part from the postgresql error */ - if (err && strlen(err) > 8) err2 = &(err[8]); - else err2 = err; + err2 = strip_severity(err); /* if msg is not NULL, add it to the error message, after a '\n' */ if (msg && code) { diff --git a/psycopg/pqpath.h b/psycopg/pqpath.h index 64d113e..57be047 100644 --- a/psycopg/pqpath.h +++ b/psycopg/pqpath.h @@ -37,5 +37,6 @@ extern int pq_commit(connectionObject *conn); extern int pq_abort(connectionObject *conn); extern int pq_is_busy(connectionObject *conn); extern void pq_set_critical(connectionObject *conn, const char *msg); +extern PyObject *pq_resolve_critical(connectionObject *conn, int close); #endif /* !defined(PSYCOPG_PQPATH_H) */ diff --git a/psycopg/psycopg.h b/psycopg/psycopg.h index 945ce92..f97fee1 100644 --- a/psycopg/psycopg.h +++ b/psycopg/psycopg.h @@ -133,8 +133,8 @@ typedef struct { extern PyObject *psyco_GetDecimalType(void); /* some utility functions */ -extern void psyco_set_error(PyObject *exc, PyObject *curs, char *msg, - char *pgerror, char *pgcode); +extern void psyco_set_error(PyObject *exc, PyObject *curs, const char *msg, + const char *pgerror, const char *pgcode); /* Exceptions docstrings */ #define Error_doc \ diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 53fd641..43b9ea3 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -481,6 +481,13 @@ psyco_errors_init(void) *exctable[i].exc = PyErr_NewException(exctable[i].name, base, dict); } + + /* Make pgerror, pgcode and cursor default to None on psycopg + error objects. This simplifies error handling code that checks + these attributes. */ + PyObject_SetAttrString(Error, "pgerror", Py_None); + PyObject_SetAttrString(Error, "pgcode", Py_None); + PyObject_SetAttrString(Error, "cursor", Py_None); } void @@ -518,8 +525,8 @@ psyco_errors_set(PyObject *type) Create a new error of the given type with extra attributes. */ void -psyco_set_error(PyObject *exc, PyObject *curs, char *msg, - char *pgerror, char *pgcode) +psyco_set_error(PyObject *exc, PyObject *curs, const char *msg, + const char *pgerror, const char *pgcode) { PyObject *t; @@ -528,26 +535,18 @@ psyco_set_error(PyObject *exc, PyObject *curs, char *msg, if (err) { if (pgerror) { t = PyString_FromString(pgerror); + PyObject_SetAttrString(err, "pgerror", t); + Py_DECREF(t); } - else { - t = Py_None ; Py_INCREF(t); - } - PyObject_SetAttrString(err, "pgerror", t); - Py_DECREF(t); if (pgcode) { t = PyString_FromString(pgcode); + PyObject_SetAttrString(err, "pgcode", t); + Py_DECREF(t); } - else { - t = Py_None ; Py_INCREF(t); - } - PyObject_SetAttrString(err, "pgcode", t); - Py_DECREF(t); if (curs) PyObject_SetAttrString(err, "cursor", curs); - else - PyObject_SetAttrString(err, "cursor", Py_None); PyErr_SetObject(exc, err); Py_DECREF(err); @@ -557,7 +556,7 @@ psyco_set_error(PyObject *exc, PyObject *curs, char *msg, /* Return nonzero if the current one is the main interpreter */ static int -psyco_is_main_interp() +psyco_is_main_interp(void) { static PyInterpreterState *main_interp = NULL; /* Cached reference */ PyInterpreterState *interp; |
