29
29
* MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
30
30
*
31
31
* 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 $
33
33
*
34
34
*********************************************************************
35
35
*/
@@ -161,7 +161,7 @@ typedef struct PLyResultObject
161
161
{
162
162
PyObject_HEAD
163
163
/* HeapTuple *tuples; */
164
- PyObject * nrows ; /* number of rows returned by query */
164
+ PyObject * nrows ; /* number of rows returned by query */
165
165
PyObject * rows ; /* data rows, or None if no data returned */
166
166
PyObject * status ; /* query status, SPI_OK_*, or SPI_ERR_* */
167
167
} PLyResultObject ;
@@ -209,6 +209,7 @@ static char *PLy_printf(const char *fmt,...);
209
209
210
210
static void * PLy_malloc (size_t );
211
211
static void * PLy_realloc (void * , size_t );
212
+ static char * PLy_strdup (const char * );
212
213
static void PLy_free (void * );
213
214
214
215
/* sub handlers for functions and triggers
@@ -256,7 +257,7 @@ static PyObject *PLyString_FromString(const char *);
256
257
257
258
/* global data
258
259
*/
259
- static int PLy_first_call = 1 ;
260
+ static bool PLy_first_call = true ;
260
261
261
262
/*
262
263
* Currently active plpython function
@@ -420,8 +421,8 @@ PLy_trigger_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
420
421
{
421
422
TriggerData * tdata = (TriggerData * ) fcinfo -> context ;
422
423
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 ))
425
426
rv = PLy_modify_tuple (proc , plargs , tdata , rv );
426
427
else
427
428
elog (WARNING , "ignoring modified tuple in DELETE trigger" );
@@ -781,7 +782,7 @@ PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
781
782
plrv_sc = PyString_AsString (plrv_so );
782
783
rv = FunctionCall3 (& proc -> result .out .d .typfunc ,
783
784
PointerGetDatum (plrv_sc ),
784
- ObjectIdGetDatum (proc -> result .out .d .typioparam ),
785
+ ObjectIdGetDatum (proc -> result .out .d .typioparam ),
785
786
Int32GetDatum (-1 ));
786
787
}
787
788
@@ -824,7 +825,7 @@ PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
824
825
ReThrowError (edata );
825
826
}
826
827
827
- if (( rv == NULL ) || ( PyErr_Occurred () ))
828
+ if (rv == NULL || PyErr_Occurred ())
828
829
{
829
830
Py_XDECREF (rv );
830
831
PLy_elog (ERROR , "function \"%s\" failed" , proc -> proname );
@@ -945,7 +946,7 @@ PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
945
946
elog (ERROR , "cache lookup failed for function %u" , fn_oid );
946
947
947
948
rv = snprintf (key , sizeof (key ), "%u_%u" , fn_oid , tgreloid );
948
- if (( rv >= sizeof (key )) || ( rv < 0 ) )
949
+ if (rv >= sizeof (key ) || rv < 0 )
949
950
elog (ERROR , "key too long" );
950
951
951
952
plproc = PyDict_GetItemString (PLy_procedure_cache , key );
@@ -1002,14 +1003,12 @@ PLy_procedure_create(FunctionCallInfo fcinfo, Oid tgreloid,
1002
1003
"__plpython_procedure_%s_%u" ,
1003
1004
NameStr (procStruct -> proname ),
1004
1005
fcinfo -> flinfo -> fn_oid );
1005
- if (( rv >= sizeof (procName )) || ( rv < 0 ) )
1006
+ if (rv >= sizeof (procName ) || rv < 0 )
1006
1007
elog (ERROR , "procedure name would overrun buffer" );
1007
1008
1008
1009
proc = 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 );
1013
1012
proc -> fn_xmin = HeapTupleHeaderGetXmin (procTup -> t_data );
1014
1013
proc -> fn_cmin = HeapTupleHeaderGetCmin (procTup -> t_data );
1015
1014
/* Remember if function is STABLE/IMMUTABLE */
@@ -1164,7 +1163,7 @@ PLy_procedure_compile(PLyProcedure * proc, const char *src)
1164
1163
crv = PyRun_String (msrc , Py_file_input , proc -> globals , NULL );
1165
1164
free (msrc );
1166
1165
1167
- if (( crv != NULL ) && (!PyErr_Occurred ()))
1166
+ if (crv != NULL && (!PyErr_Occurred ()))
1168
1167
{
1169
1168
int clen ;
1170
1169
char call [NAMEDATALEN + 256 ];
@@ -1175,10 +1174,10 @@ PLy_procedure_compile(PLyProcedure * proc, const char *src)
1175
1174
* compile a call to the function
1176
1175
*/
1177
1176
clen = snprintf (call , sizeof (call ), "%s()" , proc -> pyname );
1178
- if (( clen < 0 ) || ( clen >= sizeof (call ) ))
1177
+ if (clen < 0 || clen >= sizeof (call ))
1179
1178
elog (ERROR , "string would overflow buffer" );
1180
1179
proc -> code = Py_CompileString (call , "<string>" , Py_eval_input );
1181
- if (( proc -> code != NULL ) && (!PyErr_Occurred ()))
1180
+ if (proc -> code != NULL && (!PyErr_Occurred ()))
1182
1181
return ;
1183
1182
}
1184
1183
else
@@ -1268,7 +1267,7 @@ PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1268
1267
1269
1268
arg -> is_rowtype = 1 ;
1270
1269
arg -> 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 ));
1272
1271
1273
1272
for (i = 0 ; i < desc -> natts ; i ++ )
1274
1273
{
@@ -1302,7 +1301,7 @@ PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
1302
1301
1303
1302
arg -> is_rowtype = 1 ;
1304
1303
arg -> 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 ));
1306
1305
1307
1306
for (i = 0 ; i < desc -> natts ; i ++ )
1308
1307
{
@@ -1425,7 +1424,7 @@ PLyFloat_FromString(const char *src)
1425
1424
1426
1425
errno = 0 ;
1427
1426
v = strtod (src , & eptr );
1428
- if (( * eptr != '\0' ) || ( errno ) )
1427
+ if (* eptr != '\0' || errno )
1429
1428
return NULL ;
1430
1429
return PyFloat_FromDouble (v );
1431
1430
}
@@ -1438,7 +1437,7 @@ PLyInt_FromString(const char *src)
1438
1437
1439
1438
errno = 0 ;
1440
1439
v = strtol (src , & eptr , 0 );
1441
- if (( * eptr != '\0' ) || ( errno ) )
1440
+ if (* eptr != '\0' || errno )
1442
1441
return NULL ;
1443
1442
return PyInt_FromLong (v );
1444
1443
}
@@ -1485,7 +1484,7 @@ PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
1485
1484
key = NameStr (desc -> attrs [i ]-> attname );
1486
1485
vattr = heap_getattr (tuple , (i + 1 ), desc , & is_null );
1487
1486
1488
- if (( is_null ) || ( info -> in .r .atts [i ].func == NULL ) )
1487
+ if (is_null || info -> in .r .atts [i ].func == NULL )
1489
1488
PyDict_SetItemString (dict , key , Py_None );
1490
1489
else
1491
1490
{
@@ -1860,7 +1859,7 @@ PLy_spi_prepare(PyObject * self, PyObject * args)
1860
1859
return NULL ;
1861
1860
}
1862
1861
1863
- if (( list ) && (!PySequence_Check (list )))
1862
+ if (list && (!PySequence_Check (list )))
1864
1863
{
1865
1864
PyErr_SetString (PLy_exc_spi_error ,
1866
1865
"Second argument in plpy.prepare() must be a sequence" );
@@ -1982,8 +1981,8 @@ PLy_spi_execute(PyObject * self, PyObject * args)
1982
1981
1983
1982
PyErr_Clear ();
1984
1983
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 ))
1987
1986
return PLy_spi_execute_plan (plan , list , limit );
1988
1987
1989
1988
PyErr_SetString (PLy_exc_error , "Expected a query or plan." );
@@ -2002,7 +2001,7 @@ PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
2002
2001
2003
2002
if (list != NULL )
2004
2003
{
2005
- if (( !PySequence_Check (list )) || ( PyString_Check (list ) ))
2004
+ if (!PySequence_Check (list ) || PyString_Check (list ))
2006
2005
{
2007
2006
char * msg = "plpy.execute() takes a sequence as its second argument" ;
2008
2007
@@ -2251,15 +2250,15 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int rows, int status)
2251
2250
void
2252
2251
plpython_init (void )
2253
2252
{
2254
- static volatile int init_active = 0 ;
2253
+ static volatile bool init_active = false ;
2255
2254
2256
2255
/* Do initialization only once */
2257
2256
if (!PLy_first_call )
2258
2257
return ;
2259
2258
2260
2259
if (init_active )
2261
2260
elog (FATAL , "initialization of language module failed" );
2262
- init_active = 1 ;
2261
+ init_active = true ;
2263
2262
2264
2263
Py_Initialize ();
2265
2264
PLy_init_interp ();
@@ -2270,7 +2269,7 @@ plpython_init(void)
2270
2269
if (PLy_procedure_cache == NULL )
2271
2270
PLy_elog (ERROR , "could not create procedure cache" );
2272
2271
2273
- PLy_first_call = 0 ;
2272
+ PLy_first_call = false ;
2274
2273
}
2275
2274
2276
2275
static void
@@ -2284,7 +2283,6 @@ PLy_init_all(void)
2284
2283
* Any other initialization that must be done each time a new backend
2285
2284
* starts -- currently none
2286
2285
*/
2287
-
2288
2286
}
2289
2287
2290
2288
static void
@@ -2293,14 +2291,14 @@ PLy_init_interp(void)
2293
2291
PyObject * mainmod ;
2294
2292
2295
2293
mainmod = PyImport_AddModule ("__main__" );
2296
- if (( mainmod == NULL ) || ( PyErr_Occurred () ))
2294
+ if (mainmod == NULL || PyErr_Occurred ())
2297
2295
PLy_elog (ERROR , "could not import \"__main__\" module." );
2298
2296
Py_INCREF (mainmod );
2299
2297
PLy_interp_globals = PyModule_GetDict (mainmod );
2300
2298
PLy_interp_safe_globals = PyDict_New ();
2301
2299
PyDict_SetItemString (PLy_interp_globals , "GD" , PLy_interp_safe_globals );
2302
2300
Py_DECREF (mainmod );
2303
- if (( PLy_interp_globals == NULL ) || ( PyErr_Occurred () ))
2301
+ if (PLy_interp_globals == NULL || PyErr_Occurred ())
2304
2302
PLy_elog (ERROR , "could not initialize globals" );
2305
2303
}
2306
2304
@@ -2396,7 +2394,7 @@ PLy_output(volatile int level, PyObject * self, PyObject * args)
2396
2394
MemoryContext oldcontext ;
2397
2395
2398
2396
so = PyObject_Str (args );
2399
- if (( so == NULL ) || ((sv = PyString_AsString (so )) == NULL ))
2397
+ if (so == NULL || ((sv = PyString_AsString (so )) == NULL ))
2400
2398
{
2401
2399
level = ERROR ;
2402
2400
sv = "Unable to parse error message in `plpy.elog'" ;
@@ -2439,7 +2437,7 @@ PLy_output(volatile int level, PyObject * self, PyObject * args)
2439
2437
* If a plpython procedure call calls the backend and the backend calls
2440
2438
* another plpython procedure )
2441
2439
*
2442
- * NB: this returns SQL name, not the internal Python procedure name
2440
+ * NB: this returns the SQL name, not the internal Python procedure name
2443
2441
*/
2444
2442
2445
2443
static char *
@@ -2533,7 +2531,7 @@ PLy_traceback(int *xlevel)
2533
2531
PyErr_NormalizeException (& e , & v , & tb );
2534
2532
2535
2533
eob = PyObject_Str (e );
2536
- if (( v ) && ((vob = PyObject_Str (v )) != NULL ))
2534
+ if (v && ((vob = PyObject_Str (v )) != NULL ))
2537
2535
vstr = PyString_AsString (vob );
2538
2536
else
2539
2537
vstr = "Unknown" ;
@@ -2553,9 +2551,9 @@ PLy_traceback(int *xlevel)
2553
2551
/*
2554
2552
* intuit an appropriate error level for based on the exception type
2555
2553
*/
2556
- if (( PLy_exc_error ) && ( PyErr_GivenExceptionMatches (e , PLy_exc_error ) ))
2554
+ if (PLy_exc_error && PyErr_GivenExceptionMatches (e , PLy_exc_error ))
2557
2555
* 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 ))
2559
2557
* xlevel = FATAL ;
2560
2558
else
2561
2559
* xlevel = ERROR ;
@@ -2591,7 +2589,7 @@ PLy_vprintf(const char *fmt, va_list ap)
2591
2589
while (1 )
2592
2590
{
2593
2591
bchar = vsnprintf (buf , blen , fmt , ap );
2594
- if (( bchar > 0 ) && ( bchar < blen ) )
2592
+ if (bchar > 0 && bchar < blen )
2595
2593
return buf ;
2596
2594
if (tries -- <= 0 )
2597
2595
break ;
@@ -2636,6 +2634,19 @@ PLy_realloc(void *optr, size_t bytes)
2636
2634
return nptr ;
2637
2635
}
2638
2636
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
+
2639
2650
/* define this away
2640
2651
*/
2641
2652
static void
0 commit comments