aboutsummaryrefslogtreecommitdiff
path: root/src/pl/plpython/plpy_cursorobject.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_cursorobject.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_cursorobject.c')
-rw-r--r--src/pl/plpython/plpy_cursorobject.c84
1 files changed, 59 insertions, 25 deletions
diff --git a/src/pl/plpython/plpy_cursorobject.c b/src/pl/plpython/plpy_cursorobject.c
index bb3fa8a3909..1c6be756120 100644
--- a/src/pl/plpython/plpy_cursorobject.c
+++ b/src/pl/plpython/plpy_cursorobject.c
@@ -20,7 +20,7 @@
#include "utils/memutils.h"
static PyObject *PLy_cursor_query(const char *query);
-static void PLy_cursor_dealloc(PyObject *arg);
+static void PLy_cursor_dealloc(PLyCursorObject *self);
static PyObject *PLy_cursor_iternext(PyObject *self);
static PyObject *PLy_cursor_fetch(PyObject *self, PyObject *args);
static PyObject *PLy_cursor_close(PyObject *self, PyObject *unused);
@@ -33,22 +33,43 @@ static PyMethodDef PLy_cursor_methods[] = {
{NULL, NULL, 0, NULL}
};
-static PyTypeObject PLy_CursorType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "PLyCursor",
- .tp_basicsize = sizeof(PLyCursorObject),
- .tp_dealloc = PLy_cursor_dealloc,
- .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
- .tp_doc = PLy_cursor_doc,
- .tp_iter = PyObject_SelfIter,
- .tp_iternext = PLy_cursor_iternext,
- .tp_methods = PLy_cursor_methods,
+static PyType_Slot PLyCursor_slots[] =
+{
+ {
+ Py_tp_dealloc, PLy_cursor_dealloc
+ },
+ {
+ Py_tp_doc, (char *) PLy_cursor_doc
+ },
+ {
+ Py_tp_iter, PyObject_SelfIter
+ },
+ {
+ Py_tp_iternext, PLy_cursor_iternext
+ },
+ {
+ Py_tp_methods, PLy_cursor_methods
+ },
+ {
+ 0, NULL
+ }
};
+static PyType_Spec PLyCursor_spec =
+{
+ .name = "PLyCursor",
+ .basicsize = sizeof(PLyCursorObject),
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ .slots = PLyCursor_slots,
+};
+
+static PyTypeObject *PLy_CursorType;
+
void
PLy_cursor_init_type(void)
{
- if (PyType_Ready(&PLy_CursorType) < 0)
+ PLy_CursorType = (PyTypeObject *) PyType_FromSpec(&PLyCursor_spec);
+ if (!PLy_CursorType)
elog(ERROR, "could not initialize PLy_CursorType");
}
@@ -80,8 +101,12 @@ PLy_cursor_query(const char *query)
volatile MemoryContext oldcontext;
volatile ResourceOwner oldowner;
- if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
+ if ((cursor = PyObject_New(PLyCursorObject, PLy_CursorType)) == NULL)
return NULL;
+#if PY_VERSION_HEX < 0x03080000
+ /* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
+ Py_INCREF(PLy_CursorType);
+#endif
cursor->portalname = NULL;
cursor->closed = false;
cursor->mcxt = AllocSetContextCreate(TopMemoryContext,
@@ -177,8 +202,12 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
return NULL;
}
- if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
+ if ((cursor = PyObject_New(PLyCursorObject, PLy_CursorType)) == NULL)
return NULL;
+#if PY_VERSION_HEX < 0x03080000
+ /* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
+ Py_INCREF(PLy_CursorType);
+#endif
cursor->portalname = NULL;
cursor->closed = false;
cursor->mcxt = AllocSetContextCreate(TopMemoryContext,
@@ -272,30 +301,35 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
}
static void
-PLy_cursor_dealloc(PyObject *arg)
+PLy_cursor_dealloc(PLyCursorObject *self)
{
- PLyCursorObject *cursor;
+#if PY_VERSION_HEX >= 0x03080000
+ PyTypeObject *tp = Py_TYPE(self);
+#endif
Portal portal;
- cursor = (PLyCursorObject *) arg;
-
- if (!cursor->closed)
+ if (!self->closed)
{
- portal = GetPortalByName(cursor->portalname);
+ portal = GetPortalByName(self->portalname);
if (PortalIsValid(portal))
{
UnpinPortal(portal);
SPI_cursor_close(portal);
}
- cursor->closed = true;
+ self->closed = true;
}
- if (cursor->mcxt)
+ if (self->mcxt)
{
- MemoryContextDelete(cursor->mcxt);
- cursor->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
}
static PyObject *