21 char **xmsg,
char **tbmsg,
int *tb_depth);
23 char **hint,
char **query,
int *position,
24 char **schema_name,
char **table_name,
char **column_name,
25 char **datatype_name,
char **constraint_name);
27 char **hint,
char **schema_name,
char **table_name,
char **column_name,
28 char **datatype_name,
char **constraint_name);
45 int save_errno = errno;
53 const char *primary = NULL;
59 char *schema_name = NULL;
60 char *table_name = NULL;
61 char *column_name = NULL;
62 char *datatype_name = NULL;
63 char *constraint_name = NULL;
65 PyErr_Fetch(&exc, &
val, &tb);
69 PyErr_NormalizeException(&exc, &
val, &tb);
73 &detail, &hint, &query, &position,
74 &schema_name, &table_name, &column_name,
75 &datatype_name, &constraint_name);
78 &schema_name, &table_name, &column_name,
79 &datatype_name, &constraint_name);
86 &xmsg, &tbmsg, &tb_depth);
119 (
errcode(sqlerrcode ? sqlerrcode : ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
122 (tb_depth > 0 && tbmsg) ?
errcontext(
"%s", tbmsg) : 0,
123 (hint) ?
errhint(
"%s", hint) : 0,
135 constraint_name) : 0));
163 char **xmsg,
char **tbmsg,
int *tb_depth)
166 PyObject *e_module_o;
167 char *e_type_s = NULL;
168 char *e_module_s = NULL;
169 PyObject *vob = NULL;
190 e_type_o = PyObject_GetAttrString(
e,
"__name__");
191 e_module_o = PyObject_GetAttrString(
e,
"__module__");
197 if (v && ((vob = PyObject_Str(v)) != NULL))
203 if (!e_type_s || !e_module_s)
209 else if (strcmp(e_module_s,
"builtins") == 0
210 || strcmp(e_module_s,
"__main__") == 0
211 || strcmp(e_module_s,
"exceptions") == 0)
227 while (tb != NULL && tb != Py_None)
229 PyObject *
volatile tb_prev = NULL;
230 PyObject *
volatile frame = NULL;
231 PyObject *
volatile code = NULL;
232 PyObject *
volatile name = NULL;
233 PyObject *
volatile lineno = NULL;
238 lineno = PyObject_GetAttrString(tb,
"tb_lineno");
240 elog(
ERROR,
"could not get line number from Python traceback");
242 frame = PyObject_GetAttrString(tb,
"tb_frame");
244 elog(
ERROR,
"could not get frame from Python traceback");
246 code = PyObject_GetAttrString(frame,
"f_code");
248 elog(
ERROR,
"could not get code object from Python frame");
250 name = PyObject_GetAttrString(code,
"co_name");
252 elog(
ERROR,
"could not get function name from Python code object");
254 filename = PyObject_GetAttrString(code,
"co_filename");
256 elog(
ERROR,
"could not get file name from Python code object");
276 char *plain_filename;
290 plain_lineno = PyLong_AsLong(lineno);
293 appendStringInfo(&tbstr,
"\n PL/Python anonymous code block, line %ld, in %s",
294 plain_lineno - 1, fname);
297 proname, plain_lineno - 1, fname);
303 if (exec_ctx->
curr_proc && plain_filename != NULL &&
304 strcmp(plain_filename,
"<string>") == 0)
332 tb = PyObject_GetAttrString(tb,
"tb_next");
333 Assert(tb_prev != Py_None);
336 elog(
ERROR,
"could not traverse Python traceback");
343 Py_XDECREF(e_type_o);
344 Py_XDECREF(e_module_o);
357 sqlstate = PyObject_GetAttrString(exc,
"sqlstate");
358 if (sqlstate == NULL)
362 if (strlen(buffer) == 5 &&
363 strspn(buffer,
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 5)
366 buffer[3], buffer[4]);
377 char **hint,
char **query,
int *position,
378 char **schema_name,
char **table_name,
380 char **datatype_name,
char **constraint_name)
384 spidata = PyObject_GetAttrString(exc,
"spidata");
388 PyArg_ParseTuple(spidata,
"izzzizzzzz",
389 sqlerrcode, detail, hint, query, position,
390 schema_name, table_name, column_name,
391 datatype_name, constraint_name);
413 char **schema_name,
char **table_name,
char **column_name,
414 char **datatype_name,
char **constraint_name)
432 const char *s = NULL;
433 const char *
next = src;
440 while (current < lineno)
443 next = strchr(s + 1,
'\n');
449 if (current != lineno)
452 while (*s && isspace((
unsigned char) *s))
481 PyErr_SetString(exc,
buf);
487 const char *fmt_singular,
const char *fmt_plural,
499 PyErr_SetString(exc,
buf);
506 PyObject *
args = NULL;
507 PyObject *
error = NULL;
514 error = PyObject_CallObject(excclass,
args);
546 PyErr_SetObject(excclass,
error);
557 elog(
ERROR,
"could not convert error to Python exception");
566 val = PyObject_GetAttrString(obj, attrname);
567 if (
val != NULL &&
val != Py_None)
595 result = PyObject_SetAttrString(obj, attrname,
val);
#define Assert(condition)
#define dngettext(d, s, p, n)
int err_generic_string(int field, const char *str)
int internalerrquery(const char *query)
int internalerrposition(int cursorpos)
int errmsg_internal(const char *fmt,...)
int errdetail_internal(const char *fmt,...)
int errhint(const char *fmt,...)
char * unpack_sql_state(int sql_state)
int errcode(int sqlerrcode)
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
#define ereport(elevel,...)
static void const char * fmt
char * pnstrdup(const char *in, Size len)
char * pstrdup(const char *in)
void pfree(void *pointer)
static void get_string_attr(PyObject *obj, char *attrname, char **str)
static bool set_string_attr(PyObject *obj, char *attrname, char *str)
static void PLy_get_error_data(PyObject *exc, int *sqlerrcode, char **detail, char **hint, char **schema_name, char **table_name, char **column_name, char **datatype_name, char **constraint_name)
static void PLy_traceback(PyObject *e, PyObject *v, PyObject *tb, char **xmsg, char **tbmsg, int *tb_depth)
void PLy_elog_impl(int elevel, const char *fmt,...)
PyObject * PLy_exc_spi_error
void PLy_exception_set_with_details(PyObject *excclass, ErrorData *edata)
static void PLy_get_spi_error_data(PyObject *exc, int *sqlerrcode, char **detail, char **hint, char **query, int *position, char **schema_name, char **table_name, char **column_name, char **datatype_name, char **constraint_name)
void PLy_exception_set(PyObject *exc, const char *fmt,...)
static char * get_source_line(const char *src, int lineno)
static void PLy_get_sqlerrcode(PyObject *exc, int *sqlerrcode)
void PLy_exception_set_plural(PyObject *exc, const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
PLyExecutionContext * PLy_current_execution_context(void)
char * PLy_procedure_name(PLyProcedure *proc)
char * PLyUnicode_AsString(PyObject *unicode)
PyObject * PLyUnicode_FromString(const char *s)
#define PG_DIAG_SCHEMA_NAME
#define PG_DIAG_CONSTRAINT_NAME
#define PG_DIAG_DATATYPE_NAME
#define PG_DIAG_TABLE_NAME
#define PG_DIAG_COLUMN_NAME
int appendStringInfoVA(StringInfo str, const char *fmt, va_list args)
void appendStringInfo(StringInfo str, const char *fmt,...)
void enlargeStringInfo(StringInfo str, int needed)
void appendStringInfoString(StringInfo str, const char *s)
void initStringInfo(StringInfo str)