File size: 22,004 Bytes
a4edccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in
index b9ffdcc65d1..2c3013e3d0c 100644
--- a/Modules/Setup.stdlib.in
+++ b/Modules/Setup.stdlib.in
@@ -175,7 +175,7 @@
 @MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
 @MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
 @MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c _testinternalcapi/complex.c
-@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/modsupport.c _testcapi/monitoring.c _testcapi/config.c _testcapi/import.c _testcapi/frame.c _testcapi/type.c _testcapi/function.c
+@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/modsupport.c _testcapi/monitoring.c _testcapi/config.c _testcapi/import.c _testcapi/frame.c _testcapi/type.c _testcapi/function.c _testcapi/module.c
 @MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c _testlimitedcapi/file.c
 @MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
 @MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
diff --git a/Modules/_testcapi/heaptype.c b/Modules/_testcapi/heaptype.c
index 69dcf072da1..4fdcc850a33 100644
--- a/Modules/_testcapi/heaptype.c
+++ b/Modules/_testcapi/heaptype.c
@@ -528,6 +528,21 @@ pytype_getmodulebydef(PyObject *self, PyObject *type)
     return Py_XNewRef(mod);
 }
 
+static PyObject *
+pytype_getmodulebytoken(PyObject *self, PyObject *args)
+{
+    PyObject *type;
+    PyObject *py_token;
+    if (!PyArg_ParseTuple(args, "OO", &type, &py_token)) {
+        return NULL;
+    }
+    void *token = PyLong_AsVoidPtr(py_token);
+    if ((!token) && PyErr_Occurred()) {
+        return NULL;
+    }
+    return PyType_GetModuleByToken((PyTypeObject *)type, token);
+}
+
 
 static PyMethodDef TestMethods[] = {
     {"pytype_fromspec_meta",    pytype_fromspec_meta,            METH_O},
@@ -546,6 +561,7 @@ static PyMethodDef TestMethods[] = {
     {"get_tp_token", get_tp_token, METH_O},
     {"pytype_getbasebytoken", pytype_getbasebytoken, METH_VARARGS},
     {"pytype_getmodulebydef", pytype_getmodulebydef, METH_O},
+    {"pytype_getmodulebytoken", pytype_getmodulebytoken, METH_VARARGS},
     {NULL},
 };
 
diff --git a/Modules/_testcapi/module.c b/Modules/_testcapi/module.c
new file mode 100644
index 00000000000..9349445351e
--- /dev/null
+++ b/Modules/_testcapi/module.c
@@ -0,0 +1,378 @@
+#include "parts.h"
+#include "util.h"
+
+// Test PyModule_* API
+
+/* unittest Cases that use these functions are in:
+ * Lib/test/test_capi/test_module.py
+ */
+
+static PyObject *
+module_from_slots_empty(PyObject *self, PyObject *spec)
+{
+    PyModuleDef_Slot slots[] = {
+        {0},
+    };
+    return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_slots_null(PyObject *self, PyObject *spec)
+{
+    return PyModule_FromSlotsAndSpec(NULL, spec);
+}
+
+static PyObject *
+module_from_slots_name(PyObject *self, PyObject *spec)
+{
+    PyModuleDef_Slot slots[] = {
+        {Py_mod_name, "currently ignored..."},
+        {0},
+    };
+    return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_slots_doc(PyObject *self, PyObject *spec)
+{
+    PyModuleDef_Slot slots[] = {
+        {Py_mod_doc, "the docstring"},
+        {0},
+    };
+    return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_slots_size(PyObject *self, PyObject *spec)
+{
+    PyModuleDef_Slot slots[] = {
+        {Py_mod_state_size, (void*)123},
+        {0},
+    };
+    PyObject *mod = PyModule_FromSlotsAndSpec(slots, spec);
+    if (!mod) {
+        return NULL;
+    }
+    return mod;
+}
+
+static PyObject *
+a_method(PyObject *self, PyObject *arg)
+{
+    return PyTuple_Pack(2, self, arg);
+}
+
+static PyMethodDef a_methoddef_array[] = {
+    {"a_method", a_method, METH_O},
+    {0},
+};
+
+static PyObject *
+module_from_slots_methods(PyObject *self, PyObject *spec)
+{
+    PyModuleDef_Slot slots[] = {
+        {Py_mod_methods, a_methoddef_array},
+        {0},
+    };
+    return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static int noop_traverse(PyObject *self, visitproc visit, void *arg) {
+    return 0;
+}
+static int noop_clear(PyObject *self) { return 0; }
+static void noop_free(void *self) { }
+
+static PyObject *
+module_from_slots_gc(PyObject *self, PyObject *spec)
+{
+    PyModuleDef_Slot slots[] = {
+        {Py_mod_state_traverse, noop_traverse},
+        {Py_mod_state_clear, noop_clear},
+        {Py_mod_state_free, noop_free},
+        {0},
+    };
+    PyObject *mod = PyModule_FromSlotsAndSpec(slots, spec);
+    if (!mod) {
+        return NULL;
+    }
+    if (PyModule_Add(mod, "traverse", PyLong_FromVoidPtr(&noop_traverse)) < 0) {
+        Py_DECREF(mod);
+        return NULL;
+    }
+    if (PyModule_Add(mod, "clear", PyLong_FromVoidPtr(&noop_clear)) < 0) {
+        Py_DECREF(mod);
+        return NULL;
+    }
+    if (PyModule_Add(mod, "free", PyLong_FromVoidPtr(&noop_free)) < 0) {
+        Py_DECREF(mod);
+        return NULL;
+    }
+    return mod;
+}
+
+static const char test_token;
+
+static PyObject *
+module_from_slots_token(PyObject *self, PyObject *spec)
+{
+    PyModuleDef_Slot slots[] = {
+        {Py_mod_token, (void*)&test_token},
+        {0},
+    };
+    PyObject *mod = PyModule_FromSlotsAndSpec(slots, spec);
+    if (!mod) {
+        return NULL;
+    }
+    void *got_token;
+    if (PyModule_GetToken(mod, &got_token) < 0) {
+        Py_DECREF(mod);
+        return NULL;
+    }
+    assert(got_token == &test_token);
+    return mod;
+}
+
+static int
+simple_exec(PyObject *module)
+{
+    return PyModule_AddIntConstant(module, "a_number", 456);
+}
+
+static PyObject *
+module_from_slots_exec(PyObject *self, PyObject *spec)
+{
+    PyModuleDef_Slot slots[] = {
+        {Py_mod_exec, simple_exec},
+        {0},
+    };
+    PyObject *mod = PyModule_FromSlotsAndSpec(slots, spec);
+    if (!mod) {
+        return NULL;
+    }
+    int res = PyObject_HasAttrStringWithError(mod, "a_number");
+    if (res < 0) {
+        Py_DECREF(mod);
+        return NULL;
+    }
+    assert(res == 0);
+    if (PyModule_Exec(mod) < 0) {
+        Py_DECREF(mod);
+        return NULL;
+    }
+    return mod;
+}
+
+static PyObject *
+create_attr_from_spec(PyObject *spec, PyObject *def)
+{
+    assert(!def);
+    return PyObject_GetAttrString(spec, "_gimme_this");
+}
+
+static PyObject *
+module_from_slots_create(PyObject *self, PyObject *spec)
+{
+    PyModuleDef_Slot slots[] = {
+        {Py_mod_create, create_attr_from_spec},
+        {0},
+    };
+    return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+
+static int
+slot_from_object(PyObject *obj)
+{
+    PyObject *slot_id_obj = PyObject_GetAttrString(obj, "_test_slot_id");
+    if (slot_id_obj == NULL) {
+        return -1;
+    }
+    int slot_id = PyLong_AsInt(slot_id_obj);
+    if (PyErr_Occurred()) {
+        return -1;
+    }
+    return slot_id;
+}
+
+static PyObject *
+module_from_slots_repeat_slot(PyObject *self, PyObject *spec)
+{
+    int slot_id = slot_from_object(spec);
+    if (slot_id < 0) {
+        return NULL;
+    }
+    PyModuleDef_Slot slots[] = {
+        {slot_id, "anything"},
+        {slot_id, "anything else"},
+        {0},
+    };
+    return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_slots_null_slot(PyObject *self, PyObject *spec)
+{
+    int slot_id = slot_from_object(spec);
+    if (slot_id < 0) {
+        return NULL;
+    }
+    PyModuleDef_Slot slots[] = {
+        {slot_id, NULL},
+        {0},
+    };
+    return PyModule_FromSlotsAndSpec(slots, spec);
+}
+
+static PyObject *
+module_from_def_slot(PyObject *self, PyObject *spec)
+{
+    int slot_id = slot_from_object(spec);
+    if (slot_id < 0) {
+        return NULL;
+    }
+    PyModuleDef_Slot slots[] = {
+        {slot_id, "anything"},
+        {0},
+    };
+    PyModuleDef def = {
+        PyModuleDef_HEAD_INIT,
+        .m_name = "currently ignored",
+        .m_slots = slots,
+    };
+    // PyModuleDef is normally static; the real requirement is that it
+    // must outlive its module.
+    // Here, module creation fails, so it's fine on the stack.
+    PyObject *result = PyModule_FromDefAndSpec(&def, spec);
+    assert(result == NULL);
+    return result;
+}
+
+static int
+another_exec(PyObject *module)
+{
+    /* Make sure simple_exec was called */
+    assert(PyObject_HasAttrString(module, "a_number"));
+
+    /* Add or negate a global called 'another_number'  */
+    PyObject *another_number;
+    if (PyObject_GetOptionalAttrString(module, "another_number",
+                                       &another_number) < 0) {
+        return -1;
+    }
+    if (!another_number) {
+        return PyModule_AddIntConstant(module, "another_number", 789);
+    }
+    PyObject *neg_number = PyNumber_Negative(another_number);
+    Py_DECREF(another_number);
+    if (!neg_number) {
+        return -1;
+    }
+    int result = PyObject_SetAttrString(module, "another_number",
+                                        neg_number);
+    Py_DECREF(neg_number);
+    return result;
+}
+
+static PyObject *
+module_from_def_multiple_exec(PyObject *self, PyObject *spec)
+{
+    static PyModuleDef_Slot slots[] = {
+        {Py_mod_exec, simple_exec},
+        {Py_mod_exec, another_exec},
+        {Py_mod_gil, Py_MOD_GIL_NOT_USED},
+        {0},
+    };
+    static PyModuleDef def = {
+        PyModuleDef_HEAD_INIT,
+        .m_name = "currently ignored",
+        .m_slots = slots,
+    };
+    return PyModule_FromDefAndSpec(&def, spec);
+}
+
+static PyObject *
+pymodule_exec(PyObject *self, PyObject *module)
+{
+    if (PyModule_Exec(module) < 0) {
+        return NULL;
+    }
+    Py_RETURN_NONE;
+}
+
+static PyObject *
+pymodule_get_token(PyObject *self, PyObject *module)
+{
+    void *token;
+    if (PyModule_GetToken(module, &token) < 0) {
+        return NULL;
+    }
+    return PyLong_FromVoidPtr(token);
+}
+
+static PyObject *
+pymodule_get_def(PyObject *self, PyObject *module)
+{
+    PyModuleDef *def = PyModule_GetDef(module);
+    if (!def && PyErr_Occurred()) {
+        return NULL;
+    }
+    return PyLong_FromVoidPtr(def);
+}
+
+static PyObject *
+pymodule_get_state_size(PyObject *self, PyObject *module)
+{
+    Py_ssize_t size;
+    if (PyModule_GetStateSize(module, &size) < 0) {
+        return NULL;
+    }
+    return PyLong_FromSsize_t(size);
+}
+
+static PyMethodDef test_methods[] = {
+    {"module_from_slots_empty", module_from_slots_empty, METH_O},
+    {"module_from_slots_null", module_from_slots_null, METH_O},
+    {"module_from_slots_name", module_from_slots_name, METH_O},
+    {"module_from_slots_doc", module_from_slots_doc, METH_O},
+    {"module_from_slots_size", module_from_slots_size, METH_O},
+    {"module_from_slots_methods", module_from_slots_methods, METH_O},
+    {"module_from_slots_gc", module_from_slots_gc, METH_O},
+    {"module_from_slots_token", module_from_slots_token, METH_O},
+    {"module_from_slots_exec", module_from_slots_exec, METH_O},
+    {"module_from_slots_create", module_from_slots_create, METH_O},
+    {"module_from_slots_repeat_slot", module_from_slots_repeat_slot, METH_O},
+    {"module_from_slots_null_slot", module_from_slots_null_slot, METH_O},
+    {"module_from_def_multiple_exec", module_from_def_multiple_exec, METH_O},
+    {"module_from_def_slot", module_from_def_slot, METH_O},
+    {"pymodule_get_token", pymodule_get_token, METH_O},
+    {"pymodule_get_def", pymodule_get_def, METH_O},
+    {"pymodule_get_state_size", pymodule_get_state_size, METH_O},
+    {"pymodule_exec", pymodule_exec, METH_O},
+    {NULL},
+};
+
+int
+_PyTestCapi_Init_Module(PyObject *m)
+{
+#define ADD_INT_MACRO(C) if (PyModule_AddIntConstant(m, #C, C) < 0) return -1;
+    ADD_INT_MACRO(Py_mod_create);
+    ADD_INT_MACRO(Py_mod_exec);
+    ADD_INT_MACRO(Py_mod_multiple_interpreters);
+    ADD_INT_MACRO(Py_mod_gil);
+    ADD_INT_MACRO(Py_mod_name);
+    ADD_INT_MACRO(Py_mod_doc);
+    ADD_INT_MACRO(Py_mod_state_size);
+    ADD_INT_MACRO(Py_mod_methods);
+    ADD_INT_MACRO(Py_mod_state_traverse);
+    ADD_INT_MACRO(Py_mod_state_clear);
+    ADD_INT_MACRO(Py_mod_state_free);
+    ADD_INT_MACRO(Py_mod_token);
+#undef ADD_INT_MACRO
+    if (PyModule_Add(m, "module_test_token",
+                     PyLong_FromVoidPtr((void*)&test_token)) < 0)
+    {
+        return -1;
+    }
+    return PyModule_AddFunctions(m, test_methods);
+}
diff --git a/Modules/_testcapi/parts.h b/Modules/_testcapi/parts.h
index 32915d04bd3..a7feca5bd96 100644
--- a/Modules/_testcapi/parts.h
+++ b/Modules/_testcapi/parts.h
@@ -66,5 +66,6 @@ int _PyTestCapi_Init_Import(PyObject *mod);
 int _PyTestCapi_Init_Frame(PyObject *mod);
 int _PyTestCapi_Init_Type(PyObject *mod);
 int _PyTestCapi_Init_Function(PyObject *mod);
+int _PyTestCapi_Init_Module(PyObject *mod);
 
 #endif // Py_TESTCAPI_PARTS_H
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index e29b9ae354b..22cd731d410 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -3512,6 +3512,9 @@ _testcapi_exec(PyObject *m)
     if (_PyTestCapi_Init_Function(m) < 0) {
         return -1;
     }
+    if (_PyTestCapi_Init_Module(m) < 0) {
+        return -1;
+    }
 
     return 0;
 }
diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c
index c2647d405e2..dede05960d7 100644
--- a/Modules/_testinternalcapi.c
+++ b/Modules/_testinternalcapi.c
@@ -2418,6 +2418,34 @@ set_vectorcall_nop(PyObject *self, PyObject *func)
     Py_RETURN_NONE;
 }
 
+static PyObject *
+module_get_gc_hooks(PyObject *self, PyObject *arg)
+{
+    PyModuleObject *mod = (PyModuleObject *)arg;
+    PyObject *traverse = NULL;
+    PyObject *clear = NULL;
+    PyObject *free = NULL;
+    PyObject *result = NULL;
+    traverse = PyLong_FromVoidPtr(mod->md_state_traverse);
+    if (!traverse) {
+        goto finally;
+    }
+    clear = PyLong_FromVoidPtr(mod->md_state_clear);
+    if (!clear) {
+        goto finally;
+    }
+    free = PyLong_FromVoidPtr(mod->md_state_free);
+    if (!free) {
+        goto finally;
+    }
+    result = PyTuple_FromArray((PyObject*[]){ traverse, clear, free }, 3);
+finally:
+    Py_XDECREF(traverse);
+    Py_XDECREF(clear);
+    Py_XDECREF(free);
+    return result;
+}
+
 static PyMethodDef module_functions[] = {
     {"get_configs", get_configs, METH_NOARGS},
     {"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@@ -2527,6 +2555,7 @@ static PyMethodDef module_functions[] = {
 #endif
     {"simple_pending_call", simple_pending_call, METH_O},
     {"set_vectorcall_nop", set_vectorcall_nop, METH_O},
+    {"module_get_gc_hooks", module_get_gc_hooks, METH_O},
     {NULL, NULL} /* sentinel */
 };
 
diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c
index bfec0678e2c..220fa888e49 100644
--- a/Modules/_testmultiphase.c
+++ b/Modules/_testmultiphase.c
@@ -850,6 +850,28 @@ PyInit__testmultiphase_exec_unreported_exception(void)
     return PyModuleDef_Init(&def_exec_unreported_exception);
 }
 
+static int execfn_a1(PyObject*m) { return PyModule_AddIntConstant(m, "a", 1); }
+static int execfn_b2(PyObject*m) { return PyModule_AddIntConstant(m, "b", 2); }
+static int execfn_c3(PyObject*m) { return PyModule_AddIntConstant(m, "c", 3); }
+
+PyMODINIT_FUNC
+PyInit__testmultiphase_exec_multiple(void)
+{
+    static PyModuleDef_Slot slots[] = {
+        {Py_mod_exec, execfn_a1},
+        {Py_mod_exec, execfn_b2},
+        {Py_mod_exec, execfn_c3},
+        {Py_mod_gil, Py_MOD_GIL_NOT_USED},
+        {0}
+    };
+    static PyModuleDef def = {
+        PyModuleDef_HEAD_INIT,
+        .m_name="_testmultiphase_exec_multiple",
+        .m_slots=slots,
+    };
+    return PyModuleDef_Init(&def);
+}
+
 static int
 meth_state_access_exec(PyObject *m)
 {
@@ -993,3 +1015,156 @@ PyInit__test_no_multiple_interpreter_slot(void)
 {
     return PyModuleDef_Init(&no_multiple_interpreter_slot_def);
 }
+
+
+/* PyModExport_* hooks */
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport(void)
+{
+    static PyModuleDef_Slot slots[] = {
+        {Py_mod_name, "_test_from_modexport"},
+        {0},
+    };
+    return slots;
+}
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_null(void)
+{
+    return NULL;
+}
+
+PyMODINIT_FUNC
+PyModInit__test_from_modexport_null(void)
+{
+    // This is not called as fallback for failed PyModExport_*
+    assert(0);
+    PyErr_SetString(PyExc_AssertionError, "PyInit_ fallback called");
+    return NULL;
+}
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_exception(void)
+{
+    PyErr_SetString(PyExc_ValueError, "failed as requested");
+    return NULL;
+}
+
+PyMODINIT_FUNC
+PyModInit__test_from_modexport_exception(void)
+{
+    // This is not called as fallback for failed PyModExport_*
+    assert(0);
+    PyErr_SetString(PyExc_AssertionError, "PyInit_ fallback called");
+    return NULL;
+}
+
+static PyObject *
+modexport_create_string(PyObject *spec, PyObject *def)
+{
+    assert(def == NULL);
+    return PyUnicode_FromString("is this \xf0\x9f\xa6\x8b... a module?");
+}
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_create_nonmodule(void)
+{
+    static PyModuleDef_Slot slots[] = {
+        {Py_mod_name, "_test_from_modexport_create_nonmodule"},
+        {Py_mod_create, modexport_create_string},
+        {0},
+    };
+    return slots;
+}
+
+static PyModuleDef_Slot modexport_empty_slots[] = {
+    {0},
+};
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_empty_slots(void)
+{
+    return modexport_empty_slots;
+}
+
+static int
+modexport_smoke_exec(PyObject *mod)
+{
+    // "magic" values 147 & 258 are expected in the test
+    if (PyModule_AddIntConstant(mod, "number", 147) < 0) {
+        return 0;
+    }
+    int *state = PyModule_GetState(mod);
+    if (!state) {
+        return -1;
+    }
+    *state = 258;
+
+    PyObject *tp = PyType_FromModuleAndSpec(mod, &StateAccessType_spec, NULL);
+    if (PyModule_Add(mod, "Example", tp) < 0) {
+        return -1;
+    }
+
+    return 0;
+}
+
+static PyObject *
+modexport_smoke_get_state_int(PyObject *mod, PyObject *arg)
+{
+    int *state = PyModule_GetState(mod);
+    if (!state) {
+        return NULL;
+    }
+    return PyLong_FromLong(*state);
+}
+
+static const char modexport_smoke_test_token;
+
+static PyObject *
+modexport_smoke_get_test_token(PyObject *mod, PyObject *arg)
+{
+    return PyLong_FromVoidPtr((void*)&modexport_smoke_test_token);
+}
+
+static PyObject *
+modexport_get_empty_slots(PyObject *mod, PyObject *arg)
+{
+    /* Get the address of modexport_empty_slots.
+     * This method would be in the `_test_from_modexport_empty_slots` module,
+     * if it had a methods slot.
+     */
+    return PyLong_FromVoidPtr(&modexport_empty_slots);
+}
+
+static void
+modexport_smoke_free(PyObject *mod)
+{
+    int *state = PyModule_GetState(mod);
+    if (!state) {
+        PyErr_FormatUnraisable("Exception ignored in module %R free", mod);
+    }
+    assert(*state == 258);
+}
+
+PyMODEXPORT_FUNC
+PyModExport__test_from_modexport_smoke(void)
+{
+    static PyMethodDef methods[] = {
+        {"get_state_int", modexport_smoke_get_state_int, METH_NOARGS},
+        {"get_test_token", modexport_smoke_get_test_token, METH_NOARGS},
+        {"get_modexport_empty_slots", modexport_get_empty_slots, METH_NOARGS},
+        {0},
+    };
+    static PyModuleDef_Slot slots[] = {
+        {Py_mod_name, "_test_from_modexport_smoke"},
+        {Py_mod_doc, "the expected docstring"},
+        {Py_mod_exec, modexport_smoke_exec},
+        {Py_mod_state_size, (void*)sizeof(int)},
+        {Py_mod_methods, methods},
+        {Py_mod_state_free, modexport_smoke_free},
+        {Py_mod_token, (void*)&modexport_smoke_test_token},
+        {0},
+    };
+    return slots;
+}
diff --git a/Modules/_testsinglephase.c b/Modules/_testsinglephase.c
index 2c59085d15b..ee38d61b43a 100644
--- a/Modules/_testsinglephase.c
+++ b/Modules/_testsinglephase.c
@@ -244,6 +244,8 @@ static inline module_state *
 get_module_state(PyObject *module)
 {
     PyModuleDef *def = PyModule_GetDef(module);
+    assert(def);
+
     if (def->m_size == -1) {
         return &global_state.module;
     }