◐ Shell
clean mode source ↗

Allow #[pyexception] on module attr by youknowone · Pull Request #6218 · RustPython/RustPython

Walkthrough

Introduces support for a new #[pyexception] attribute in the derive-impl framework to define exception types, refactors the SSL module to use a structured exception hierarchy with PySslError as the base, makes exception slot methods public, and extends exception metadata validation to support module attributes.

Changes

Cohort / File(s) Summary
Derive infrastructure for pyexception support
derive-impl/src/pymodule.rs, derive-impl/src/util.rs
Adds Exception variant to attribute name enum, wires it through Display and FromStr conversions, treats pyexception like pyclass in module item creation, includes Exception in validation flow, and adds "module" to ALLOWED_NAMES for ExceptionItemMeta.
Exception slot method visibility
derive-impl/src/pyclass.rs, vm/src/exceptions.rs
Changes slot_new and slot_init from private/pub(crate) to pub visibility in PyException and PyOSError implementations; adds new PyPayload implementation path for PyException when no ctx_name is provided.
SSL module exception hierarchy refactoring
stdlib/src/ssl.rs
Replaces ad-hoc SSL exception creation with a structured hierarchy: introduces PySslError (base, derives from PyOSError) and six subclasses (PySslCertVerificationError, PySslZeroReturnError, PySslWantReadError, PySslWantWriteError, PySslSyscallError, PySslEOFError), all marked as pyexceptions; updates all error construction sites to use the new exception classes and adds __str__ implementation for PySslError.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • stdlib/src/ssl.rs: Requires careful review of the exception hierarchy refactoring, all error construction points, and the __str__ implementation for correctness and consistency across the module. Verify that all old ssl_error(vm) calls have been properly replaced.
  • derive-impl/src/pymodule.rs and derive-impl/src/util.rs: Review the integration of Exception variant into the attribute parsing and validation pipeline to ensure it follows the same pattern as Class and Function.
  • Visibility changes: Verify that making slot methods public in derive-impl and vm/src/exceptions.rs does not expose internal implementation details unintentionally.

Possibly related PRs

  • Impl PyAttributeError args #5805: Modifies exception slot implementations in vm/src/exceptions.rs, including slot_init and slot_new visibility and exception slot implementations, directly overlapping with exception infrastructure changes in this PR.

Poem

🐰 Exceptions now have a home, a hierarchy so grand,
From PySslError's base, the subclasses expand,
The #[pyexception] magic makes definitions neat,
Public slots and module attributes, the feature's complete! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "Allow #[pyexception] on module attr" directly describes the primary objective of the changeset. The modifications across multiple files demonstrate this core feature: derive-impl/src/pymodule.rs introduces an Exception variant to support pyexception attributes in modules, derive-impl/src/util.rs adds "module" to the allowed attribute names for ExceptionItemMeta, and stdlib/src/ssl.rs exemplifies the new capability with public pyexception definitions. The title is specific, concise, and clearly communicates the main change without being vague or misleading.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.