Repository: python/cpython
Pull request: gh-124058: remove _PyCompile_IsNestedScope, roll it into _PyCompile_IsInteractive
Fixes #124058.

<!--
Thanks for your contribution!
Please read this comment in its entirety. It's quite important.

# Pull Request title

It should be in the following format:

```
gh-NNNNN: Summary of the changes made
```

Where: gh-NNNNN refers to the GitHub issue number.

Most PRs will require an issue number. Trivial changes, like fixing a typo, do not need an issue.

# Backport Pull Request title

If this is a backport PR (PR made against branches other than `main`),
please ensure that the PR title is in the following format:

```
[X.Y] <title from the original PR> (GH-NNNN)
```

Where: [X.Y] is the branch name, e.g. [3.6].

GH-NNNN refers to the PR number from `main`.

-->


<!-- gh-issue-number: gh-124058 -->
* Issue: gh-124058
<!-- /gh-issue-number -->


Problem statement:
Remove `_PyCompile_IsNestedScope` roll it into `_PyCompile_IsInteractive`

Currently the `c_interactive` field is used in only one place - to special-case the code generation of interactive statements. We can move this special case from `codegen_stmt_expr` to `_PyCodegen_Body` (where there is the `is_interactive` arg) and then we no longer need `c_interactive`, its accessor function _PyCompile_IsInteractive, and the `_PyCompile_IsNestedScope` function.

<!-- gh-linked-prs -->
### Linked PRs
* gh-124061
<!-- /gh-linked-prs -->

Hints from issue discussion:
The original plan turnout out too complicated. Instead I'll remove `_PyCompile_IsNestedScope` from the compiler's API and make `_PyCompile_IsInteractive` apply its logic.

Commit message: gh-124058: remove _PyCompile_IsNestedScope, roll it into _PyCompile_IsInteractive

Patch to review:
diff --git a/Include/internal/pycore_compile.h b/Include/internal/pycore_compile.h
index 2304d698474355..a62d2ac72566db 100644
--- a/Include/internal/pycore_compile.h
+++ b/Include/internal/pycore_compile.h
@@ -134,7 +134,6 @@ int _PyCompile_ResolveNameop(struct _PyCompiler *c, PyObject *mangled, int scope
                              _PyCompile_optype *optype, Py_ssize_t *arg);
 
 int _PyCompile_IsInteractive(struct _PyCompiler *c);
-int _PyCompile_IsNestedScope(struct _PyCompiler *c);
 int _PyCompile_IsInInlinedComp(struct _PyCompiler *c);
 int _PyCompile_ScopeType(struct _PyCompiler *c);
 int _PyCompile_OptimizationLevel(struct _PyCompiler *c);
diff --git a/Python/codegen.c b/Python/codegen.c
index 5565d3011c465a..f0f90de8922759 100644
--- a/Python/codegen.c
+++ b/Python/codegen.c
@@ -71,7 +71,6 @@ typedef struct _PyCompiler compiler;
 #define SYMTABLE_ENTRY(C) _PyCompile_SymtableEntry(C)
 #define OPTIMIZATION_LEVEL(C) _PyCompile_OptimizationLevel(C)
 #define IS_INTERACTIVE(C) _PyCompile_IsInteractive(C)
-#define IS_NESTED_SCOPE(C) _PyCompile_IsNestedScope(C)
 #define SCOPE_TYPE(C) _PyCompile_ScopeType(C)
 #define QUALNAME(C) _PyCompile_Qualname(C)
 #define METADATA(C) _PyCompile_Metadata(C)
@@ -2823,7 +2822,7 @@ codegen_assert(compiler *c, stmt_ty s)
 static int
 codegen_stmt_expr(compiler *c, location loc, expr_ty value)
 {
-    if (IS_INTERACTIVE(c) && !IS_NESTED_SCOPE(c)) {
+    if (IS_INTERACTIVE(c)) {
         VISIT(c, expr, value);
         ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_PRINT);
         ADDOP(c, NO_LOCATION, POP_TOP);
diff --git a/Python/compile.c b/Python/compile.c
index e1d2c30944d132..5510bd92b7ff4a 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1205,16 +1205,11 @@ _PyCompile_OptimizationLevel(compiler *c)
 
 int
 _PyCompile_IsInteractive(compiler *c)
-{
-    return c->c_interactive;
-}
-
-int
-_PyCompile_IsNestedScope(compiler *c)
 {
     assert(c->c_stack != NULL);
     assert(PyList_CheckExact(c->c_stack));
-    return PyList_GET_SIZE(c->c_stack) > 0;
+    bool is_nested_scope = PyList_GET_SIZE(c->c_stack) > 0;
+    return c->c_interactive && !is_nested_scope;
 }
 
 int


Code review comments:
diff_hunk: @@ -1205,16 +1205,11 @@ _PyCompile_OptimizationLevel(compiler *c)
 
 int
 _PyCompile_IsInteractive(compiler *c)
path: Python/compile.c
text: @user1:
What do you think about renaming this to something like `_PyCompile_IsInteractiveTopLevel` for clarity?