no need mut for PyErr::clone_ref; IntoPyTuple::to_tuple is not needed

This commit is contained in:
Nikolay Kim 2017-07-26 15:08:34 -07:00
parent fc3ab84bfa
commit e5c1fcf11d
3 changed files with 6 additions and 37 deletions

View File

@ -55,9 +55,6 @@ pub trait IntoPyObject {
/// Conversion trait that allows various objects to be converted into `PyTuple` object.
pub trait IntoPyTuple {
/// Converts self into a PyTuple object.
fn to_tuple(&self, py: Python) -> Py<PyTuple>;
/// Converts self into a PyTuple object.
fn into_tuple(self, py: Python) -> Py<PyTuple>;

View File

@ -395,11 +395,12 @@ impl PyErr {
}
}
pub fn clone_ref(&mut self, py: Python) -> PyErr {
&self.normalize(py);
pub fn clone_ref(&self, py: Python) -> PyErr {
let v = match self.pvalue {
PyErrValue::Value(ref instance) => PyErrValue::Value(instance.clone_ref(py)),
_ => PyErrValue::None,
PyErrValue::None => PyErrValue::None,
PyErrValue::Value(ref ob) => PyErrValue::Value(ob.clone_ref(py)),
PyErrValue::ToArgs(ref ob) => PyErrValue::Value(ob.arguments(py)),
PyErrValue::ToObject(ref ob) => PyErrValue::Value(ob.to_object(py)),
};
let t = if let Some(ref val) = self.ptraceback { Some(val.clone_ref(py))} else { None };

View File

@ -101,32 +101,18 @@ impl PyTuple {
}
impl<'a> IntoPyTuple for &'a PyTuple {
fn to_tuple(&self, _py: Python) -> Py<PyTuple> {
let t: Py<PyTuple> = (*self).into();
t
}
fn into_tuple(self, _py: Python) -> Py<PyTuple> {
self.into()
}
}
impl IntoPyTuple for Py<PyTuple> {
fn to_tuple(&self, py: Python) -> Py<PyTuple> {
self.clone_ref(py)
}
fn into_tuple(self, _py: Python) -> Py<PyTuple> {
self
}
}
impl<'a> IntoPyTuple for &'a str {
fn to_tuple(&self, py: Python) -> Py<PyTuple> {
unsafe {
let ptr = ffi::PyTuple_New(1);
ffi::PyTuple_SetItem(ptr, 0, ToPyObject::to_object(self, py).into_ptr());
Py::from_owned_ptr_or_panic(ptr)
}
}
fn into_tuple(self, py: Python) -> Py<PyTuple> {
unsafe {
let ptr = ffi::PyTuple_New(1);
@ -162,14 +148,7 @@ macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+
}
}
impl <$($T: ToPyObject + IntoPyObject),+> IntoPyTuple for ($($T,)+) {
fn to_tuple(&self, py: Python) -> Py<PyTuple> {
unsafe {
let ptr = ffi::PyTuple_New($length);
$(ffi::PyTuple_SetItem(ptr, $n, ToPyObject::to_object(&self.$n, py).into_ptr());)+;
Py::from_owned_ptr_or_panic(ptr)
}
}
impl <$($T: IntoPyObject),+> IntoPyTuple for ($($T,)+) {
fn into_tuple(self, py: Python) -> Py<PyTuple> {
unsafe {
let ptr = ffi::PyTuple_New($length);
@ -243,10 +222,6 @@ impl IntoPyObject for NoArgs {
/// Converts `NoArgs` to an empty Python tuple.
impl IntoPyTuple for NoArgs {
fn to_tuple(&self, py: Python) -> Py<PyTuple> {
PyTuple::empty(py)
}
fn into_tuple(self, py: Python) -> Py<PyTuple> {
PyTuple::empty(py)
}
@ -255,10 +230,6 @@ impl IntoPyTuple for NoArgs {
/// Converts `()` to an empty Python tuple.
impl IntoPyTuple for () {
fn to_tuple(&self, py: Python) -> Py<PyTuple> {
PyTuple::empty(py)
}
fn into_tuple(self, py: Python) -> Py<PyTuple> {
PyTuple::empty(py)
}