summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authortyomitch <tyomitch@gmail.com>2019-04-03 05:12:07 (GMT)
committerPablo Galindo <Pablogsal@gmail.com>2019-04-03 05:12:07 (GMT)
commitcb0748d3939c31168ab5d3b80e3677494497d5e3 (patch)
tree8debb59a0158afc0dd194ea161ca2669e7bdcef7 /Modules
parent76b387bf7402863c5e64e3459e2f91ddc3b9d2d3 (diff)
downloadcpython-cb0748d3939c31168ab5d3b80e3677494497d5e3.zip
cpython-cb0748d3939c31168ab5d3b80e3677494497d5e3.tar.gz
cpython-cb0748d3939c31168ab5d3b80e3677494497d5e3.tar.bz2
bpo-36440: include node names in ParserError messages, instead of numeric IDs (GH-12565)
The error messages in the parser module are referring to numeric IDs for the nodes. To improve readability, use the node names when reporting errors.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/parsermodule.c17
1 files changed, 11 insertions, 6 deletions
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index fd330b5..a215c7e 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -24,10 +24,6 @@
* Py_[X]DECREF() and Py_[X]INCREF() macros. The lint annotations
* look like "NOTE(...)".
*
- * To debug parser errors like
- * "parser.ParserError: Expected node type 12, got 333."
- * decode symbol numbers using the automatically-generated files
- * Lib/symbol.h and Include/token.h.
*/
#include "Python.h" /* general Python API */
@@ -666,6 +662,13 @@ validate_node(node *tree)
for (pos = 0; pos < nch; ++pos) {
node *ch = CHILD(tree, pos);
int ch_type = TYPE(ch);
+ if ((ch_type >= NT_OFFSET + _PyParser_Grammar.g_ndfas)
+ || (ISTERMINAL(ch_type) && (ch_type >= N_TOKENS))
+ || (ch_type < 0)
+ ) {
+ PyErr_Format(parser_error, "Unrecognized node type %d.", ch_type);
+ return 0;
+ }
if (ch_type == suite && TYPE(tree) == funcdef) {
/* This is the opposite hack of what we do in parser.c
(search for func_body_suite), except we don't ever
@@ -700,8 +703,10 @@ validate_node(node *tree)
const char *expected_str = _PyParser_Grammar.g_ll.ll_label[a_label].lb_str;
if (ISNONTERMINAL(next_type)) {
- PyErr_Format(parser_error, "Expected node type %d, got %d.",
- next_type, ch_type);
+ PyErr_Format(parser_error, "Expected %s, got %s.",
+ _PyParser_Grammar.g_dfa[next_type - NT_OFFSET].d_name,
+ ISTERMINAL(ch_type) ? _PyParser_TokenNames[ch_type] :
+ _PyParser_Grammar.g_dfa[ch_type - NT_OFFSET].d_name);
}
else if (expected_str != NULL) {
PyErr_Format(parser_error, "Illegal terminal: expected '%s'.",