[Python-checkins] Sanitize macros and debug functions in pegen.c (GH-25291)

pablogsal webhook-mailer at python.org
Thu Apr 8 20:17:36 EDT 2021


https://github.com/python/cpython/commit/58bafe42ab161473ba36c9231c3bf2e64ac8db82
commit: 58bafe42ab161473ba36c9231c3bf2e64ac8db82
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-04-09T01:17:31+01:00
summary:

Sanitize macros and debug functions in pegen.c (GH-25291)

files:
M Parser/pegen.c
M Parser/pegen.h
M Tools/peg_generator/peg_extension/peg_extension.c

diff --git a/Parser/pegen.c b/Parser/pegen.c
index 729b74762a6d2..0aa55cf63abdc 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -725,6 +725,8 @@ _PyPegen_fill_token(Parser *p)
     return 0;
 }
 
+
+#if defined(Py_DEBUG)
 // Instrumentation to count the effectiveness of memoization.
 // The array counts the number of tokens skipped by memoization,
 // indexed by type.
@@ -761,6 +763,7 @@ _PyPegen_get_memo_statistics()
     }
     return ret;
 }
+#endif
 
 int  // bool
 _PyPegen_is_memoized(Parser *p, int type, void *pres)
@@ -776,6 +779,7 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
 
     for (Memo *m = t->memo; m != NULL; m = m->next) {
         if (m->type == type) {
+#if defined(PY_DEBUG)
             if (0 <= type && type < NSTATISTICS) {
                 long count = m->mark - p->mark;
                 // A memoized negative result counts for one.
@@ -784,6 +788,7 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
                 }
                 memo_statistics[type] += count;
             }
+#endif
             p->mark = m->mark;
             *(void **)(pres) = m->node;
             return 1;
@@ -2286,9 +2291,9 @@ _PyPegen_get_invalid_target(expr_ty e, TARGETS_TYPE targets_type)
     }
 
 #define VISIT_CONTAINER(CONTAINER, TYPE) do { \
-        Py_ssize_t len = asdl_seq_LEN(CONTAINER->v.TYPE.elts);\
+        Py_ssize_t len = asdl_seq_LEN((CONTAINER)->v.TYPE.elts);\
         for (Py_ssize_t i = 0; i < len; i++) {\
-            expr_ty other = asdl_seq_GET(CONTAINER->v.TYPE.elts, i);\
+            expr_ty other = asdl_seq_GET((CONTAINER)->v.TYPE.elts, i);\
             expr_ty child = _PyPegen_get_invalid_target(other, targets_type);\
             if (child != NULL) {\
                 return child;\
diff --git a/Parser/pegen.h b/Parser/pegen.h
index af160d6a43d6b..53d8e5221bfcf 100644
--- a/Parser/pegen.h
+++ b/Parser/pegen.h
@@ -107,8 +107,10 @@ typedef struct {
     int is_keyword;
 } KeywordOrStarred;
 
+#if defined(Py_DEBUG)
 void _PyPegen_clear_memo_statistics(void);
 PyObject *_PyPegen_get_memo_statistics(void);
+#endif
 
 int _PyPegen_insert_memo(Parser *p, int mark, int type, void *node);
 int _PyPegen_update_memo(Parser *p, int mark, int type, void *node);
@@ -150,7 +152,7 @@ RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
 
 
 #define UNUSED(expr) do { (void)(expr); } while (0)
-#define EXTRA_EXPR(head, tail) head->lineno, head->col_offset, tail->end_lineno, tail->end_col_offset, p->arena
+#define EXTRA_EXPR(head, tail) head->lineno, (head)->col_offset, (tail)->end_lineno, (tail)->end_col_offset, p->arena
 #define EXTRA _start_lineno, _start_col_offset, _end_lineno, _end_col_offset, p->arena
 #define RAISE_SYNTAX_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_SyntaxError, msg, ##__VA_ARGS__)
 #define RAISE_INDENTATION_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_IndentationError, msg, ##__VA_ARGS__)
diff --git a/Tools/peg_generator/peg_extension/peg_extension.c b/Tools/peg_generator/peg_extension/peg_extension.c
index 94e729e56d9b9..bb4c1b0178c91 100644
--- a/Tools/peg_generator/peg_extension/peg_extension.c
+++ b/Tools/peg_generator/peg_extension/peg_extension.c
@@ -109,20 +109,27 @@ parse_string(PyObject *self, PyObject *args, PyObject *kwds)
 static PyObject *
 clear_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
 {
+#if defined(PY_DEBUG)
     _PyPegen_clear_memo_statistics();
+#endif
     Py_RETURN_NONE;
 }
 
 static PyObject *
 get_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
 {
+#if defined(PY_DEBUG)
     return _PyPegen_get_memo_statistics();
+#else
+    Py_RETURN_NONE;
+#endif
 }
 
 // TODO: Write to Python's sys.stdout instead of C's stdout.
 static PyObject *
 dump_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
 {
+#if defined(PY_DEBUG)
     PyObject *list = _PyPegen_get_memo_statistics();
     if (list == NULL) {
         return NULL;
@@ -139,6 +146,7 @@ dump_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
         }
     }
     Py_DECREF(list);
+#endif
     Py_RETURN_NONE;
 }
 



More information about the Python-checkins mailing list