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.

102 lines
2.8 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. char *filename;
  12. char *startstr;
  13. int start;
  14. if (!PyArg_ParseTuple(args, "sss:symtable", &str, &filename,
  15. &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. return NULL;
  27. }
  28. st = Py_SymtableString(str, filename, start);
  29. if (st == NULL)
  30. return NULL;
  31. t = st->st_blocks;
  32. Py_INCREF(t);
  33. PyMem_Free((void *)st->st_future);
  34. PySymtable_Free(st);
  35. return t;
  36. }
  37. static PyMethodDef symtable_methods[] = {
  38. {"symtable", symtable_symtable, METH_VARARGS,
  39. PyDoc_STR("Return symbol and scope dictionaries"
  40. " used internally by compiler.")},
  41. {NULL, NULL} /* sentinel */
  42. };
  43. static struct PyModuleDef symtablemodule = {
  44. PyModuleDef_HEAD_INIT,
  45. "_symtable",
  46. NULL,
  47. -1,
  48. symtable_methods,
  49. NULL,
  50. NULL,
  51. NULL,
  52. NULL
  53. };
  54. PyMODINIT_FUNC
  55. PyInit__symtable(void)
  56. {
  57. PyObject *m;
  58. if (PyType_Ready(&PySTEntry_Type) < 0)
  59. return NULL;
  60. m = PyModule_Create(&symtablemodule);
  61. if (m == NULL)
  62. return NULL;
  63. PyModule_AddIntConstant(m, "USE", USE);
  64. PyModule_AddIntConstant(m, "DEF_GLOBAL", DEF_GLOBAL);
  65. PyModule_AddIntConstant(m, "DEF_LOCAL", DEF_LOCAL);
  66. PyModule_AddIntConstant(m, "DEF_PARAM", DEF_PARAM);
  67. PyModule_AddIntConstant(m, "DEF_FREE", DEF_FREE);
  68. PyModule_AddIntConstant(m, "DEF_FREE_CLASS", DEF_FREE_CLASS);
  69. PyModule_AddIntConstant(m, "DEF_IMPORT", DEF_IMPORT);
  70. PyModule_AddIntConstant(m, "DEF_BOUND", DEF_BOUND);
  71. PyModule_AddIntConstant(m, "TYPE_FUNCTION", FunctionBlock);
  72. PyModule_AddIntConstant(m, "TYPE_CLASS", ClassBlock);
  73. PyModule_AddIntConstant(m, "TYPE_MODULE", ModuleBlock);
  74. PyModule_AddIntConstant(m, "OPT_IMPORT_STAR", OPT_IMPORT_STAR);
  75. PyModule_AddIntConstant(m, "OPT_TOPLEVEL", OPT_TOPLEVEL);
  76. PyModule_AddIntConstant(m, "LOCAL", LOCAL);
  77. PyModule_AddIntConstant(m, "GLOBAL_EXPLICIT", GLOBAL_EXPLICIT);
  78. PyModule_AddIntConstant(m, "GLOBAL_IMPLICIT", GLOBAL_IMPLICIT);
  79. PyModule_AddIntConstant(m, "FREE", FREE);
  80. PyModule_AddIntConstant(m, "CELL", CELL);
  81. PyModule_AddIntConstant(m, "SCOPE_OFF", SCOPE_OFFSET);
  82. PyModule_AddIntConstant(m, "SCOPE_MASK", SCOPE_MASK);
  83. if (PyErr_Occurred()) {
  84. Py_DECREF(m);
  85. m = 0;
  86. }
  87. return m;
  88. }