]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105148: make _PyASTOptimizeState internal to ast_opt.c (#105149)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Wed, 31 May 2023 19:21:46 +0000 (20:21 +0100)
committerGitHub <noreply@github.com>
Wed, 31 May 2023 19:21:46 +0000 (20:21 +0100)
Include/internal/pycore_compile.h
Misc/NEWS.d/next/Core and Builtins/2023-05-31-16-22-29.gh-issue-105148.MOlb1d.rst [new file with mode: 0644]
Python/ast_opt.c
Python/compile.c
Python/traceback.c

index 80a637e5bf9aed286c57f50dbe38f57d6d5e31b9..e6481734a675c63235fb10d381c4bf182e0617cb 100644 (file)
@@ -21,18 +21,11 @@ PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
 
 static const _PyCompilerSrcLocation NO_LOCATION = {-1, -1, -1, -1};
 
-typedef struct {
-    int optimize;
-    int ff_features;
-
-    int recursion_depth;            /* current recursion depth */
-    int recursion_limit;            /* recursion limit */
-} _PyASTOptimizeState;
-
 extern int _PyAST_Optimize(
     struct _mod *,
     struct _arena *arena,
-    _PyASTOptimizeState *state);
+    int optimize,
+    int ff_features);
 
 typedef struct {
     int h_offset;
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-31-16-22-29.gh-issue-105148.MOlb1d.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-31-16-22-29.gh-issue-105148.MOlb1d.rst
new file mode 100644 (file)
index 0000000..4dcdcdf
--- /dev/null
@@ -0,0 +1,3 @@
+Make ``_PyASTOptimizeState`` internal to ast_opt.c. Make ``_PyAST_Optimize``
+take two integers instead of a pointer to this struct. This avoids the need
+to include pycore_compile.h in ast_opt.c.
index 274bd134e1435be6bc28d2ad09aafdab3d9b410a..276e910089a277d286a367c04fd1fc3759e71fa5 100644 (file)
@@ -1,12 +1,20 @@
 /* AST Optimizer */
 #include "Python.h"
 #include "pycore_ast.h"           // _PyAST_GetDocString()
-#include "pycore_compile.h"       // _PyASTOptimizeState
 #include "pycore_long.h"           // _PyLong
 #include "pycore_pystate.h"       // _PyThreadState_GET()
 #include "pycore_format.h"        // F_LJUST
 
 
+typedef struct {
+    int optimize;
+    int ff_features;
+
+    int recursion_depth;            /* current recursion depth */
+    int recursion_limit;            /* recursion limit */
+} _PyASTOptimizeState;
+
+
 static int
 make_const(expr_ty node, PyObject *val, PyArena *arena)
 {
@@ -1106,11 +1114,15 @@ astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
 #define COMPILER_STACK_FRAME_SCALE 3
 
 int
-_PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
+_PyAST_Optimize(mod_ty mod, PyArena *arena, int optimize, int ff_features)
 {
     PyThreadState *tstate;
     int starting_recursion_depth;
 
+    _PyASTOptimizeState state;
+    state.optimize = optimize;
+    state.ff_features = ff_features;
+
     /* Setup recursion depth check counters */
     tstate = _PyThreadState_GET();
     if (!tstate) {
@@ -1119,17 +1131,17 @@ _PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
     /* Be careful here to prevent overflow. */
     int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
     starting_recursion_depth = recursion_depth * COMPILER_STACK_FRAME_SCALE;
-    state->recursion_depth = starting_recursion_depth;
-    state->recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
+    state.recursion_depth = starting_recursion_depth;
+    state.recursion_limit = C_RECURSION_LIMIT * COMPILER_STACK_FRAME_SCALE;
 
-    int ret = astfold_mod(mod, arena, state);
+    int ret = astfold_mod(mod, arena, &state);
     assert(ret || PyErr_Occurred());
 
     /* Check that the recursion depth counting balanced correctly */
-    if (ret && state->recursion_depth != starting_recursion_depth) {
+    if (ret && state.recursion_depth != starting_recursion_depth) {
         PyErr_Format(PyExc_SystemError,
             "AST optimizer recursion depth mismatch (before=%d, after=%d)",
-            starting_recursion_depth, state->recursion_depth);
+            starting_recursion_depth, state.recursion_depth);
         return 0;
     }
 
index f2314ae11c417beebe8eb81e7c71edd44a2fde9c..8f20e39315fb3f54b096605ea0449affcae15100 100644 (file)
@@ -534,11 +534,7 @@ compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
     c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
     c->c_nestlevel = 0;
 
-    _PyASTOptimizeState state;
-    state.optimize = c->c_optimize;
-    state.ff_features = merged;
-
-    if (!_PyAST_Optimize(mod, arena, &state)) {
+    if (!_PyAST_Optimize(mod, arena, c->c_optimize, merged)) {
         return ERROR;
     }
     c->c_st = _PySymtable_Build(mod, filename, &c->c_future);
index b2479542047308cba71aab004716e0aa1631d81b..a58df04b3460101388d221fbe74e635eed02269d 100644 (file)
@@ -675,16 +675,12 @@ extract_anchors_from_line(PyObject *filename, PyObject *line,
 
     PyCompilerFlags flags = _PyCompilerFlags_INIT;
 
-    _PyASTOptimizeState state;
-    state.optimize = _Py_GetConfig()->optimization_level;
-    state.ff_features = 0;
-
     mod_ty module = _PyParser_ASTFromString(segment_str, filename, Py_file_input,
                                             &flags, arena);
     if (!module) {
         goto done;
     }
-    if (!_PyAST_Optimize(module, arena, &state)) {
+    if (!_PyAST_Optimize(module, arena, _Py_GetConfig()->optimization_level, 0)) {
         goto done;
     }