You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.5 KiB

  1. #include "Python.h"
  2. #include "code.h"
  3. #include "Python-ast.h"
  4. #include "symtable.h"
  5. static PyObject *
  6. symtable_symtable(PyObject *self, PyObject *args)
  7. {
  8. struct symtable *st;
  9. PyObject *t;
  10. char *str;
  11. PyObject *filename;
  12. char *startstr;
  13. int start;
  14. if (!PyArg_ParseTuple(args, "sO&s:symtable",
  15. &str, PyUnicode_FSDecoder, &filename, &startstr))
  16. return NULL;
  17. if (strcmp(startstr, "exec") == 0)
  18. start = Py_file_input;
  19. else if (strcmp(startstr, "eval") == 0)
  20. start = Py_eval_input;
  21. else if (strcmp(startstr, "single") == 0)
  22. start = Py_single_input;
  23. else {
  24. PyErr_SetString(PyExc_ValueError,
  25. "symtable() arg 3 must be 'exec' or 'eval' or 'single'");
  26. Py_DECREF(filename);
  27. return NULL;
  28. }
  29. st = Py_SymtableStringObject(str, filename, start);
  30. Py_DECREF(filename);
  31. if (st == NULL)
  32. return NULL;
  33. t = (PyObject *)st->st_top;
  34. Py_INCREF(t);
  35. PyMem_Free((void *)st->st_future);
  36. PySymtable_Free(st);
  37. return t;
  38. }
  39. static PyMethodDef symtable_methods[] = {
  40. {"symtable", symtable_symtable, METH_VARARGS,
  41. PyDoc_STR("Return symbol and scope dictionaries"
  42. " used internally by compiler.")},
  43. {NULL, NULL} /* sentinel */
  44. };
  45. static struct PyModuleDef symtablemodule = {
  46. PyModuleDef_HEAD_INIT,
  47. "_symtable",
  48. NULL,
  49. -1,
  50. symtable_methods,
  51. NULL,
  52. NULL,
  53. NULL,
  54. NULL
  55. };
  56. PyMODINIT_FUNC
  57. PyInit__symtable(void)
  58. {
  59. PyObject *m;
  60. if (PyType_Ready(&PySTEntry_Type) < 0)
  61. return NULL;
  62. m = PyModule_Create(&symtablemodule);
  63. if (m == NULL)
  64. return NULL;
  65. PyModule_AddIntMacro(m, USE);
  66. PyModule_AddIntMacro(m, DEF_GLOBAL);
  67. PyModule_AddIntMacro(m, DEF_LOCAL);
  68. PyModule_AddIntMacro(m, DEF_PARAM);
  69. PyModule_AddIntMacro(m, DEF_FREE);
  70. PyModule_AddIntMacro(m, DEF_FREE_CLASS);
  71. PyModule_AddIntMacro(m, DEF_IMPORT);
  72. PyModule_AddIntMacro(m, DEF_BOUND);
  73. PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock);
  74. PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock);
  75. PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock);
  76. PyModule_AddIntMacro(m, LOCAL);
  77. PyModule_AddIntMacro(m, GLOBAL_EXPLICIT);
  78. PyModule_AddIntMacro(m, GLOBAL_IMPLICIT);
  79. PyModule_AddIntMacro(m, FREE);
  80. PyModule_AddIntMacro(m, CELL);
  81. PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET);
  82. PyModule_AddIntMacro(m, SCOPE_MASK);
  83. if (PyErr_Occurred()) {
  84. Py_DECREF(m);
  85. m = 0;
  86. }
  87. return m;
  88. }