PostgreSQL Source Code  git master
plpy_elog.c File Reference
#include "postgres.h"
#include "lib/stringinfo.h"
#include "plpy_elog.h"
#include "plpy_main.h"
#include "plpy_procedure.h"
#include "plpython.h"
Include dependency graph for plpy_elog.c:

Go to the source code of this file.

Functions

static void PLy_traceback (PyObject *e, PyObject *v, PyObject *tb, char **xmsg, char **tbmsg, int *tb_depth)
 
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)
 
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 char * get_source_line (const char *src, int lineno)
 
static void get_string_attr (PyObject *obj, char *attrname, char **str)
 
static bool set_string_attr (PyObject *obj, char *attrname, char *str)
 
void PLy_elog_impl (int elevel, const char *fmt,...)
 
static void PLy_get_sqlerrcode (PyObject *exc, int *sqlerrcode)
 
void PLy_exception_set (PyObject *exc, const char *fmt,...)
 
void PLy_exception_set_plural (PyObject *exc, const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
 
void PLy_exception_set_with_details (PyObject *excclass, ErrorData *edata)
 

Variables

PyObject * PLy_exc_error = NULL
 
PyObject * PLy_exc_fatal = NULL
 
PyObject * PLy_exc_spi_error = NULL
 

Function Documentation

◆ get_source_line()

static char * get_source_line ( const char *  src,
int  lineno 
)
static

Definition at line 445 of file plpy_elog.c.

References next, pnstrdup(), and pstrdup().

Referenced by PLy_traceback().

446 {
447  const char *s = NULL;
448  const char *next = src;
449  int current = 0;
450 
451  /* sanity check */
452  if (lineno <= 0)
453  return NULL;
454 
455  while (current < lineno)
456  {
457  s = next;
458  next = strchr(s + 1, '\n');
459  current++;
460  if (next == NULL)
461  break;
462  }
463 
464  if (current != lineno)
465  return NULL;
466 
467  while (*s && isspace((unsigned char) *s))
468  s++;
469 
470  if (next == NULL)
471  return pstrdup(s);
472 
473  /*
474  * Sanity check, next < s if the line was all-whitespace, which should
475  * never happen if Python reported a frame created on that line, but check
476  * anyway.
477  */
478  if (next < s)
479  return NULL;
480 
481  return pnstrdup(s, next - s);
482 }
char * pnstrdup(const char *in, Size len)
Definition: mcxt.c:1197
static int32 next
Definition: blutils.c:213
char * pstrdup(const char *in)
Definition: mcxt.c:1186

◆ get_string_attr()

static void get_string_attr ( PyObject *  obj,
char *  attrname,
char **  str 
)
static

Definition at line 577 of file plpy_elog.c.

References pstrdup(), and val.

Referenced by PLy_get_error_data().

578 {
579  PyObject *val;
580 
581  val = PyObject_GetAttrString(obj, attrname);
582  if (val != NULL && val != Py_None)
583  {
584  *str = pstrdup(PyString_AsString(val));
585  }
586  Py_XDECREF(val);
587 }
char * pstrdup(const char *in)
Definition: mcxt.c:1186
long val
Definition: informix.c:664

◆ PLy_elog_impl()

void PLy_elog_impl ( int  elevel,
const char *  fmt,
  ... 
)

Definition at line 43 of file plpy_elog.c.

References appendStringInfoVA(), Assert, StringInfoData::data, dgettext, enlargeStringInfo(), ereport, err_generic_string(), errcode(), errcontext, errdetail_internal(), errhint(), errmsg_internal(), FATAL, initStringInfo(), internalerrposition(), internalerrquery(), pfree(), PG_DIAG_COLUMN_NAME, PG_DIAG_CONSTRAINT_NAME, PG_DIAG_DATATYPE_NAME, PG_DIAG_SCHEMA_NAME, PG_DIAG_TABLE_NAME, PG_END_TRY, PG_FINALLY, PG_TRY, PLy_exc_error, PLy_exc_fatal, PLy_exc_spi_error, PLy_get_error_data(), PLy_get_spi_error_data(), PLy_traceback(), TEXTDOMAIN, and val.

Referenced by _PG_init().

44 {
45  int save_errno = errno;
46  char *xmsg;
47  char *tbmsg;
48  int tb_depth;
49  StringInfoData emsg;
50  PyObject *exc,
51  *val,
52  *tb;
53  const char *primary = NULL;
54  int sqlerrcode = 0;
55  char *detail = NULL;
56  char *hint = NULL;
57  char *query = NULL;
58  int position = 0;
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;
64 
65  PyErr_Fetch(&exc, &val, &tb);
66 
67  if (exc != NULL)
68  {
69  PyErr_NormalizeException(&exc, &val, &tb);
70 
71  if (PyErr_GivenExceptionMatches(val, PLy_exc_spi_error))
72  PLy_get_spi_error_data(val, &sqlerrcode,
73  &detail, &hint, &query, &position,
74  &schema_name, &table_name, &column_name,
75  &datatype_name, &constraint_name);
76  else if (PyErr_GivenExceptionMatches(val, PLy_exc_error))
77  PLy_get_error_data(val, &sqlerrcode, &detail, &hint,
78  &schema_name, &table_name, &column_name,
79  &datatype_name, &constraint_name);
80  else if (PyErr_GivenExceptionMatches(val, PLy_exc_fatal))
81  elevel = FATAL;
82  }
83 
84  /* this releases our refcount on tb! */
85  PLy_traceback(exc, val, tb,
86  &xmsg, &tbmsg, &tb_depth);
87 
88  if (fmt)
89  {
90  initStringInfo(&emsg);
91  for (;;)
92  {
93  va_list ap;
94  int needed;
95 
96  errno = save_errno;
97  va_start(ap, fmt);
98  needed = appendStringInfoVA(&emsg, dgettext(TEXTDOMAIN, fmt), ap);
99  va_end(ap);
100  if (needed == 0)
101  break;
102  enlargeStringInfo(&emsg, needed);
103  }
104  primary = emsg.data;
105 
106  /* Since we have a format string, we cannot have a SPI detail. */
107  Assert(detail == NULL);
108 
109  /* If there's an exception message, it goes in the detail. */
110  if (xmsg)
111  detail = xmsg;
112  }
113  else
114  {
115  if (xmsg)
116  primary = xmsg;
117  }
118 
119  PG_TRY();
120  {
121  ereport(elevel,
122  (errcode(sqlerrcode ? sqlerrcode : ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
123  errmsg_internal("%s", primary ? primary : "no exception data"),
124  (detail) ? errdetail_internal("%s", detail) : 0,
125  (tb_depth > 0 && tbmsg) ? errcontext("%s", tbmsg) : 0,
126  (hint) ? errhint("%s", hint) : 0,
127  (query) ? internalerrquery(query) : 0,
128  (position) ? internalerrposition(position) : 0,
129  (schema_name) ? err_generic_string(PG_DIAG_SCHEMA_NAME,
130  schema_name) : 0,
131  (table_name) ? err_generic_string(PG_DIAG_TABLE_NAME,
132  table_name) : 0,
133  (column_name) ? err_generic_string(PG_DIAG_COLUMN_NAME,
134  column_name) : 0,
135  (datatype_name) ? err_generic_string(PG_DIAG_DATATYPE_NAME,
136  datatype_name) : 0,
137  (constraint_name) ? err_generic_string(PG_DIAG_CONSTRAINT_NAME,
138  constraint_name) : 0));
139  }
140  PG_FINALLY();
141  {
142  if (fmt)
143  pfree(emsg.data);
144  if (xmsg)
145  pfree(xmsg);
146  if (tbmsg)
147  pfree(tbmsg);
148  Py_XDECREF(exc);
149  Py_XDECREF(val);
150  }
151  PG_END_TRY();
152 }
int errhint(const char *fmt,...)
Definition: elog.c:1069
#define PG_DIAG_SCHEMA_NAME
Definition: postgres_ext.h:65
#define TEXTDOMAIN
Definition: elog.h:144
#define PG_DIAG_COLUMN_NAME
Definition: postgres_ext.h:67
#define PG_DIAG_TABLE_NAME
Definition: postgres_ext.h:66
int errcode(int sqlerrcode)
Definition: elog.c:608
PyObject * PLy_exc_error
Definition: plpy_elog.c:15
static void PLy_traceback(PyObject *e, PyObject *v, PyObject *tb, char **xmsg, char **tbmsg, int *tb_depth)
Definition: plpy_elog.c:165
int errdetail_internal(const char *fmt,...)
Definition: elog.c:982
void pfree(void *pointer)
Definition: mcxt.c:1056
#define FATAL
Definition: elog.h:52
void enlargeStringInfo(StringInfo str, int needed)
Definition: stringinfo.c:283
#define ereport(elevel, rest)
Definition: elog.h:141
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
static int elevel
Definition: vacuumlazy.c:143
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)
Definition: plpy_elog.c:427
#define PG_FINALLY()
Definition: elog.h:339
#define dgettext(d, x)
Definition: c.h:1133
#define PG_DIAG_DATATYPE_NAME
Definition: postgres_ext.h:68
#define PG_DIAG_CONSTRAINT_NAME
Definition: postgres_ext.h:69
int internalerrquery(const char *query)
Definition: elog.c:1243
int errmsg_internal(const char *fmt,...)
Definition: elog.c:909
PyObject * PLy_exc_fatal
Definition: plpy_elog.c:16
#define Assert(condition)
Definition: c.h:739
#define errcontext
Definition: elog.h:183
int err_generic_string(int field, const char *str)
Definition: elog.c:1273
#define PG_TRY()
Definition: elog.h:322
int appendStringInfoVA(StringInfo str, const char *fmt, va_list args)
Definition: stringinfo.c:133
PyObject * PLy_exc_spi_error
Definition: plpy_elog.c:17
long val
Definition: informix.c:664
#define PG_END_TRY()
Definition: elog.h:347
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)
Definition: plpy_elog.c:391
int internalerrposition(int cursorpos)
Definition: elog.c:1223

◆ PLy_exception_set()

void PLy_exception_set ( PyObject *  exc,
const char *  fmt,
  ... 
)

Definition at line 487 of file plpy_elog.c.

References buf, dgettext, TEXTDOMAIN, and vsnprintf.

Referenced by PLy_cursor(), PLy_cursor_close(), PLy_cursor_fetch(), PLy_cursor_iternext(), PLy_cursor_plan(), PLy_output(), PLy_result_colnames(), PLy_result_coltypes(), PLy_result_coltypmods(), PLy_spi_execute(), PLy_spi_execute_plan(), PLy_spi_execute_query(), PLy_spi_prepare(), PLy_subtransaction_enter(), and PLy_subtransaction_exit().

488 {
489  char buf[1024];
490  va_list ap;
491 
492  va_start(ap, fmt);
493  vsnprintf(buf, sizeof(buf), dgettext(TEXTDOMAIN, fmt), ap);
494  va_end(ap);
495 
496  PyErr_SetString(exc, buf);
497 }
#define TEXTDOMAIN
Definition: elog.h:144
#define vsnprintf
Definition: port.h:191
static char * buf
Definition: pg_test_fsync.c:67
#define dgettext(d, x)
Definition: c.h:1133

◆ PLy_exception_set_plural()

void PLy_exception_set_plural ( PyObject *  exc,
const char *  fmt_singular,
const char *  fmt_plural,
unsigned long  n,
  ... 
)

Definition at line 501 of file plpy_elog.c.

References buf, dngettext, TEXTDOMAIN, and vsnprintf.

Referenced by PLy_cursor_plan(), and PLy_spi_execute_plan().

504 {
505  char buf[1024];
506  va_list ap;
507 
508  va_start(ap, n);
509  vsnprintf(buf, sizeof(buf),
510  dngettext(TEXTDOMAIN, fmt_singular, fmt_plural, n),
511  ap);
512  va_end(ap);
513 
514  PyErr_SetString(exc, buf);
515 }
#define TEXTDOMAIN
Definition: elog.h:144
#define vsnprintf
Definition: port.h:191
static char * buf
Definition: pg_test_fsync.c:67
#define dngettext(d, s, p, n)
Definition: c.h:1135

◆ PLy_exception_set_with_details()

void PLy_exception_set_with_details ( PyObject *  excclass,
ErrorData edata 
)

Definition at line 519 of file plpy_elog.c.

References generate_unaccent_rules::args, ErrorData::column_name, ErrorData::constraint_name, ErrorData::datatype_name, ErrorData::detail, elog, ERROR, error(), ErrorData::hint, ErrorData::internalquery, ErrorData::message, ErrorData::schema_name, set_string_attr(), ErrorData::sqlerrcode, ErrorData::table_name, and unpack_sql_state().

Referenced by PLy_output().

520 {
521  PyObject *args = NULL;
522  PyObject *error = NULL;
523 
524  args = Py_BuildValue("(s)", edata->message);
525  if (!args)
526  goto failure;
527 
528  /* create a new exception with the error message as the parameter */
529  error = PyObject_CallObject(excclass, args);
530  if (!error)
531  goto failure;
532 
533  if (!set_string_attr(error, "sqlstate",
534  unpack_sql_state(edata->sqlerrcode)))
535  goto failure;
536 
537  if (!set_string_attr(error, "detail", edata->detail))
538  goto failure;
539 
540  if (!set_string_attr(error, "hint", edata->hint))
541  goto failure;
542 
543  if (!set_string_attr(error, "query", edata->internalquery))
544  goto failure;
545 
546  if (!set_string_attr(error, "schema_name", edata->schema_name))
547  goto failure;
548 
549  if (!set_string_attr(error, "table_name", edata->table_name))
550  goto failure;
551 
552  if (!set_string_attr(error, "column_name", edata->column_name))
553  goto failure;
554 
555  if (!set_string_attr(error, "datatype_name", edata->datatype_name))
556  goto failure;
557 
558  if (!set_string_attr(error, "constraint_name", edata->constraint_name))
559  goto failure;
560 
561  PyErr_SetObject(excclass, error);
562 
563  Py_DECREF(args);
564  Py_DECREF(error);
565 
566  return;
567 
568 failure:
569  Py_XDECREF(args);
570  Py_XDECREF(error);
571 
572  elog(ERROR, "could not convert error to Python exception");
573 }
char * schema_name
Definition: elog.h:399
static void error(void)
Definition: sql-dyntest.c:147
int sqlerrcode
Definition: elog.h:391
static bool set_string_attr(PyObject *obj, char *attrname, char *str)
Definition: plpy_elog.c:593
char * unpack_sql_state(int sql_state)
Definition: elog.c:2939
char * internalquery
Definition: elog.h:406
#define ERROR
Definition: elog.h:43
char * table_name
Definition: elog.h:400
char * datatype_name
Definition: elog.h:402
char * detail
Definition: elog.h:393
char * column_name
Definition: elog.h:401
char * hint
Definition: elog.h:395
#define elog(elevel,...)
Definition: elog.h:228
char * constraint_name
Definition: elog.h:403
char * message
Definition: elog.h:392

◆ PLy_get_error_data()

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

Definition at line 427 of file plpy_elog.c.

References get_string_attr(), and PLy_get_sqlerrcode().

Referenced by PLy_elog_impl().

430 {
431  PLy_get_sqlerrcode(exc, sqlerrcode);
432  get_string_attr(exc, "detail", detail);
433  get_string_attr(exc, "hint", hint);
434  get_string_attr(exc, "schema_name", schema_name);
435  get_string_attr(exc, "table_name", table_name);
436  get_string_attr(exc, "column_name", column_name);
437  get_string_attr(exc, "datatype_name", datatype_name);
438  get_string_attr(exc, "constraint_name", constraint_name);
439 }
static void get_string_attr(PyObject *obj, char *attrname, char **str)
Definition: plpy_elog.c:577
static void PLy_get_sqlerrcode(PyObject *exc, int *sqlerrcode)
Definition: plpy_elog.c:367

◆ PLy_get_spi_error_data()

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 
)
static

Definition at line 391 of file plpy_elog.c.

References PLy_get_sqlerrcode().

Referenced by PLy_elog_impl().

396 {
397  PyObject *spidata;
398 
399  spidata = PyObject_GetAttrString(exc, "spidata");
400 
401  if (spidata != NULL)
402  {
403  PyArg_ParseTuple(spidata, "izzzizzzzz",
404  sqlerrcode, detail, hint, query, position,
405  schema_name, table_name, column_name,
406  datatype_name, constraint_name);
407  }
408  else
409  {
410  /*
411  * If there's no spidata, at least set the sqlerrcode. This can happen
412  * if someone explicitly raises a SPI exception from Python code.
413  */
414  PLy_get_sqlerrcode(exc, sqlerrcode);
415  }
416 
417  Py_XDECREF(spidata);
418 }
static void PLy_get_sqlerrcode(PyObject *exc, int *sqlerrcode)
Definition: plpy_elog.c:367

◆ PLy_get_sqlerrcode()

static void PLy_get_sqlerrcode ( PyObject *  exc,
int *  sqlerrcode 
)
static

Definition at line 367 of file plpy_elog.c.

References MAKE_SQLSTATE.

Referenced by PLy_get_error_data(), and PLy_get_spi_error_data().

368 {
369  PyObject *sqlstate;
370  char *buffer;
371 
372  sqlstate = PyObject_GetAttrString(exc, "sqlstate");
373  if (sqlstate == NULL)
374  return;
375 
376  buffer = PyString_AsString(sqlstate);
377  if (strlen(buffer) == 5 &&
378  strspn(buffer, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 5)
379  {
380  *sqlerrcode = MAKE_SQLSTATE(buffer[0], buffer[1], buffer[2],
381  buffer[3], buffer[4]);
382  }
383 
384  Py_DECREF(sqlstate);
385 }
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
Definition: elog.h:62

◆ PLy_traceback()

static void PLy_traceback ( PyObject *  e,
PyObject *  v,
PyObject *  tb,
char **  xmsg,
char **  tbmsg,
int *  tb_depth 
)
static

Definition at line 165 of file plpy_elog.c.

References appendStringInfo(), appendStringInfoString(), Assert, PLyExecutionContext::curr_proc, StringInfoData::data, elog, ERROR, filename, get_source_line(), initStringInfo(), name, pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLy_current_execution_context(), PLy_procedure_name(), proname, and PLyProcedure::src.

Referenced by PLy_elog_impl().

167 {
168  PyObject *e_type_o;
169  PyObject *e_module_o;
170  char *e_type_s = NULL;
171  char *e_module_s = NULL;
172  PyObject *vob = NULL;
173  char *vstr;
174  StringInfoData xstr;
175  StringInfoData tbstr;
176 
177  /*
178  * if no exception, return nulls
179  */
180  if (e == NULL)
181  {
182  *xmsg = NULL;
183  *tbmsg = NULL;
184  *tb_depth = 0;
185 
186  return;
187  }
188 
189  /*
190  * Format the exception and its value and put it in xmsg.
191  */
192 
193  e_type_o = PyObject_GetAttrString(e, "__name__");
194  e_module_o = PyObject_GetAttrString(e, "__module__");
195  if (e_type_o)
196  e_type_s = PyString_AsString(e_type_o);
197  if (e_type_s)
198  e_module_s = PyString_AsString(e_module_o);
199 
200  if (v && ((vob = PyObject_Str(v)) != NULL))
201  vstr = PyString_AsString(vob);
202  else
203  vstr = "unknown";
204 
205  initStringInfo(&xstr);
206  if (!e_type_s || !e_module_s)
207  {
208  if (PyString_Check(e))
209  /* deprecated string exceptions */
210  appendStringInfoString(&xstr, PyString_AsString(e));
211  else
212  /* shouldn't happen */
213  appendStringInfoString(&xstr, "unrecognized exception");
214  }
215  /* mimics behavior of traceback.format_exception_only */
216  else if (strcmp(e_module_s, "builtins") == 0
217  || strcmp(e_module_s, "__main__") == 0
218  || strcmp(e_module_s, "exceptions") == 0)
219  appendStringInfo(&xstr, "%s", e_type_s);
220  else
221  appendStringInfo(&xstr, "%s.%s", e_module_s, e_type_s);
222  appendStringInfo(&xstr, ": %s", vstr);
223 
224  *xmsg = xstr.data;
225 
226  /*
227  * Now format the traceback and put it in tbmsg.
228  */
229 
230  *tb_depth = 0;
231  initStringInfo(&tbstr);
232  /* Mimic Python traceback reporting as close as possible. */
233  appendStringInfoString(&tbstr, "Traceback (most recent call last):");
234  while (tb != NULL && tb != Py_None)
235  {
236  PyObject *volatile tb_prev = NULL;
237  PyObject *volatile frame = NULL;
238  PyObject *volatile code = NULL;
239  PyObject *volatile name = NULL;
240  PyObject *volatile lineno = NULL;
241  PyObject *volatile filename = NULL;
242 
243  PG_TRY();
244  {
245  /*
246  * Ancient versions of Python (circa 2.3) contain a bug whereby
247  * the fetches below can fail if the error indicator is set.
248  */
249  PyErr_Clear();
250 
251  lineno = PyObject_GetAttrString(tb, "tb_lineno");
252  if (lineno == NULL)
253  elog(ERROR, "could not get line number from Python traceback");
254 
255  frame = PyObject_GetAttrString(tb, "tb_frame");
256  if (frame == NULL)
257  elog(ERROR, "could not get frame from Python traceback");
258 
259  code = PyObject_GetAttrString(frame, "f_code");
260  if (code == NULL)
261  elog(ERROR, "could not get code object from Python frame");
262 
263  name = PyObject_GetAttrString(code, "co_name");
264  if (name == NULL)
265  elog(ERROR, "could not get function name from Python code object");
266 
267  filename = PyObject_GetAttrString(code, "co_filename");
268  if (filename == NULL)
269  elog(ERROR, "could not get file name from Python code object");
270  }
271  PG_CATCH();
272  {
273  Py_XDECREF(frame);
274  Py_XDECREF(code);
275  Py_XDECREF(name);
276  Py_XDECREF(lineno);
277  Py_XDECREF(filename);
278  PG_RE_THROW();
279  }
280  PG_END_TRY();
281 
282  /* The first frame always points at <module>, skip it. */
283  if (*tb_depth > 0)
284  {
286  char *proname;
287  char *fname;
288  char *line;
289  char *plain_filename;
290  long plain_lineno;
291 
292  /*
293  * The second frame points at the internal function, but to mimic
294  * Python error reporting we want to say <module>.
295  */
296  if (*tb_depth == 1)
297  fname = "<module>";
298  else
299  fname = PyString_AsString(name);
300 
301  proname = PLy_procedure_name(exec_ctx->curr_proc);
302  plain_filename = PyString_AsString(filename);
303  plain_lineno = PyInt_AsLong(lineno);
304 
305  if (proname == NULL)
307  &tbstr, "\n PL/Python anonymous code block, line %ld, in %s",
308  plain_lineno - 1, fname);
309  else
311  &tbstr, "\n PL/Python function \"%s\", line %ld, in %s",
312  proname, plain_lineno - 1, fname);
313 
314  /*
315  * function code object was compiled with "<string>" as the
316  * filename
317  */
318  if (exec_ctx->curr_proc && plain_filename != NULL &&
319  strcmp(plain_filename, "<string>") == 0)
320  {
321  /*
322  * If we know the current procedure, append the exact line
323  * from the source, again mimicking Python's traceback.py
324  * module behavior. We could store the already line-split
325  * source to avoid splitting it every time, but producing a
326  * traceback is not the most important scenario to optimize
327  * for. But we do not go as far as traceback.py in reading
328  * the source of imported modules.
329  */
330  line = get_source_line(exec_ctx->curr_proc->src, plain_lineno);
331  if (line)
332  {
333  appendStringInfo(&tbstr, "\n %s", line);
334  pfree(line);
335  }
336  }
337  }
338 
339  Py_DECREF(frame);
340  Py_DECREF(code);
341  Py_DECREF(name);
342  Py_DECREF(lineno);
343  Py_DECREF(filename);
344 
345  /* Release the current frame and go to the next one. */
346  tb_prev = tb;
347  tb = PyObject_GetAttrString(tb, "tb_next");
348  Assert(tb_prev != Py_None);
349  Py_DECREF(tb_prev);
350  if (tb == NULL)
351  elog(ERROR, "could not traverse Python traceback");
352  (*tb_depth)++;
353  }
354 
355  /* Return the traceback. */
356  *tbmsg = tbstr.data;
357 
358  Py_XDECREF(e_type_o);
359  Py_XDECREF(e_module_o);
360  Py_XDECREF(vob);
361 }
NameData proname
Definition: pg_proc.h:35
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:410
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
char * PLy_procedure_name(PLyProcedure *proc)
#define ERROR
Definition: elog.h:43
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
PLyProcedure * curr_proc
Definition: plpy_main.h:20
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define PG_CATCH()
Definition: elog.h:332
#define Assert(condition)
Definition: c.h:739
#define PG_RE_THROW()
Definition: elog.h:363
const char * name
Definition: encode.c:521
static char * filename
Definition: pg_dumpall.c:90
e
Definition: preproc-init.c:82
#define elog(elevel,...)
Definition: elog.h:228
#define PG_TRY()
Definition: elog.h:322
static char * get_source_line(const char *src, int lineno)
Definition: plpy_elog.c:445
#define PG_END_TRY()
Definition: elog.h:347

◆ set_string_attr()

static bool set_string_attr ( PyObject *  obj,
char *  attrname,
char *  str 
)
static

Definition at line 593 of file plpy_elog.c.

References val.

Referenced by PLy_exception_set_with_details().

594 {
595  int result;
596  PyObject *val;
597 
598  if (str != NULL)
599  {
600  val = PyString_FromString(str);
601  if (!val)
602  return false;
603  }
604  else
605  {
606  val = Py_None;
607  Py_INCREF(Py_None);
608  }
609 
610  result = PyObject_SetAttrString(obj, attrname, val);
611  Py_DECREF(val);
612 
613  return result != -1;
614 }
long val
Definition: informix.c:664

Variable Documentation

◆ PLy_exc_error

◆ PLy_exc_fatal

PyObject* PLy_exc_fatal = NULL

Definition at line 16 of file plpy_elog.c.

Referenced by PLy_add_exceptions(), and PLy_elog_impl().

◆ PLy_exc_spi_error

PyObject* PLy_exc_spi_error = NULL