Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc47e8df

Browse files
committed
Prepare for Python "Limited API" in PL/Python
Using the Python Limited API would allow building PL/Python againstany Python 3.x version and using another Python 3.x version at runtime. This commit does not activate that, but it prepares the code toonly 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 parent0e42d31 commitc47e8df

File tree

6 files changed

+177
-105
lines changed

6 files changed

+177
-105
lines changed

‎src/pl/plpython/plpy_cursorobject.c

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include"utils/memutils.h"
2121

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

36-
staticPyTypeObjectPLy_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+
staticPyType_SlotPLyCursor_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+
staticPyType_SpecPLyCursor_spec=
59+
{
60+
.name="PLyCursor",
61+
.basicsize=sizeof(PLyCursorObject),
62+
.flags=Py_TPFLAGS_DEFAULT |Py_TPFLAGS_BASETYPE,
63+
.slots=PLyCursor_slots,
64+
};
65+
66+
staticPyTypeObject*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
volatileMemoryContextoldcontext;
81102
volatileResourceOwneroldowner;
82103

83-
if ((cursor=PyObject_New(PLyCursorObject,&PLy_CursorType))==NULL)
104+
if ((cursor=PyObject_New(PLyCursorObject,PLy_CursorType))==NULL)
84105
returnNULL;
85106
cursor->portalname=NULL;
86107
cursor->closed= false;
@@ -177,7 +198,7 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
177198
returnNULL;
178199
}
179200

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

274295
staticvoid
275-
PLy_cursor_dealloc(PyObject*arg)
296+
PLy_cursor_dealloc(PLyCursorObject*self)
276297
{
277-
PLyCursorObject*cursor;
298+
PyTypeObject*tp=Py_TYPE(self);
278299
Portalportal;
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
staticPyObject*

‎src/pl/plpython/plpy_planobject.c

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include"plpython.h"
1313
#include"utils/memutils.h"
1414

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

29-
staticPyTypeObjectPLy_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+
staticPyType_SlotPLyPlan_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+
staticPyType_SpecPLyPlan_spec=
46+
{
47+
.name="PLyPlan",
48+
.basicsize=sizeof(PLyPlanObject),
49+
.flags=Py_TPFLAGS_DEFAULT |Py_TPFLAGS_BASETYPE,
50+
.slots=PLyPlan_slots,
51+
};
52+
53+
staticPyTypeObject*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
returnNULL;
5370

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

6986
staticvoid
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

Lines changed: 4 additions & 1 deletion
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)

[8]ページ先頭

©2009-2025 Movatter.jp