Skip to content

Commit 770c02e

Browse files
authored
Merge pull request #2026 from davidhewitt/err-method-names
err: tweak names, inlining and docs
2 parents b2bbf97 + 2ac30ec commit 770c02e

File tree

12 files changed

+207
-102
lines changed

12 files changed

+207
-102
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
### Changed
2626

2727
- `PyType::is_subclass`, `PyErr::is_instance` and `PyAny::is_instance` now operate run-time type object instead of a type known at compile-time. The old behavior is still available as `PyType::is_subclass_of`, `PyErr::is_instance_of` and `PyAny::is_instance_of`. [#1985](https://github.com/PyO3/pyo3/pull/1985)
28+
- Rename some methods on `PyErr` (the old names are just marked deprecated for now): [#2026](https://github.com/PyO3/pyo3/pull/2026)
29+
- `pytype` -> `get_type`
30+
- `pvalue` -> `value` (and deprecate equivalent `instance`)
31+
- `ptraceback` -> `traceback`
32+
- `from_instance` -> `from_value`
33+
- `into_instance` -> `into_value`
2834

2935
### Removed
3036

@@ -34,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3440
### Fixed
3541

3642
- Fix undefined symbol for `PyObject_HasAttr` on PyPy. [#2025](https://github.com/PyO3/pyo3/pull/2025)
43+
- Fix memory leak in `PyErr::into_value`. [#2026](https://github.com/PyO3/pyo3/pull/2026)
3744

3845
## [0.15.1] - 2021-11-19
3946

guide/src/exception.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ Python::with_gil(|py| {
9090
});
9191
```
9292

93-
If you already have a Python exception instance, you can simply call [`PyErr::from_instance`].
93+
If you already have a Python exception object, you can simply call [`PyErr::from_value`].
9494

9595
```rust,ignore
96-
PyErr::from_instance(py, err).restore(py);
96+
PyErr::from_value(py, err).restore(py);
9797
```
9898

9999

@@ -134,7 +134,7 @@ which is an alias for the type `Result<T, PyErr>`.
134134
A [`PyErr`] represents a Python exception. Errors within the PyO3 library are also exposed as
135135
Python exceptions.
136136

137-
If your code has a custom error type, adding an implementation of `std::convert::From<MyError> for PyErr`
137+
If your code has a custom error type, adding an implementation of `std::convert::From<MyError> for PyErr`
138138
is usually enough. PyO3 will then automatically convert your error to a Python exception when needed.
139139

140140
The following code snippet defines a Rust error named `CustomIOError`. In its `From<CustomIOError> for PyErr`
@@ -188,7 +188,7 @@ fn main() {
188188
```
189189

190190
This has been implemented for most of Rust's standard library errors, so that you can use the `?`
191-
("try") operator with them. The following code snippet will raise a `ValueError` in Python if
191+
("try") operator with them. The following code snippet will raise a `ValueError` in Python if
192192
`String::parse()` returns an error.
193193

194194
```rust
@@ -254,6 +254,6 @@ defines exceptions for several standard library modules.
254254

255255
[`PyErr`]: {{#PYO3_DOCS_URL}}/pyo3/struct.PyErr.html
256256
[`PyResult`]: {{#PYO3_DOCS_URL}}/pyo3/type.PyResult.html
257-
[`PyErr::from_instance`]: {{#PYO3_DOCS_URL}}/pyo3/struct.PyErr.html#method.from_instance
257+
[`PyErr::from_value`]: {{#PYO3_DOCS_URL}}/pyo3/struct.PyErr.html#method.from_value
258258
[`PyAny::is_instance`]: {{#PYO3_DOCS_URL}}/pyo3/struct.PyAny.html#method.is_instance
259259
[`PyAny::is_instance_of`]: {{#PYO3_DOCS_URL}}/pyo3/struct.PyAny.html#method.is_instance_of

guide/src/migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ let err: PyErr = TypeError::py_err("error message");
170170

171171
After:
172172

173-
```rust
173+
```rust,ignore
174174
# use pyo3::{PyErr, PyResult, Python, type_object::PyTypeObject};
175175
# use pyo3::exceptions::{PyBaseException, PyTypeError};
176176
# Python::with_gil(|py| -> PyResult<()> {

guide/src/python_from_rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class House(object):
280280
Err(e) => {
281281
house.call_method1(
282282
"__exit__",
283-
(e.ptype(py), e.pvalue(py), e.ptraceback(py))
283+
(e.get_type(py), e.value(py), e.traceback(py))
284284
).unwrap();
285285
}
286286
}

pyo3-macros-backend/src/from_pyobject.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ impl<'a> Enum<'a> {
6161

6262
match maybe_ret {
6363
ok @ ::std::result::Result::Ok(_) => return ok,
64-
::std::result::Result::Err(inner) => {
64+
::std::result::Result::Err(err) => {
6565
let py = ::pyo3::PyNativeType::py(obj);
66-
err_reasons.push_str(&::std::format!("{}\n", inner.instance(py).str()?));
66+
err_reasons.push_str(&::std::format!("{}\n", err.value(py).str()?));
6767
}
6868
}
6969
);
@@ -221,11 +221,11 @@ impl<'a> Container<'a> {
221221
format!("failed to extract inner field of {}", quote!(#self_ty))
222222
};
223223
quote!(
224-
::std::result::Result::Ok(#self_ty(obj.extract().map_err(|inner| {
224+
::std::result::Result::Ok(#self_ty(obj.extract().map_err(|err| {
225225
let py = ::pyo3::PyNativeType::py(obj);
226226
let err_msg = ::std::format!("{}: {}",
227227
#error_msg,
228-
inner.instance(py).str().unwrap());
228+
err.value(py).str().unwrap());
229229
::pyo3::exceptions::PyTypeError::new_err(err_msg)
230230
})?))
231231
)

src/conversions/anyhow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod test_anyhow {
144144
Python::with_gil(|py| {
145145
let locals = [("err", pyerr)].into_py_dict(py);
146146
let pyerr = py.run("raise err", None, Some(locals)).unwrap_err();
147-
assert_eq!(pyerr.pvalue(py).to_string(), expected_contents);
147+
assert_eq!(pyerr.value(py).to_string(), expected_contents);
148148
})
149149
}
150150

@@ -161,7 +161,7 @@ mod test_anyhow {
161161
Python::with_gil(|py| {
162162
let locals = [("err", pyerr)].into_py_dict(py);
163163
let pyerr = py.run("raise err", None, Some(locals)).unwrap_err();
164-
assert_eq!(pyerr.pvalue(py).to_string(), expected_contents);
164+
assert_eq!(pyerr.value(py).to_string(), expected_contents);
165165
})
166166
}
167167
}

src/conversions/eyre.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ mod tests {
145145
Python::with_gil(|py| {
146146
let locals = [("err", pyerr)].into_py_dict(py);
147147
let pyerr = py.run("raise err", None, Some(locals)).unwrap_err();
148-
assert_eq!(pyerr.pvalue(py).to_string(), expected_contents);
148+
assert_eq!(pyerr.value(py).to_string(), expected_contents);
149149
})
150150
}
151151
}

src/derive_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ impl FunctionDescription {
273273

274274
/// Add the argument name to the error message of an error which occurred during argument extraction
275275
pub fn argument_extraction_error(py: Python, arg_name: &str, error: PyErr) -> PyErr {
276-
if error.ptype(py) == py.get_type::<PyTypeError>() {
276+
if error.is_instance_of::<PyTypeError>(py) {
277277
let reason = error
278-
.instance(py)
278+
.value(py)
279279
.str()
280280
.unwrap_or_else(|_| PyString::new(py, ""));
281281
PyTypeError::new_err(format!("argument '{}': {}", arg_name, reason))

0 commit comments

Comments
 (0)