-
Notifications
You must be signed in to change notification settings - Fork 958
add PyObject_HasAttrWithError & use it in PyAnyMethods::hasattr
#5944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bschoenmaeckers
wants to merge
3
commits into
PyO3:main
Choose a base branch
from
bschoenmaeckers:hasattr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+62
−53
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| add `pyo3-ffi::compat::PyObject_HasAttrWithError` & `pyo3-ffi::compat::PyObject_GetOptionalAttr` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| use `PyObject_HasAttrWithError` in `PyAnyMethods::hasattr` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,8 @@ | ||||||||
| use crate::call::PyCallArgs; | ||||||||
| use crate::class::basic::CompareOp; | ||||||||
| use crate::conversion::{FromPyObject, IntoPyObject}; | ||||||||
| use crate::err::{PyErr, PyResult}; | ||||||||
| use crate::exceptions::{PyAttributeError, PyTypeError}; | ||||||||
| use crate::err::{error_on_minusone, PyErr, PyResult}; | ||||||||
| use crate::exceptions::PyTypeError; | ||||||||
| use crate::ffi_ptr_ext::FfiPtrExt; | ||||||||
| use crate::impl_::pycell::PyStaticClassObject; | ||||||||
| use crate::instance::Bound; | ||||||||
|
|
@@ -11,7 +11,7 @@ use crate::py_result_ext::PyResultExt; | |||||||
| use crate::type_object::{PyTypeCheck, PyTypeInfo}; | ||||||||
| use crate::types::PySuper; | ||||||||
| use crate::types::{PyDict, PyIterator, PyList, PyString, PyType}; | ||||||||
| use crate::{err, ffi, Borrowed, BoundObject, IntoPyObjectExt, Py, Python}; | ||||||||
| use crate::{err, ffi, Borrowed, BoundObject, IntoPyObjectExt, Py}; | ||||||||
| #[allow(deprecated)] | ||||||||
| use crate::{DowncastError, DowncastIntoError}; | ||||||||
| use std::cell::UnsafeCell; | ||||||||
|
|
@@ -980,17 +980,15 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { | |||||||
| where | ||||||||
| N: IntoPyObject<'py, Target = PyString>, | ||||||||
| { | ||||||||
| // PyObject_HasAttr suppresses all exceptions, which was the behaviour of `hasattr` in Python 2. | ||||||||
| // Use an implementation which suppresses only AttributeError, which is consistent with `hasattr` in Python 3. | ||||||||
| fn inner(py: Python<'_>, getattr_result: PyResult<Bound<'_, PyAny>>) -> PyResult<bool> { | ||||||||
| match getattr_result { | ||||||||
| Ok(_) => Ok(true), | ||||||||
| Err(err) if err.is_instance_of::<PyAttributeError>(py) => Ok(false), | ||||||||
| Err(e) => Err(e), | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| inner(self.py(), self.getattr(attr_name)) | ||||||||
| let py = self.py(); | ||||||||
| let result = unsafe { | ||||||||
| ffi::compat::PyObject_HasAttrWithError( | ||||||||
| self.as_ptr(), | ||||||||
| attr_name.into_pyobject(py).map_err(Into::into)?.as_ptr(), | ||||||||
| ) | ||||||||
| }; | ||||||||
| error_on_minusone(py, result)?; | ||||||||
| Ok(result > 0) | ||||||||
| } | ||||||||
|
|
||||||||
| fn getattr<N>(&self, attr_name: N) -> PyResult<Bound<'py, PyAny>> | ||||||||
|
|
@@ -1020,48 +1018,26 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { | |||||||
| where | ||||||||
| N: IntoPyObject<'py, Target = PyString>, | ||||||||
| { | ||||||||
| fn inner<'py>( | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be desirable to restore the |
||||||||
| any: &Bound<'py, PyAny>, | ||||||||
| attr_name: Borrowed<'_, 'py, PyString>, | ||||||||
| ) -> PyResult<Option<Bound<'py, PyAny>>> { | ||||||||
| #[cfg(Py_3_13)] | ||||||||
| { | ||||||||
| let mut resp_ptr: *mut ffi::PyObject = std::ptr::null_mut(); | ||||||||
| match unsafe { | ||||||||
| ffi::PyObject_GetOptionalAttr(any.as_ptr(), attr_name.as_ptr(), &mut resp_ptr) | ||||||||
| } { | ||||||||
| // Attribute found, result is a new strong reference | ||||||||
| 1 => { | ||||||||
| let bound = unsafe { Bound::from_owned_ptr(any.py(), resp_ptr) }; | ||||||||
| Ok(Some(bound)) | ||||||||
| } | ||||||||
| // Attribute not found, result is NULL | ||||||||
| 0 => Ok(None), | ||||||||
|
|
||||||||
| // An error occurred (other than AttributeError) | ||||||||
| _ => Err(PyErr::fetch(any.py())), | ||||||||
| } | ||||||||
| let py = self.py(); | ||||||||
| let mut resp_ptr: *mut ffi::PyObject = std::ptr::null_mut(); | ||||||||
| match unsafe { | ||||||||
| ffi::compat::PyObject_GetOptionalAttr( | ||||||||
| self.as_ptr(), | ||||||||
| attr_name.into_pyobject(py).map_err(Into::into)?.as_ptr(), | ||||||||
| &mut resp_ptr, | ||||||||
| ) | ||||||||
| } { | ||||||||
| // Attribute found, result is a new strong reference | ||||||||
| 1 => { | ||||||||
| let bound = unsafe { Bound::from_owned_ptr(py, resp_ptr) }; | ||||||||
| Ok(Some(bound)) | ||||||||
|
Comment on lines
+1032
to
+1033
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| } | ||||||||
| // Attribute not found, result is NULL | ||||||||
| 0 => Ok(None), | ||||||||
|
|
||||||||
| #[cfg(not(Py_3_13))] | ||||||||
| { | ||||||||
| match any.getattr(attr_name) { | ||||||||
| Ok(bound) => Ok(Some(bound)), | ||||||||
| Err(err) => { | ||||||||
| let err_type = err | ||||||||
| .get_type(any.py()) | ||||||||
| .is(PyType::new::<PyAttributeError>(any.py())); | ||||||||
| match err_type { | ||||||||
| true => Ok(None), | ||||||||
| false => Err(err), | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| // An error occurred (other than AttributeError) | ||||||||
| _ => Err(PyErr::fetch(py)), | ||||||||
| } | ||||||||
|
|
||||||||
| let py = self.py(); | ||||||||
| inner(self, attr_name.into_pyobject_or_pyerr(py)?.as_borrowed()) | ||||||||
| } | ||||||||
|
|
||||||||
| fn setattr<N, V>(&self, attr_name: N, value: V) -> PyResult<()> | ||||||||
|
|
||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have generally tried to avoid the
.as_ptr()on temporaries like this because the innocent-looking refactoring out to a variable creates a dangling ptr.I think also it's worth having the
fn innerpattern, see e.g.getattr, can passany: &Bound<'py, PyAny>, attr_name: Borrowed<'_, '_, PyString>,, which can slightly reduce generic code bloat by having the error handling portion only compiled once.