8000 py: fix bug with doc string not recognised after first newline of file. · comfuture/micropython@e388f10 · GitHub
[go: up one dir, main page]

Skip to content

Commit e388f10

Browse files
committed
py: fix bug with doc string not recognised after first newline of file.
1 parent 02f8941 commit e388f10

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

py/compile.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2646,10 +2646,21 @@ void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) {
26462646

26472647
// look for the first statement
26482648
if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2649-
// fall through
2649+
// a statement; fall through
26502650
} else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
2651-
pn = ((py_parse_node_struct_t*)pn)->nodes[0];
2651+
// file input; find the first non-newline node
2652+
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
2653+
int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
2654+
for (int i = 0; i < num_nodes; i++) {
2655+
pn = pns->nodes[i];
2656+
if (!(PY_PARSE_NODE_IS_LEAF(pn) && PY_PARSE_NODE_LEAF_KIND(pn) == PY_PARSE_NODE_TOKEN && PY_PARSE_NODE_LEAF_ARG(pn) == PY_TOKEN_NEWLINE)) {
2657+
// not a newline, so this is the first statement; finish search
2658+
break;
2659+
}
2660+
}
2661+
// if we didn't find a non-newline then it's okay to fall through; pn will be a newline and so doc-string test below will fail gracefully
26522662
} else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
2663+
// a list of statements; get the first one
26532664
pn = ((py_parse_node_struct_t*)pn)->nodes[0];
26542665
} else {
26552666
return;

py/grammar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// # single_input is a single interactive statement;
77
// # file_input is a module or sequence of commands read from an input file;
88
// # eval_input is the input for the eval() functions.
9-
// # NB: compound_stmt in single_input is followed by extra NEWLINE! --> not in Micropython
9+
// # NB: compound_stmt in single_input is followed by extra NEWLINE! --> not in Micro Python
1010
// single_input: NEWLINE | simple_stmt | compound_stmt
1111
// file_input: (NEWLINE | stmt)* ENDMARKER
1212
// eval_input: testlist NEWLINE* ENDMARKER

0 commit comments

Comments
 (0)
0