Merge pull request #447 from birkenfeld/tuple_new

Make PyTuple constructors return &PyTuple
This commit is contained in:
Yuji Kanagawa 2019-05-26 01:07:14 +09:00 committed by GitHub
commit 4d7ca3a450
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 20 deletions

View file

@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* `PySet::new` and `PyFrozenSet::new` now return `PyResult<&Py[Frozen]Set>`; exceptions are raised if
the items are not hashable.
* Fixed building using `venv` on Windows.
* `PyTuple::new` now returns `&PyTuple` instead of `Py<PyTuple>`.
## [0.6.0] - 2018-03-28

View file

@ -11,7 +11,7 @@ fn make_date<'p>(py: Python<'p>, year: i32, month: u8, day: u8) -> PyResult<&'p
}
#[pyfunction]
fn get_date_tuple(py: Python<'_>, d: &PyDate) -> Py<PyTuple> {
fn get_date_tuple<'p>(py: Python<'p>, d: &PyDate) -> &'p PyTuple {
PyTuple::new(
py,
&[d.get_year(), d.get_month() as i32, d.get_day() as i32],
@ -65,7 +65,7 @@ fn time_with_fold<'p>(
}
#[pyfunction]
fn get_time_tuple(py: Python<'_>, dt: &PyTime) -> Py<PyTuple> {
fn get_time_tuple<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
PyTuple::new(
py,
&[
@ -79,7 +79,7 @@ fn get_time_tuple(py: Python<'_>, dt: &PyTime) -> Py<PyTuple> {
#[cfg(Py_3_6)]
#[pyfunction]
fn get_time_tuple_fold(py: Python, dt: &PyTime) -> Py<PyTuple> {
fn get_time_tuple_fold<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
PyTuple::new(
py,
&[
@ -103,7 +103,7 @@ fn make_delta<'p>(
}
#[pyfunction]
fn get_delta_tuple(py: Python<'_>, delta: &PyDelta) -> Py<PyTuple> {
fn get_delta_tuple<'p>(py: Python<'p>, delta: &PyDelta) -> &'p PyTuple {
PyTuple::new(
py,
&[
@ -140,7 +140,7 @@ fn make_datetime<'p>(
}
#[pyfunction]
fn get_datetime_tuple(py: Python<'_>, dt: &PyDateTime) -> Py<PyTuple> {
fn get_datetime_tuple<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
PyTuple::new(
py,
&[
@ -157,7 +157,7 @@ fn get_datetime_tuple(py: Python<'_>, dt: &PyDateTime) -> Py<PyTuple> {
#[cfg(Py_3_6)]
#[pyfunction]
fn get_datetime_tuple_fold(py: Python, dt: &PyDateTime) -> Py<PyTuple> {
fn get_datetime_tuple_fold<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
PyTuple::new(
py,
&[

View file

@ -426,7 +426,7 @@ where
/// Converts `()` to an empty Python tuple.
impl FromPy<()> for Py<PyTuple> {
fn from_py(_: (), py: Python) -> Py<PyTuple> {
PyTuple::empty(py)
Py::from_py(PyTuple::empty(py), py)
}
}

View file

@ -21,7 +21,10 @@ pyobject_native_type!(PyTuple, ffi::PyTuple_Type, ffi::PyTuple_Check);
impl PyTuple {
/// 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
T: ToPyObject,
U: ExactSizeIterator<Item = T>,
@ -33,13 +36,13 @@ impl PyTuple {
for (i, e) in elements_iter.enumerate() {
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.
pub fn empty(_py: Python) -> Py<PyTuple> {
unsafe { Py::from_owned_ptr_or_panic(ffi::PyTuple_New(0)) }
pub fn empty<'p>(py: Python<'p>) -> &'p PyTuple {
unsafe { py.from_owned_ptr(ffi::PyTuple_New(0)) }
}
/// Gets the length of the tuple.
@ -264,8 +267,7 @@ mod test {
fn test_new() {
let gil = Python::acquire_gil();
let py = gil.python();
let pyob = PyTuple::new(py, &[1, 2, 3]);
let ob = pyob.as_ref(py);
let ob = PyTuple::new(py, &[1, 2, 3]);
assert_eq!(3, ob.len());
let ob: &PyAny = ob.into();
assert_eq!((1, 2, 3), ob.extract().unwrap());

View file

@ -93,8 +93,8 @@ impl Drop for ClassWithDrop {
unsafe {
let py = Python::assume_gil_acquired();
let _empty1 = PyTuple::empty(py);
let _empty2: PyObject = PyTuple::empty(py).into();
let _empty1: Py<PyTuple> = FromPy::from_py(PyTuple::empty(py), py);
let _empty2 = PyTuple::empty(py).into_object(py);
let _empty3: &PyAny = py.from_owned_ptr(ffi::PyTuple_New(0));
}
}
@ -110,9 +110,10 @@ fn create_pointers_in_drop() {
{
let gil = Python::acquire_gil();
let py = gil.python();
let empty = PyTuple::empty(py);
let empty = PyTuple::empty(py).into_object(py);
ptr = empty.as_ptr();
cnt = empty.get_refcnt() - 1;
// substract 2, because `PyTuple::empty(py).into_object(py)` increases the refcnt by 2
cnt = empty.get_refcnt() - 2;
let inst = Py::new(py, ClassWithDrop {}).unwrap();
drop(inst);
}

View file

@ -129,9 +129,10 @@ impl PickleSupport {
obj.init({ PickleSupport {} });
}
pub fn __reduce__(slf: PyRef<Self>) -> PyResult<(PyObject, Py<PyTuple>, PyObject)> {
let gil = Python::acquire_gil();
let py = gil.python();
pub fn __reduce__<'py>(
slf: PyRef<Self>,
py: Python<'py>,
) -> PyResult<(PyObject, &'py PyTuple, PyObject)> {
let cls = slf.to_object(py).getattr(py, "__class__")?;
let dict = slf.to_object(py).getattr(py, "__dict__")?;
Ok((cls, PyTuple::empty(py), dict))