Add more c-api error functions#7915
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThis PR adds C-API predicates and getters for exception inspection, an FFI binding PyTraceBack_Print that calls Python's traceback.print_tb inside the VM, and re-exports the ChangesException and Traceback C-API Bindings
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
crates/capi/src/traceback.rs (1)
10-13: 💤 Low valueConsider null pointer checks for defensive safety.
While this is an
unsafe extern "C"function, dereferencingtbandfilewithout null checks at lines 12-13 will cause undefined behavior if callers pass null. A defensive check could improve robustness:🛡️ Optional defensive null check
pub unsafe extern "C" fn PyTraceBack_Print(tb: *mut PyObject, file: *mut PyObject) -> c_int { + if tb.is_null() || file.is_null() { + return -1; + } with_vm(|vm| { let tb = unsafe { &*tb }; let file = unsafe { &*file };🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/capi/src/traceback.rs` around lines 10 - 13, The function PyTraceBack_Print currently dereferences tb and file unsafely; add null-pointer checks for the incoming raw pointers (tb and file) before the unsafe dereferences inside PyTraceBack_Print (the locations where it does let tb = &*tb and let file = &*file) and return an appropriate c_int error value (e.g., -1) if either is null (and optionally set a Python C-API error), otherwise proceed into with_vm as before.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/capi/src/pyerrors.rs`:
- Around line 290-297: PyException_GetContext currently returns Py_None when no
context is set (via vm.unwrap_or_none(context)), but CPython semantics and the
tests expect a NULL pointer; update PyException_GetContext to mirror the style
used in PyException_GetCause/PyException_GetTraceback by converting the optional
context to a raw pointer and returning null when absent (e.g., map the Option to
either the object's raw pointer or std::ptr::null_mut()), and avoid using
vm.unwrap_or_none so the function returns NULL when no context is present.
---
Nitpick comments:
In `@crates/capi/src/traceback.rs`:
- Around line 10-13: The function PyTraceBack_Print currently dereferences tb
and file unsafely; add null-pointer checks for the incoming raw pointers (tb and
file) before the unsafe dereferences inside PyTraceBack_Print (the locations
where it does let tb = &*tb and let file = &*file) and return an appropriate
c_int error value (e.g., -1) if either is null (and optionally set a Python
C-API error), otherwise proceed into with_vm as before.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 8fc1a143-8d65-43f6-a6c5-3868e128ca2a
📒 Files selected for processing (4)
crates/capi/src/lib.rscrates/capi/src/object.rscrates/capi/src/pyerrors.rscrates/capi/src/traceback.rs
0ecb4a4 to
0ef4258
Compare
0ef4258 to
1b3ad5b
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/capi/src/traceback.rs (1)
10-13: 💤 Low valueConsider adding null-pointer guards for defensive safety.
While CPython's C-API generally assumes valid (non-null) pointers and the
unsafemarker delegates this responsibility to the caller, dereferencing null pointers would cause undefined behavior. Adding defensive checks would make the function more robust against misuse from C callers.🛡️ Optional defensive null checks
pub unsafe extern "C" fn PyTraceBack_Print(tb: *mut PyObject, file: *mut PyObject) -> c_int { + if tb.is_null() || file.is_null() { + return -1; + } with_vm(|vm| { let tb = unsafe { &*tb }; let file = unsafe { &*file };🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/capi/src/traceback.rs` around lines 10 - 13, Add defensive null-pointer guards at the start of PyTraceBack_Print: check if the raw pointers tb or file are null (e.g., tb.is_null() or file.is_null()) before entering the unsafe dereference or calling with_vm, and if either is null return an error code (c_int, e.g., -1) so you avoid undefined behavior from dereferencing; update the function PyTraceBack_Print to perform these checks and early-return prior to using &*tb or &*file.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@crates/capi/src/traceback.rs`:
- Around line 10-13: Add defensive null-pointer guards at the start of
PyTraceBack_Print: check if the raw pointers tb or file are null (e.g.,
tb.is_null() or file.is_null()) before entering the unsafe dereference or
calling with_vm, and if either is null return an error code (c_int, e.g., -1) so
you avoid undefined behavior from dereferencing; update the function
PyTraceBack_Print to perform these checks and early-return prior to using &*tb
or &*file.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: f389499e-0a6e-4d74-a5f1-a8f6d4becfaf
📒 Files selected for processing (4)
crates/capi/src/lib.rscrates/capi/src/object.rscrates/capi/src/pyerrors.rscrates/capi/src/traceback.rs
✅ Files skipped from review due to trivial changes (1)
- crates/capi/src/object.rs
🚧 Files skipped from review as they are similar to previous changes (2)
- crates/capi/src/lib.rs
- crates/capi/src/pyerrors.rs
Head branch was pushed to by a user without write access
737ad27 to
2cca0d5
Compare
Head branch was pushed to by a user without write access
2cca0d5 to
7bda56a
Compare
Summary by CodeRabbit