From 68d8bb94a3facf73cebdc93c64a5acbc110b1e46 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Thu, 11 Jun 2026 15:52:11 +0100 Subject: [PATCH] gh-151112: Fix crash in `compiler_mod()` when entering the current compilation unit fails (GH-151234) (cherry picked from commit 937d89c4d9b7c5fda6730a1127db118d881d13cb) Co-authored-by: Stan Ulbrych --- Python/compile.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Python/compile.c b/Python/compile.c index d79e4b44c2c02c..ab2e532e43e8d1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -853,12 +853,15 @@ compiler_mod(compiler *c, mod_ty mod) { PyCodeObject *co = NULL; int addNone = mod->kind != Expression_kind; + assert(c->u == NULL); if (compiler_codegen(c, mod) < 0) { goto finally; } co = _PyCompile_OptimizeAndAssemble(c, addNone); finally: - _PyCompile_ExitScope(c); + if (c->u != NULL) { + _PyCompile_ExitScope(c); + } return co; }