Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Dec 16, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Unhandled-exception messages now include full traceback when available and always end with a newline
    • Exception type output improved to show module and qualified type name where appropriate
    • Exception value formatting more robust (surrogate-safe fallback) and ensured colon separates type and value
    • Error output flushed to stderr for more reliable logging and display

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 16, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Updated the sys module's unraisable-exception hook: prints traceback via exc_traceback using Python's traceback.print_tb, improves exception type and value formatting (module name, qualname, surrogate-safe value), adds colon/newline formatting and stderr flush, and adds KwArgs to imported function types.

Changes

Cohort / File(s) Summary
Unraisable exception hook & imports
crates/vm/src/stdlib/sys.rs
Added KwArgs to function imports; implemented real traceback printing using exc_traceback and traceback.print_tb; enhanced exception type printing (module name filtering, qualname fallback), improved exception value formatting with surrogate-safe fallback, ensured colon and trailing newline in message, and flushed stderr after printing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Inspect exc_traceback None handling and call to traceback.print_tb.
  • Verify module name filtering logic for builtins and __main__.
  • Confirm qualname fallback and surrogate-safe str() fallback behave correctly.
  • Ensure stderr flush occurs after all writes.

Possibly related PRs

  • Unraiseable traceback #6449 — Similar modifications to crates/vm/src/stdlib/sys.rs adding KwArgs and improving unraisable traceback printing and formatting.

Poem

🐰 I hopped through code, found traces to show,
A traceback now prints where silent errors go.
Module names, qualnames, each line in its place,
A flushed little message, tidy and ace.
Hooray — the hook sings, and I twitch my nose.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly corresponds to the main change: implementing unraisable exception traceback printing functionality in the sys module.

📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c805f33 and ad8e5b8.

📒 Files selected for processing (1)
  • crates/vm/src/stdlib/sys.rs (2 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@youknowone youknowone marked this pull request as ready for review December 16, 2025 14:46
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between aef4de4 and c805f33.

📒 Files selected for processing (1)
  • crates/vm/src/stdlib/sys.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style by running cargo fmt to format Rust code
Always run clippy to lint Rust code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

Files:

  • crates/vm/src/stdlib/sys.rs
🧠 Learnings (1)
📓 Common learnings
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-11-29T12:17:28.606Z
Learning: Applies to Lib/test/**/*.py : When modifying test files in `Lib/test/`, use `unittest.skip("TODO: RustPython <reason>")` or `unittest.expectedFailure` with `# TODO: RUSTPYTHON <reason>` comment to mark unsupported features
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run tests under miri
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Check Rust code with clippy
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Check the WASM package and demo
🔇 Additional comments (4)
crates/vm/src/stdlib/sys.rs (4)

18-18: LGTM!

The KwArgs import is correctly added and used for the traceback printing functionality.


703-711: LGTM!

The early return when exc_type is None is correct, and the assertion ensures type safety for subsequent operations.


724-730: LGTM!

The qualname printing with fallback to the type name is correctly implemented.


732-748: LGTM!

The exception value formatting with proper separator, error handling for str() failures, and stderr flushing are all well implemented.

Comment on lines +691 to +701
// Print traceback (using actual exc_traceback, not current stack)
if !vm.is_none(&unraisable.exc_traceback) {
let tb_module = vm.import("traceback", 0)?;
let print_tb = tb_module.get_attr("print_tb", vm)?;
let stderr_obj = super::get_stderr(vm)?;
let kwargs: KwArgs = [("file".to_string(), stderr_obj)].into_iter().collect();
let _ = print_tb.call(
FuncArgs::new(vec![unraisable.exc_traceback.clone()], kwargs),
vm,
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Error propagation may suppress unraisable exception output.

The ? operator on vm.import() and get_attr() will cause the entire _unraisablehook to return early on failure, potentially losing the exception type/value output that follows. Consider catching these errors to ensure the hook remains resilient:

         // Print traceback (using actual exc_traceback, not current stack)
         if !vm.is_none(&unraisable.exc_traceback) {
-            let tb_module = vm.import("traceback", 0)?;
-            let print_tb = tb_module.get_attr("print_tb", vm)?;
-            let stderr_obj = super::get_stderr(vm)?;
-            let kwargs: KwArgs = [("file".to_string(), stderr_obj)].into_iter().collect();
-            let _ = print_tb.call(
-                FuncArgs::new(vec![unraisable.exc_traceback.clone()], kwargs),
-                vm,
-            );
+            if let Ok(tb_module) = vm.import("traceback", 0)
+                && let Ok(print_tb) = tb_module.get_attr("print_tb", vm)
+                && let Ok(stderr_obj) = super::get_stderr(vm)
+            {
+                let kwargs: KwArgs = [("file".to_string(), stderr_obj)].into_iter().collect();
+                let _ = print_tb.call(
+                    FuncArgs::new(vec![unraisable.exc_traceback.clone()], kwargs),
+                    vm,
+                );
+            }
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Print traceback (using actual exc_traceback, not current stack)
if !vm.is_none(&unraisable.exc_traceback) {
let tb_module = vm.import("traceback", 0)?;
let print_tb = tb_module.get_attr("print_tb", vm)?;
let stderr_obj = super::get_stderr(vm)?;
let kwargs: KwArgs = [("file".to_string(), stderr_obj)].into_iter().collect();
let _ = print_tb.call(
FuncArgs::new(vec![unraisable.exc_traceback.clone()], kwargs),
vm,
);
}
// Print traceback (using actual exc_traceback, not current stack)
if !vm.is_none(&unraisable.exc_traceback) {
if let Ok(tb_module) = vm.import("traceback", 0)
&& let Ok(print_tb) = tb_module.get_attr("print_tb", vm)
&& let Ok(stderr_obj) = super::get_stderr(vm)
{
let kwargs: KwArgs = [("file".to_string(), stderr_obj)].into_iter().collect();
let _ = print_tb.call(
FuncArgs::new(vec![unraisable.exc_traceback.clone()], kwargs),
vm,
);
}
}
🤖 Prompt for AI Agents
In crates/vm/src/stdlib/sys.rs around lines 691 to 701, the use of the `?`
operator on `vm.import("traceback", 0)` and `tb_module.get_attr("print_tb", vm)`
can return early and suppress the rest of the unraisable hook output; change
these to explicit error-handling so failures do not propagate out: attempt to
import the "traceback" module with a match/if-let, and if that fails, write a
minimal note or ignore and continue without returning; if import succeeds,
attempt to get the "print_tb" attribute with a match/if-let, and if that fails
similarly continue; only call `print_tb` when both steps succeed, and ensure any
incidental errors are caught (not propagated) and optionally logged to stderr
via `super::get_stderr(vm)?` so the unraisable exception type/value remains
printed.

@youknowone youknowone merged commit 9aa1f18 into RustPython:main Dec 16, 2025
12 of 13 checks passed
@youknowone youknowone deleted the unraiseable branch December 16, 2025 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant