Skip to content

Commit 841408b

Browse files
committed
Tidy up examples and PR code
1 parent a566b8d commit 841408b

18 files changed

+97
-108
lines changed

examples/rustapi_module/src/datetime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn make_time<'p>(
3838
minute,
3939
second,
4040
microsecond,
41-
tzinfo.map(|o| o.to_object(py)),
41+
tzinfo.map(|o| o.as_ref()),
4242
)
4343
}
4444

@@ -59,7 +59,7 @@ fn time_with_fold<'p>(
5959
minute,
6060
second,
6161
microsecond,
62-
tzinfo.map(|o| o.to_object(py)),
62+
tzinfo.map(|o| o.as_ref()),
6363
fold,
6464
)
6565
}
@@ -135,7 +135,7 @@ fn make_datetime<'p>(
135135
minute,
136136
second,
137137
microsecond,
138-
tzinfo.map(|o| (o.to_object(py))),
138+
tzinfo.map(|o| o.as_ref()),
139139
)
140140
}
141141

examples/rustapi_module/src/objstore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ impl ObjStore {
1313
ObjStore::default()
1414
}
1515

16-
fn push(&mut self, py: Python, obj: &PyObject) {
17-
self.obj.push(obj.to_object(py).into());
16+
fn push(&mut self, obj: &PyObject) {
17+
self.obj.push(obj.into());
1818
}
1919
}
2020

src/derive_utils.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
use crate::err::{PyErr, PyResult};
88
use crate::exceptions::TypeError;
9-
use crate::instance::PyNativeType;
109
use crate::pyclass::PyClass;
1110
use crate::pyclass_init::PyClassInitializer;
1211
use crate::types::{PyDict, PyModule, PyObject, PyTuple};
@@ -98,9 +97,7 @@ pub fn parse_fn_args<'p>(
9897
}
9998
// Adjust the remaining args
10099
let args = if accept_args {
101-
let py = args.py();
102-
let slice = args.slice(used_args as isize, nargs as isize);
103-
py.checked_cast_as(slice.as_ref().into()).unwrap()
100+
args.slice(used_args as isize, nargs as isize)
104101
} else {
105102
args
106103
};

src/ffi/setobject.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ pub unsafe fn PyFrozenSet_CheckExact(ob: *mut PyObject) -> c_int {
4141
}
4242

4343
#[inline]
44-
#[cfg_attr(PyPy, link_name = "PyPyObjectSet_CheckExact")]
45-
pub unsafe fn PyObjectSet_CheckExact(ob: *mut PyObject) -> c_int {
44+
#[cfg_attr(PyPy, link_name = "PyPyAnySet_CheckExact")]
45+
pub unsafe fn PyAnySet_CheckExact(ob: *mut PyObject) -> c_int {
4646
(Py_TYPE(ob) == &mut PySet_Type || Py_TYPE(ob) == &mut PyFrozenSet_Type) as c_int
4747
}
4848

4949
#[inline]
50-
pub unsafe fn PyObjectSet_Check(ob: *mut PyObject) -> c_int {
51-
(PyObjectSet_CheckExact(ob) != 0
50+
pub unsafe fn PyAnySet_Check(ob: *mut PyObject) -> c_int {
51+
(PyAnySet_CheckExact(ob) != 0
5252
|| PyType_IsSubtype(Py_TYPE(ob), &mut PySet_Type) != 0
5353
|| PyType_IsSubtype(Py_TYPE(ob), &mut PyFrozenSet_Type) != 0) as c_int
5454
}

src/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<T> Py<T> {
278278
// }
279279
}
280280

281-
/// Retrieves `&'py` types from `Py<T>` or `Py<PyObject>`.
281+
/// Retrieves `&'py` types from `Py<T>`.
282282
///
283283
/// # Examples
284284
/// `Py<T>::as_ref` returns `&PyDict`, `&PyList` or so for native types, and `&PyCell<T>`

src/objectprotocol.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,14 @@ mod test {
497497
use super::*;
498498
use crate::types::{IntoPyDict, PyString};
499499
use crate::Python;
500-
use crate::{PyTryFrom, ToPyObject};
500+
use crate::ToPyObject;
501501

502502
#[test]
503503
fn test_debug_string() {
504504
let gil = Python::acquire_gil();
505505
let py = gil.python();
506506
let v = "Hello\n".to_object(py);
507-
let s = <PyString as PyTryFrom>::try_from(v).unwrap();
507+
let s: &PyString = v.downcast().unwrap();
508508
assert_eq!(format!("{:?}", s), "'Hello\\n'");
509509
}
510510

@@ -513,7 +513,7 @@ mod test {
513513
let gil = Python::acquire_gil();
514514
let py = gil.python();
515515
let v = "Hello\n".to_object(py);
516-
let s = <PyString as PyTryFrom>::try_from(v).unwrap();
516+
let s: &PyString = v.downcast().unwrap();
517517
assert_eq!(format!("{}", s), "Hello\n");
518518
}
519519

src/python.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ impl<'p> Python<'p> {
290290
/// Registers the object in the release pool, and tries to downcast to specific type.
291291
pub fn checked_cast_as<T>(self, obj: Py<PyObject>) -> Result<&'p T, PyDowncastError>
292292
where
293-
T: PyTryFrom<'p>,
293+
T: for<'py> PyTryFrom<'py>,
294294
{
295295
let obj = unsafe { gil::register_owned(self, obj.into_non_null()) };
296-
<T as PyTryFrom>::try_from(obj)
296+
obj.downcast()
297297
}
298298

299299
/// Registers the object in the release pool, and does an unchecked downcast

src/types/boolobject.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2017-present PyO3 Project and Contributors
22
use crate::{
3-
ffi, AsPyPointer, FromPy, FromPyObject, IntoPy, Py, PyObject, PyResult, PyTryFrom, Python,
4-
ToPyObject,
3+
ffi, AsPyPointer, FromPy, FromPyObject, IntoPy, Py, PyObject, PyResult, Python, ToPyObject,
54
};
65

76
/// Represents a Python `bool`.
@@ -50,7 +49,7 @@ impl FromPy<bool> for Py<PyObject> {
5049
/// Fails with `TypeError` if the input is not a Python `bool`.
5150
impl<'source> FromPyObject<'source> for bool {
5251
fn extract(obj: &'source PyObject) -> PyResult<Self> {
53-
Ok(<PyBool as PyTryFrom>::try_from(obj)?.is_true())
52+
Ok(obj.downcast::<PyBool>()?.is_true())
5453
}
5554
}
5655

src/types/bytes.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::{
2-
ffi, AsPyPointer, FromPy, FromPyObject, IntoPy, Py, PyObject, PyResult, PyTryFrom, Python,
3-
};
1+
use crate::{ffi, AsPyPointer, FromPy, FromPyObject, IntoPy, Py, PyObject, PyResult, Python};
42
use std::ops::Index;
53
use std::os::raw::c_char;
64
use std::slice::SliceIndex;
@@ -64,7 +62,7 @@ impl<'a> FromPy<&'a [u8]> for Py<PyObject> {
6462

6563
impl<'a> FromPyObject<'a> for &'a [u8] {
6664
fn extract(obj: &'a PyObject) -> PyResult<Self> {
67-
Ok(<PyBytes as PyTryFrom>::try_from(obj)?.as_bytes())
65+
Ok(obj.downcast::<PyBytes>()?.as_bytes())
6866
}
6967
}
7068
#[cfg(test)]

src/types/datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl PyDateTime {
168168
let timestamp = timestamp.to_object(py);
169169

170170
let time_zone_info = match time_zone_info {
171-
Some(time_zone_info) => time_zone_info.to_object(py),
171+
Some(time_zone_info) => time_zone_info.as_ref(),
172172
None => py.None(),
173173
};
174174

src/types/dict.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use crate::err::{self, PyErr, PyResult};
44
use crate::instance::PyNativeType;
55
use crate::types::{PyList, PyObject};
66
use crate::AsPyPointer;
7+
use crate::FromPyObject;
78
#[cfg(not(PyPy))]
89
use crate::IntoPyPointer;
910
use crate::Python;
1011
use crate::{ffi, IntoPy, Py};
11-
use crate::{FromPyObject, PyTryFrom};
1212
use crate::{ToBorrowedObject, ToPyObject};
1313
use std::collections::{BTreeMap, HashMap};
1414
use std::{cmp, collections, hash};
@@ -324,7 +324,7 @@ where
324324
S: hash::BuildHasher + Default,
325325
{
326326
fn extract(ob: &'source PyObject) -> Result<Self, PyErr> {
327-
let dict = <PyDict as PyTryFrom>::try_from(ob)?;
327+
let dict: &PyDict = ob.downcast()?;
328328
let mut ret = HashMap::default();
329329
for (k, v) in dict.iter() {
330330
ret.insert(K::extract(k)?, V::extract(v)?);
@@ -339,7 +339,7 @@ where
339339
V: FromPyObject<'source>,
340340
{
341341
fn extract(ob: &'source PyObject) -> Result<Self, PyErr> {
342-
let dict = <PyDict as PyTryFrom>::try_from(ob)?;
342+
let dict: &PyDict = ob.downcast()?;
343343
let mut ret = BTreeMap::new();
344344
for (k, v) in dict.iter() {
345345
ret.insert(K::extract(k)?, V::extract(v)?);
@@ -354,7 +354,7 @@ mod test {
354354
use crate::types::{PyDict, PyList, PyTuple};
355355
use crate::ObjectProtocol;
356356
use crate::Python;
357-
use crate::{PyTryFrom, ToPyObject};
357+
use crate::ToPyObject;
358358
use std::collections::{BTreeMap, HashMap};
359359

360360
#[test]
@@ -409,11 +409,11 @@ mod test {
409409
let py = gil.python();
410410
let mut v = HashMap::new();
411411
let ob = v.to_object(py);
412-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
412+
let dict: &PyDict = ob.downcast().unwrap();
413413
assert_eq!(0, dict.len());
414414
v.insert(7, 32);
415415
let ob = v.to_object(py);
416-
let dict2 = <PyDict as PyTryFrom>::try_from(ob).unwrap();
416+
let dict2: &PyDict = ob.downcast().unwrap();
417417
assert_eq!(1, dict2.len());
418418
}
419419

@@ -424,7 +424,7 @@ mod test {
424424
let mut v = HashMap::new();
425425
v.insert(7, 32);
426426
let ob = v.to_object(py);
427-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
427+
let dict: &PyDict = ob.downcast().unwrap();
428428
assert_eq!(true, dict.contains(7i32).unwrap());
429429
assert_eq!(false, dict.contains(8i32).unwrap());
430430
}
@@ -436,7 +436,7 @@ mod test {
436436
let mut v = HashMap::new();
437437
v.insert(7, 32);
438438
let ob = v.to_object(py);
439-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
439+
let dict: &PyDict = ob.downcast().unwrap();
440440
assert_eq!(32, dict.get_item(7i32).unwrap().extract::<i32>().unwrap());
441441
assert_eq!(None, dict.get_item(8i32));
442442
}
@@ -448,7 +448,7 @@ mod test {
448448
let mut v = HashMap::new();
449449
v.insert(7, 32);
450450
let ob = v.to_object(py);
451-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
451+
let dict: &PyDict = ob.downcast().unwrap();
452452
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
453453
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
454454
assert_eq!(
@@ -485,7 +485,7 @@ mod test {
485485
let mut v = HashMap::new();
486486
v.insert(7, 32);
487487
let ob = v.to_object(py);
488-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
488+
let dict: &PyDict = ob.downcast().unwrap();
489489
assert!(dict.set_item(7i32, 42i32).is_ok()); // change
490490
assert!(dict.set_item(8i32, 123i32).is_ok()); // insert
491491
assert_eq!(32i32, v[&7i32]); // not updated!
@@ -499,7 +499,7 @@ mod test {
499499
let mut v = HashMap::new();
500500
v.insert(7, 32);
501501
let ob = v.to_object(py);
502-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
502+
let dict: &PyDict = ob.downcast().unwrap();
503503
assert!(dict.del_item(7i32).is_ok());
504504
assert_eq!(0, dict.len());
505505
assert_eq!(None, dict.get_item(7i32));
@@ -512,7 +512,7 @@ mod test {
512512
let mut v = HashMap::new();
513513
v.insert(7, 32);
514514
let ob = v.to_object(py);
515-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
515+
let dict: &PyDict = ob.downcast().unwrap();
516516
assert!(dict.del_item(7i32).is_ok()); // change
517517
assert_eq!(32i32, *v.get(&7i32).unwrap()); // not updated!
518518
}
@@ -526,7 +526,7 @@ mod test {
526526
v.insert(8, 42);
527527
v.insert(9, 123);
528528
let ob = v.to_object(py);
529-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
529+
let dict: &PyDict = ob.downcast().unwrap();
530530
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
531531
let mut key_sum = 0;
532532
let mut value_sum = 0;
@@ -548,7 +548,7 @@ mod test {
548548
v.insert(8, 42);
549549
v.insert(9, 123);
550550
let ob = v.to_object(py);
551-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
551+
let dict: &PyDict = ob.downcast().unwrap();
552552
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
553553
let mut key_sum = 0;
554554
for el in dict.keys().iter() {
@@ -566,7 +566,7 @@ mod test {
566566
v.insert(8, 42);
567567
v.insert(9, 123);
568568
let ob = v.to_object(py);
569-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
569+
let dict: &PyDict = ob.downcast().unwrap();
570570
// Can't just compare against a vector of tuples since we don't have a guaranteed ordering.
571571
let mut values_sum = 0;
572572
for el in dict.values().iter() {
@@ -584,7 +584,7 @@ mod test {
584584
v.insert(8, 42);
585585
v.insert(9, 123);
586586
let ob = v.to_object(py);
587-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
587+
let dict: &PyDict = ob.downcast().unwrap();
588588
let mut key_sum = 0;
589589
let mut value_sum = 0;
590590
for (key, value) in dict.iter() {
@@ -604,7 +604,7 @@ mod test {
604604
v.insert(8, 42);
605605
v.insert(9, 123);
606606
let ob = v.to_object(py);
607-
let dict = <PyDict as PyTryFrom>::try_from(ob).unwrap();
607+
let dict: &PyDict = ob.downcast().unwrap();
608608
let mut key_sum = 0;
609609
let mut value_sum = 0;
610610
for (key, value) in dict {
@@ -624,7 +624,7 @@ mod test {
624624
map.insert(1, 1);
625625

626626
let m = map.to_object(py);
627-
let py_map = <PyDict as PyTryFrom>::try_from(m).unwrap();
627+
let py_map: &PyDict = m.downcast().unwrap();
628628

629629
assert!(py_map.len() == 1);
630630
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
@@ -640,7 +640,7 @@ mod test {
640640
map.insert(1, 1);
641641

642642
let m = map.to_object(py);
643-
let py_map = <PyDict as PyTryFrom>::try_from(m).unwrap();
643+
let py_map: &PyDict = m.downcast().unwrap();
644644

645645
assert!(py_map.len() == 1);
646646
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
@@ -656,7 +656,7 @@ mod test {
656656
map.insert(1, 1);
657657

658658
let m = map.to_object(py);
659-
let py_map = <PyDict as PyTryFrom>::try_from(m).unwrap();
659+
let py_map: &PyDict = m.downcast().unwrap();
660660

661661
assert!(py_map.len() == 1);
662662
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);
@@ -685,7 +685,7 @@ mod test {
685685
map.insert(1, 1);
686686

687687
let m = map.to_object(py);
688-
let py_map = <PyDict as PyTryFrom>::try_from(m).unwrap();
688+
let py_map: &PyDict = m.downcast().unwrap();
689689

690690
assert!(py_map.len() == 1);
691691
assert!(py_map.get_item(1).unwrap().extract::<i32>().unwrap() == 1);

0 commit comments

Comments
 (0)