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.

137 lines
4.4 KiB

14 years ago
  1. #include "Python.h"
  2. #include "Python-ast.h"
  3. #include "node.h"
  4. #include "token.h"
  5. #include "graminit.h"
  6. #include "code.h"
  7. #include "compile.h"
  8. #include "symtable.h"
  9. #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
  10. #define ERR_LATE_FUTURE \
  11. "from __future__ imports must occur at the beginning of the file"
  12. static int
  13. future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
  14. {
  15. int i;
  16. asdl_seq *names;
  17. assert(s->kind == ImportFrom_kind);
  18. names = s->v.ImportFrom.names;
  19. for (i = 0; i < asdl_seq_LEN(names); i++) {
  20. alias_ty name = (alias_ty)asdl_seq_GET(names, i);
  21. const char *feature = PyString_AsString(name->name);
  22. if (!feature)
  23. return 0;
  24. if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
  25. continue;
  26. } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
  27. continue;
  28. } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
  29. ff->ff_features |= CO_FUTURE_DIVISION;
  30. } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
  31. ff->ff_features |= CO_FUTURE_ABSOLUTE_IMPORT;
  32. } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
  33. ff->ff_features |= CO_FUTURE_WITH_STATEMENT;
  34. } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
  35. ff->ff_features |= CO_FUTURE_PRINT_FUNCTION;
  36. } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
  37. ff->ff_features |= CO_FUTURE_UNICODE_LITERALS;
  38. } else if (strcmp(feature, "braces") == 0) {
  39. PyErr_SetString(PyExc_SyntaxError,
  40. "not a chance");
  41. PyErr_SyntaxLocation(filename, s->lineno);
  42. return 0;
  43. } else {
  44. PyErr_Format(PyExc_SyntaxError,
  45. UNDEFINED_FUTURE_FEATURE, feature);
  46. PyErr_SyntaxLocation(filename, s->lineno);
  47. return 0;
  48. }
  49. }
  50. return 1;
  51. }
  52. static int
  53. future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
  54. {
  55. int i, found_docstring = 0, done = 0, prev_line = 0;
  56. if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
  57. return 1;
  58. /* A subsequent pass will detect future imports that don't
  59. appear at the beginning of the file. There's one case,
  60. however, that is easier to handle here: A series of imports
  61. joined by semi-colons, where the first import is a future
  62. statement but some subsequent import has the future form
  63. but is preceded by a regular import.
  64. */
  65. for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
  66. stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
  67. if (done && s->lineno > prev_line)
  68. return 1;
  69. prev_line = s->lineno;
  70. /* The tests below will return from this function unless it is
  71. still possible to find a future statement. The only things
  72. that can precede a future statement are another future
  73. statement and a doc string.
  74. */
  75. if (s->kind == ImportFrom_kind) {
  76. identifier modname = s->v.ImportFrom.module;
  77. if (modname && PyString_GET_SIZE(modname) == 10 &&
  78. !strcmp(PyString_AS_STRING(modname), "__future__")) {
  79. if (done) {
  80. PyErr_SetString(PyExc_SyntaxError,
  81. ERR_LATE_FUTURE);
  82. PyErr_SyntaxLocation(filename,
  83. s->lineno);
  84. return 0;
  85. }
  86. if (!future_check_features(ff, s, filename))
  87. return 0;
  88. ff->ff_lineno = s->lineno;
  89. }
  90. else
  91. done = 1;
  92. }
  93. else if (s->kind == Expr_kind && !found_docstring) {
  94. expr_ty e = s->v.Expr.value;
  95. if (e->kind != Str_kind)
  96. done = 1;
  97. else
  98. found_docstring = 1;
  99. }
  100. else
  101. done = 1;
  102. }
  103. return 1;
  104. }
  105. PyFutureFeatures *
  106. PyFuture_FromAST(mod_ty mod, const char *filename)
  107. {
  108. PyFutureFeatures *ff;
  109. ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
  110. if (ff == NULL) {
  111. PyErr_NoMemory();
  112. return NULL;
  113. }
  114. ff->ff_features = 0;
  115. ff->ff_lineno = -1;
  116. if (!future_parse(ff, mod, filename)) {
  117. PyObject_Free(ff);
  118. return NULL;
  119. }
  120. return ff;
  121. }