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
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
diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py
--- a/Lib/test/test_capi/test_misc.py
+++ b/Lib/test/test_capi/test_misc.py
@@ -923,8 +923,8 @@ def test_tp_bases_slot(self):
 
     def test_tp_bases_slot_none(self):
         self.assertRaisesRegex(
-            SystemError,
-            "Py_tp_bases is not a tuple",
+            TypeError,
+            "",
             _testcapi.create_heapctype_with_none_bases_slot
         )
 
@@ -1055,13 +1055,13 @@ def test_heaptype_relative_members(self):
     def test_heaptype_relative_members_errors(self):
         with self.assertRaisesRegex(
                 SystemError,
-                r"With Py_RELATIVE_OFFSET, basicsize must be negative"):
+                r"basicsize must be extended"):
             _testlimitedcapi.make_heaptype_with_member(0, 1234, 0, True)
         with self.assertRaisesRegex(
-                SystemError, r"Member offset out of range \(0\.\.-basicsize\)"):
+                SystemError, r"extra_basicsize"):
             _testlimitedcapi.make_heaptype_with_member(0, -8, 1234, True)
         with self.assertRaisesRegex(
-                SystemError, r"Member offset out of range \(0\.\.-basicsize\)"):
+                SystemError, r"must not be negative"):
             _testlimitedcapi.make_heaptype_with_member(0, -8, -1, True)
 
         Sub = _testlimitedcapi.make_heaptype_with_member(0, -8, 0, True)
@@ -1078,7 +1078,7 @@ def test_heaptype_relative_special_members_errors(self):
             with self.subTest(member_name=member_name):
                 with self.assertRaisesRegex(
                         SystemError,
-                        r"With Py_RELATIVE_OFFSET, basicsize must be negative."):
+                        r"basicsize must be extended"):
                     _testlimitedcapi.make_heaptype_with_member(
                         basicsize=sys.getsizeof(object()) + 100,
                         add_relative_flag=True,
@@ -1089,7 +1089,7 @@ def test_heaptype_relative_special_members_errors(self):
                         )
                 with self.assertRaisesRegex(
                         SystemError,
-                        r"Member offset out of range \(0\.\.-basicsize\)"):
+                        r"must not be negative"):
                     _testlimitedcapi.make_heaptype_with_member(
                         basicsize=-8,
                         add_relative_flag=True,
@@ -1098,6 +1098,17 @@ def test_heaptype_relative_special_members_errors(self):
                         member_type=_testlimitedcapi.Py_T_PYSSIZET,
                         member_flags=_testlimitedcapi.Py_READONLY,
                         )
+                with self.assertRaisesRegex(
+                        SystemError,
+                        r"extra_basicsize"):
+                    _testlimitedcapi.make_heaptype_with_member(
+                        basicsize=-8,
+                        add_relative_flag=True,
+                        member_name=member_name,
+                        member_offset=1234,
+                        member_type=_testlimitedcapi.Py_T_PYSSIZET,
+                        member_flags=_testlimitedcapi.Py_READONLY,
+                        )
                 with self.assertRaisesRegex(
                         SystemError,
                         r"type of %s must be Py_T_PYSSIZET" % member_name):
diff --git a/Lib/test/test_capi/test_module.py b/Lib/test/test_capi/test_module.py
--- a/Lib/test/test_capi/test_module.py
+++ b/Lib/test/test_capi/test_module.py
@@ -150,11 +150,13 @@ def test_repeated_def_slot(self):
                 with self.assertRaises(SystemError) as cm:
                     _testcapi.module_from_slots_repeat_slot(spec)
                 self.assertIn(name, str(cm.exception))
-                self.assertIn("more than one", str(cm.exception))
+                self.assertRegex(
+                    str(cm.exception),
+                    rf"multiple.*{name}|{name}.*multiple")
 
     def test_null_def_slot(self):
         """Slots that replace PyModuleDef fields can't be NULL"""
-        for name in (*DEF_SLOTS, 'Py_mod_exec'):
+        for name in {*DEF_SLOTS, 'Py_mod_exec'} - {'Py_mod_state_size'}:
             with self.subTest(name):
                 spec = FakeSpec()
                 spec._test_slot_id = getattr(_testcapi, name)
diff --git a/Lib/test/test_capi/test_slots.py b/Lib/test/test_capi/test_slots.py
new file mode 100644
index 0000000..4e5479c
--- /dev/null
+++ b/Lib/test/test_capi/test_slots.py
@@ -0,0 +1,313 @@
+from test.support import import_helper, subTests
+import contextlib
+import unittest
+import types
+import sys
+
+_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
+_testcapi = import_helper.import_module('_testcapi')
+
+class FakeSpec:
+    name = 'module'
+
+
+# See Modules/_testlimitedcapi/slots.c for the definitions.
+# This module is full of "magic constants" which simply need to match
+# between the C and Python part of the tests.
+
+class TypeSlotsTests(unittest.TestCase):
+    def test_basic_type_slots(self):
+        cls = _testlimitedcapi.type_from_slots("basic")
+        self.assertEqual(cls.__name__, "MyType")
+
+        # Py_TPFLAGS_IMMUTABLETYPE is *not* set
+        cls.attr = 123
+
+        # Py_TPFLAGS_BASETYPE is *not* set
+        with self.assertRaises(TypeError):
+            class Sub(cls):
+                pass
+
+    def test_mod_slot_in_type(self):
+        with self.assertRaisesRegex(SystemError, "invalid.* 100 .*Py_mod_name"):
+            _testlimitedcapi.type_from_slots("foreign_slot")
+
+    def test_size_slots(self):
+        cls = _testlimitedcapi.type_from_slots("basicsize")
+        self.assertGreaterEqual(cls.__basicsize__, 256)
+
+        cls = _testlimitedcapi.type_from_slots("extra_basicsize")
+        self.assertGreaterEqual(cls.__basicsize__, object.__basicsize__ + 256)
+
+        cls = _testlimitedcapi.type_from_slots("itemsize")
+        self.assertGreaterEqual(cls.__itemsize__, 16)
+
+    def test_flag_slots(self):
+        cls = _testlimitedcapi.type_from_slots("flags")
+        with self.assertRaises(TypeError):
+            # Py_TPFLAGS_IMMUTABLETYPE is set
+            cls.attr = 123
+        class Sub(cls):
+            # Py_TPFLAGS_BASETYPE is set
+            pass
+
+    def test_func_slots(self):
+        cls = _testlimitedcapi.type_from_slots("matmul_123")
+        self.assertEqual(cls() @ None, 123)
+
+    def test_optional_end(self):
+        with self.assertRaisesRegex(SystemError, "invalid flags.*Py_slot_end"):
+            cls = _testlimitedcapi.type_from_slots("optional_end")
+
+    def test_invalid(self):
+        with self.assertRaisesRegex(SystemError, "Py_slot_invalid"):
+            cls = _testlimitedcapi.type_from_slots("invalid")
+        with self.assertRaisesRegex(SystemError, f"slot ID {0xfbad}"):
+            cls = _testlimitedcapi.type_from_slots("invalid_fbad")
+
+        cls = _testlimitedcapi.type_from_slots("optional_invalid")
+        self.assertGreaterEqual(cls.__basicsize__, object.__basicsize__ + 256)
+
+        cls = _testlimitedcapi.type_from_slots("optional_invalid_fbad")
+        self.assertGreaterEqual(cls.__basicsize__, object.__basicsize__ + 256)
+
+    @subTests("case_name", ["old_slot_numbers", "new_slot_numbers"])
+    def test_compat_slot_numbers(self, case_name):
+        cls = _testlimitedcapi.type_from_slots(case_name)
+        obj = cls()
+
+        # Py_bf_getbuffer (1), Py_bf_releasebuffer (2)
+        self.assertEqual(obj.buf_counter, 0)
+        mem = memoryview(obj)
+        self.assertEqual(bytes(mem), b"buf\0")
+        self.assertEqual(obj.buf_counter, 1)
+        mem.release()
+        self.assertEqual(obj.buf_counter, 0)
+
+        # Py_mp_ass_subscript (3)
+        with self.assertRaises(KeyError):
+            obj["key"] = "value"
+
+        # Py_mp_length (4)
+        self.assertEqual(len(obj), 456)
+
+    def test_nonstatic_tp_members(self):
+        with self.assertRaisesRegex(SystemError, "Py_tp_members.*STATIC"):
+            _testlimitedcapi.type_from_slots("nonstatic_tp_members")
+
+    def test_intptr_flags(self):
+        cls = _testlimitedcapi.type_from_slots("intptr_flags_macro")
+        with self.assertRaises(TypeError):
+            # Py_TPFLAGS_IMMUTABLETYPE is set
+            cls.attr = 123
+
+        cls = _testlimitedcapi.type_from_slots("intptr_flags_struct")
+        with self.assertRaises(TypeError):
+            # Py_TPFLAGS_IMMUTABLETYPE is set
+            cls.attr = 123
+
+        cls = _testlimitedcapi.type_from_slots("intptr_static")
+        cls.attribute = 123
+
+    def test_nested(self):
+        cls = _testlimitedcapi.type_from_slots("nested")
+        self.assertEqual(cls() + 1, 123)
+        self.assertEqual(cls() - 1, 234)
+
+        cls = _testlimitedcapi.type_from_slots("nested_max")
+        self.assertEqual(cls() + 1, 123)
+        self.assertEqual(cls() - 1, 234)
+        self.assertEqual(cls() * 1, 345)
+        self.assertEqual(cls() / 1, 456)
+        self.assertEqual(cls() % 1, 567)
+
+        with self.assertRaises(SystemError):
+            _testlimitedcapi.type_from_slots("nested_over_limit")
+
+        cls = _testlimitedcapi.type_from_slots("nested_old")
+        self.assertEqual(cls() + 1, 123)
+        self.assertEqual(cls() - 1, 234)
+
+        cls = _testlimitedcapi.type_from_slots("nested_old_max")
+        self.assertEqual(cls() + 1, 123)
+        self.assertEqual(cls() - 1, 234)
+        self.assertEqual(cls() * 1, 345)
+        self.assertEqual(cls() / 1, 456)
+        self.assertEqual(cls() % 1, 567)
+
+        with self.assertRaises(SystemError):
+            _testlimitedcapi.type_from_slots("nested_old_over_limit")
+
+        cls = _testlimitedcapi.type_from_slots("nested_pingpong")
+        self.assertEqual(cls() + 1, 123)
+        self.assertEqual(cls() - 1, 234)
+        self.assertEqual(cls() * 1, 345)
+        self.assertEqual(cls() / 1, 456)
+        self.assertEqual(cls() % 1, 567)
+
+    # Slot names aren't exposed to Python yet; see Include/slots_generated.h
+    # for the definitions.
+
+    @subTests("slot_number", [
+        *range(1, 83),      # Original slots
+        *range(88, 92),     # New compat slot values
+        *range(95, 99),     # Slots for PyType_Spec fields
+        *range(107, 109),   # Slots for PyType_FromMetaclass args
+    ])
+    def test_null_slot_handling(self, slot_number):
+        if slot_number == 56:
+            # Py_tp_doc
+            return
+        elif slot_number == 72 or slot_number >= 95:
+            # Py_tp_members; all new slots
+            ctx = self.assertRaises(SystemError)
+            ctx_old = ctx
+        else:
+            ctx = self.assertWarnsRegex(DeprecationWarning, "NULL")
+            ctx_old = contextlib.nullcontext()
+        with ctx:
+            _testlimitedcapi.type_from_null_slot(slot_number)
+        if slot_number < 95:
+            with ctx_old:
+                _testlimitedcapi.type_from_null_spec_slot(slot_number)
+
+    def test_repeat_warning(self):
+        with self.assertWarnsRegex(DeprecationWarning, "multiple"):
+            cls = _testlimitedcapi.type_from_slots("repeat_add")
+        self.assertEqual(cls() + 1, 456)
+
+    def test_repeat_error(self):
+        with self.assertRaisesRegex(SystemError, "multiple"):
+            cls = _testlimitedcapi.type_from_slots("repeat_module")
+
+class ModuleSlotsTests(unittest.TestCase):
+    def test_basic_module_slots(self):
+        mod = _testlimitedcapi.module_from_slots("basic", FakeSpec())
+        self.assertIsInstance(mod, types.ModuleType)
+
+    def test_type_slot_in_module(self):
+        with self.assertRaisesRegex(SystemError, "invalid.* 95 .*Py_tp_name"):
+            _testlimitedcapi.module_from_slots("foreign_slot", FakeSpec())
+
+    def test_size_slots(self):
+        mod = _testlimitedcapi.module_from_slots("state_size", FakeSpec())
+        self.assertEqual(mod.state_size, 42)
+
+    def test_flag_slots(self):
+        mod = _testlimitedcapi.module_from_slots("multi_interp", FakeSpec())
+
+    def test_exec_slot(self):
+        mod = _testlimitedcapi.module_from_slots("exec", FakeSpec())
+        self.assertEqual(mod.exec_done, "yes")
+
+    def test_optional_end(self):
+        with self.assertRaisesRegex(SystemError, "invalid flags.*Py_slot_end"):
+            _testlimitedcapi.module_from_slots("optional_end", FakeSpec())
+
+    def test_invalid(self):
+        with self.assertRaisesRegex(SystemError, "Py_slot_invalid"):
+            _testlimitedcapi.module_from_slots("invalid", FakeSpec())
+        with self.assertRaisesRegex(SystemError, f"slot ID {0xfbad}"):
+            _testlimitedcapi.module_from_slots("invalid_fbad", FakeSpec())
+
+        mod = _testlimitedcapi.module_from_slots("optional_invalid", FakeSpec())
+        self.assertEqual(mod.exec_done, "yes")
+
+        mod = _testlimitedcapi.module_from_slots("optional_invalid_fbad", FakeSpec())
+        self.assertEqual(mod.exec_done, "yes")
+
+    @subTests("case_name", ["old_slot_numbers", "new_slot_numbers"])
+    def test_compat_slot_numbers(self, case_name):
+        mod = _testlimitedcapi.module_from_slots(case_name, FakeSpec())
+        self.assertEqual(mod.exec_done, "yes")
+
+    @subTests("case_name", ["old_slot_number_create", "new_slot_number_create"])
+    def test_compat_slot_number_create(self, case_name):
+        spec = FakeSpec()
+        mod = _testlimitedcapi.module_from_slots(case_name, spec)
+        self.assertIs(mod, spec)
+
+    @subTests("slot_number", [4, 87])
+    def test_compat_slot_number_gil(self, slot_number):
+        spec = FakeSpec()
+        gil_enabled = sys._is_gil_enabled()
+        mod = _testlimitedcapi.module_from_gil_slot(slot_number, spec)
+        self.assertEqual(gil_enabled, sys._is_gil_enabled())
+
+    def test_nonstatic_mod_methods(self):
+        with self.assertRaisesRegex(SystemError, "Py_mod_methods.*STATIC"):
+            _testlimitedcapi.module_from_slots("nonstatic_mod_methods",
+                                               FakeSpec())
+
+    def test_intptr_methods(self):
+        mod = _testlimitedcapi.module_from_slots("intptr_methods",
+                                                 FakeSpec())
+        self.assertEqual(mod.type_from_slots.__name__, "type_from_slots")
+
+    def test_nested(self):
+        mod = _testlimitedcapi.module_from_slots("nested", FakeSpec())
+        self.assertEqual(mod.exec_done, "yes")
+        self.assertEqual(mod.__doc__, "doc")
+
+        mod = _testlimitedcapi.module_from_slots("nested_max", FakeSpec())
+        self.assertEqual(mod.exec_done, "yes")
+        self.assertEqual(mod.state_size, 53)
+        self.assertEqual(mod.__doc__, "doc")
+
+        with self.assertRaises(SystemError):
+            _testlimitedcapi.module_from_slots("nested_over_limit", FakeSpec())
+
+        mod = _testlimitedcapi.module_from_slots("nested_old", FakeSpec())
+        self.assertEqual(mod.exec_done, "yes")
+        self.assertEqual(mod.__doc__, "doc")
+
+        mod = _testlimitedcapi.module_from_slots("nested_old_max", FakeSpec())
+        self.assertEqual(mod.exec_done, "yes")
+        self.assertEqual(mod.state_size, 53)
+        self.assertEqual(mod.__doc__, "doc")
+
+        with self.assertRaises(SystemError):
+            _testlimitedcapi.module_from_slots("nested_old_over_limit", FakeSpec())
+
+        mod = _testlimitedcapi.module_from_slots("nested_pingpong", FakeSpec())
+        self.assertEqual(mod.exec_done, "yes")
+        self.assertEqual(mod.state_size, 53)
+        self.assertEqual(mod.__doc__, "doc")
+
+    def test_nested_nonstatic_from_def(self):
+        with self.assertRaisesRegex(SystemError, "must be static"):
+            _testcapi.module_from_def_nonstatic_nested(FakeSpec())
+
+    # Slot names aren't exposed to Python yet; see Include/slots_generated.h
+    # for the definitions.
+
+    @subTests("slot_number", [
+        *range(1, 5),       # Old compat slot values
+        *range(84, 88),     # New compat slot values
+        *range(100, 107),   # Slots for PyModuleDef fields
+        *range(109, 111),   # Slots new in 3.15
+    ])
+    def test_null_slot_handling(self, slot_number):
+        if slot_number in {3, 86, 4, 87, 102}:
+            # Py_mod_mult.interp., Py_mod_gil, Py_mod_state_size
+            return
+        elif slot_number in {2, 85} or slot_number > 85:
+            # Py_mod_exec, new slots
+            ctx = self.assertRaises(SystemError)
+            ctx_old = ctx
+        else:
+            ctx = self.assertWarnsRegex(DeprecationWarning, "NULL")
+            ctx_old = contextlib.nullcontext()
+        with ctx:
+            _testlimitedcapi.module_from_null_slot(slot_number, FakeSpec())
+        with ctx_old:
+            _testcapi.module_from_null_def_slot(slot_number,
+                                                FakeSpec())
+
+    def test_repeat_error(self):
+        with self.assertRaisesRegex(SystemError, "multiple"):
+            _testlimitedcapi.module_from_slots("repeat_create", FakeSpec())
+        with self.assertRaisesRegex(SystemError, "multiple"):
+            _testlimitedcapi.module_from_slots("repeat_exec", FakeSpec())
+        with self.assertRaisesRegex(SystemError, "multiple"):
+            _testlimitedcapi.module_from_slots("repeat_gil", FakeSpec())
diff --git a/Lib/test/test_cext/extension.c b/Lib/test/test_cext/extension.c
--- a/Lib/test/test_cext/extension.c
+++ b/Lib/test/test_cext/extension.c
@@ -125,13 +125,13 @@ _Py_COMP_DIAG_PUSH
 PyDoc_STRVAR(_testcext_doc, "C test extension.");
 PyABIInfo_VAR(abi_info);
 
-static PyModuleDef_Slot _testcext_slots[] = {
-    {Py_mod_abi, &abi_info},
-    {Py_mod_name, STR(MODULE_NAME)},
-    {Py_mod_doc, (void*)(char*)_testcext_doc},
-    {Py_mod_exec, (void*)_testcext_exec},
-    {Py_mod_methods, _testcext_methods},
-    {0, NULL}
+static PySlot _testcext_slots[] = {
+    PySlot_STATIC_DATA(Py_mod_abi, &abi_info),
+    PySlot_STATIC_DATA(Py_mod_name, STR(MODULE_NAME)),
+    PySlot_STATIC_DATA(Py_mod_doc, (void*)(char*)_testcext_doc),
+    PySlot_FUNC(Py_mod_exec, (void*)_testcext_exec),
+    PySlot_STATIC_DATA(Py_mod_methods, _testcext_methods),
+    PySlot_END,
 };
 
 _Py_COMP_DIAG_POP
diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py
--- a/Lib/test/test_stable_abi_ctypes.py
+++ b/Lib/test/test_stable_abi_ctypes.py
@@ -743,6 +743,7 @@ def test_windows_feature_macros(self):
     "PyType_Freeze",
     "PyType_FromMetaclass",
     "PyType_FromModuleAndSpec",
+    "PyType_FromSlots",
     "PyType_FromSpec",
     "PyType_FromSpecWithBases",
     "PyType_GenericAlloc",