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