Skip to content

Commit 72a3d04

Browse files
committed
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 c47e8df 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 <python/cpython@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 <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent c872516 commit 72a3d04

File tree

6 files changed

+213
-107
lines changed

6 files changed

+213
-107
lines changed

src/pl/plpython/plpy_cursorobject.c

+59-25
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "utils/memutils.h"
2121

2222
static PyObject *PLy_cursor_query(const char *query);
23-
static void PLy_cursor_dealloc(PyObject *arg);
23+
static void PLy_cursor_dealloc(PLyCursorObject *self);
2424
static PyObject *PLy_cursor_iternext(PyObject *self);
2525
static PyObject *PLy_cursor_fetch(PyObject *self, PyObject *args);
2626
static PyObject *PLy_cursor_close(PyObject *self, PyObject *unused);
@@ -33,22 +33,43 @@ static PyMethodDef PLy_cursor_methods[] = {
3333
{NULL, NULL, 0, NULL}
3434
};
3535

36-
static PyTypeObject PLy_CursorType = {
37-
PyVarObject_HEAD_INIT(NULL, 0)
38-
.tp_name = "PLyCursor",
39-
.tp_basicsize = sizeof(PLyCursorObject),
40-
.tp_dealloc = PLy_cursor_dealloc,
41-
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
42-
.tp_doc = PLy_cursor_doc,
43-
.tp_iter = PyObject_SelfIter,
44-
.tp_iternext = PLy_cursor_iternext,
45-
.tp_methods = PLy_cursor_methods,
36+
static PyType_Slot PLyCursor_slots[] =
37+
{
38+
{
39+
Py_tp_dealloc, PLy_cursor_dealloc
40+
},
41+
{
42+
Py_tp_doc, (char *) PLy_cursor_doc
43+
},
44+
{
45+
Py_tp_iter, PyObject_SelfIter
46+
},
47+
{
48+
Py_tp_iternext, PLy_cursor_iternext
49+
},
50+
{
51+
Py_tp_methods, PLy_cursor_methods
52+
},
53+
{
54+
0, NULL
55+
}
4656
};
4757

58+
static PyType_Spec PLyCursor_spec =
59+
{
60+
.name = "PLyCursor",
61+
.basicsize = sizeof(PLyCursorObject),
62+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
63+
.slots = PLyCursor_slots,
64+
};
65+
66+
static PyTypeObject *PLy_CursorType;
67+
4868
void
4969
PLy_cursor_init_type(void)
5070
{
51-
if (PyType_Ready(&PLy_CursorType) < 0)
71+
PLy_CursorType = (PyTypeObject *) PyType_FromSpec(&PLyCursor_spec);
72+
if (!PLy_CursorType)
5273
elog(ERROR, "could not initialize PLy_CursorType");
5374
}
5475

@@ -80,8 +101,12 @@ PLy_cursor_query(const char *query)
80101
volatile MemoryContext oldcontext;
81102
volatile ResourceOwner oldowner;
82103

83-
if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
104+
if ((cursor = PyObject_New(PLyCursorObject, PLy_CursorType)) == NULL)
84105
return NULL;
106+
#if PY_VERSION_HEX < 0x03080000
107+
/* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
108+
Py_INCREF(PLy_CursorType);
109+
#endif
85110
cursor->portalname = NULL;
86111
cursor->closed = false;
87112
cursor->mcxt = AllocSetContextCreate(TopMemoryContext,
@@ -177,8 +202,12 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
177202
return NULL;
178203
}
179204

180-
if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
205+
if ((cursor = PyObject_New(PLyCursorObject, PLy_CursorType)) == NULL)
181206
return NULL;
207+
#if PY_VERSION_HEX < 0x03080000
208+
/* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
209+
Py_INCREF(PLy_CursorType);
210+
#endif
182211
cursor->portalname = NULL;
183212
cursor->closed = false;
184213
cursor->mcxt = AllocSetContextCreate(TopMemoryContext,
@@ -272,30 +301,35 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
272301
}
273302

274303
static void
275-
PLy_cursor_dealloc(PyObject *arg)
304+
PLy_cursor_dealloc(PLyCursorObject *self)
276305
{
277-
PLyCursorObject *cursor;
306+
#if PY_VERSION_HEX >= 0x03080000
307+
PyTypeObject *tp = Py_TYPE(self);
308+
#endif
278309
Portal portal;
279310

280-
cursor = (PLyCursorObject *) arg;
281-
282-
if (!cursor->closed)
311+
if (!self->closed)
283312
{
284-
portal = GetPortalByName(cursor->portalname);
313+
portal = GetPortalByName(self->portalname);
285314

286315
if (PortalIsValid(portal))
287316
{
288317
UnpinPortal(portal);
289318
SPI_cursor_close(portal);
290319
}
291-
cursor->closed = true;
320+
self->closed = true;
292321
}
293-
if (cursor->mcxt)
322+
if (self->mcxt)
294323
{
295-
MemoryContextDelete(cursor->mcxt);
296-
cursor->mcxt = NULL;
324+
MemoryContextDelete(self->mcxt);
325+
self->mcxt = NULL;
297326
}
298-
arg->ob_type->tp_free(arg);
327+
328+
PyObject_Free(self);
329+
#if PY_VERSION_HEX >= 0x03080000
330+
/* This was not needed before Python 3.8 (Python issue 35810) */
331+
Py_DECREF(tp);
332+
#endif
299333
}
300334

301335
static PyObject *

src/pl/plpython/plpy_planobject.c

+49-21
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "plpython.h"
1313
#include "utils/memutils.h"
1414

15-
static void PLy_plan_dealloc(PyObject *arg);
15+
static void PLy_plan_dealloc(PLyPlanObject *self);
1616
static PyObject *PLy_plan_cursor(PyObject *self, PyObject *args);
1717
static PyObject *PLy_plan_execute(PyObject *self, PyObject *args);
1818
static PyObject *PLy_plan_status(PyObject *self, PyObject *args);
@@ -26,20 +26,37 @@ static PyMethodDef PLy_plan_methods[] = {
2626
{NULL, NULL, 0, NULL}
2727
};
2828

29-
static PyTypeObject PLy_PlanType = {
30-
PyVarObject_HEAD_INIT(NULL, 0)
31-
.tp_name = "PLyPlan",
32-
.tp_basicsize = sizeof(PLyPlanObject),
33-
.tp_dealloc = PLy_plan_dealloc,
34-
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
35-
.tp_doc = PLy_plan_doc,
36-
.tp_methods = PLy_plan_methods,
29+
static PyType_Slot PLyPlan_slots[] =
30+
{
31+
{
32+
Py_tp_dealloc, PLy_plan_dealloc
33+
},
34+
{
35+
Py_tp_doc, (char *) PLy_plan_doc
36+
},
37+
{
38+
Py_tp_methods, PLy_plan_methods
39+
},
40+
{
41+
0, NULL
42+
}
3743
};
3844

45+
static PyType_Spec PLyPlan_spec =
46+
{
47+
.name = "PLyPlan",
48+
.basicsize = sizeof(PLyPlanObject),
49+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
50+
.slots = PLyPlan_slots,
51+
};
52+
53+
static PyTypeObject *PLy_PlanType;
54+
3955
void
4056
PLy_plan_init_type(void)
4157
{
42-
if (PyType_Ready(&PLy_PlanType) < 0)
58+
PLy_PlanType = (PyTypeObject *) PyType_FromSpec(&PLyPlan_spec);
59+
if (!PLy_PlanType)
4360
elog(ERROR, "could not initialize PLy_PlanType");
4461
}
4562

@@ -48,8 +65,12 @@ PLy_plan_new(void)
4865
{
4966
PLyPlanObject *ob;
5067

51-
if ((ob = PyObject_New(PLyPlanObject, &PLy_PlanType)) == NULL)
68+
if ((ob = PyObject_New(PLyPlanObject, PLy_PlanType)) == NULL)
5269
return NULL;
70+
#if PY_VERSION_HEX < 0x03080000
71+
/* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
72+
Py_INCREF(PLy_PlanType);
73+
#endif
5374

5475
ob->plan = NULL;
5576
ob->nargs = 0;
@@ -63,25 +84,32 @@ PLy_plan_new(void)
6384
bool
6485
is_PLyPlanObject(PyObject *ob)
6586
{
66-
return ob->ob_type == &PLy_PlanType;
87+
return ob->ob_type == PLy_PlanType;
6788
}
6889

6990
static void
70-
PLy_plan_dealloc(PyObject *arg)
91+
PLy_plan_dealloc(PLyPlanObject *self)
7192
{
72-
PLyPlanObject *ob = (PLyPlanObject *) arg;
93+
#if PY_VERSION_HEX >= 0x03080000
94+
PyTypeObject *tp = Py_TYPE(self);
95+
#endif
7396

74-
if (ob->plan)
97+
if (self->plan)
7598
{
76-
SPI_freeplan(ob->plan);
77-
ob->plan = NULL;
99+
SPI_freeplan(self->plan);
100+
self->plan = NULL;
78101
}
79-
if (ob->mcxt)
102+
if (self->mcxt)
80103
{
81-
MemoryContextDelete(ob->mcxt);
82-
ob->mcxt = NULL;
104+
MemoryContextDelete(self->mcxt);
105+
self->mcxt = NULL;
83106
}
84-
arg->ob_type->tp_free(arg);
107+
108+
PyObject_Free(self);
109+
#if PY_VERSION_HEX >= 0x03080000
110+
/* This was not needed before Python 3.8 (Python issue 35810) */
111+
Py_DECREF(tp);
112+
#endif
85113
}
86114

87115

src/pl/plpython/plpy_procedure.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ PLy_procedure_compile(PLyProcedure *proc, const char *src)
350350
{
351351
PyObject *crv = NULL;
352352
char *msrc;
353+
PyObject *code0;
353354

354355
proc->globals = PyDict_Copy(PLy_interp_globals);
355356

@@ -368,7 +369,9 @@ PLy_procedure_compile(PLyProcedure *proc, const char *src)
368369
msrc = PLy_procedure_munge_source(proc->pyname, src);
369370
/* Save the mangled source for later inclusion in tracebacks */
370371
proc->src = MemoryContextStrdup(proc->mcxt, msrc);
371-
crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
372+
code0 = Py_CompileString(msrc, "<string>", Py_file_input);
373+
if (code0)
374+
crv = PyEval_EvalCode(code0, proc->globals, NULL);
372375
pfree(msrc);
373376

374377
if (crv != NULL)

0 commit comments

Comments
 (0)