Skip to content

Commit 73dd697

Browse files
committed
Sanitize macros and debug functions in pegen.c
1 parent f32d022 commit 73dd697

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

Parser/pegen.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ _PyPegen_fill_token(Parser *p)
722722
return 0;
723723
}
724724

725+
726+
#if defined(Py_DEBUG)
725727
// Instrumentation to count the effectiveness of memoization.
726728
// The array counts the number of tokens skipped by memoization,
727729
// indexed by type.
@@ -758,6 +760,7 @@ _PyPegen_get_memo_statistics()
758760
}
759761
return ret;
760762
}
763+
#endif
761764

762765
int // bool
763766
_PyPegen_is_memoized(Parser *p, int type, void *pres)
@@ -773,6 +776,7 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
773776

774777
for (Memo *m = t->memo; m != NULL; m = m->next) {
775778
if (m->type == type) {
779+
#if defined(PY_DEBUG)
776780
if (0 <= type && type < NSTATISTICS) {
777781
long count = m->mark - p->mark;
778782
// A memoized negative result counts for one.
@@ -781,6 +785,7 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
781785
}
782786
memo_statistics[type] += count;
783787
}
788+
#endif
784789
p->mark = m->mark;
785790
*(void **)(pres) = m->node;
786791
return 1;
@@ -2271,9 +2276,9 @@ _PyPegen_get_invalid_target(expr_ty e, TARGETS_TYPE targets_type)
22712276
}
22722277

22732278
#define VISIT_CONTAINER(CONTAINER, TYPE) do { \
2274-
Py_ssize_t len = asdl_seq_LEN(CONTAINER->v.TYPE.elts);\
2279+
Py_ssize_t len = asdl_seq_LEN((CONTAINER)->v.TYPE.elts);\
22752280
for (Py_ssize_t i = 0; i < len; i++) {\
2276-
expr_ty other = asdl_seq_GET(CONTAINER->v.TYPE.elts, i);\
2281+
expr_ty other = asdl_seq_GET((CONTAINER)->v.TYPE.elts, i);\
22772282
expr_ty child = _PyPegen_get_invalid_target(other, targets_type);\
22782283
if (child != NULL) {\
22792284
return child;\

Parser/pegen.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ typedef struct {
107107
int is_keyword;
108108
} KeywordOrStarred;
109109

110+
#if defined(Py_DEBUG)
110111
void _PyPegen_clear_memo_statistics(void);
111112
PyObject *_PyPegen_get_memo_statistics(void);
113+
#endif
112114

113115
int _PyPegen_insert_memo(Parser *p, int mark, int type, void *node);
114116
int _PyPegen_update_memo(Parser *p, int mark, int type, void *node);
@@ -150,7 +152,7 @@ RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
150152

151153

152154
#define UNUSED(expr) do { (void)(expr); } while (0)
153-
#define EXTRA_EXPR(head, tail) head->lineno, head->col_offset, tail->end_lineno, tail->end_col_offset, p->arena
155+
#define EXTRA_EXPR(head, tail) head->lineno, (head)->col_offset, (tail)->end_lineno, (tail)->end_col_offset, p->arena
154156
#define EXTRA _start_lineno, _start_col_offset, _end_lineno, _end_col_offset, p->arena
155157
#define RAISE_SYNTAX_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_SyntaxError, msg, ##__VA_ARGS__)
156158
#define RAISE_INDENTATION_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_IndentationError, msg, ##__VA_ARGS__)

Tools/peg_generator/peg_extension/peg_extension.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,27 @@ parse_string(PyObject *self, PyObject *args, PyObject *kwds)
109109
static PyObject *
110110
clear_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
111111
{
112+
#if defined(PY_DEBUG)
112113
_PyPegen_clear_memo_statistics();
114+
#endif
113115
Py_RETURN_NONE;
114116
}
115117

116118
static PyObject *
117119
get_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
118120
{
121+
#if defined(PY_DEBUG)
119122
return _PyPegen_get_memo_statistics();
123+
#else
124+
Py_RETURN_NONE;
125+
#endif
120126
}
121127

122128
// TODO: Write to Python's sys.stdout instead of C's stdout.
123129
static PyObject *
124130
dump_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
125131
{
132+
#if defined(PY_DEBUG)
126133
PyObject *list = _PyPegen_get_memo_statistics();
127134
if (list == NULL) {
128135
return NULL;
@@ -139,6 +146,7 @@ dump_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
139146
}
140147
}
141148
Py_DECREF(list);
149+
#endif
142150
Py_RETURN_NONE;
143151
}
144152

0 commit comments

Comments
 (0)