1 /* typing accelerator C extension: _typing module. */
2
3 #ifndef Py_BUILD_CORE
4 #define Py_BUILD_CORE
5 #endif
6
7 #include "Python.h"
8 #include "pycore_interp.h"
9 #include "pycore_pystate.h" // _PyInterpreterState_GET()
10 #include "pycore_typevarobject.h"
11 #include "clinic/_typingmodule.c.h"
12
13 /*[clinic input]
14 module _typing
15
16 [clinic start generated code]*/
17 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=1db35baf1c72942b]*/
18
19 /* helper function to make typing.NewType.__call__ method faster */
20
21 /*[clinic input]
22 _typing._idfunc -> object
23
24 x: object
25 /
26
27 [clinic start generated code]*/
28
29 static PyObject *
_typing__idfunc(PyObject * module,PyObject * x)30 _typing__idfunc(PyObject *module, PyObject *x)
31 /*[clinic end generated code: output=63c38be4a6ec5f2c input=49f17284b43de451]*/
32 {
33 return Py_NewRef(x);
34 }
35
36
37 static PyMethodDef typing_methods[] = {
38 _TYPING__IDFUNC_METHODDEF
39 {NULL, NULL, 0, NULL}
40 };
41
42 PyDoc_STRVAR(typing_doc,
43 "Primitives and accelerators for the typing module.\n");
44
45 static int
_typing_exec(PyObject * m)46 _typing_exec(PyObject *m)
47 {
48 PyInterpreterState *interp = _PyInterpreterState_GET();
49
50 #define EXPORT_TYPE(name, typename) \
51 if (PyModule_AddObjectRef(m, name, \
52 (PyObject *)interp->cached_objects.typename) < 0) { \
53 return -1; \
54 }
55
56 EXPORT_TYPE("TypeVar", typevar_type);
57 EXPORT_TYPE("TypeVarTuple", typevartuple_type);
58 EXPORT_TYPE("ParamSpec", paramspec_type);
59 EXPORT_TYPE("ParamSpecArgs", paramspecargs_type);
60 EXPORT_TYPE("ParamSpecKwargs", paramspeckwargs_type);
61 EXPORT_TYPE("Generic", generic_type);
62 #undef EXPORT_TYPE
63 if (PyModule_AddObjectRef(m, "TypeAliasType", (PyObject *)&_PyTypeAlias_Type) < 0) {
64 return -1;
65 }
66 if (PyModule_AddObjectRef(m, "NoDefault", (PyObject *)&_Py_NoDefaultStruct) < 0) {
67 return -1;
68 }
69 return 0;
70 }
71
72 static struct PyModuleDef_Slot _typingmodule_slots[] = {
73 {Py_mod_exec, _typing_exec},
74 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
75 {Py_mod_gil, Py_MOD_GIL_NOT_USED},
76 {0, NULL}
77 };
78
79 static struct PyModuleDef typingmodule = {
80 PyModuleDef_HEAD_INIT,
81 "_typing",
82 typing_doc,
83 0,
84 typing_methods,
85 _typingmodule_slots,
86 NULL,
87 NULL,
88 NULL
89 };
90
91 PyMODINIT_FUNC
PyInit__typing(void)92 PyInit__typing(void)
93 {
94 return PyModuleDef_Init(&typingmodule);
95 }
96