-
Notifications
You must be signed in to change notification settings - Fork 792
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
implement PySequenceMethods
#3535
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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 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 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,59 @@ | ||
use std::os::raw::c_int; | ||
|
||
use crate::err::{error_on_minusone, SignedInteger}; | ||
use crate::types::any::PyAnyMethods; | ||
use crate::{ffi, Py2, PyErr, PyResult, Python}; | ||
|
||
/// Internal helper to convert raw ffi call results such as pointers | ||
/// or integers into safe wrappers. | ||
/// | ||
/// `unsafe` to implement because it is highly likely this trait is | ||
/// passed a pointer, and is free to do interpret it as it sees fit. | ||
pub(crate) unsafe trait FromFfiCallResult<'py, RawResult>: Sized { | ||
fn from_ffi_call_result(py: Python<'py>, raw: RawResult) -> PyResult<Self>; | ||
} | ||
|
||
/// For Py2<T>, perform an unchecked downcast to the target type T. | ||
unsafe impl<'py, T> FromFfiCallResult<'py, *mut ffi::PyObject> for Py2<'py, T> { | ||
fn from_ffi_call_result(py: Python<'py>, raw: *mut ffi::PyObject) -> PyResult<Self> { | ||
unsafe { Py2::from_owned_ptr_or_err(py, raw).map(|any| any.downcast_into_unchecked()) } | ||
} | ||
} | ||
|
||
unsafe impl<'py, T> FromFfiCallResult<'py, T> for () | ||
where | ||
T: SignedInteger, | ||
{ | ||
fn from_ffi_call_result(py: Python<'py>, raw: T) -> PyResult<Self> { | ||
error_on_minusone(py, raw) | ||
} | ||
} | ||
|
||
unsafe impl<'py, T> FromFfiCallResult<'py, T> for T | ||
where | ||
T: SignedInteger, | ||
{ | ||
fn from_ffi_call_result(py: Python<'py>, raw: T) -> PyResult<Self> { | ||
if raw != T::MINUS_ONE { | ||
Ok(raw) | ||
} else { | ||
Err(PyErr::fetch(py)) | ||
} | ||
} | ||
} | ||
|
||
unsafe impl<'py> FromFfiCallResult<'py, c_int> for bool { | ||
fn from_ffi_call_result(py: Python<'py>, raw: c_int) -> PyResult<Self> { | ||
match raw { | ||
0 => Ok(false), | ||
1 => Ok(true), | ||
_ => Err(PyErr::fetch(py)), | ||
} | ||
} | ||
} | ||
|
||
/// Convert an isize which is known to be positive to a usize. | ||
#[inline] | ||
pub(crate) fn positive_isize_as_usize(x: isize) -> usize { | ||
x as usize | ||
} |
This file contains 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 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
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.
Staring at this again I wonder if it should have been a safe trait with the method being
unsafe fn from_ffi_call_result
. Both are a bit awkward because only the implementation for*mut ffi::PyObject
actually needs to beunsafe
. Anyway, this is only an internal API so it doesn't need to be perfect...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.
Reading the comment above, why does it have to be unsafe at all? "May get a pointer and do unsafe stuff with it" doesn't sound like a strong argument :)
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.
I'm generally of the understanding that the pointer alone can't communicate that's it's safe to read.
Probably the right approach is to combine this with some wrapper along the lines of #1809 so that the trait doesn't need to be unsafe at all. I'll keep playing.