Make PyTuple constructors return &PyTuple
This commit is contained in:
parent
a5a2059691
commit
39d3ceb551
|
@ -426,7 +426,7 @@ where
|
||||||
/// Converts `()` to an empty Python tuple.
|
/// Converts `()` to an empty Python tuple.
|
||||||
impl FromPy<()> for Py<PyTuple> {
|
impl FromPy<()> for Py<PyTuple> {
|
||||||
fn from_py(_: (), py: Python) -> Py<PyTuple> {
|
fn from_py(_: (), py: Python) -> Py<PyTuple> {
|
||||||
PyTuple::empty(py)
|
Py::from_py(PyTuple::empty(py), py)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ pyobject_native_type!(PyTuple, ffi::PyTuple_Type, ffi::PyTuple_Check);
|
||||||
|
|
||||||
impl PyTuple {
|
impl PyTuple {
|
||||||
/// Construct a new tuple with the given elements.
|
/// Construct a new tuple with the given elements.
|
||||||
pub fn new<T, U>(py: Python, elements: impl IntoIterator<Item = T, IntoIter = U>) -> Py<PyTuple>
|
pub fn new<'p, T, U>(py: Python<'p>, elements: impl IntoIterator<Item = T, IntoIter = U>) -> &'p PyTuple
|
||||||
where
|
where
|
||||||
T: ToPyObject,
|
T: ToPyObject,
|
||||||
U: ExactSizeIterator<Item = T>,
|
U: ExactSizeIterator<Item = T>,
|
||||||
|
@ -33,13 +33,13 @@ impl PyTuple {
|
||||||
for (i, e) in elements_iter.enumerate() {
|
for (i, e) in elements_iter.enumerate() {
|
||||||
ffi::PyTuple_SetItem(ptr, i as Py_ssize_t, e.to_object(py).into_ptr());
|
ffi::PyTuple_SetItem(ptr, i as Py_ssize_t, e.to_object(py).into_ptr());
|
||||||
}
|
}
|
||||||
Py::from_owned_ptr_or_panic(ptr)
|
py.from_owned_ptr(ptr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves the empty tuple.
|
/// Retrieves the empty tuple.
|
||||||
pub fn empty(_py: Python) -> Py<PyTuple> {
|
pub fn empty<'p>(py: Python<'p>) -> &'p PyTuple {
|
||||||
unsafe { Py::from_owned_ptr_or_panic(ffi::PyTuple_New(0)) }
|
unsafe { py.from_owned_ptr(ffi::PyTuple_New(0)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the length of the tuple.
|
/// Gets the length of the tuple.
|
||||||
|
@ -264,8 +264,7 @@ mod test {
|
||||||
fn test_new() {
|
fn test_new() {
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let pyob = PyTuple::new(py, &[1, 2, 3]);
|
let ob = PyTuple::new(py, &[1, 2, 3]);
|
||||||
let ob = pyob.as_ref(py);
|
|
||||||
assert_eq!(3, ob.len());
|
assert_eq!(3, ob.len());
|
||||||
let ob: &PyAny = ob.into();
|
let ob: &PyAny = ob.into();
|
||||||
assert_eq!((1, 2, 3), ob.extract().unwrap());
|
assert_eq!((1, 2, 3), ob.extract().unwrap());
|
||||||
|
|
|
@ -93,8 +93,9 @@ impl Drop for ClassWithDrop {
|
||||||
unsafe {
|
unsafe {
|
||||||
let py = Python::assume_gil_acquired();
|
let py = Python::assume_gil_acquired();
|
||||||
|
|
||||||
let _empty1 = PyTuple::empty(py);
|
let _empty1: Py<PyTuple> = FromPy::from_py(PyTuple::empty(py), py);
|
||||||
let _empty2: PyObject = PyTuple::empty(py).into();
|
let _empty2: Py<PyTuple> = FromPy::from_py(PyTuple::empty(py), py);
|
||||||
|
let _empty2: PyObject = _empty2.into();
|
||||||
let _empty3: &PyAny = py.from_owned_ptr(ffi::PyTuple_New(0));
|
let _empty3: &PyAny = py.from_owned_ptr(ffi::PyTuple_New(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,7 +111,7 @@ fn create_pointers_in_drop() {
|
||||||
{
|
{
|
||||||
let gil = Python::acquire_gil();
|
let gil = Python::acquire_gil();
|
||||||
let py = gil.python();
|
let py = gil.python();
|
||||||
let empty = PyTuple::empty(py);
|
let empty: Py<PyTuple> = FromPy::from_py(PyTuple::empty(py), py);
|
||||||
ptr = empty.as_ptr();
|
ptr = empty.as_ptr();
|
||||||
cnt = empty.get_refcnt() - 1;
|
cnt = empty.get_refcnt() - 1;
|
||||||
let inst = Py::new(py, ClassWithDrop {}).unwrap();
|
let inst = Py::new(py, ClassWithDrop {}).unwrap();
|
||||||
|
|
Loading…
Reference in New Issue