2929 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
3030 *
3131 * IDENTIFICATION
32- *$PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.66 2005/10/15 02:49:50 momjian Exp $
32+ *$PostgreSQL: pgsql/src/pl/plpython/plpython.c,v 1.67 2005/12/26 04:28:48 neilc Exp $
3333 *
3434 *********************************************************************
3535 */
@@ -161,7 +161,7 @@ typedef struct PLyResultObject
161161{
162162PyObject_HEAD
163163/* HeapTuple *tuples; */
164- PyObject * nrows ;/* number of rows returned by query */
164+ PyObject * nrows ;/* number of rows returned by query */
165165PyObject * rows ;/* data rows, or None if no data returned */
166166PyObject * status ;/* query status, SPI_OK_*, or SPI_ERR_* */
167167}PLyResultObject ;
@@ -209,6 +209,7 @@ static char *PLy_printf(const char *fmt,...);
209209
210210static void * PLy_malloc (size_t );
211211static void * PLy_realloc (void * ,size_t );
212+ static char * PLy_strdup (const char * );
212213static void PLy_free (void * );
213214
214215/* sub handlers for functions and triggers
@@ -256,7 +257,7 @@ static PyObject *PLyString_FromString(const char *);
256257
257258/* global data
258259 */
259- static int PLy_first_call = 1 ;
260+ static bool PLy_first_call = true ;
260261
261262/*
262263 * Currently active plpython function
@@ -420,8 +421,8 @@ PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
420421{
421422TriggerData * tdata = (TriggerData * )fcinfo -> context ;
422423
423- if (( TRIGGER_FIRED_BY_INSERT (tdata -> tg_event ) )||
424- ( TRIGGER_FIRED_BY_UPDATE (tdata -> tg_event ) ))
424+ if (TRIGGER_FIRED_BY_INSERT (tdata -> tg_event )||
425+ TRIGGER_FIRED_BY_UPDATE (tdata -> tg_event ))
425426rv = PLy_modify_tuple (proc ,plargs ,tdata ,rv );
426427else
427428elog (WARNING ,"ignoring modified tuple in DELETE trigger" );
@@ -781,7 +782,7 @@ PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
781782plrv_sc = PyString_AsString (plrv_so );
782783rv = FunctionCall3 (& proc -> result .out .d .typfunc ,
783784PointerGetDatum (plrv_sc ),
784- ObjectIdGetDatum (proc -> result .out .d .typioparam ),
785+ ObjectIdGetDatum (proc -> result .out .d .typioparam ),
785786Int32GetDatum (-1 ));
786787}
787788
@@ -824,7 +825,7 @@ PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
824825ReThrowError (edata );
825826}
826827
827- if (( rv == NULL ) || ( PyErr_Occurred () ))
828+ if (rv == NULL || PyErr_Occurred ())
828829{
829830Py_XDECREF (rv );
830831PLy_elog (ERROR ,"function \"%s\" failed" ,proc -> proname );
@@ -945,7 +946,7 @@ PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
945946elog (ERROR ,"cache lookup failed for function %u" ,fn_oid );
946947
947948rv = snprintf (key ,sizeof (key ),"%u_%u" ,fn_oid ,tgreloid );
948- if (( rv >=sizeof (key )) || ( rv < 0 ) )
949+ if (rv >=sizeof (key )|| rv < 0 )
949950elog (ERROR ,"key too long" );
950951
951952plproc = PyDict_GetItemString (PLy_procedure_cache ,key );
@@ -1002,14 +1003,12 @@ PLy_procedure_create(FunctionCallInfo fcinfo, Oid tgreloid,
10021003"__plpython_procedure_%s_%u" ,
10031004NameStr (procStruct -> proname ),
10041005fcinfo -> flinfo -> fn_oid );
1005- if (( rv >=sizeof (procName )) || ( rv < 0 ) )
1006+ if (rv >=sizeof (procName )|| rv < 0 )
10061007elog (ERROR ,"procedure name would overrun buffer" );
10071008
10081009proc = PLy_malloc (sizeof (PLyProcedure ));
1009- proc -> proname = PLy_malloc (strlen (NameStr (procStruct -> proname ))+ 1 );
1010- strcpy (proc -> proname ,NameStr (procStruct -> proname ));
1011- proc -> pyname = PLy_malloc (strlen (procName )+ 1 );
1012- strcpy (proc -> pyname ,procName );
1010+ proc -> proname = PLy_strdup (NameStr (procStruct -> proname ));
1011+ proc -> pyname = PLy_strdup (procName );
10131012proc -> fn_xmin = HeapTupleHeaderGetXmin (procTup -> t_data );
10141013proc -> fn_cmin = HeapTupleHeaderGetCmin (procTup -> t_data );
10151014/* Remember if function is STABLE/IMMUTABLE */
@@ -1164,7 +1163,7 @@ PLy_procedure_compile(PLyProcedure * proc, const char *src)
11641163crv = PyRun_String (msrc ,Py_file_input ,proc -> globals ,NULL );
11651164free (msrc );
11661165
1167- if (( crv != NULL ) && (!PyErr_Occurred ()))
1166+ if (crv != NULL && (!PyErr_Occurred ()))
11681167{
11691168int clen ;
11701169char call [NAMEDATALEN + 256 ];
@@ -1175,10 +1174,10 @@ PLy_procedure_compile(PLyProcedure * proc, const char *src)
11751174 * compile a call to the function
11761175 */
11771176clen = snprintf (call ,sizeof (call ),"%s()" ,proc -> pyname );
1178- if (( clen < 0 ) || ( clen >=sizeof (call ) ))
1177+ if (clen < 0 || clen >=sizeof (call ))
11791178elog (ERROR ,"string would overflow buffer" );
11801179proc -> code = Py_CompileString (call ,"<string>" ,Py_eval_input );
1181- if (( proc -> code != NULL ) && (!PyErr_Occurred ()))
1180+ if (proc -> code != NULL && (!PyErr_Occurred ()))
11821181return ;
11831182}
11841183else
@@ -1268,7 +1267,7 @@ PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
12681267
12691268arg -> is_rowtype = 1 ;
12701269arg -> in .r .natts = desc -> natts ;
1271- arg -> in .r .atts = malloc (desc -> natts * sizeof (PLyDatumToOb ));
1270+ arg -> in .r .atts = PLy_malloc (desc -> natts * sizeof (PLyDatumToOb ));
12721271
12731272for (i = 0 ;i < desc -> natts ;i ++ )
12741273{
@@ -1302,7 +1301,7 @@ PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
13021301
13031302arg -> is_rowtype = 1 ;
13041303arg -> out .r .natts = desc -> natts ;
1305- arg -> out .r .atts = malloc (desc -> natts * sizeof (PLyDatumToOb ));
1304+ arg -> out .r .atts = PLy_malloc (desc -> natts * sizeof (PLyDatumToOb ));
13061305
13071306for (i = 0 ;i < desc -> natts ;i ++ )
13081307{
@@ -1425,7 +1424,7 @@ PLyFloat_FromString(const char *src)
14251424
14261425errno = 0 ;
14271426v = strtod (src ,& eptr );
1428- if (( * eptr != '\0' ) || ( errno ) )
1427+ if (* eptr != '\0' || errno )
14291428return NULL ;
14301429return PyFloat_FromDouble (v );
14311430}
@@ -1438,7 +1437,7 @@ PLyInt_FromString(const char *src)
14381437
14391438errno = 0 ;
14401439v = strtol (src ,& eptr ,0 );
1441- if (( * eptr != '\0' ) || ( errno ) )
1440+ if (* eptr != '\0' || errno )
14421441return NULL ;
14431442return PyInt_FromLong (v );
14441443}
@@ -1485,7 +1484,7 @@ PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
14851484key = NameStr (desc -> attrs [i ]-> attname );
14861485vattr = heap_getattr (tuple , (i + 1 ),desc ,& is_null );
14871486
1488- if (( is_null ) || ( info -> in .r .atts [i ].func == NULL ) )
1487+ if (is_null || info -> in .r .atts [i ].func == NULL )
14891488PyDict_SetItemString (dict ,key ,Py_None );
14901489else
14911490{
@@ -1860,7 +1859,7 @@ PLy_spi_prepare(PyObject * self, PyObject * args)
18601859return NULL ;
18611860}
18621861
1863- if (( list ) && (!PySequence_Check (list )))
1862+ if (list && (!PySequence_Check (list )))
18641863{
18651864PyErr_SetString (PLy_exc_spi_error ,
18661865"Second argument in plpy.prepare() must be a sequence" );
@@ -1982,8 +1981,8 @@ PLy_spi_execute(PyObject * self, PyObject * args)
19821981
19831982PyErr_Clear ();
19841983
1985- if (( PyArg_ParseTuple (args ,"O|Ol" ,& plan ,& list ,& limit ) )&&
1986- ( is_PLyPlanObject (plan ) ))
1984+ if (PyArg_ParseTuple (args ,"O|Ol" ,& plan ,& list ,& limit )&&
1985+ is_PLyPlanObject (plan ))
19871986return PLy_spi_execute_plan (plan ,list ,limit );
19881987
19891988PyErr_SetString (PLy_exc_error ,"Expected a query or plan." );
@@ -2002,7 +2001,7 @@ PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
20022001
20032002if (list != NULL )
20042003{
2005- if (( !PySequence_Check (list )) || ( PyString_Check (list ) ))
2004+ if (!PySequence_Check (list )|| PyString_Check (list ))
20062005{
20072006char * msg = "plpy.execute() takes a sequence as its second argument" ;
20082007
@@ -2251,15 +2250,15 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
22512250void
22522251plpython_init (void )
22532252{
2254- static volatile int init_active = 0 ;
2253+ static volatile bool init_active = false ;
22552254
22562255/* Do initialization only once */
22572256if (!PLy_first_call )
22582257return ;
22592258
22602259if (init_active )
22612260elog (FATAL ,"initialization of language module failed" );
2262- init_active = 1 ;
2261+ init_active = true ;
22632262
22642263Py_Initialize ();
22652264PLy_init_interp ();
@@ -2270,7 +2269,7 @@ plpython_init(void)
22702269if (PLy_procedure_cache == NULL )
22712270PLy_elog (ERROR ,"could not create procedure cache" );
22722271
2273- PLy_first_call = 0 ;
2272+ PLy_first_call = false ;
22742273}
22752274
22762275static void
@@ -2284,7 +2283,6 @@ PLy_init_all(void)
22842283 * Any other initialization that must be done each time a new backend
22852284 * starts -- currently none
22862285 */
2287-
22882286}
22892287
22902288static void
@@ -2293,14 +2291,14 @@ PLy_init_interp(void)
22932291PyObject * mainmod ;
22942292
22952293mainmod = PyImport_AddModule ("__main__" );
2296- if (( mainmod == NULL ) || ( PyErr_Occurred () ))
2294+ if (mainmod == NULL || PyErr_Occurred ())
22972295PLy_elog (ERROR ,"could not import \"__main__\" module." );
22982296Py_INCREF (mainmod );
22992297PLy_interp_globals = PyModule_GetDict (mainmod );
23002298PLy_interp_safe_globals = PyDict_New ();
23012299PyDict_SetItemString (PLy_interp_globals ,"GD" ,PLy_interp_safe_globals );
23022300Py_DECREF (mainmod );
2303- if (( PLy_interp_globals == NULL ) || ( PyErr_Occurred () ))
2301+ if (PLy_interp_globals == NULL || PyErr_Occurred ())
23042302PLy_elog (ERROR ,"could not initialize globals" );
23052303}
23062304
@@ -2396,7 +2394,7 @@ PLy_output(volatile int level, PyObject * self, PyObject * args)
23962394MemoryContext oldcontext ;
23972395
23982396so = PyObject_Str (args );
2399- if (( so == NULL ) || ((sv = PyString_AsString (so ))== NULL ))
2397+ if (so == NULL || ((sv = PyString_AsString (so ))== NULL ))
24002398{
24012399level = ERROR ;
24022400sv = "Unable to parse error message in `plpy.elog'" ;
@@ -2439,7 +2437,7 @@ PLy_output(volatile int level, PyObject * self, PyObject * args)
24392437 * If a plpython procedure call calls the backend and the backend calls
24402438 * another plpython procedure )
24412439 *
2442- * NB: this returns SQL name, not the internal Python procedure name
2440+ * NB: this returnsthe SQL name, not the internal Python procedure name
24432441 */
24442442
24452443static char *
@@ -2533,7 +2531,7 @@ PLy_traceback(int *xlevel)
25332531PyErr_NormalizeException (& e ,& v ,& tb );
25342532
25352533eob = PyObject_Str (e );
2536- if (( v ) && ((vob = PyObject_Str (v ))!= NULL ))
2534+ if (v && ((vob = PyObject_Str (v ))!= NULL ))
25372535vstr = PyString_AsString (vob );
25382536else
25392537vstr = "Unknown" ;
@@ -2553,9 +2551,9 @@ PLy_traceback(int *xlevel)
25532551/*
25542552 * intuit an appropriate error level for based on the exception type
25552553 */
2556- if (( PLy_exc_error ) && ( PyErr_GivenExceptionMatches (e ,PLy_exc_error ) ))
2554+ if (PLy_exc_error && PyErr_GivenExceptionMatches (e ,PLy_exc_error ))
25572555* xlevel = ERROR ;
2558- else if (( PLy_exc_fatal ) && ( PyErr_GivenExceptionMatches (e ,PLy_exc_fatal ) ))
2556+ else if (PLy_exc_fatal && PyErr_GivenExceptionMatches (e ,PLy_exc_fatal ))
25592557* xlevel = FATAL ;
25602558else
25612559* xlevel = ERROR ;
@@ -2591,7 +2589,7 @@ PLy_vprintf(const char *fmt, va_list ap)
25912589while (1 )
25922590{
25932591bchar = vsnprintf (buf ,blen ,fmt ,ap );
2594- if (( bchar > 0 ) && ( bchar < blen ) )
2592+ if (bchar > 0 && bchar < blen )
25952593return buf ;
25962594if (tries -- <=0 )
25972595break ;
@@ -2636,6 +2634,19 @@ PLy_realloc(void *optr, size_t bytes)
26362634return nptr ;
26372635}
26382636
2637+ static char *
2638+ PLy_strdup (const char * str )
2639+ {
2640+ char * result ;
2641+ size_t len ;
2642+
2643+ len = strlen (str )+ 1 ;
2644+ result = PLy_malloc (len );
2645+ memcpy (result ,str ,len );
2646+
2647+ return result ;
2648+ }
2649+
26392650/* define this away
26402651 */
26412652static void