Skip to content

Add more error functions to the c-api#8248

Open
bschoenmaeckers wants to merge 8 commits into
RustPython:mainfrom
bschoenmaeckers:c-api-more-error
Open

Add more error functions to the c-api#8248
bschoenmaeckers wants to merge 8 commits into
RustPython:mainfrom
bschoenmaeckers:c-api-more-error

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Expanded native C-API support for creating, inspecting, and managing Python exceptions, including traceback updates, handled exceptions, exc_info, and interrupt/signal helpers.
    • Added OS-error helpers for generating exception instances from errno and Windows error codes, with optional filename context.
    • Added C-ABI creation plus getters/setters for Unicode decode/encode/translate error objects.
  • Bug Fixes
    • Improved error/traceback handling, including normalization of “None traceback” and special EINTR signal checking.
  • Chores
    • Refined infallible FFI error propagation semantics for exception setting.

@bschoenmaeckers bschoenmaeckers changed the title C api more error Add more error functions to the c-api Jul 10, 2026
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 8cc95e46-2a68-42c8-b7c4-97ac47c36e0b

📥 Commits

Reviewing files that changed from the base of the PR and between 26359ab and 7c47ed9.

📒 Files selected for processing (1)
  • crates/capi/src/pyerrors/errno.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/capi/src/pyerrors/errno.rs

📝 Walkthrough

Walkthrough

The C-API exception surface now includes VM-backed exception state management, errno and Windows error constructors, Unicode error accessors, traceback handling, interrupt utilities, and pointer-returning FFI error conversion.

Changes

Exception and error C-API

Layer / File(s) Summary
Exception state and FFI error plumbing
crates/capi/src/pyerrors.rs, crates/capi/src/util.rs
Adds exception state, traceback, interrupt, fatal-error, handled-exception, exc_info, and exception-argument APIs, plus pointer-returning FFI error conversion.
Errno exception construction
crates/capi/src/pyerrors/errno.rs
Constructs Python exceptions from host errno values with optional filenames and EINTR signal checks.
Unicode error accessors
crates/capi/src/pyerrors/unicode.rs
Adds Unicode decode, encode, and translate error construction plus attribute accessors and mutators.
Windows error construction
crates/capi/src/pyerrors/windows.rs
Adds Windows error helpers using Win32 error codes, optional filenames, and targeted exception types or OSError.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CAPI
  participant VM
  participant Exception
  CAPI->>VM: fetch, restore, or update exception state
  VM->>Exception: normalize traceback and exception values
  Exception-->>VM: exception attributes and triple
  VM-->>CAPI: return pointers or FFI error sentinel
Loading

Possibly related PRs

Suggested reviewers: youknowone

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 77.27% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding more error-related C API functions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
crates/capi/src/pyerrors/errno.rs (1)

18-56: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Reduce duplication by delegating to the object-based helper.

PyErr_SetFromErrnoWithFilename re-implements the errno read, EINTR handling, message construction, downcast, and call logic that already exists in PyErr_SetFromErrnoWithFilenameObjects. As in CPython, this can decode the filename and then delegate to PyErr_SetFromErrnoWithFilenameObject, keeping the errno/message logic in one place.

🤖 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/pyerrors/errno.rs` around lines 18 - 56, Refactor
PyErr_SetFromErrnoWithFilename to only decode the optional C filename, convert
it to a Python object, and delegate to PyErr_SetFromErrnoWithFilenameObject.
Remove its duplicated errno retrieval, EINTR handling, message construction,
exception downcast, and call logic so the object-based helper remains the single
implementation.
🤖 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/windows.rs`:
- Around line 69-84: In the Windows error conversion branch, avoid leaking the
decoded filename created by decode_fsdefault_and_size. Update the
Some(Ok(filename)) handling to keep the PyObjectRef alive locally and pass its
borrowed raw pointer to set_windows_error, rather than calling into_raw();
preserve the existing error and None handling.

---

Nitpick comments:
In `@crates/capi/src/pyerrors/errno.rs`:
- Around line 18-56: Refactor PyErr_SetFromErrnoWithFilename to only decode the
optional C filename, convert it to a Python object, and delegate to
PyErr_SetFromErrnoWithFilenameObject. Remove its duplicated errno retrieval,
EINTR handling, message construction, exception downcast, and call logic so the
object-based helper remains the single implementation.
🪄 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: 05c2152b-5010-4ab6-a5ca-c0afe9842760

📥 Commits

Reviewing files that changed from the base of the PR and between 9c064c1 and bebdf8e.

📒 Files selected for processing (5)
  • crates/capi/src/pyerrors.rs
  • crates/capi/src/pyerrors/errno.rs
  • crates/capi/src/pyerrors/unicode.rs
  • crates/capi/src/pyerrors/windows.rs
  • crates/capi/src/util.rs

Comment thread crates/capi/src/pyerrors/windows.rs Outdated
@bschoenmaeckers bschoenmaeckers force-pushed the c-api-more-error branch 4 times, most recently from 8f6e3ec to 29f68a9 Compare July 13, 2026 11:44
@bschoenmaeckers

Copy link
Copy Markdown
Contributor Author

@coderabbitai resume

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Reviews resumed.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
crates/capi/src/pyerrors/errno.rs (1)

23-34: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Handle EINTR before decoding the filename. Match the PyErr_SetFromErrnoWithFilenameObjects ordering here: check EINTR immediately after reading errno, before decode_fsdefault_and_size(...). Otherwise a decode failure can bypass vm.check_signals() and return the wrong exception.

🤖 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/pyerrors/errno.rs` around lines 23 - 34, Move the errno ==
libc::EINTR handling immediately after retrieving errno in the
PyErr_SetFromErrnoWithFilenameObjects flow, before any filename null check or
decode_fsdefault_and_size call. Preserve vm.check_signals()? behavior, then
decode the filename only after signal handling completes.
🤖 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.

Outside diff comments:
In `@crates/capi/src/pyerrors/errno.rs`:
- Around line 23-34: Move the errno == libc::EINTR handling immediately after
retrieving errno in the PyErr_SetFromErrnoWithFilenameObjects flow, before any
filename null check or decode_fsdefault_and_size call. Preserve
vm.check_signals()? behavior, then decode the filename only after signal
handling completes.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 415416af-d06c-4cb0-bcad-3eb97d1d62a2

📥 Commits

Reviewing files that changed from the base of the PR and between 2c47ef9 and 29f68a9.

📒 Files selected for processing (2)
  • crates/capi/src/pyerrors/errno.rs
  • crates/capi/src/pyerrors/windows.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/capi/src/pyerrors/windows.rs

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