/Modules/symtablemodule.c

http://unladen-swallow.googlecode.com/ · C · 87 lines · 76 code · 11 blank · 0 comment · 14 complexity · 2ad164cd5a5f11c4c8b72454591c1744 MD5 · raw file

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