GeezerVison is showing github.com — gh-98811: use full source location to simplify __future__ imports err… · Maddie-Code-Collab/cpython@39448ad
File treeExpand file treeCollapse file tree Open diff view settings Misc/NEWS.d/next/Core and Builtins
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Include/cpython/compile.h +17-2Lines changed: 17 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,11 +31,26 @@ typedef struct { | 31 | 31 | #define _PyCompilerFlags_INIT \ | 32 | 32 | (PyCompilerFlags){.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION} | 33 | 33 |
| | 34 | +/* source location information */ | | 35 | +typedef struct { | | 36 | + int lineno; | | 37 | + int end_lineno; | | 38 | + int col_offset; | | 39 | + int end_col_offset; | | 40 | +} _PyCompilerSrcLocation; | | 41 | + | | 42 | +#define SRC_LOCATION_FROM_AST(n) \ | | 43 | + (_PyCompilerSrcLocation){ \ | | 44 | + .lineno = (n)->lineno, \ | | 45 | + .end_lineno = (n)->end_lineno, \ | | 46 | + .col_offset = (n)->col_offset, \ | | 47 | + .end_col_offset = (n)->end_col_offset } | | 48 | + | 34 | 49 | /* Future feature support */ | 35 | 50 |
| 36 | 51 | typedef struct { | 37 | | - int ff_features; /* flags set by future statements */ | 38 | | - int ff_lineno; /* line number of last future statement */ | | 52 | + int ff_features; /* flags set by future statements */ | | 53 | + _PyCompilerSrcLocation ff_location; /* location of last future statement */ | 39 | 54 | } PyFutureFeatures; | 40 | 55 |
| 41 | 56 | #define FUTURE_NESTED_SCOPES "nested_scopes" | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_future.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,7 +60,7 @@ def test_badfuture6(self): | 60 | 60 | def test_badfuture7(self): | 61 | 61 | with self.assertRaises(SyntaxError) as cm: | 62 | 62 | from test import badsyntax_future7 | 63 | | - self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 53) | | 63 | + self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 54) | 64 | 64 |
| 65 | 65 | def test_badfuture8(self): | 66 | 66 | with self.assertRaises(SyntaxError) as cm: | |
Collapse file Copy file name to clipboard +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +Use complete source locations to simplify detection of ``__future__`` | | 2 | +imports which are not at the beginning of the file. Also corrects the offset | | 3 | +in the exception raised in one case, which was off by one and impeded | | 4 | +highlighting. |
Collapse file Copy file name to clipboardExpand all lines: Python/compile.c +32-30Lines changed: 32 additions & 30 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 125 | 125 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ | 126 | 126 | && (c->u->u_ste->ste_type == ModuleBlock)) | 127 | 127 |
| 128 | | -typedef struct location_ { | 129 | | - int lineno; | 130 | | - int end_lineno; | 131 | | - int col_offset; | 132 | | - int end_col_offset; | 133 | | -} location; | | 128 | +typedef _PyCompilerSrcLocation location; | 134 | 129 |
| 135 | 130 | #define LOCATION(LNO, END_LNO, COL, END_COL) \ | 136 | 131 | ((const location){(LNO), (END_LNO), (COL), (END_COL)}) | 137 | 132 |
| 138 | 133 | static location NO_LOCATION = {-1, -1, -1, -1}; | 139 | 134 |
| | 135 | +/* Return true if loc1 starts after loc2 ends. */ | | 136 | +static inline bool | | 137 | +location_is_after(location loc1, location loc2) { | | 138 | + return (loc1.lineno > loc2.end_lineno) || | | 139 | + ((loc1.lineno == loc2.end_lineno) && | | 140 | + (loc1.col_offset > loc2.end_col_offset)); | | 141 | +} | | 142 | + | | 143 | +#define LOC(x) SRC_LOCATION_FROM_AST(x) | | 144 | + | 140 | 145 | typedef struct jump_target_label_ { | 141 | 146 | int id; | 142 | 147 | } jump_target_label; | @@ -1012,11 +1017,6 @@ basicblock_next_instr(basicblock *b) | 1012 | 1017 | // Artificial instructions | 1013 | 1018 | #define UNSET_LOC(c) | 1014 | 1019 |
| 1015 | | -#define LOC(x) LOCATION((x)->lineno, \ | 1016 | | - (x)->end_lineno, \ | 1017 | | - (x)->col_offset, \ | 1018 | | - (x)->end_col_offset) | 1019 | | - | 1020 | 1020 |
| 1021 | 1021 | /* Return the stack effect of opcode with argument oparg. | 1022 | 1022 |
| @@ -3911,59 +3911,61 @@ compiler_import(struct compiler *c, stmt_ty s) | 3911 | 3911 | static int | 3912 | 3912 | compiler_from_import(struct compiler *c, stmt_ty s) | 3913 | 3913 | { | 3914 | | - location loc = LOC(s); | 3915 | | - Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); | 3916 | | - PyObject *names; | | 3914 | + Py_ssize_t n = asdl_seq_LEN(s->v.ImportFrom.names); | 3917 | 3915 |
| 3918 | | - ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromLong(s->v.ImportFrom.level)); | | 3916 | + ADDOP_LOAD_CONST_NEW(c, LOC(s), PyLong_FromLong(s->v.ImportFrom.level)); | 3919 | 3917 |
| 3920 | | - names = PyTuple_New(n); | 3921 | | - if (!names) | | 3918 | + PyObject *names = PyTuple_New(n); | | 3919 | + if (!names) { | 3922 | 3920 | return 0; | | 3921 | + } | 3923 | 3922 |
| 3924 | 3923 | /* build up the names */ | 3925 | | - for (i = 0; i < n; i++) { | | 3924 | + for (Py_ssize_t i = 0; i < n; i++) { | 3926 | 3925 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); | 3927 | 3926 | Py_INCREF(alias->name); | 3928 | 3927 | PyTuple_SET_ITEM(names, i, alias->name); | 3929 | 3928 | } | 3930 | 3929 |
| 3931 | | - if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && | 3932 | | - _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { | | 3930 | + if (location_is_after(LOC(s), c->c_future->ff_location) && | | 3931 | + s->v.ImportFrom.module && | | 3932 | + _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) | | 3933 | + { | 3933 | 3934 | Py_DECREF(names); | 3934 | | - return compiler_error(c, loc, "from __future__ imports must occur " | | 3935 | + return compiler_error(c, LOC(s), "from __future__ imports must occur " | 3935 | 3936 | "at the beginning of the file"); | 3936 | 3937 | } | 3937 | | - ADDOP_LOAD_CONST_NEW(c, loc, names); | | 3938 | + ADDOP_LOAD_CONST_NEW(c, LOC(s), names); | 3938 | 3939 |
| 3939 | 3940 | if (s->v.ImportFrom.module) { | 3940 | | - ADDOP_NAME(c, loc, IMPORT_NAME, s->v.ImportFrom.module, names); | | 3941 | + ADDOP_NAME(c, LOC(s), IMPORT_NAME, s->v.ImportFrom.module, names); | 3941 | 3942 | } | 3942 | 3943 | else { | 3943 | 3944 | _Py_DECLARE_STR(empty, ""); | 3944 | | - ADDOP_NAME(c, loc, IMPORT_NAME, &_Py_STR(empty), names); | | 3945 | + ADDOP_NAME(c, LOC(s), IMPORT_NAME, &_Py_STR(empty), names); | 3945 | 3946 | } | 3946 | | - for (i = 0; i < n; i++) { | | 3947 | + for (Py_ssize_t i = 0; i < n; i++) { | 3947 | 3948 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); | 3948 | 3949 | identifier store_name; | 3949 | 3950 |
| 3950 | 3951 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { | 3951 | 3952 | assert(n == 1); | 3952 | | - ADDOP(c, loc, IMPORT_STAR); | | 3953 | + ADDOP(c, LOC(s), IMPORT_STAR); | 3953 | 3954 | return 1; | 3954 | 3955 | } | 3955 | 3956 |
| 3956 | | - ADDOP_NAME(c, loc, IMPORT_FROM, alias->name, names); | | 3957 | + ADDOP_NAME(c, LOC(s), IMPORT_FROM, alias->name, names); | 3957 | 3958 | store_name = alias->name; | 3958 | | - if (alias->asname) | | 3959 | + if (alias->asname) { | 3959 | 3960 | store_name = alias->asname; | | 3961 | + } | 3960 | 3962 |
| 3961 | | - if (!compiler_nameop(c, loc, store_name, Store)) { | | 3963 | + if (!compiler_nameop(c, LOC(s), store_name, Store)) { | 3962 | 3964 | return 0; | 3963 | 3965 | } | 3964 | 3966 | } | 3965 | 3967 | /* remove imported module */ | 3966 | | - ADDOP(c, loc, POP_TOP); | | 3968 | + ADDOP(c, LOC(s), POP_TOP); | 3967 | 3969 | return 1; | 3968 | 3970 | } | 3969 | 3971 |
| |
Collapse file Copy file name to clipboardExpand all lines: Python/future.c +17-36Lines changed: 17 additions & 36 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | #include "pycore_ast.h" // _PyAST_GetDocString() | 3 | 3 |
| 4 | 4 | #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined" | 5 | | -#define ERR_LATE_FUTURE \ | 6 | | -"from __future__ imports must occur at the beginning of the file" | 7 | 5 |
| 8 | 6 | static int | 9 | 7 | future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename) | @@ -56,59 +54,42 @@ future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename) | 56 | 54 | static int | 57 | 55 | future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename) | 58 | 56 | { | 59 | | - int i, done = 0, prev_line = 0; | 60 | | - | 61 | | - if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) | | 57 | + if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) { | 62 | 58 | return 1; | | 59 | + } | 63 | 60 |
| 64 | | - if (asdl_seq_LEN(mod->v.Module.body) == 0) | | 61 | + Py_ssize_t n = asdl_seq_LEN(mod->v.Module.body); | | 62 | + if (n == 0) { | 65 | 63 | return 1; | | 64 | + } | 66 | 65 |
| 67 | | - /* A subsequent pass will detect future imports that don't | 68 | | - appear at the beginning of the file. There's one case, | 69 | | - however, that is easier to handle here: A series of imports | 70 | | - joined by semi-colons, where the first import is a future | 71 | | - statement but some subsequent import has the future form | 72 | | - but is preceded by a regular import. | 73 | | - */ | 74 | | - | 75 | | - i = 0; | 76 | | - if (_PyAST_GetDocString(mod->v.Module.body) != NULL) | | 66 | + Py_ssize_t i = 0; | | 67 | + if (_PyAST_GetDocString(mod->v.Module.body) != NULL) { | 77 | 68 | i++; | | 69 | + } | 78 | 70 |
| 79 | | - for (; i < asdl_seq_LEN(mod->v.Module.body); i++) { | | 71 | + for (; i < n; i++) { | 80 | 72 | stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i); | 81 | 73 |
| 82 | | - if (done && s->lineno > prev_line) | 83 | | - return 1; | 84 | | - prev_line = s->lineno; | 85 | | - | 86 | | - /* The tests below will return from this function unless it is | 87 | | - still possible to find a future statement. The only things | 88 | | - that can precede a future statement are another future | 89 | | - statement and a doc string. | 90 | | - */ | | 74 | + /* The only things that can precede a future statement | | 75 | + * are another future statement and a doc string. | | 76 | + */ | 91 | 77 |
| 92 | 78 | if (s->kind == ImportFrom_kind) { | 93 | 79 | identifier modname = s->v.ImportFrom.module; | 94 | 80 | if (modname && | 95 | 81 | _PyUnicode_EqualToASCIIString(modname, "__future__")) { | 96 | | - if (done) { | 97 | | - PyErr_SetString(PyExc_SyntaxError, | 98 | | - ERR_LATE_FUTURE); | 99 | | - PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); | | 82 | + if (!future_check_features(ff, s, filename)) { | 100 | 83 | return 0; | 101 | 84 | } | 102 | | - if (!future_check_features(ff, s, filename)) | 103 | | - return 0; | 104 | | - ff->ff_lineno = s->lineno; | | 85 | + ff->ff_location = SRC_LOCATION_FROM_AST(s); | 105 | 86 | } | 106 | 87 | else { | 107 | | - done = 1; | | 88 | + return 1; | 108 | 89 | } | 109 | 90 | } | 110 | 91 | else { | 111 | | - done = 1; | | 92 | + return 1; | 112 | 93 | } | 113 | 94 | } | 114 | 95 | return 1; | @@ -126,7 +107,7 @@ _PyFuture_FromAST(mod_ty mod, PyObject *filename) | 126 | 107 | return NULL; | 127 | 108 | } | 128 | 109 | ff->ff_features = 0; | 129 | | - ff->ff_lineno = -1; | | 110 | + ff->ff_location = (_PyCompilerSrcLocation){-1, -1, -1, -1}; | 130 | 111 |
| 131 | 112 | if (!future_parse(ff, mod, filename)) { | 132 | 113 | PyObject_Free(ff); | |
|