1use crate::call::PyCallArgs;
2use crate::class::basic::CompareOp;
3use crate::conversion::{FromPyObject, IntoPyObject};
4use crate::err::{error_on_minusone, PyErr, PyResult};
5use crate::exceptions::PyTypeError;
6use crate::ffi_ptr_ext::FfiPtrExt;
7#[cfg(not(all(Py_LIMITED_API, Py_GIL_DISABLED)))]
8use crate::impl_::pycell::{PyClassObjectBase, PyStaticClassObject};
9use crate::instance::Bound;
10use crate::internal::get_slot::TP_DESCR_GET;
11use crate::py_result_ext::PyResultExt;
12use crate::type_object::{PyTypeCheck, PyTypeInfo};
13use crate::types::PySuper;
14use crate::types::{PyDict, PyIterator, PyList, PyString, PyType};
15use crate::{err, ffi, Borrowed, BoundObject, IntoPyObjectExt, Py};
16#[cfg(RustPython)]
17use crate::{sync::PyOnceLock, types::typeobject::PyTypeMethods};
18use core::cell::UnsafeCell;
19use core::cmp::Ordering;
20use core::ffi::c_int;
21use core::ptr;
22
23#[doc = concat!("[the guide](https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/types.html#concrete-python-types)")]
33#[repr(transparent)]
35pub struct PyAny(UnsafeCell<ffi::PyObject>);
36
37#[allow(non_snake_case)]
38fn PyObject_Check(_: *mut ffi::PyObject) -> c_int {
41 1
42}
43
44#[cfg(not(RustPython))]
46pyobject_native_type_info!(
47 PyAny,
48 pyobject_native_static_type_object!(ffi::PyBaseObject_Type),
49 "typing",
50 "Any",
51 Some("builtins"),
52 #checkfunction=PyObject_Check
53);
54
55#[cfg(RustPython)]
56pyobject_native_type_info!(
57 PyAny,
58 |py| {
59 static TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
60 TYPE.import(py, "builtins", "object").unwrap().as_type_ptr()
61 },
62 "typing",
63 "Any",
64 Some("builtins"),
65 #checkfunction=PyObject_Check
66);
67
68pyobject_native_type_sized!(PyAny, ffi::PyObject);
69#[cfg(not(all(Py_LIMITED_API, Py_GIL_DISABLED)))]
76impl crate::impl_::pyclass::PyClassBaseType for PyAny {
77 type LayoutAsBase = PyClassObjectBase<ffi::PyObject>;
78 type BaseNativeType = PyAny;
79 type Initializer = crate::impl_::pyclass_init::PyNativeTypeInitializer<Self>;
80 type PyClassMutability = crate::pycell::impl_::ImmutableClass;
81 type Layout<T: crate::impl_::pyclass::PyClassImpl> = PyStaticClassObject<T>;
82}
83
84#[cfg(all(Py_LIMITED_API, Py_GIL_DISABLED))]
85pyobject_subclassable_native_type!(PyAny, ffi::PyObject);
86
87#[doc(alias = "PyAny")]
92pub trait PyAnyMethods<'py>: crate::sealed::Sealed {
93 fn is<T: AsRef<Py<PyAny>>>(&self, other: T) -> bool;
98
99 fn hasattr<N>(&self, attr_name: N) -> PyResult<bool>
122 where
123 N: IntoPyObject<'py, Target = PyString>;
124
125 fn getattr<N>(&self, attr_name: N) -> PyResult<Bound<'py, PyAny>>
148 where
149 N: IntoPyObject<'py, Target = PyString>;
150
151 fn getattr_opt<N>(&self, attr_name: N) -> PyResult<Option<Bound<'py, PyAny>>>
180 where
181 N: IntoPyObject<'py, Target = PyString>;
182
183 fn setattr<N, V>(&self, attr_name: N, value: V) -> PyResult<()>
206 where
207 N: IntoPyObject<'py, Target = PyString>,
208 V: IntoPyObject<'py>;
209
210 fn delattr<N>(&self, attr_name: N) -> PyResult<()>
217 where
218 N: IntoPyObject<'py, Target = PyString>;
219
220 fn compare<O>(&self, other: O) -> PyResult<Ordering>
267 where
268 O: IntoPyObject<'py>;
269
270 fn rich_compare<O>(&self, other: O, compare_op: CompareOp) -> PyResult<Bound<'py, PyAny>>
304 where
305 O: IntoPyObject<'py>;
306
307 fn neg(&self) -> PyResult<Bound<'py, PyAny>>;
311
312 fn pos(&self) -> PyResult<Bound<'py, PyAny>>;
316
317 fn abs(&self) -> PyResult<Bound<'py, PyAny>>;
321
322 fn bitnot(&self) -> PyResult<Bound<'py, PyAny>>;
324
325 fn lt<O>(&self, other: O) -> PyResult<bool>
329 where
330 O: IntoPyObject<'py>;
331
332 fn le<O>(&self, other: O) -> PyResult<bool>
336 where
337 O: IntoPyObject<'py>;
338
339 fn eq<O>(&self, other: O) -> PyResult<bool>
343 where
344 O: IntoPyObject<'py>;
345
346 fn ne<O>(&self, other: O) -> PyResult<bool>
350 where
351 O: IntoPyObject<'py>;
352
353 fn gt<O>(&self, other: O) -> PyResult<bool>
357 where
358 O: IntoPyObject<'py>;
359
360 fn ge<O>(&self, other: O) -> PyResult<bool>
364 where
365 O: IntoPyObject<'py>;
366
367 fn add<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
369 where
370 O: IntoPyObject<'py>;
371
372 fn sub<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
374 where
375 O: IntoPyObject<'py>;
376
377 fn mul<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
379 where
380 O: IntoPyObject<'py>;
381
382 fn matmul<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
384 where
385 O: IntoPyObject<'py>;
386
387 fn div<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
389 where
390 O: IntoPyObject<'py>;
391
392 fn floor_div<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
394 where
395 O: IntoPyObject<'py>;
396
397 fn rem<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
399 where
400 O: IntoPyObject<'py>;
401
402 fn divmod<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
404 where
405 O: IntoPyObject<'py>;
406
407 fn lshift<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
409 where
410 O: IntoPyObject<'py>;
411
412 fn rshift<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
414 where
415 O: IntoPyObject<'py>;
416
417 fn pow<O1, O2>(&self, other: O1, modulus: O2) -> PyResult<Bound<'py, PyAny>>
420 where
421 O1: IntoPyObject<'py>,
422 O2: IntoPyObject<'py>;
423
424 fn bitand<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
426 where
427 O: IntoPyObject<'py>;
428
429 fn bitor<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
431 where
432 O: IntoPyObject<'py>;
433
434 fn bitxor<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
436 where
437 O: IntoPyObject<'py>;
438
439 fn is_callable(&self) -> bool;
467
468 fn call<A>(&self, args: A, kwargs: Option<&Bound<'py, PyDict>>) -> PyResult<Bound<'py, PyAny>>
501 where
502 A: PyCallArgs<'py>;
503
504 fn call0(&self) -> PyResult<Bound<'py, PyAny>>;
525
526 fn call1<A>(&self, args: A) -> PyResult<Bound<'py, PyAny>>
556 where
557 A: PyCallArgs<'py>;
558
559 fn call_method<N, A>(
597 &self,
598 name: N,
599 args: A,
600 kwargs: Option<&Bound<'py, PyDict>>,
601 ) -> PyResult<Bound<'py, PyAny>>
602 where
603 N: IntoPyObject<'py, Target = PyString>,
604 A: PyCallArgs<'py>;
605
606 fn call_method0<N>(&self, name: N) -> PyResult<Bound<'py, PyAny>>
640 where
641 N: IntoPyObject<'py, Target = PyString>;
642
643 fn call_method1<N, A>(&self, name: N, args: A) -> PyResult<Bound<'py, PyAny>>
678 where
679 N: IntoPyObject<'py, Target = PyString>,
680 A: PyCallArgs<'py>;
681
682 fn is_truthy(&self) -> PyResult<bool>;
686
687 fn is_none(&self) -> bool;
691
692 fn is_empty(&self) -> PyResult<bool>;
696
697 fn get_item<K>(&self, key: K) -> PyResult<Bound<'py, PyAny>>
701 where
702 K: IntoPyObject<'py>;
703
704 fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>
708 where
709 K: IntoPyObject<'py>,
710 V: IntoPyObject<'py>;
711
712 fn del_item<K>(&self, key: K) -> PyResult<()>
716 where
717 K: IntoPyObject<'py>;
718
719 fn try_iter(&self) -> PyResult<Bound<'py, PyIterator>>;
744
745 fn get_type(&self) -> Bound<'py, PyType>;
747
748 fn get_type_ptr(&self) -> *mut ffi::PyTypeObject;
750
751 fn extract<'a, T>(&'a self) -> Result<T, T::Error>
755 where
756 T: FromPyObject<'a, 'py>;
757
758 #[deprecated(
760 since = "0.29.0",
761 note = "use `pyo3::ffi::Py_REFCNT(obj.as_ptr())` instead"
762 )]
763 fn get_refcnt(&self) -> isize;
764
765 fn repr(&self) -> PyResult<Bound<'py, PyString>>;
769
770 fn str(&self) -> PyResult<Bound<'py, PyString>>;
774
775 fn hash(&self) -> PyResult<isize>;
779
780 fn len(&self) -> PyResult<usize>;
784
785 fn dir(&self) -> PyResult<Bound<'py, PyList>>;
789
790 fn is_instance(&self, ty: &Bound<'py, PyAny>) -> PyResult<bool>;
794
795 fn is_exact_instance(&self, ty: &Bound<'py, PyAny>) -> bool;
799
800 fn is_instance_of<T: PyTypeCheck>(&self) -> bool;
805
806 fn is_exact_instance_of<T: PyTypeInfo>(&self) -> bool;
811
812 fn contains<V>(&self, value: V) -> PyResult<bool>
816 where
817 V: IntoPyObject<'py>;
818
819 fn py_super(&self) -> PyResult<Bound<'py, PySuper>>;
823}
824
825macro_rules! implement_binop {
826 ($name:ident, $c_api:ident, $op:expr) => {
827 #[doc = concat!("Computes `self ", $op, " other`.")]
828 fn $name<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
829 where
830 O: IntoPyObject<'py>,
831 {
832 fn inner<'py>(
833 any: &Bound<'py, PyAny>,
834 other: Borrowed<'_, 'py, PyAny>,
835 ) -> PyResult<Bound<'py, PyAny>> {
836 unsafe { ffi::$c_api(any.as_ptr(), other.as_ptr()).assume_owned_or_err(any.py()) }
837 }
838
839 let py = self.py();
840 inner(
841 self,
842 other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
843 )
844 }
845 };
846}
847
848impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
849 #[inline]
850 fn is<T: AsRef<Py<PyAny>>>(&self, other: T) -> bool {
851 ptr::eq(self.as_ptr(), other.as_ref().as_ptr())
852 }
853
854 fn hasattr<N>(&self, attr_name: N) -> PyResult<bool>
855 where
856 N: IntoPyObject<'py, Target = PyString>,
857 {
858 fn inner<'py>(
859 any: &Bound<'py, PyAny>,
860 attr_name: Borrowed<'_, '_, PyString>,
861 ) -> PyResult<bool> {
862 let result =
863 unsafe { ffi::compat::PyObject_HasAttrWithError(any.as_ptr(), attr_name.as_ptr()) };
864 error_on_minusone(any.py(), result)?;
865 Ok(result > 0)
866 }
867
868 inner(
869 self,
870 attr_name
871 .into_pyobject(self.py())
872 .map_err(Into::into)?
873 .as_borrowed(),
874 )
875 }
876
877 fn getattr<N>(&self, attr_name: N) -> PyResult<Bound<'py, PyAny>>
878 where
879 N: IntoPyObject<'py, Target = PyString>,
880 {
881 fn inner<'py>(
882 any: &Bound<'py, PyAny>,
883 attr_name: Borrowed<'_, '_, PyString>,
884 ) -> PyResult<Bound<'py, PyAny>> {
885 unsafe {
886 ffi::PyObject_GetAttr(any.as_ptr(), attr_name.as_ptr())
887 .assume_owned_or_err(any.py())
888 }
889 }
890
891 inner(
892 self,
893 attr_name
894 .into_pyobject(self.py())
895 .map_err(Into::into)?
896 .as_borrowed(),
897 )
898 }
899
900 fn getattr_opt<N>(&self, attr_name: N) -> PyResult<Option<Bound<'py, PyAny>>>
901 where
902 N: IntoPyObject<'py, Target = PyString>,
903 {
904 fn inner<'py>(
905 any: &Bound<'py, PyAny>,
906 attr_name: Borrowed<'_, 'py, PyString>,
907 ) -> PyResult<Option<Bound<'py, PyAny>>> {
908 let mut resp_ptr: *mut ffi::PyObject = core::ptr::null_mut();
909 match unsafe {
910 ffi::compat::PyObject_GetOptionalAttr(
911 any.as_ptr(),
912 attr_name.as_ptr(),
913 &mut resp_ptr,
914 )
915 } {
916 1 => Ok(Some(unsafe { Bound::from_owned_ptr(any.py(), resp_ptr) })),
918 0 => Ok(None),
920 _ => Err(PyErr::fetch(any.py())),
922 }
923 }
924
925 let py = self.py();
926 inner(self, attr_name.into_pyobject_or_pyerr(py)?.as_borrowed())
927 }
928
929 fn setattr<N, V>(&self, attr_name: N, value: V) -> PyResult<()>
930 where
931 N: IntoPyObject<'py, Target = PyString>,
932 V: IntoPyObject<'py>,
933 {
934 fn inner(
935 any: &Bound<'_, PyAny>,
936 attr_name: Borrowed<'_, '_, PyString>,
937 value: Borrowed<'_, '_, PyAny>,
938 ) -> PyResult<()> {
939 err::error_on_minusone(any.py(), unsafe {
940 ffi::PyObject_SetAttr(any.as_ptr(), attr_name.as_ptr(), value.as_ptr())
941 })
942 }
943
944 let py = self.py();
945 inner(
946 self,
947 attr_name.into_pyobject_or_pyerr(py)?.as_borrowed(),
948 value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
949 )
950 }
951
952 fn delattr<N>(&self, attr_name: N) -> PyResult<()>
953 where
954 N: IntoPyObject<'py, Target = PyString>,
955 {
956 fn inner(any: &Bound<'_, PyAny>, attr_name: Borrowed<'_, '_, PyString>) -> PyResult<()> {
957 err::error_on_minusone(any.py(), unsafe {
958 ffi::PyObject_DelAttr(any.as_ptr(), attr_name.as_ptr())
959 })
960 }
961
962 let py = self.py();
963 inner(self, attr_name.into_pyobject_or_pyerr(py)?.as_borrowed())
964 }
965
966 fn compare<O>(&self, other: O) -> PyResult<Ordering>
967 where
968 O: IntoPyObject<'py>,
969 {
970 fn inner(any: &Bound<'_, PyAny>, other: Borrowed<'_, '_, PyAny>) -> PyResult<Ordering> {
971 let other = other.as_ptr();
972 let do_compare = |other, op| unsafe {
975 ffi::PyObject_RichCompare(any.as_ptr(), other, op)
976 .assume_owned_or_err(any.py())
977 .and_then(|obj| obj.is_truthy())
978 };
979 if do_compare(other, ffi::Py_EQ)? {
980 Ok(Ordering::Equal)
981 } else if do_compare(other, ffi::Py_LT)? {
982 Ok(Ordering::Less)
983 } else if do_compare(other, ffi::Py_GT)? {
984 Ok(Ordering::Greater)
985 } else {
986 Err(PyTypeError::new_err(
987 "PyAny::compare(): All comparisons returned false",
988 ))
989 }
990 }
991
992 let py = self.py();
993 inner(
994 self,
995 other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
996 )
997 }
998
999 fn rich_compare<O>(&self, other: O, compare_op: CompareOp) -> PyResult<Bound<'py, PyAny>>
1000 where
1001 O: IntoPyObject<'py>,
1002 {
1003 fn inner<'py>(
1004 any: &Bound<'py, PyAny>,
1005 other: Borrowed<'_, 'py, PyAny>,
1006 compare_op: CompareOp,
1007 ) -> PyResult<Bound<'py, PyAny>> {
1008 unsafe {
1009 ffi::PyObject_RichCompare(any.as_ptr(), other.as_ptr(), compare_op as c_int)
1010 .assume_owned_or_err(any.py())
1011 }
1012 }
1013
1014 let py = self.py();
1015 inner(
1016 self,
1017 other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
1018 compare_op,
1019 )
1020 }
1021
1022 fn neg(&self) -> PyResult<Bound<'py, PyAny>> {
1023 unsafe { ffi::PyNumber_Negative(self.as_ptr()).assume_owned_or_err(self.py()) }
1024 }
1025
1026 fn pos(&self) -> PyResult<Bound<'py, PyAny>> {
1027 fn inner<'py>(any: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
1028 unsafe { ffi::PyNumber_Positive(any.as_ptr()).assume_owned_or_err(any.py()) }
1029 }
1030
1031 inner(self)
1032 }
1033
1034 fn abs(&self) -> PyResult<Bound<'py, PyAny>> {
1035 fn inner<'py>(any: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
1036 unsafe { ffi::PyNumber_Absolute(any.as_ptr()).assume_owned_or_err(any.py()) }
1037 }
1038
1039 inner(self)
1040 }
1041
1042 fn bitnot(&self) -> PyResult<Bound<'py, PyAny>> {
1043 fn inner<'py>(any: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
1044 unsafe { ffi::PyNumber_Invert(any.as_ptr()).assume_owned_or_err(any.py()) }
1045 }
1046
1047 inner(self)
1048 }
1049
1050 fn lt<O>(&self, other: O) -> PyResult<bool>
1051 where
1052 O: IntoPyObject<'py>,
1053 {
1054 self.rich_compare(other, CompareOp::Lt)
1055 .and_then(|any| any.is_truthy())
1056 }
1057
1058 fn le<O>(&self, other: O) -> PyResult<bool>
1059 where
1060 O: IntoPyObject<'py>,
1061 {
1062 self.rich_compare(other, CompareOp::Le)
1063 .and_then(|any| any.is_truthy())
1064 }
1065
1066 fn eq<O>(&self, other: O) -> PyResult<bool>
1067 where
1068 O: IntoPyObject<'py>,
1069 {
1070 self.rich_compare(other, CompareOp::Eq)
1071 .and_then(|any| any.is_truthy())
1072 }
1073
1074 fn ne<O>(&self, other: O) -> PyResult<bool>
1075 where
1076 O: IntoPyObject<'py>,
1077 {
1078 self.rich_compare(other, CompareOp::Ne)
1079 .and_then(|any| any.is_truthy())
1080 }
1081
1082 fn gt<O>(&self, other: O) -> PyResult<bool>
1083 where
1084 O: IntoPyObject<'py>,
1085 {
1086 self.rich_compare(other, CompareOp::Gt)
1087 .and_then(|any| any.is_truthy())
1088 }
1089
1090 fn ge<O>(&self, other: O) -> PyResult<bool>
1091 where
1092 O: IntoPyObject<'py>,
1093 {
1094 self.rich_compare(other, CompareOp::Ge)
1095 .and_then(|any| any.is_truthy())
1096 }
1097
1098 implement_binop!(add, PyNumber_Add, "+");
1099 implement_binop!(sub, PyNumber_Subtract, "-");
1100 implement_binop!(mul, PyNumber_Multiply, "*");
1101 implement_binop!(matmul, PyNumber_MatrixMultiply, "@");
1102 implement_binop!(div, PyNumber_TrueDivide, "/");
1103 implement_binop!(floor_div, PyNumber_FloorDivide, "//");
1104 implement_binop!(rem, PyNumber_Remainder, "%");
1105 implement_binop!(lshift, PyNumber_Lshift, "<<");
1106 implement_binop!(rshift, PyNumber_Rshift, ">>");
1107 implement_binop!(bitand, PyNumber_And, "&");
1108 implement_binop!(bitor, PyNumber_Or, "|");
1109 implement_binop!(bitxor, PyNumber_Xor, "^");
1110
1111 fn divmod<O>(&self, other: O) -> PyResult<Bound<'py, PyAny>>
1113 where
1114 O: IntoPyObject<'py>,
1115 {
1116 fn inner<'py>(
1117 any: &Bound<'py, PyAny>,
1118 other: Borrowed<'_, 'py, PyAny>,
1119 ) -> PyResult<Bound<'py, PyAny>> {
1120 unsafe {
1121 ffi::PyNumber_Divmod(any.as_ptr(), other.as_ptr()).assume_owned_or_err(any.py())
1122 }
1123 }
1124
1125 let py = self.py();
1126 inner(
1127 self,
1128 other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
1129 )
1130 }
1131
1132 fn pow<O1, O2>(&self, other: O1, modulus: O2) -> PyResult<Bound<'py, PyAny>>
1135 where
1136 O1: IntoPyObject<'py>,
1137 O2: IntoPyObject<'py>,
1138 {
1139 fn inner<'py>(
1140 any: &Bound<'py, PyAny>,
1141 other: Borrowed<'_, 'py, PyAny>,
1142 modulus: Borrowed<'_, 'py, PyAny>,
1143 ) -> PyResult<Bound<'py, PyAny>> {
1144 unsafe {
1145 ffi::PyNumber_Power(any.as_ptr(), other.as_ptr(), modulus.as_ptr())
1146 .assume_owned_or_err(any.py())
1147 }
1148 }
1149
1150 let py = self.py();
1151 inner(
1152 self,
1153 other.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
1154 modulus.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
1155 )
1156 }
1157
1158 fn is_callable(&self) -> bool {
1159 unsafe { ffi::PyCallable_Check(self.as_ptr()) != 0 }
1160 }
1161
1162 fn call<A>(&self, args: A, kwargs: Option<&Bound<'py, PyDict>>) -> PyResult<Bound<'py, PyAny>>
1163 where
1164 A: PyCallArgs<'py>,
1165 {
1166 if let Some(kwargs) = kwargs {
1167 args.call(
1168 self.as_borrowed(),
1169 kwargs.as_borrowed(),
1170 crate::call::private::Token,
1171 )
1172 } else {
1173 args.call_positional(self.as_borrowed(), crate::call::private::Token)
1174 }
1175 }
1176
1177 #[inline]
1178 fn call0(&self) -> PyResult<Bound<'py, PyAny>> {
1179 unsafe { ffi::compat::PyObject_CallNoArgs(self.as_ptr()).assume_owned_or_err(self.py()) }
1180 }
1181
1182 fn call1<A>(&self, args: A) -> PyResult<Bound<'py, PyAny>>
1183 where
1184 A: PyCallArgs<'py>,
1185 {
1186 args.call_positional(self.as_borrowed(), crate::call::private::Token)
1187 }
1188
1189 #[inline]
1190 fn call_method<N, A>(
1191 &self,
1192 name: N,
1193 args: A,
1194 kwargs: Option<&Bound<'py, PyDict>>,
1195 ) -> PyResult<Bound<'py, PyAny>>
1196 where
1197 N: IntoPyObject<'py, Target = PyString>,
1198 A: PyCallArgs<'py>,
1199 {
1200 if kwargs.is_none() {
1201 self.call_method1(name, args)
1202 } else {
1203 self.getattr(name)
1204 .and_then(|method| method.call(args, kwargs))
1205 }
1206 }
1207
1208 #[inline]
1209 fn call_method0<N>(&self, name: N) -> PyResult<Bound<'py, PyAny>>
1210 where
1211 N: IntoPyObject<'py, Target = PyString>,
1212 {
1213 let py = self.py();
1214 let name = name.into_pyobject_or_pyerr(py)?.into_bound();
1215 unsafe {
1216 ffi::compat::PyObject_CallMethodNoArgs(self.as_ptr(), name.as_ptr())
1217 .assume_owned_or_err(py)
1218 }
1219 }
1220
1221 fn call_method1<N, A>(&self, name: N, args: A) -> PyResult<Bound<'py, PyAny>>
1222 where
1223 N: IntoPyObject<'py, Target = PyString>,
1224 A: PyCallArgs<'py>,
1225 {
1226 let name = name.into_pyobject_or_pyerr(self.py())?;
1227 args.call_method_positional(
1228 self.as_borrowed(),
1229 name.as_borrowed(),
1230 crate::call::private::Token,
1231 )
1232 }
1233
1234 fn is_truthy(&self) -> PyResult<bool> {
1235 let v = unsafe { ffi::PyObject_IsTrue(self.as_ptr()) };
1236 err::error_on_minusone(self.py(), v)?;
1237 Ok(v != 0)
1238 }
1239
1240 #[inline]
1241 fn is_none(&self) -> bool {
1242 unsafe { ptr::eq(ffi::Py_None(), self.as_ptr()) }
1243 }
1244
1245 fn is_empty(&self) -> PyResult<bool> {
1246 self.len().map(|l| l == 0)
1247 }
1248
1249 fn get_item<K>(&self, key: K) -> PyResult<Bound<'py, PyAny>>
1250 where
1251 K: IntoPyObject<'py>,
1252 {
1253 fn inner<'py>(
1254 any: &Bound<'py, PyAny>,
1255 key: Borrowed<'_, 'py, PyAny>,
1256 ) -> PyResult<Bound<'py, PyAny>> {
1257 unsafe {
1258 ffi::PyObject_GetItem(any.as_ptr(), key.as_ptr()).assume_owned_or_err(any.py())
1259 }
1260 }
1261
1262 let py = self.py();
1263 inner(
1264 self,
1265 key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
1266 )
1267 }
1268
1269 fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>
1270 where
1271 K: IntoPyObject<'py>,
1272 V: IntoPyObject<'py>,
1273 {
1274 fn inner(
1275 any: &Bound<'_, PyAny>,
1276 key: Borrowed<'_, '_, PyAny>,
1277 value: Borrowed<'_, '_, PyAny>,
1278 ) -> PyResult<()> {
1279 err::error_on_minusone(any.py(), unsafe {
1280 ffi::PyObject_SetItem(any.as_ptr(), key.as_ptr(), value.as_ptr())
1281 })
1282 }
1283
1284 let py = self.py();
1285 inner(
1286 self,
1287 key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
1288 value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
1289 )
1290 }
1291
1292 fn del_item<K>(&self, key: K) -> PyResult<()>
1293 where
1294 K: IntoPyObject<'py>,
1295 {
1296 fn inner(any: &Bound<'_, PyAny>, key: Borrowed<'_, '_, PyAny>) -> PyResult<()> {
1297 err::error_on_minusone(any.py(), unsafe {
1298 ffi::PyObject_DelItem(any.as_ptr(), key.as_ptr())
1299 })
1300 }
1301
1302 let py = self.py();
1303 inner(
1304 self,
1305 key.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
1306 )
1307 }
1308
1309 fn try_iter(&self) -> PyResult<Bound<'py, PyIterator>> {
1310 PyIterator::from_object(self)
1311 }
1312
1313 fn get_type(&self) -> Bound<'py, PyType> {
1314 unsafe { PyType::from_borrowed_type_ptr(self.py(), ffi::Py_TYPE(self.as_ptr())) }
1315 }
1316
1317 #[inline]
1318 fn get_type_ptr(&self) -> *mut ffi::PyTypeObject {
1319 unsafe { ffi::Py_TYPE(self.as_ptr()) }
1320 }
1321
1322 fn extract<'a, T>(&'a self) -> Result<T, T::Error>
1323 where
1324 T: FromPyObject<'a, 'py>,
1325 {
1326 FromPyObject::extract(self.as_borrowed())
1327 }
1328
1329 fn get_refcnt(&self) -> isize {
1330 self._get_refcnt()
1331 }
1332
1333 fn repr(&self) -> PyResult<Bound<'py, PyString>> {
1334 unsafe {
1335 ffi::PyObject_Repr(self.as_ptr())
1336 .assume_owned_or_err(self.py())
1337 .cast_into_unchecked()
1338 }
1339 }
1340
1341 fn str(&self) -> PyResult<Bound<'py, PyString>> {
1342 unsafe {
1343 ffi::PyObject_Str(self.as_ptr())
1344 .assume_owned_or_err(self.py())
1345 .cast_into_unchecked()
1346 }
1347 }
1348
1349 fn hash(&self) -> PyResult<isize> {
1350 let v = unsafe { ffi::PyObject_Hash(self.as_ptr()) };
1351 crate::err::error_on_minusone(self.py(), v)?;
1352 Ok(v)
1353 }
1354
1355 fn len(&self) -> PyResult<usize> {
1356 let v = unsafe { ffi::PyObject_Size(self.as_ptr()) };
1357 crate::err::error_on_minusone(self.py(), v)?;
1358 Ok(v as usize)
1359 }
1360
1361 fn dir(&self) -> PyResult<Bound<'py, PyList>> {
1362 unsafe {
1363 ffi::PyObject_Dir(self.as_ptr())
1364 .assume_owned_or_err(self.py())
1365 .cast_into_unchecked()
1366 }
1367 }
1368
1369 #[inline]
1370 fn is_instance(&self, ty: &Bound<'py, PyAny>) -> PyResult<bool> {
1371 let result = unsafe { ffi::PyObject_IsInstance(self.as_ptr(), ty.as_ptr()) };
1372 err::error_on_minusone(self.py(), result)?;
1373 Ok(result == 1)
1374 }
1375
1376 #[inline]
1377 fn is_exact_instance(&self, ty: &Bound<'py, PyAny>) -> bool {
1378 self.get_type().is(ty)
1379 }
1380
1381 #[inline]
1382 fn is_instance_of<T: PyTypeCheck>(&self) -> bool {
1383 T::type_check(self)
1384 }
1385
1386 #[inline]
1387 fn is_exact_instance_of<T: PyTypeInfo>(&self) -> bool {
1388 T::is_exact_type_of(self)
1389 }
1390
1391 fn contains<V>(&self, value: V) -> PyResult<bool>
1392 where
1393 V: IntoPyObject<'py>,
1394 {
1395 fn inner(any: &Bound<'_, PyAny>, value: Borrowed<'_, '_, PyAny>) -> PyResult<bool> {
1396 match unsafe { ffi::PySequence_Contains(any.as_ptr(), value.as_ptr()) } {
1397 0 => Ok(false),
1398 1 => Ok(true),
1399 _ => Err(PyErr::fetch(any.py())),
1400 }
1401 }
1402
1403 let py = self.py();
1404 inner(
1405 self,
1406 value.into_pyobject_or_pyerr(py)?.into_any().as_borrowed(),
1407 )
1408 }
1409
1410 fn py_super(&self) -> PyResult<Bound<'py, PySuper>> {
1411 PySuper::new(&self.get_type(), self)
1412 }
1413}
1414
1415impl<'py> Bound<'py, PyAny> {
1416 #[allow(dead_code, reason = "currently only used with num-complex + abi3")]
1427 pub(crate) fn lookup_special<N>(&self, attr_name: N) -> PyResult<Option<Bound<'py, PyAny>>>
1428 where
1429 N: IntoPyObject<'py, Target = PyString>,
1430 {
1431 let py = self.py();
1432 let self_type = self.get_type();
1433 let attr = if let Ok(attr) = self_type.getattr(attr_name) {
1434 attr
1435 } else {
1436 return Ok(None);
1437 };
1438
1439 if let Some(descr_get) = attr.get_type().get_slot(TP_DESCR_GET) {
1441 unsafe {
1443 descr_get(attr.as_ptr(), self.as_ptr(), self_type.as_ptr())
1444 .assume_owned_or_err(py)
1445 .map(Some)
1446 }
1447 } else {
1448 Ok(Some(attr))
1449 }
1450 }
1451
1452 #[inline]
1453 pub(crate) fn _get_refcnt(&self) -> isize {
1454 unsafe { ffi::Py_REFCNT(self.as_ptr()) }
1455 }
1456}
1457
1458#[cfg(test)]
1459mod tests {
1460 use crate::{
1461 basic::CompareOp,
1462 test_utils::generate_unique_module_name,
1463 types::{IntoPyDict, PyAny, PyAnyMethods, PyBool, PyInt, PyList, PyModule, PyTypeMethods},
1464 Bound, BoundObject, IntoPyObject, PyTypeInfo, Python,
1465 };
1466 use core::fmt::Debug;
1467 use pyo3_ffi::c_str;
1468
1469 #[test]
1470 fn test_lookup_special() {
1471 Python::attach(|py| {
1472 let module = PyModule::from_code(
1473 py,
1474 cr#"
1475class CustomCallable:
1476 def __call__(self):
1477 return 1
1478
1479class SimpleInt:
1480 def __int__(self):
1481 return 1
1482
1483class InheritedInt(SimpleInt): pass
1484
1485class NoInt: pass
1486
1487class NoDescriptorInt:
1488 __int__ = CustomCallable()
1489
1490class InstanceOverrideInt:
1491 def __int__(self):
1492 return 1
1493instance_override = InstanceOverrideInt()
1494instance_override.__int__ = lambda self: 2
1495
1496class ErrorInDescriptorInt:
1497 @property
1498 def __int__(self):
1499 raise ValueError("uh-oh!")
1500
1501class NonHeapNonDescriptorInt:
1502 # A static-typed callable that doesn't implement `__get__`. These are pretty hard to come by.
1503 __int__ = int
1504 "#,
1505 c"test.py",
1506 &generate_unique_module_name("test"),
1507 )
1508 .unwrap();
1509
1510 let int = crate::intern!(py, "__int__");
1511 let eval_int =
1512 |obj: Bound<'_, PyAny>| obj.lookup_special(int)?.unwrap().call0()?.extract::<u32>();
1513
1514 let simple = module.getattr("SimpleInt").unwrap().call0().unwrap();
1515 assert_eq!(eval_int(simple).unwrap(), 1);
1516 let inherited = module.getattr("InheritedInt").unwrap().call0().unwrap();
1517 assert_eq!(eval_int(inherited).unwrap(), 1);
1518 let no_descriptor = module.getattr("NoDescriptorInt").unwrap().call0().unwrap();
1519 assert_eq!(eval_int(no_descriptor).unwrap(), 1);
1520 let missing = module.getattr("NoInt").unwrap().call0().unwrap();
1521 assert!(missing.lookup_special(int).unwrap().is_none());
1522 let instance_override = module.getattr("instance_override").unwrap();
1525 assert_eq!(eval_int(instance_override).unwrap(), 1);
1526 let descriptor_error = module
1527 .getattr("ErrorInDescriptorInt")
1528 .unwrap()
1529 .call0()
1530 .unwrap();
1531 assert!(descriptor_error.lookup_special(int).is_err());
1532 let nonheap_nondescriptor = module
1533 .getattr("NonHeapNonDescriptorInt")
1534 .unwrap()
1535 .call0()
1536 .unwrap();
1537 assert_eq!(eval_int(nonheap_nondescriptor).unwrap(), 0);
1538 })
1539 }
1540
1541 #[test]
1542 fn test_getattr_opt() {
1543 Python::attach(|py| {
1544 let module = PyModule::from_code(
1545 py,
1546 cr#"
1547class Test:
1548 class_str_attribute = "class_string"
1549
1550 @property
1551 def error(self):
1552 raise ValueError("This is an intentional error")
1553 "#,
1554 c"test.py",
1555 &generate_unique_module_name("test"),
1556 )
1557 .unwrap();
1558
1559 let class_test = module.getattr_opt("Test").unwrap().unwrap();
1561
1562 let cls_attr_str = class_test
1564 .getattr_opt("class_str_attribute")
1565 .unwrap()
1566 .unwrap();
1567 assert_eq!(cls_attr_str.extract::<String>().unwrap(), "class_string");
1568
1569 let do_not_exist = class_test.getattr_opt("doNotExist").unwrap();
1571 assert!(do_not_exist.is_none());
1572
1573 let instance = class_test.call0().unwrap();
1575 let error = instance.getattr_opt("error");
1576 assert!(error.is_err());
1577 assert!(error
1578 .unwrap_err()
1579 .to_string()
1580 .contains("This is an intentional error"));
1581 });
1582 }
1583
1584 #[test]
1585 fn test_getattr_opt_attribute_error_subclass() {
1586 Python::attach(|py| {
1587 let module = PyModule::from_code(
1588 py,
1589 cr#"
1590class CustomAttrError(AttributeError):
1591 pass
1592
1593class Obj:
1594 @property
1595 def missing(self):
1596 raise CustomAttrError("not here")
1597 "#,
1598 c"test.py",
1599 &generate_unique_module_name("test"),
1600 )
1601 .unwrap();
1602
1603 let obj = module.getattr("Obj").unwrap().call0().unwrap();
1604
1605 let result = obj.getattr_opt("missing").unwrap();
1607 assert!(result.is_none());
1608 });
1609 }
1610
1611 #[test]
1612 fn test_call_for_non_existing_method() {
1613 Python::attach(|py| {
1614 let a = py.eval(c"42", None, None).unwrap();
1615 a.call_method0("__str__").unwrap(); assert!(a.call_method("nonexistent_method", (1,), None).is_err());
1617 assert!(a.call_method0("nonexistent_method").is_err());
1618 assert!(a.call_method1("nonexistent_method", (1,)).is_err());
1619 });
1620 }
1621
1622 #[test]
1623 fn test_call_with_kwargs() {
1624 Python::attach(|py| {
1625 let list = vec![3, 6, 5, 4, 7].into_pyobject(py).unwrap();
1626 let dict = vec![("reverse", true)].into_py_dict(py).unwrap();
1627 list.call_method("sort", (), Some(&dict)).unwrap();
1628 assert_eq!(list.extract::<Vec<i32>>().unwrap(), vec![7, 6, 5, 4, 3]);
1629 });
1630 }
1631
1632 #[test]
1633 fn test_call_method0() {
1634 Python::attach(|py| {
1635 let module = PyModule::from_code(
1636 py,
1637 cr#"
1638class SimpleClass:
1639 def foo(self):
1640 return 42
1641"#,
1642 c_str!(file!()),
1643 &generate_unique_module_name("test_module"),
1644 )
1645 .expect("module creation failed");
1646
1647 let simple_class = module.getattr("SimpleClass").unwrap().call0().unwrap();
1648 assert_eq!(
1649 simple_class
1650 .call_method0("foo")
1651 .unwrap()
1652 .extract::<u32>()
1653 .unwrap(),
1654 42
1655 );
1656 })
1657 }
1658
1659 #[test]
1660 fn test_type() {
1661 Python::attach(|py| {
1662 let obj = py.eval(c"42", None, None).unwrap();
1663 assert_eq!(obj.get_type().as_type_ptr(), obj.get_type_ptr());
1664 });
1665 }
1666
1667 #[test]
1668 fn test_dir() {
1669 Python::attach(|py| {
1670 let obj = py.eval(c"42", None, None).unwrap();
1671 let dir = py
1672 .eval(c"dir(42)", None, None)
1673 .unwrap()
1674 .cast_into::<PyList>()
1675 .unwrap();
1676 let a = obj
1677 .dir()
1678 .unwrap()
1679 .into_iter()
1680 .map(|x| x.extract::<String>().unwrap());
1681 let b = dir.into_iter().map(|x| x.extract::<String>().unwrap());
1682 assert!(a.eq(b));
1683 });
1684 }
1685
1686 #[test]
1687 fn test_hasattr() {
1688 Python::attach(|py| {
1689 let x = 5i32.into_pyobject(py).unwrap();
1690 assert!(x.is_instance_of::<PyInt>());
1691
1692 assert!(x.hasattr("to_bytes").unwrap());
1693 assert!(!x.hasattr("bbbbbbytes").unwrap());
1694 })
1695 }
1696
1697 #[cfg(feature = "macros")]
1698 #[test]
1699 #[allow(unknown_lints, non_local_definitions)]
1700 fn test_hasattr_error() {
1701 use crate::exceptions::PyValueError;
1702 use crate::prelude::*;
1703
1704 #[pyclass(crate = "crate")]
1705 struct GetattrFail;
1706
1707 #[pymethods(crate = "crate")]
1708 impl GetattrFail {
1709 fn __getattr__(&self, attr: Py<PyAny>) -> PyResult<Py<PyAny>> {
1710 Err(PyValueError::new_err(attr))
1711 }
1712 }
1713
1714 Python::attach(|py| {
1715 let obj = Py::new(py, GetattrFail).unwrap();
1716 let obj = obj.bind(py).as_any();
1717
1718 assert!(obj
1719 .hasattr("foo")
1720 .unwrap_err()
1721 .is_instance_of::<PyValueError>(py));
1722 })
1723 }
1724
1725 #[test]
1726 fn test_nan_eq() {
1727 Python::attach(|py| {
1728 let nan = py.eval(c"float('nan')", None, None).unwrap();
1729 assert!(nan.compare(&nan).is_err());
1730 });
1731 }
1732
1733 #[test]
1734 fn test_any_is_instance_of() {
1735 Python::attach(|py| {
1736 let x = 5i32.into_pyobject(py).unwrap();
1737 assert!(x.is_instance_of::<PyInt>());
1738
1739 let l = vec![&x, &x].into_pyobject(py).unwrap();
1740 assert!(l.is_instance_of::<PyList>());
1741 });
1742 }
1743
1744 #[test]
1745 fn test_any_is_instance() {
1746 Python::attach(|py| {
1747 let l = vec![1i8, 2].into_pyobject(py).unwrap();
1748 assert!(l.is_instance(&py.get_type::<PyList>()).unwrap());
1749 });
1750 }
1751
1752 #[test]
1753 fn test_any_is_exact_instance_of() {
1754 Python::attach(|py| {
1755 let x = 5i32.into_pyobject(py).unwrap();
1756 assert!(x.is_exact_instance_of::<PyInt>());
1757
1758 let t = PyBool::new(py, true);
1759 assert!(t.is_instance_of::<PyInt>());
1760 assert!(!t.is_exact_instance_of::<PyInt>());
1761 assert!(t.is_exact_instance_of::<PyBool>());
1762
1763 let l = vec![&x, &x].into_pyobject(py).unwrap();
1764 assert!(l.is_exact_instance_of::<PyList>());
1765 });
1766 }
1767
1768 #[test]
1769 fn test_any_is_exact_instance() {
1770 Python::attach(|py| {
1771 let t = PyBool::new(py, true);
1772 assert!(t.is_instance(&py.get_type::<PyInt>()).unwrap());
1773 assert!(!t.is_exact_instance(&py.get_type::<PyInt>()));
1774 assert!(t.is_exact_instance(&py.get_type::<PyBool>()));
1775 });
1776 }
1777
1778 #[test]
1779 fn test_any_contains() {
1780 Python::attach(|py| {
1781 let v: Vec<i32> = vec![1, 1, 2, 3, 5, 8];
1782 let ob = v.into_pyobject(py).unwrap();
1783
1784 let bad_needle = 7i32.into_pyobject(py).unwrap();
1785 assert!(!ob.contains(&bad_needle).unwrap());
1786
1787 let good_needle = 8i32.into_pyobject(py).unwrap();
1788 assert!(ob.contains(&good_needle).unwrap());
1789
1790 let type_coerced_needle = 8f32.into_pyobject(py).unwrap();
1791 assert!(ob.contains(&type_coerced_needle).unwrap());
1792
1793 let n: u32 = 42;
1794 let bad_haystack = n.into_pyobject(py).unwrap();
1795 let irrelevant_needle = 0i32.into_pyobject(py).unwrap();
1796 assert!(bad_haystack.contains(&irrelevant_needle).is_err());
1797 });
1798 }
1799
1800 fn test_eq_methods_generic<'a, T>(list: &'a [T])
1802 where
1803 T: PartialEq + PartialOrd,
1804 for<'py> &'a T: IntoPyObject<'py>,
1805 for<'py> <&'a T as IntoPyObject<'py>>::Error: Debug,
1806 {
1807 Python::attach(|py| {
1808 for a in list {
1809 for b in list {
1810 let a_py = a.into_pyobject(py).unwrap().into_any().into_bound();
1811 let b_py = b.into_pyobject(py).unwrap().into_any().into_bound();
1812
1813 assert_eq!(
1814 a.lt(b),
1815 a_py.lt(&b_py).unwrap(),
1816 "{} < {} should be {}.",
1817 a_py,
1818 b_py,
1819 a.lt(b)
1820 );
1821 assert_eq!(
1822 a.le(b),
1823 a_py.le(&b_py).unwrap(),
1824 "{} <= {} should be {}.",
1825 a_py,
1826 b_py,
1827 a.le(b)
1828 );
1829 assert_eq!(
1830 a.eq(b),
1831 a_py.eq(&b_py).unwrap(),
1832 "{} == {} should be {}.",
1833 a_py,
1834 b_py,
1835 a.eq(b)
1836 );
1837 assert_eq!(
1838 a.ne(b),
1839 a_py.ne(&b_py).unwrap(),
1840 "{} != {} should be {}.",
1841 a_py,
1842 b_py,
1843 a.ne(b)
1844 );
1845 assert_eq!(
1846 a.gt(b),
1847 a_py.gt(&b_py).unwrap(),
1848 "{} > {} should be {}.",
1849 a_py,
1850 b_py,
1851 a.gt(b)
1852 );
1853 assert_eq!(
1854 a.ge(b),
1855 a_py.ge(&b_py).unwrap(),
1856 "{} >= {} should be {}.",
1857 a_py,
1858 b_py,
1859 a.ge(b)
1860 );
1861 }
1862 }
1863 });
1864 }
1865
1866 #[test]
1867 fn test_eq_methods_integers() {
1868 let ints = [-4, -4, 1, 2, 0, -100, 1_000_000];
1869 test_eq_methods_generic::<i32>(&ints);
1870 }
1871
1872 #[test]
1873 fn test_eq_methods_strings() {
1874 let strings = ["Let's", "test", "some", "eq", "methods"];
1875 test_eq_methods_generic::<&str>(&strings);
1876 }
1877
1878 #[test]
1879 fn test_eq_methods_floats() {
1880 let floats = [
1881 -1.0,
1882 2.5,
1883 0.0,
1884 3.0,
1885 core::f64::consts::PI,
1886 10.0,
1887 10.0 / 3.0,
1888 -1_000_000.0,
1889 ];
1890 test_eq_methods_generic::<f64>(&floats);
1891 }
1892
1893 #[test]
1894 fn test_eq_methods_bools() {
1895 let bools = [true, false];
1896 test_eq_methods_generic::<bool>(&bools);
1897 }
1898
1899 #[test]
1900 fn test_rich_compare_type_error() {
1901 Python::attach(|py| {
1902 let py_int = 1i32.into_pyobject(py).unwrap();
1903 let py_str = "1".into_pyobject(py).unwrap();
1904
1905 assert!(py_int.rich_compare(&py_str, CompareOp::Lt).is_err());
1906 assert!(!py_int
1907 .rich_compare(py_str, CompareOp::Eq)
1908 .unwrap()
1909 .is_truthy()
1910 .unwrap());
1911 })
1912 }
1913
1914 #[test]
1915 fn test_is_callable() {
1916 Python::attach(|py| {
1917 assert!(PyList::type_object(py).is_callable());
1918
1919 let not_callable = 5i32.into_pyobject(py).unwrap();
1920 assert!(!not_callable.is_callable());
1921 });
1922 }
1923
1924 #[test]
1925 fn test_is_empty() {
1926 Python::attach(|py| {
1927 let empty_list = PyList::empty(py).into_any();
1928 assert!(empty_list.is_empty().unwrap());
1929
1930 let list = PyList::new(py, vec![1, 2, 3]).unwrap().into_any();
1931 assert!(!list.is_empty().unwrap());
1932
1933 let not_container = 5i32.into_pyobject(py).unwrap();
1934 assert!(not_container.is_empty().is_err());
1935 });
1936 }
1937
1938 #[cfg(feature = "macros")]
1939 #[test]
1940 #[allow(unknown_lints, non_local_definitions)]
1941 fn test_fallible_dir() {
1942 use crate::exceptions::PyValueError;
1943 use crate::prelude::*;
1944
1945 #[pyclass(crate = "crate")]
1946 struct DirFail;
1947
1948 #[pymethods(crate = "crate")]
1949 impl DirFail {
1950 fn __dir__(&self) -> PyResult<Py<PyAny>> {
1951 Err(PyValueError::new_err("uh-oh!"))
1952 }
1953 }
1954
1955 Python::attach(|py| {
1956 let obj = Bound::new(py, DirFail).unwrap();
1957 assert!(obj.dir().unwrap_err().is_instance_of::<PyValueError>(py));
1958 })
1959 }
1960}