1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
/* Testing module for single-phase initialization of extension modules
*/
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
//#include <time.h>
#include "Python.h"
#include "pycore_namespace.h" // _PyNamespace_New()
typedef struct {
_PyTime_t initialized;
PyObject *error;
PyObject *int_const;
PyObject *str_const;
} module_state;
/* Process-global state is only used by _testsinglephase
since it's the only one that does not support re-init. */
static struct {
int initialized_count;
module_state module;
} global_state = {
#define NOT_INITIALIZED -1
.initialized_count = NOT_INITIALIZED,
};
static void clear_state(module_state *state);
static void
clear_global_state(void)
{
clear_state(&global_state.module);
global_state.initialized_count = NOT_INITIALIZED;
}
static inline module_state *
get_module_state(PyObject *module)
{
PyModuleDef *def = PyModule_GetDef(module);
if (def->m_size == -1) {
return &global_state.module;
}
else if (def->m_size == 0) {
return NULL;
}
else {
module_state *state = (module_state*)PyModule_GetState(module);
assert(state != NULL);
return state;
}
}
static void
clear_state(module_state *state)
{
state->initialized = 0;
Py_CLEAR(state->error);
Py_CLEAR(state->int_const);
Py_CLEAR(state->str_const);
}
static int
_set_initialized(_PyTime_t *initialized)
{
/* We go strictly monotonic to ensure each time is unique. */
_PyTime_t prev;
if (_PyTime_GetMonotonicClockWithInfo(&prev, NULL) != 0) {
return -1;
}
/* We do a busy sleep since the interval should be super short. */
_PyTime_t t;
do {
if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) != 0) {
return -1;
}
} while (t == prev);
*initialized = t;
return 0;
}
static int
init_state(module_state *state)
{
assert(state->initialized == 0 &&
state->error == NULL &&
state->int_const == NULL &&
state->str_const == NULL);
if (_set_initialized(&state->initialized) != 0) {
goto error;
}
assert(state->initialized > 0);
/* Add an exception type */
state->error = PyErr_NewException("_testsinglephase.error", NULL, NULL);
if (state->error == NULL) {
goto error;
}
state->int_const = PyLong_FromLong(1969);
if (state->int_const == NULL) {
goto error;
}
state->str_const = PyUnicode_FromString("something different");
if (state->str_const == NULL) {
goto error;
}
return 0;
error:
clear_state(state);
return -1;
}
static int
init_module(PyObject *module, module_state *state)
{
if (PyModule_AddObjectRef(module, "error", state->error) != 0) {
return -1;
}
if (PyModule_AddObjectRef(module, "int_const", state->int_const) != 0) {
return -1;
}
if (PyModule_AddObjectRef(module, "str_const", state->str_const) != 0) {
return -1;
}
double d = _PyTime_AsSecondsDouble(state->initialized);
PyObject *initialized = PyFloat_FromDouble(d);
if (initialized == NULL) {
return -1;
}
if (PyModule_AddObjectRef(module, "_module_initialized", initialized) != 0) {
return -1;
}
return 0;
}
PyDoc_STRVAR(common_state_initialized_doc,
"state_initialized()\n\
\n\
Return the seconds-since-epoch when the module state was initialized.");
static PyObject *
common_state_initialized(PyObject *self, PyObject *Py_UNUSED(ignored))
{
module_state *state = get_module_state(self);
if (state == NULL) {
Py_RETURN_NONE;
}
double d = _PyTime_AsSecondsDouble(state->initialized);
return PyFloat_FromDouble(d);
}
#define STATE_INITIALIZED_METHODDEF \
{"state_initialized", common_state_initialized, METH_NOARGS, \
common_state_initialized_doc}
PyDoc_STRVAR(common_look_up_self_doc,
"look_up_self()\n\
\n\
Return the module associated with this module's def.m_base.m_index.");
static PyObject *
common_look_up_self(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyModuleDef *def = PyModule_GetDef(self);
if (def == NULL) {
return NULL;
}
return Py_NewRef(
PyState_FindModule(def));
}
#define LOOK_UP_SELF_METHODDEF \
{"look_up_self", common_look_up_self, METH_NOARGS, common_look_up_self_doc}
/* Function of two integers returning integer */
PyDoc_STRVAR(common_sum_doc,
"sum(i,j)\n\
\n\
Return the sum of i and j.");
static PyObject *
common_sum(PyObject *self, PyObject *args)
{
long i, j;
long res;
if (!PyArg_ParseTuple(args, "ll:sum", &i, &j))
return NULL;
res = i + j;
return PyLong_FromLong(res);
}
#define SUM_METHODDEF \
{"sum", common_sum, METH_VARARGS, common_sum_doc}
PyDoc_STRVAR(basic_initialized_count_doc,
"initialized_count()\n\
\n\
Return how many times the module has been initialized.");
static PyObject *
basic_initialized_count(PyObject *self, PyObject *Py_UNUSED(ignored))
{
assert(PyModule_GetDef(self)->m_size == -1);
return PyLong_FromLong(global_state.initialized_count);
}
#define INITIALIZED_COUNT_METHODDEF \
{"initialized_count", basic_initialized_count, METH_NOARGS, \
basic_initialized_count_doc}
PyDoc_STRVAR(basic__clear_globals_doc,
"_clear_globals()\n\
\n\
Free all global state and set it to uninitialized.");
static PyObject *
basic__clear_globals(PyObject *self, PyObject *Py_UNUSED(ignored))
{
assert(PyModule_GetDef(self)->m_size == -1);
clear_global_state();
Py_RETURN_NONE;
}
#define _CLEAR_GLOBALS_METHODDEF \
{"_clear_globals", basic__clear_globals, METH_NOARGS, \
basic__clear_globals_doc}
/*********************************************/
/* the _testsinglephase module (and aliases) */
/*********************************************/
/* This ia more typical of legacy extensions in the wild:
- single-phase init
- no module state
- does not support repeated initialization
(so m_copy is used)
- the module def is cached in _PyRuntime.extensions
(by name/filename)
Also note that, because the module has single-phase init,
it is cached in interp->module_by_index (using mod->md_def->m_base.m_index).
*/
static PyMethodDef TestMethods_Basic[] = {
LOOK_UP_SELF_METHODDEF,
SUM_METHODDEF,
STATE_INITIALIZED_METHODDEF,
INITIALIZED_COUNT_METHODDEF,
_CLEAR_GLOBALS_METHODDEF,
{NULL, NULL} /* sentinel */
};
static struct PyModuleDef _testsinglephase_basic = {
PyModuleDef_HEAD_INIT,
.m_name = "_testsinglephase",
.m_doc = PyDoc_STR("Test module _testsinglephase"),
.m_size = -1, // no module state
.m_methods = TestMethods_Basic,
};
static PyObject *
init__testsinglephase_basic(PyModuleDef *def)
{
if (global_state.initialized_count == -1) {
global_state.initialized_count = 0;
}
PyObject *module = PyModule_Create(def);
if (module == NULL) {
return NULL;
}
module_state *state = &global_state.module;
// It may have been set by a previous run or under a different name.
clear_state(state);
if (init_state(state) < 0) {
Py_CLEAR(module);
return NULL;
}
if (init_module(module, state) < 0) {
Py_CLEAR(module);
goto finally;
}
global_state.initialized_count++;
finally:
return module;
}
PyMODINIT_FUNC
PyInit__testsinglephase(void)
{
return init__testsinglephase_basic(&_testsinglephase_basic);
}
PyMODINIT_FUNC
PyInit__testsinglephase_basic_wrapper(void)
{
return PyInit__testsinglephase();
}
PyMODINIT_FUNC
PyInit__testsinglephase_basic_copy(void)
{
static struct PyModuleDef def = {
PyModuleDef_HEAD_INIT,
.m_name = "_testsinglephase_basic_copy",
.m_doc = PyDoc_STR("Test module _testsinglephase_basic_copy"),
.m_size = -1, // no module state
.m_methods = TestMethods_Basic,
};
return init__testsinglephase_basic(&def);
}
/*******************************************/
/* the _testsinglephase_with_reinit module */
/*******************************************/
/* This ia less typical of legacy extensions in the wild:
- single-phase init (same as _testsinglephase above)
- no module state
- supports repeated initialization
(so m_copy is not used)
- the module def is not cached in _PyRuntime.extensions
At this point most modules would reach for multi-phase init (PEP 489).
However, module state has been around a while (PEP 3121),
and most extensions predate multi-phase init.
(This module is basically the same as _testsinglephase,
but supports repeated initialization.)
*/
static PyMethodDef TestMethods_Reinit[] = {
LOOK_UP_SELF_METHODDEF,
SUM_METHODDEF,
STATE_INITIALIZED_METHODDEF,
{NULL, NULL} /* sentinel */
};
static struct PyModuleDef _testsinglephase_with_reinit = {
PyModuleDef_HEAD_INIT,
.m_name = "_testsinglephase_with_reinit",
.m_doc = PyDoc_STR("Test module _testsinglephase_with_reinit"),
.m_size = 0,
.m_methods = TestMethods_Reinit,
};
PyMODINIT_FUNC
PyInit__testsinglephase_with_reinit(void)
{
/* We purposefully do not try PyState_FindModule() first here
since we want to check the behavior of re-loading the module. */
PyObject *module = PyModule_Create(&_testsinglephase_with_reinit);
if (module == NULL) {
return NULL;
}
assert(get_module_state(module) == NULL);
module_state state = {0};
if (init_state(&state) < 0) {
Py_CLEAR(module);
return NULL;
}
if (init_module(module, &state) < 0) {
Py_CLEAR(module);
goto finally;
}
finally:
/* We only needed the module state for setting the module attrs. */
clear_state(&state);
return module;
}
/******************************************/
/* the _testsinglephase_with_state module */
/******************************************/
/* This ia less typical of legacy extensions in the wild:
- single-phase init (same as _testsinglephase above)
- has some module state
- supports repeated initialization
(so m_copy is not used)
- the module def is not cached in _PyRuntime.extensions
At this point most modules would reach for multi-phase init (PEP 489).
However, module state has been around a while (PEP 3121),
and most extensions predate multi-phase init.
*/
static PyMethodDef TestMethods_WithState[] = {
LOOK_UP_SELF_METHODDEF,
SUM_METHODDEF,
STATE_INITIALIZED_METHODDEF,
{NULL, NULL} /* sentinel */
};
static struct PyModuleDef _testsinglephase_with_state = {
PyModuleDef_HEAD_INIT,
.m_name = "_testsinglephase_with_state",
.m_doc = PyDoc_STR("Test module _testsinglephase_with_state"),
.m_size = sizeof(module_state),
.m_methods = TestMethods_WithState,
};
PyMODINIT_FUNC
PyInit__testsinglephase_with_state(void)
{
/* We purposefully do not try PyState_FindModule() first here
since we want to check the behavior of re-loading the module. */
PyObject *module = PyModule_Create(&_testsinglephase_with_state);
if (module == NULL) {
return NULL;
}
module_state *state = get_module_state(module);
assert(state != NULL);
if (init_state(state) < 0) {
Py_CLEAR(module);
return NULL;
}
if (init_module(module, state) < 0) {
clear_state(state);
Py_CLEAR(module);
goto finally;
}
finally:
return module;
}
|