aboutsummaryrefslogtreecommitdiff
path: root/src/pl/plpython/plpy_planobject.c
diff options
context:
space:
mode:
authorPeter Eisentraut <peter@eisentraut.org>2025-03-12 08:49:37 +0100
committerPeter Eisentraut <peter@eisentraut.org>2025-03-12 08:53:54 +0100
commit72a3d0462b9a7f6265267950668af0c0246e7c01 (patch)
tree8360cb6fc037f984a888458f8f978471ee81b98e /src/pl/plpython/plpy_planobject.c
parentc872516d8fe5ba3ce27e7020fad887d90d308e29 (diff)
downloadpostgresql-72a3d0462b9a7f6265267950668af0c0246e7c01.tar.gz
postgresql-72a3d0462b9a7f6265267950668af0c0246e7c01.zip
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(). This was previously committed as c47e8df815c and then reverted because it wasn't working under Python older than 3.8. That has been fixed in this version. There was a Python API change/bugfix between 3.7 and 3.8 that directly affects this patch. The relevant commit is <https://github.com/python/cpython/commit/364f0b0f19c>. The workarounds described there have been applied in this patch, and it has been confirmed to work with Python 3.6 and 3.7. Reviewed-by: Jakob Egger <jakob@eggerapps.at> Discussion: https://www.postgresql.org/message-id/flat/ee410de1-1e0b-4770-b125-eeefd4726a24@eisentraut.org
Diffstat (limited to 'src/pl/plpython/plpy_planobject.c')
-rw-r--r--src/pl/plpython/plpy_planobject.c70
1 files changed, 49 insertions, 21 deletions
diff --git a/src/pl/plpython/plpy_planobject.c b/src/pl/plpython/plpy_planobject.c
index 9427674d2f4..3e385555e5e 100644
--- a/src/pl/plpython/plpy_planobject.c
+++ b/src/pl/plpython/plpy_planobject.c
@@ -12,7 +12,7 @@
#include "plpython.h"
#include "utils/memutils.h"
-static void PLy_plan_dealloc(PyObject *arg);
+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);
@@ -26,20 +26,37 @@ static PyMethodDef PLy_plan_methods[] = {
{NULL, NULL, 0, NULL}
};
-static PyTypeObject PLy_PlanType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "PLyPlan",
- .tp_basicsize = sizeof(PLyPlanObject),
- .tp_dealloc = PLy_plan_dealloc,
- .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- .tp_doc = PLy_plan_doc,
- .tp_methods = PLy_plan_methods,
+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)
{
- if (PyType_Ready(&PLy_PlanType) < 0)
+ PLy_PlanType = (PyTypeObject *) PyType_FromSpec(&PLyPlan_spec);
+ if (!PLy_PlanType)
elog(ERROR, "could not initialize PLy_PlanType");
}
@@ -48,8 +65,12 @@ PLy_plan_new(void)
{
PLyPlanObject *ob;
- if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
+ if ((ob = PyObject_New(PLyPlanObject, PLy_PlanType)) == NULL)
return NULL;
+#if PY_VERSION_HEX < 0x03080000
+ /* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
+ Py_INCREF(PLy_PlanType);
+#endif
ob->plan = NULL;
ob->nargs = 0;
@@ -63,25 +84,32 @@ PLy_plan_new(void)
bool
is_PLyPlanObject(PyObject *ob)
{
- return ob->ob_type == &PLy_PlanType;
+ return ob->ob_type == PLy_PlanType;
}
static void
-PLy_plan_dealloc(PyObject *arg)
+PLy_plan_dealloc(PLyPlanObject *self)
{
- PLyPlanObject *ob = (PLyPlanObject *) arg;
+#if PY_VERSION_HEX >= 0x03080000
+ PyTypeObject *tp = Py_TYPE(self);
+#endif
- if (ob->plan)
+ if (self->plan)
{
- SPI_freeplan(ob->plan);
- ob->plan = NULL;
+ SPI_freeplan(self->plan);
+ self->plan = NULL;
}
- if (ob->mcxt)
+ if (self->mcxt)
{
- MemoryContextDelete(ob->mcxt);
- ob->mcxt = NULL;
+ MemoryContextDelete(self->mcxt);
+ self->mcxt = NULL;
}
- arg->ob_type->tp_free(arg);
+
+ PyObject_Free(self);
+#if PY_VERSION_HEX >= 0x03080000
+ /* This was not needed before Python 3.8 (Python issue 35810) */
+ Py_DECREF(tp);
+#endif
}