]> granicus.if.org Git - postgresql/blob - src/pl/plpython/plpython.c
Define errcode as __msvc_errcode not __vc_errcode for MSVC builds,
[postgresql] / src / pl / plpython / plpython.c
1 /**********************************************************************
2  * plpython.c - python as a procedural language for PostgreSQL
3  *
4  *      $PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.88 2006/09/16 13:35:49 tgl Exp $
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 #else
19 #include <Python.h>
20 #endif
21 #include "postgres.h"
22
23 /* system stuff */
24 #include <unistd.h>
25 #include <fcntl.h>
26
27 /* postgreSQL stuff */
28 #include "access/heapam.h"
29 #include "catalog/pg_proc.h"
30 #include "catalog/pg_type.h"
31 #include "commands/trigger.h"
32 #include "executor/spi.h"
33 #include "funcapi.h"
34 #include "fmgr.h"
35 #include "nodes/makefuncs.h"
36 #include "parser/parse_type.h"
37 #include "tcop/tcopprot.h"
38 #include "utils/builtins.h"
39 #include "utils/lsyscache.h"
40 #include "utils/memutils.h"
41 #include "utils/syscache.h"
42 #include "utils/typcache.h"
43
44 #include <compile.h>
45 #include <eval.h>
46
47 PG_MODULE_MAGIC;
48
49 /* convert Postgresql Datum or tuple into a PyObject.
50  * input to Python.  Tuples are converted to dictionary
51  * objects.
52  */
53
54 typedef PyObject *(*PLyDatumToObFunc) (const char *);
55
56 typedef struct PLyDatumToOb
57 {
58         PLyDatumToObFunc func;
59         FmgrInfo        typfunc;
60         Oid                     typioparam;
61         bool            typbyval;
62 }       PLyDatumToOb;
63
64 typedef struct PLyTupleToOb
65 {
66         PLyDatumToOb *atts;
67         int                     natts;
68 }       PLyTupleToOb;
69
70 typedef union PLyTypeInput
71 {
72         PLyDatumToOb d;
73         PLyTupleToOb r;
74 }       PLyTypeInput;
75
76 /* convert PyObject to a Postgresql Datum or tuple.
77  * output from Python
78  */
79 typedef struct PLyObToDatum
80 {
81         FmgrInfo        typfunc;                /* The type's input function */
82         Oid                     typoid;                 /* The OID of the type */
83         Oid                     typioparam;
84         bool            typbyval;
85 }       PLyObToDatum;
86
87 typedef struct PLyObToTuple
88 {
89         PLyObToDatum *atts;
90         int                     natts;
91 }       PLyObToTuple;
92
93 typedef union PLyTypeOutput
94 {
95         PLyObToDatum d;
96         PLyObToTuple r;
97 }       PLyTypeOutput;
98
99 /* all we need to move Postgresql data to Python objects,
100  * and vis versa
101  */
102 typedef struct PLyTypeInfo
103 {
104         PLyTypeInput in;
105         PLyTypeOutput out;
106         int                     is_rowtype;
107
108         /*
109          * is_rowtype can be: -1  not known yet (initial state) 0  scalar datatype
110          * 1  rowtype 2  rowtype, but I/O functions not set up yet
111          */
112 }       PLyTypeInfo;
113
114
115 /* cached procedure data */
116 typedef struct PLyProcedure
117 {
118         char       *proname;            /* SQL name of procedure */
119         char       *pyname;                     /* Python name of procedure */
120         TransactionId fn_xmin;
121         CommandId       fn_cmin;
122         bool            fn_readonly;
123         PLyTypeInfo result;                     /* also used to store info for trigger tuple
124                                                                  * type */
125         bool        is_setof;           /* true, if procedure returns result set */
126         PyObject    *setof;             /* contents of result set. */
127         char        **argnames;         /* Argument names */
128         PLyTypeInfo args[FUNC_MAX_ARGS];
129         int                     nargs;
130         PyObject   *code;                       /* compiled procedure code */
131         PyObject   *statics;            /* data saved across calls, local scope */
132         PyObject   *globals;            /* data saved across calls, global scope */
133         PyObject   *me;                         /* PyCObject containing pointer to this
134                                                                  * PLyProcedure */
135 }       PLyProcedure;
136
137
138 /* Python objects */
139 typedef struct PLyPlanObject
140 {
141         PyObject_HEAD
142         void       *plan;                       /* return of an SPI_saveplan */
143         int                     nargs;
144         Oid                *types;
145         Datum      *values;
146         PLyTypeInfo *args;
147 }       PLyPlanObject;
148
149 typedef struct PLyResultObject
150 {
151         PyObject_HEAD
152         /* HeapTuple *tuples; */
153         PyObject   *nrows;                      /* number of rows returned by query */
154         PyObject   *rows;                       /* data rows, or None if no data returned */
155         PyObject   *status;                     /* query status, SPI_OK_*, or SPI_ERR_* */
156 }       PLyResultObject;
157
158
159 /* function declarations */
160
161 /* Two exported functions: first is the magic telling Postgresql
162  * what function call interface it implements. Second is for
163  * initialization of the interpreter during library load.
164  */
165 Datum           plpython_call_handler(PG_FUNCTION_ARGS);
166 void            _PG_init(void);
167
168 PG_FUNCTION_INFO_V1(plpython_call_handler);
169
170 /* most of the remaining of the declarations, all static */
171
172 /* these should only be called once at the first call
173  * of plpython_call_handler.  initialize the python interpreter
174  * and global data.
175  */
176 static void PLy_init_interp(void);
177 static void PLy_init_plpy(void);
178
179 /* call PyErr_SetString with a vprint interface */
180 static void
181 PLy_exception_set(PyObject *, const char *,...)
182 __attribute__((format(printf, 2, 3)));
183
184 /* Get the innermost python procedure called from the backend */
185 static char *PLy_procedure_name(PLyProcedure *);
186
187 /* some utility functions */
188 static void PLy_elog(int, const char *,...);
189 static char *PLy_traceback(int *);
190 static char *PLy_vprintf(const char *fmt, va_list ap);
191 static char *PLy_printf(const char *fmt,...);
192
193 static void *PLy_malloc(size_t);
194 static void *PLy_realloc(void *, size_t);
195 static char *PLy_strdup(const char *);
196 static void PLy_free(void *);
197
198 /* sub handlers for functions and triggers */
199 static Datum PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *);
200 static HeapTuple PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure *);
201
202 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *);
203 static void PLy_function_delete_args(PLyProcedure *);
204 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *,
205                                            HeapTuple *);
206 static HeapTuple PLy_modify_tuple(PLyProcedure *, PyObject *,
207                                  TriggerData *, HeapTuple);
208
209 static PyObject *PLy_procedure_call(PLyProcedure *, char *, PyObject *);
210
211 static PLyProcedure *PLy_procedure_get(FunctionCallInfo fcinfo,
212                                   Oid tgreloid);
213
214 static PLyProcedure *PLy_procedure_create(FunctionCallInfo fcinfo,
215                                          Oid tgreloid,
216                                          HeapTuple procTup, char *key);
217
218 static void PLy_procedure_compile(PLyProcedure *, const char *);
219 static char *PLy_procedure_munge_source(const char *, const char *);
220 static void PLy_procedure_delete(PLyProcedure *);
221
222 static void PLy_typeinfo_init(PLyTypeInfo *);
223 static void PLy_typeinfo_dealloc(PLyTypeInfo *);
224 static void PLy_output_datum_func(PLyTypeInfo *, HeapTuple);
225 static void PLy_output_datum_func2(PLyObToDatum *, HeapTuple);
226 static void PLy_input_datum_func(PLyTypeInfo *, Oid, HeapTuple);
227 static void PLy_input_datum_func2(PLyDatumToOb *, Oid, HeapTuple);
228 static void PLy_output_tuple_funcs(PLyTypeInfo *, TupleDesc);
229 static void PLy_input_tuple_funcs(PLyTypeInfo *, TupleDesc);
230
231 /* conversion functions */
232 static PyObject *PLyDict_FromTuple(PLyTypeInfo *, HeapTuple, TupleDesc);
233 static PyObject *PLyBool_FromString(const char *);
234 static PyObject *PLyFloat_FromString(const char *);
235 static PyObject *PLyInt_FromString(const char *);
236 static PyObject *PLyLong_FromString(const char *);
237 static PyObject *PLyString_FromString(const char *);
238
239 static HeapTuple PLyMapping_ToTuple(PLyTypeInfo *, PyObject *);
240 static HeapTuple PLySequence_ToTuple(PLyTypeInfo *, PyObject *);
241 static HeapTuple PLyObject_ToTuple(PLyTypeInfo *, PyObject *);
242
243 /*
244  * Currently active plpython function
245  */
246 static PLyProcedure *PLy_curr_procedure = NULL;
247
248 /*
249  * When a callback from Python into PG incurs an error, we temporarily store
250  * the error information here, and return NULL to the Python interpreter.
251  * Any further callback attempts immediately fail, and when the Python
252  * interpreter returns to the calling function, we re-throw the error (even if
253  * Python thinks it trapped the error and doesn't return NULL).  Eventually
254  * this ought to be improved to let Python code really truly trap the error,
255  * but that's more of a change from the pre-8.0 semantics than I have time for
256  * now --- it will only be possible if the callback query is executed inside a
257  * subtransaction.
258  */
259 static ErrorData *PLy_error_in_progress = NULL;
260
261 static PyObject *PLy_interp_globals = NULL;
262 static PyObject *PLy_interp_safe_globals = NULL;
263 static PyObject *PLy_procedure_cache = NULL;
264
265 /* Python exceptions */
266 static PyObject *PLy_exc_error = NULL;
267 static PyObject *PLy_exc_fatal = NULL;
268 static PyObject *PLy_exc_spi_error = NULL;
269
270 /* some globals for the python module */
271 static char PLy_plan_doc[] = {
272         "Store a PostgreSQL plan"
273 };
274
275 static char PLy_result_doc[] = {
276         "Results of a PostgreSQL query"
277 };
278
279
280 /*
281  * the function definitions
282  */
283
284 /*
285  * This routine is a crock, and so is everyplace that calls it.  The problem
286  * is that the cached form of plpython functions/queries is allocated permanently
287  * (mostly via malloc()) and never released until backend exit.  Subsidiary
288  * data structures such as fmgr info records therefore must live forever
289  * as well.  A better implementation would store all this stuff in a per-
290  * function memory context that could be reclaimed at need.  In the meantime,
291  * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
292  * it might allocate, and whatever the eventual function might allocate using
293  * fn_mcxt, will live forever too.
294  */
295 static void
296 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
297 {
298         fmgr_info_cxt(functionId, finfo, TopMemoryContext);
299 }
300
301 Datum
302 plpython_call_handler(PG_FUNCTION_ARGS)
303 {
304         Datum           retval;
305         PLyProcedure *save_curr_proc;
306         PLyProcedure *volatile proc = NULL;
307
308         if (SPI_connect() != SPI_OK_CONNECT)
309                 elog(ERROR, "could not connect to SPI manager");
310
311         save_curr_proc = PLy_curr_procedure;
312
313         PG_TRY();
314         {
315                 if (CALLED_AS_TRIGGER(fcinfo))
316                 {
317                         TriggerData *tdata = (TriggerData *) fcinfo->context;
318                         HeapTuple       trv;
319
320                         proc = PLy_procedure_get(fcinfo,
321                                                                          RelationGetRelid(tdata->tg_relation));
322                         PLy_curr_procedure = proc;
323                         trv = PLy_trigger_handler(fcinfo, proc);
324                         retval = PointerGetDatum(trv);
325                 }
326                 else
327                 {
328                         proc = PLy_procedure_get(fcinfo, InvalidOid);
329                         PLy_curr_procedure = proc;
330                         retval = PLy_function_handler(fcinfo, proc);
331                 }
332         }
333         PG_CATCH();
334         {
335                 PLy_curr_procedure = save_curr_proc;
336                 if (proc)
337                 {
338                         /* note: Py_DECREF needs braces around it, as of 2003/08 */
339                         Py_DECREF(proc->me);
340                 }
341                 PyErr_Clear();
342                 PG_RE_THROW();
343         }
344         PG_END_TRY();
345
346         PLy_curr_procedure = save_curr_proc;
347
348         Py_DECREF(proc->me);
349
350         return retval;
351 }
352
353 /* trigger and function sub handlers
354  *
355  * the python function is expected to return Py_None if the tuple is
356  * acceptable and unmodified.  Otherwise it should return a PyString
357  * object who's value is SKIP, or MODIFY.  SKIP means don't perform
358  * this action.  MODIFY means the tuple has been modified, so update
359  * tuple and perform action.  SKIP and MODIFY assume the trigger fires
360  * BEFORE the event and is ROW level.  postgres expects the function
361  * to take no arguments and return an argument of type trigger.
362  */
363 static HeapTuple
364 PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
365 {
366         HeapTuple       rv = NULL;
367         PyObject   *volatile plargs = NULL;
368         PyObject   *volatile plrv = NULL;
369
370         PG_TRY();
371         {
372                 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
373                 plrv = PLy_procedure_call(proc, "TD", plargs);
374
375                 Assert(plrv != NULL);
376                 Assert(!PLy_error_in_progress);
377
378                 /*
379                  * Disconnect from SPI manager
380                  */
381                 if (SPI_finish() != SPI_OK_FINISH)
382                         elog(ERROR, "SPI_finish failed");
383
384                 /*
385                  * return of None means we're happy with the tuple
386                  */
387                 if (plrv != Py_None)
388                 {
389                         char       *srv;
390
391                         if (!PyString_Check(plrv))
392                                 ereport(ERROR,
393                                                 (errcode(ERRCODE_DATA_EXCEPTION),
394                                                  errmsg("unexpected return value from trigger procedure"),
395                                                  errdetail("Expected None or a String.")));
396
397                         srv = PyString_AsString(plrv);
398                         if (pg_strcasecmp(srv, "SKIP") == 0)
399                                 rv = NULL;
400                         else if (pg_strcasecmp(srv, "MODIFY") == 0)
401                         {
402                                 TriggerData *tdata = (TriggerData *) fcinfo->context;
403
404                                 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
405                                         TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
406                                         rv = PLy_modify_tuple(proc, plargs, tdata, rv);
407                                 else
408                                         elog(WARNING, "ignoring modified tuple in DELETE trigger");
409                         }
410                         else if (pg_strcasecmp(srv, "OK") != 0)
411                         {
412                                 /*
413                                  * accept "OK" as an alternative to None; otherwise,
414                                  * raise an error
415                                  */
416                                 ereport(ERROR,
417                                                 (errcode(ERRCODE_DATA_EXCEPTION),
418                                                  errmsg("unexpected return value from trigger procedure"),
419                                                  errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
420                         }
421                 }
422         }
423         PG_CATCH();
424         {
425                 Py_XDECREF(plargs);
426                 Py_XDECREF(plrv);
427
428                 PG_RE_THROW();
429         }
430         PG_END_TRY();
431
432         Py_DECREF(plargs);
433         Py_DECREF(plrv);
434
435         return rv;
436 }
437
438 static HeapTuple
439 PLy_modify_tuple(PLyProcedure * proc, PyObject * pltd, TriggerData *tdata,
440                                  HeapTuple otup)
441 {
442         PyObject   *volatile plntup;
443         PyObject   *volatile plkeys;
444         PyObject   *volatile platt;
445         PyObject   *volatile plval;
446         PyObject   *volatile plstr;
447         HeapTuple       rtup;
448         int                     natts,
449                                 i,
450                                 attn,
451                                 atti;
452         int                *volatile modattrs;
453         Datum      *volatile modvalues;
454         char       *volatile modnulls;
455         TupleDesc       tupdesc;
456
457         plntup = plkeys = platt = plval = plstr = NULL;
458         modattrs = NULL;
459         modvalues = NULL;
460         modnulls = NULL;
461
462         PG_TRY();
463         {
464                 if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
465                         elog(ERROR, "TD[\"new\"] deleted, unable to modify tuple");
466                 if (!PyDict_Check(plntup))
467                         elog(ERROR, "TD[\"new\"] is not a dictionary object");
468                 Py_INCREF(plntup);
469
470                 plkeys = PyDict_Keys(plntup);
471                 natts = PyList_Size(plkeys);
472
473                 modattrs = (int *) palloc(natts * sizeof(int));
474                 modvalues = (Datum *) palloc(natts * sizeof(Datum));
475                 modnulls = (char *) palloc(natts * sizeof(char));
476
477                 tupdesc = tdata->tg_relation->rd_att;
478
479                 for (i = 0; i < natts; i++)
480                 {
481                         char       *src;
482
483                         platt = PyList_GetItem(plkeys, i);
484                         if (!PyString_Check(platt))
485                                 elog(ERROR, "attribute name is not a string");
486                         attn = SPI_fnumber(tupdesc, PyString_AsString(platt));
487                         if (attn == SPI_ERROR_NOATTRIBUTE)
488                                 elog(ERROR, "invalid attribute \"%s\" in tuple",
489                                          PyString_AsString(platt));
490                         atti = attn - 1;
491
492                         plval = PyDict_GetItem(plntup, platt);
493                         if (plval == NULL)
494                                 elog(FATAL, "python interpreter is probably corrupted");
495
496                         Py_INCREF(plval);
497
498                         modattrs[i] = attn;
499
500                         if (tupdesc->attrs[atti]->attisdropped)
501                         {
502                                 modvalues[i] = (Datum) 0;
503                                 modnulls[i] = 'n';
504                         }
505                         else if (plval != Py_None)
506                         {
507                                 plstr = PyObject_Str(plval);
508                                 if (!plstr)
509                                         PLy_elog(ERROR, "function \"%s\" could not modify tuple",
510                                                          proc->proname);
511                                 src = PyString_AsString(plstr);
512
513                                 modvalues[i] =
514                                         InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
515                                                                           src,
516                                                                           proc->result.out.r.atts[atti].typioparam,
517                                                                           tupdesc->attrs[atti]->atttypmod);
518                                 modnulls[i] = ' ';
519
520                                 Py_DECREF(plstr);
521                                 plstr = NULL;
522                         }
523                         else
524                         {
525                                 modvalues[i] =
526                                         InputFunctionCall(&proc->result.out.r.atts[atti].typfunc,
527                                                                           NULL,
528                                                                           proc->result.out.r.atts[atti].typioparam,
529                                                                           tupdesc->attrs[atti]->atttypmod);
530                                 modnulls[i] = 'n';
531                         }
532
533                         Py_DECREF(plval);
534                         plval = NULL;
535                 }
536
537                 rtup = SPI_modifytuple(tdata->tg_relation, otup, natts,
538                                                            modattrs, modvalues, modnulls);
539                 if (rtup == NULL)
540                         elog(ERROR, "SPI_modifytuple failed -- error %d", SPI_result);
541         }
542         PG_CATCH();
543         {
544                 Py_XDECREF(plntup);
545                 Py_XDECREF(plkeys);
546                 Py_XDECREF(plval);
547                 Py_XDECREF(plstr);
548
549                 if (modnulls)
550                         pfree(modnulls);
551                 if (modvalues)
552                         pfree(modvalues);
553                 if (modattrs)
554                         pfree(modattrs);
555
556                 PG_RE_THROW();
557         }
558         PG_END_TRY();
559
560         Py_DECREF(plntup);
561         Py_DECREF(plkeys);
562
563         pfree(modattrs);
564         pfree(modvalues);
565         pfree(modnulls);
566
567         return rtup;
568 }
569
570 static PyObject *
571 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc, HeapTuple *rv)
572 {
573         TriggerData *tdata = (TriggerData *) fcinfo->context;
574         PyObject   *pltname,
575                            *pltevent,
576                            *pltwhen,
577                            *pltlevel,
578                        *pltrelid,
579                        *plttablename,
580                        *plttableschema;
581         PyObject   *pltargs,
582                            *pytnew,
583                            *pytold;
584         PyObject   *volatile pltdata = NULL;
585         char       *stroid;
586
587         PG_TRY();
588         {
589                 pltdata = PyDict_New();
590                 if (!pltdata)
591                         PLy_elog(ERROR, "could not build arguments for trigger procedure");
592
593                 pltname = PyString_FromString(tdata->tg_trigger->tgname);
594                 PyDict_SetItemString(pltdata, "name", pltname);
595                 Py_DECREF(pltname);
596
597                 stroid = DatumGetCString(DirectFunctionCall1(oidout,
598                                                            ObjectIdGetDatum(tdata->tg_relation->rd_id)));
599                 pltrelid = PyString_FromString(stroid);
600                 PyDict_SetItemString(pltdata, "relid", pltrelid);
601                 Py_DECREF(pltrelid);
602                 pfree(stroid);
603
604                 stroid = SPI_getrelname(tdata->tg_relation);
605                 plttablename = PyString_FromString(stroid);
606                 PyDict_SetItemString(pltdata, "table_name", plttablename);
607                 Py_DECREF(plttablename);
608                 pfree(stroid);
609                 
610                 stroid = SPI_getnspname(tdata->tg_relation);
611                 plttableschema = PyString_FromString(stroid);
612                 PyDict_SetItemString(pltdata, "table_schema", plttableschema);
613                 Py_DECREF(plttableschema);
614                 pfree(stroid);
615                 
616
617                 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
618                         pltwhen = PyString_FromString("BEFORE");
619                 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
620                         pltwhen = PyString_FromString("AFTER");
621                 else
622                 {
623                         elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
624                         pltwhen = NULL;         /* keep compiler quiet */
625                 }
626                 PyDict_SetItemString(pltdata, "when", pltwhen);
627                 Py_DECREF(pltwhen);
628
629                 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
630                 {
631                         pltlevel = PyString_FromString("ROW");
632                         PyDict_SetItemString(pltdata, "level", pltlevel);
633                         Py_DECREF(pltlevel);
634
635                         if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
636                         {
637                                 pltevent = PyString_FromString("INSERT");
638
639                                 PyDict_SetItemString(pltdata, "old", Py_None);
640                                 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
641                                                                                    tdata->tg_relation->rd_att);
642                                 PyDict_SetItemString(pltdata, "new", pytnew);
643                                 Py_DECREF(pytnew);
644                                 *rv = tdata->tg_trigtuple;
645                         }
646                         else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
647                         {
648                                 pltevent = PyString_FromString("DELETE");
649
650                                 PyDict_SetItemString(pltdata, "new", Py_None);
651                                 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
652                                                                                    tdata->tg_relation->rd_att);
653                                 PyDict_SetItemString(pltdata, "old", pytold);
654                                 Py_DECREF(pytold);
655                                 *rv = tdata->tg_trigtuple;
656                         }
657                         else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
658                         {
659                                 pltevent = PyString_FromString("UPDATE");
660
661                                 pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
662                                                                                    tdata->tg_relation->rd_att);
663                                 PyDict_SetItemString(pltdata, "new", pytnew);
664                                 Py_DECREF(pytnew);
665                                 pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
666                                                                                    tdata->tg_relation->rd_att);
667                                 PyDict_SetItemString(pltdata, "old", pytold);
668                                 Py_DECREF(pytold);
669                                 *rv = tdata->tg_newtuple;
670                         }
671                         else
672                         {
673                                 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
674                                 pltevent = NULL;        /* keep compiler quiet */
675                         }
676
677                         PyDict_SetItemString(pltdata, "event", pltevent);
678                         Py_DECREF(pltevent);
679                 }
680                 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
681                 {
682                         pltlevel = PyString_FromString("STATEMENT");
683                         PyDict_SetItemString(pltdata, "level", pltlevel);
684                         Py_DECREF(pltlevel);
685
686                         PyDict_SetItemString(pltdata, "old", Py_None);
687                         PyDict_SetItemString(pltdata, "new", Py_None);
688                         *rv = NULL;
689
690                         if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
691                                 pltevent = PyString_FromString("INSERT");
692                         else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
693                                 pltevent = PyString_FromString("DELETE");
694                         else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
695                                 pltevent = PyString_FromString("UPDATE");
696                         else
697                         {
698                                 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
699                                 pltevent = NULL;        /* keep compiler quiet */
700                         }
701
702                         PyDict_SetItemString(pltdata, "event", pltevent);
703                         Py_DECREF(pltevent);
704                 }
705                 else
706                         elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
707
708                 if (tdata->tg_trigger->tgnargs)
709                 {
710                         /*
711                          * all strings...
712                          */
713                         int                     i;
714                         PyObject   *pltarg;
715
716                         pltargs = PyList_New(tdata->tg_trigger->tgnargs);
717                         for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
718                         {
719                                 pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
720
721                                 /*
722                                  * stolen, don't Py_DECREF
723                                  */
724                                 PyList_SetItem(pltargs, i, pltarg);
725                         }
726                 }
727                 else
728                 {
729                         Py_INCREF(Py_None);
730                         pltargs = Py_None;
731                 }
732                 PyDict_SetItemString(pltdata, "args", pltargs);
733                 Py_DECREF(pltargs);
734         }
735         PG_CATCH();
736         {
737                 Py_XDECREF(pltdata);
738                 PG_RE_THROW();
739         }
740         PG_END_TRY();
741
742         return pltdata;
743 }
744
745
746
747 /* function handler and friends */
748 static Datum
749 PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
750 {
751         Datum           rv;
752         PyObject   *volatile plargs = NULL;
753         PyObject   *volatile plrv = NULL;
754         PyObject   *volatile plrv_so = NULL;
755         char       *plrv_sc;
756
757         PG_TRY();
758         {
759                 if (!proc->is_setof || proc->setof == NULL)
760                 {
761                         /* Simple type returning function or first time for SETOF function */
762                         plargs = PLy_function_build_args(fcinfo, proc);
763                         plrv = PLy_procedure_call(proc, "args", plargs);
764                         if (!proc->is_setof)
765                                 /* SETOF function parameters will be deleted when last row is returned */
766                                 PLy_function_delete_args(proc);
767                         Assert(plrv != NULL);
768                         Assert(!PLy_error_in_progress);
769                 }
770
771                 /*
772                  * Disconnect from SPI manager and then create the return values datum
773                  * (if the input function does a palloc for it this must not be
774                  * allocated in the SPI memory context because SPI_finish would free
775                  * it).
776                  */
777                 if (SPI_finish() != SPI_OK_FINISH)
778                         elog(ERROR, "SPI_finish failed");
779
780                 if (proc->is_setof)
781                 {
782                         bool has_error = false;
783                         ReturnSetInfo *rsi = (ReturnSetInfo *)fcinfo->resultinfo;
784
785                         if (proc->setof == NULL)
786                         {
787                                 /* first time -- do checks and setup */
788                                 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
789                                                 (rsi->allowedModes & SFRM_ValuePerCall) == 0)
790                                 {
791                                         ereport(ERROR,
792                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
793                                                          errmsg("only value per call is allowed")));
794                                 }
795                                 rsi->returnMode = SFRM_ValuePerCall;
796
797                                 /* Make iterator out of returned object */
798                                 proc->setof = PyObject_GetIter(plrv);
799                                 Py_DECREF(plrv);
800                                 plrv = NULL;
801
802                                 if (proc->setof == NULL)
803                                         ereport(ERROR,
804                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
805                                                          errmsg("returned object can not be iterated"),
806                                                          errdetail("SETOF must be returned as iterable object")));
807                         }
808
809                         /* Fetch next from iterator */
810                         plrv = PyIter_Next(proc->setof);
811                         if (plrv)
812                                 rsi->isDone = ExprMultipleResult;
813                         else
814                         {
815                                 rsi->isDone = ExprEndResult;
816                                 has_error = PyErr_Occurred() != NULL;
817                         }
818
819                         if (rsi->isDone == ExprEndResult)
820                         {
821                                 /* Iterator is exhausted or error happened */
822                                 Py_DECREF(proc->setof);
823                                 proc->setof = NULL;
824
825                                 Py_XDECREF(plargs);
826                                 Py_XDECREF(plrv);
827                                 Py_XDECREF(plrv_so);
828
829                                 PLy_function_delete_args(proc);
830
831                                 if (has_error)
832                                         ereport(ERROR,
833                                                         (errcode(ERRCODE_DATA_EXCEPTION),
834                                                          errmsg("error fetching next item from iterator")));
835
836                                 fcinfo->isnull = true;
837                                 return (Datum)NULL;
838                         }
839                 }
840
841                 /*
842                  * If the function is declared to return void, the Python
843                  * return value must be None. For void-returning functions, we
844                  * also treat a None return value as a special "void datum"
845                  * rather than NULL (as is the case for non-void-returning
846                  * functions).
847                  */
848                 if (proc->result.out.d.typoid == VOIDOID)
849                 {
850                         if (plrv != Py_None)
851                                 ereport(ERROR,
852                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
853                                                  errmsg("invalid return value from plpython function"),
854                                                  errdetail("Functions returning type \"void\" must return None.")));
855
856                         fcinfo->isnull = false;
857                         rv = (Datum) 0;
858                 }
859                 else if (plrv == Py_None)
860                 {
861                         fcinfo->isnull = true;
862                         if (proc->result.is_rowtype < 1)
863                                 rv = InputFunctionCall(&proc->result.out.d.typfunc,
864                                                 NULL,
865                                                 proc->result.out.d.typioparam,
866                                                 -1);
867                         else
868                                 /* Tuple as None */
869                                 rv = (Datum) NULL;
870                 }
871                 else if (proc->result.is_rowtype >= 1)
872                 {
873                         HeapTuple   tuple = NULL;
874
875                         if (PySequence_Check(plrv))
876                                 /* composite type as sequence (tuple, list etc) */
877                                 tuple = PLySequence_ToTuple(&proc->result, plrv);
878                         else if (PyMapping_Check(plrv))
879                                 /* composite type as mapping (currently only dict) */
880                                 tuple = PLyMapping_ToTuple(&proc->result, plrv);
881                         else
882                                 /* returned as smth, must provide method __getattr__(name) */
883                                 tuple = PLyObject_ToTuple(&proc->result, plrv);
884
885                         if (tuple != NULL)
886                         {
887                                 fcinfo->isnull = false;
888                                 rv = HeapTupleGetDatum(tuple);
889                         }
890                         else
891                         {
892                                 fcinfo->isnull = true;
893                                 rv = (Datum) NULL;
894                         }
895                 }
896                 else
897                 {
898                         fcinfo->isnull = false;
899                         plrv_so = PyObject_Str(plrv);
900                         if (!plrv_so)
901                                 PLy_elog(ERROR, "function \"%s\" could not create return value", proc->proname);
902                         plrv_sc = PyString_AsString(plrv_so);
903                         rv = InputFunctionCall(&proc->result.out.d.typfunc,
904                                                                    plrv_sc,
905                                                                    proc->result.out.d.typioparam,
906                                                                    -1);
907                 }
908         }
909         PG_CATCH();
910         {
911                 Py_XDECREF(plargs);
912                 Py_XDECREF(plrv);
913                 Py_XDECREF(plrv_so);
914
915                 PG_RE_THROW();
916         }
917         PG_END_TRY();
918
919         Py_XDECREF(plargs);
920         Py_DECREF(plrv);
921         Py_XDECREF(plrv_so);
922
923         return rv;
924 }
925
926 static PyObject *
927 PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
928 {
929         PyObject   *rv;
930
931         PyDict_SetItemString(proc->globals, kargs, vargs);
932         rv = PyEval_EvalCode((PyCodeObject *) proc->code,
933                                                  proc->globals, proc->globals);
934
935         /*
936          * If there was an error in a PG callback, propagate that no matter what
937          * Python claims about its success.
938          */
939         if (PLy_error_in_progress)
940         {
941                 ErrorData  *edata = PLy_error_in_progress;
942
943                 PLy_error_in_progress = NULL;
944                 ReThrowError(edata);
945         }
946
947         if (rv == NULL || PyErr_Occurred())
948         {
949                 Py_XDECREF(rv);
950                 PLy_elog(ERROR, "function \"%s\" failed", proc->proname);
951         }
952
953         return rv;
954 }
955
956 static PyObject *
957 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
958 {
959         PyObject   *volatile arg = NULL;
960         PyObject   *volatile args = NULL;
961         int                     i;
962
963         PG_TRY();
964         {
965                 args = PyList_New(proc->nargs);
966                 for (i = 0; i < proc->nargs; i++)
967                 {
968                         if (proc->args[i].is_rowtype > 0)
969                         {
970                                 if (fcinfo->argnull[i])
971                                         arg = NULL;
972                                 else
973                                 {
974                                         HeapTupleHeader td;
975                                         Oid                     tupType;
976                                         int32           tupTypmod;
977                                         TupleDesc       tupdesc;
978                                         HeapTupleData tmptup;
979
980                                         td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
981                                         /* Extract rowtype info and find a tupdesc */
982                                         tupType = HeapTupleHeaderGetTypeId(td);
983                                         tupTypmod = HeapTupleHeaderGetTypMod(td);
984                                         tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
985
986                                         /* Set up I/O funcs if not done yet */
987                                         if (proc->args[i].is_rowtype != 1)
988                                                 PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
989
990                                         /* Build a temporary HeapTuple control structure */
991                                         tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
992                                         tmptup.t_data = td;
993
994                                         arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
995                                         ReleaseTupleDesc(tupdesc);
996                                 }
997                         }
998                         else
999                         {
1000                                 if (fcinfo->argnull[i])
1001                                         arg = NULL;
1002                                 else
1003                                 {
1004                                         char       *ct;
1005
1006                                         ct = OutputFunctionCall(&(proc->args[i].in.d.typfunc),
1007                                                                                         fcinfo->arg[i]);
1008                                         arg = (proc->args[i].in.d.func) (ct);
1009                                         pfree(ct);
1010                                 }
1011                         }
1012
1013                         if (arg == NULL)
1014                         {
1015                                 Py_INCREF(Py_None);
1016                                 arg = Py_None;
1017                         }
1018
1019                         if (PyList_SetItem(args, i, arg) == -1 ||
1020                                         (proc->argnames &&
1021                                          PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1))
1022                                 PLy_elog(ERROR, "problem setting up arguments for \"%s\"", proc->proname);
1023                         arg = NULL;
1024                 }
1025         }
1026         PG_CATCH();
1027         {
1028                 Py_XDECREF(arg);
1029                 Py_XDECREF(args);
1030
1031                 PG_RE_THROW();
1032         }
1033         PG_END_TRY();
1034
1035         return args;
1036 }
1037
1038
1039 static void
1040 PLy_function_delete_args(PLyProcedure *proc)
1041 {
1042         int     i;
1043
1044         if (!proc->argnames)
1045                 return;
1046
1047         for (i = 0;  i < proc->nargs;  i++)
1048                 PyDict_DelItemString(proc->globals, proc->argnames[i]);
1049 }
1050
1051
1052 /*
1053  * PLyProcedure functions
1054  */
1055
1056 /* PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
1057  * returns a new PLyProcedure.  fcinfo is the call info, tgreloid is the
1058  * relation OID when calling a trigger, or InvalidOid (zero) for ordinary
1059  * function calls.
1060  */
1061 static PLyProcedure *
1062 PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
1063 {
1064         Oid                     fn_oid;
1065         HeapTuple       procTup;
1066         char            key[128];
1067         PyObject   *plproc;
1068         PLyProcedure *proc = NULL;
1069         int                     rv;
1070
1071         fn_oid = fcinfo->flinfo->fn_oid;
1072         procTup = SearchSysCache(PROCOID,
1073                                                          ObjectIdGetDatum(fn_oid),
1074                                                          0, 0, 0);
1075         if (!HeapTupleIsValid(procTup))
1076                 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1077
1078         rv = snprintf(key, sizeof(key), "%u_%u", fn_oid, tgreloid);
1079         if (rv >= sizeof(key) || rv < 0)
1080                 elog(ERROR, "key too long");
1081
1082         plproc = PyDict_GetItemString(PLy_procedure_cache, key);
1083
1084         if (plproc != NULL)
1085         {
1086                 Py_INCREF(plproc);
1087                 if (!PyCObject_Check(plproc))
1088                         elog(FATAL, "expected a PyCObject, didn't get one");
1089
1090                 proc = PyCObject_AsVoidPtr(plproc);
1091                 if (proc->me != plproc)
1092                         elog(FATAL, "proc->me != plproc");
1093                 /* did we find an up-to-date cache entry? */
1094                 if (proc->fn_xmin != HeapTupleHeaderGetXmin(procTup->t_data) ||
1095                         proc->fn_cmin != HeapTupleHeaderGetCmin(procTup->t_data))
1096                 {
1097                         Py_DECREF(plproc);
1098                         proc = NULL;
1099                 }
1100         }
1101
1102         if (proc == NULL)
1103                 proc = PLy_procedure_create(fcinfo, tgreloid, procTup, key);
1104
1105         ReleaseSysCache(procTup);
1106
1107         return proc;
1108 }
1109
1110 static PLyProcedure *
1111 PLy_procedure_create(FunctionCallInfo fcinfo, Oid tgreloid,
1112                                          HeapTuple procTup, char *key)
1113 {
1114         char            procName[NAMEDATALEN + 256];
1115         Form_pg_proc procStruct;
1116         PLyProcedure *volatile proc;
1117         char       *volatile procSource = NULL;
1118         Datum           prosrcdatum;
1119         bool            isnull;
1120         int                     i,
1121                                 rv;
1122         Datum           argnames;
1123         Datum           *elems;
1124         int             nelems;
1125
1126         procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1127
1128         if (OidIsValid(tgreloid))
1129                 rv = snprintf(procName, sizeof(procName),
1130                                           "__plpython_procedure_%s_%u_trigger_%u",
1131                                           NameStr(procStruct->proname),
1132                                           fcinfo->flinfo->fn_oid,
1133                                           tgreloid);
1134         else
1135                 rv = snprintf(procName, sizeof(procName),
1136                                           "__plpython_procedure_%s_%u",
1137                                           NameStr(procStruct->proname),
1138                                           fcinfo->flinfo->fn_oid);
1139         if (rv >= sizeof(procName) || rv < 0)
1140                 elog(ERROR, "procedure name would overrun buffer");
1141
1142         proc = PLy_malloc(sizeof(PLyProcedure));
1143         proc->proname = PLy_strdup(NameStr(procStruct->proname));
1144         proc->pyname = PLy_strdup(procName);
1145         proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1146         proc->fn_cmin = HeapTupleHeaderGetCmin(procTup->t_data);
1147         /* Remember if function is STABLE/IMMUTABLE */
1148         proc->fn_readonly =
1149                 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1150         PLy_typeinfo_init(&proc->result);
1151         for (i = 0; i < FUNC_MAX_ARGS; i++)
1152                 PLy_typeinfo_init(&proc->args[i]);
1153         proc->nargs = 0;
1154         proc->code = proc->statics = NULL;
1155         proc->globals = proc->me = NULL;
1156         proc->is_setof = procStruct->proretset;
1157         proc->setof = NULL;
1158         proc->argnames = NULL;
1159
1160         PG_TRY();
1161         {
1162                 /*
1163                  * get information required for output conversion of the return value,
1164                  * but only if this isn't a trigger.
1165                  */
1166                 if (!CALLED_AS_TRIGGER(fcinfo))
1167                 {
1168                         HeapTuple       rvTypeTup;
1169                         Form_pg_type rvTypeStruct;
1170
1171                         rvTypeTup = SearchSysCache(TYPEOID,
1172                                                                         ObjectIdGetDatum(procStruct->prorettype),
1173                                                                            0, 0, 0);
1174                         if (!HeapTupleIsValid(rvTypeTup))
1175                                 elog(ERROR, "cache lookup failed for type %u",
1176                                          procStruct->prorettype);
1177                         rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
1178
1179                         /* Disallow pseudotype result, except for void */
1180                         if (rvTypeStruct->typtype == 'p' &&
1181                                 procStruct->prorettype != VOIDOID)
1182                         {
1183                                 if (procStruct->prorettype == TRIGGEROID)
1184                                         ereport(ERROR,
1185                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1186                                                          errmsg("trigger functions may only be called as triggers")));
1187                                 else
1188                                         ereport(ERROR,
1189                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1190                                                    errmsg("plpython functions cannot return type %s",
1191                                                                   format_type_be(procStruct->prorettype))));
1192                         }
1193
1194                         if (rvTypeStruct->typtype == 'c')
1195                         {
1196                                 /* Tuple: set up later, during first call to PLy_function_handler */
1197                                 proc->result.out.d.typoid = procStruct->prorettype;
1198                                 proc->result.is_rowtype = 2;
1199                         }
1200                         else
1201                                 PLy_output_datum_func(&proc->result, rvTypeTup);
1202
1203                         ReleaseSysCache(rvTypeTup);
1204                 }
1205                 else
1206                 {
1207                         /*
1208                          * input/output conversion for trigger tuples.  use the result
1209                          * TypeInfo variable to store the tuple conversion info.
1210                          */
1211                         TriggerData *tdata = (TriggerData *) fcinfo->context;
1212
1213                         PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1214                         PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
1215                 }
1216
1217                 /*
1218                  * now get information required for input conversion of the procedure's
1219                  * arguments.
1220                  */
1221                 proc->nargs = fcinfo->nargs;
1222                 if (proc->nargs)
1223                 {
1224                         argnames = SysCacheGetAttr(PROCOID, procTup, Anum_pg_proc_proargnames, &isnull);
1225                         if (!isnull)
1226                         {
1227                                 deconstruct_array(DatumGetArrayTypeP(argnames), TEXTOID, -1, false, 'i',
1228                                                 &elems, NULL, &nelems);
1229                                 if (nelems != proc->nargs)
1230                                         elog(ERROR,
1231                                                         "proargnames must have the same number of elements "
1232                                                         "as the function has arguments");
1233                                 proc->argnames = (char **) PLy_malloc(sizeof(char *)*proc->nargs);
1234                         }
1235                 }
1236                 for (i = 0; i < fcinfo->nargs; i++)
1237                 {
1238                         HeapTuple       argTypeTup;
1239                         Form_pg_type argTypeStruct;
1240
1241                         argTypeTup = SearchSysCache(TYPEOID,
1242                                                  ObjectIdGetDatum(procStruct->proargtypes.values[i]),
1243                                                                                 0, 0, 0);
1244                         if (!HeapTupleIsValid(argTypeTup))
1245                                 elog(ERROR, "cache lookup failed for type %u",
1246                                          procStruct->proargtypes.values[i]);
1247                         argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
1248
1249                         /* Disallow pseudotype argument */
1250                         if (argTypeStruct->typtype == 'p')
1251                                 ereport(ERROR,
1252                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1253                                                  errmsg("plpython functions cannot take type %s",
1254                                                 format_type_be(procStruct->proargtypes.values[i]))));
1255
1256                         if (argTypeStruct->typtype != 'c')
1257                                 PLy_input_datum_func(&(proc->args[i]),
1258                                                                          procStruct->proargtypes.values[i],
1259                                                                          argTypeTup);
1260                         else
1261                                 proc->args[i].is_rowtype = 2;   /* still need to set I/O funcs */
1262
1263                         ReleaseSysCache(argTypeTup);
1264
1265                         /* Fetch argument name */
1266                         if (proc->argnames)
1267                                 proc->argnames[i] = PLy_strdup(DatumGetCString(DirectFunctionCall1(textout, elems[i])));
1268                 }
1269
1270                 /*
1271                  * get the text of the function.
1272                  */
1273                 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1274                                                                           Anum_pg_proc_prosrc, &isnull);
1275                 if (isnull)
1276                         elog(ERROR, "null prosrc");
1277                 procSource = DatumGetCString(DirectFunctionCall1(textout,
1278                                                                                                                  prosrcdatum));
1279
1280                 PLy_procedure_compile(proc, procSource);
1281
1282                 pfree(procSource);
1283
1284                 proc->me = PyCObject_FromVoidPtr(proc, NULL);
1285                 PyDict_SetItemString(PLy_procedure_cache, key, proc->me);
1286         }
1287         PG_CATCH();
1288         {
1289                 PLy_procedure_delete(proc);
1290                 if (procSource)
1291                         pfree(procSource);
1292
1293                 PG_RE_THROW();
1294         }
1295         PG_END_TRY();
1296
1297         return proc;
1298 }
1299
1300 static void
1301 PLy_procedure_compile(PLyProcedure * proc, const char *src)
1302 {
1303         PyObject   *crv = NULL;
1304         char       *msrc;
1305
1306         proc->globals = PyDict_Copy(PLy_interp_globals);
1307
1308         /*
1309          * SD is private preserved data between calls. GD is global data
1310          * shared by all functions
1311          */
1312         proc->statics = PyDict_New();
1313         PyDict_SetItemString(proc->globals, "SD", proc->statics);
1314
1315         /*
1316          * insert the function code into the interpreter
1317          */
1318         msrc = PLy_procedure_munge_source(proc->pyname, src);
1319         crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1320         free(msrc);
1321
1322         if (crv != NULL && (!PyErr_Occurred()))
1323         {
1324                 int                     clen;
1325                 char            call[NAMEDATALEN + 256];
1326
1327                 Py_DECREF(crv);
1328
1329                 /*
1330                  * compile a call to the function
1331                  */
1332                 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1333                 if (clen < 0 || clen >= sizeof(call))
1334                         elog(ERROR, "string would overflow buffer");
1335                 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1336                 if (proc->code != NULL && (!PyErr_Occurred()))
1337                         return;
1338         }
1339         else
1340                 Py_XDECREF(crv);
1341
1342         PLy_elog(ERROR, "could not compile function \"%s\"", proc->proname);
1343 }
1344
1345 static char *
1346 PLy_procedure_munge_source(const char *name, const char *src)
1347 {
1348         char       *mrc,
1349                            *mp;
1350         const char *sp;
1351         size_t          mlen,
1352                                 plen;
1353
1354         /*
1355          * room for function source and the def statement
1356          */
1357         mlen = (strlen(src) * 2) + strlen(name) + 16;
1358
1359         mrc = PLy_malloc(mlen);
1360         plen = snprintf(mrc, mlen, "def %s():\n\t", name);
1361         Assert(plen >= 0 && plen < mlen);
1362
1363         sp = src;
1364         mp = mrc + plen;
1365
1366         while (*sp != '\0')
1367         {
1368                 if (*sp == '\r' && *(sp + 1) == '\n')
1369                         sp++;
1370
1371                 if (*sp == '\n' || *sp == '\r')
1372                 {
1373                         *mp++ = '\n';
1374                         *mp++ = '\t';
1375                         sp++;
1376                 }
1377                 else
1378                         *mp++ = *sp++;
1379         }
1380         *mp++ = '\n';
1381         *mp++ = '\n';
1382         *mp = '\0';
1383
1384         if (mp > (mrc + mlen))
1385                 elog(FATAL, "buffer overrun in PLy_munge_source");
1386
1387         return mrc;
1388 }
1389
1390 static void
1391 PLy_procedure_delete(PLyProcedure * proc)
1392 {
1393         int                     i;
1394
1395         Py_XDECREF(proc->code);
1396         Py_XDECREF(proc->statics);
1397         Py_XDECREF(proc->globals);
1398         Py_XDECREF(proc->me);
1399         if (proc->proname)
1400                 PLy_free(proc->proname);
1401         if (proc->pyname)
1402                 PLy_free(proc->pyname);
1403         for (i = 0; i < proc->nargs; i++)
1404         {
1405                 if (proc->args[i].is_rowtype == 1)
1406                 {
1407                         if (proc->args[i].in.r.atts)
1408                                 PLy_free(proc->args[i].in.r.atts);
1409                         if (proc->args[i].out.r.atts)
1410                                 PLy_free(proc->args[i].out.r.atts);
1411                 }
1412                 if (proc->argnames && proc->argnames[i])
1413                         PLy_free(proc->argnames[i]);
1414         }
1415         if (proc->argnames)
1416                 PLy_free(proc->argnames);
1417 }
1418
1419 /* conversion functions.  remember output from python is
1420  * input to postgresql, and vis versa.
1421  */
1422 static void
1423 PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1424 {
1425         int                     i;
1426
1427         if (arg->is_rowtype == 0)
1428                 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1429
1430         arg->is_rowtype = 1;
1431         arg->in.r.natts = desc->natts;
1432         arg->in.r.atts = PLy_malloc(desc->natts * sizeof(PLyDatumToOb));
1433
1434         for (i = 0; i < desc->natts; i++)
1435         {
1436                 HeapTuple       typeTup;
1437
1438                 if (desc->attrs[i]->attisdropped)
1439                         continue;
1440
1441                 typeTup = SearchSysCache(TYPEOID,
1442                                                                  ObjectIdGetDatum(desc->attrs[i]->atttypid),
1443                                                                  0, 0, 0);
1444                 if (!HeapTupleIsValid(typeTup))
1445                         elog(ERROR, "cache lookup failed for type %u",
1446                                  desc->attrs[i]->atttypid);
1447
1448                 PLy_input_datum_func2(&(arg->in.r.atts[i]),
1449                                                           desc->attrs[i]->atttypid,
1450                                                           typeTup);
1451
1452                 ReleaseSysCache(typeTup);
1453         }
1454 }
1455
1456 static void
1457 PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1458 {
1459         int                     i;
1460
1461         if (arg->is_rowtype == 0)
1462                 elog(ERROR, "PLyTypeInfo struct is initialized for a Datum");
1463
1464         arg->is_rowtype = 1;
1465         arg->out.r.natts = desc->natts;
1466         arg->out.r.atts = PLy_malloc(desc->natts * sizeof(PLyDatumToOb));
1467
1468         for (i = 0; i < desc->natts; i++)
1469         {
1470                 HeapTuple       typeTup;
1471
1472                 if (desc->attrs[i]->attisdropped)
1473                         continue;
1474
1475                 typeTup = SearchSysCache(TYPEOID,
1476                                                                  ObjectIdGetDatum(desc->attrs[i]->atttypid),
1477                                                                  0, 0, 0);
1478                 if (!HeapTupleIsValid(typeTup))
1479                         elog(ERROR, "cache lookup failed for type %u",
1480                                  desc->attrs[i]->atttypid);
1481
1482                 PLy_output_datum_func2(&(arg->out.r.atts[i]), typeTup);
1483
1484                 ReleaseSysCache(typeTup);
1485         }
1486 }
1487
1488 static void
1489 PLy_output_datum_func(PLyTypeInfo * arg, HeapTuple typeTup)
1490 {
1491         if (arg->is_rowtype > 0)
1492                 elog(ERROR, "PLyTypeInfo struct is initialized for a Tuple");
1493         arg->is_rowtype = 0;
1494         PLy_output_datum_func2(&(arg->out.d), typeTup);
1495 }
1496
1497 static void
1498 PLy_output_datum_func2(PLyObToDatum * arg, HeapTuple typeTup)
1499 {
1500         Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1501
1502         perm_fmgr_info(typeStruct->typinput, &arg->typfunc);
1503         arg->typoid = HeapTupleGetOid(typeTup);
1504         arg->typioparam = getTypeIOParam(typeTup);
1505         arg->typbyval = typeStruct->typbyval;
1506 }
1507
1508 static void
1509 PLy_input_datum_func(PLyTypeInfo * arg, Oid typeOid, HeapTuple typeTup)
1510 {
1511         if (arg->is_rowtype > 0)
1512                 elog(ERROR, "PLyTypeInfo struct is initialized for Tuple");
1513         arg->is_rowtype = 0;
1514         PLy_input_datum_func2(&(arg->in.d), typeOid, typeTup);
1515 }
1516
1517 static void
1518 PLy_input_datum_func2(PLyDatumToOb * arg, Oid typeOid, HeapTuple typeTup)
1519 {
1520         Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1521
1522         /* Get the type's conversion information */
1523         perm_fmgr_info(typeStruct->typoutput, &arg->typfunc);
1524         arg->typioparam = getTypeIOParam(typeTup);
1525         arg->typbyval = typeStruct->typbyval;
1526
1527         /* Determine which kind of Python object we will convert to */
1528         switch (typeOid)
1529         {
1530                 case BOOLOID:
1531                         arg->func = PLyBool_FromString;
1532                         break;
1533                 case FLOAT4OID:
1534                 case FLOAT8OID:
1535                 case NUMERICOID:
1536                         arg->func = PLyFloat_FromString;
1537                         break;
1538                 case INT2OID:
1539                 case INT4OID:
1540                         arg->func = PLyInt_FromString;
1541                         break;
1542                 case INT8OID:
1543                         arg->func = PLyLong_FromString;
1544                         break;
1545                 default:
1546                         arg->func = PLyString_FromString;
1547                         break;
1548         }
1549 }
1550
1551 static void
1552 PLy_typeinfo_init(PLyTypeInfo * arg)
1553 {
1554         arg->is_rowtype = -1;
1555         arg->in.r.natts = arg->out.r.natts = 0;
1556         arg->in.r.atts = NULL;
1557         arg->out.r.atts = NULL;
1558 }
1559
1560 static void
1561 PLy_typeinfo_dealloc(PLyTypeInfo * arg)
1562 {
1563         if (arg->is_rowtype == 1)
1564         {
1565                 if (arg->in.r.atts)
1566                         PLy_free(arg->in.r.atts);
1567                 if (arg->out.r.atts)
1568                         PLy_free(arg->out.r.atts);
1569         }
1570 }
1571
1572 /* assumes that a bool is always returned as a 't' or 'f' */
1573 static PyObject *
1574 PLyBool_FromString(const char *src)
1575 {
1576         if (src[0] == 't')
1577                 return PyInt_FromLong(1);
1578         return PyInt_FromLong(0);
1579 }
1580
1581 static PyObject *
1582 PLyFloat_FromString(const char *src)
1583 {
1584         double          v;
1585         char       *eptr;
1586
1587         errno = 0;
1588         v = strtod(src, &eptr);
1589         if (*eptr != '\0' || errno)
1590                 return NULL;
1591         return PyFloat_FromDouble(v);
1592 }
1593
1594 static PyObject *
1595 PLyInt_FromString(const char *src)
1596 {
1597         long            v;
1598         char       *eptr;
1599
1600         errno = 0;
1601         v = strtol(src, &eptr, 0);
1602         if (*eptr != '\0' || errno)
1603                 return NULL;
1604         return PyInt_FromLong(v);
1605 }
1606
1607 static PyObject *
1608 PLyLong_FromString(const char *src)
1609 {
1610         return PyLong_FromString((char *) src, NULL, 0);
1611 }
1612
1613 static PyObject *
1614 PLyString_FromString(const char *src)
1615 {
1616         return PyString_FromString(src);
1617 }
1618
1619 static PyObject *
1620 PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
1621 {
1622         PyObject   *volatile dict;
1623         int                     i;
1624
1625         if (info->is_rowtype != 1)
1626                 elog(ERROR, "PLyTypeInfo structure describes a datum");
1627
1628         dict = PyDict_New();
1629         if (dict == NULL)
1630                 PLy_elog(ERROR, "could not create tuple dictionary");
1631
1632         PG_TRY();
1633         {
1634                 for (i = 0; i < info->in.r.natts; i++)
1635                 {
1636                         char       *key,
1637                                            *vsrc;
1638                         Datum           vattr;
1639                         bool            is_null;
1640                         PyObject   *value;
1641
1642                         if (desc->attrs[i]->attisdropped)
1643                                 continue;
1644
1645                         key = NameStr(desc->attrs[i]->attname);
1646                         vattr = heap_getattr(tuple, (i + 1), desc, &is_null);
1647
1648                         if (is_null || info->in.r.atts[i].func == NULL)
1649                                 PyDict_SetItemString(dict, key, Py_None);
1650                         else
1651                         {
1652                                 vsrc = OutputFunctionCall(&info->in.r.atts[i].typfunc,
1653                                                                                   vattr);
1654
1655                                 /*
1656                                  * no exceptions allowed
1657                                  */
1658                                 value = info->in.r.atts[i].func(vsrc);
1659                                 pfree(vsrc);
1660                                 PyDict_SetItemString(dict, key, value);
1661                                 Py_DECREF(value);
1662                         }
1663                 }
1664         }
1665         PG_CATCH();
1666         {
1667                 Py_DECREF(dict);
1668                 PG_RE_THROW();
1669         }
1670         PG_END_TRY();
1671
1672         return dict;
1673 }
1674
1675
1676 static HeapTuple
1677 PLyMapping_ToTuple(PLyTypeInfo *info, PyObject *mapping)
1678 {
1679         TupleDesc       desc;
1680         HeapTuple       tuple;
1681         Datum           *values;
1682         char            *nulls;
1683         int             i;
1684
1685         Assert(PyMapping_Check(mapping));
1686
1687         desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1688         if (info->is_rowtype == 2)
1689                 PLy_output_tuple_funcs(info, desc);
1690         Assert(info->is_rowtype == 1);
1691
1692         /* Build tuple */
1693         values = palloc(sizeof(Datum)*desc->natts);
1694         nulls = palloc(sizeof(char)*desc->natts);
1695         for (i = 0;  i < desc->natts;  ++i)
1696         {
1697                 char            *key;
1698                 PyObject        *value,
1699                                 *so;
1700
1701                 key = NameStr(desc->attrs[i]->attname);
1702                 value = so = NULL;
1703                 PG_TRY();
1704                 {
1705                         value = PyMapping_GetItemString(mapping, key);
1706                         if (value == Py_None)
1707                         {
1708                                 values[i] = (Datum) NULL;
1709                                 nulls[i] = 'n';
1710                         }
1711                         else if (value)
1712                         {
1713                                 char *valuestr;
1714
1715                                 so = PyObject_Str(value);
1716                                 if (so == NULL)
1717                                         PLy_elog(ERROR, "can't convert mapping type");
1718                                 valuestr = PyString_AsString(so);
1719
1720                                 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1721                                                 , valuestr
1722                                                 , info->out.r.atts[i].typioparam
1723                                                 , -1);
1724                                 Py_DECREF(so);
1725                                 so = NULL;
1726                                 nulls[i] = ' ';
1727                         }
1728                         else
1729                                 ereport(ERROR,
1730                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
1731                                                  errmsg("no mapping found with key \"%s\"", key),
1732                                                  errhint("to return null in specific column, "
1733                                                          "add value None to map with key named after column")));
1734
1735                         Py_XDECREF(value);
1736                         value = NULL;
1737                 }
1738                 PG_CATCH();
1739                 {
1740                         Py_XDECREF(so);
1741                         Py_XDECREF(value);
1742                         PG_RE_THROW();
1743                 }
1744                 PG_END_TRY();
1745         }
1746
1747         tuple = heap_formtuple(desc, values, nulls);
1748         ReleaseTupleDesc(desc);
1749         pfree(values);
1750         pfree(nulls);
1751
1752         return tuple;
1753 }
1754
1755
1756 static HeapTuple
1757 PLySequence_ToTuple(PLyTypeInfo *info, PyObject *sequence)
1758 {
1759         TupleDesc       desc;
1760         HeapTuple       tuple;
1761         Datum           *values;
1762         char            *nulls;
1763         int             i;
1764
1765         Assert(PySequence_Check(sequence));
1766
1767         /*
1768          * Check that sequence length is exactly same as PG tuple's. We actually
1769          * can ignore exceeding items or assume missing ones as null but to
1770          * avoid plpython developer's errors we are strict here
1771          */
1772         desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1773         if (PySequence_Length(sequence) != desc->natts)
1774                 ereport(ERROR,
1775                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1776                                  errmsg("returned sequence's length must be same as tuple's length")));
1777
1778         if (info->is_rowtype == 2)
1779                 PLy_output_tuple_funcs(info, desc);
1780         Assert(info->is_rowtype == 1);
1781
1782         /* Build tuple */
1783         values = palloc(sizeof(Datum)*desc->natts);
1784         nulls = palloc(sizeof(char)*desc->natts);
1785         for (i = 0;  i < desc->natts;  ++i)
1786         {
1787                 PyObject        *value,
1788                                 *so;
1789
1790                 value = so = NULL;
1791                 PG_TRY();
1792                 {
1793                         value = PySequence_GetItem(sequence, i);
1794                         Assert(value);
1795                         if (value == Py_None)
1796                         {
1797                                 values[i] = (Datum) NULL;
1798                                 nulls[i] = 'n';
1799                         }
1800                         else if (value)
1801                         {
1802                                 char *valuestr;
1803
1804                                 so = PyObject_Str(value);
1805                                 if (so == NULL)
1806                                         PLy_elog(ERROR, "can't convert sequence type");
1807                                 valuestr = PyString_AsString(so);
1808                                 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1809                                                 , valuestr
1810                                                 , info->out.r.atts[i].typioparam
1811                                                 , -1);
1812                                 Py_DECREF(so);
1813                                 so = NULL;
1814                                 nulls[i] = ' ';
1815                         }
1816
1817                         Py_XDECREF(value);
1818                         value = NULL;
1819                 }
1820                 PG_CATCH();
1821                 {
1822                         Py_XDECREF(so);
1823                         Py_XDECREF(value);
1824                         PG_RE_THROW();
1825                 }
1826                 PG_END_TRY();
1827         }
1828
1829         tuple = heap_formtuple(desc, values, nulls);
1830         ReleaseTupleDesc(desc);
1831         pfree(values);
1832         pfree(nulls);
1833
1834         return tuple;
1835 }
1836
1837
1838 static HeapTuple
1839 PLyObject_ToTuple(PLyTypeInfo *info, PyObject *object)
1840 {
1841         TupleDesc       desc;
1842         HeapTuple       tuple;
1843         Datum           *values;
1844         char            *nulls;
1845         int             i;
1846
1847         desc = lookup_rowtype_tupdesc(info->out.d.typoid, -1);
1848         if (info->is_rowtype == 2)
1849                 PLy_output_tuple_funcs(info, desc);
1850         Assert(info->is_rowtype == 1);
1851
1852         /* Build tuple */
1853         values = palloc(sizeof(Datum)*desc->natts);
1854         nulls = palloc(sizeof(char)*desc->natts);
1855         for (i = 0;  i < desc->natts;  ++i)
1856         {
1857                 char            *key;
1858                 PyObject        *value,
1859                                 *so;
1860
1861                 key = NameStr(desc->attrs[i]->attname);
1862                 value = so = NULL;
1863                 PG_TRY();
1864                 {
1865                         value = PyObject_GetAttrString(object, key);
1866                         if (value == Py_None)
1867                         {
1868                                 values[i] = (Datum) NULL;
1869                                 nulls[i] = 'n';
1870                         }
1871                         else if (value)
1872                         {
1873                                 char *valuestr;
1874
1875                                 so = PyObject_Str(value);
1876                                 if (so == NULL)
1877                                         PLy_elog(ERROR, "can't convert object type");
1878                                 valuestr = PyString_AsString(so);
1879                                 values[i] = InputFunctionCall(&info->out.r.atts[i].typfunc
1880                                                 , valuestr
1881                                                 , info->out.r.atts[i].typioparam
1882                                                 , -1);
1883                                 Py_DECREF(so);
1884                                 so = NULL;
1885                                 nulls[i] = ' ';
1886                         }
1887                         else
1888                                 ereport(ERROR,
1889                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
1890                                                  errmsg("no attribute named \"%s\"", key),
1891                                                  errhint("to return null in specific column, "
1892                                                          "let returned object to have attribute named "
1893                                                          "after column with value None")));
1894
1895                         Py_XDECREF(value);
1896                         value = NULL;
1897                 }
1898                 PG_CATCH();
1899                 {
1900                         Py_XDECREF(so);
1901                         Py_XDECREF(value);
1902                         PG_RE_THROW();
1903                 }
1904                 PG_END_TRY();
1905         }
1906
1907         tuple = heap_formtuple(desc, values, nulls);
1908         ReleaseTupleDesc(desc);
1909         pfree(values);
1910         pfree(nulls);
1911
1912         return tuple;
1913 }
1914
1915
1916 /* initialization, some python variables function declared here */
1917
1918 /* interface to postgresql elog */
1919 static PyObject *PLy_debug(PyObject *, PyObject *);
1920 static PyObject *PLy_log(PyObject *, PyObject *);
1921 static PyObject *PLy_info(PyObject *, PyObject *);
1922 static PyObject *PLy_notice(PyObject *, PyObject *);
1923 static PyObject *PLy_warning(PyObject *, PyObject *);
1924 static PyObject *PLy_error(PyObject *, PyObject *);
1925 static PyObject *PLy_fatal(PyObject *, PyObject *);
1926
1927 /* PLyPlanObject, PLyResultObject and SPI interface */
1928 #define is_PLyPlanObject(x) ((x)->ob_type == &PLy_PlanType)
1929 static PyObject *PLy_plan_new(void);
1930 static void PLy_plan_dealloc(PyObject *);
1931 static PyObject *PLy_plan_getattr(PyObject *, char *);
1932 static PyObject *PLy_plan_status(PyObject *, PyObject *);
1933
1934 static PyObject *PLy_result_new(void);
1935 static void PLy_result_dealloc(PyObject *);
1936 static PyObject *PLy_result_getattr(PyObject *, char *);
1937 static PyObject *PLy_result_nrows(PyObject *, PyObject *);
1938 static PyObject *PLy_result_status(PyObject *, PyObject *);
1939 static int      PLy_result_length(PyObject *);
1940 static PyObject *PLy_result_item(PyObject *, int);
1941 static PyObject *PLy_result_slice(PyObject *, int, int);
1942 static int      PLy_result_ass_item(PyObject *, int, PyObject *);
1943 static int      PLy_result_ass_slice(PyObject *, int, int, PyObject *);
1944
1945
1946 static PyObject *PLy_spi_prepare(PyObject *, PyObject *);
1947 static PyObject *PLy_spi_execute(PyObject *, PyObject *);
1948 static PyObject *PLy_spi_execute_query(char *query, long limit);
1949 static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, long);
1950 static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int);
1951
1952
1953 static PyTypeObject PLy_PlanType = {
1954         PyObject_HEAD_INIT(NULL)
1955         0,                                                      /* ob_size */
1956         "PLyPlan",                                      /* tp_name */
1957         sizeof(PLyPlanObject),          /* tp_size */
1958         0,                                                      /* tp_itemsize */
1959
1960         /*
1961          * methods
1962          */
1963         (destructor) PLy_plan_dealloc,          /* tp_dealloc */
1964         0,                                                      /* tp_print */
1965         (getattrfunc) PLy_plan_getattr,         /* tp_getattr */
1966         0,                                                      /* tp_setattr */
1967         0,                                                      /* tp_compare */
1968         0,                                                      /* tp_repr */
1969         0,                                                      /* tp_as_number */
1970         0,                                                      /* tp_as_sequence */
1971         0,                                                      /* tp_as_mapping */
1972         0,                                                      /* tp_hash */
1973         0,                                                      /* tp_call */
1974         0,                                                      /* tp_str */
1975         0,                                                      /* tp_getattro */
1976         0,                                                      /* tp_setattro */
1977         0,                                                      /* tp_as_buffer */
1978         0,                                                      /* tp_xxx4 */
1979         PLy_plan_doc,                           /* tp_doc */
1980 };
1981
1982 static PyMethodDef PLy_plan_methods[] = {
1983         {"status", PLy_plan_status, METH_VARARGS, NULL},
1984         {NULL, NULL, 0, NULL}
1985 };
1986
1987
1988 static PySequenceMethods PLy_result_as_sequence = {
1989         (inquiry) PLy_result_length,    /* sq_length */
1990         (binaryfunc) 0,                         /* sq_concat */
1991         (intargfunc) 0,                         /* sq_repeat */
1992         (intargfunc) PLy_result_item,           /* sq_item */
1993         (intintargfunc) PLy_result_slice,       /* sq_slice */
1994         (intobjargproc) PLy_result_ass_item,            /* sq_ass_item */
1995         (intintobjargproc) PLy_result_ass_slice,        /* sq_ass_slice */
1996 };
1997
1998 static PyTypeObject PLy_ResultType = {
1999         PyObject_HEAD_INIT(NULL)
2000         0,                                                      /* ob_size */
2001         "PLyResult",                            /* tp_name */
2002         sizeof(PLyResultObject),        /* tp_size */
2003         0,                                                      /* tp_itemsize */
2004
2005         /*
2006          * methods
2007          */
2008         (destructor) PLy_result_dealloc,        /* tp_dealloc */
2009         0,                                                      /* tp_print */
2010         (getattrfunc) PLy_result_getattr,       /* tp_getattr */
2011         0,                                                      /* tp_setattr */
2012         0,                                                      /* tp_compare */
2013         0,                                                      /* tp_repr */
2014         0,                                                      /* tp_as_number */
2015         &PLy_result_as_sequence,        /* tp_as_sequence */
2016         0,                                                      /* tp_as_mapping */
2017         0,                                                      /* tp_hash */
2018         0,                                                      /* tp_call */
2019         0,                                                      /* tp_str */
2020         0,                                                      /* tp_getattro */
2021         0,                                                      /* tp_setattro */
2022         0,                                                      /* tp_as_buffer */
2023         0,                                                      /* tp_xxx4 */
2024         PLy_result_doc,                         /* tp_doc */
2025 };
2026
2027 static PyMethodDef PLy_result_methods[] = {
2028         {"nrows", PLy_result_nrows, METH_VARARGS, NULL},
2029         {"status", PLy_result_status, METH_VARARGS, NULL},
2030         {NULL, NULL, 0, NULL}
2031 };
2032
2033 static PyMethodDef PLy_methods[] = {
2034         /*
2035          * logging methods
2036          */
2037         {"debug", PLy_debug, METH_VARARGS, NULL},
2038         {"log", PLy_log, METH_VARARGS, NULL},
2039         {"info", PLy_info, METH_VARARGS, NULL},
2040         {"notice", PLy_notice, METH_VARARGS, NULL},
2041         {"warning", PLy_warning, METH_VARARGS, NULL},
2042         {"error", PLy_error, METH_VARARGS, NULL},
2043         {"fatal", PLy_fatal, METH_VARARGS, NULL},
2044
2045         /*
2046          * create a stored plan
2047          */
2048         {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
2049
2050         /*
2051          * execute a plan or query
2052          */
2053         {"execute", PLy_spi_execute, METH_VARARGS, NULL},
2054
2055         {NULL, NULL, 0, NULL}
2056 };
2057
2058
2059 /* plan object methods */
2060 static PyObject *
2061 PLy_plan_new(void)
2062 {
2063         PLyPlanObject *ob;
2064
2065         if ((ob = PyObject_NEW(PLyPlanObject, &PLy_PlanType)) == NULL)
2066                 return NULL;
2067
2068         ob->plan = NULL;
2069         ob->nargs = 0;
2070         ob->types = NULL;
2071         ob->args = NULL;
2072
2073         return (PyObject *) ob;
2074 }
2075
2076
2077 static void
2078 PLy_plan_dealloc(PyObject * arg)
2079 {
2080         PLyPlanObject *ob = (PLyPlanObject *) arg;
2081
2082         if (ob->plan)
2083                 SPI_freeplan(ob->plan);
2084         if (ob->types)
2085                 PLy_free(ob->types);
2086         if (ob->args)
2087         {
2088                 int                     i;
2089
2090                 for (i = 0; i < ob->nargs; i++)
2091                         PLy_typeinfo_dealloc(&ob->args[i]);
2092                 PLy_free(ob->args);
2093         }
2094
2095         PyMem_DEL(arg);
2096 }
2097
2098
2099 static PyObject *
2100 PLy_plan_getattr(PyObject * self, char *name)
2101 {
2102         return Py_FindMethod(PLy_plan_methods, self, name);
2103 }
2104
2105 static PyObject *
2106 PLy_plan_status(PyObject * self, PyObject * args)
2107 {
2108         if (PyArg_ParseTuple(args, ""))
2109         {
2110                 Py_INCREF(Py_True);
2111                 return Py_True;
2112                 /* return PyInt_FromLong(self->status); */
2113         }
2114         PyErr_SetString(PLy_exc_error, "plan.status() takes no arguments");
2115         return NULL;
2116 }
2117
2118
2119
2120 /* result object methods */
2121
2122 static PyObject *
2123 PLy_result_new(void)
2124 {
2125         PLyResultObject *ob;
2126
2127         if ((ob = PyObject_NEW(PLyResultObject, &PLy_ResultType)) == NULL)
2128                 return NULL;
2129
2130         /* ob->tuples = NULL; */
2131
2132         Py_INCREF(Py_None);
2133         ob->status = Py_None;
2134         ob->nrows = PyInt_FromLong(-1);
2135         ob->rows = PyList_New(0);
2136
2137         return (PyObject *) ob;
2138 }
2139
2140 static void
2141 PLy_result_dealloc(PyObject * arg)
2142 {
2143         PLyResultObject *ob = (PLyResultObject *) arg;
2144
2145         Py_XDECREF(ob->nrows);
2146         Py_XDECREF(ob->rows);
2147         Py_XDECREF(ob->status);
2148
2149         PyMem_DEL(ob);
2150 }
2151
2152 static PyObject *
2153 PLy_result_getattr(PyObject * self, char *name)
2154 {
2155         return Py_FindMethod(PLy_result_methods, self, name);
2156 }
2157
2158 static PyObject *
2159 PLy_result_nrows(PyObject * self, PyObject * args)
2160 {
2161         PLyResultObject *ob = (PLyResultObject *) self;
2162
2163         Py_INCREF(ob->nrows);
2164         return ob->nrows;
2165 }
2166
2167 static PyObject *
2168 PLy_result_status(PyObject * self, PyObject * args)
2169 {
2170         PLyResultObject *ob = (PLyResultObject *) self;
2171
2172         Py_INCREF(ob->status);
2173         return ob->status;
2174 }
2175
2176 static int
2177 PLy_result_length(PyObject * arg)
2178 {
2179         PLyResultObject *ob = (PLyResultObject *) arg;
2180
2181         return PyList_Size(ob->rows);
2182 }
2183
2184 static PyObject *
2185 PLy_result_item(PyObject * arg, int idx)
2186 {
2187         PyObject   *rv;
2188         PLyResultObject *ob = (PLyResultObject *) arg;
2189
2190         rv = PyList_GetItem(ob->rows, idx);
2191         if (rv != NULL)
2192                 Py_INCREF(rv);
2193         return rv;
2194 }
2195
2196 static int
2197 PLy_result_ass_item(PyObject * arg, int idx, PyObject * item)
2198 {
2199         int                     rv;
2200         PLyResultObject *ob = (PLyResultObject *) arg;
2201
2202         Py_INCREF(item);
2203         rv = PyList_SetItem(ob->rows, idx, item);
2204         return rv;
2205 }
2206
2207 static PyObject *
2208 PLy_result_slice(PyObject * arg, int lidx, int hidx)
2209 {
2210         PyObject   *rv;
2211         PLyResultObject *ob = (PLyResultObject *) arg;
2212
2213         rv = PyList_GetSlice(ob->rows, lidx, hidx);
2214         if (rv == NULL)
2215                 return NULL;
2216         Py_INCREF(rv);
2217         return rv;
2218 }
2219
2220 static int
2221 PLy_result_ass_slice(PyObject * arg, int lidx, int hidx, PyObject * slice)
2222 {
2223         int                     rv;
2224         PLyResultObject *ob = (PLyResultObject *) arg;
2225
2226         rv = PyList_SetSlice(ob->rows, lidx, hidx, slice);
2227         return rv;
2228 }
2229
2230 /* SPI interface */
2231 static PyObject *
2232 PLy_spi_prepare(PyObject * self, PyObject * args)
2233 {
2234         PLyPlanObject *plan;
2235         PyObject   *list = NULL;
2236         PyObject   *volatile optr = NULL;
2237         char       *query;
2238         void       *tmpplan;
2239         MemoryContext oldcontext;
2240
2241         /* Can't execute more if we have an unhandled error */
2242         if (PLy_error_in_progress)
2243         {
2244                 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2245                 return NULL;
2246         }
2247
2248         if (!PyArg_ParseTuple(args, "s|O", &query, &list))
2249         {
2250                 PyErr_SetString(PLy_exc_spi_error,
2251                                                 "Invalid arguments for plpy.prepare()");
2252                 return NULL;
2253         }
2254
2255         if (list && (!PySequence_Check(list)))
2256         {
2257                 PyErr_SetString(PLy_exc_spi_error,
2258                                          "Second argument in plpy.prepare() must be a sequence");
2259                 return NULL;
2260         }
2261
2262         if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
2263                 return NULL;
2264
2265         oldcontext = CurrentMemoryContext;
2266         PG_TRY();
2267         {
2268                 if (list != NULL)
2269                 {
2270                         int                     nargs,
2271                                                 i;
2272
2273                         nargs = PySequence_Length(list);
2274                         if (nargs > 0)
2275                         {
2276                                 plan->nargs = nargs;
2277                                 plan->types = PLy_malloc(sizeof(Oid) * nargs);
2278                                 plan->values = PLy_malloc(sizeof(Datum) * nargs);
2279                                 plan->args = PLy_malloc(sizeof(PLyTypeInfo) * nargs);
2280
2281                                 /*
2282                                  * the other loop might throw an exception, if PLyTypeInfo
2283                                  * member isn't properly initialized the Py_DECREF(plan) will
2284                                  * go boom
2285                                  */
2286                                 for (i = 0; i < nargs; i++)
2287                                 {
2288                                         PLy_typeinfo_init(&plan->args[i]);
2289                                         plan->values[i] = PointerGetDatum(NULL);
2290                                 }
2291
2292                                 for (i = 0; i < nargs; i++)
2293                                 {
2294                                         char       *sptr;
2295                                         List       *names;
2296                                         HeapTuple       typeTup;
2297                                         Form_pg_type typeStruct;
2298
2299                                         optr = PySequence_GetItem(list, i);
2300                                         if (!PyString_Check(optr))
2301                                                 elog(ERROR, "Type names must be strings.");
2302                                         sptr = PyString_AsString(optr);
2303
2304                                         /*
2305                                          * Parse possibly-qualified type name and look it up in
2306                                          * pg_type
2307                                          */
2308                                         names = stringToQualifiedNameList(sptr,
2309                                                                                                           "PLy_spi_prepare");
2310                                         typeTup = typenameType(NULL,
2311                                                                                    makeTypeNameFromNameList(names));
2312                                         Py_DECREF(optr);
2313                                         optr = NULL;    /* this is important */
2314
2315                                         plan->types[i] = HeapTupleGetOid(typeTup);
2316                                         typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2317                                         if (typeStruct->typtype != 'c')
2318                                                 PLy_output_datum_func(&plan->args[i], typeTup);
2319                                         else
2320                                                 elog(ERROR, "tuples not handled in plpy.prepare, yet.");
2321                                         ReleaseSysCache(typeTup);
2322                                 }
2323                         }
2324                 }
2325
2326                 plan->plan = SPI_prepare(query, plan->nargs, plan->types);
2327                 if (plan->plan == NULL)
2328                         elog(ERROR, "SPI_prepare failed: %s",
2329                                  SPI_result_code_string(SPI_result));
2330
2331                 /* transfer plan from procCxt to topCxt */
2332                 tmpplan = plan->plan;
2333                 plan->plan = SPI_saveplan(tmpplan);
2334                 SPI_freeplan(tmpplan);
2335                 if (plan->plan == NULL)
2336                         elog(ERROR, "SPI_saveplan failed: %s",
2337                                  SPI_result_code_string(SPI_result));
2338         }
2339         PG_CATCH();
2340         {
2341                 MemoryContextSwitchTo(oldcontext);
2342                 PLy_error_in_progress = CopyErrorData();
2343                 FlushErrorState();
2344                 Py_DECREF(plan);
2345                 Py_XDECREF(optr);
2346                 if (!PyErr_Occurred())
2347                         PyErr_SetString(PLy_exc_spi_error,
2348                                                         "Unknown error in PLy_spi_prepare");
2349                 /* XXX this oughta be replaced with errcontext mechanism */
2350                 PLy_elog(WARNING, "in function %s:",
2351                                  PLy_procedure_name(PLy_curr_procedure));
2352                 return NULL;
2353         }
2354         PG_END_TRY();
2355
2356         return (PyObject *) plan;
2357 }
2358
2359 /* execute(query="select * from foo", limit=5)
2360  * execute(plan=plan, values=(foo, bar), limit=5)
2361  */
2362 static PyObject *
2363 PLy_spi_execute(PyObject * self, PyObject * args)
2364 {
2365         char       *query;
2366         PyObject   *plan;
2367         PyObject   *list = NULL;
2368         long            limit = 0;
2369
2370         /* Can't execute more if we have an unhandled error */
2371         if (PLy_error_in_progress)
2372         {
2373                 PyErr_SetString(PLy_exc_error, "Transaction aborted.");
2374                 return NULL;
2375         }
2376
2377         if (PyArg_ParseTuple(args, "s|l", &query, &limit))
2378                 return PLy_spi_execute_query(query, limit);
2379
2380         PyErr_Clear();
2381
2382         if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
2383                 is_PLyPlanObject(plan))
2384                 return PLy_spi_execute_plan(plan, list, limit);
2385
2386         PyErr_SetString(PLy_exc_error, "Expected a query or plan.");
2387         return NULL;
2388 }
2389
2390 static PyObject *
2391 PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
2392 {
2393         volatile int nargs;
2394         int                     i,
2395                                 rv;
2396         PLyPlanObject *plan;
2397         MemoryContext oldcontext;
2398
2399         if (list != NULL)
2400         {
2401                 if (!PySequence_Check(list) || PyString_Check(list))
2402                 {
2403                         char       *msg = "plpy.execute() takes a sequence as its second argument";
2404
2405                         PyErr_SetString(PLy_exc_spi_error, msg);
2406                         return NULL;
2407                 }
2408                 nargs = PySequence_Length(list);
2409         }
2410         else
2411                 nargs = 0;
2412
2413         plan = (PLyPlanObject *) ob;
2414
2415         if (nargs != plan->nargs)
2416         {
2417                 char       *sv;
2418                 PyObject   *so = PyObject_Str(list);
2419
2420                 if (!so)
2421                         PLy_elog(ERROR, "function \"%s\" could not execute plan",
2422                                          PLy_procedure_name(PLy_curr_procedure));
2423                 sv = PyString_AsString(so);
2424                 PLy_exception_set(PLy_exc_spi_error,
2425                                                   "Expected sequence of %d arguments, got %d. %s",
2426                                                   plan->nargs, nargs, sv);
2427                 Py_DECREF(so);
2428
2429                 return NULL;
2430         }
2431
2432         oldcontext = CurrentMemoryContext;
2433         PG_TRY();
2434         {
2435                 char       *nulls = palloc(nargs * sizeof(char));
2436
2437                 for (i = 0; i < nargs; i++)
2438                 {
2439                         PyObject   *elem,
2440                                            *so;
2441
2442                         elem = PySequence_GetItem(list, i);
2443                         if (elem != Py_None)
2444                         {
2445                                 so = PyObject_Str(elem);
2446                                 if (!so)
2447                                         PLy_elog(ERROR, "function \"%s\" could not execute plan",
2448                                                          PLy_procedure_name(PLy_curr_procedure));
2449                                 Py_DECREF(elem);
2450
2451                                 PG_TRY();
2452                                 {
2453                                         char *sv = PyString_AsString(so);
2454
2455                                         plan->values[i] =
2456                                                 InputFunctionCall(&(plan->args[i].out.d.typfunc),
2457                                                                                   sv,
2458                                                                                   plan->args[i].out.d.typioparam,
2459                                                                                   -1);
2460                                 }
2461                                 PG_CATCH();
2462                                 {
2463                                         Py_DECREF(so);
2464                                         PG_RE_THROW();
2465                                 }
2466                                 PG_END_TRY();
2467
2468                                 Py_DECREF(so);
2469                                 nulls[i] = ' ';
2470                         }
2471                         else
2472                         {
2473                                 Py_DECREF(elem);
2474                                 plan->values[i] =
2475                                         InputFunctionCall(&(plan->args[i].out.d.typfunc),
2476                                                                           NULL,
2477                                                                           plan->args[i].out.d.typioparam,
2478                                                                           -1);
2479                                 nulls[i] = 'n';
2480                         }
2481                 }
2482
2483                 rv = SPI_execute_plan(plan->plan, plan->values, nulls,
2484                                                           PLy_curr_procedure->fn_readonly, limit);
2485
2486                 pfree(nulls);
2487         }
2488         PG_CATCH();
2489         {
2490                 MemoryContextSwitchTo(oldcontext);
2491                 PLy_error_in_progress = CopyErrorData();
2492                 FlushErrorState();
2493
2494                 /*
2495                  * cleanup plan->values array
2496                  */
2497                 for (i = 0; i < nargs; i++)
2498                 {
2499                         if (!plan->args[i].out.d.typbyval &&
2500                                 (plan->values[i] != PointerGetDatum(NULL)))
2501                         {
2502                                 pfree(DatumGetPointer(plan->values[i]));
2503                                 plan->values[i] = PointerGetDatum(NULL);
2504                         }
2505                 }
2506
2507                 if (!PyErr_Occurred())
2508                         PyErr_SetString(PLy_exc_error,
2509                                                         "Unknown error in PLy_spi_execute_plan");
2510                 /* XXX this oughta be replaced with errcontext mechanism */
2511                 PLy_elog(WARNING, "in function %s:",
2512                                  PLy_procedure_name(PLy_curr_procedure));
2513                 return NULL;
2514         }
2515         PG_END_TRY();
2516
2517         for (i = 0; i < nargs; i++)
2518         {
2519                 if (!plan->args[i].out.d.typbyval &&
2520                         (plan->values[i] != PointerGetDatum(NULL)))
2521                 {
2522                         pfree(DatumGetPointer(plan->values[i]));
2523                         plan->values[i] = PointerGetDatum(NULL);
2524                 }
2525         }
2526
2527         if (rv < 0)
2528         {
2529                 PLy_exception_set(PLy_exc_spi_error,
2530                                                   "SPI_execute_plan failed: %s",
2531                                                   SPI_result_code_string(rv));
2532                 return NULL;
2533         }
2534
2535         return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2536 }
2537
2538 static PyObject *
2539 PLy_spi_execute_query(char *query, long limit)
2540 {
2541         int                     rv;
2542         MemoryContext oldcontext;
2543
2544         oldcontext = CurrentMemoryContext;
2545         PG_TRY();
2546         {
2547                 rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
2548         }
2549         PG_CATCH();
2550         {
2551                 MemoryContextSwitchTo(oldcontext);
2552                 PLy_error_in_progress = CopyErrorData();
2553                 FlushErrorState();
2554                 if (!PyErr_Occurred())
2555                         PyErr_SetString(PLy_exc_spi_error,
2556                                                         "Unknown error in PLy_spi_execute_query");
2557                 /* XXX this oughta be replaced with errcontext mechanism */
2558                 PLy_elog(WARNING, "in function %s:",
2559                                  PLy_procedure_name(PLy_curr_procedure));
2560                 return NULL;
2561         }
2562         PG_END_TRY();
2563
2564         if (rv < 0)
2565         {
2566                 PLy_exception_set(PLy_exc_spi_error,
2567                                                   "SPI_execute failed: %s",
2568                                                   SPI_result_code_string(rv));
2569                 return NULL;
2570         }
2571
2572         return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
2573 }
2574
2575 static PyObject *
2576 PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
2577 {
2578         PLyResultObject *result;
2579         MemoryContext oldcontext;
2580
2581         result = (PLyResultObject *) PLy_result_new();
2582         Py_DECREF(result->status);
2583         result->status = PyInt_FromLong(status);
2584
2585         if (status > 0 && tuptable == NULL)
2586         {
2587                 Py_DECREF(result->nrows);
2588                 result->nrows = PyInt_FromLong(rows);
2589         }
2590         else if (status > 0 && tuptable != NULL)
2591         {
2592                 PLyTypeInfo args;
2593                 int                     i;
2594
2595                 Py_DECREF(result->nrows);
2596                 result->nrows = PyInt_FromLong(rows);
2597                 PLy_typeinfo_init(&args);
2598
2599                 oldcontext = CurrentMemoryContext;
2600                 PG_TRY();
2601                 {
2602                         if (rows)
2603                         {
2604                                 Py_DECREF(result->rows);
2605                                 result->rows = PyList_New(rows);
2606
2607                                 PLy_input_tuple_funcs(&args, tuptable->tupdesc);
2608                                 for (i = 0; i < rows; i++)
2609                                 {
2610                                         PyObject   *row = PLyDict_FromTuple(&args, tuptable->vals[i],
2611                                                                                                                 tuptable->tupdesc);
2612
2613                                         PyList_SetItem(result->rows, i, row);
2614                                 }
2615                                 PLy_typeinfo_dealloc(&args);
2616
2617                                 SPI_freetuptable(tuptable);
2618                         }
2619                 }
2620                 PG_CATCH();
2621                 {
2622                         MemoryContextSwitchTo(oldcontext);
2623                         PLy_error_in_progress = CopyErrorData();
2624                         FlushErrorState();
2625                         if (!PyErr_Occurred())
2626                                 PyErr_SetString(PLy_exc_error,
2627                                                         "Unknown error in PLy_spi_execute_fetch_result");
2628                         Py_DECREF(result);
2629                         PLy_typeinfo_dealloc(&args);
2630                         return NULL;
2631                 }
2632                 PG_END_TRY();
2633         }
2634
2635         return (PyObject *) result;
2636 }
2637
2638
2639 /*
2640  * language handler and interpreter initialization
2641  */
2642
2643 /*
2644  * _PG_init()                   - library load-time initialization
2645  *
2646  * DO NOT make this static nor change its name!
2647  */
2648 void
2649 _PG_init(void)
2650 {
2651         /* Be sure we do initialization only once (should be redundant now) */
2652         static bool inited = false;
2653
2654         if (inited)
2655                 return;
2656
2657         Py_Initialize();
2658         PLy_init_interp();
2659         PLy_init_plpy();
2660         if (PyErr_Occurred())
2661                 PLy_elog(FATAL, "untrapped error in initialization");
2662         PLy_procedure_cache = PyDict_New();
2663         if (PLy_procedure_cache == NULL)
2664                 PLy_elog(ERROR, "could not create procedure cache");
2665
2666         inited = true;
2667 }
2668
2669 static void
2670 PLy_init_interp(void)
2671 {
2672         PyObject   *mainmod;
2673
2674         mainmod = PyImport_AddModule("__main__");
2675         if (mainmod == NULL || PyErr_Occurred())
2676                 PLy_elog(ERROR, "could not import \"__main__\" module.");
2677         Py_INCREF(mainmod);
2678         PLy_interp_globals = PyModule_GetDict(mainmod);
2679         PLy_interp_safe_globals = PyDict_New();
2680         PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
2681         Py_DECREF(mainmod);
2682         if (PLy_interp_globals == NULL || PyErr_Occurred())
2683                 PLy_elog(ERROR, "could not initialize globals");
2684 }
2685
2686 static void
2687 PLy_init_plpy(void)
2688 {
2689         PyObject   *main_mod,
2690                            *main_dict,
2691                            *plpy_mod;
2692         PyObject   *plpy,
2693                            *plpy_dict;
2694
2695         /*
2696          * initialize plpy module
2697          */
2698         PLy_PlanType.ob_type = PLy_ResultType.ob_type = &PyType_Type;
2699         plpy = Py_InitModule("plpy", PLy_methods);
2700         plpy_dict = PyModule_GetDict(plpy);
2701
2702         /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
2703
2704         PLy_exc_error = PyErr_NewException("plpy.Error", NULL, NULL);
2705         PLy_exc_fatal = PyErr_NewException("plpy.Fatal", NULL, NULL);
2706         PLy_exc_spi_error = PyErr_NewException("plpy.SPIError", NULL, NULL);
2707         PyDict_SetItemString(plpy_dict, "Error", PLy_exc_error);
2708         PyDict_SetItemString(plpy_dict, "Fatal", PLy_exc_fatal);
2709         PyDict_SetItemString(plpy_dict, "SPIError", PLy_exc_spi_error);
2710
2711         /*
2712          * initialize main module, and add plpy
2713          */
2714         main_mod = PyImport_AddModule("__main__");
2715         main_dict = PyModule_GetDict(main_mod);
2716         plpy_mod = PyImport_AddModule("plpy");
2717         PyDict_SetItemString(main_dict, "plpy", plpy_mod);
2718         if (PyErr_Occurred())
2719                 elog(ERROR, "could not init plpy");
2720 }
2721
2722 /* the python interface to the elog function
2723  * don't confuse these with PLy_elog
2724  */
2725 static PyObject *PLy_output(volatile int, PyObject *, PyObject *);
2726
2727 static PyObject *
2728 PLy_debug(PyObject * self, PyObject * args)
2729 {
2730         return PLy_output(DEBUG2, self, args);
2731 }
2732
2733 static PyObject *
2734 PLy_log(PyObject * self, PyObject * args)
2735 {
2736         return PLy_output(LOG, self, args);
2737 }
2738
2739 static PyObject *
2740 PLy_info(PyObject * self, PyObject * args)
2741 {
2742         return PLy_output(INFO, self, args);
2743 }
2744
2745 static PyObject *
2746 PLy_notice(PyObject * self, PyObject * args)
2747 {
2748         return PLy_output(NOTICE, self, args);
2749 }
2750
2751 static PyObject *
2752 PLy_warning(PyObject * self, PyObject * args)
2753 {
2754         return PLy_output(WARNING, self, args);
2755 }
2756
2757 static PyObject *
2758 PLy_error(PyObject * self, PyObject * args)
2759 {
2760         return PLy_output(ERROR, self, args);
2761 }
2762
2763 static PyObject *
2764 PLy_fatal(PyObject * self, PyObject * args)
2765 {
2766         return PLy_output(FATAL, self, args);
2767 }
2768
2769
2770 static PyObject *
2771 PLy_output(volatile int level, PyObject * self, PyObject * args)
2772 {
2773         PyObject   *so;
2774         char       *volatile sv;
2775         MemoryContext oldcontext;
2776
2777         so = PyObject_Str(args);
2778         if (so == NULL || ((sv = PyString_AsString(so)) == NULL))
2779         {
2780                 level = ERROR;
2781                 sv = "Unable to parse error message in `plpy.elog'";
2782         }
2783
2784         oldcontext = CurrentMemoryContext;
2785         PG_TRY();
2786         {
2787                 elog(level, "%s", sv);
2788         }
2789         PG_CATCH();
2790         {
2791                 MemoryContextSwitchTo(oldcontext);
2792                 PLy_error_in_progress = CopyErrorData();
2793                 FlushErrorState();
2794                 Py_XDECREF(so);
2795
2796                 /*
2797                  * returning NULL here causes the python interpreter to bail. when
2798                  * control passes back to PLy_procedure_call, we check for PG
2799                  * exceptions and re-throw the error.
2800                  */
2801                 PyErr_SetString(PLy_exc_error, sv);
2802                 return NULL;
2803         }
2804         PG_END_TRY();
2805
2806         Py_XDECREF(so);
2807
2808         /*
2809          * return a legal object so the interpreter will continue on its merry way
2810          */
2811         Py_INCREF(Py_None);
2812         return Py_None;
2813 }
2814
2815
2816 /*
2817  * Get the name of the last procedure called by the backend (the
2818  * innermost, if a plpython procedure call calls the backend and the
2819  * backend calls another plpython procedure).
2820  *
2821  * NB: this returns the SQL name, not the internal Python procedure name
2822  */
2823 static char *
2824 PLy_procedure_name(PLyProcedure * proc)
2825 {
2826         if (proc == NULL)
2827                 return "<unknown procedure>";
2828         return proc->proname;
2829 }
2830
2831 /* output a python traceback/exception via the postgresql elog
2832  * function.  not pretty.
2833  */
2834 static void
2835 PLy_exception_set(PyObject * exc, const char *fmt,...)
2836 {
2837         char            buf[1024];
2838         va_list         ap;
2839
2840         va_start(ap, fmt);
2841         vsnprintf(buf, sizeof(buf), fmt, ap);
2842         va_end(ap);
2843
2844         PyErr_SetString(exc, buf);
2845 }
2846
2847 /* Emit a PG error or notice, together with any available info about the
2848  * current Python error.  This should be used to propagate Python errors
2849  * into PG.
2850  */
2851 static void
2852 PLy_elog(int elevel, const char *fmt,...)
2853 {
2854         va_list         ap;
2855         char       *xmsg,
2856                            *emsg;
2857         int                     xlevel;
2858
2859         xmsg = PLy_traceback(&xlevel);
2860
2861         va_start(ap, fmt);
2862         emsg = PLy_vprintf(fmt, ap);
2863         va_end(ap);
2864
2865         PG_TRY();
2866         {
2867                 ereport(elevel,
2868                                 (errmsg("plpython: %s", emsg),
2869                                  (xmsg) ? errdetail("%s", xmsg) : 0));
2870         }
2871         PG_CATCH();
2872         {
2873                 PLy_free(emsg);
2874                 if (xmsg)
2875                         PLy_free(xmsg);
2876                 PG_RE_THROW();
2877         }
2878         PG_END_TRY();
2879
2880         PLy_free(emsg);
2881         if (xmsg)
2882                 PLy_free(xmsg);
2883 }
2884
2885 static char *
2886 PLy_traceback(int *xlevel)
2887 {
2888         PyObject   *e,
2889                            *v,
2890                            *tb;
2891         PyObject   *eob,
2892                            *vob = NULL;
2893         char       *vstr,
2894                            *estr,
2895                            *xstr = NULL;
2896
2897         /*
2898          * get the current exception
2899          */
2900         PyErr_Fetch(&e, &v, &tb);
2901
2902         /*
2903          * oops, no exception, return
2904          */
2905         if (e == NULL)
2906         {
2907                 *xlevel = WARNING;
2908                 return NULL;
2909         }
2910
2911         PyErr_NormalizeException(&e, &v, &tb);
2912         Py_XDECREF(tb);
2913
2914         eob = PyObject_Str(e);
2915         if (v && ((vob = PyObject_Str(v)) != NULL))
2916                 vstr = PyString_AsString(vob);
2917         else
2918                 vstr = "Unknown";
2919
2920         /*
2921          * I'm not sure what to do if eob is NULL here -- we can't call PLy_elog
2922          * because that function calls us, so we could end up with infinite
2923          * recursion.  I'm not even sure if eob could be NULL here -- would an
2924          * Assert() be more appropriate?
2925          */
2926         estr = eob ? PyString_AsString(eob) : "Unknown Exception";
2927         xstr = PLy_printf("%s: %s", estr, vstr);
2928
2929         Py_DECREF(eob);
2930         Py_XDECREF(vob);
2931         Py_XDECREF(v);
2932
2933         /*
2934          * intuit an appropriate error level based on the exception type
2935          */
2936         if (PLy_exc_error && PyErr_GivenExceptionMatches(e, PLy_exc_error))
2937                 *xlevel = ERROR;
2938         else if (PLy_exc_fatal && PyErr_GivenExceptionMatches(e, PLy_exc_fatal))
2939                 *xlevel = FATAL;
2940         else
2941                 *xlevel = ERROR;
2942
2943         Py_DECREF(e);
2944         return xstr;
2945 }
2946
2947 static char *
2948 PLy_printf(const char *fmt,...)
2949 {
2950         va_list         ap;
2951         char       *emsg;
2952
2953         va_start(ap, fmt);
2954         emsg = PLy_vprintf(fmt, ap);
2955         va_end(ap);
2956         return emsg;
2957 }
2958
2959 static char *
2960 PLy_vprintf(const char *fmt, va_list ap)
2961 {
2962         size_t          blen;
2963         int                     bchar,
2964                                 tries = 2;
2965         char       *buf;
2966
2967         blen = strlen(fmt) * 2;
2968         if (blen < 256)
2969                 blen = 256;
2970         buf = PLy_malloc(blen * sizeof(char));
2971
2972         while (1)
2973         {
2974                 bchar = vsnprintf(buf, blen, fmt, ap);
2975                 if (bchar > 0 && bchar < blen)
2976                         return buf;
2977                 if (tries-- <= 0)
2978                         break;
2979                 if (blen > 0)
2980                         blen = bchar + 1;
2981                 else
2982                         blen *= 2;
2983                 buf = PLy_realloc(buf, blen);
2984         }
2985         PLy_free(buf);
2986         return NULL;
2987 }
2988
2989 /* python module code */
2990
2991 /* some dumb utility functions */
2992 static void *
2993 PLy_malloc(size_t bytes)
2994 {
2995         void       *ptr = malloc(bytes);
2996
2997         if (ptr == NULL)
2998                 ereport(FATAL,
2999                                 (errcode(ERRCODE_OUT_OF_MEMORY),
3000                                  errmsg("out of memory")));
3001         return ptr;
3002 }
3003
3004 static void *
3005 PLy_realloc(void *optr, size_t bytes)
3006 {
3007         void       *nptr = realloc(optr, bytes);
3008
3009         if (nptr == NULL)
3010                 ereport(FATAL,
3011                                 (errcode(ERRCODE_OUT_OF_MEMORY),
3012                                  errmsg("out of memory")));
3013         return nptr;
3014 }
3015
3016 static char *
3017 PLy_strdup(const char *str)
3018 {
3019         char       *result;
3020         size_t          len;
3021
3022         len = strlen(str) + 1;
3023         result = PLy_malloc(len);
3024         memcpy(result, str, len);
3025
3026         return result;
3027 }
3028
3029 /* define this away */
3030 static void
3031 PLy_free(void *ptr)
3032 {
3033         free(ptr);
3034 }