postgres/src/pl/plpython/plpy_planobject.c
Peter Eisentraut c47e8df815 Prepare for Python "Limited API" in PL/Python
Using the Python Limited API would allow building PL/Python against
any Python 3.x version and using another Python 3.x version at run
time.  This commit does not activate that, but it prepares the code to
only use APIs supported by the Limited API.

Implementation details:

- Convert static types to heap types
  (https://docs.python.org/3/howto/isolating-extensions.html#heap-types).

- Replace PyRun_String() with component functions.

- Replace PyList_SET_ITEM() with PyList_SetItem().

Reviewed-by: Jakob Egger <jakob@eggerapps.at>
Discussion: https://www.postgresql.org/message-id/flat/ee410de1-1e0b-4770-b125-eeefd4726a24@eisentraut.org
2025-02-26 16:14:39 +01:00

143 lines
2.5 KiB
C

/*
* the PLyPlan class
*
* src/pl/plpython/plpy_planobject.c
*/
#include "postgres.h"
#include "plpy_cursorobject.h"
#include "plpy_planobject.h"
#include "plpy_spi.h"
#include "plpython.h"
#include "utils/memutils.h"
static void PLy_plan_dealloc(PLyPlanObject *self);
static PyObject *PLy_plan_cursor(PyObject *self, PyObject *args);
static PyObject *PLy_plan_execute(PyObject *self, PyObject *args);
static PyObject *PLy_plan_status(PyObject *self, PyObject *args);
static char PLy_plan_doc[] = "Store a PostgreSQL plan";
static PyMethodDef PLy_plan_methods[] = {
{"cursor", PLy_plan_cursor, METH_VARARGS, NULL},
{"execute", PLy_plan_execute, METH_VARARGS, NULL},
{"status", PLy_plan_status, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
static PyType_Slot PLyPlan_slots[] =
{
{
Py_tp_dealloc, PLy_plan_dealloc
},
{
Py_tp_doc, (char *) PLy_plan_doc
},
{
Py_tp_methods, PLy_plan_methods
},
{
0, NULL
}
};
static PyType_Spec PLyPlan_spec =
{
.name = "PLyPlan",
.basicsize = sizeof(PLyPlanObject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.slots = PLyPlan_slots,
};
static PyTypeObject *PLy_PlanType;
void
PLy_plan_init_type(void)
{
PLy_PlanType = (PyTypeObject *) PyType_FromSpec(&PLyPlan_spec);
if (!PLy_PlanType)
elog(ERROR, "could not initialize PLy_PlanType");
}
PyObject *
PLy_plan_new(void)
{
PLyPlanObject *ob;
if ((ob = PyObject_New(PLyPlanObject, PLy_PlanType)) == NULL)
return NULL;
ob->plan = NULL;
ob->nargs = 0;
ob->types = NULL;
ob->args = NULL;
ob->mcxt = NULL;
return (PyObject *) ob;
}
bool
is_PLyPlanObject(PyObject *ob)
{
return ob->ob_type == PLy_PlanType;
}
static void
PLy_plan_dealloc(PLyPlanObject *self)
{
PyTypeObject *tp = Py_TYPE(self);
if (self->plan)
{
SPI_freeplan(self->plan);
self->plan = NULL;
}
if (self->mcxt)
{
MemoryContextDelete(self->mcxt);
self->mcxt = NULL;
}
PyObject_Free(self);
Py_DECREF(tp);
}
static PyObject *
PLy_plan_cursor(PyObject *self, PyObject *args)
{
PyObject *planargs = NULL;
if (!PyArg_ParseTuple(args, "|O", &planargs))
return NULL;
return PLy_cursor_plan(self, planargs);
}
static PyObject *
PLy_plan_execute(PyObject *self, PyObject *args)
{
PyObject *list = NULL;
long limit = 0;
if (!PyArg_ParseTuple(args, "|Ol", &list, &limit))
return NULL;
return PLy_spi_execute_plan(self, list, limit);
}
static PyObject *
PLy_plan_status(PyObject *self, PyObject *args)
{
if (PyArg_ParseTuple(args, ":status"))
{
Py_INCREF(Py_True);
return Py_True;
/* return PyLong_FromLong(self->status); */
}
return NULL;
}