Repository navigation
Allow calling PyTuple_SetItem
#9441
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,9 @@ use crate::object::define_py_check; | |
| use crate::pystate::with_vm; | ||
| use crate::util::FfiPtrExt; | ||
| use core::ffi::c_int; | ||
| use core::ptr; | ||
| use core::slice; | ||
| use rustpython_vm::PyResult; | ||
| use rustpython_vm::AsObject; | ||
| use rustpython_vm::builtins::PyTuple; | ||
| use rustpython_vm::sliceable::SliceableSequenceOp; | ||
|
|
||
|
|
@@ -15,12 +16,13 @@ define_py_check!(exact fn PyTuple_CheckExact, types.tuple_type); | |
| pub extern "C" fn PyTuple_New(len: isize) -> *mut PyObject { | ||
| with_vm(|vm| { | ||
| if len == 0 { | ||
| return Ok(vm.ctx.empty_tuple.to_owned()); | ||
| Ok(vm.ctx.empty_tuple.to_owned()) | ||
| } else { | ||
| let len: usize = len | ||
| .try_into() | ||
| .map_err(|_| vm.new_system_error("Negative size passed to PyTuple_New"))?; | ||
| Ok(vm.new_tuple(vec![vm.ctx.none(); len])) | ||
|
bschoenmaeckers marked this conversation as resolved.
bschoenmaeckers marked this conversation as resolved.
|
||
| } | ||
|
|
||
| Err(vm.new_not_implemented_error( | ||
| "PyTuple_New for non zero sized tuples is not supported, use PyTuple_FromArray instead", | ||
| )) | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -61,14 +63,30 @@ pub unsafe extern "C" fn PyTuple_Pack(len: isize, mut args: ...) -> *mut PyObjec | |
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn PyTuple_SetItem( | ||
| _tuple: *mut PyObject, | ||
| _pos: isize, | ||
| _value: *mut PyObject, | ||
| pub unsafe extern "C" fn PyTuple_SetItem( | ||
| tuple: *mut PyObject, | ||
| pos: isize, | ||
| value: *mut PyObject, | ||
| ) -> c_int { | ||
| with_vm::<PyResult<()>, _>( | ||
| |vm| Err(vm.new_not_implemented_error("Tuple objects are immutable")), | ||
| ) | ||
| with_vm(|vm| { | ||
| let tuple = unsafe { tuple.assume_borrowed_and_cast::<PyTuple>(vm)? }; | ||
| let value = unsafe { value.assume_owned() }; | ||
| let index: usize = pos | ||
| .try_into() | ||
| .map_err(|_| vm.new_index_error("negative position"))?; | ||
|
|
||
| if tuple.len() <= index { | ||
| Err(vm.new_index_error("tuple index out of range")) | ||
| } else if tuple.as_object().strong_count() > 1 { | ||
| Err(vm.new_system_error("Tuple objects are immutable when the reference count > 1")) | ||
|
bschoenmaeckers marked this conversation as resolved.
|
||
| } else { | ||
| // SAFETY: We have exclusive access to the tuple payload (strong_count == 1), | ||
| // so mutating the selected element in place is valid. | ||
| let data_ptr = unsafe { tuple.as_ptr().add(index).cast_mut() }; | ||
| unsafe { ptr::replace(data_ptr, value) }; | ||
|
Comment on lines
+85
to
+86
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.
On every successful insertion, Useful? React with 👍 / 👎. |
||
| Ok(()) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
|
|
@@ -111,6 +129,7 @@ pub unsafe extern "C" fn PyTuple_GetSlice( | |
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use pyo3::ffi; | ||
| use pyo3::prelude::*; | ||
| use pyo3::types::PyTuple; | ||
|
|
||
|
|
@@ -138,4 +157,29 @@ mod tests { | |
| assert_eq!(slice.extract::<(u32,)>().unwrap(), (2,)); | ||
| }) | ||
| } | ||
|
|
||
| #[test] | ||
| fn tuple_is_mutable_with_one_ref() { | ||
| Python::attach(|py| { | ||
| let value: Bound<'_, _> = 1.into_pyobject(py).unwrap(); | ||
| let tuple: Bound<'_, PyTuple> = | ||
| unsafe { Bound::from_owned_ptr(py, ffi::PyTuple_New(1)).cast_into_unchecked() }; | ||
| assert!(tuple.get_item(0).unwrap().is_none()); | ||
| unsafe { | ||
| assert_eq!( | ||
| ffi::PyTuple_SetItem(tuple.as_ptr(), 0, value.clone().into_ptr()), | ||
| 0 | ||
| ); | ||
| } | ||
| assert_eq!(tuple.get_item(0).unwrap().extract::<u32>().unwrap(), 1); | ||
| let _new_ref = tuple.clone(); | ||
| unsafe { | ||
| assert_eq!( | ||
| ffi::PyTuple_SetItem(tuple.as_ptr(), 0, value.clone().into_ptr()), | ||
| -1 | ||
| ); | ||
| } | ||
| assert!(PyErr::take(py).is_some()) | ||
| }) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.