@@ -445,6 +445,30 @@ impl PyErr {
445
445
}
446
446
}
447
447
448
+ /// Deprecated form of [`PyErr::new_type_bound`]
449
+ #[ cfg_attr(
450
+ not( feature = "gil-refs" ) ,
451
+ deprecated(
452
+ since = "0.21.0" ,
453
+ note = "`PyErr::new_type` will be replaced by `PyErr::new_type_bound` in a future PyO3 version"
454
+ )
455
+ ) ]
456
+ pub fn new_type (
457
+ py : Python < ' _ > ,
458
+ name : & str ,
459
+ doc : Option < & str > ,
460
+ base : Option < & PyType > ,
461
+ dict : Option < PyObject > ,
462
+ ) -> PyResult < Py < PyType > > {
463
+ Self :: new_type_bound (
464
+ py,
465
+ name,
466
+ doc,
467
+ base. map ( PyNativeType :: as_borrowed) . as_deref ( ) ,
468
+ dict,
469
+ )
470
+ }
471
+
448
472
/// Creates a new exception type with the given name and docstring.
449
473
///
450
474
/// - `base` can be an existing exception type to subclass, or a tuple of classes.
@@ -459,11 +483,11 @@ impl PyErr {
459
483
/// # Panics
460
484
///
461
485
/// This function will panic if `name` or `doc` cannot be converted to [`CString`]s.
462
- pub fn new_type (
463
- py : Python < ' _ > ,
486
+ pub fn new_type_bound < ' py > (
487
+ py : Python < ' py > ,
464
488
name : & str ,
465
489
doc : Option < & str > ,
466
- base : Option < & PyType > ,
490
+ base : Option < & Bound < ' py , PyType > > ,
467
491
dict : Option < PyObject > ,
468
492
) -> PyResult < Py < PyType > > {
469
493
let base: * mut ffi:: PyObject = match base {
@@ -635,6 +659,18 @@ impl PyErr {
635
659
unsafe { ffi:: PyErr_WriteUnraisable ( obj. map_or ( std:: ptr:: null_mut ( ) , Bound :: as_ptr) ) }
636
660
}
637
661
662
+ /// Deprecated form of [`PyErr::warn_bound`].
663
+ #[ cfg_attr(
664
+ not( feature = "gil-refs" ) ,
665
+ deprecated(
666
+ since = "0.21.0" ,
667
+ note = "`PyErr::warn` will be replaced by `PyErr::warn_bound` in a future PyO3 version"
668
+ )
669
+ ) ]
670
+ pub fn warn ( py : Python < ' _ > , category : & PyAny , message : & str , stacklevel : i32 ) -> PyResult < ( ) > {
671
+ Self :: warn_bound ( py, & category. as_borrowed ( ) , message, stacklevel)
672
+ }
673
+
638
674
/// Issues a warning message.
639
675
///
640
676
/// May return an `Err(PyErr)` if warnings-as-errors is enabled.
@@ -650,13 +686,18 @@ impl PyErr {
650
686
/// # use pyo3::prelude::*;
651
687
/// # fn main() -> PyResult<()> {
652
688
/// Python::with_gil(|py| {
653
- /// let user_warning = py.get_type::<pyo3::exceptions::PyUserWarning>();
654
- /// PyErr::warn (py, user_warning, "I am warning you", 0)?;
689
+ /// let user_warning = py.get_type::<pyo3::exceptions::PyUserWarning>().as_borrowed() ;
690
+ /// PyErr::warn_bound (py, & user_warning, "I am warning you", 0)?;
655
691
/// Ok(())
656
692
/// })
657
693
/// # }
658
694
/// ```
659
- pub fn warn ( py : Python < ' _ > , category : & PyAny , message : & str , stacklevel : i32 ) -> PyResult < ( ) > {
695
+ pub fn warn_bound < ' py > (
696
+ py : Python < ' py > ,
697
+ category : & Bound < ' py , PyAny > ,
698
+ message : & str ,
699
+ stacklevel : i32 ,
700
+ ) -> PyResult < ( ) > {
660
701
let message = CString :: new ( message) ?;
661
702
error_on_minusone ( py, unsafe {
662
703
ffi:: PyErr_WarnEx (
@@ -667,6 +708,34 @@ impl PyErr {
667
708
} )
668
709
}
669
710
711
+ /// Deprecated form of [`PyErr::warn_explicit_bound`].
712
+ #[ cfg_attr(
713
+ not( feature = "gil-refs" ) ,
714
+ deprecated(
715
+ since = "0.21.0" ,
716
+ note = "`PyErr::warn_explicit` will be replaced by `PyErr::warn_explicit_bound` in a future PyO3 version"
717
+ )
718
+ ) ]
719
+ pub fn warn_explicit (
720
+ py : Python < ' _ > ,
721
+ category : & PyAny ,
722
+ message : & str ,
723
+ filename : & str ,
724
+ lineno : i32 ,
725
+ module : Option < & str > ,
726
+ registry : Option < & PyAny > ,
727
+ ) -> PyResult < ( ) > {
728
+ Self :: warn_explicit_bound (
729
+ py,
730
+ & category. as_borrowed ( ) ,
731
+ message,
732
+ filename,
733
+ lineno,
734
+ module,
735
+ registry. map ( PyNativeType :: as_borrowed) . as_deref ( ) ,
736
+ )
737
+ }
738
+
670
739
/// Issues a warning message, with more control over the warning attributes.
671
740
///
672
741
/// May return a `PyErr` if warnings-as-errors is enabled.
@@ -675,14 +744,14 @@ impl PyErr {
675
744
///
676
745
/// The `category` should be one of the `Warning` classes available in
677
746
/// [`pyo3::exceptions`](crate::exceptions), or a subclass.
678
- pub fn warn_explicit (
679
- py : Python < ' _ > ,
680
- category : & PyAny ,
747
+ pub fn warn_explicit_bound < ' py > (
748
+ py : Python < ' py > ,
749
+ category : & Bound < ' py , PyAny > ,
681
750
message : & str ,
682
751
filename : & str ,
683
752
lineno : i32 ,
684
753
module : Option < & str > ,
685
- registry : Option < & PyAny > ,
754
+ registry : Option < & Bound < ' py , PyAny > > ,
686
755
) -> PyResult < ( ) > {
687
756
let message = CString :: new ( message) ?;
688
757
let filename = CString :: new ( filename) ?;
@@ -975,7 +1044,7 @@ mod tests {
975
1044
use super :: PyErrState ;
976
1045
use crate :: exceptions:: { self , PyTypeError , PyValueError } ;
977
1046
use crate :: types:: any:: PyAnyMethods ;
978
- use crate :: { PyErr , PyTypeInfo , Python } ;
1047
+ use crate :: { PyErr , PyNativeType , PyTypeInfo , Python } ;
979
1048
980
1049
#[ test]
981
1050
fn no_error ( ) {
@@ -1172,7 +1241,7 @@ mod tests {
1172
1241
// GIL locked should prevent effects to be visible to other testing
1173
1242
// threads.
1174
1243
Python :: with_gil ( |py| {
1175
- let cls = py. get_type :: < exceptions:: PyUserWarning > ( ) ;
1244
+ let cls = py. get_type :: < exceptions:: PyUserWarning > ( ) . as_borrowed ( ) ;
1176
1245
1177
1246
// Reset warning filter to default state
1178
1247
let warnings = py. import_bound ( "warnings" ) . unwrap ( ) ;
@@ -1181,15 +1250,15 @@ mod tests {
1181
1250
// First, test the warning is emitted
1182
1251
assert_warnings ! (
1183
1252
py,
1184
- { PyErr :: warn ( py, cls, "I am warning you" , 0 ) . unwrap( ) } ,
1253
+ { PyErr :: warn_bound ( py, & cls, "I am warning you" , 0 ) . unwrap( ) } ,
1185
1254
[ ( exceptions:: PyUserWarning , "I am warning you" ) ]
1186
1255
) ;
1187
1256
1188
1257
// Test with raising
1189
1258
warnings
1190
1259
. call_method1 ( "simplefilter" , ( "error" , cls) )
1191
1260
. unwrap ( ) ;
1192
- PyErr :: warn ( py, cls, "I am warning you" , 0 ) . unwrap_err ( ) ;
1261
+ PyErr :: warn_bound ( py, & cls, "I am warning you" , 0 ) . unwrap_err ( ) ;
1193
1262
1194
1263
// Test with error for an explicit module
1195
1264
warnings. call_method0 ( "resetwarnings" ) . unwrap ( ) ;
@@ -1200,13 +1269,20 @@ mod tests {
1200
1269
// This has the wrong module and will not raise, just be emitted
1201
1270
assert_warnings ! (
1202
1271
py,
1203
- { PyErr :: warn ( py, cls, "I am warning you" , 0 ) . unwrap( ) } ,
1272
+ { PyErr :: warn_bound ( py, & cls, "I am warning you" , 0 ) . unwrap( ) } ,
1204
1273
[ ( exceptions:: PyUserWarning , "I am warning you" ) ]
1205
1274
) ;
1206
1275
1207
- let err =
1208
- PyErr :: warn_explicit ( py, cls, "I am warning you" , "pyo3test.py" , 427 , None , None )
1209
- . unwrap_err ( ) ;
1276
+ let err = PyErr :: warn_explicit_bound (
1277
+ py,
1278
+ & cls,
1279
+ "I am warning you" ,
1280
+ "pyo3test.py" ,
1281
+ 427 ,
1282
+ None ,
1283
+ None ,
1284
+ )
1285
+ . unwrap_err ( ) ;
1210
1286
assert ! ( err
1211
1287
. value( py)
1212
1288
. getattr( "args" )
0 commit comments