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.

142 lines
4.4 KiB

  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. static PyObject *future;
  57. if (!future) {
  58. future = PyString_InternFromString("__future__");
  59. if (!future)
  60. return 0;
  61. }
  62. if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
  63. return 1;
  64. /* A subsequent pass will detect future imports that don't
  65. appear at the beginning of the file. There's one case,
  66. however, that is easier to handle here: A series of imports
  67. joined by semi-colons, where the first import is a future
  68. statement but some subsequent import has the future form
  69. but is preceded by a regular import.
  70. */
  71. for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
  72. stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
  73. if (done && s->lineno > prev_line)
  74. return 1;
  75. prev_line = s->lineno;
  76. /* The tests below will return from this function unless it is
  77. still possible to find a future statement. The only things
  78. that can precede a future statement are another future
  79. statement and a doc string.
  80. */
  81. if (s->kind == ImportFrom_kind) {
  82. if (s->v.ImportFrom.module == future) {
  83. if (done) {
  84. PyErr_SetString(PyExc_SyntaxError,
  85. ERR_LATE_FUTURE);
  86. PyErr_SyntaxLocation(filename,
  87. s->lineno);
  88. return 0;
  89. }
  90. if (!future_check_features(ff, s, filename))
  91. return 0;
  92. ff->ff_lineno = s->lineno;
  93. }
  94. else
  95. done = 1;
  96. }
  97. else if (s->kind == Expr_kind && !found_docstring) {
  98. expr_ty e = s->v.Expr.value;
  99. if (e->kind != Str_kind)
  100. done = 1;
  101. else
  102. found_docstring = 1;
  103. }
  104. else
  105. done = 1;
  106. }
  107. return 1;
  108. }
  109. PyFutureFeatures *
  110. PyFuture_FromAST(mod_ty mod, const char *filename)
  111. {
  112. PyFutureFeatures *ff;
  113. ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
  114. if (ff == NULL) {
  115. PyErr_NoMemory();
  116. return NULL;
  117. }
  118. ff->ff_features = 0;
  119. ff->ff_lineno = -1;
  120. if (!future_parse(ff, mod, filename)) {
  121. PyObject_Free(ff);
  122. return NULL;
  123. }
  124. return ff;
  125. }