OSDN Git Service

9081cffb498bedfc411afecab374441c8a58bda6
[pg-rex/syncrep.git] / src / pl / plpython / plpython.c
1 /**********************************************************************
2  * plpython.c - python as a procedural language for PostgreSQL
3  *
4  *      src/pl/plpython/plpython.c
5  *
6  *********************************************************************
7  */
8
9 #if defined(_MSC_VER) && defined(_DEBUG)
10 /* Python uses #pragma to bring in a non-default libpython on VC++ if
11  * _DEBUG is defined */
12 #undef _DEBUG
13 /* Also hide away errcode, since we load Python.h before postgres.h */
14 #define errcode __msvc_errcode
15 #include <Python.h>
16 #undef errcode
17 #define _DEBUG
18 #elif defined (_MSC_VER)
19 #define errcode __msvc_errcode
20 #include <Python.h>
21 #undef errcode
22 #else
23 #include <Python.h>
24 #endif
25
26 /*
27  * Py_ssize_t compat for Python <= 2.4
28  */
29 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
30 typedef int Py_ssize_t;
31
32 #define PY_SSIZE_T_MAX INT_MAX
33 #define PY_SSIZE_T_MIN INT_MIN
34 #endif
35
36 /*
37  * PyBool_FromLong is supported from 2.3.
38  */
39 #if PY_VERSION_HEX < 0x02030000
40 #define PyBool_FromLong(x) PyInt_FromLong(x)
41 #endif
42
43 /*
44  * Python 2/3 strings/unicode/bytes handling.  Python 2 has strings
45  * and unicode, Python 3 has strings, which are unicode on the C
46  * level, and bytes.  The porting convention, which is similarly used
47  * in Python 2.6, is that "Unicode" is always unicode, and "Bytes" are
48  * bytes in Python 3 and strings in Python 2.  Since we keep
49  * supporting Python 2 and its usual strings, we provide a
50  * compatibility layer for Python 3 that when asked to convert a C
51  * string to a Python string it converts the C string from the
52  * PostgreSQL server encoding to a Python Unicode object.
53  */
54
55 #if PY_VERSION_HEX < 0x02060000
56 /* This is exactly the compatibility layer that Python 2.6 uses. */
57 #define PyBytes_AsString PyString_AsString
58 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
59 #define PyBytes_Size PyString_Size
60 #define PyObject_Bytes PyObject_Str
61 #endif
62
63 #if PY_MAJOR_VERSION >= 3
64 #define PyString_Check(x) 0
65 #define PyString_AsString(x) PLyUnicode_AsString(x)
66 #define PyString_FromString(x) PLyUnicode_FromString(x)
67 #endif
68
69 /*
70  * Python 3 only has long.
71  */
72 #if PY_MAJOR_VERSION >= 3
73 #define PyInt_FromLong(x) PyLong_FromLong(x)
74 #define PyInt_AsLong(x) PyLong_AsLong(x)
75 #endif
76
77 /*
78  * PyVarObject_HEAD_INIT was added in Python 2.6.  Its use is
79  * necessary to handle both Python 2 and 3.  This replacement
80  * definition is for Python <=2.5
81  */
82 #ifndef PyVarObject_HEAD_INIT
83 #define PyVarObject_HEAD_INIT(type, size)               \
84                 PyObject_HEAD_INIT(type) size,
85 #endif
86
87 #include "postgres.h"
88
89 /* system stuff */
90 #include <unistd.h>
91 #include <fcntl.h>
92
93 /* postgreSQL stuff */
94 #include "catalog/pg_proc.h"
95 #include "catalog/pg_type.h"
96 #include "commands/trigger.h"
97 #include "executor/spi.h"
98 #include "funcapi.h"
99 #include "fmgr.h"
100 #include "mb/pg_wchar.h"
101 #include "miscadmin.h"
102 #include "nodes/makefuncs.h"
103 #include "parser/parse_type.h"
104 #include "tcop/tcopprot.h"
105 #include "access/transam.h"
106 #include "access/xact.h"
107 #include "utils/builtins.h"
108 #include "utils/hsearch.h"
109 #include "utils/lsyscache.h"
110 #include "utils/memutils.h"
111 #include "utils/syscache.h"
112 #include "utils/typcache.h"
113
114 /* define our text domain for translations */
115 #undef TEXTDOMAIN
116 #define TEXTDOMAIN PG_TEXTDOMAIN("plpython")
117
118 #include <compile.h>
119 #include <eval.h>
120
121 PG_MODULE_MAGIC;
122
123 /* convert Postgresql Datum or tuple into a PyObject.
124  * input to Python.  Tuples are converted to dictionary
125  * objects.
126  */
127
128 struct PLyDatumToOb;
129 typedef PyObject *(*PLyDatumToObFunc) (struct PLyDatumToOb *, Datum);
130
131 typedef struct PLyDatumToOb
132 {
133         PLyDatumToObFunc func;
134         FmgrInfo        typfunc;                /* The type's output function */
135         Oid                     typoid;                 /* The OID of the type */
136         int32           typmod;                 /* The typmod of the type */
137         Oid                     typioparam;
138         bool            typbyval;
139         int16           typlen;
140         char            typalign;
141         struct PLyDatumToOb *elm;
142 } PLyDatumToOb;
143
144 typedef struct PLyTupleToOb
145 {
146         PLyDatumToOb *atts;
147         int                     natts;
148 } PLyTupleToOb;
149
150 typedef union PLyTypeInput
151 {
152         PLyDatumToOb d;
153         PLyTupleToOb r;
154 } PLyTypeInput;
155
156 /* convert PyObject to a Postgresql Datum or tuple.
157  * output from Python
158  */
159
160 struct PLyObToDatum;
161 typedef Datum (*PLyObToDatumFunc) (struct PLyObToDatum *, int32 typmod,
162                                                                                            PyObject *);
163
164 typedef struct PLyObToDatum
165 {
166         PLyObToDatumFunc func;
167         FmgrInfo        typfunc;                /* The type's input function */
168         Oid                     typoid;                 /* The OID of the type */
169         int32           typmod;                 /* The typmod of the type */
170         Oid                     typioparam;
171         bool            typbyval;
172         int16           typlen;
173         char            typalign;
174         struct PLyObToDatum *elm;
175 } PLyObToDatum;
176
177 typedef struct PLyObToTuple
178 {
179         PLyObToDatum *atts;
180         int                     natts;
181 } PLyObToTuple;
182
183 typedef union PLyTypeOutput
184 {
185         PLyObToDatum d;
186         PLyObToTuple r;
187 } PLyTypeOutput;
188
189 /* all we need to move Postgresql data to Python objects,
190  * and vice versa
191  */
192 typedef struct PLyTypeInfo
193 {
194         PLyTypeInput in;
195         PLyTypeOutput out;
196
197         /*
198          * is_rowtype can be: -1 = not known yet (initial state); 0 = scalar
199          * datatype; 1 = rowtype; 2 = rowtype, but I/O functions not set up yet
200          */
201         int                     is_rowtype;
202         /* used to check if the type has been modified */
203         Oid                     typ_relid;
204         TransactionId typrel_xmin;
205         ItemPointerData typrel_tid;
206 } PLyTypeInfo;
207
208
209 /* cached procedure data */
210 typedef struct PLyProcedure
211 {
212         char       *proname;            /* SQL name of procedure */
213         char       *pyname;                     /* Python name of procedure */
214         TransactionId fn_xmin;
215         ItemPointerData fn_tid;
216         bool            fn_readonly;
217         PLyTypeInfo result;                     /* also used to store info for trigger tuple
218                                                                  * type */
219         bool            is_setof;               /* true, if procedure returns result set */
220         PyObject   *setof;                      /* contents of result set. */
221         char       *src;                        /* textual procedure code, after mangling */
222         char      **argnames;           /* Argument names */
223         PLyTypeInfo args[FUNC_MAX_ARGS];
224         int                     nargs;
225         PyObject   *code;                       /* compiled procedure code */
226         PyObject   *statics;            /* data saved across calls, local scope */
227         PyObject   *globals;            /* data saved across calls, global scope */
228 } PLyProcedure;
229
230
231 /* the procedure cache entry */
232 typedef struct PLyProcedureEntry
233 {
234         Oid                     fn_oid;                 /* hash key */
235         PLyProcedure *proc;
236 } PLyProcedureEntry;
237
238 /* explicit subtransaction data */
239 typedef struct PLySubtransactionData
240 {
241         MemoryContext oldcontext;
242         ResourceOwner oldowner;
243 } PLySubtransactionData;
244
245
246 /* Python objects */
247 typedef struct PLyPlanObject
248 {
249         PyObject_HEAD
250         void       *plan;                       /* return of an SPI_saveplan */
251         int                     nargs;
252         Oid                *types;
253         Datum      *values;
254         PLyTypeInfo *args;
255 } PLyPlanObject;
256
257 typedef struct PLyResultObject
258 {
259         PyObject_HEAD
260         /* HeapTuple *tuples; */
261         PyObject   *nrows;                      /* number of rows returned by query */
262         PyObject   *rows;                       /* data rows, or None if no data returned */
263         PyObject   *status;                     /* query status, SPI_OK_*, or SPI_ERR_* */
264 } PLyResultObject;
265
266 typedef struct PLySubtransactionObject
267 {
268         PyObject_HEAD
269         bool            started;
270         bool            exited;
271 } PLySubtransactionObject;
272
273 /* A list of all known exceptions, generated from backend/utils/errcodes.txt */
274 typedef struct ExceptionMap
275 {
276         char       *name;
277         char       *classname;
278         int                     sqlstate;
279 } ExceptionMap;
280
281 static const ExceptionMap exception_map[] = {
282 #include "spiexceptions.h"
283         {NULL, NULL, 0}
284 };
285
286 /* A hash table mapping sqlstates to exceptions, for speedy lookup */
287 static HTAB *PLy_spi_exceptions;
288
289 typedef struct PLyExceptionEntry
290 {
291         int                     sqlstate;               /* hash key, must be first */
292         PyObject   *exc;                        /* corresponding exception */
293 } PLyExceptionEntry;
294
295
296 /* function declarations */
297
298 #if PY_MAJOR_VERSION >= 3
299 /* Use separate names to avoid clash in pg_pltemplate */
300 #define plpython_validator plpython3_validator
301 #define plpython_call_handler plpython3_call_handler
302 #define plpython_inline_handler plpython3_inline_handler
303 #endif
304
305 /* exported functions */
306 Datum           plpython_validator(PG_FUNCTION_ARGS);
307 Datum           plpython_call_handler(PG_FUNCTION_ARGS);
308 Datum           plpython_inline_handler(PG_FUNCTION_ARGS);
309 void            _PG_init(void);
310
311 PG_FUNCTION_INFO_V1(plpython_validator);
312 PG_FUNCTION_INFO_V1(plpython_call_handler);
313 PG_FUNCTION_INFO_V1(plpython_inline_handler);
314
315 /* most of the remaining of the declarations, all static */
316
317 /*
318  * These should only be called once from _PG_init.      Initialize the
319  * Python interpreter and global data.
320  */
321 static void PLy_init_interp(void);
322 static void PLy_init_plpy(void);
323
324 /* call PyErr_SetString with a vprint interface and translation support */
325 static void
326 PLy_exception_set(PyObject *, const char *,...)
327 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
328
329 /* same, with pluralized message */
330 static void
331 PLy_exception_set_plural(PyObject *, const char *, const char *,
332                                                  unsigned long n,...)
333 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 5)))
334 __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 5)));
335
336 /* like PLy_exception_set, but conserve more fields from ErrorData */
337 static void PLy_spi_exception_set(PyObject *excclass, ErrorData *edata);
338
339 /* Get the innermost python procedure called from the backend */
340 static char *PLy_procedure_name(PLyProcedure *);
341
342 /* some utility functions */
343 static void
344 PLy_elog(int, const char *,...)
345 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
346 static void PLy_get_spi_error_data(PyObject *exc, char **detail, char **hint, char **query, int *position);
347 static void PLy_traceback(char **, char **, int *);
348
349 static void *PLy_malloc(size_t);
350 static void *PLy_malloc0(size_t);
351 static char *PLy_strdup(const char *);
352 static void PLy_free(void *);
353
354 static PyObject *PLyUnicode_Bytes(PyObject *unicode);
355 static char *PLyUnicode_AsString(PyObject *unicode);
356
357 #if PY_MAJOR_VERSION >= 3
358 static PyObject *PLyUnicode_FromString(const char *s);
359 #endif
360
361 /* sub handlers for functions and triggers */
362 static Datum PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *);
363 static HeapTuple PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *);
364
365 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *);
366 static void PLy_function_delete_args(PLyProcedure *);
367 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *,
368                                            HeapTuple *);
369 static HeapTuple PLy_modify_tuple(PLyProcedure *, PyObject *,
370                                  TriggerData *, HeapTuple);
371
372 static PyObject *PLy_procedure_call(PLyProcedure *, char *, PyObject *);
373
374 static PLyProcedure *PLy_procedure_get(Oid fn_oid, bool is_trigger);
375
376 static PLyProcedure *PLy_procedure_create(HeapTuple procTup,
377                                          Oid fn_oid, bool is_trigger);
378
379 static void PLy_procedure_compile(PLyProcedure *, const char *);
380 static char *PLy_procedure_munge_source(const char *, const char *);
381 static void PLy_procedure_delete(PLyProcedure *);
382
383 static void PLy_typeinfo_init(PLyTypeInfo *);
384 static void PLy_typeinfo_dealloc(PLyTypeInfo *);
385 static void PLy_output_datum_func(PLyTypeInfo *, HeapTuple);
386 static void PLy_output_datum_func2(PLyObToDatum *, HeapTuple);
387 static void PLy_input_datum_func(PLyTypeInfo *, Oid, HeapTuple);
388 static void PLy_input_datum_func2(PLyDatumToOb *, Oid, HeapTuple);
389 static void PLy_output_tuple_funcs(PLyTypeInfo *, TupleDesc);
390 static void PLy_input_tuple_funcs(PLyTypeInfo *, TupleDesc);
391 static void PLy_output_record_funcs(PLyTypeInfo *, TupleDesc);
392
393 /* conversion functions */
394 static PyObject *PLyBool_FromBool(PLyDatumToOb *arg, Datum d);
395 static PyObject *PLyFloat_FromFloat4(PLyDatumToOb *arg, Datum d);
396 static PyObject *PLyFloat_FromFloat8(PLyDatumToOb *arg, Datum d);
397 static PyObject *PLyFloat_FromNumeric(PLyDatumToOb *arg, Datum d);
398 static PyObject *PLyInt_FromInt16(PLyDatumToOb *arg, Datum d);
399 static PyObject *PLyInt_FromInt32(PLyDatumToOb *arg, Datum d);
400 static PyObject *PLyLong_FromInt64(PLyDatumToOb *arg, Datum d);
401 static PyObject *PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d);
402 static PyObject *PLyString_FromDatum(PLyDatumToOb *arg, Datum d);
403 static PyObject *PLyList_FromArray(PLyDatumToOb *arg, Datum d);
404
405 static PyObject *PLyDict_FromTuple(PLyTypeInfo *, HeapTuple, TupleDesc);
406
407 static Datum PLyObject_ToBool(PLyObToDatum *, int32, PyObject *);
408 static Datum PLyObject_ToBytea(PLyObToDatum *, int32, PyObject *);
409 static Datum PLyObject_ToComposite(PLyObToDatum *, int32, PyObject *);
410 static Datum PLyObject_ToDatum(PLyObToDatum *, int32, PyObject *);
411 static Datum PLySequence_ToArray(PLyObToDatum *, int32, PyObject *);
412
413 static HeapTuple PLyObject_ToTuple(PLyTypeInfo *, TupleDesc, PyObject *);
414 static HeapTuple PLyMapping_ToTuple(PLyTypeInfo *, TupleDesc, PyObject *);
415 static HeapTuple PLySequence_ToTuple(PLyTypeInfo *, TupleDesc, PyObject *);
416 static HeapTuple PLyGenericObject_ToTuple(PLyTypeInfo *, TupleDesc, PyObject *);
417
418 /*
419  * Currently active plpython function
420  */
421 static PLyProcedure *PLy_curr_procedure = NULL;
422
423 /* list of explicit subtransaction data */
424 static List *explicit_subtransactions = NIL;
425
426 static PyObject *PLy_interp_globals = NULL;
427 static PyObject *PLy_interp_safe_globals = NULL;
428 static HTAB *PLy_procedure_cache = NULL;
429 static HTAB *PLy_trigger_cache = NULL;
430
431 /* Python exceptions */
432 static PyObject *PLy_exc_error = NULL;
433 static PyObject *PLy_exc_fatal = NULL;
434 static PyObject *PLy_exc_spi_error = NULL;
435
436 /* some globals for the python module */
437 static char PLy_plan_doc[] = {
438         "Store a PostgreSQL plan"
439 };
440
441 static char PLy_result_doc[] = {
442         "Results of a PostgreSQL query"
443 };
444
445 static char PLy_subtransaction_doc[] = {
446         "PostgreSQL subtransaction context manager"
447 };
448
449
450 /*
451  * the function definitions
452  */
453
454 /*
455  * This routine is a crock, and so is everyplace that calls it.  The problem
456  * is that the cached form of plpython functions/queries is allocated permanently
457  * (mostly via malloc()) and never released until backend exit.  Subsidiary
458  * data structures such as fmgr info records therefore must live forever
459  * as well.  A better implementation would store all this stuff in a per-
460  * function memory context that could be reclaimed at need.  In the meantime,
461  * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
462  * it might allocate, and whatever the eventual function might allocate using
463  * fn_mcxt, will live forever too.
464  */
465 static void
466 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
467 {
468         fmgr_info_cxt(functionId, finfo, TopMemoryContext);
469 }
470
471 static void
472 plpython_error_callback(void *arg)
473 {
474         if (PLy_curr_procedure)
475                 errcontext("PL/Python function \"%s\"",
476                                    PLy_procedure_name(PLy_curr_procedure));
477 }
478
479 static void
480 plpython_inline_error_callback(void *arg)
481 {
482         errcontext("PL/Python anonymous code block");
483 }
484
485 static void
486 plpython_trigger_error_callback(void *arg)
487 {
488         if (PLy_curr_procedure)
489                 errcontext("while modifying trigger row");
490 }
491
492 static void
493 plpython_return_error_callback(void *arg)
494 {
495         if (PLy_curr_procedure)
496                 errcontext("while creating return value");
497 }
498
499 static bool
500 PLy_procedure_is_trigger(Form_pg_proc procStruct)
501 {
502         return (procStruct->prorettype == TRIGGEROID ||
503                         (procStruct->prorettype == OPAQUEOID &&
504                          procStruct->pronargs == 0));
505 }
506
507 Datum
508 plpython_validator(PG_FUNCTION_ARGS)
509 {
510         Oid                     funcoid = PG_GETARG_OID(0);
511         HeapTuple       tuple;
512         Form_pg_proc procStruct;
513         bool            is_trigger;
514
515         if (!check_function_bodies)
516         {
517                 PG_RETURN_VOID();
518         }
519
520         /* Get the new function's pg_proc entry */
521         tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
522         if (!HeapTupleIsValid(tuple))
523                 elog(ERROR, "cache lookup failed for function %u", funcoid);
524         procStruct = (Form_pg_proc) GETSTRUCT(tuple);
525
526         is_trigger = PLy_procedure_is_trigger(procStruct);
527
528         ReleaseSysCache(tuple);
529
530         PLy_procedure_get(funcoid, is_trigger);
531
532         PG_RETURN_VOID();
533 }
534
535 Datum
536 plpython_call_handler(PG_FUNCTION_ARGS)
537 {
538         Datum           retval;
539         PLyProcedure *save_curr_proc;
540         ErrorContextCallback plerrcontext;
541
542         if (SPI_connect() != SPI_OK_CONNECT)
543                 elog(ERROR, "SPI_connect failed");
544
545         save_curr_proc = PLy_curr_procedure;
546
547         /*
548          * Setup error traceback support for ereport()
549          */
550         plerrcontext.callback = plpython_error_callback;
551         plerrcontext.previous = error_context_stack;
552         error_context_stack = &plerrcontext;
553
554         PG_TRY();
555         {
556                 PLyProcedure *proc;
557
558                 if (CALLED_AS_TRIGGER(fcinfo))
559                 {
560                         HeapTuple       trv;
561
562                         proc = PLy_procedure_get(fcinfo->flinfo->fn_oid, true);
563                         PLy_curr_procedure = proc;
564                         trv = PLy_trigger_handler(fcinfo, proc);
565                         retval = PointerGetDatum(trv);
566                 }
567                 else
568                 {
569                         proc = PLy_procedure_get(fcinfo->flinfo->fn_oid, false);
570                         PLy_curr_procedure = proc;
571                         retval = PLy_function_handler(fcinfo, proc);
572                 }
573         }
574         PG_CATCH();
575         {
576                 PLy_curr_procedure = save_curr_proc;
577                 PyErr_Clear();
578                 PG_RE_THROW();
579         }
580         PG_END_TRY();
581
582         /* Pop the error context stack */
583         error_context_stack = plerrcontext.previous;
584
585         PLy_curr_procedure = save_curr_proc;
586
587         return retval;
588 }
589
590 Datum
591 plpython_inline_handler(PG_FUNCTION_ARGS)
592 {
593         InlineCodeBlock *codeblock = (InlineCodeBlock *) DatumGetPointer(PG_GETARG_DATUM(0));
594         FunctionCallInfoData fake_fcinfo;
595         FmgrInfo        flinfo;
596         PLyProcedure *save_curr_proc;
597         PLyProcedure proc;
598         ErrorContextCallback plerrcontext;
599
600         if (SPI_connect() != SPI_OK_CONNECT)
601                 elog(ERROR, "SPI_connect failed");
602
603         save_curr_proc = PLy_curr_procedure;
604
605         /*
606          * Setup error traceback support for ereport()
607          */
608         plerrcontext.callback = plpython_inline_error_callback;
609         plerrcontext.previous = error_context_stack;
610         error_context_stack = &plerrcontext;
611
612         MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
613         MemSet(&flinfo, 0, sizeof(flinfo));
614         fake_fcinfo.flinfo = &flinfo;
615         flinfo.fn_oid = InvalidOid;
616         flinfo.fn_mcxt = CurrentMemoryContext;
617
618         MemSet(&proc, 0, sizeof(PLyProcedure));
619         proc.pyname = PLy_strdup("__plpython_inline_block");
620         proc.result.out.d.typoid = VOIDOID;
621
622         PG_TRY();
623         {
624                 PLy_procedure_compile(&proc, codeblock->source_text);
625                 PLy_curr_procedure = &proc;
626                 PLy_function_handler(&fake_fcinfo, &proc);
627         }
628         PG_CATCH();
629         {
630                 PLy_procedure_delete(&proc);
631                 PLy_curr_procedure = save_curr_proc;
632                 PyErr_Clear();
633                 PG_RE_THROW();
634         }
635         PG_END_TRY();
636
637         PLy_procedure_delete(&proc);
638
639         /* Pop the error context stack */
640         error_context_stack = plerrcontext.previous;
641
642         PLy_curr_procedure = save_curr_proc;
643
644         PG_RETURN_VOID();
645 }
646
647 /* trigger and function sub handlers
648  *
649  * the python function is expected to return Py_None if the tuple is
650  * acceptable and unmodified.  Otherwise it should return a PyString
651  * object who's value is SKIP, or MODIFY.  SKIP means don't perform
652  * this action.  MODIFY means the tuple has been modified, so update
653  * tuple and perform action.  SKIP and MODIFY assume the trigger fires
654  * BEFORE the event and is ROW level.  postgres expects the function
655  * to take no arguments and return an argument of type trigger.
656  */
657 static HeapTuple
658 PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *proc)
659 {
660         HeapTuple       rv = NULL;
661         PyObject   *volatile plargs = NULL;
662         PyObject   *volatile plrv = NULL;
663         TriggerData *tdata;
664
665         Assert(CALLED_AS_TRIGGER(fcinfo));
666
667         /*
668          * Input/output conversion for trigger tuples.  Use the result TypeInfo
669          * variable to store the tuple conversion info.  We do this over again on
670          * each call to cover the possibility that the relation's tupdesc changed
671          * since the trigger was last called. PLy_input_tuple_funcs and
672          * PLy_output_tuple_funcs are responsible for not doing repetitive work.
673          */
674         tdata = (TriggerData *) fcinfo->context;
675
676         PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
677         PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
678
679         PG_TRY();
680         {
681                 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
682                 plrv = PLy_procedure_call(proc, "TD", plargs);
683
684                 Assert(plrv != NULL);
685
686                 /*
687                  * Disconnect from SPI manager
688                  */
689                 if (SPI_finish() != SPI_OK_FINISH)
690                         elog(ERROR, "SPI_finish failed");
691
692                 /*
693                  * return of None means we're happy with the tuple
694                  */
695                 if (plrv != Py_None)
696                 {
697                         char       *srv;
698
699                         if (PyString_Check(plrv))
700                                 srv = PyString_AsString(plrv);
701                         else if (PyUnicode_Check(plrv))
702                                 srv = PLyUnicode_AsString(plrv);
703                         else
704                         {
705                                 ereport(ERROR,
706                                                 (errcode(ERRCODE_DATA_EXCEPTION),
707                                         errmsg("unexpected return value from trigger procedure"),
708                                                  errdetail("Expected None or a string.")));
709                                 srv = NULL;             /* keep compiler quiet */
710                         }
711
712                         if (pg_strcasecmp(srv, "SKIP") == 0)
713                                 rv = NULL;
714                         else if (pg_strcasecmp(srv, "MODIFY") == 0)
715                         {
716                                 TriggerData *tdata = (TriggerData *) fcinfo->context;
717
718                                 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
719                                         TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
720                                         rv = PLy_modify_tuple(proc, plargs, tdata, rv);
721                                 else
722                                         ereport(WARNING,
723                                                         (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
724                         }
725                         else if (pg_strcasecmp(srv, "OK") != 0)
726                         {
727                                 /*
728                                  * accept "OK" as an alternative to None; otherwise, raise an
729                                  * error
730                                  */
731                                 ereport(ERROR,
732                                                 (errcode(ERRCODE_DATA_EXCEPTION),
733                                         errmsg("unexpected return value from trigger procedure"),
734                                                  errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
735                         }
736                 }
737         }
738         PG_CATCH();
739         {
740                 Py_XDECREF(plargs);
741                 Py_XDECREF(plrv);
742
743                 PG_RE_THROW();
744         }
745         PG_END_TRY();
746
747         Py_DECREF(plargs);
748         Py_DECREF(plrv);
749
750         return rv;
751 }
752
753 static HeapTuple
754 PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata,
755                                  HeapTuple otup)
756 {
757         PyObject   *volatile plntup;
758         PyObject   *volatile plkeys;
759         PyObject   *volatile platt;
760         PyObject   *volatile plval;
761         PyObject   *volatile plstr;
762         HeapTuple       rtup;
763         int                     natts,
764                                 i,
765                                 attn,
766                                 atti;
767         int                *volatile modattrs;
768         Datum      *volatile modvalues;
769         char       *volatile modnulls;
770         TupleDesc       tupdesc;
771         ErrorContextCallback plerrcontext;
772
773         plerrcontext.callback = plpython_trigger_error_callback;
774         plerrcontext.previous = error_context_stack;
775         error_context_stack = &plerrcontext;
776
777         plntup = plkeys = platt = plval = plstr = NULL;
778         modattrs = NULL;
779         modvalues = NULL;
780         modnulls = NULL;
781
782         PG_TRY();
783         {
784                 if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
785                         ereport(ERROR,
786                                         (errmsg("TD[\"new\"] deleted, cannot modify row")));
787                 if (!PyDict_Check(plntup))
788                         ereport(ERROR,
789                                         (errmsg("TD[\"new\"] is not a dictionary")));
790                 Py_INCREF(plntup);
791
792                 plkeys = PyDict_Keys(plntup);
793                 natts = PyList_Size(plkeys);
794
795                 modattrs = (int *) palloc(natts * sizeof(int));
796                 modvalues = (Datum *) palloc(natts * sizeof(Datum));
797                 modnulls = (char *) palloc(natts * sizeof(char));
798
799                 tupdesc = tdata->tg_relation->rd_att;
800
801                 for (i = 0; i < natts; i++)
802                 {
803                         char       *plattstr;
804
805                         platt = PyList_GetItem(plkeys, i);
806                         if (PyString_Check(platt))
807                                 plattstr = PyString_AsString(platt);
808                         else if (PyUnicode_Check(platt))
809                                 plattstr = PLyUnicode_AsString(platt);
810                         else
811                         {
812                                 ereport(ERROR,
813                                                 (errmsg("TD[\"new\"] dictionary key at ordinal position %d is not a string", i)));
814                                 plattstr = NULL;        /* keep compiler quiet */
815                         }
816                         attn = SPI_fnumber(tupdesc, plattstr);
817                         if (attn == SPI_ERROR_NOATTRIBUTE)
818                                 ereport(ERROR,
819                                                 (errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
820                                                                 plattstr)));
821                         atti = attn - 1;
822
823                         plval = PyDict_GetItem(plntup, platt);
824                         if (plval == NULL)
825                                 elog(FATAL, "Python interpreter is probably corrupted");
826
827                         Py_INCREF(plval);
828
829                         modattrs[i] = attn;
830
831                         if (tupdesc->attrs[atti]->attisdropped)
832                         {
833                                 modvalues[i] = (Datum) 0;
834                                 modnulls[i] = 'n';
835                         }
836                         else if (plval != Py_None)
837                         {
838                                 PLyObToDatum *att = &proc->result.out.r.atts[atti];
839
840                                 modvalues[i] = (att->func) (att,
841                                                                                         tupdesc->attrs[atti]->atttypmod,
842                                                                                         plval);
843                                 modnulls[i] = ' ';
844                         }
845                         else
846                         {
847                                 modvalues[i] =
848                                         InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
849                                                                           NULL,
850                                                                         proc->result.out.r.atts[atti].typioparam,
851                                                                           tupdesc->attrs[atti]->atttypmod);
852                                 modnulls[i] = 'n';
853                         }
854
855                         Py_DECREF(plval);
856                         plval = NULL;
857                 }
858
859                 rtup = SPI_modifytuple(tdata->tg_relation, otup, natts,
860                                                            modattrs, modvalues, modnulls);
861                 if (rtup == NULL)
862                         elog(ERROR, "SPI_modifytuple failed: error %d", SPI_result);
863         }
864         PG_CATCH();
865         {
866                 Py_XDECREF(plntup);
867                 Py_XDECREF(plkeys);
868                 Py_XDECREF(plval);
869                 Py_XDECREF(plstr);
870
871                 if (modnulls)
872                         pfree(modnulls);
873                 if (modvalues)
874                         pfree(modvalues);
875                 if (modattrs)
876                         pfree(modattrs);
877
878                 PG_RE_THROW();
879         }
880         PG_END_TRY();
881
882         Py_DECREF(plntup);
883         Py_DECREF(plkeys);
884
885         pfree(modattrs);
886         pfree(modvalues);
887         pfree(modnulls);
888
889         error_context_stack = plerrcontext.previous;
890
891         return rtup;
892 }
893
894 static PyObject *
895 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
896 {
897         TriggerData *tdata = (TriggerData *) fcinfo->context;
898         PyObject   *pltname,
899                            *pltevent,
900                            *pltwhen,
901                            *pltlevel,
902                            *pltrelid,
903                            *plttablename,
904                            *plttableschema;
905         PyObject   *pltargs,
906                            *pytnew,
907                            *pytold;
908         PyObject   *volatile pltdata = NULL;
909         char       *stroid;
910
911         PG_TRY();
912         {
913                 pltdata = PyDict_New();
914                 if (!pltdata)
915                         PLy_elog(ERROR, "could not create new dictionary while building trigger arguments");
916
917                 pltname = PyString_FromString(tdata->tg_trigger->tgname);
918                 PyDict_SetItemString(pltdata, "name", pltname);
919                 Py_DECREF(pltname);
920
921                 stroid = DatumGetCString(DirectFunctionCall1(oidout,
922                                                            ObjectIdGetDatum(tdata->tg_relation->rd_id)));
923                 pltrelid = PyString_FromString(stroid);
924                 PyDict_SetItemString(pltdata, "relid", pltrelid);
925                 Py_DECREF(pltrelid);
926                 pfree(stroid);
927
928                 stroid = SPI_getrelname(tdata->tg_relation);
929                 plttablename = PyString_FromString(stroid);
930                 PyDict_SetItemString(pltdata, "table_name", plttablename);
931                 Py_DECREF(plttablename);
932                 pfree(stroid);
933
934                 stroid = SPI_getnspname(tdata->tg_relation);
935                 plttableschema = PyString_FromString(stroid);
936                 PyDict_SetItemString(pltdata, "table_schema", plttableschema);
937                 Py_DECREF(plttableschema);
938                 pfree(stroid);
939
940                 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
941                         pltwhen = PyString_FromString("BEFORE");
942                 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
943                         pltwhen = PyString_FromString("AFTER");
944                 else if (TRIGGER_FIRED_INSTEAD(tdata->tg_event))
945                         pltwhen = PyString_FromString("INSTEAD OF");
946                 else
947                 {
948                         elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
949                         pltwhen = NULL;         /* keep compiler quiet */
950                 }
951                 PyDict_SetItemString(pltdata, "when", pltwhen);
952                 Py_DECREF(pltwhen);
953
954                 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
955                 {
956                         pltlevel = PyString_FromString("ROW");
957                         PyDict_SetItemString(pltdata, "level", pltlevel);
958                         Py_DECREF(pltlevel);
959
960                         if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
961                         {
962                                 pltevent = PyString_FromString("INSERT");
963
964                                 PyDict_SetItemString(pltdata, "old", Py_None);
965                                 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
966                                                                                    tdata->tg_relation->rd_att);
967                                 PyDict_SetItemString(pltdata, "new", pytnew);
968                                 Py_DECREF(pytnew);
969                                 *rv = tdata->tg_trigtuple;
970                         }
971                         else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
972                         {
973                                 pltevent = PyString_FromString("DELETE");
974
975                                 PyDict_SetItemString(pltdata, "new", Py_None);
976                                 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
977                                                                                    tdata->tg_relation->rd_att);
978                                 PyDict_SetItemString(pltdata, "old", pytold);
979                                 Py_DECREF(pytold);
980                                 *rv = tdata->tg_trigtuple;
981                         }
982                         else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
983                         {
984                                 pltevent = PyString_FromString("UPDATE");
985
986                                 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
987                                                                                    tdata->tg_relation->rd_att);
988                                 PyDict_SetItemString(pltdata, "new", pytnew);
989                                 Py_DECREF(pytnew);
990                                 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
991                                                                                    tdata->tg_relation->rd_att);
992                                 PyDict_SetItemString(pltdata, "old", pytold);
993                                 Py_DECREF(pytold);
994                                 *rv = tdata->tg_newtuple;
995                         }
996                         else
997                         {
998                                 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
999                                 pltevent = NULL;        /* keep compiler quiet */
1000                         }
1001
1002                         PyDict_SetItemString(pltdata, "event", pltevent);
1003                         Py_DECREF(pltevent);
1004                 }
1005                 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
1006                 {
1007                         pltlevel = PyString_FromString("STATEMENT");
1008                         PyDict_SetItemString(pltdata, "level", pltlevel);
1009                         Py_DECREF(pltlevel);
1010
1011                         PyDict_SetItemString(pltdata, "old", Py_None);
1012                         PyDict_SetItemString(pltdata, "new", Py_None);
1013                         *rv = NULL;
1014
1015                         if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
1016                                 pltevent = PyString_FromString("INSERT");
1017                         else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
1018                                 pltevent = PyString_FromString("DELETE");
1019                         else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
1020                                 pltevent = PyString_FromString("UPDATE");
1021                         else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
1022                                 pltevent = PyString_FromString("TRUNCATE");
1023                         else
1024                         {
1025                                 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
1026                                 pltevent = NULL;        /* keep compiler quiet */
1027                         }
1028
1029                         PyDict_SetItemString(pltdata, "event", pltevent);
1030                         Py_DECREF(pltevent);
1031                 }
1032                 else
1033                         elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
1034
1035                 if (tdata->tg_trigger->tgnargs)
1036                 {
1037                         /*
1038                          * all strings...
1039                          */
1040                         int                     i;
1041                         PyObject   *pltarg;
1042
1043                         pltargs = PyList_New(tdata->tg_trigger->tgnargs);
1044                         for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
1045                         {
1046                                 pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
1047
1048                                 /*
1049                                  * stolen, don't Py_DECREF
1050                                  */
1051                                 PyList_SetItem(pltargs, i, pltarg);
1052                         }
1053                 }
1054                 else
1055                 {
1056                         Py_INCREF(Py_None);
1057                         pltargs = Py_None;
1058                 }
1059                 PyDict_SetItemString(pltdata, "args", pltargs);
1060                 Py_DECREF(pltargs);
1061         }
1062         PG_CATCH();
1063         {
1064                 Py_XDECREF(pltdata);
1065                 PG_RE_THROW();
1066         }
1067         PG_END_TRY();
1068
1069         return pltdata;
1070 }
1071
1072
1073
1074 /* function handler and friends */
1075 static Datum
1076 PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *proc)
1077 {
1078         Datum           rv;
1079         PyObject   *volatile plargs = NULL;
1080         PyObject   *volatile plrv = NULL;
1081         ErrorContextCallback plerrcontext;
1082
1083         PG_TRY();
1084         {
1085                 if (!proc->is_setof || proc->setof == NULL)
1086                 {
1087                         /*
1088                          * Simple type returning function or first time for SETOF
1089                          * function: actually execute the function.
1090                          */
1091                         plargs = PLy_function_build_args(fcinfo, proc);
1092                         plrv = PLy_procedure_call(proc, "args", plargs);
1093                         if (!proc->is_setof)
1094                         {
1095                                 /*
1096                                  * SETOF function parameters will be deleted when last row is
1097                                  * returned
1098                                  */
1099                                 PLy_function_delete_args(proc);
1100                         }
1101                         Assert(plrv != NULL);
1102                 }
1103
1104                 /*
1105                  * If it returns a set, call the iterator to get the next return item.
1106                  * We stay in the SPI context while doing this, because PyIter_Next()
1107                  * calls back into Python code which might contain SPI calls.
1108                  */
1109                 if (proc->is_setof)
1110                 {
1111                         bool            has_error = false;
1112                         ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1113
1114                         if (proc->setof == NULL)
1115                         {
1116                                 /* first time -- do checks and setup */
1117                                 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1118                                         (rsi->allowedModes & SFRM_ValuePerCall) == 0)
1119                                 {
1120                                         ereport(ERROR,
1121                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1122                                                          errmsg("unsupported set function return mode"),
1123                                                          errdetail("PL/Python set-returning functions only support returning only value per call.")));
1124                                 }
1125                                 rsi->returnMode = SFRM_ValuePerCall;
1126
1127                                 /* Make iterator out of returned object */
1128                                 proc->setof = PyObject_GetIter(plrv);
1129                                 Py_DECREF(plrv);
1130                                 plrv = NULL;
1131
1132                                 if (proc->setof == NULL)
1133                                         ereport(ERROR,
1134                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1135                                                          errmsg("returned object cannot be iterated"),
1136                                                          errdetail("PL/Python set-returning functions must return an iterable object.")));
1137                         }
1138
1139                         /* Fetch next from iterator */
1140                         plrv = PyIter_Next(proc->setof);
1141                         if (plrv)
1142                                 rsi->isDone = ExprMultipleResult;
1143                         else
1144                         {
1145                                 rsi->isDone = ExprEndResult;
1146                                 has_error = PyErr_Occurred() != NULL;
1147                         }
1148
1149                         if (rsi->isDone == ExprEndResult)
1150                         {
1151                                 /* Iterator is exhausted or error happened */
1152                                 Py_DECREF(proc->setof);
1153                                 proc->setof = NULL;
1154
1155                                 Py_XDECREF(plargs);
1156                                 Py_XDECREF(plrv);
1157
1158                                 PLy_function_delete_args(proc);
1159
1160                                 if (has_error)
1161                                         PLy_elog(ERROR, "error fetching next item from iterator");
1162
1163                                 /* Disconnect from the SPI manager before returning */
1164                                 if (SPI_finish() != SPI_OK_FINISH)
1165                                         elog(ERROR, "SPI_finish failed");
1166
1167                                 fcinfo->isnull = true;
1168                                 return (Datum) NULL;
1169                         }
1170                 }
1171
1172                 /*
1173                  * Disconnect from SPI manager and then create the return values datum
1174                  * (if the input function does a palloc for it this must not be
1175                  * allocated in the SPI memory context because SPI_finish would free
1176                  * it).
1177                  */
1178                 if (SPI_finish() != SPI_OK_FINISH)
1179                         elog(ERROR, "SPI_finish failed");
1180
1181                 plerrcontext.callback = plpython_return_error_callback;
1182                 plerrcontext.previous = error_context_stack;
1183                 error_context_stack = &plerrcontext;
1184
1185                 /*
1186                  * If the function is declared to return void, the Python return value
1187                  * must be None. For void-returning functions, we also treat a None
1188                  * return value as a special "void datum" rather than NULL (as is the
1189                  * case for non-void-returning functions).
1190                  */
1191                 if (proc->result.out.d.typoid == VOIDOID)
1192                 {
1193                         if (plrv != Py_None)
1194                                 ereport(ERROR,
1195                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1196                                                  errmsg("PL/Python function with return type \"void\" did not return None")));
1197
1198                         fcinfo->isnull = false;
1199                         rv = (Datum) 0;
1200                 }
1201                 else if (plrv == Py_None)
1202                 {
1203                         fcinfo->isnull = true;
1204                         if (proc->result.is_rowtype < 1)
1205                                 rv = InputFunctionCall(&proc->result.out.d.typfunc,
1206                                                                            NULL,
1207                                                                            proc->result.out.d.typioparam,
1208                                                                            -1);
1209                         else
1210                                 /* Tuple as None */
1211                                 rv = (Datum) NULL;
1212                 }
1213                 else if (proc->result.is_rowtype >= 1)
1214                 {
1215                         TupleDesc       desc;
1216                         HeapTuple       tuple = NULL;
1217
1218                         /* make sure it's not an unnamed record */
1219                         Assert((proc->result.out.d.typoid == RECORDOID &&
1220                                         proc->result.out.d.typmod != -1) ||
1221                                    (proc->result.out.d.typoid != RECORDOID &&
1222                                         proc->result.out.d.typmod == -1));
1223
1224                         desc = lookup_rowtype_tupdesc(proc->result.out.d.typoid,
1225                                                                                   proc->result.out.d.typmod);
1226
1227                         tuple = PLyObject_ToTuple(&proc->result, desc, plrv);
1228
1229                         if (tuple != NULL)
1230                         {
1231                                 fcinfo->isnull = false;
1232                                 rv = HeapTupleGetDatum(tuple);
1233                         }
1234                         else
1235                         {
1236                                 fcinfo->isnull = true;
1237                                 rv = (Datum) NULL;
1238                         }
1239                 }
1240                 else
1241                 {
1242                         fcinfo->isnull = false;
1243                         rv = (proc->result.out.d.func) (&proc->result.out.d, -1, plrv);
1244                 }
1245         }
1246         PG_CATCH();
1247         {
1248                 Py_XDECREF(plargs);
1249                 Py_XDECREF(plrv);
1250
1251                 /*
1252                  * If there was an error the iterator might have not been exhausted
1253                  * yet. Set it to NULL so the next invocation of the function will
1254                  * start the iteration again.
1255                  */
1256                 Py_XDECREF(proc->setof);
1257                 proc->setof = NULL;
1258
1259                 PG_RE_THROW();
1260         }
1261         PG_END_TRY();
1262
1263         error_context_stack = plerrcontext.previous;
1264
1265         Py_XDECREF(plargs);
1266         Py_DECREF(plrv);
1267
1268         return rv;
1269 }
1270
1271 /*
1272  * Abort lingering subtransactions that have been explicitly started
1273  * by plpy.subtransaction().start() and not properly closed.
1274  */
1275 static void
1276 PLy_abort_open_subtransactions(int save_subxact_level)
1277 {
1278         Assert(save_subxact_level >= 0);
1279
1280         while (list_length(explicit_subtransactions) > save_subxact_level)
1281         {
1282                 PLySubtransactionData *subtransactiondata;
1283
1284                 Assert(explicit_subtransactions != NIL);
1285
1286                 ereport(WARNING,
1287                                 (errmsg("forcibly aborting a subtransaction that has not been exited")));
1288
1289                 RollbackAndReleaseCurrentSubTransaction();
1290
1291                 SPI_restore_connection();
1292
1293                 subtransactiondata = (PLySubtransactionData *) linitial(explicit_subtransactions);
1294                 explicit_subtransactions = list_delete_first(explicit_subtransactions);
1295
1296                 MemoryContextSwitchTo(subtransactiondata->oldcontext);
1297                 CurrentResourceOwner = subtransactiondata->oldowner;
1298                 PLy_free(subtransactiondata);
1299         }
1300 }
1301
1302 static PyObject *
1303 PLy_procedure_call(PLyProcedure *proc, char *kargs, PyObject *vargs)
1304 {
1305         PyObject   *rv;
1306         int volatile save_subxact_level = list_length(explicit_subtransactions);
1307
1308         PyDict_SetItemString(proc->globals, kargs, vargs);
1309
1310         PG_TRY();
1311         {
1312 #if PY_VERSION_HEX >= 0x03020000
1313                 rv = PyEval_EvalCode(proc->code,
1314                                                          proc->globals, proc->globals);
1315 #else
1316                 rv = PyEval_EvalCode((PyCodeObject *) proc->code,
1317                                                          proc->globals, proc->globals);
1318 #endif
1319
1320                 /*
1321                  * Since plpy will only let you close subtransactions that you
1322                  * started, you cannot *unnest* subtransactions, only *nest* them
1323                  * without closing.
1324                  */
1325                 Assert(list_length(explicit_subtransactions) >= save_subxact_level);
1326         }
1327         PG_CATCH();
1328         {
1329                 PLy_abort_open_subtransactions(save_subxact_level);
1330                 PG_RE_THROW();
1331         }
1332         PG_END_TRY();
1333
1334         PLy_abort_open_subtransactions(save_subxact_level);
1335
1336         /* If the Python code returned an error, propagate it */
1337         if (rv == NULL)
1338                 PLy_elog(ERROR, NULL);
1339
1340         return rv;
1341 }
1342
1343 static PyObject *
1344 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
1345 {
1346         PyObject   *volatile arg = NULL;
1347         PyObject   *volatile args = NULL;
1348         int                     i;
1349
1350         PG_TRY();
1351         {
1352                 args = PyList_New(proc->nargs);
1353                 for (i = 0; i < proc->nargs; i++)
1354                 {
1355                         if (proc->args[i].is_rowtype > 0)
1356                         {
1357                                 if (fcinfo->argnull[i])
1358                                         arg = NULL;
1359                                 else
1360                                 {
1361                                         HeapTupleHeader td;
1362                                         Oid                     tupType;
1363                                         int32           tupTypmod;
1364                                         TupleDesc       tupdesc;
1365                                         HeapTupleData tmptup;
1366
1367                                         td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
1368                                         /* Extract rowtype info and find a tupdesc */
1369                                         tupType = HeapTupleHeaderGetTypeId(td);
1370                                         tupTypmod = HeapTupleHeaderGetTypMod(td);
1371                                         tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
1372
1373                                         /* Set up I/O funcs if not done yet */
1374                                         if (proc->args[i].is_rowtype != 1)
1375                                                 PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
1376
1377                                         /* Build a temporary HeapTuple control structure */
1378                                         tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
1379                                         tmptup.t_data = td;
1380
1381                                         arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
1382                                         ReleaseTupleDesc(tupdesc);
1383                                 }
1384                         }
1385                         else
1386                         {
1387                                 if (fcinfo->argnull[i])
1388                                         arg = NULL;
1389                                 else
1390                                 {
1391                                         arg = (proc->args[i].in.d.func) (&(proc->args[i].in.d),
1392                                                                                                          fcinfo->arg[i]);
1393                                 }
1394                         }
1395
1396                         if (arg == NULL)
1397                         {
1398                                 Py_INCREF(Py_None);
1399                                 arg = Py_None;
1400                         }
1401
1402                         if (PyList_SetItem(args, i, arg) == -1)
1403                                 PLy_elog(ERROR, "PyList_SetItem() failed, while setting up arguments");
1404
1405                         if (proc->argnames && proc->argnames[i] &&
1406                         PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
1407                                 PLy_elog(ERROR, "PyDict_SetItemString() failed, while setting up arguments");
1408                         arg = NULL;
1409                 }
1410
1411                 /* Set up output conversion for functions returning RECORD */
1412                 if (proc->result.out.d.typoid == RECORDOID)
1413                 {
1414                         TupleDesc       desc;
1415
1416                         if (get_call_result_type(fcinfo, NULL, &desc) != TYPEFUNC_COMPOSITE)
1417                                 ereport(ERROR,
1418                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1419                                                  errmsg("function returning record called in context "
1420                                                                 "that cannot accept type record")));
1421
1422                         /* cache the output conversion functions */
1423                         PLy_output_record_funcs(&(proc->result), desc);
1424                 }
1425         }
1426         PG_CATCH();
1427         {
1428                 Py_XDECREF(arg);
1429                 Py_XDECREF(args);
1430
1431                 PG_RE_THROW();
1432         }
1433         PG_END_TRY();
1434
1435         return args;
1436 }
1437
1438
1439 static void
1440 PLy_function_delete_args(PLyProcedure *proc)
1441 {
1442         int                     i;
1443
1444         if (!proc->argnames)
1445                 return;
1446
1447         for (i = 0; i < proc->nargs; i++)
1448                 if (proc->argnames[i])
1449                         PyDict_DelItemString(proc->globals, proc->argnames[i]);
1450 }
1451
1452 /*
1453  * Decide whether a cached PLyProcedure struct is still valid
1454  */
1455 static bool
1456 PLy_procedure_valid(PLyProcedure *proc, HeapTuple procTup)
1457 {
1458         int                     i;
1459         bool            valid;
1460
1461         Assert(proc != NULL);
1462
1463         /* If the pg_proc tuple has changed, it's not valid */
1464         if (!(proc->fn_xmin == HeapTupleHeaderGetXmin(procTup->t_data) &&
1465                   ItemPointerEquals(&proc->fn_tid, &procTup->t_self)))
1466                 return false;
1467
1468         valid = true;
1469         /* If there are composite input arguments, they might have changed */
1470         for (i = 0; i < proc->nargs; i++)
1471         {
1472                 Oid                     relid;
1473                 HeapTuple       relTup;
1474
1475                 /* Short-circuit on first changed argument */
1476                 if (!valid)
1477                         break;
1478
1479                 /* Only check input arguments that are composite */
1480                 if (proc->args[i].is_rowtype != 1)
1481                         continue;
1482
1483                 Assert(OidIsValid(proc->args[i].typ_relid));
1484                 Assert(TransactionIdIsValid(proc->args[i].typrel_xmin));
1485                 Assert(ItemPointerIsValid(&proc->args[i].typrel_tid));
1486
1487                 /* Get the pg_class tuple for the argument type */
1488                 relid = proc->args[i].typ_relid;
1489                 relTup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
1490                 if (!HeapTupleIsValid(relTup))
1491                         elog(ERROR, "cache lookup failed for relation %u", relid);
1492
1493                 /* If it has changed, the function is not valid */
1494                 if (!(proc->args[i].typrel_xmin == HeapTupleHeaderGetXmin(relTup->t_data) &&
1495                           ItemPointerEquals(&proc->args[i].typrel_tid, &relTup->t_self)))
1496                         valid = false;
1497
1498                 ReleaseSysCache(relTup);
1499         }
1500
1501         return valid;
1502 }
1503
1504
1505 /*
1506  * PLyProcedure functions
1507  */
1508
1509 /* PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
1510  * returns a new PLyProcedure.  fcinfo is the call info, tgreloid is the
1511  * relation OID when calling a trigger, or InvalidOid (zero) for ordinary
1512  * function calls.
1513  */
1514 static PLyProcedure *
1515 PLy_procedure_get(Oid fn_oid, bool is_trigger)
1516 {
1517         HeapTuple       procTup;
1518         PLyProcedureEntry *volatile entry;
1519         bool            found;
1520
1521         procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
1522         if (!HeapTupleIsValid(procTup))
1523                 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1524
1525         /* Look for the function in the corresponding cache */
1526         if (is_trigger)
1527                 entry = hash_search(PLy_trigger_cache,
1528                                                         &fn_oid, HASH_ENTER, &found);
1529         else
1530                 entry = hash_search(PLy_procedure_cache,
1531                                                         &fn_oid, HASH_ENTER, &found);
1532
1533         PG_TRY();
1534         {
1535                 if (!found)
1536                 {
1537                         /* Haven't found it, create a new cache entry */
1538                         entry->proc = PLy_procedure_create(procTup, fn_oid, is_trigger);
1539                 }
1540                 else if (!PLy_procedure_valid(entry->proc, procTup))
1541                 {
1542                         /* Found it, but it's invalid, free and reuse the cache entry */
1543                         PLy_procedure_delete(entry->proc);
1544                         PLy_free(entry->proc);
1545                         entry->proc = PLy_procedure_create(procTup, fn_oid, is_trigger);
1546                 }
1547                 /* Found it and it's valid, it's fine to use it */
1548         }
1549         PG_CATCH();
1550         {
1551                 /* Do not leave an uninitialised entry in the cache */
1552                 if (is_trigger)
1553                         hash_search(PLy_trigger_cache,
1554                                                 &fn_oid, HASH_REMOVE, NULL);
1555                 else
1556                         hash_search(PLy_procedure_cache,
1557                                                 &fn_oid, HASH_REMOVE, NULL);
1558                 PG_RE_THROW();
1559         }
1560         PG_END_TRY();
1561
1562         ReleaseSysCache(procTup);
1563
1564         return entry->proc;
1565 }
1566
1567 /*
1568  * Create a new PLyProcedure structure
1569  */
1570 static PLyProcedure *
1571 PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
1572 {
1573         char            procName[NAMEDATALEN + 256];
1574         Form_pg_proc procStruct;
1575         PLyProcedure *volatile proc;
1576         char       *volatile procSource = NULL;
1577         Datum           prosrcdatum;
1578         bool            isnull;
1579         int                     i,
1580                                 rv;
1581
1582         procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1583         rv = snprintf(procName, sizeof(procName),
1584                                   "__plpython_procedure_%s_%u",
1585                                   NameStr(procStruct->proname),
1586                                   fn_oid);
1587         if (rv >= sizeof(procName) || rv < 0)
1588                 elog(ERROR, "procedure name would overrun buffer");
1589
1590         proc = PLy_malloc(sizeof(PLyProcedure));
1591         proc->proname = PLy_strdup(NameStr(procStruct->proname));
1592         proc->pyname = PLy_strdup(procName);
1593         proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1594         proc->fn_tid = procTup->t_self;
1595         /* Remember if function is STABLE/IMMUTABLE */
1596         proc->fn_readonly =
1597                 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1598         PLy_typeinfo_init(&proc->result);
1599         for (i = 0; i < FUNC_MAX_ARGS; i++)
1600                 PLy_typeinfo_init(&proc->args[i]);
1601         proc->nargs = 0;
1602         proc->code = proc->statics = NULL;
1603         proc->globals = NULL;
1604         proc->is_setof = procStruct->proretset;
1605         proc->setof = NULL;
1606         proc->src = NULL;
1607         proc->argnames = NULL;
1608
1609         PG_TRY();
1610         {
1611                 /*
1612                  * get information required for output conversion of the return value,
1613                  * but only if this isn't a trigger.
1614                  */
1615                 if (!is_trigger)
1616                 {
1617                         HeapTuple       rvTypeTup;
1618                         Form_pg_type rvTypeStruct;
1619
1620                         rvTypeTup = SearchSysCache1(TYPEOID,
1621                                                                    ObjectIdGetDatum(procStruct->prorettype));
1622                         if (!HeapTupleIsValid(rvTypeTup))
1623                                 elog(ERROR, "cache lookup failed for type %u",
1624                                          procStruct->prorettype);
1625                         rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
1626
1627                         /* Disallow pseudotype result, except for void or record */
1628                         if (rvTypeStruct->typtype == TYPTYPE_PSEUDO)
1629                         {
1630                                 if (procStruct->prorettype == TRIGGEROID)
1631                                         ereport(ERROR,
1632                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1633                                                          errmsg("trigger functions can only be called as triggers")));
1634                                 else if (procStruct->prorettype != VOIDOID &&
1635                                                  procStruct->prorettype != RECORDOID)
1636                                         ereport(ERROR,
1637                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1638                                                   errmsg("PL/Python functions cannot return type %s",
1639                                                                  format_type_be(procStruct->prorettype))));
1640                         }
1641
1642                         if (rvTypeStruct->typtype == TYPTYPE_COMPOSITE ||
1643                                 procStruct->prorettype == RECORDOID)
1644                         {
1645                                 /*
1646                                  * Tuple: set up later, during first call to
1647                                  * PLy_function_handler
1648                                  */
1649                                 proc->result.out.d.typoid = procStruct->prorettype;
1650                                 proc->result.out.d.typmod = -1;
1651                                 proc->result.is_rowtype = 2;
1652                         }
1653                         else
1654                         {
1655                                 /* do the real work */
1656                                 PLy_output_datum_func(&proc->result, rvTypeTup);
1657                         }
1658
1659                         ReleaseSysCache(rvTypeTup);
1660                 }
1661
1662                 /*
1663                  * Now get information required for input conversion of the
1664                  * procedure's arguments.  Note that we ignore output arguments here
1665                  * --- since we don't support returning record, and that was already
1666                  * checked above, there's no need to worry about multiple output
1667                  * arguments.
1668                  */
1669                 if (procStruct->pronargs)
1670                 {
1671                         Oid                *types;
1672                         char      **names,
1673                                            *modes;
1674                         int                     i,
1675                                                 pos,
1676                                                 total;
1677
1678                         /* extract argument type info from the pg_proc tuple */
1679                         total = get_func_arg_info(procTup, &types, &names, &modes);
1680
1681                         /* count number of in+inout args into proc->nargs */
1682                         if (modes == NULL)
1683                                 proc->nargs = total;
1684                         else
1685                         {
1686                                 /* proc->nargs was initialized to 0 above */
1687                                 for (i = 0; i < total; i++)
1688                                 {
1689                                         if (modes[i] != PROARGMODE_OUT &&
1690                                                 modes[i] != PROARGMODE_TABLE)
1691                                                 (proc->nargs)++;
1692                                 }
1693                         }
1694
1695                         proc->argnames = (char **) PLy_malloc0(sizeof(char *) * proc->nargs);
1696                         for (i = pos = 0; i < total; i++)
1697                         {
1698                                 HeapTuple       argTypeTup;
1699                                 Form_pg_type argTypeStruct;
1700
1701                                 if (modes &&
1702                                         (modes[i] == PROARGMODE_OUT ||
1703                                          modes[i] == PROARGMODE_TABLE))
1704                                         continue;       /* skip OUT arguments */
1705
1706                                 Assert(types[i] == procStruct->proargtypes.values[pos]);
1707
1708                                 argTypeTup = SearchSysCache1(TYPEOID,
1709                                                                                          ObjectIdGetDatum(types[i]));
1710                                 if (!HeapTupleIsValid(argTypeTup))
1711                                         elog(ERROR, "cache lookup failed for type %u", types[i]);
1712                                 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
1713
1714                                 /* check argument type is OK, set up I/O function info */
1715                                 switch (argTypeStruct->typtype)
1716                                 {
1717                                         case TYPTYPE_PSEUDO:
1718                                                 /* Disallow pseudotype argument */
1719                                                 ereport(ERROR,
1720                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1721                                                   errmsg("PL/Python functions cannot accept type %s",
1722                                                                  format_type_be(types[i]))));
1723                                                 break;
1724                                         case TYPTYPE_COMPOSITE:
1725                                                 /* we'll set IO funcs at first call */
1726                                                 proc->args[pos].is_rowtype = 2;
1727                                                 break;
1728                                         default:
1729                                                 PLy_input_datum_func(&(proc->args[pos]),
1730                                                                                          types[i],
1731                                                                                          argTypeTup);
1732                                                 break;
1733                                 }
1734
1735                                 /* get argument name */
1736                                 proc->argnames[pos] = names ? PLy_strdup(names[i]) : NULL;
1737
1738                                 ReleaseSysCache(argTypeTup);
1739
1740                                 pos++;
1741                         }
1742                 }
1743
1744                 /*
1745                  * get the text of the function.
1746                  */
1747                 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1748                                                                           Anum_pg_proc_prosrc, &isnull);
1749                 if (isnull)
1750                         elog(ERROR, "null prosrc");
1751                 procSource = TextDatumGetCString(prosrcdatum);
1752
1753                 PLy_procedure_compile(proc, procSource);
1754
1755                 pfree(procSource);
1756                 procSource = NULL;
1757         }
1758         PG_CATCH();
1759         {
1760                 PLy_procedure_delete(proc);
1761                 if (procSource)
1762                         pfree(procSource);
1763
1764                 PG_RE_THROW();
1765         }
1766         PG_END_TRY();
1767
1768         return proc;
1769 }
1770
1771 /*
1772  * Insert the procedure into the Python interpreter
1773  */
1774 static void
1775 PLy_procedure_compile(PLyProcedure *proc, const char *src)
1776 {
1777         PyObject   *crv = NULL;
1778         char       *msrc;
1779
1780         proc->globals = PyDict_Copy(PLy_interp_globals);
1781
1782         /*
1783          * SD is private preserved data between calls. GD is global data shared by
1784          * all functions
1785          */
1786         proc->statics = PyDict_New();
1787         PyDict_SetItemString(proc->globals, "SD", proc->statics);
1788
1789         /*
1790          * insert the function code into the interpreter
1791          */
1792         msrc = PLy_procedure_munge_source(proc->pyname, src);
1793         /* Save the mangled source for later inclusion in tracebacks */
1794         proc->src = PLy_strdup(msrc);
1795         crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1796         pfree(msrc);
1797
1798         if (crv != NULL)
1799         {
1800                 int                     clen;
1801                 char            call[NAMEDATALEN + 256];
1802
1803                 Py_DECREF(crv);
1804
1805                 /*
1806                  * compile a call to the function
1807                  */
1808                 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1809                 if (clen < 0 || clen >= sizeof(call))
1810                         elog(ERROR, "string would overflow buffer");
1811                 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1812                 if (proc->code != NULL)
1813                         return;
1814         }
1815
1816         if (proc->proname)
1817                 PLy_elog(ERROR, "could not compile PL/Python function \"%s\"",
1818                                  proc->proname);
1819         else
1820                 PLy_elog(ERROR, "could not compile anonymous PL/Python code block");
1821 }
1822
1823 static char *
1824 PLy_procedure_munge_source(const char *name, const char *src)
1825 {
1826         char       *mrc,
1827                            *mp;
1828         const char *sp;
1829         size_t          mlen,
1830                                 plen;
1831
1832         /*
1833          * room for function source and the def statement
1834          */
1835         mlen = (strlen(src) * 2) + strlen(name) + 16;
1836
1837         mrc = palloc(mlen);
1838         plen = snprintf(mrc, mlen, "def %s():\n\t", name);
1839         Assert(plen >= 0 && plen < mlen);
1840
1841         sp = src;
1842         mp = mrc + plen;
1843
1844         while (*sp != '\0')
1845         {
1846                 if (*sp == '\r' && *(sp + 1) == '\n')
1847                         sp++;
1848
1849                 if (*sp == '\n' || *sp == '\r')
1850                 {
1851                         *mp++ = '\n';
1852                         *mp++ = '\t';
1853                         sp++;
1854                 }
1855                 else
1856                         *mp++ = *sp++;
1857         }
1858         *mp++ = '\n';
1859         *mp++ = '\n';
1860         *mp = '\0';
1861
1862         if (mp > (mrc + mlen))
1863                 elog(FATAL, "buffer overrun in PLy_munge_source");
1864
1865         return mrc;
1866 }
1867
1868 static void
1869 PLy_procedure_delete(PLyProcedure *proc)
1870 {
1871         int                     i;
1872
1873         Py_XDECREF(proc->code);
1874         Py_XDECREF(proc->statics);
1875         Py_XDECREF(proc->globals);
1876         if (proc->proname)
1877                 PLy_free(proc->proname);
1878         if (proc->pyname)
1879                 PLy_free(proc->pyname);
1880         for (i = 0; i < proc->nargs; i++)
1881         {
1882                 if (proc->args[i].is_rowtype == 1)
1883                 {
1884                         if (proc->args[i].in.r.atts)
1885                                 PLy_free(proc->args[i].in.r.atts);
1886                         if (proc->args[i].out.r.atts)
1887                                 PLy_free(proc->args[i].out.r.atts);
1888                 }
1889                 if (proc->argnames && proc->argnames[i])
1890                         PLy_free(proc->argnames[i]);
1891         }
1892         if (proc->src)
1893                 PLy_free(proc->src);
1894         if (proc->argnames)
1895                 PLy_free(proc->argnames);
1896 }
1897
1898 /*
1899  * Conversion functions.  Remember output from Python is input to
1900  * PostgreSQL, and vice versa.
1901  */
1902 static void
1903 PLy_input_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc)
1904 {
1905         int                     i;
1906
1907         if (arg->is_rowtype == 0)
1908                 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1909         arg->is_rowtype = 1;
1910
1911         if (arg->in.r.natts != desc->natts)
1912         {
1913                 if (arg->in.r.atts)
1914                         PLy_free(arg->in.r.atts);
1915                 arg->in.r.natts = desc->natts;
1916                 arg->in.r.atts = PLy_malloc0(desc->natts * sizeof(PLyDatumToOb));
1917         }
1918
1919         /* Can this be an unnamed tuple? If not, then an Assert would be enough */
1920         if (desc->tdtypmod != -1)
1921                 elog(ERROR, "received unnamed record type as input");
1922
1923         Assert(OidIsValid(desc->tdtypeid));
1924
1925         /*
1926          * RECORDOID means we got called to create input functions for a tuple
1927          * fetched by plpy.execute or for an anonymous record type
1928          */
1929         if (desc->tdtypeid != RECORDOID && !TransactionIdIsValid(arg->typrel_xmin))
1930         {
1931                 HeapTuple       relTup;
1932
1933                 /* Get the pg_class tuple corresponding to the type of the input */
1934                 arg->typ_relid = typeidTypeRelid(desc->tdtypeid);
1935                 relTup = SearchSysCache1(RELOID, ObjectIdGetDatum(arg->typ_relid));
1936                 if (!HeapTupleIsValid(relTup))
1937                         elog(ERROR, "cache lookup failed for relation %u", arg->typ_relid);
1938
1939                 /* Extract the XMIN value to later use it in PLy_procedure_valid */
1940                 arg->typrel_xmin = HeapTupleHeaderGetXmin(relTup->t_data);
1941                 arg->typrel_tid = relTup->t_self;
1942
1943                 ReleaseSysCache(relTup);
1944         }
1945
1946         for (i = 0; i < desc->natts; i++)
1947         {
1948                 HeapTuple       typeTup;
1949
1950                 if (desc->attrs[i]->attisdropped)
1951                         continue;
1952
1953                 if (arg->in.r.atts[i].typoid == desc->attrs[i]->atttypid)
1954                         continue;                       /* already set up this entry */
1955
1956                 typeTup = SearchSysCache1(TYPEOID,
1957                                                                   ObjectIdGetDatum(desc->attrs[i]->atttypid));
1958                 if (!HeapTupleIsValid(typeTup))
1959                         elog(ERROR, "cache lookup failed for type %u",
1960                                  desc->attrs[i]->atttypid);
1961
1962                 PLy_input_datum_func2(&(arg->in.r.atts[i]),
1963                                                           desc->attrs[i]->atttypid,
1964                                                           typeTup);
1965
1966                 ReleaseSysCache(typeTup);
1967         }
1968 }
1969
1970 static void
1971 PLy_output_record_funcs(PLyTypeInfo *arg, TupleDesc desc)
1972 {
1973         /*
1974          * If the output record functions are already set, we just have to check
1975          * if the record descriptor has not changed
1976          */
1977         if ((arg->is_rowtype == 1) &&
1978                 (arg->out.d.typmod != -1) &&
1979                 (arg->out.d.typmod == desc->tdtypmod))
1980                 return;
1981
1982         /* bless the record to make it known to the typcache lookup code */
1983         BlessTupleDesc(desc);
1984         /* save the freshly generated typmod */
1985         arg->out.d.typmod = desc->tdtypmod;
1986         /* proceed with normal I/O function caching */
1987         PLy_output_tuple_funcs(arg, desc);
1988
1989         /*
1990          * it should change is_rowtype to 1, so we won't go through this again
1991          * unless the the output record description changes
1992          */
1993         Assert(arg->is_rowtype == 1);
1994 }
1995
1996 static void
1997 PLy_output_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc)
1998 {
1999         int                     i;
2000
2001         if (arg->is_rowtype == 0)
2002                 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
2003         arg->is_rowtype = 1;
2004
2005         if (arg->out.r.natts != desc->natts)
2006         {
2007                 if (arg->out.r.atts)
2008                         PLy_free(arg->out.r.atts);
2009                 arg->out.r.natts = desc->natts;
2010                 arg->out.r.atts = PLy_malloc0(desc->natts * sizeof(PLyDatumToOb));
2011         }
2012
2013         for (i = 0; i < desc->natts; i++)
2014         {
2015                 HeapTuple       typeTup;
2016
2017                 if (desc->attrs[i]->attisdropped)
2018                         continue;
2019
2020                 if (arg->out.r.atts[i].typoid == desc->attrs[i]->atttypid)
2021                         continue;                       /* already set up this entry */
2022
2023                 typeTup = SearchSysCache1(TYPEOID,
2024                                                                   ObjectIdGetDatum(desc->attrs[i]->atttypid));
2025                 if (!HeapTupleIsValid(typeTup))
2026                         elog(ERROR, "cache lookup failed for type %u",
2027                                  desc->attrs[i]->atttypid);
2028
2029                 PLy_output_datum_func2(&(arg->out.r.atts[i]), typeTup);
2030
2031                 ReleaseSysCache(typeTup);
2032         }
2033 }
2034
2035 static void
2036 PLy_output_datum_func(PLyTypeInfo *arg, HeapTuple typeTup)
2037 {
2038         if (arg->is_rowtype > 0)
2039                 elog(ERROR, "PLyTypeInfo struct is initialized for a Tuple");
2040         arg->is_rowtype = 0;
2041         PLy_output_datum_func2(&(arg->out.d), typeTup);
2042 }
2043
2044 static void
2045 PLy_output_datum_func2(PLyObToDatum *arg, HeapTuple typeTup)
2046 {
2047         Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2048         Oid                     element_type;
2049
2050         perm_fmgr_info(typeStruct->typinput, &arg->typfunc);
2051         arg->typoid = HeapTupleGetOid(typeTup);
2052         arg->typmod = -1;
2053         arg->typioparam = getTypeIOParam(typeTup);
2054         arg->typbyval = typeStruct->typbyval;
2055
2056         element_type = get_element_type(arg->typoid);
2057
2058         /*
2059          * Select a conversion function to convert Python objects to PostgreSQL
2060          * datums.      Most data types can go through the generic function.
2061          */
2062         switch (getBaseType(element_type ? element_type : arg->typoid))
2063         {
2064                 case BOOLOID:
2065                         arg->func = PLyObject_ToBool;
2066                         break;
2067                 case BYTEAOID:
2068                         arg->func = PLyObject_ToBytea;
2069                         break;
2070                 default:
2071                         arg->func = PLyObject_ToDatum;
2072                         break;
2073         }
2074
2075         /* Composite types need their own input routine, though */
2076         if (typeStruct->typtype == TYPTYPE_COMPOSITE)
2077         {
2078                 arg->func = PLyObject_ToComposite;
2079         }
2080
2081         if (element_type)
2082         {
2083                 char            dummy_delim;
2084                 Oid                     funcid;
2085
2086                 if (type_is_rowtype(element_type))
2087                         ereport(ERROR,
2088                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2089                                          errmsg("PL/Python functions cannot return type %s",
2090                                                         format_type_be(arg->typoid)),
2091                                          errdetail("PL/Python does not support conversion to arrays of row types.")));
2092
2093                 arg->elm = PLy_malloc0(sizeof(*arg->elm));
2094                 arg->elm->func = arg->func;
2095                 arg->func = PLySequence_ToArray;
2096
2097                 arg->elm->typoid = element_type;
2098                 arg->elm->typmod = -1;
2099                 get_type_io_data(element_type, IOFunc_input,
2100                                                  &arg->elm->typlen, &arg->elm->typbyval, &arg->elm->typalign, &dummy_delim,
2101                                                  &arg->elm->typioparam, &funcid);
2102                 perm_fmgr_info(funcid, &arg->elm->typfunc);
2103         }
2104 }
2105
2106 static void
2107 PLy_input_datum_func(PLyTypeInfo *arg, Oid typeOid, HeapTuple typeTup)
2108 {
2109         if (arg->is_rowtype > 0)
2110                 elog(ERROR, "PLyTypeInfo struct is initialized for Tuple");
2111         arg->is_rowtype = 0;
2112         PLy_input_datum_func2(&(arg->in.d), typeOid, typeTup);
2113 }
2114
2115 static void
2116 PLy_input_datum_func2(PLyDatumToOb *arg, Oid typeOid, HeapTuple typeTup)
2117 {
2118         Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2119         Oid                     element_type = get_element_type(typeOid);
2120
2121         /* Get the type's conversion information */
2122         perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
2123         arg->typoid = HeapTupleGetOid(typeTup);
2124         arg->typmod = -1;
2125         arg->typioparam = getTypeIOParam(typeTup);
2126         arg->typbyval = typeStruct->typbyval;
2127         arg->typlen = typeStruct->typlen;
2128         arg->typalign = typeStruct->typalign;
2129
2130         /* Determine which kind of Python object we will convert to */
2131         switch (getBaseType(element_type ? element_type : typeOid))
2132         {
2133                 case BOOLOID:
2134                         arg->func = PLyBool_FromBool;
2135                         break;
2136                 case FLOAT4OID:
2137                         arg->func = PLyFloat_FromFloat4;
2138                         break;
2139                 case FLOAT8OID:
2140                         arg->func = PLyFloat_FromFloat8;
2141                         break;
2142                 case NUMERICOID:
2143                         arg->func = PLyFloat_FromNumeric;
2144                         break;
2145                 case INT2OID:
2146                         arg->func = PLyInt_FromInt16;
2147                         break;
2148                 case INT4OID:
2149                         arg->func = PLyInt_FromInt32;
2150                         break;
2151                 case INT8OID:
2152                         arg->func = PLyLong_FromInt64;
2153                         break;
2154                 case BYTEAOID:
2155                         arg->func = PLyBytes_FromBytea;
2156                         break;
2157                 default:
2158                         arg->func = PLyString_FromDatum;
2159                         break;
2160         }
2161
2162         if (element_type)
2163         {
2164                 char            dummy_delim;
2165                 Oid                     funcid;
2166
2167                 arg->elm = PLy_malloc0(sizeof(*arg->elm));
2168                 arg->elm->func = arg->func;
2169                 arg->func = PLyList_FromArray;
2170                 arg->elm->typoid = element_type;
2171                 arg->elm->typmod = -1;
2172                 get_type_io_data(element_type, IOFunc_output,
2173                                                  &arg->elm->typlen, &arg->elm->typbyval, &arg->elm->typalign, &dummy_delim,
2174                                                  &arg->elm->typioparam, &funcid);
2175                 perm_fmgr_info(funcid, &arg->elm->typfunc);
2176         }
2177 }
2178
2179 static void
2180 PLy_typeinfo_init(PLyTypeInfo *arg)
2181 {
2182         arg->is_rowtype = -1;
2183         arg->in.r.natts = arg->out.r.natts = 0;
2184         arg->in.r.atts = NULL;
2185         arg->out.r.atts = NULL;
2186         arg->typ_relid = InvalidOid;
2187         arg->typrel_xmin = InvalidTransactionId;
2188         ItemPointerSetInvalid(&arg->typrel_tid);
2189 }
2190
2191 static void
2192 PLy_typeinfo_dealloc(PLyTypeInfo *arg)
2193 {
2194         if (arg->is_rowtype == 1)
2195         {
2196                 if (arg->in.r.atts)
2197                         PLy_free(arg->in.r.atts);
2198                 if (arg->out.r.atts)
2199                         PLy_free(arg->out.r.atts);
2200         }
2201 }
2202
2203 static PyObject *
2204 PLyBool_FromBool(PLyDatumToOb *arg, Datum d)
2205 {
2206         /*
2207          * We would like to use Py_RETURN_TRUE and Py_RETURN_FALSE here for
2208          * generating SQL from trigger functions, but those are only supported in
2209          * Python >= 2.3, and we support older versions.
2210          * http://docs.python.org/api/boolObjects.html
2211          */
2212         if (DatumGetBool(d))
2213                 return PyBool_FromLong(1);
2214         return PyBool_FromLong(0);
2215 }
2216
2217 static PyObject *
2218 PLyFloat_FromFloat4(PLyDatumToOb *arg, Datum d)
2219 {
2220         return PyFloat_FromDouble(DatumGetFloat4(d));
2221 }
2222
2223 static PyObject *
2224 PLyFloat_FromFloat8(PLyDatumToOb *arg, Datum d)
2225 {
2226         return PyFloat_FromDouble(DatumGetFloat8(d));
2227 }
2228
2229 static PyObject *
2230 PLyFloat_FromNumeric(PLyDatumToOb *arg, Datum d)
2231 {
2232         /*
2233          * Numeric is cast to a PyFloat: This results in a loss of precision Would
2234          * it be better to cast to PyString?
2235          */
2236         Datum           f = DirectFunctionCall1(numeric_float8, d);
2237         double          x = DatumGetFloat8(f);
2238
2239         return PyFloat_FromDouble(x);
2240 }
2241
2242 static PyObject *
2243 PLyInt_FromInt16(PLyDatumToOb *arg, Datum d)
2244 {
2245         return PyInt_FromLong(DatumGetInt16(d));
2246 }
2247
2248 static PyObject *
2249 PLyInt_FromInt32(PLyDatumToOb *arg, Datum d)
2250 {
2251         return PyInt_FromLong(DatumGetInt32(d));
2252 }
2253
2254 static PyObject *
2255 PLyLong_FromInt64(PLyDatumToOb *arg, Datum d)
2256 {
2257         /* on 32 bit platforms "long" may be too small */
2258         if (sizeof(int64) > sizeof(long))
2259                 return PyLong_FromLongLong(DatumGetInt64(d));
2260         else
2261                 return PyLong_FromLong(DatumGetInt64(d));
2262 }
2263
2264 static PyObject *
2265 PLyBytes_FromBytea(PLyDatumToOb *arg, Datum d)
2266 {
2267         text       *txt = DatumGetByteaP(d);
2268         char       *str = VARDATA(txt);
2269         size_t          size = VARSIZE(txt) - VARHDRSZ;
2270
2271         return PyBytes_FromStringAndSize(str, size);
2272 }
2273
2274 static PyObject *
2275 PLyString_FromDatum(PLyDatumToOb *arg, Datum d)
2276 {
2277         char       *x = OutputFunctionCall(&arg->typfunc, d);
2278         PyObject   *r = PyString_FromString(x);
2279
2280         pfree(x);
2281         return r;
2282 }
2283
2284 static PyObject *
2285 PLyList_FromArray(PLyDatumToOb *arg, Datum d)
2286 {
2287         ArrayType  *array = DatumGetArrayTypeP(d);
2288         PLyDatumToOb *elm = arg->elm;
2289         PyObject   *list;
2290         int                     length;
2291         int                     lbound;
2292         int                     i;
2293
2294         if (ARR_NDIM(array) == 0)
2295                 return PyList_New(0);
2296
2297         if (ARR_NDIM(array) != 1)
2298                 ereport(ERROR,
2299                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2300                           errmsg("cannot convert multidimensional array to Python list"),
2301                           errdetail("PL/Python only supports one-dimensional arrays.")));
2302
2303         length = ARR_DIMS(array)[0];
2304         lbound = ARR_LBOUND(array)[0];
2305         list = PyList_New(length);
2306
2307         for (i = 0; i < length; i++)
2308         {
2309                 Datum           elem;
2310                 bool            isnull;
2311                 int                     offset;
2312
2313                 offset = lbound + i;
2314                 elem = array_ref(array, 1, &offset, arg->typlen,
2315                                                  elm->typlen, elm->typbyval, elm->typalign,
2316                                                  &isnull);
2317                 if (isnull)
2318                 {
2319                         Py_INCREF(Py_None);
2320                         PyList_SET_ITEM(list, i, Py_None);
2321                 }
2322                 else
2323                         PyList_SET_ITEM(list, i, elm->func(elm, elem));
2324         }
2325
2326         return list;
2327 }
2328
2329 static PyObject *
2330 PLyDict_FromTuple(PLyTypeInfo *info, HeapTuple tuple, TupleDesc desc)
2331 {
2332         PyObject   *volatile dict;
2333         int                     i;
2334
2335         if (info->is_rowtype != 1)
2336                 elog(ERROR, "PLyTypeInfo structure describes a datum");
2337
2338         dict = PyDict_New();
2339         if (dict == NULL)
2340                 PLy_elog(ERROR, "could not create new dictionary");
2341
2342         PG_TRY();
2343         {
2344                 for (i = 0; i < info->in.r.natts; i++)
2345                 {
2346                         char       *key;
2347                         Datum           vattr;
2348                         bool            is_null;
2349                         PyObject   *value;
2350
2351                         if (desc->attrs[i]->attisdropped)
2352                                 continue;
2353
2354                         key = NameStr(desc->attrs[i]->attname);
2355                         vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
2356
2357                         if (is_null || info->in.r.atts[i].func == NULL)
2358                                 PyDict_SetItemString(dict, key, Py_None);
2359                         else
2360                         {
2361                                 value = (info->in.r.atts[i].func) (&info->in.r.atts[i], vattr);
2362                                 PyDict_SetItemString(dict, key, value);
2363                                 Py_DECREF(value);
2364                         }
2365                 }
2366         }
2367         PG_CATCH();
2368         {
2369                 Py_DECREF(dict);
2370                 PG_RE_THROW();
2371         }
2372         PG_END_TRY();
2373
2374         return dict;
2375 }
2376
2377 /*
2378  *      Convert a Python object to a PostgreSQL tuple, using all supported
2379  *      conversion methods: tuple as a sequence, as a mapping or as an object that
2380  *      has __getattr__ support.
2381  */
2382 static HeapTuple
2383 PLyObject_ToTuple(PLyTypeInfo *info, TupleDesc desc, PyObject *plrv)
2384 {
2385         HeapTuple       tuple;
2386
2387         if (PySequence_Check(plrv))
2388                 /* composite type as sequence (tuple, list etc) */
2389                 tuple = PLySequence_ToTuple(info, desc, plrv);
2390         else if (PyMapping_Check(plrv))
2391                 /* composite type as mapping (currently only dict) */
2392                 tuple = PLyMapping_ToTuple(info, desc, plrv);
2393         else
2394                 /* returned as smth, must provide method __getattr__(name) */
2395                 tuple = PLyGenericObject_ToTuple(info, desc, plrv);
2396
2397         return tuple;
2398 }
2399
2400 /*
2401  * Convert a Python object to a PostgreSQL bool datum.  This can't go
2402  * through the generic conversion function, because Python attaches a
2403  * Boolean value to everything, more things than the PostgreSQL bool
2404  * type can parse.
2405  */
2406 static Datum
2407 PLyObject_ToBool(PLyObToDatum *arg, int32 typmod, PyObject *plrv)
2408 {
2409         Datum           rv;
2410
2411         Assert(plrv != Py_None);
2412         rv = BoolGetDatum(PyObject_IsTrue(plrv));
2413
2414         if (get_typtype(arg->typoid) == TYPTYPE_DOMAIN)
2415                 domain_check(rv, false, arg->typoid, &arg->typfunc.fn_extra, arg->typfunc.fn_mcxt);
2416
2417         return rv;
2418 }
2419
2420 /*
2421  * Convert a Python object to a PostgreSQL bytea datum.  This doesn't
2422  * go through the generic conversion function to circumvent problems
2423  * with embedded nulls.  And it's faster this way.
2424  */
2425 static Datum
2426 PLyObject_ToBytea(PLyObToDatum *arg, int32 typmod, PyObject *plrv)
2427 {
2428         PyObject   *volatile plrv_so = NULL;
2429         Datum           rv;
2430
2431         Assert(plrv != Py_None);
2432
2433         plrv_so = PyObject_Bytes(plrv);
2434         if (!plrv_so)
2435                 PLy_elog(ERROR, "could not create bytes representation of Python object");
2436
2437         PG_TRY();
2438         {
2439                 char       *plrv_sc = PyBytes_AsString(plrv_so);
2440                 size_t          len = PyBytes_Size(plrv_so);
2441                 size_t          size = len + VARHDRSZ;
2442                 bytea      *result = palloc(size);
2443
2444                 SET_VARSIZE(result, size);
2445                 memcpy(VARDATA(result), plrv_sc, len);
2446                 rv = PointerGetDatum(result);
2447         }
2448         PG_CATCH();
2449         {
2450                 Py_XDECREF(plrv_so);
2451                 PG_RE_THROW();
2452         }
2453         PG_END_TRY();
2454
2455         Py_XDECREF(plrv_so);
2456
2457         if (get_typtype(arg->typoid) == TYPTYPE_DOMAIN)
2458                 domain_check(rv, false, arg->typoid, &arg->typfunc.fn_extra, arg->typfunc.fn_mcxt);
2459
2460         return rv;
2461 }
2462
2463
2464 /*
2465  * Convert a Python object to a composite type. First look up the type's
2466  * description, then route the Python object through the conversion function
2467  * for obtaining PostgreSQL tuples.
2468  */
2469 static Datum
2470 PLyObject_ToComposite(PLyObToDatum *arg, int32 typmod, PyObject *plrv)
2471 {
2472         HeapTuple       tuple = NULL;
2473         Datum           rv;
2474         PLyTypeInfo info;
2475         TupleDesc       desc;
2476
2477         if (typmod != -1)
2478                 elog(ERROR, "received unnamed record type as input");
2479
2480         /* Create a dummy PLyTypeInfo */
2481         MemSet(&info, 0, sizeof(PLyTypeInfo));
2482         PLy_typeinfo_init(&info);
2483         /* Mark it as needing output routines lookup */
2484         info.is_rowtype = 2;
2485
2486         desc = lookup_rowtype_tupdesc(arg->typoid, arg->typmod);
2487
2488         /*
2489          * This will set up the dummy PLyTypeInfo's output conversion routines,
2490          * since we left is_rowtype as 2. A future optimisation could be caching
2491          * that info instead of looking it up every time a tuple is returned from
2492          * the function.
2493          */
2494         tuple = PLyObject_ToTuple(&info, desc, plrv);
2495
2496         PLy_typeinfo_dealloc(&info);
2497
2498         if (tuple != NULL)
2499                 rv = HeapTupleGetDatum(tuple);
2500         else
2501                 rv = (Datum) NULL;
2502
2503         return rv;
2504 }
2505
2506
2507 /*
2508  * Generic conversion function: Convert PyObject to cstring and
2509  * cstring into PostgreSQL type.
2510  */
2511 static Datum
2512 PLyObject_ToDatum(PLyObToDatum *arg, int32 typmod, PyObject *plrv)
2513 {
2514         PyObject   *volatile plrv_bo = NULL;
2515         Datum           rv;
2516
2517         Assert(plrv != Py_None);
2518
2519         if (PyUnicode_Check(plrv))
2520                 plrv_bo = PLyUnicode_Bytes(plrv);
2521         else
2522         {
2523 #if PY_MAJOR_VERSION >= 3
2524                 PyObject   *s = PyObject_Str(plrv);
2525
2526                 plrv_bo = PLyUnicode_Bytes(s);
2527                 Py_XDECREF(s);
2528 #else
2529                 plrv_bo = PyObject_Str(plrv);
2530 #endif
2531         }
2532         if (!plrv_bo)
2533                 PLy_elog(ERROR, "could not create string representation of Python object");
2534
2535         PG_TRY();
2536         {
2537                 char       *plrv_sc = PyBytes_AsString(plrv_bo);
2538                 size_t          plen = PyBytes_Size(plrv_bo);
2539                 size_t          slen = strlen(plrv_sc);
2540
2541                 if (slen < plen)
2542                         ereport(ERROR,
2543                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
2544                                          errmsg("could not convert Python object into cstring: Python string representation appears to contain null bytes")));
2545                 else if (slen > plen)
2546                         elog(ERROR, "could not convert Python object into cstring: Python string longer than reported length");
2547                 pg_verifymbstr(plrv_sc, slen, false);
2548                 rv = InputFunctionCall(&arg->typfunc,
2549                                                            plrv_sc,
2550                                                            arg->typioparam,
2551                                                            typmod);
2552         }
2553         PG_CATCH();
2554         {
2555                 Py_XDECREF(plrv_bo);
2556                 PG_RE_THROW();
2557         }
2558         PG_END_TRY();
2559
2560         Py_XDECREF(plrv_bo);
2561
2562         return rv;
2563 }
2564
2565 static Datum
2566 PLySequence_ToArray(PLyObToDatum *arg, int32 typmod, PyObject *plrv)
2567 {
2568         ArrayType  *array;
2569         int                     i;
2570         Datum      *elems;
2571         bool       *nulls;
2572         int                     len;
2573         int                     lbs;
2574
2575         Assert(plrv != Py_None);
2576
2577         if (!PySequence_Check(plrv))
2578                 PLy_elog(ERROR, "return value of function with array return type is not a Python sequence");
2579
2580         len = PySequence_Length(plrv);
2581         elems = palloc(sizeof(*elems) * len);
2582         nulls = palloc(sizeof(*nulls) * len);
2583
2584         for (i = 0; i < len; i++)
2585         {
2586                 PyObject   *obj = PySequence_GetItem(plrv, i);
2587
2588                 if (obj == Py_None)
2589                         nulls[i] = true;
2590                 else
2591                 {
2592                         nulls[i] = false;
2593
2594                         /*
2595                          * We don't support arrays of row types yet, so the first argument
2596                          * can be NULL.
2597                          */
2598                         elems[i] = arg->elm->func(arg->elm, -1, obj);
2599                 }
2600                 Py_XDECREF(obj);
2601         }
2602
2603         lbs = 1;
2604         array = construct_md_array(elems, nulls, 1, &len, &lbs,
2605                                                            get_element_type(arg->typoid), arg->elm->typlen, arg->elm->typbyval, arg->elm->typalign);
2606         return PointerGetDatum(array);
2607 }
2608
2609 static HeapTuple
2610 PLyMapping_ToTuple(PLyTypeInfo *info, TupleDesc desc, PyObject *mapping)
2611 {
2612         HeapTuple       tuple;
2613         Datum      *values;
2614         bool       *nulls;
2615         volatile int i;
2616
2617         Assert(PyMapping_Check(mapping));
2618
2619         if (info->is_rowtype == 2)
2620                 PLy_output_tuple_funcs(info, desc);
2621         Assert(info->is_rowtype == 1);
2622
2623         /* Build tuple */
2624         values = palloc(sizeof(Datum) * desc->natts);
2625         nulls = palloc(sizeof(bool) * desc->natts);
2626         for (i = 0; i < desc->natts; ++i)
2627         {
2628                 char       *key;
2629                 PyObject   *volatile value;
2630                 PLyObToDatum *att;
2631
2632                 if (desc->attrs[i]->attisdropped)
2633                         continue;
2634
2635                 key = NameStr(desc->attrs[i]->attname);
2636                 value = NULL;
2637                 att = &info->out.r.atts[i];
2638                 PG_TRY();
2639                 {
2640                         value = PyMapping_GetItemString(mapping, key);
2641                         if (value == Py_None)
2642                         {
2643                                 values[i] = (Datum) NULL;
2644                                 nulls[i] = true;
2645                         }
2646                         else if (value)
2647                         {
2648                                 values[i] = (att->func) (att, -1, value);
2649                                 nulls[i] = false;
2650                         }
2651                         else
2652                                 ereport(ERROR,
2653                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
2654                                                  errmsg("key \"%s\" not found in mapping", key),
2655                                                  errhint("To return null in a column, "
2656                                                                  "add the value None to the mapping with the key named after the column.")));
2657
2658                         Py_XDECREF(value);
2659                         value = NULL;
2660                 }
2661                 PG_CATCH();
2662                 {
2663                         Py_XDECREF(value);
2664                         PG_RE_THROW();
2665                 }
2666                 PG_END_TRY();
2667         }
2668
2669         tuple = heap_form_tuple(desc, values, nulls);
2670         ReleaseTupleDesc(desc);
2671         pfree(values);
2672         pfree(nulls);
2673
2674         return tuple;
2675 }
2676
2677
2678 static HeapTuple
2679 PLySequence_ToTuple(PLyTypeInfo *info, TupleDesc desc, PyObject *sequence)
2680 {
2681         HeapTuple       tuple;
2682         Datum      *values;
2683         bool       *nulls;
2684         volatile int idx;
2685         volatile int i;
2686
2687         Assert(PySequence_Check(sequence));
2688
2689         /*
2690          * Check that sequence length is exactly same as PG tuple's. We actually
2691          * can ignore exceeding items or assume missing ones as null but to avoid
2692          * plpython developer's errors we are strict here
2693          */
2694         idx = 0;
2695         for (i = 0; i < desc->natts; i++)
2696         {
2697                 if (!desc->attrs[i]->attisdropped)
2698                         idx++;
2699         }
2700         if (PySequence_Length(sequence) != idx)
2701                 ereport(ERROR,
2702                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
2703                                  errmsg("length of returned sequence did not match number of columns in row")));
2704
2705         if (info->is_rowtype == 2)
2706                 PLy_output_tuple_funcs(info, desc);
2707         Assert(info->is_rowtype == 1);
2708
2709         /* Build tuple */
2710         values = palloc(sizeof(Datum) * desc->natts);
2711         nulls = palloc(sizeof(bool) * desc->natts);
2712         idx = 0;
2713         for (i = 0; i < desc->natts; ++i)
2714         {
2715                 PyObject   *volatile value;
2716                 PLyObToDatum *att;
2717
2718                 if (desc->attrs[i]->attisdropped)
2719                         continue;
2720
2721                 value = NULL;
2722                 att = &info->out.r.atts[i];
2723                 PG_TRY();
2724                 {
2725                         value = PySequence_GetItem(sequence, idx);
2726                         Assert(value);
2727                         if (value == Py_None)
2728                         {
2729                                 values[i] = (Datum) NULL;
2730                                 nulls[i] = true;
2731                         }
2732                         else if (value)
2733                         {
2734                                 values[i] = (att->func) (att, -1, value);
2735                                 nulls[i] = false;
2736                         }
2737
2738                         Py_XDECREF(value);
2739                         value = NULL;
2740                 }
2741                 PG_CATCH();
2742                 {
2743                         Py_XDECREF(value);
2744                         PG_RE_THROW();
2745                 }
2746                 PG_END_TRY();
2747
2748                 idx++;
2749         }
2750
2751         tuple = heap_form_tuple(desc, values, nulls);
2752         ReleaseTupleDesc(desc);
2753         pfree(values);
2754         pfree(nulls);
2755
2756         return tuple;
2757 }
2758
2759
2760 static HeapTuple
2761 PLyGenericObject_ToTuple(PLyTypeInfo *info, TupleDesc desc, PyObject *object)
2762 {
2763         HeapTuple       tuple;
2764         Datum      *values;
2765         bool       *nulls;
2766         volatile int i;
2767
2768         if (info->is_rowtype == 2)
2769                 PLy_output_tuple_funcs(info, desc);
2770         Assert(info->is_rowtype == 1);
2771
2772         /* Build tuple */
2773         values = palloc(sizeof(Datum) * desc->natts);
2774         nulls = palloc(sizeof(bool) * desc->natts);
2775         for (i = 0; i < desc->natts; ++i)
2776         {
2777                 char       *key;
2778                 PyObject   *volatile value;
2779                 PLyObToDatum *att;
2780
2781                 if (desc->attrs[i]->attisdropped)
2782                         continue;
2783
2784                 key = NameStr(desc->attrs[i]->attname);
2785                 value = NULL;
2786                 att = &info->out.r.atts[i];
2787                 PG_TRY();
2788                 {
2789                         value = PyObject_GetAttrString(object, key);
2790                         if (value == Py_None)
2791                         {
2792                                 values[i] = (Datum) NULL;
2793                                 nulls[i] = true;
2794                         }
2795                         else if (value)
2796                         {
2797                                 values[i] = (att->func) (att, -1, value);
2798                                 nulls[i] = false;
2799                         }
2800                         else
2801                                 ereport(ERROR,
2802                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
2803                                                  errmsg("attribute \"%s\" does not exist in Python object", key),
2804                                                  errhint("To return null in a column, "
2805                                                    "let the returned object have an attribute named "
2806                                                                  "after column with value None.")));
2807
2808                         Py_XDECREF(value);
2809                         value = NULL;
2810                 }
2811                 PG_CATCH();
2812                 {
2813                         Py_XDECREF(value);
2814                         PG_RE_THROW();
2815                 }
2816                 PG_END_TRY();
2817         }
2818
2819         tuple = heap_form_tuple(desc, values, nulls);
2820         ReleaseTupleDesc(desc);
2821         pfree(values);
2822         pfree(nulls);
2823
2824         return tuple;
2825 }
2826
2827
2828 /* initialization, some python variables function declared here */
2829
2830 /* interface to postgresql elog */
2831 static PyObject *PLy_debug(PyObject *, PyObject *);
2832 static PyObject *PLy_log(PyObject *, PyObject *);
2833 static PyObject *PLy_info(PyObject *, PyObject *);
2834 static PyObject *PLy_notice(PyObject *, PyObject *);
2835 static PyObject *PLy_warning(PyObject *, PyObject *);
2836 static PyObject *PLy_error(PyObject *, PyObject *);
2837 static PyObject *PLy_fatal(PyObject *, PyObject *);
2838
2839 /* PLyPlanObject, PLyResultObject and SPI interface */
2840 #define is_PLyPlanObject(x) ((x)->ob_type == &PLy_PlanType)
2841 static PyObject *PLy_plan_new(void);
2842 static void PLy_plan_dealloc(PyObject *);
2843 static PyObject *PLy_plan_status(PyObject *, PyObject *);
2844
2845 static PyObject *PLy_result_new(void);
2846 static void PLy_result_dealloc(PyObject *);
2847 static PyObject *PLy_result_nrows(PyObject *, PyObject *);
2848 static PyObject *PLy_result_status(PyObject *, PyObject *);
2849 static Py_ssize_t PLy_result_length(PyObject *);
2850 static PyObject *PLy_result_item(PyObject *, Py_ssize_t);
2851 static PyObject *PLy_result_slice(PyObject *, Py_ssize_t, Py_ssize_t);
2852 static int      PLy_result_ass_item(PyObject *, Py_ssize_t, PyObject *);
2853 static int      PLy_result_ass_slice(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
2854
2855
2856 static PyObject *PLy_spi_prepare(PyObject *, PyObject *);
2857 static PyObject *PLy_spi_execute(PyObject *, PyObject *);
2858 static PyObject *PLy_spi_execute_query(char *query, long limit);
2859 static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, long);
2860 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int);
2861
2862 static PyObject *PLy_quote_literal(PyObject *self, PyObject *args);
2863 static PyObject *PLy_quote_nullable(PyObject *self, PyObject *args);
2864 static PyObject *PLy_quote_ident(PyObject *self, PyObject *args);
2865
2866 static PyObject *PLy_subtransaction(PyObject *, PyObject *);
2867 static PyObject *PLy_subtransaction_new(void);
2868 static void PLy_subtransaction_dealloc(PyObject *);
2869 static PyObject *PLy_subtransaction_enter(PyObject *, PyObject *);
2870 static PyObject *PLy_subtransaction_exit(PyObject *, PyObject *);
2871
2872
2873 static PyMethodDef PLy_plan_methods[] = {
2874         {"status", PLy_plan_status, METH_VARARGS, NULL},
2875         {NULL, NULL, 0, NULL}
2876 };
2877
2878 static PyTypeObject PLy_PlanType = {
2879         PyVarObject_HEAD_INIT(NULL, 0)
2880         "PLyPlan",                                      /* tp_name */
2881         sizeof(PLyPlanObject),          /* tp_size */
2882         0,                                                      /* tp_itemsize */
2883
2884         /*
2885          * methods
2886          */
2887         PLy_plan_dealloc,                       /* tp_dealloc */
2888         0,                                                      /* tp_print */
2889         0,                                                      /* tp_getattr */
2890         0,                                                      /* tp_setattr */
2891         0,                                                      /* tp_compare */
2892         0,                                                      /* tp_repr */
2893         0,                                                      /* tp_as_number */
2894         0,                                                      /* tp_as_sequence */
2895         0,                                                      /* tp_as_mapping */
2896         0,                                                      /* tp_hash */
2897         0,                                                      /* tp_call */
2898         0,                                                      /* tp_str */
2899         0,                                                      /* tp_getattro */
2900         0,                                                      /* tp_setattro */
2901         0,                                                      /* tp_as_buffer */
2902         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
2903         PLy_plan_doc,                           /* tp_doc */
2904         0,                                                      /* tp_traverse */
2905         0,                                                      /* tp_clear */
2906         0,                                                      /* tp_richcompare */
2907         0,                                                      /* tp_weaklistoffset */
2908         0,                                                      /* tp_iter */
2909         0,                                                      /* tp_iternext */
2910         PLy_plan_methods,                       /* tp_tpmethods */
2911 };
2912
2913 static PySequenceMethods PLy_result_as_sequence = {
2914         PLy_result_length,                      /* sq_length */
2915         NULL,                                           /* sq_concat */
2916         NULL,                                           /* sq_repeat */
2917         PLy_result_item,                        /* sq_item */
2918         PLy_result_slice,                       /* sq_slice */
2919         PLy_result_ass_item,            /* sq_ass_item */
2920         PLy_result_ass_slice,           /* sq_ass_slice */
2921 };
2922
2923 static PyMethodDef PLy_result_methods[] = {
2924         {"nrows", PLy_result_nrows, METH_VARARGS, NULL},
2925         {"status", PLy_result_status, METH_VARARGS, NULL},
2926         {NULL, NULL, 0, NULL}
2927 };
2928
2929 static PyTypeObject PLy_ResultType = {
2930         PyVarObject_HEAD_INIT(NULL, 0)
2931         "PLyResult",                            /* tp_name */
2932         sizeof(PLyResultObject),        /* tp_size */
2933         0,                                                      /* tp_itemsize */
2934
2935         /*
2936          * methods
2937          */
2938         PLy_result_dealloc,                     /* tp_dealloc */
2939         0,                                                      /* tp_print */
2940         0,                                                      /* tp_getattr */
2941         0,                                                      /* tp_setattr */
2942         0,                                                      /* tp_compare */
2943         0,                                                      /* tp_repr */
2944         0,                                                      /* tp_as_number */
2945         &PLy_result_as_sequence,        /* tp_as_sequence */
2946         0,                                                      /* tp_as_mapping */
2947         0,                                                      /* tp_hash */
2948         0,                                                      /* tp_call */
2949         0,                                                      /* tp_str */
2950         0,                                                      /* tp_getattro */
2951         0,                                                      /* tp_setattro */
2952         0,                                                      /* tp_as_buffer */
2953         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
2954         PLy_result_doc,                         /* tp_doc */
2955         0,                                                      /* tp_traverse */
2956         0,                                                      /* tp_clear */
2957         0,                                                      /* tp_richcompare */
2958         0,                                                      /* tp_weaklistoffset */
2959         0,                                                      /* tp_iter */
2960         0,                                                      /* tp_iternext */
2961         PLy_result_methods,                     /* tp_tpmethods */
2962 };
2963
2964 static PyMethodDef PLy_subtransaction_methods[] = {
2965         {"__enter__", PLy_subtransaction_enter, METH_VARARGS, NULL},
2966         {"__exit__", PLy_subtransaction_exit, METH_VARARGS, NULL},
2967         /* user-friendly names for Python <2.6 */
2968         {"enter", PLy_subtransaction_enter, METH_VARARGS, NULL},
2969         {"exit", PLy_subtransaction_exit, METH_VARARGS, NULL},
2970         {NULL, NULL, 0, NULL}
2971 };
2972
2973 static PyTypeObject PLy_SubtransactionType = {
2974         PyVarObject_HEAD_INIT(NULL, 0)
2975         "PLySubtransaction",            /* tp_name */
2976         sizeof(PLySubtransactionObject),        /* tp_size */
2977         0,                                                      /* tp_itemsize */
2978
2979         /*
2980          * methods
2981          */
2982         PLy_subtransaction_dealloc, /* tp_dealloc */
2983         0,                                                      /* tp_print */
2984         0,                                                      /* tp_getattr */
2985         0,                                                      /* tp_setattr */
2986         0,                                                      /* tp_compare */
2987         0,                                                      /* tp_repr */
2988         0,                                                      /* tp_as_number */
2989         0,                                                      /* tp_as_sequence */
2990         0,                                                      /* tp_as_mapping */
2991         0,                                                      /* tp_hash */
2992         0,                                                      /* tp_call */
2993         0,                                                      /* tp_str */
2994         0,                                                      /* tp_getattro */
2995         0,                                                      /* tp_setattro */
2996         0,                                                      /* tp_as_buffer */
2997         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,       /* tp_flags */
2998         PLy_subtransaction_doc,         /* tp_doc */
2999         0,                                                      /* tp_traverse */
3000         0,                                                      /* tp_clear */
3001         0,                                                      /* tp_richcompare */
3002         0,                                                      /* tp_weaklistoffset */
3003         0,                                                      /* tp_iter */
3004         0,                                                      /* tp_iternext */
3005         PLy_subtransaction_methods, /* tp_tpmethods */
3006 };
3007
3008 static PyMethodDef PLy_methods[] = {
3009         /*
3010          * logging methods
3011          */
3012         {"debug", PLy_debug, METH_VARARGS, NULL},
3013         {"log", PLy_log, METH_VARARGS, NULL},
3014         {"info", PLy_info, METH_VARARGS, NULL},
3015         {"notice", PLy_notice, METH_VARARGS, NULL},
3016         {"warning", PLy_warning, METH_VARARGS, NULL},
3017         {"error", PLy_error, METH_VARARGS, NULL},
3018         {"fatal", PLy_fatal, METH_VARARGS, NULL},
3019
3020         /*
3021          * create a stored plan
3022          */
3023         {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
3024
3025         /*
3026          * execute a plan or query
3027          */
3028         {"execute", PLy_spi_execute, METH_VARARGS, NULL},
3029
3030         /*
3031          * escaping strings
3032          */
3033         {"quote_literal", PLy_quote_literal, METH_VARARGS, NULL},
3034         {"quote_nullable", PLy_quote_nullable, METH_VARARGS, NULL},
3035         {"quote_ident", PLy_quote_ident, METH_VARARGS, NULL},
3036
3037         /*
3038          * create the subtransaction context manager
3039          */
3040         {"subtransaction", PLy_subtransaction, METH_NOARGS, NULL},
3041
3042         {NULL, NULL, 0, NULL}
3043 };
3044
3045 static PyMethodDef PLy_exc_methods[] = {
3046         {NULL, NULL, 0, NULL}
3047 };
3048
3049 #if PY_MAJOR_VERSION >= 3
3050 static PyModuleDef PLy_module = {
3051         PyModuleDef_HEAD_INIT,          /* m_base */
3052         "plpy",                                         /* m_name */
3053         NULL,                                           /* m_doc */
3054         -1,                                                     /* m_size */
3055         PLy_methods,                            /* m_methods */
3056 };
3057
3058 static PyModuleDef PLy_exc_module = {
3059         PyModuleDef_HEAD_INIT,          /* m_base */
3060         "spiexceptions",                        /* m_name */
3061         NULL,                                           /* m_doc */
3062         -1,                                                     /* m_size */
3063         PLy_exc_methods,                        /* m_methods */
3064         NULL,                                           /* m_reload */
3065         NULL,                                           /* m_traverse */
3066         NULL,                                           /* m_clear */
3067         NULL                                            /* m_free */
3068 };
3069 #endif
3070
3071 /* plan object methods */
3072 static PyObject *
3073 PLy_plan_new(void)
3074 {
3075         PLyPlanObject *ob;
3076
3077         if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
3078                 return NULL;
3079
3080         ob->plan = NULL;
3081         ob->nargs = 0;
3082         ob->types = NULL;
3083         ob->values = NULL;
3084         ob->args = NULL;
3085
3086         return (PyObject *) ob;
3087 }
3088
3089
3090 static void
3091 PLy_plan_dealloc(PyObject *arg)
3092 {
3093         PLyPlanObject *ob = (PLyPlanObject *) arg;
3094
3095         if (ob->plan)
3096                 SPI_freeplan(ob->plan);
3097         if (ob->types)
3098                 PLy_free(ob->types);
3099         if (ob->values)
3100                 PLy_free(ob->values);
3101         if (ob->args)
3102         {
3103                 int                     i;
3104
3105                 for (i = 0; i < ob->nargs; i++)
3106                         PLy_typeinfo_dealloc(&ob->args[i]);
3107                 PLy_free(ob->args);
3108         }
3109
3110         arg->ob_type->tp_free(arg);
3111 }
3112
3113
3114 static PyObject *
3115 PLy_plan_status(PyObject *self, PyObject *args)
3116 {
3117         if (PyArg_ParseTuple(args, ""))
3118         {
3119                 Py_INCREF(Py_True);
3120                 return Py_True;
3121                 /* return PyInt_FromLong(self->status); */
3122         }
3123         PLy_exception_set(PLy_exc_error, "plan.status takes no arguments");
3124         return NULL;
3125 }
3126
3127
3128
3129 /* result object methods */
3130
3131 static PyObject *
3132 PLy_result_new(void)
3133 {
3134         PLyResultObject *ob;
3135
3136         if ((ob = PyObject_New(PLyResultObject, &PLy_ResultType)) == NULL)
3137                 return NULL;
3138
3139         /* ob->tuples = NULL; */
3140
3141         Py_INCREF(Py_None);
3142         ob->status = Py_None;
3143         ob->nrows = PyInt_FromLong(-1);
3144         ob->rows = PyList_New(0);
3145
3146         return (PyObject *) ob;
3147 }
3148
3149 static void
3150 PLy_result_dealloc(PyObject *arg)
3151 {
3152         PLyResultObject *ob = (PLyResultObject *) arg;
3153
3154         Py_XDECREF(ob->nrows);
3155         Py_XDECREF(ob->rows);
3156         Py_XDECREF(ob->status);
3157
3158         arg->ob_type->tp_free(arg);
3159 }
3160
3161 static PyObject *
3162 PLy_result_nrows(PyObject *self, PyObject *args)
3163 {
3164         PLyResultObject *ob = (PLyResultObject *) self;
3165
3166         Py_INCREF(ob->nrows);
3167         return ob->nrows;
3168 }
3169
3170 static PyObject *
3171 PLy_result_status(PyObject *self, PyObject *args)
3172 {
3173         PLyResultObject *ob = (PLyResultObject *) self;
3174
3175         Py_INCREF(ob->status);
3176         return ob->status;
3177 }
3178
3179 static Py_ssize_t
3180 PLy_result_length(PyObject *arg)
3181 {
3182         PLyResultObject *ob = (PLyResultObject *) arg;
3183
3184         return PyList_Size(ob->rows);
3185 }
3186
3187 static PyObject *
3188 PLy_result_item(PyObject *arg, Py_ssize_t idx)
3189 {
3190         PyObject   *rv;
3191         PLyResultObject *ob = (PLyResultObject *) arg;
3192
3193         rv = PyList_GetItem(ob->rows, idx);
3194         if (rv != NULL)
3195                 Py_INCREF(rv);
3196         return rv;
3197 }
3198
3199 static int
3200 PLy_result_ass_item(PyObject *arg, Py_ssize_t idx, PyObject *item)
3201 {
3202         int                     rv;
3203         PLyResultObject *ob = (PLyResultObject *) arg;
3204
3205         Py_INCREF(item);
3206         rv = PyList_SetItem(ob->rows, idx, item);
3207         return rv;
3208 }
3209
3210 static PyObject *
3211 PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx)
3212 {
3213         PLyResultObject *ob = (PLyResultObject *) arg;
3214
3215         return PyList_GetSlice(ob->rows, lidx, hidx);
3216 }
3217
3218 static int
3219 PLy_result_ass_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *slice)
3220 {
3221         int                     rv;
3222         PLyResultObject *ob = (PLyResultObject *) arg;
3223
3224         rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
3225         return rv;
3226 }
3227
3228 /* SPI interface */
3229 static PyObject *
3230 PLy_spi_prepare(PyObject *self, PyObject *args)
3231 {
3232         PLyPlanObject *plan;
3233         PyObject   *list = NULL;
3234         PyObject   *volatile optr = NULL;
3235         char       *query;
3236         void       *tmpplan;
3237         volatile MemoryContext oldcontext;
3238         volatile ResourceOwner oldowner;
3239         volatile int nargs;
3240
3241         if (!PyArg_ParseTuple(args, "s|O", &query, &list))
3242                 return NULL;
3243
3244         if (list && (!PySequence_Check(list)))
3245         {
3246                 PLy_exception_set(PyExc_TypeError,
3247                                            "second argument of plpy.prepare must be a sequence");
3248                 return NULL;
3249         }
3250
3251         if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
3252                 return NULL;
3253
3254         nargs = list ? PySequence_Length(list) : 0;
3255
3256         plan->nargs = nargs;
3257         plan->types = nargs ? PLy_malloc(sizeof(Oid) * nargs) : NULL;
3258         plan->values = nargs ? PLy_malloc(sizeof(Datum) * nargs) : NULL;
3259         plan->args = nargs ? PLy_malloc(sizeof(PLyTypeInfo) * nargs) : NULL;
3260
3261         oldcontext = CurrentMemoryContext;
3262         oldowner = CurrentResourceOwner;
3263
3264         BeginInternalSubTransaction(NULL);
3265         MemoryContextSwitchTo(oldcontext);
3266
3267         PG_TRY();
3268         {
3269                 int                     i;
3270
3271                 /*
3272                  * the other loop might throw an exception, if PLyTypeInfo member
3273                  * isn't properly initialized the Py_DECREF(plan) will go boom
3274                  */
3275                 for (i = 0; i < nargs; i++)
3276                 {
3277                         PLy_typeinfo_init(&plan->args[i]);
3278                         plan->values[i] = PointerGetDatum(NULL);
3279                 }
3280
3281                 for (i = 0; i < nargs; i++)
3282                 {
3283                         char       *sptr;
3284                         HeapTuple       typeTup;
3285                         Oid                     typeId;
3286                         int32           typmod;
3287                         Form_pg_type typeStruct;
3288
3289                         optr = PySequence_GetItem(list, i);
3290                         if (PyString_Check(optr))
3291                                 sptr = PyString_AsString(optr);
3292                         else if (PyUnicode_Check(optr))
3293                                 sptr = PLyUnicode_AsString(optr);
3294                         else
3295                         {
3296                                 ereport(ERROR,
3297                                                 (errmsg("plpy.prepare: type name at ordinal position %d is not a string", i)));
3298                                 sptr = NULL;    /* keep compiler quiet */
3299                         }
3300
3301                         /********************************************************
3302                          * Resolve argument type names and then look them up by
3303                          * oid in the system cache, and remember the required
3304                          *information for input conversion.
3305                          ********************************************************/
3306
3307                         parseTypeString(sptr, &typeId, &typmod);
3308
3309                         typeTup = SearchSysCache1(TYPEOID,
3310                                                                           ObjectIdGetDatum(typeId));
3311                         if (!HeapTupleIsValid(typeTup))
3312                                 elog(ERROR, "cache lookup failed for type %u", typeId);
3313
3314                         Py_DECREF(optr);
3315
3316                         /*
3317                          * set optr to NULL, so we won't try to unref it again in case of
3318                          * an error
3319                          */
3320                         optr = NULL;
3321
3322                         plan->types[i] = typeId;
3323                         typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
3324                         if (typeStruct->typtype != TYPTYPE_COMPOSITE)
3325                                 PLy_output_datum_func(&plan->args[i], typeTup);
3326                         else
3327                                 ereport(ERROR,
3328                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3329                                    errmsg("plpy.prepare does not support composite types")));
3330                         ReleaseSysCache(typeTup);
3331                 }
3332
3333                 pg_verifymbstr(query, strlen(query), false);
3334                 plan->plan = SPI_prepare(query, plan->nargs, plan->types);
3335                 if (plan->plan == NULL)
3336                         elog(ERROR, "SPI_prepare failed: %s",
3337                                  SPI_result_code_string(SPI_result));
3338
3339                 /* transfer plan from procCxt to topCxt */
3340                 tmpplan = plan->plan;
3341                 plan->plan = SPI_saveplan(tmpplan);
3342                 SPI_freeplan(tmpplan);
3343                 if (plan->plan == NULL)
3344                         elog(ERROR, "SPI_saveplan failed: %s",
3345                                  SPI_result_code_string(SPI_result));
3346
3347                 /* Commit the inner transaction, return to outer xact context */
3348                 ReleaseCurrentSubTransaction();
3349                 MemoryContextSwitchTo(oldcontext);
3350                 CurrentResourceOwner = oldowner;
3351
3352                 /*
3353                  * AtEOSubXact_SPI() should not have popped any SPI context, but just
3354                  * in case it did, make sure we remain connected.
3355                  */
3356                 SPI_restore_connection();
3357         }
3358         PG_CATCH();
3359         {
3360                 ErrorData  *edata;
3361                 PLyExceptionEntry *entry;
3362                 PyObject   *exc;
3363
3364                 /* Save error info */
3365                 MemoryContextSwitchTo(oldcontext);
3366                 edata = CopyErrorData();
3367                 FlushErrorState();
3368                 Py_DECREF(plan);
3369                 Py_XDECREF(optr);
3370
3371                 /* Abort the inner transaction */
3372                 RollbackAndReleaseCurrentSubTransaction();
3373                 MemoryContextSwitchTo(oldcontext);
3374                 CurrentResourceOwner = oldowner;
3375
3376                 /*
3377                  * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
3378                  * have left us in a disconnected state.  We need this hack to return
3379                  * to connected state.
3380                  */
3381                 SPI_restore_connection();
3382
3383                 /* Look up the correct exception */
3384                 entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
3385                                                         HASH_FIND, NULL);
3386                 /* We really should find it, but just in case have a fallback */
3387                 Assert(entry != NULL);
3388                 exc = entry ? entry->exc : PLy_exc_spi_error;
3389                 /* Make Python raise the exception */
3390                 PLy_spi_exception_set(exc, edata);
3391                 return NULL;
3392         }
3393         PG_END_TRY();
3394
3395         Assert(plan->plan != NULL);
3396         return (PyObject *) plan;
3397 }
3398
3399 /* execute(query="select * from foo", limit=5)
3400  * execute(plan=plan, values=(foo, bar), limit=5)
3401  */
3402 static PyObject *
3403 PLy_spi_execute(PyObject *self, PyObject *args)
3404 {
3405         char       *query;
3406         PyObject   *plan;
3407         PyObject   *list = NULL;
3408         long            limit = 0;
3409
3410         if (PyArg_ParseTuple(args, "s|l", &query, &limit))
3411                 return PLy_spi_execute_query(query, limit);
3412
3413         PyErr_Clear();
3414
3415         if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
3416                 is_PLyPlanObject(plan))
3417                 return PLy_spi_execute_plan(plan, list, limit);
3418
3419         PLy_exception_set(PLy_exc_error, "plpy.execute expected a query or a plan");
3420         return NULL;
3421 }
3422
3423 static PyObject *
3424 PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
3425 {
3426         volatile int nargs;
3427         int                     i,
3428                                 rv;
3429         PLyPlanObject *plan;
3430         volatile MemoryContext oldcontext;
3431         volatile ResourceOwner oldowner;
3432         PyObject   *ret;
3433
3434         if (list != NULL)
3435         {
3436                 if (!PySequence_Check(list) || PyString_Check(list) || PyUnicode_Check(list))
3437                 {
3438                         PLy_exception_set(PyExc_TypeError, "plpy.execute takes a sequence as its second argument");
3439                         return NULL;
3440                 }
3441                 nargs = PySequence_Length(list);
3442         }
3443         else
3444                 nargs = 0;
3445
3446         plan = (PLyPlanObject *) ob;
3447
3448         if (nargs != plan->nargs)
3449         {
3450                 char       *sv;
3451                 PyObject   *so = PyObject_Str(list);
3452
3453                 if (!so)
3454                         PLy_elog(ERROR, "could not execute plan");
3455                 sv = PyString_AsString(so);
3456                 PLy_exception_set_plural(PyExc_TypeError,
3457                                                           "Expected sequence of %d argument, got %d: %s",
3458                                                          "Expected sequence of %d arguments, got %d: %s",
3459                                                                  plan->nargs,
3460                                                                  plan->nargs, nargs, sv);
3461                 Py_DECREF(so);
3462
3463                 return NULL;
3464         }
3465
3466         oldcontext = CurrentMemoryContext;
3467         oldowner = CurrentResourceOwner;
3468
3469         BeginInternalSubTransaction(NULL);
3470         /* Want to run inside function's memory context */
3471         MemoryContextSwitchTo(oldcontext);
3472
3473         PG_TRY();
3474         {
3475                 char       *volatile nulls;
3476                 volatile int j;
3477
3478                 if (nargs > 0)
3479                         nulls = palloc(nargs * sizeof(char));
3480                 else
3481                         nulls = NULL;
3482
3483                 for (j = 0; j < nargs; j++)
3484                 {
3485                         PyObject   *elem;
3486
3487                         elem = PySequence_GetItem(list, j);
3488                         if (elem != Py_None)
3489                         {
3490                                 PG_TRY();
3491                                 {
3492                                         plan->values[j] =
3493                                                 plan->args[j].out.d.func(&(plan->args[j].out.d),
3494                                                                                                  -1,
3495                                                                                                  elem);
3496                                 }
3497                                 PG_CATCH();
3498                                 {
3499                                         Py_DECREF(elem);
3500                                         PG_RE_THROW();
3501                                 }
3502                                 PG_END_TRY();
3503
3504                                 Py_DECREF(elem);
3505                                 nulls[j] = ' ';
3506                         }
3507                         else
3508                         {
3509                                 Py_DECREF(elem);
3510                                 plan->values[j] =
3511                                         InputFunctionCall(&(plan->args[j].out.d.typfunc),
3512                                                                           NULL,
3513                                                                           plan->args[j].out.d.typioparam,
3514                                                                           -1);
3515                                 nulls[j] = 'n';
3516                         }
3517                 }
3518
3519                 rv = SPI_execute_plan(plan->plan, plan->values, nulls,
3520                                                           PLy_curr_procedure->fn_readonly, limit);
3521                 ret = PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
3522
3523                 if (nargs > 0)
3524                         pfree(nulls);
3525
3526                 /* Commit the inner transaction, return to outer xact context */
3527                 ReleaseCurrentSubTransaction();
3528                 MemoryContextSwitchTo(oldcontext);
3529                 CurrentResourceOwner = oldowner;
3530
3531                 /*
3532                  * AtEOSubXact_SPI() should not have popped any SPI context, but just
3533                  * in case it did, make sure we remain connected.
3534                  */
3535                 SPI_restore_connection();
3536         }
3537         PG_CATCH();
3538         {
3539                 int                     k;
3540                 ErrorData  *edata;
3541                 PLyExceptionEntry *entry;
3542                 PyObject   *exc;
3543
3544                 /* Save error info */
3545                 MemoryContextSwitchTo(oldcontext);
3546                 edata = CopyErrorData();
3547                 FlushErrorState();
3548
3549                 /*
3550                  * cleanup plan->values array
3551                  */
3552                 for (k = 0; k < nargs; k++)
3553                 {
3554                         if (!plan->args[k].out.d.typbyval &&
3555                                 (plan->values[k] != PointerGetDatum(NULL)))
3556                         {
3557                                 pfree(DatumGetPointer(plan->values[k]));
3558                                 plan->values[k] = PointerGetDatum(NULL);
3559                         }
3560                 }
3561
3562                 /* Abort the inner transaction */
3563                 RollbackAndReleaseCurrentSubTransaction();
3564                 MemoryContextSwitchTo(oldcontext);
3565                 CurrentResourceOwner = oldowner;
3566
3567                 /*
3568                  * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
3569                  * have left us in a disconnected state.  We need this hack to return
3570                  * to connected state.
3571                  */
3572                 SPI_restore_connection();
3573
3574                 /* Look up the correct exception */
3575                 entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
3576                                                         HASH_FIND, NULL);
3577                 /* We really should find it, but just in case have a fallback */
3578                 Assert(entry != NULL);
3579                 exc = entry ? entry->exc : PLy_exc_spi_error;
3580                 /* Make Python raise the exception */
3581                 PLy_spi_exception_set(exc, edata);
3582                 return NULL;
3583         }
3584         PG_END_TRY();
3585
3586         for (i = 0; i < nargs; i++)
3587         {
3588                 if (!plan->args[i].out.d.typbyval &&
3589                         (plan->values[i] != PointerGetDatum(NULL)))
3590                 {
3591                         pfree(DatumGetPointer(plan->values[i]));
3592                         plan->values[i] = PointerGetDatum(NULL);
3593                 }
3594         }
3595
3596         if (rv < 0)
3597         {
3598                 PLy_exception_set(PLy_exc_spi_error,
3599                                                   "SPI_execute_plan failed: %s",
3600                                                   SPI_result_code_string(rv));
3601                 return NULL;
3602         }
3603
3604         return ret;
3605 }
3606
3607 static PyObject *
3608 PLy_spi_execute_query(char *query, long limit)
3609 {
3610         int                     rv;
3611         volatile MemoryContext oldcontext;
3612         volatile ResourceOwner oldowner;
3613         PyObject   *ret;
3614
3615         oldcontext = CurrentMemoryContext;
3616         oldowner = CurrentResourceOwner;
3617
3618         BeginInternalSubTransaction(NULL);
3619         /* Want to run inside function's memory context */
3620         MemoryContextSwitchTo(oldcontext);
3621
3622         PG_TRY();
3623         {
3624                 pg_verifymbstr(query, strlen(query), false);
3625                 rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
3626                 ret = PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
3627
3628                 /* Commit the inner transaction, return to outer xact context */
3629                 ReleaseCurrentSubTransaction();
3630                 MemoryContextSwitchTo(oldcontext);
3631                 CurrentResourceOwner = oldowner;
3632
3633                 /*
3634                  * AtEOSubXact_SPI() should not have popped any SPI context, but just
3635                  * in case it did, make sure we remain connected.
3636                  */
3637                 SPI_restore_connection();
3638         }
3639         PG_CATCH();
3640         {
3641                 ErrorData  *edata;
3642                 PLyExceptionEntry *entry;
3643                 PyObject   *exc;
3644
3645                 /* Save error info */
3646                 MemoryContextSwitchTo(oldcontext);
3647                 edata = CopyErrorData();
3648                 FlushErrorState();
3649
3650                 /* Abort the inner transaction */
3651                 RollbackAndReleaseCurrentSubTransaction();
3652                 MemoryContextSwitchTo(oldcontext);
3653                 CurrentResourceOwner = oldowner;
3654
3655                 /*
3656                  * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
3657                  * have left us in a disconnected state.  We need this hack to return
3658                  * to connected state.
3659                  */
3660                 SPI_restore_connection();
3661
3662                 /* Look up the correct exception */
3663                 entry = hash_search(PLy_spi_exceptions, &edata->sqlerrcode,
3664                                                         HASH_FIND, NULL);
3665                 /* We really should find it, but just in case have a fallback */
3666                 Assert(entry != NULL);
3667                 exc = entry ? entry->exc : PLy_exc_spi_error;
3668                 /* Make Python raise the exception */
3669                 PLy_spi_exception_set(exc, edata);
3670                 return NULL;
3671         }
3672         PG_END_TRY();
3673
3674         if (rv < 0)
3675         {
3676                 PLy_exception_set(PLy_exc_spi_error,
3677                                                   "SPI_execute failed: %s",
3678                                                   SPI_result_code_string(rv));
3679                 return NULL;
3680         }
3681
3682         return ret;
3683 }
3684
3685 static PyObject *
3686 PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
3687 {
3688         PLyResultObject *result;
3689         volatile MemoryContext oldcontext;
3690
3691         result = (PLyResultObject *) PLy_result_new();
3692         Py_DECREF(result->status);
3693         result->status = PyInt_FromLong(status);
3694
3695         if (status > 0 && tuptable == NULL)
3696         {
3697                 Py_DECREF(result->nrows);
3698                 result->nrows = PyInt_FromLong(rows);
3699         }
3700         else if (status > 0 && tuptable != NULL)
3701         {
3702                 PLyTypeInfo args;
3703                 int                     i;
3704
3705                 Py_DECREF(result->nrows);
3706                 result->nrows = PyInt_FromLong(rows);
3707                 PLy_typeinfo_init(&args);
3708
3709                 oldcontext = CurrentMemoryContext;
3710                 PG_TRY();
3711                 {
3712                         if (rows)
3713                         {
3714                                 Py_DECREF(result->rows);
3715                                 result->rows = PyList_New(rows);
3716
3717                                 PLy_input_tuple_funcs(&args, tuptable->tupdesc);
3718                                 for (i = 0; i < rows; i++)
3719                                 {
3720                                         PyObject   *row = PLyDict_FromTuple(&args, tuptable->vals[i],
3721                                                                                                                 tuptable->tupdesc);
3722
3723                                         PyList_SetItem(result->rows, i, row);
3724                                 }
3725                         }
3726                 }
3727                 PG_CATCH();
3728                 {
3729                         MemoryContextSwitchTo(oldcontext);
3730                         if (!PyErr_Occurred())
3731                                 PLy_exception_set(PLy_exc_error,
3732                                            "unrecognized error in PLy_spi_execute_fetch_result");
3733                         PLy_typeinfo_dealloc(&args);
3734                         SPI_freetuptable(tuptable);
3735                         Py_DECREF(result);
3736                         return NULL;
3737                 }
3738                 PG_END_TRY();
3739
3740                 PLy_typeinfo_dealloc(&args);
3741                 SPI_freetuptable(tuptable);
3742         }
3743
3744         return (PyObject *) result;
3745 }
3746
3747 /* s = plpy.subtransaction() */
3748 static PyObject *
3749 PLy_subtransaction(PyObject *self, PyObject *unused)
3750 {
3751         return PLy_subtransaction_new();
3752 }
3753
3754 /* Allocate and initialize a PLySubtransactionObject */
3755 static PyObject *
3756 PLy_subtransaction_new(void)
3757 {
3758         PLySubtransactionObject *ob;
3759
3760         ob = PyObject_New(PLySubtransactionObject, &PLy_SubtransactionType);
3761
3762         if (ob == NULL)
3763                 return NULL;
3764
3765         ob->started = false;
3766         ob->exited = false;
3767
3768         return (PyObject *) ob;
3769 }
3770
3771 /* Python requires a dealloc function to be defined */
3772 static void
3773 PLy_subtransaction_dealloc(PyObject *subxact)
3774 {
3775 }
3776
3777 /*
3778  * subxact.__enter__() or subxact.enter()
3779  *
3780  * Start an explicit subtransaction.  SPI calls within an explicit
3781  * subtransaction will not start another one, so you can atomically
3782  * execute many SPI calls and still get a controllable exception if
3783  * one of them fails.
3784  */
3785 static PyObject *
3786 PLy_subtransaction_enter(PyObject *self, PyObject *unused)
3787 {
3788         PLySubtransactionData *subxactdata;
3789         MemoryContext oldcontext;
3790         PLySubtransactionObject *subxact = (PLySubtransactionObject *) self;
3791
3792         if (subxact->started)
3793         {
3794                 PLy_exception_set(PyExc_ValueError, "this subtransaction has already been entered");
3795                 return NULL;
3796         }
3797
3798         if (subxact->exited)
3799         {
3800                 PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
3801                 return NULL;
3802         }
3803
3804         subxact->started = true;
3805         oldcontext = CurrentMemoryContext;
3806
3807         subxactdata = PLy_malloc(sizeof(*subxactdata));
3808         subxactdata->oldcontext = oldcontext;
3809         subxactdata->oldowner = CurrentResourceOwner;
3810
3811         BeginInternalSubTransaction(NULL);
3812         /* Do not want to leave the previous memory context */
3813         MemoryContextSwitchTo(oldcontext);
3814
3815         explicit_subtransactions = lcons(subxactdata, explicit_subtransactions);
3816
3817         Py_INCREF(self);
3818         return self;
3819 }
3820
3821 /*
3822  * subxact.__exit__(exc_type, exc, tb) or subxact.exit(exc_type, exc, tb)
3823  *
3824  * Exit an explicit subtransaction. exc_type is an exception type, exc
3825  * is the exception object, tb is the traceback.  If exc_type is None,
3826  * commit the subtransactiony, if not abort it.
3827  *
3828  * The method signature is chosen to allow subtransaction objects to
3829  * be used as context managers as described in
3830  * <http://www.python.org/dev/peps/pep-0343/>.
3831  */
3832 static PyObject *
3833 PLy_subtransaction_exit(PyObject *self, PyObject *args)
3834 {
3835         PyObject   *type;
3836         PyObject   *value;
3837         PyObject   *traceback;
3838         PLySubtransactionData *subxactdata;
3839         PLySubtransactionObject *subxact = (PLySubtransactionObject *) self;
3840
3841         if (!PyArg_ParseTuple(args, "OOO", &type, &value, &traceback))
3842                 return NULL;
3843
3844         if (!subxact->started)
3845         {
3846                 PLy_exception_set(PyExc_ValueError, "this subtransaction has not been entered");
3847                 return NULL;
3848         }
3849
3850         if (subxact->exited)
3851         {
3852                 PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
3853                 return NULL;
3854         }
3855
3856         if (explicit_subtransactions == NIL)
3857         {
3858                 PLy_exception_set(PyExc_ValueError, "there is no subtransaction to exit from");
3859                 return NULL;
3860         }
3861
3862         subxact->exited = true;
3863
3864         if (type != Py_None)
3865         {
3866                 /* Abort the inner transaction */
3867                 RollbackAndReleaseCurrentSubTransaction();
3868         }
3869         else
3870         {
3871                 ReleaseCurrentSubTransaction();
3872         }
3873
3874         subxactdata = (PLySubtransactionData *) linitial(explicit_subtransactions);
3875         explicit_subtransactions = list_delete_first(explicit_subtransactions);
3876
3877         MemoryContextSwitchTo(subxactdata->oldcontext);
3878         CurrentResourceOwner = subxactdata->oldowner;
3879         PLy_free(subxactdata);
3880
3881         /*
3882          * AtEOSubXact_SPI() should not have popped any SPI context, but just in
3883          * case it did, make sure we remain connected.
3884          */
3885         SPI_restore_connection();
3886
3887         Py_INCREF(Py_None);
3888         return Py_None;
3889 }
3890
3891
3892 /*
3893  * language handler and interpreter initialization
3894  */
3895
3896 /*
3897  * Add exceptions to the plpy module
3898  */
3899
3900 /*
3901  * Add all the autogenerated exceptions as subclasses of SPIError
3902  */
3903 static void
3904 PLy_generate_spi_exceptions(PyObject *mod, PyObject *base)
3905 {
3906         int                     i;
3907
3908         for (i = 0; exception_map[i].name != NULL; i++)
3909         {
3910                 bool            found;
3911                 PyObject   *exc;
3912                 PLyExceptionEntry *entry;
3913                 PyObject   *sqlstate;
3914                 PyObject   *dict = PyDict_New();
3915
3916                 sqlstate = PyString_FromString(unpack_sql_state(exception_map[i].sqlstate));
3917                 PyDict_SetItemString(dict, "sqlstate", sqlstate);
3918                 Py_DECREF(sqlstate);
3919                 exc = PyErr_NewException(exception_map[i].name, base, dict);
3920                 PyModule_AddObject(mod, exception_map[i].classname, exc);
3921                 entry = hash_search(PLy_spi_exceptions, &exception_map[i].sqlstate,
3922                                                         HASH_ENTER, &found);
3923                 entry->exc = exc;
3924                 Assert(!found);
3925         }
3926 }
3927
3928 static void
3929 PLy_add_exceptions(PyObject *plpy)
3930 {
3931         PyObject   *excmod;
3932         HASHCTL         hash_ctl;
3933
3934 #if PY_MAJOR_VERSION < 3
3935         excmod = Py_InitModule("spiexceptions", PLy_exc_methods);
3936 #else
3937         excmod = PyModule_Create(&PLy_exc_module);
3938 #endif
3939         if (PyModule_AddObject(plpy, "spiexceptions", excmod) < 0)
3940                 PLy_elog(ERROR, "could not add the spiexceptions module");
3941
3942 /*
3943  * XXX it appears that in some circumstances the reference count of the
3944  * spiexceptions module drops to zero causing a Python assert failure when
3945  * the garbage collector visits the module. This has been observed on the
3946  * buildfarm. To fix this, add an additional ref for the module here.
3947  *
3948  * This shouldn't cause a memory leak - we don't want this garbage collected,
3949  * and this function shouldn't be called more than once per backend.
3950  */
3951         Py_INCREF(excmod);
3952
3953         PLy_exc_error = PyErr_NewException("plpy.Error", NULL, NULL);
3954         PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
3955         PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
3956
3957         Py_INCREF(PLy_exc_error);
3958         PyModule_AddObject(plpy, "Error", PLy_exc_error);
3959         Py_INCREF(PLy_exc_fatal);
3960         PyModule_AddObject(plpy, "Fatal", PLy_exc_fatal);
3961         Py_INCREF(PLy_exc_spi_error);
3962         PyModule_AddObject(plpy, "SPIError", PLy_exc_spi_error);
3963
3964         memset(&hash_ctl, 0, sizeof(hash_ctl));
3965         hash_ctl.keysize = sizeof(int);
3966         hash_ctl.entrysize = sizeof(PLyExceptionEntry);
3967         hash_ctl.hash = tag_hash;
3968         PLy_spi_exceptions = hash_create("SPI exceptions", 256,
3969                                                                          &hash_ctl, HASH_ELEM | HASH_FUNCTION);
3970
3971         PLy_generate_spi_exceptions(excmod, PLy_exc_spi_error);
3972 }
3973
3974 #if PY_MAJOR_VERSION >= 3
3975 static PyMODINIT_FUNC
3976 PyInit_plpy(void)
3977 {
3978         PyObject   *m;
3979
3980         m = PyModule_Create(&PLy_module);
3981         if (m == NULL)
3982                 return NULL;
3983
3984         PLy_add_exceptions(m);
3985
3986         return m;
3987 }
3988 #endif
3989
3990
3991 static const int plpython_python_version = PY_MAJOR_VERSION;
3992
3993 /*
3994  * _PG_init()                   - library load-time initialization
3995  *
3996  * DO NOT make this static nor change its name!
3997  */
3998 void
3999 _PG_init(void)
4000 {
4001         /* Be sure we do initialization only once (should be redundant now) */
4002         static bool inited = false;
4003         const int **version_ptr;
4004         HASHCTL         hash_ctl;
4005
4006         if (inited)
4007                 return;
4008
4009         /* Be sure we don't run Python 2 and 3 in the same session (might crash) */
4010         version_ptr = (const int **) find_rendezvous_variable("plpython_python_version");
4011         if (!(*version_ptr))
4012                 *version_ptr = &plpython_python_version;
4013         else
4014         {
4015                 if (**version_ptr != plpython_python_version)
4016                         ereport(FATAL,
4017                                         (errmsg("Python major version mismatch in session"),
4018                                          errdetail("This session has previously used Python major version %d, and it is now attempting to use Python major version %d.",
4019                                                            **version_ptr, plpython_python_version),
4020                                          errhint("Start a new session to use a different Python major version.")));
4021         }
4022
4023         pg_bindtextdomain(TEXTDOMAIN);
4024
4025 #if PY_MAJOR_VERSION >= 3
4026         PyImport_AppendInittab("plpy", PyInit_plpy);
4027 #endif
4028         Py_Initialize();
4029 #if PY_MAJOR_VERSION >= 3
4030         PyImport_ImportModule("plpy");
4031 #endif
4032         PLy_init_interp();
4033         PLy_init_plpy();
4034         if (PyErr_Occurred())
4035                 PLy_elog(FATAL, "untrapped error in initialization");
4036
4037         memset(&hash_ctl, 0, sizeof(hash_ctl));
4038         hash_ctl.keysize = sizeof(Oid);
4039         hash_ctl.entrysize = sizeof(PLyProcedureEntry);
4040         hash_ctl.hash = oid_hash;
4041         PLy_procedure_cache = hash_create("PL/Python procedures", 32, &hash_ctl,
4042                                                                           HASH_ELEM | HASH_FUNCTION);
4043
4044         memset(&hash_ctl, 0, sizeof(hash_ctl));
4045         hash_ctl.keysize = sizeof(Oid);
4046         hash_ctl.entrysize = sizeof(PLyProcedureEntry);
4047         hash_ctl.hash = oid_hash;
4048         PLy_trigger_cache = hash_create("PL/Python triggers", 32, &hash_ctl,
4049                                                                         HASH_ELEM | HASH_FUNCTION);
4050
4051         explicit_subtransactions = NIL;
4052
4053         inited = true;
4054 }
4055
4056 static void
4057 PLy_init_interp(void)
4058 {
4059         PyObject   *mainmod;
4060
4061         mainmod = PyImport_AddModule("__main__");
4062         if (mainmod == NULL || PyErr_Occurred())
4063                 PLy_elog(ERROR, "could not import \"__main__\" module");
4064         Py_INCREF(mainmod);
4065         PLy_interp_globals = PyModule_GetDict(mainmod);
4066         PLy_interp_safe_globals = PyDict_New();
4067         PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
4068         Py_DECREF(mainmod);
4069         if (PLy_interp_globals == NULL || PyErr_Occurred())
4070                 PLy_elog(ERROR, "could not initialize globals");
4071 }
4072
4073 static void
4074 PLy_init_plpy(void)
4075 {
4076         PyObject   *main_mod,
4077                            *main_dict,
4078                            *plpy_mod;
4079         PyObject   *plpy;
4080
4081         /*
4082          * initialize plpy module
4083          */
4084         if (PyType_Ready(&PLy_PlanType) < 0)
4085                 elog(ERROR, "could not initialize PLy_PlanType");
4086         if (PyType_Ready(&PLy_ResultType) < 0)
4087                 elog(ERROR, "could not initialize PLy_ResultType");
4088         if (PyType_Ready(&PLy_SubtransactionType) < 0)
4089                 elog(ERROR, "could not initialize PLy_SubtransactionType");
4090
4091 #if PY_MAJOR_VERSION >= 3
4092         plpy = PyModule_Create(&PLy_module);
4093         /* for Python 3 we initialized the exceptions in PyInit_plpy */
4094 #else
4095         plpy = Py_InitModule("plpy", PLy_methods);
4096         PLy_add_exceptions(plpy);
4097 #endif
4098
4099         /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
4100
4101         /*
4102          * initialize main module, and add plpy
4103          */
4104         main_mod = PyImport_AddModule("__main__");
4105         main_dict = PyModule_GetDict(main_mod);
4106         plpy_mod = PyImport_AddModule("plpy");
4107         PyDict_SetItemString(main_dict, "plpy", plpy_mod);
4108         if (PyErr_Occurred())
4109                 elog(ERROR, "could not initialize plpy");
4110 }
4111
4112 /* the python interface to the elog function
4113  * don't confuse these with PLy_elog
4114  */
4115 static PyObject *PLy_output(volatile int, PyObject *, PyObject *);
4116
4117 static PyObject *
4118 PLy_debug(PyObject *self, PyObject *args)
4119 {
4120         return PLy_output(DEBUG2, self, args);
4121 }
4122
4123 static PyObject *
4124 PLy_log(PyObject *self, PyObject *args)
4125 {
4126         return PLy_output(LOG, self, args);
4127 }
4128
4129 static PyObject *
4130 PLy_info(PyObject *self, PyObject *args)
4131 {
4132         return PLy_output(INFO, self, args);
4133 }
4134
4135 static PyObject *
4136 PLy_notice(PyObject *self, PyObject *args)
4137 {
4138         return PLy_output(NOTICE, self, args);
4139 }
4140
4141 static PyObject *
4142 PLy_warning(PyObject *self, PyObject *args)
4143 {
4144         return PLy_output(WARNING, self, args);
4145 }
4146
4147 static PyObject *
4148 PLy_error(PyObject *self, PyObject *args)
4149 {
4150         return PLy_output(ERROR, self, args);
4151 }
4152
4153 static PyObject *
4154 PLy_fatal(PyObject *self, PyObject *args)
4155 {
4156         return PLy_output(FATAL, self, args);
4157 }
4158
4159
4160 static PyObject *
4161 PLy_output(volatile int level, PyObject *self, PyObject *args)
4162 {
4163         PyObject   *volatile so;
4164         char       *volatile sv;
4165         volatile MemoryContext oldcontext;
4166
4167         if (PyTuple_Size(args) == 1)
4168         {
4169                 /*
4170                  * Treat single argument specially to avoid undesirable ('tuple',)
4171                  * decoration.
4172                  */
4173                 PyObject   *o;
4174
4175                 PyArg_UnpackTuple(args, "plpy.elog", 1, 1, &o);
4176                 so = PyObject_Str(o);
4177         }
4178         else
4179                 so = PyObject_Str(args);
4180         if (so == NULL || ((sv = PyString_AsString(so)) == NULL))
4181         {
4182                 level = ERROR;
4183                 sv = dgettext(TEXTDOMAIN, "could not parse error message in plpy.elog");
4184         }
4185
4186         oldcontext = CurrentMemoryContext;
4187         PG_TRY();
4188         {
4189                 pg_verifymbstr(sv, strlen(sv), false);
4190                 elog(level, "%s", sv);
4191         }
4192         PG_CATCH();
4193         {
4194                 ErrorData  *edata;
4195
4196                 MemoryContextSwitchTo(oldcontext);
4197                 edata = CopyErrorData();
4198                 FlushErrorState();
4199
4200                 /*
4201                  * Note: If sv came from PyString_AsString(), it points into storage
4202                  * owned by so.  So free so after using sv.
4203                  */
4204                 Py_XDECREF(so);
4205
4206                 /* Make Python raise the exception */
4207                 PLy_exception_set(PLy_exc_error, "%s", edata->message);
4208                 return NULL;
4209         }
4210         PG_END_TRY();
4211
4212         Py_XDECREF(so);
4213
4214         /*
4215          * return a legal object so the interpreter will continue on its merry way
4216          */
4217         Py_INCREF(Py_None);
4218         return Py_None;
4219 }
4220
4221
4222 static PyObject *
4223 PLy_quote_literal(PyObject *self, PyObject *args)
4224 {
4225         const char *str;
4226         char       *quoted;
4227         PyObject   *ret;
4228
4229         if (!PyArg_ParseTuple(args, "s", &str))
4230                 return NULL;
4231
4232         quoted = quote_literal_cstr(str);
4233         ret = PyString_FromString(quoted);
4234         pfree(quoted);
4235
4236         return ret;
4237 }
4238
4239 static PyObject *
4240 PLy_quote_nullable(PyObject *self, PyObject *args)
4241 {
4242         const char *str;
4243         char       *quoted;
4244         PyObject   *ret;
4245
4246         if (!PyArg_ParseTuple(args, "z", &str))
4247                 return NULL;
4248
4249         if (str == NULL)
4250                 return PyString_FromString("NULL");
4251
4252         quoted = quote_literal_cstr(str);
4253         ret = PyString_FromString(quoted);
4254         pfree(quoted);
4255
4256         return ret;
4257 }
4258
4259 static PyObject *
4260 PLy_quote_ident(PyObject *self, PyObject *args)
4261 {
4262         const char *str;
4263         const char *quoted;
4264         PyObject   *ret;
4265
4266         if (!PyArg_ParseTuple(args, "s", &str))
4267                 return NULL;
4268
4269         quoted = quote_identifier(str);
4270         ret = PyString_FromString(quoted);
4271
4272         return ret;
4273 }
4274
4275
4276 /*
4277  * Get the name of the last procedure called by the backend (the
4278  * innermost, if a plpython procedure call calls the backend and the
4279  * backend calls another plpython procedure).
4280  *
4281  * NB: this returns the SQL name, not the internal Python procedure name
4282  */
4283 static char *
4284 PLy_procedure_name(PLyProcedure *proc)
4285 {
4286         if (proc == NULL)
4287                 return "<unknown procedure>";
4288         return proc->proname;
4289 }
4290
4291 /*
4292  * Call PyErr_SetString with a vprint interface and translation support
4293  */
4294 static void
4295 PLy_exception_set(PyObject *exc, const char *fmt,...)
4296 {
4297         char            buf[1024];
4298         va_list         ap;
4299
4300         va_start(ap, fmt);
4301         vsnprintf(buf, sizeof(buf), dgettext(TEXTDOMAIN, fmt), ap);
4302         va_end(ap);
4303
4304         PyErr_SetString(exc, buf);
4305 }
4306
4307 /*
4308  * The same, pluralized.
4309  */
4310 static void
4311 PLy_exception_set_plural(PyObject *exc,
4312                                                  const char *fmt_singular, const char *fmt_plural,
4313                                                  unsigned long n,...)
4314 {
4315         char            buf[1024];
4316         va_list         ap;
4317
4318         va_start(ap, n);
4319         vsnprintf(buf, sizeof(buf),
4320                           dngettext(TEXTDOMAIN, fmt_singular, fmt_plural, n),
4321                           ap);
4322         va_end(ap);
4323
4324         PyErr_SetString(exc, buf);
4325 }
4326
4327 /*
4328  * Raise a SPIError, passing in it more error details, like the
4329  * internal query and error position.
4330  */
4331 static void
4332 PLy_spi_exception_set(PyObject *excclass, ErrorData *edata)
4333 {
4334         PyObject   *args = NULL;
4335         PyObject   *spierror = NULL;
4336         PyObject   *spidata = NULL;
4337
4338         args = Py_BuildValue("(s)", edata->message);
4339         if (!args)
4340                 goto failure;
4341
4342         /* create a new SPI exception with the error message as the parameter */
4343         spierror = PyObject_CallObject(excclass, args);
4344         if (!spierror)
4345                 goto failure;
4346
4347         spidata = Py_BuildValue("(zzzi)", edata->detail, edata->hint,
4348                                                         edata->internalquery, edata->internalpos);
4349         if (!spidata)
4350                 goto failure;
4351
4352         if (PyObject_SetAttrString(spierror, "spidata", spidata) == -1)
4353                 goto failure;
4354
4355         PyErr_SetObject(excclass, spierror);
4356
4357         Py_DECREF(args);
4358         Py_DECREF(spierror);
4359         Py_DECREF(spidata);
4360         return;
4361
4362 failure:
4363         Py_XDECREF(args);
4364         Py_XDECREF(spierror);
4365         Py_XDECREF(spidata);
4366         elog(ERROR, "could not convert SPI error to Python exception");
4367 }
4368
4369 /* Emit a PG error or notice, together with any available info about
4370  * the current Python error, previously set by PLy_exception_set().
4371  * This should be used to propagate Python errors into PG.      If fmt is
4372  * NULL, the Python error becomes the primary error message, otherwise
4373  * it becomes the detail.  If there is a Python traceback, it is put
4374  * in the context.
4375  */
4376 static void
4377 PLy_elog(int elevel, const char *fmt,...)
4378 {
4379         char       *xmsg;
4380         char       *tbmsg;
4381         int                     tb_depth;
4382         StringInfoData emsg;
4383         PyObject   *exc,
4384                            *val,
4385                            *tb;
4386         const char *primary = NULL;
4387         char       *detail = NULL;
4388         char       *hint = NULL;
4389         char       *query = NULL;
4390         int                     position = 0;
4391
4392         PyErr_Fetch(&exc, &val, &tb);
4393         if (exc != NULL)
4394         {
4395                 if (PyErr_GivenExceptionMatches(val, PLy_exc_spi_error))
4396                         PLy_get_spi_error_data(val, &detail, &hint, &query, &position);
4397                 else if (PyErr_GivenExceptionMatches(val, PLy_exc_fatal))
4398                         elevel = FATAL;
4399         }
4400         PyErr_Restore(exc, val, tb);
4401
4402         PLy_traceback(&xmsg, &tbmsg, &tb_depth);
4403
4404         if (fmt)
4405         {
4406                 initStringInfo(&emsg);
4407                 for (;;)
4408                 {
4409                         va_list         ap;
4410                         bool            success;
4411
4412                         va_start(ap, fmt);
4413                         success = appendStringInfoVA(&emsg, dgettext(TEXTDOMAIN, fmt), ap);
4414                         va_end(ap);
4415                         if (success)
4416                                 break;
4417                         enlargeStringInfo(&emsg, emsg.maxlen);
4418                 }
4419                 primary = emsg.data;
4420
4421                 /* Since we have a format string, we cannot have a SPI detail. */
4422                 Assert(detail == NULL);
4423
4424                 /* If there's an exception message, it goes in the detail. */
4425                 if (xmsg)
4426                         detail = xmsg;
4427         }
4428         else
4429         {
4430                 if (xmsg)
4431                         primary = xmsg;
4432         }
4433
4434         PG_TRY();
4435         {
4436                 ereport(elevel,
4437                                 (errmsg("%s", primary ? primary : "no exception data"),
4438                                  (detail) ? errdetail("%s", detail) : 0,
4439                                  (tb_depth > 0 && tbmsg) ? errcontext("%s", tbmsg) : 0,
4440                                  (hint) ? errhint("%s", hint) : 0,
4441                                  (query) ? internalerrquery(query) : 0,
4442                                  (position) ? internalerrposition(position) : 0));
4443         }
4444         PG_CATCH();
4445         {
4446                 if (fmt)
4447                         pfree(emsg.data);
4448                 if (xmsg)
4449                         pfree(xmsg);
4450                 if (tbmsg)
4451                         pfree(tbmsg);
4452                 PG_RE_THROW();
4453         }
4454         PG_END_TRY();
4455
4456         if (fmt)
4457                 pfree(emsg.data);
4458         if (xmsg)
4459                 pfree(xmsg);
4460         if (tbmsg)
4461                 pfree(tbmsg);
4462 }
4463
4464 /*
4465  * Extract the error data from a SPIError
4466  */
4467 static void
4468 PLy_get_spi_error_data(PyObject *exc, char **detail, char **hint, char **query, int *position)
4469 {
4470         PyObject   *spidata = NULL;
4471
4472         spidata = PyObject_GetAttrString(exc, "spidata");
4473         if (!spidata)
4474                 goto cleanup;
4475
4476         if (!PyArg_ParseTuple(spidata, "zzzi", detail, hint, query, position))
4477                 goto cleanup;
4478
4479 cleanup:
4480         PyErr_Clear();
4481         /* no elog here, we simply won't report the errhint, errposition etc */
4482         Py_XDECREF(spidata);
4483 }
4484
4485 /*
4486  * Get the given source line as a palloc'd string
4487  */
4488 static char *
4489 get_source_line(const char *src, int lineno)
4490 {
4491         const char *s = NULL;
4492         const char *next = src;
4493         int                     current = 0;
4494
4495         while (current < lineno)
4496         {
4497                 s = next;
4498                 next = strchr(s + 1, '\n');
4499                 current++;
4500                 if (next == NULL)
4501                         break;
4502         }
4503
4504         if (current != lineno)
4505                 return NULL;
4506
4507         while (*s && isspace((unsigned char) *s))
4508                 s++;
4509
4510         if (next == NULL)
4511                 return pstrdup(s);
4512
4513         /*
4514          * Sanity check, next < s if the line was all-whitespace, which should
4515          * never happen if Python reported a frame created on that line, but check
4516          * anyway.
4517          */
4518         if (next < s)
4519                 return NULL;
4520
4521         return pnstrdup(s, next - s);
4522 }
4523
4524 /*
4525  * Extract a Python traceback from the current exception.
4526  *
4527  * The exception error message is returned in xmsg, the traceback in
4528  * tbmsg (both as palloc'd strings) and the traceback depth in
4529  * tb_depth.
4530  */
4531 static void
4532 PLy_traceback(char **xmsg, char **tbmsg, int *tb_depth)
4533 {
4534         PyObject   *e,
4535                            *v,
4536                            *tb;
4537         PyObject   *e_type_o;
4538         PyObject   *e_module_o;
4539         char       *e_type_s = NULL;
4540         char       *e_module_s = NULL;
4541         PyObject   *vob = NULL;
4542         char       *vstr;
4543         StringInfoData xstr;
4544         StringInfoData tbstr;
4545
4546         /*
4547          * get the current exception
4548          */
4549         PyErr_Fetch(&e, &v, &tb);
4550
4551         /*
4552          * oops, no exception, return
4553          */
4554         if (e == NULL)
4555         {
4556                 *xmsg = NULL;
4557                 *tbmsg = NULL;
4558                 *tb_depth = 0;
4559
4560                 return;
4561         }
4562
4563         PyErr_NormalizeException(&e, &v, &tb);
4564
4565         /*
4566          * Format the exception and its value and put it in xmsg.
4567          */
4568
4569         e_type_o = PyObject_GetAttrString(e, "__name__");
4570         e_module_o = PyObject_GetAttrString(e, "__module__");
4571         if (e_type_o)
4572                 e_type_s = PyString_AsString(e_type_o);
4573         if (e_type_s)
4574                 e_module_s = PyString_AsString(e_module_o);
4575
4576         if (v && ((vob = PyObject_Str(v)) != NULL))
4577                 vstr = PyString_AsString(vob);
4578         else
4579                 vstr = "unknown";
4580
4581         initStringInfo(&xstr);
4582         if (!e_type_s || !e_module_s)
4583         {
4584                 if (PyString_Check(e))
4585                         /* deprecated string exceptions */
4586                         appendStringInfoString(&xstr, PyString_AsString(e));
4587                 else
4588                         /* shouldn't happen */
4589                         appendStringInfoString(&xstr, "unrecognized exception");
4590         }
4591         /* mimics behavior of traceback.format_exception_only */
4592         else if (strcmp(e_module_s, "builtins") == 0
4593                          || strcmp(e_module_s, "__main__") == 0
4594                          || strcmp(e_module_s, "exceptions") == 0)
4595                 appendStringInfo(&xstr, "%s", e_type_s);
4596         else
4597                 appendStringInfo(&xstr, "%s.%s", e_module_s, e_type_s);
4598         appendStringInfo(&xstr, ": %s", vstr);
4599
4600         *xmsg = xstr.data;
4601
4602         /*
4603          * Now format the traceback and put it in tbmsg.
4604          */
4605
4606         *tb_depth = 0;
4607         initStringInfo(&tbstr);
4608         /* Mimick Python traceback reporting as close as possible. */
4609         appendStringInfoString(&tbstr, "Traceback (most recent call last):");
4610         while (tb != NULL && tb != Py_None)
4611         {
4612                 PyObject   *volatile tb_prev = NULL;
4613                 PyObject   *volatile frame = NULL;
4614                 PyObject   *volatile code = NULL;
4615                 PyObject   *volatile name = NULL;
4616                 PyObject   *volatile lineno = NULL;
4617                 PyObject   *volatile filename = NULL;
4618
4619                 PG_TRY();
4620                 {
4621                         lineno = PyObject_GetAttrString(tb, "tb_lineno");
4622                         if (lineno == NULL)
4623                                 elog(ERROR, "could not get line number from Python traceback");
4624
4625                         frame = PyObject_GetAttrString(tb, "tb_frame");
4626                         if (frame == NULL)
4627                                 elog(ERROR, "could not get frame from Python traceback");
4628
4629                         code = PyObject_GetAttrString(frame, "f_code");
4630                         if (code == NULL)
4631                                 elog(ERROR, "could not get code object from Python frame");
4632
4633                         name = PyObject_GetAttrString(code, "co_name");
4634                         if (name == NULL)
4635                                 elog(ERROR, "could not get function name from Python code object");
4636
4637                         filename = PyObject_GetAttrString(code, "co_filename");
4638                         if (filename == NULL)
4639                                 elog(ERROR, "could not get file name from Python code object");
4640                 }
4641                 PG_CATCH();
4642                 {
4643                         Py_XDECREF(frame);
4644                         Py_XDECREF(code);
4645                         Py_XDECREF(name);
4646                         Py_XDECREF(lineno);
4647                         Py_XDECREF(filename);
4648                         PG_RE_THROW();
4649                 }
4650                 PG_END_TRY();
4651
4652                 /* The first frame always points at <module>, skip it. */
4653                 if (*tb_depth > 0)
4654                 {
4655                         char       *proname;
4656                         char       *fname;
4657                         char       *line;
4658                         char       *plain_filename;
4659                         long            plain_lineno;
4660
4661                         /*
4662                          * The second frame points at the internal function, but to mimick
4663                          * Python error reporting we want to say <module>.
4664                          */
4665                         if (*tb_depth == 1)
4666                                 fname = "<module>";
4667                         else
4668                                 fname = PyString_AsString(name);
4669
4670                         proname = PLy_procedure_name(PLy_curr_procedure);
4671                         plain_filename = PyString_AsString(filename);
4672                         plain_lineno = PyInt_AsLong(lineno);
4673
4674                         if (proname == NULL)
4675                                 appendStringInfo(
4676                                 &tbstr, "\n  PL/Python anonymous code block, line %ld, in %s",
4677                                                                  plain_lineno - 1, fname);
4678                         else
4679                                 appendStringInfo(
4680                                         &tbstr, "\n  PL/Python function \"%s\", line %ld, in %s",
4681                                                                  proname, plain_lineno - 1, fname);
4682
4683                         /*
4684                          * function code object was compiled with "<string>" as the
4685                          * filename
4686                          */
4687                         if (PLy_curr_procedure && plain_filename != NULL &&
4688                                 strcmp(plain_filename, "<string>") == 0)
4689                         {
4690                                 /*
4691                                  * If we know the current procedure, append the exact line
4692                                  * from the source, again mimicking Python's traceback.py
4693                                  * module behavior.  We could store the already line-split
4694                                  * source to avoid splitting it every time, but producing a
4695                                  * traceback is not the most important scenario to optimize
4696                                  * for.  But we do not go as far as traceback.py in reading
4697                                  * the source of imported modules.
4698                                  */
4699                                 line = get_source_line(PLy_curr_procedure->src, plain_lineno);
4700                                 if (line)
4701                                 {
4702                                         appendStringInfo(&tbstr, "\n    %s", line);
4703                                         pfree(line);
4704                                 }
4705                         }
4706                 }
4707
4708                 Py_DECREF(frame);
4709                 Py_DECREF(code);
4710                 Py_DECREF(name);
4711                 Py_DECREF(lineno);
4712                 Py_DECREF(filename);
4713
4714                 /* Release the current frame and go to the next one. */
4715                 tb_prev = tb;
4716                 tb = PyObject_GetAttrString(tb, "tb_next");
4717                 Assert(tb_prev != Py_None);
4718                 Py_DECREF(tb_prev);
4719                 if (tb == NULL)
4720                         elog(ERROR, "could not traverse Python traceback");
4721                 (*tb_depth)++;
4722         }
4723
4724         /* Return the traceback. */
4725         *tbmsg = tbstr.data;
4726
4727         Py_XDECREF(e_type_o);
4728         Py_XDECREF(e_module_o);
4729         Py_XDECREF(vob);
4730         Py_XDECREF(v);
4731         Py_DECREF(e);
4732 }
4733
4734 /* python module code */
4735
4736 /* some dumb utility functions */
4737 static void *
4738 PLy_malloc(size_t bytes)
4739 {
4740         /* We need our allocations to be long-lived, so use TopMemoryContext */
4741         return MemoryContextAlloc(TopMemoryContext, bytes);
4742 }
4743
4744 static void *
4745 PLy_malloc0(size_t bytes)
4746 {
4747         void       *ptr = PLy_malloc(bytes);
4748
4749         MemSet(ptr, 0, bytes);
4750         return ptr;
4751 }
4752
4753 static char *
4754 PLy_strdup(const char *str)
4755 {
4756         char       *result;
4757         size_t          len;
4758
4759         len = strlen(str) + 1;
4760         result = PLy_malloc(len);
4761         memcpy(result, str, len);
4762
4763         return result;
4764 }
4765
4766 /* define this away */
4767 static void
4768 PLy_free(void *ptr)
4769 {
4770         pfree(ptr);
4771 }
4772
4773 /*
4774  * Convert a Python unicode object to a Python string/bytes object in
4775  * PostgreSQL server encoding.  Reference ownership is passed to the
4776  * caller.
4777  */
4778 static PyObject *
4779 PLyUnicode_Bytes(PyObject *unicode)
4780 {
4781         PyObject   *rv;
4782         const char *serverenc;
4783
4784         /*
4785          * Python understands almost all PostgreSQL encoding names, but it doesn't
4786          * know SQL_ASCII.
4787          */
4788         if (GetDatabaseEncoding() == PG_SQL_ASCII)
4789                 serverenc = "ascii";
4790         else
4791                 serverenc = GetDatabaseEncodingName();
4792         rv = PyUnicode_AsEncodedString(unicode, serverenc, "strict");
4793         if (rv == NULL)
4794                 PLy_elog(ERROR, "could not convert Python Unicode object to PostgreSQL server encoding");
4795         return rv;
4796 }
4797
4798 /*
4799  * Convert a Python unicode object to a C string in PostgreSQL server
4800  * encoding.  No Python object reference is passed out of this
4801  * function.  The result is palloc'ed.
4802  *
4803  * Note that this function is disguised as PyString_AsString() when
4804  * using Python 3.      That function retuns a pointer into the internal
4805  * memory of the argument, which isn't exactly the interface of this
4806  * function.  But in either case you get a rather short-lived
4807  * reference that you ought to better leave alone.
4808  */
4809 static char *
4810 PLyUnicode_AsString(PyObject *unicode)
4811 {
4812         PyObject   *o = PLyUnicode_Bytes(unicode);
4813         char       *rv = pstrdup(PyBytes_AsString(o));
4814
4815         Py_XDECREF(o);
4816         return rv;
4817 }
4818
4819 #if PY_MAJOR_VERSION >= 3
4820 /*
4821  * Convert a C string in the PostgreSQL server encoding to a Python
4822  * unicode object.      Reference ownership is passed to the caller.
4823  */
4824 static PyObject *
4825 PLyUnicode_FromString(const char *s)
4826 {
4827         char       *utf8string;
4828         PyObject   *o;
4829
4830         utf8string = (char *) pg_do_encoding_conversion((unsigned char *) s,
4831                                                                                                         strlen(s),
4832                                                                                                         GetDatabaseEncoding(),
4833                                                                                                         PG_UTF8);
4834
4835         o = PyUnicode_FromString(utf8string);
4836
4837         if (utf8string != s)
4838                 pfree(utf8string);
4839
4840         return o;
4841 }
4842 #endif   /* PY_MAJOR_VERSION >= 3 */
4843
4844 #if PY_MAJOR_VERSION < 3
4845
4846 /* Define aliases plpython2_call_handler etc */
4847 Datum           plpython2_call_handler(PG_FUNCTION_ARGS);
4848 Datum           plpython2_inline_handler(PG_FUNCTION_ARGS);
4849 Datum           plpython2_validator(PG_FUNCTION_ARGS);
4850
4851 PG_FUNCTION_INFO_V1(plpython2_call_handler);
4852
4853 Datum
4854 plpython2_call_handler(PG_FUNCTION_ARGS)
4855 {
4856         return plpython_call_handler(fcinfo);
4857 }
4858
4859 PG_FUNCTION_INFO_V1(plpython2_inline_handler);
4860
4861 Datum
4862 plpython2_inline_handler(PG_FUNCTION_ARGS)
4863 {
4864         return plpython_inline_handler(fcinfo);
4865 }
4866
4867 PG_FUNCTION_INFO_V1(plpython2_validator);
4868
4869 Datum
4870 plpython2_validator(PG_FUNCTION_ARGS)
4871 {
4872         return plpython_validator(fcinfo);
4873 }
4874
4875 #endif   /* PY_MAJOR_VERSION < 3 */