Skip to content

Commit d7f2a22

Browse files
authored
Change OSError type aliases to also be type aliases in Rust (#5803)
1 parent 4e81116 commit d7f2a22

4 files changed

Lines changed: 10 additions & 31 deletions

File tree

guide/src/building-and-distribution.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ These are:
276276
- `#[pyo3(text_signature = "...")]` does not work on classes until Python 3.10 or greater.
277277
- The `dict` and `weakref` options on classes are not supported until Python 3.9 or greater.
278278
- The buffer API is not supported until Python 3.11 or greater.
279+
- Subclassing native types (e.g. `PyException`) is not supported until Python 3.12 or greater.
279280
- Optimizations which rely on knowledge of the exact Python version compiled against.
280281

281282
## Embedding Python in Rust

guide/src/exception.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Python::attach(|py| {
172172

173173
```
174174

175-
Note that this is not possible when the ``abi3`` feature is enabled, as that prevents subclassing ``PyException``.
175+
Note that when the `abi3` feature is enabled, subclassing `PyException` is only possible on Python 3.12 or greater.
176176

177177
[`create_exception!`]: {{#PYO3_DOCS_URL}}/pyo3/macro.create_exception.html
178178
[`import_exception!`]: {{#PYO3_DOCS_URL}}/pyo3/macro.import_exception.html

newsfragments/5803.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`PyEnvironmentError`, `PyIOError`, and `PyWindowsError` are now type aliases of `PyOSError` (as is the case in Python since 3.3).

src/exceptions.rs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,6 @@ macro_rules! impl_native_exception (
294294
)
295295
);
296296

297-
#[cfg(windows)]
298-
macro_rules! impl_windows_native_exception (
299-
($name:ident, $exc_name:ident, $python_name:expr, $doc:expr, $layout:path) => (
300-
#[cfg(windows)]
301-
#[doc = $doc]
302-
#[repr(transparent)]
303-
#[allow(clippy::upper_case_acronyms, reason = "Python exception names")]
304-
pub struct $name($crate::PyAny);
305-
306-
$crate::impl_exception_boilerplate!($name);
307-
$crate::pyobject_native_type!($name, $layout, |_py| unsafe { $crate::ffi::$exc_name as *mut $crate::ffi::PyTypeObject }, "builtins", $python_name);
308-
);
309-
($name:ident, $exc_name:ident, $python_name:expr, $doc:expr) => (
310-
impl_windows_native_exception!($name, $exc_name, $python_name, $doc, $crate::ffi::PyBaseExceptionObject);
311-
)
312-
);
313-
314297
macro_rules! native_doc(
315298
($name: literal, $alt: literal) => (
316299
concat!(
@@ -732,21 +715,15 @@ impl_native_exception!(
732715
native_doc!("TimeoutError")
733716
);
734717

735-
impl_native_exception!(
736-
PyEnvironmentError,
737-
PyExc_EnvironmentError,
738-
"EnvironmentError",
739-
native_doc!("EnvironmentError")
740-
);
741-
impl_native_exception!(PyIOError, PyExc_IOError, "IOError", native_doc!("IOError"));
718+
/// Alias of `PyOSError`, corresponding to `EnvironmentError` alias in Python.
719+
pub type PyEnvironmentError = PyOSError;
720+
721+
/// Alias of `PyOSError`, corresponding to `IOError` alias in Python.
722+
pub type PyIOError = PyOSError;
742723

743724
#[cfg(windows)]
744-
impl_windows_native_exception!(
745-
PyWindowsError,
746-
PyExc_WindowsError,
747-
"WindowsError",
748-
native_doc!("WindowsError")
749-
);
725+
/// Alias of `PyOSError`, corresponding to `WindowsError` alias in Python.
726+
pub type PyWindowsError = PyOSError;
750727

751728
impl PyUnicodeDecodeError {
752729
/// Creates a Python `UnicodeDecodeError`.

0 commit comments

Comments
 (0)