build: finalize embedded python before exit#1707
build: finalize embedded python before exit#1707cpunion wants to merge 3 commits intogoplus:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the stability and correctness of Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly adds a call to Py_Finalize() for programs that use the embedded Python interpreter, ensuring proper cleanup before exit. The implementation is straightforward and includes a test to verify the change. I've added one comment suggesting a refactoring to improve the maintainability of an internal function that is accumulating a large number of parameters.
| // initialization hooks (Python, runtime, package init), and finally calls | ||
| // main.main before returning 0. | ||
| func defineEntryFunction(ctx *context, pkg llssa.Package, argcVar, argvVar llssa.Global, argvType llssa.Type, runtimeStub, mainInit, mainMain llssa.Function, pyInit, rtInit, abiInit llssa.Function) llssa.Function { | ||
| func defineEntryFunction(ctx *context, pkg llssa.Package, argcVar, argvVar llssa.Global, argvType llssa.Type, runtimeStub, mainInit, mainMain llssa.Function, pyInit, pyFinalize, rtInit, abiInit llssa.Function) llssa.Function { |
There was a problem hiding this comment.
This function now has 11 parameters, which can make it hard to read and maintain. Consider grouping the initialization and finalization functions into a struct to improve clarity.
For example:
type entryHooks struct {
runtimeStub llssa.Function
mainInit llssa.Function
mainMain llssa.Function
pyInit llssa.Function
pyFinalize llssa.Function
rtInit llssa.Function
abiInit llssa.Function
}
func defineEntryFunction(ctx *context, pkg llssa.Package, argcVar, argvVar llssa.Global, argvType llssa.Type, hooks *entryHooks) llssa.Function {
// ...
if hooks.pyInit != nil {
b.Call(hooks.pyInit.Expr)
}
// ...
}This would make the function signature more manageable and easier to extend in the future.
| // The entry stores argc/argv, optionally disables stdio buffering, runs | ||
| // initialization hooks (Python, runtime, package init), and finally calls | ||
| // main.main before returning 0. |
There was a problem hiding this comment.
The doc comment no longer reflects the full behavior — the function now also calls Py_Finalize() after main.main before returning 0.
| // The entry stores argc/argv, optionally disables stdio buffering, runs | |
| // initialization hooks (Python, runtime, package init), and finally calls | |
| // main.main before returning 0. | |
| // The entry stores argc/argv, optionally disables stdio buffering, runs | |
| // initialization hooks (Python, runtime, package init), calls main.main, | |
| // finalizes embedded Python if initialized, and returns 0. |
| checks := []string{ | ||
| "define i32 @main(", | ||
| "call void @Py_Initialize()", | ||
| "call void @Py_Finalize()", | ||
| "call void @\"example.com/foo.init\"()", | ||
| "define weak void @_start()", | ||
| } |
There was a problem hiding this comment.
nit: The test checks that each substring is present in the IR, but doesn't verify call ordering. An accidental swap (e.g., Py_Finalize before main.main) would still pass. Consider adding an index-based ordering check:
initIdx := strings.Index(ir, "call void @Py_Initialize()")
mainIdx := strings.Index(ir, `call void @"example.com/foo.main"()`)
finalIdx := strings.Index(ir, "call void @Py_Finalize()")
if !(initIdx < mainIdx && mainIdx < finalIdx) {
t.Fatalf("incorrect call ordering: init@%d, main@%d, finalize@%d", initIdx, mainIdx, finalIdx)
}|
Clean, well-scoped change. The |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1707 +/- ##
==========================================
- Coverage 93.15% 93.05% -0.11%
==========================================
Files 48 48
Lines 13331 13331
==========================================
- Hits 12419 12405 -14
- Misses 725 738 +13
- Partials 187 188 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
Fixes #487