Skip to content

Commit f990bb8

Browse files
authored
gh-105148: make _PyASTOptimizeState internal to ast_opt.c (#105149)
1 parent dd29ae2 commit f990bb8

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

Include/internal/pycore_compile.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,11 @@ PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
2121

2222
static const _PyCompilerSrcLocation NO_LOCATION = {-1, -1, -1, -1};
2323

24-
typedef struct {
25-
int optimize;
26-
int ff_features;
27-
28-
int recursion_depth; /* current recursion depth */
29-
int recursion_limit; /* recursion limit */
30-
} _PyASTOptimizeState;
31-
3224
extern int _PyAST_Optimize(
3325
struct _mod *,
3426
struct _arena *arena,
35-
_PyASTOptimizeState *state);
27+
int optimize,
28+
int ff_features);
3629

3730
typedef struct {
3831
int h_offset;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Make ``_PyASTOptimizeState`` internal to ast_opt.c. Make ``_PyAST_Optimize``
2+
take two integers instead of a pointer to this struct. This avoids the need
3+
to include pycore_compile.h in ast_opt.c.

Python/ast_opt.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
/* AST Optimizer */
22
#include "Python.h"
33
#include "pycore_ast.h" // _PyAST_GetDocString()
4-
#include "pycore_compile.h" // _PyASTOptimizeState
54
#include "pycore_long.h" // _PyLong
65
#include "pycore_pystate.h" // _PyThreadState_GET()
76
#include "pycore_format.h" // F_LJUST
87

98

9+
typedef struct {
10+
int optimize;
11+
int ff_features;
12+
13+
int recursion_depth; /* current recursion depth */
14+
int recursion_limit; /* recursion limit */
15+
} _PyASTOptimizeState;
16+
17+
1018
static int
1119
make_const(expr_ty node, PyObject *val, PyArena *arena)
1220
{
@@ -1106,11 +1114,15 @@ astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
11061114
#define COMPILER_STACK_FRAME_SCALE 3
11071115

11081116
int
1109-
_PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
1117+
_PyAST_Optimize(mod_ty mod, PyArena *arena, int optimize, int ff_features)
11101118
{
11111119
PyThreadState *tstate;
11121120
int starting_recursion_depth;
11131121

1122+
_PyASTOptimizeState state;
1123+
state.optimize = optimize;
1124+
state.ff_features = ff_features;
1125+
11141126
/* Setup recursion depth check counters */
11151127
tstate = _PyThreadState_GET();
11161128
if (!tstate) {
@@ -1119,17 +1131,17 @@ _PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
11191131
/* Be careful here to prevent overflow. */
11201132
int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
11211133
starting_recursion_depth = recursion_depth * COMPILER_STACK_FRAME_SCALE;
1122-
state->recursion_depth = starting_recursion_depth;
1123-
state->recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
1134+
state.recursion_depth = starting_recursion_depth;
1135+
state.recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
11241136

1125-
int ret = astfold_mod(mod, arena, state);
1137+
int ret = astfold_mod(mod, arena, &state);
11261138
assert(ret || PyErr_Occurred());
11271139

11281140
/* Check that the recursion depth counting balanced correctly */
1129-
if (ret && state->recursion_depth != starting_recursion_depth) {
1141+
if (ret && state.recursion_depth != starting_recursion_depth) {
11301142
PyErr_Format(PyExc_SystemError,
11311143
"AST optimizer recursion depth mismatch (before=%d, after=%d)",
1132-
starting_recursion_depth, state->recursion_depth);
1144+
starting_recursion_depth, state.recursion_depth);
11331145
return 0;
11341146
}
11351147

Python/compile.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,7 @@ compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
534534
c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
535535
c->c_nestlevel = 0;
536536

537-
_PyASTOptimizeState state;
538-
state.optimize = c->c_optimize;
539-
state.ff_features = merged;
540-
541-
if (!_PyAST_Optimize(mod, arena, &state)) {
537+
if (!_PyAST_Optimize(mod, arena, c->c_optimize, merged)) {
542538
return ERROR;
543539
}
544540
c->c_st = _PySymtable_Build(mod, filename, &c->c_future);

Python/traceback.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,16 +675,12 @@ extract_anchors_from_line(PyObject *filename, PyObject *line,
675675

676676
PyCompilerFlags flags = _PyCompilerFlags_INIT;
677677

678-
_PyASTOptimizeState state;
679-
state.optimize = _Py_GetConfig()->optimization_level;
680-
state.ff_features = 0;
681-
682678
mod_ty module = _PyParser_ASTFromString(segment_str, filename, Py_file_input,
683679
&flags, arena);
684680
if (!module) {
685681
goto done;
686682
}
687-
if (!_PyAST_Optimize(module, arena, &state)) {
683+
if (!_PyAST_Optimize(module, arena, _Py_GetConfig()->optimization_level, 0)) {
688684
goto done;
689685
}
690686

0 commit comments

Comments
 (0)