Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 97e1535

Browse files
author
Neil Conway
committed
Various cosmetic code cleanup for PL/Python:
- use "bool" rather than "int" for boolean variables - use "PLy_malloc" rather than "malloc" in two places - define "PLy_strdup", and use it rather than malloc() + strcpy() in two places (which should have been memcpy(), anyway). - remove a bunch of redundant parentheses from expressions that do not need the parentheses for code clarity
1 parent 261114a commit 97e1535

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

src/pl/plpython/plpython.c

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
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
{
162162
PyObject_HEAD
163163
/* HeapTuple *tuples; */
164-
PyObject * nrows; /* number of rows returned by query */
164+
PyObject *nrows; /* number of rows returned by query */
165165
PyObject *rows; /* data rows, or None if no data returned */
166166
PyObject *status; /* query status, SPI_OK_*, or SPI_ERR_* */
167167
} PLyResultObject;
@@ -209,6 +209,7 @@ static char *PLy_printf(const char *fmt,...);
209209

210210
static void *PLy_malloc(size_t);
211211
static void *PLy_realloc(void *, size_t);
212+
static char *PLy_strdup(const char *);
212213
static 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
{
421422
TriggerData *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))
425426
rv = PLy_modify_tuple(proc, plargs, tdata, rv);
426427
else
427428
elog(WARNING, "ignoring modified tuple in DELETE trigger");
@@ -781,7 +782,7 @@ PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure * proc)
781782
plrv_sc = PyString_AsString(plrv_so);
782783
rv = FunctionCall3(&proc->result.out.d.typfunc,
783784
PointerGetDatum(plrv_sc),
784-
ObjectIdGetDatum(proc->result.out.d.typioparam),
785+
ObjectIdGetDatum(proc->result.out.d.typioparam),
785786
Int32GetDatum(-1));
786787
}
787788

@@ -824,7 +825,7 @@ PLy_procedure_call(PLyProcedure * proc, char *kargs, PyObject * vargs)
824825
ReThrowError(edata);
825826
}
826827

827-
if ((rv == NULL) || (PyErr_Occurred()))
828+
if (rv == NULL || PyErr_Occurred())
828829
{
829830
Py_XDECREF(rv);
830831
PLy_elog(ERROR, "function \"%s\" failed", proc->proname);
@@ -945,7 +946,7 @@ PLy_procedure_get(FunctionCallInfo fcinfo, Oid tgreloid)
945946
elog(ERROR, "cache lookup failed for function %u", fn_oid);
946947

947948
rv = snprintf(key, sizeof(key), "%u_%u", fn_oid, tgreloid);
948-
if ((rv >= sizeof(key)) || (rv < 0))
949+
if (rv >= sizeof(key) || rv < 0)
949950
elog(ERROR, "key too long");
950951

951952
plproc = PyDict_GetItemString(PLy_procedure_cache, key);
@@ -1002,14 +1003,12 @@ PLy_procedure_create(FunctionCallInfo fcinfo, Oid tgreloid,
10021003
"__plpython_procedure_%s_%u",
10031004
NameStr(procStruct->proname),
10041005
fcinfo->flinfo->fn_oid);
1005-
if ((rv >= sizeof(procName)) || (rv < 0))
1006+
if (rv >= sizeof(procName) || rv < 0)
10061007
elog(ERROR, "procedure name would overrun buffer");
10071008

10081009
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);
10131012
proc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
10141013
proc->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)
11641163
crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
11651164
free(msrc);
11661165

1167-
if ((crv != NULL) && (!PyErr_Occurred()))
1166+
if (crv != NULL && (!PyErr_Occurred()))
11681167
{
11691168
int clen;
11701169
char call[NAMEDATALEN + 256];
@@ -1175,10 +1174,10 @@ PLy_procedure_compile(PLyProcedure * proc, const char *src)
11751174
* compile a call to the function
11761175
*/
11771176
clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
1178-
if ((clen < 0) || (clen >= sizeof(call)))
1177+
if (clen < 0 || clen >= sizeof(call))
11791178
elog(ERROR, "string would overflow buffer");
11801179
proc->code = Py_CompileString(call, "<string>", Py_eval_input);
1181-
if ((proc->code != NULL) && (!PyErr_Occurred()))
1180+
if (proc->code != NULL && (!PyErr_Occurred()))
11821181
return;
11831182
}
11841183
else
@@ -1268,7 +1267,7 @@ PLy_input_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
12681267

12691268
arg->is_rowtype = 1;
12701269
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));
12721271

12731272
for (i = 0; i < desc->natts; i++)
12741273
{
@@ -1302,7 +1301,7 @@ PLy_output_tuple_funcs(PLyTypeInfo * arg, TupleDesc desc)
13021301

13031302
arg->is_rowtype = 1;
13041303
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));
13061305

13071306
for (i = 0; i < desc->natts; i++)
13081307
{
@@ -1425,7 +1424,7 @@ PLyFloat_FromString(const char *src)
14251424

14261425
errno = 0;
14271426
v = strtod(src, &eptr);
1428-
if ((*eptr != '\0') || (errno))
1427+
if (*eptr != '\0' || errno)
14291428
return NULL;
14301429
return PyFloat_FromDouble(v);
14311430
}
@@ -1438,7 +1437,7 @@ PLyInt_FromString(const char *src)
14381437

14391438
errno = 0;
14401439
v = strtol(src, &eptr, 0);
1441-
if ((*eptr != '\0') || (errno))
1440+
if (*eptr != '\0' || errno)
14421441
return NULL;
14431442
return PyInt_FromLong(v);
14441443
}
@@ -1485,7 +1484,7 @@ PLyDict_FromTuple(PLyTypeInfo * info, HeapTuple tuple, TupleDesc desc)
14851484
key = NameStr(desc->attrs[i]->attname);
14861485
vattr = 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)
14891488
PyDict_SetItemString(dict, key, Py_None);
14901489
else
14911490
{
@@ -1860,7 +1859,7 @@ PLy_spi_prepare(PyObject * self, PyObject * args)
18601859
return NULL;
18611860
}
18621861

1863-
if ((list) && (!PySequence_Check(list)))
1862+
if (list && (!PySequence_Check(list)))
18641863
{
18651864
PyErr_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

19831982
PyErr_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))
19871986
return PLy_spi_execute_plan(plan, list, limit);
19881987

19891988
PyErr_SetString(PLy_exc_error, "Expected a query or plan.");
@@ -2002,7 +2001,7 @@ PLy_spi_execute_plan(PyObject * ob, PyObject * list, long limit)
20022001

20032002
if (list != NULL)
20042003
{
2005-
if ((!PySequence_Check(list)) || (PyString_Check(list)))
2004+
if (!PySequence_Check(list) || PyString_Check(list))
20062005
{
20072006
char *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)
22512250
void
22522251
plpython_init(void)
22532252
{
2254-
static volatile int init_active = 0;
2253+
static volatile bool init_active = false;
22552254

22562255
/* Do initialization only once */
22572256
if (!PLy_first_call)
22582257
return;
22592258

22602259
if (init_active)
22612260
elog(FATAL, "initialization of language module failed");
2262-
init_active = 1;
2261+
init_active = true;
22632262

22642263
Py_Initialize();
22652264
PLy_init_interp();
@@ -2270,7 +2269,7 @@ plpython_init(void)
22702269
if (PLy_procedure_cache == NULL)
22712270
PLy_elog(ERROR, "could not create procedure cache");
22722271

2273-
PLy_first_call = 0;
2272+
PLy_first_call = false;
22742273
}
22752274

22762275
static 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

22902288
static void
@@ -2293,14 +2291,14 @@ PLy_init_interp(void)
22932291
PyObject *mainmod;
22942292

22952293
mainmod = PyImport_AddModule("__main__");
2296-
if ((mainmod == NULL) || (PyErr_Occurred()))
2294+
if (mainmod == NULL || PyErr_Occurred())
22972295
PLy_elog(ERROR, "could not import \"__main__\" module.");
22982296
Py_INCREF(mainmod);
22992297
PLy_interp_globals = PyModule_GetDict(mainmod);
23002298
PLy_interp_safe_globals = PyDict_New();
23012299
PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
23022300
Py_DECREF(mainmod);
2303-
if ((PLy_interp_globals == NULL) || (PyErr_Occurred()))
2301+
if (PLy_interp_globals == NULL || PyErr_Occurred())
23042302
PLy_elog(ERROR, "could not initialize globals");
23052303
}
23062304

@@ -2396,7 +2394,7 @@ PLy_output(volatile int level, PyObject * self, PyObject * args)
23962394
MemoryContext oldcontext;
23972395

23982396
so = PyObject_Str(args);
2399-
if ((so == NULL) || ((sv = PyString_AsString(so)) == NULL))
2397+
if (so == NULL || ((sv = PyString_AsString(so)) == NULL))
24002398
{
24012399
level = ERROR;
24022400
sv = "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 returns the SQL name, not the internal Python procedure name
24432441
*/
24442442

24452443
static char *
@@ -2533,7 +2531,7 @@ PLy_traceback(int *xlevel)
25332531
PyErr_NormalizeException(&e, &v, &tb);
25342532

25352533
eob = PyObject_Str(e);
2536-
if ((v) && ((vob = PyObject_Str(v)) != NULL))
2534+
if (v && ((vob = PyObject_Str(v)) != NULL))
25372535
vstr = PyString_AsString(vob);
25382536
else
25392537
vstr = "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;
25602558
else
25612559
*xlevel = ERROR;
@@ -2591,7 +2589,7 @@ PLy_vprintf(const char *fmt, va_list ap)
25912589
while (1)
25922590
{
25932591
bchar = vsnprintf(buf, blen, fmt, ap);
2594-
if ((bchar > 0) && (bchar < blen))
2592+
if (bchar > 0 && bchar < blen)
25952593
return buf;
25962594
if (tries-- <= 0)
25972595
break;
@@ -2636,6 +2634,19 @@ PLy_realloc(void *optr, size_t bytes)
26362634
return 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
*/
26412652
static void

0 commit comments

Comments
 (0)