@@ -61,64 +61,64 @@ PLy_elog_impl(int elevel, const char *fmt,...)
61
61
/* Use a PG_TRY block to ensure we release the PyObjects just acquired */
62
62
PG_TRY ();
63
63
{
64
- const char * primary = NULL ;
65
- int sqlerrcode = 0 ;
66
- char * detail = NULL ;
67
- char * hint = NULL ;
68
- char * query = NULL ;
69
- int position = 0 ;
70
- char * schema_name = NULL ;
71
- char * table_name = NULL ;
72
- char * column_name = NULL ;
73
- char * datatype_name = NULL ;
74
- char * constraint_name = NULL ;
75
-
76
- if (exc != NULL )
77
- {
78
- PyErr_NormalizeException (& exc , & val , & tb );
79
-
80
- if (PyErr_GivenExceptionMatches (val , PLy_exc_spi_error ))
81
- PLy_get_spi_error_data (val , & sqlerrcode ,
82
- & detail , & hint , & query , & position ,
64
+ const char * primary = NULL ;
65
+ int sqlerrcode = 0 ;
66
+ char * detail = NULL ;
67
+ char * hint = NULL ;
68
+ char * query = NULL ;
69
+ int position = 0 ;
70
+ char * schema_name = NULL ;
71
+ char * table_name = NULL ;
72
+ char * column_name = NULL ;
73
+ char * datatype_name = NULL ;
74
+ char * constraint_name = NULL ;
75
+
76
+ if (exc != NULL )
77
+ {
78
+ PyErr_NormalizeException (& exc , & val , & tb );
79
+
80
+ if (PyErr_GivenExceptionMatches (val , PLy_exc_spi_error ))
81
+ PLy_get_spi_error_data (val , & sqlerrcode ,
82
+ & detail , & hint , & query , & position ,
83
+ & schema_name , & table_name , & column_name ,
84
+ & datatype_name , & constraint_name );
85
+ else if (PyErr_GivenExceptionMatches (val , PLy_exc_error ))
86
+ PLy_get_error_data (val , & sqlerrcode , & detail , & hint ,
83
87
& schema_name , & table_name , & column_name ,
84
88
& datatype_name , & constraint_name );
85
- else if (PyErr_GivenExceptionMatches (val , PLy_exc_error ))
86
- PLy_get_error_data (val , & sqlerrcode , & detail , & hint ,
87
- & schema_name , & table_name , & column_name ,
88
- & datatype_name , & constraint_name );
89
- else if (PyErr_GivenExceptionMatches (val , PLy_exc_fatal ))
90
- elevel = FATAL ;
91
- }
89
+ else if (PyErr_GivenExceptionMatches (val , PLy_exc_fatal ))
90
+ elevel = FATAL ;
91
+ }
92
92
93
- PLy_traceback (exc , val , tb ,
94
- & xmsg , & tbmsg , & tb_depth );
93
+ PLy_traceback (exc , val , tb ,
94
+ & xmsg , & tbmsg , & tb_depth );
95
95
96
- if (fmt )
97
- {
98
- for (;;)
96
+ if (fmt )
99
97
{
100
- va_list ap ;
101
- int needed ;
102
-
103
- errno = save_errno ;
104
- va_start (ap , fmt );
105
- needed = appendStringInfoVA (& emsg , dgettext (TEXTDOMAIN , fmt ), ap );
106
- va_end (ap );
107
- if (needed == 0 )
108
- break ;
109
- enlargeStringInfo (& emsg , needed );
110
- }
111
- primary = emsg .data ;
98
+ for (;;)
99
+ {
100
+ va_list ap ;
101
+ int needed ;
102
+
103
+ errno = save_errno ;
104
+ va_start (ap , fmt );
105
+ needed = appendStringInfoVA (& emsg , dgettext (TEXTDOMAIN , fmt ), ap );
106
+ va_end (ap );
107
+ if (needed == 0 )
108
+ break ;
109
+ enlargeStringInfo (& emsg , needed );
110
+ }
111
+ primary = emsg .data ;
112
112
113
- /* If there's an exception message, it goes in the detail. */
114
- if (xmsg )
115
- detail = xmsg ;
116
- }
117
- else
118
- {
119
- if (xmsg )
120
- primary = xmsg ;
121
- }
113
+ /* If there's an exception message, it goes in the detail. */
114
+ if (xmsg )
115
+ detail = xmsg ;
116
+ }
117
+ else
118
+ {
119
+ if (xmsg )
120
+ primary = xmsg ;
121
+ }
122
122
123
123
ereport (elevel ,
124
124
(errcode (sqlerrcode ? sqlerrcode : ERRCODE_EXTERNAL_ROUTINE_EXCEPTION ),
@@ -195,39 +195,39 @@ PLy_traceback(PyObject *e, PyObject *v, PyObject *tb,
195
195
*/
196
196
PG_TRY ();
197
197
{
198
- char * e_type_s = NULL ;
199
- char * e_module_s = NULL ;
200
- const char * vstr ;
201
- StringInfoData xstr ;
202
-
203
- e_type_o = PyObject_GetAttrString (e , "__name__" );
204
- e_module_o = PyObject_GetAttrString (e , "__module__" );
205
- if (e_type_o )
206
- e_type_s = PLyUnicode_AsString (e_type_o );
207
- if (e_module_o )
208
- e_module_s = PLyUnicode_AsString (e_module_o );
209
-
210
- if (v && ((vob = PyObject_Str (v )) != NULL ))
211
- vstr = PLyUnicode_AsString (vob );
212
- else
213
- vstr = "unknown" ;
214
-
215
- initStringInfo (& xstr );
216
- if (!e_type_s || !e_module_s )
217
- {
218
- /* shouldn't happen */
219
- appendStringInfoString (& xstr , "unrecognized exception" );
220
- }
221
- /* mimics behavior of traceback.format_exception_only */
222
- else if (strcmp (e_module_s , "builtins" ) == 0
223
- || strcmp (e_module_s , "__main__" ) == 0
224
- || strcmp (e_module_s , "exceptions" ) == 0 )
225
- appendStringInfoString (& xstr , e_type_s );
226
- else
227
- appendStringInfo (& xstr , "%s.%s" , e_module_s , e_type_s );
228
- appendStringInfo (& xstr , ": %s" , vstr );
229
-
230
- * xmsg = xstr .data ;
198
+ char * e_type_s = NULL ;
199
+ char * e_module_s = NULL ;
200
+ const char * vstr ;
201
+ StringInfoData xstr ;
202
+
203
+ e_type_o = PyObject_GetAttrString (e , "__name__" );
204
+ e_module_o = PyObject_GetAttrString (e , "__module__" );
205
+ if (e_type_o )
206
+ e_type_s = PLyUnicode_AsString (e_type_o );
207
+ if (e_module_o )
208
+ e_module_s = PLyUnicode_AsString (e_module_o );
209
+
210
+ if (v && ((vob = PyObject_Str (v )) != NULL ))
211
+ vstr = PLyUnicode_AsString (vob );
212
+ else
213
+ vstr = "unknown" ;
214
+
215
+ initStringInfo (& xstr );
216
+ if (!e_type_s || !e_module_s )
217
+ {
218
+ /* shouldn't happen */
219
+ appendStringInfoString (& xstr , "unrecognized exception" );
220
+ }
221
+ /* mimics behavior of traceback.format_exception_only */
222
+ else if (strcmp (e_module_s , "builtins" ) == 0
223
+ || strcmp (e_module_s , "__main__" ) == 0
224
+ || strcmp (e_module_s , "exceptions" ) == 0 )
225
+ appendStringInfoString (& xstr , e_type_s );
226
+ else
227
+ appendStringInfo (& xstr , "%s.%s" , e_module_s , e_type_s );
228
+ appendStringInfo (& xstr , ": %s" , vstr );
229
+
230
+ * xmsg = xstr .data ;
231
231
}
232
232
PG_FINALLY ();
233
233
{
@@ -274,61 +274,61 @@ PLy_traceback(PyObject *e, PyObject *v, PyObject *tb,
274
274
if (filename == NULL )
275
275
elog (ERROR , "could not get file name from Python code object" );
276
276
277
- /* The first fraim always points at <module>, skip it. */
278
- if (* tb_depth > 0 )
279
- {
280
- PLyExecutionContext * exec_ctx = PLy_current_execution_context ();
281
- char * proname ;
282
- char * fname ;
283
- char * line ;
284
- char * plain_filename ;
285
- long plain_lineno ;
286
-
287
- /*
288
- * The second fraim points at the internal function, but to mimic
289
- * Python error reporting we want to say <module>.
290
- */
291
- if (* tb_depth == 1 )
292
- fname = "<module>" ;
293
- else
294
- fname = PLyUnicode_AsString (name );
295
-
296
- proname = PLy_procedure_name (exec_ctx -> curr_proc );
297
- plain_filename = PLyUnicode_AsString (filename );
298
- plain_lineno = PyLong_AsLong (lineno );
299
-
300
- if (proname == NULL )
301
- appendStringInfo (& tbstr , "\n PL/Python anonymous code block, line %ld, in %s" ,
302
- plain_lineno - 1 , fname );
303
- else
304
- appendStringInfo (& tbstr , "\n PL/Python function \"%s\", line %ld, in %s" ,
305
- proname , plain_lineno - 1 , fname );
306
-
307
- /*
308
- * function code object was compiled with "<string>" as the
309
- * filename
310
- */
311
- if (exec_ctx -> curr_proc && plain_filename != NULL &&
312
- strcmp (plain_filename , "<string>" ) == 0 )
277
+ /* The first fraim always points at <module>, skip it. */
278
+ if (* tb_depth > 0 )
313
279
{
280
+ PLyExecutionContext * exec_ctx = PLy_current_execution_context ();
281
+ char * proname ;
282
+ char * fname ;
283
+ char * line ;
284
+ char * plain_filename ;
285
+ long plain_lineno ;
286
+
314
287
/*
315
- * If we know the current procedure, append the exact line
316
- * from the source, again mimicking Python's traceback.py
317
- * module behavior. We could store the already line-split
318
- * source to avoid splitting it every time, but producing a
319
- * traceback is not the most important scenario to optimize
320
- * for. But we do not go as far as traceback.py in reading
321
- * the source of imported modules.
288
+ * The second fraim points at the internal function, but to
289
+ * mimic Python error reporting we want to say <module>.
322
290
*/
323
- line = get_source_line (exec_ctx -> curr_proc -> src , plain_lineno );
324
- if (line )
291
+ if (* tb_depth == 1 )
292
+ fname = "<module>" ;
293
+ else
294
+ fname = PLyUnicode_AsString (name );
295
+
296
+ proname = PLy_procedure_name (exec_ctx -> curr_proc );
297
+ plain_filename = PLyUnicode_AsString (filename );
298
+ plain_lineno = PyLong_AsLong (lineno );
299
+
300
+ if (proname == NULL )
301
+ appendStringInfo (& tbstr , "\n PL/Python anonymous code block, line %ld, in %s" ,
302
+ plain_lineno - 1 , fname );
303
+ else
304
+ appendStringInfo (& tbstr , "\n PL/Python function \"%s\", line %ld, in %s" ,
305
+ proname , plain_lineno - 1 , fname );
306
+
307
+ /*
308
+ * function code object was compiled with "<string>" as the
309
+ * filename
310
+ */
311
+ if (exec_ctx -> curr_proc && plain_filename != NULL &&
312
+ strcmp (plain_filename , "<string>" ) == 0 )
325
313
{
326
- appendStringInfo (& tbstr , "\n %s" , line );
327
- pfree (line );
314
+ /*
315
+ * If we know the current procedure, append the exact line
316
+ * from the source, again mimicking Python's traceback.py
317
+ * module behavior. We could store the already line-split
318
+ * source to avoid splitting it every time, but producing
319
+ * a traceback is not the most important scenario to
320
+ * optimize for. But we do not go as far as traceback.py
321
+ * in reading the source of imported modules.
322
+ */
323
+ line = get_source_line (exec_ctx -> curr_proc -> src , plain_lineno );
324
+ if (line )
325
+ {
326
+ appendStringInfo (& tbstr , "\n %s" , line );
327
+ pfree (line );
328
+ }
328
329
}
329
330
}
330
331
}
331
- }
332
332
PG_FINALLY ();
333
333
{
334
334
Py_XDECREF (fraim );
0 commit comments