symtablemodule.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "Python.h"
  2. #include "pycore_symtable.h" // struct symtable
  3. #include "clinic/symtablemodule.c.h"
  4. /*[clinic input]
  5. module _symtable
  6. [clinic start generated code]*/
  7. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f4685845a7100605]*/
  8. /*[clinic input]
  9. _symtable.symtable
  10. source: object
  11. filename: object(converter='PyUnicode_FSDecoder')
  12. startstr: str
  13. /
  14. Return symbol and scope dictionaries used internally by compiler.
  15. [clinic start generated code]*/
  16. static PyObject *
  17. _symtable_symtable_impl(PyObject *module, PyObject *source,
  18. PyObject *filename, const char *startstr)
  19. /*[clinic end generated code: output=59eb0d5fc7285ac4 input=9dd8a50c0c36a4d7]*/
  20. {
  21. struct symtable *st;
  22. PyObject *t;
  23. int start;
  24. PyCompilerFlags cf = _PyCompilerFlags_INIT;
  25. PyObject *source_copy = NULL;
  26. cf.cf_flags = PyCF_SOURCE_IS_UTF8;
  27. const char *str = _Py_SourceAsString(source, "symtable", "string or bytes", &cf, &source_copy);
  28. if (str == NULL) {
  29. return NULL;
  30. }
  31. if (strcmp(startstr, "exec") == 0)
  32. start = Py_file_input;
  33. else if (strcmp(startstr, "eval") == 0)
  34. start = Py_eval_input;
  35. else if (strcmp(startstr, "single") == 0)
  36. start = Py_single_input;
  37. else {
  38. PyErr_SetString(PyExc_ValueError,
  39. "symtable() arg 3 must be 'exec' or 'eval' or 'single'");
  40. Py_DECREF(filename);
  41. Py_XDECREF(source_copy);
  42. return NULL;
  43. }
  44. st = _Py_SymtableStringObjectFlags(str, filename, start, &cf);
  45. Py_DECREF(filename);
  46. Py_XDECREF(source_copy);
  47. if (st == NULL) {
  48. return NULL;
  49. }
  50. t = Py_NewRef(st->st_top);
  51. _PySymtable_Free(st);
  52. return t;
  53. }
  54. static PyMethodDef symtable_methods[] = {
  55. _SYMTABLE_SYMTABLE_METHODDEF
  56. {NULL, NULL} /* sentinel */
  57. };
  58. static int
  59. symtable_init_constants(PyObject *m)
  60. {
  61. if (PyModule_AddIntMacro(m, USE) < 0) return -1;
  62. if (PyModule_AddIntMacro(m, DEF_GLOBAL) < 0) return -1;
  63. if (PyModule_AddIntMacro(m, DEF_NONLOCAL) < 0) return -1;
  64. if (PyModule_AddIntMacro(m, DEF_LOCAL) < 0) return -1;
  65. if (PyModule_AddIntMacro(m, DEF_PARAM) < 0) return -1;
  66. if (PyModule_AddIntMacro(m, DEF_FREE) < 0) return -1;
  67. if (PyModule_AddIntMacro(m, DEF_FREE_CLASS) < 0) return -1;
  68. if (PyModule_AddIntMacro(m, DEF_IMPORT) < 0) return -1;
  69. if (PyModule_AddIntMacro(m, DEF_BOUND) < 0) return -1;
  70. if (PyModule_AddIntMacro(m, DEF_ANNOT) < 0) return -1;
  71. if (PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock) < 0)
  72. return -1;
  73. if (PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock) < 0) return -1;
  74. if (PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock) < 0)
  75. return -1;
  76. if (PyModule_AddIntConstant(m, "TYPE_ANNOTATION", AnnotationBlock) < 0)
  77. return -1;
  78. if (PyModule_AddIntConstant(m, "TYPE_TYPE_VAR_BOUND", TypeVarBoundBlock) < 0)
  79. return -1;
  80. if (PyModule_AddIntConstant(m, "TYPE_TYPE_ALIAS", TypeAliasBlock) < 0)
  81. return -1;
  82. if (PyModule_AddIntConstant(m, "TYPE_TYPE_PARAM", TypeParamBlock) < 0)
  83. return -1;
  84. if (PyModule_AddIntMacro(m, LOCAL) < 0) return -1;
  85. if (PyModule_AddIntMacro(m, GLOBAL_EXPLICIT) < 0) return -1;
  86. if (PyModule_AddIntMacro(m, GLOBAL_IMPLICIT) < 0) return -1;
  87. if (PyModule_AddIntMacro(m, FREE) < 0) return -1;
  88. if (PyModule_AddIntMacro(m, CELL) < 0) return -1;
  89. if (PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET) < 0) return -1;
  90. if (PyModule_AddIntMacro(m, SCOPE_MASK) < 0) return -1;
  91. return 0;
  92. }
  93. static PyModuleDef_Slot symtable_slots[] = {
  94. {Py_mod_exec, symtable_init_constants},
  95. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  96. {0, NULL}
  97. };
  98. static struct PyModuleDef symtablemodule = {
  99. PyModuleDef_HEAD_INIT,
  100. .m_name = "_symtable",
  101. .m_size = 0,
  102. .m_methods = symtable_methods,
  103. .m_slots = symtable_slots,
  104. };
  105. PyMODINIT_FUNC
  106. PyInit__symtable(void)
  107. {
  108. return PyModuleDef_Init(&symtablemodule);
  109. }