summaryrefslogtreecommitdiff
path: root/src/pl/plpython/plpy_resultobject.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_resultobject.c
parentc872516d8fe5ba3ce27e7020fad887d90d308e29 (diff)
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_resultobject.c')
-rw-r--r--src/pl/plpython/plpy_resultobject.c109
1 files changed, 70 insertions, 39 deletions
diff --git a/src/pl/plpython/plpy_resultobject.c b/src/pl/plpython/plpy_resultobject.c
index 95acce65493..80fa1d7accc 100644
--- a/src/pl/plpython/plpy_resultobject.c
+++ b/src/pl/plpython/plpy_resultobject.c
@@ -10,7 +10,7 @@
#include "plpy_resultobject.h"
#include "plpython.h"
-static void PLy_result_dealloc(PyObject *arg);
+static void PLy_result_dealloc(PLyResultObject *self);
static PyObject *PLy_result_colnames(PyObject *self, PyObject *unused);
static PyObject *PLy_result_coltypes(PyObject *self, PyObject *unused);
static PyObject *PLy_result_coltypmods(PyObject *self, PyObject *unused);
@@ -24,17 +24,6 @@ static int PLy_result_ass_subscript(PyObject *arg, PyObject *item, PyObject *val
static char PLy_result_doc[] = "Results of a PostgreSQL query";
-static PySequenceMethods PLy_result_as_sequence = {
- .sq_length = PLy_result_length,
- .sq_item = PLy_result_item,
-};
-
-static PyMappingMethods PLy_result_as_mapping = {
- .mp_length = PLy_result_length,
- .mp_subscript = PLy_result_subscript,
- .mp_ass_subscript = PLy_result_ass_subscript,
-};
-
static PyMethodDef PLy_result_methods[] = {
{"colnames", PLy_result_colnames, METH_NOARGS, NULL},
{"coltypes", PLy_result_coltypes, METH_NOARGS, NULL},
@@ -44,23 +33,55 @@ static PyMethodDef PLy_result_methods[] = {
{NULL, NULL, 0, NULL}
};
-static PyTypeObject PLy_ResultType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "PLyResult",
- .tp_basicsize = sizeof(PLyResultObject),
- .tp_dealloc = PLy_result_dealloc,
- .tp_as_sequence = &PLy_result_as_sequence,
- .tp_as_mapping = &PLy_result_as_mapping,
- .tp_str = &PLy_result_str,
- .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- .tp_doc = PLy_result_doc,
- .tp_methods = PLy_result_methods,
+static PyType_Slot PLyResult_slots[] =
+{
+ {
+ Py_tp_dealloc, PLy_result_dealloc
+ },
+ {
+ Py_sq_length, PLy_result_length
+ },
+ {
+ Py_sq_item, PLy_result_item
+ },
+ {
+ Py_mp_length, PLy_result_length
+ },
+ {
+ Py_mp_subscript, PLy_result_subscript
+ },
+ {
+ Py_mp_ass_subscript, PLy_result_ass_subscript
+ },
+ {
+ Py_tp_str, PLy_result_str
+ },
+ {
+ Py_tp_doc, (char *) PLy_result_doc
+ },
+ {
+ Py_tp_methods, PLy_result_methods
+ },
+ {
+ 0, NULL
+ }
+};
+
+static PyType_Spec PLyResult_spec =
+{
+ .name = "PLyResult",
+ .basicsize = sizeof(PLyResultObject),
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ .slots = PLyResult_slots,
};
+static PyTypeObject *PLy_ResultType;
+
void
PLy_result_init_type(void)
{
- if (PyType_Ready(&PLy_ResultType) < 0)
+ PLy_ResultType = (PyTypeObject *) PyType_FromSpec(&PLyResult_spec);
+ if (!PLy_ResultType)
elog(ERROR, "could not initialize PLy_ResultType");
}
@@ -69,8 +90,12 @@ PLy_result_new(void)
{
PLyResultObject *ob;
- if ((ob = PyObject_New(PLyResultObject, &PLy_ResultType)) == NULL)
+ if ((ob = PyObject_New(PLyResultObject, PLy_ResultType)) == NULL)
return NULL;
+#if PY_VERSION_HEX < 0x03080000
+ /* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
+ Py_INCREF(PLy_ResultType);
+#endif
/* ob->tuples = NULL; */
@@ -89,20 +114,26 @@ PLy_result_new(void)
}
static void
-PLy_result_dealloc(PyObject *arg)
+PLy_result_dealloc(PLyResultObject *self)
{
- PLyResultObject *ob = (PLyResultObject *) arg;
-
- Py_XDECREF(ob->nrows);
- Py_XDECREF(ob->rows);
- Py_XDECREF(ob->status);
- if (ob->tupdesc)
+#if PY_VERSION_HEX >= 0x03080000
+ PyTypeObject *tp = Py_TYPE(self);
+#endif
+
+ Py_XDECREF(self->nrows);
+ Py_XDECREF(self->rows);
+ Py_XDECREF(self->status);
+ if (self->tupdesc)
{
- FreeTupleDesc(ob->tupdesc);
- ob->tupdesc = NULL;
+ FreeTupleDesc(self->tupdesc);
+ self->tupdesc = 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
}
static PyObject *
@@ -125,7 +156,7 @@ PLy_result_colnames(PyObject *self, PyObject *unused)
{
Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
- PyList_SET_ITEM(list, i, PLyUnicode_FromString(NameStr(attr->attname)));
+ PyList_SetItem(list, i, PLyUnicode_FromString(NameStr(attr->attname)));
}
return list;
@@ -151,7 +182,7 @@ PLy_result_coltypes(PyObject *self, PyObject *unused)
{
Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
- PyList_SET_ITEM(list, i, PyLong_FromLong(attr->atttypid));
+ PyList_SetItem(list, i, PyLong_FromLong(attr->atttypid));
}
return list;
@@ -177,7 +208,7 @@ PLy_result_coltypmods(PyObject *self, PyObject *unused)
{
Form_pg_attribute attr = TupleDescAttr(ob->tupdesc, i);
- PyList_SET_ITEM(list, i, PyLong_FromLong(attr->atttypmod));
+ PyList_SetItem(list, i, PyLong_FromLong(attr->atttypmod));
}
return list;
@@ -227,7 +258,7 @@ PLy_result_str(PyObject *arg)
PLyResultObject *ob = (PLyResultObject *) arg;
return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>",
- Py_TYPE(ob)->tp_name,
+ "PLyResult",
ob->status,
ob->nrows,
ob->rows);