Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c47e8df

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(). Reviewed-by: Jakob Egger <jakob@eggerapps.at> Discussion: https://www.postgresql.org/message-id/flat/ee410de1-1e0b-4770-b125-eeefd4726a24@eisentraut.org
1 parent 0e42d31 commit c47e8df

File tree

6 files changed

+177
-105
lines changed

6 files changed

+177
-105
lines changed

src/pl/plpython/plpy_cursorobject.c

+46-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,7 +101,7 @@ 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;
85106
cursor->portalname = NULL;
86107
cursor->closed = false;
@@ -177,7 +198,7 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
177198
return NULL;
178199
}
179200

180-
if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
201+
if ((cursor = PyObject_New(PLyCursorObject, PLy_CursorType)) == NULL)
181202
return NULL;
182203
cursor->portalname = NULL;
183204
cursor->closed = false;
@@ -272,30 +293,30 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
272293
}
273294

274295
static void
275-
PLy_cursor_dealloc(PyObject *arg)
296+
PLy_cursor_dealloc(PLyCursorObject *self)
276297
{
277-
PLyCursorObject *cursor;
298+
PyTypeObject *tp = Py_TYPE(self);
278299
Portal portal;
279300

280-
cursor = (PLyCursorObject *) arg;
281-
282-
if (!cursor->closed)
301+
if (!self->closed)
283302
{
284-
portal = GetPortalByName(cursor->portalname);
303+
portal = GetPortalByName(self->portalname);
285304

286305
if (PortalIsValid(portal))
287306
{
288307
UnpinPortal(portal);
289308
SPI_cursor_close(portal);
290309
}
291-
cursor->closed = true;
310+
self->closed = true;
292311
}
293-
if (cursor->mcxt)
312+
if (self->mcxt)
294313
{
295-
MemoryContextDelete(cursor->mcxt);
296-
cursor->mcxt = NULL;
314+
MemoryContextDelete(self->mcxt);
315+
self->mcxt = NULL;
297316
}
298-
arg->ob_type->tp_free(arg);
317+
318+
PyObject_Free(self);
319+
Py_DECREF(tp);
299320
}
300321

301322
static PyObject *

src/pl/plpython/plpy_planobject.c

+40-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,7 +65,7 @@ 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;
5370

5471
ob->plan = NULL;
@@ -63,25 +80,27 @@ PLy_plan_new(void)
6380
bool
6481
is_PLyPlanObject(PyObject *ob)
6582
{
66-
return ob->ob_type == &PLy_PlanType;
83+
return ob->ob_type == PLy_PlanType;
6784
}
6885

6986
static void
70-
PLy_plan_dealloc(PyObject *arg)
87+
PLy_plan_dealloc(PLyPlanObject *self)
7188
{
72-
PLyPlanObject *ob = (PLyPlanObject *) arg;
89+
PyTypeObject *tp = Py_TYPE(self);
7390

74-
if (ob->plan)
91+
if (self->plan)
7592
{
76-
SPI_freeplan(ob->plan);
77-
ob->plan = NULL;
93+
SPI_freeplan(self->plan);
94+
self->plan = NULL;
7895
}
79-
if (ob->mcxt)
96+
if (self->mcxt)
8097
{
81-
MemoryContextDelete(ob->mcxt);
82-
ob->mcxt = NULL;
98+
MemoryContextDelete(self->mcxt);
99+
self->mcxt = NULL;
83100
}
84-
arg->ob_type->tp_free(arg);
101+
102+
PyObject_Free(self);
103+
Py_DECREF(tp);
85104
}
86105

87106

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)