From 669d7a0cc689c976194b3449666b7ac388d8e0cc Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 8 Oct 2017 22:17:34 -0700 Subject: [PATCH 1/3] bpo-31698: add REQ_NAME macro @1st1 requested this in https://github.com/python/cpython/pull/1669#discussion_r142812302. --- Include/node.h | 5 +++++ Python/ast.c | 14 +++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Include/node.h b/Include/node.h index 654ad858230145..9e41d4705ca8af 100644 --- a/Include/node.h +++ b/Include/node.h @@ -35,6 +35,11 @@ PyAPI_FUNC(Py_ssize_t) _PyNode_SizeOf(node *n); /* Assert that the type of a node is what we expect */ #define REQ(n, type) assert(TYPE(n) == (type)) +/* Assert that a NAME node contains the expected name */ +#define REQ_NAME(node, name) do { \ + REQ((node), NAME); \ + assert(strcmp(STR((node)), (name)) == 0); \ +} while(0) PyAPI_FUNC(void) PyNode_ListTree(node *); diff --git a/Python/ast.c b/Python/ast.c index 6989965efabbad..0d0836e7797dc3 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -1622,8 +1622,7 @@ ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_se { /* async_funcdef: 'async' funcdef */ REQ(n, async_funcdef); - REQ(CHILD(n, 0), NAME); - assert(strcmp(STR(CHILD(n, 0)), "async") == 0); + REQ_NAME(CHILD(n, 0), "async"); REQ(CHILD(n, 1), funcdef); return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq, @@ -1644,8 +1643,7 @@ ast_for_async_stmt(struct compiling *c, const node *n) { /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */ REQ(n, async_stmt); - REQ(CHILD(n, 0), NAME); - assert(strcmp(STR(CHILD(n, 0)), "async") == 0); + REQ_NAME(CHILD(n, 0), "async"); switch (TYPE(CHILD(n, 1))) { case funcdef: @@ -1763,8 +1761,7 @@ count_comp_fors(struct compiling *c, const node *n) n_fors++; REQ(n, comp_for); if (NCH(n) == 2) { - REQ(CHILD(n, 0), NAME); - assert(strcmp(STR(CHILD(n, 0)), "async") == 0); + REQ_NAME(CHILD(n, 0), "async"); n = CHILD(n, 1); } else if (NCH(n) == 1) { @@ -1849,8 +1846,7 @@ ast_for_comprehension(struct compiling *c, const node *n) if (NCH(n) == 2) { is_async = 1; - REQ(CHILD(n, 0), NAME); - assert(strcmp(STR(CHILD(n, 0)), "async") == 0); + REQ_NAME(CHILD(n, 0), "async"); sync_n = CHILD(n, 1); } else { @@ -2592,7 +2588,7 @@ ast_for_expr(struct compiling *c, const node *n) if (!strcmp(STR(CHILD(n, 1)), "and")) return BoolOp(And, seq, LINENO(n), n->n_col_offset, c->c_arena); - assert(!strcmp(STR(CHILD(n, 1)), "or")); + REQ_NAME(CHILD(n, 1), "or"); return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena); case not_test: if (NCH(n) == 1) { From 5204b31579a769bd9d221f9a0a0c00fde0dacf14 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 8 Oct 2017 22:21:36 -0700 Subject: [PATCH 2/3] add news entry --- .../Core and Builtins/2017-10-08-22-21-27.bpo-31698.ekxKQc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-10-08-22-21-27.bpo-31698.ekxKQc.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-10-08-22-21-27.bpo-31698.ekxKQc.rst b/Misc/NEWS.d/next/Core and Builtins/2017-10-08-22-21-27.bpo-31698.ekxKQc.rst new file mode 100644 index 00000000000000..b9c509b58f85af --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-10-08-22-21-27.bpo-31698.ekxKQc.rst @@ -0,0 +1 @@ +Add a ``REQ_NAME`` macro to `Include/node.h`. From 89ff927add9032c10498fb137be483f7aeb1061e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 8 Oct 2017 22:27:48 -0700 Subject: [PATCH 3/3] run make patchcheck --- Include/node.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Include/node.h b/Include/node.h index 9e41d4705ca8af..414e55112e4496 100644 --- a/Include/node.h +++ b/Include/node.h @@ -8,12 +8,12 @@ extern "C" { #endif typedef struct _node { - short n_type; - char *n_str; - int n_lineno; - int n_col_offset; - int n_nchildren; - struct _node *n_child; + short n_type; + char *n_str; + int n_lineno; + int n_col_offset; + int n_nchildren; + struct _node *n_child; } node; PyAPI_FUNC(node *) PyNode_New(int type); @@ -25,12 +25,12 @@ PyAPI_FUNC(Py_ssize_t) _PyNode_SizeOf(node *n); #endif /* Node access functions */ -#define NCH(n) ((n)->n_nchildren) +#define NCH(n) ((n)->n_nchildren) -#define CHILD(n, i) (&(n)->n_child[i]) -#define RCHILD(n, i) (CHILD(n, NCH(n) + i)) -#define TYPE(n) ((n)->n_type) -#define STR(n) ((n)->n_str) +#define CHILD(n, i) (&(n)->n_child[i]) +#define RCHILD(n, i) (CHILD(n, NCH(n) + i)) +#define TYPE(n) ((n)->n_type) +#define STR(n) ((n)->n_str) #define LINENO(n) ((n)->n_lineno) /* Assert that the type of a node is what we expect */