summaryrefslogtreecommitdiff
path: root/src/pl/plpython/plpy_subxactobject.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_subxactobject.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_subxactobject.c')
-rw-r--r--src/pl/plpython/plpy_subxactobject.c46
1 files changed, 28 insertions, 18 deletions
diff --git a/src/pl/plpython/plpy_subxactobject.c b/src/pl/plpython/plpy_subxactobject.c
index 5c92a0e089a..ad24a9fc4b8 100644
--- a/src/pl/plpython/plpy_subxactobject.c
+++ b/src/pl/plpython/plpy_subxactobject.c
@@ -15,7 +15,6 @@
List *explicit_subtransactions = NIL;
-static void PLy_subtransaction_dealloc(PyObject *subxact);
static PyObject *PLy_subtransaction_enter(PyObject *self, PyObject *unused);
static PyObject *PLy_subtransaction_exit(PyObject *self, PyObject *args);
@@ -31,21 +30,35 @@ static PyMethodDef PLy_subtransaction_methods[] = {
{NULL, NULL, 0, NULL}
};
-static PyTypeObject PLy_SubtransactionType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "PLySubtransaction",
- .tp_basicsize = sizeof(PLySubtransactionObject),
- .tp_dealloc = PLy_subtransaction_dealloc,
- .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- .tp_doc = PLy_subtransaction_doc,
- .tp_methods = PLy_subtransaction_methods,
+static PyType_Slot PLySubtransaction_slots[] =
+{
+ {
+ Py_tp_doc, (char *) PLy_subtransaction_doc
+ },
+ {
+ Py_tp_methods, PLy_subtransaction_methods
+ },
+ {
+ 0, NULL
+ }
};
+static PyType_Spec PLySubtransaction_spec =
+{
+ .name = "PLySubtransaction",
+ .basicsize = sizeof(PLySubtransactionObject),
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ .slots = PLySubtransaction_slots,
+};
+
+static PyTypeObject *PLy_SubtransactionType;
+
void
PLy_subtransaction_init_type(void)
{
- if (PyType_Ready(&PLy_SubtransactionType) < 0)
+ PLy_SubtransactionType = (PyTypeObject *) PyType_FromSpec(&PLySubtransaction_spec);
+ if (!PLy_SubtransactionType)
elog(ERROR, "could not initialize PLy_SubtransactionType");
}
@@ -55,10 +68,13 @@ PLy_subtransaction_new(PyObject *self, PyObject *unused)
{
PLySubtransactionObject *ob;
- ob = PyObject_New(PLySubtransactionObject, &PLy_SubtransactionType);
-
+ ob = PyObject_New(PLySubtransactionObject, PLy_SubtransactionType);
if (ob == NULL)
return NULL;
+#if PY_VERSION_HEX < 0x03080000
+ /* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
+ Py_INCREF(PLy_SubtransactionType);
+#endif
ob->started = false;
ob->exited = false;
@@ -66,12 +82,6 @@ PLy_subtransaction_new(PyObject *self, PyObject *unused)
return (PyObject *) ob;
}
-/* Python requires a dealloc function to be defined */
-static void
-PLy_subtransaction_dealloc(PyObject *subxact)
-{
-}
-
/*
* subxact.__enter__() or subxact.enter()
*