_typingmodule.c 2.1 KB

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