]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-123881: make compiler add the .generic_base base class without constructing AST...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Tue, 10 Sep 2024 16:16:00 +0000 (17:16 +0100)
committerGitHub <noreply@github.com>
Tue, 10 Sep 2024 16:16:00 +0000 (16:16 +0000)
Include/internal/pycore_compile.h
Python/codegen.c
Python/compile.c

index 7b02dc17ef0d62ecdf033aa7108551a9b5d3f76f..9219d4d735a5764699f4bf230a44a8544f4cc303 100644 (file)
@@ -136,7 +136,6 @@ int _PyCompile_IsNestedScope(struct _PyCompiler *c);
 int _PyCompile_IsInInlinedComp(struct _PyCompiler *c);
 int _PyCompile_ScopeType(struct _PyCompiler *c);
 int _PyCompile_OptimizationLevel(struct _PyCompiler *c);
-PyArena *_PyCompile_Arena(struct _PyCompiler *c);
 int _PyCompile_LookupArg(struct _PyCompiler *c, PyCodeObject *co, PyObject *name);
 PyObject *_PyCompile_Qualname(struct _PyCompiler *c);
 _PyCompile_CodeUnitMetadata *_PyCompile_Metadata(struct _PyCompiler *c);
index 51d5b2a43d275ef2ca7ce5094069fd841203b191..ed06724e95d05b2c4878c8697b6da83931dfb433 100644 (file)
@@ -76,7 +76,6 @@ typedef struct _PyCompiler compiler;
 #define SCOPE_TYPE(C) _PyCompile_ScopeType(C)
 #define QUALNAME(C) _PyCompile_Qualname(C)
 #define METADATA(C) _PyCompile_Metadata(C)
-#define ARENA(C) _PyCompile_Arena(C)
 
 typedef _PyInstruction instruction;
 typedef _PyInstructionSequence instr_sequence;
@@ -209,6 +208,11 @@ static int codegen_call_simple_kw_helper(compiler *c,
                                          location loc,
                                          asdl_keyword_seq *keywords,
                                          Py_ssize_t nkwelts);
+static int codegen_call_helper_impl(compiler *c, location loc,
+                                    int n, /* Args already pushed */
+                                    asdl_expr_seq *args,
+                                    PyObject *injected_arg,
+                                    asdl_keyword_seq *keywords);
 static int codegen_call_helper(compiler *c, location loc,
                                int n, asdl_expr_seq *args,
                                asdl_keyword_seq *keywords);
@@ -1549,28 +1553,10 @@ codegen_class(compiler *c, stmt_ty s)
         ADDOP_I_IN_SCOPE(c, loc, CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC);
         RETURN_IF_ERROR_IN_SCOPE(c, codegen_nameop(c, loc, &_Py_STR(generic_base), Store));
 
-        Py_ssize_t original_len = asdl_seq_LEN(s->v.ClassDef.bases);
-        asdl_expr_seq *bases = _Py_asdl_expr_seq_new(
-            original_len + 1, ARENA(c));
-        if (bases == NULL) {
-            _PyCompile_ExitScope(c);
-            return ERROR;
-        }
-        for (Py_ssize_t i = 0; i < original_len; i++) {
-            asdl_seq_SET(bases, i, asdl_seq_GET(s->v.ClassDef.bases, i));
-        }
-        expr_ty name_node = _PyAST_Name(
-            &_Py_STR(generic_base), Load,
-            loc.lineno, loc.col_offset, loc.end_lineno, loc.end_col_offset, ARENA(c)
-        );
-        if (name_node == NULL) {
-            _PyCompile_ExitScope(c);
-            return ERROR;
-        }
-        asdl_seq_SET(bases, original_len, name_node);
-        RETURN_IF_ERROR_IN_SCOPE(c, codegen_call_helper(c, loc, 2,
-                                                        bases,
-                                                        s->v.ClassDef.keywords));
+        RETURN_IF_ERROR_IN_SCOPE(c, codegen_call_helper_impl(c, loc, 2,
+                                                             s->v.ClassDef.bases,
+                                                             &_Py_STR(generic_base),
+                                                             s->v.ClassDef.keywords));
 
         PyCodeObject *co = _PyCompile_OptimizeAndAssemble(c, 0);
 
@@ -3187,19 +3173,18 @@ codegen_boolop(compiler *c, expr_ty e)
 }
 
 static int
-starunpack_helper(compiler *c, location loc,
-                  asdl_expr_seq *elts, int pushed,
-                  int build, int add, int extend, int tuple)
+starunpack_helper_impl(compiler *c, location loc,
+                       asdl_expr_seq *elts, PyObject *injected_arg, int pushed,
+                       int build, int add, int extend, int tuple)
 {
     Py_ssize_t n = asdl_seq_LEN(elts);
-    if (n > 2 && are_all_items_const(elts, 0, n)) {
+    if (!injected_arg && n > 2 && are_all_items_const(elts, 0, n)) {
         PyObject *folded = PyTuple_New(n);
         if (folded == NULL) {
             return ERROR;
         }
-        PyObject *val;
         for (Py_ssize_t i = 0; i < n; i++) {
-            val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
+            PyObject *val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
             PyTuple_SET_ITEM(folded, i, Py_NewRef(val));
         }
         if (tuple && !pushed) {
@@ -3221,7 +3206,7 @@ starunpack_helper(compiler *c, location loc,
         return SUCCESS;
     }
 
-    int big = n+pushed > STACK_USE_GUIDELINE;
+    int big = n + pushed + (injected_arg ? 1 : 0) > STACK_USE_GUIDELINE;
     int seen_star = 0;
     for (Py_ssize_t i = 0; i < n; i++) {
         expr_ty elt = asdl_seq_GET(elts, i);
@@ -3235,6 +3220,10 @@ starunpack_helper(compiler *c, location loc,
             expr_ty elt = asdl_seq_GET(elts, i);
             VISIT(c, expr, elt);
         }
+        if (injected_arg) {
+            RETURN_IF_ERROR(codegen_nameop(c, loc, injected_arg, Load));
+            n++;
+        }
         if (tuple) {
             ADDOP_I(c, loc, BUILD_TUPLE, n+pushed);
         } else {
@@ -3265,12 +3254,25 @@ starunpack_helper(compiler *c, location loc,
         }
     }
     assert(sequence_built);
+    if (injected_arg) {
+        RETURN_IF_ERROR(codegen_nameop(c, loc, injected_arg, Load));
+        ADDOP_I(c, loc, add, 1);
+    }
     if (tuple) {
         ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_LIST_TO_TUPLE);
     }
     return SUCCESS;
 }
 
+static int
+starunpack_helper(compiler *c, location loc,
+                  asdl_expr_seq *elts, int pushed,
+                  int build, int add, int extend, int tuple)
+{
+    return starunpack_helper_impl(c, loc, elts, NULL, pushed,
+                                  build, add, extend, tuple);
+}
+
 static int
 unpack_helper(compiler *c, location loc, asdl_expr_seq *elts)
 {
@@ -3973,13 +3975,13 @@ codegen_call_simple_kw_helper(compiler *c, location loc,
     return SUCCESS;
 }
 
-
 /* shared code between codegen_call and codegen_class */
 static int
-codegen_call_helper(compiler *c, location loc,
-                    int n, /* Args already pushed */
-                    asdl_expr_seq *args,
-                    asdl_keyword_seq *keywords)
+codegen_call_helper_impl(compiler *c, location loc,
+                         int n, /* Args already pushed */
+                         asdl_expr_seq *args,
+                         PyObject *injected_arg,
+                         asdl_keyword_seq *keywords)
 {
     Py_ssize_t i, nseen, nelts, nkwelts;
 
@@ -4010,6 +4012,10 @@ codegen_call_helper(compiler *c, location loc,
         assert(elt->kind != Starred_kind);
         VISIT(c, expr, elt);
     }
+    if (injected_arg) {
+        RETURN_IF_ERROR(codegen_nameop(c, loc, injected_arg, Load));
+        nelts++;
+    }
     if (nkwelts) {
         VISIT_SEQ(c, keyword, keywords);
         RETURN_IF_ERROR(
@@ -4024,12 +4030,12 @@ codegen_call_helper(compiler *c, location loc,
 ex_call:
 
     /* Do positional arguments. */
-    if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
+    if (n == 0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
         VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
     }
     else {
-        RETURN_IF_ERROR(starunpack_helper(c, loc, args, n, BUILD_LIST,
-                                          LIST_APPEND, LIST_EXTEND, 1));
+        RETURN_IF_ERROR(starunpack_helper_impl(c, loc, args, injected_arg, n,
+                                               BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1));
     }
     /* Then keyword arguments */
     if (nkwelts) {
@@ -4074,6 +4080,14 @@ ex_call:
     return SUCCESS;
 }
 
+static int
+codegen_call_helper(compiler *c, location loc,
+                    int n, /* Args already pushed */
+                    asdl_expr_seq *args,
+                    asdl_keyword_seq *keywords)
+{
+    return codegen_call_helper_impl(c, loc, n, args, NULL, keywords);
+}
 
 /* List and set comprehensions and generator expressions work by creating a
   nested function to perform the actual iteration. This means that the
index 89d75b504cc53f3f9593e8e7f2344781112ff40e..72399c758d51e86279e78a315c3daf557b1ef55a 100644 (file)
@@ -88,7 +88,6 @@ typedef struct _PyCompiler {
                                     including names tuple */
     struct compiler_unit *u;     /* compiler state for current block */
     PyObject *c_stack;           /* Python list holding compiler_unit ptrs */
-    PyArena *c_arena;            /* pointer to memory allocation arena */
 
     bool c_save_nested_seqs;     /* if true, construct recursive instruction sequences
                                   * (including instructions for nested code objects)
@@ -112,7 +111,6 @@ compiler_setup(compiler *c, mod_ty mod, PyObject *filename,
     }
 
     c->c_filename = Py_NewRef(filename);
-    c->c_arena = arena;
     if (!_PyFuture_FromAST(mod, filename, &c->c_future)) {
         return ERROR;
     }
@@ -1244,12 +1242,6 @@ _PyCompile_Metadata(compiler *c)
     return &c->u->u_metadata;
 }
 
-PyArena *
-_PyCompile_Arena(compiler *c)
-{
-    return c->c_arena;
-}
-
 #ifndef NDEBUG
 int
 _PyCompile_IsTopLevelAwait(compiler *c)