@@ -31,12 +31,10 @@ pyobject_native_type_core!(
31
31
32
32
impl PySet {
33
33
/// Deprecated form of [`PySet::new_bound`].
34
- #[ cfg_attr(
35
- not( feature = "gil-refs" ) ,
36
- deprecated(
37
- since = "0.21.0" ,
38
- note = "`PySet::new` will be replaced by `PySet::new_bound` in a future PyO3 version"
39
- )
34
+ #[ cfg( feature = "gil-refs" ) ]
35
+ #[ deprecated(
36
+ since = "0.21.0" ,
37
+ note = "`PySet::new` will be replaced by `PySet::new_bound` in a future PyO3 version"
40
38
) ]
41
39
#[ inline]
42
40
pub fn new < ' a , ' p , T : ToPyObject + ' a > (
@@ -58,12 +56,10 @@ impl PySet {
58
56
}
59
57
60
58
/// Deprecated form of [`PySet::empty_bound`].
61
- #[ cfg_attr(
62
- not( feature = "gil-refs" ) ,
63
- deprecated(
64
- since = "0.21.2" ,
65
- note = "`PySet::empty` will be replaced by `PySet::empty_bound` in a future PyO3 version"
66
- )
59
+ #[ cfg( feature = "gil-refs" ) ]
60
+ #[ deprecated(
61
+ since = "0.21.2" ,
62
+ note = "`PySet::empty` will be replaced by `PySet::empty_bound` in a future PyO3 version"
67
63
) ]
68
64
pub fn empty ( py : Python < ' _ > ) -> PyResult < & PySet > {
69
65
Self :: empty_bound ( py) . map ( Bound :: into_gil_ref)
@@ -396,27 +392,29 @@ pub(crate) fn new_from_iter<T: ToPyObject>(
396
392
}
397
393
398
394
#[ cfg( test) ]
399
- #[ cfg_attr( not( feature = "gil-refs" ) , allow( deprecated) ) ]
400
395
mod tests {
401
396
use super :: PySet ;
402
- use crate :: { Python , ToPyObject } ;
397
+ use crate :: {
398
+ types:: { PyAnyMethods , PySetMethods } ,
399
+ Python , ToPyObject ,
400
+ } ;
403
401
use std:: collections:: HashSet ;
404
402
405
403
#[ test]
406
404
fn test_set_new ( ) {
407
405
Python :: with_gil ( |py| {
408
- let set = PySet :: new ( py, & [ 1 ] ) . unwrap ( ) ;
406
+ let set = PySet :: new_bound ( py, & [ 1 ] ) . unwrap ( ) ;
409
407
assert_eq ! ( 1 , set. len( ) ) ;
410
408
411
409
let v = vec ! [ 1 ] ;
412
- assert ! ( PySet :: new ( py, & [ v] ) . is_err( ) ) ;
410
+ assert ! ( PySet :: new_bound ( py, & [ v] ) . is_err( ) ) ;
413
411
} ) ;
414
412
}
415
413
416
414
#[ test]
417
415
fn test_set_empty ( ) {
418
416
Python :: with_gil ( |py| {
419
- let set = PySet :: empty ( py) . unwrap ( ) ;
417
+ let set = PySet :: empty_bound ( py) . unwrap ( ) ;
420
418
assert_eq ! ( 0 , set. len( ) ) ;
421
419
assert ! ( set. is_empty( ) ) ;
422
420
} ) ;
@@ -427,19 +425,19 @@ mod tests {
427
425
Python :: with_gil ( |py| {
428
426
let mut v = HashSet :: new ( ) ;
429
427
let ob = v. to_object ( py) ;
430
- let set: & PySet = ob. downcast ( py) . unwrap ( ) ;
428
+ let set = ob. downcast_bound :: < PySet > ( py) . unwrap ( ) ;
431
429
assert_eq ! ( 0 , set. len( ) ) ;
432
430
v. insert ( 7 ) ;
433
431
let ob = v. to_object ( py) ;
434
- let set2: & PySet = ob. downcast ( py) . unwrap ( ) ;
432
+ let set2 = ob. downcast_bound :: < PySet > ( py) . unwrap ( ) ;
435
433
assert_eq ! ( 1 , set2. len( ) ) ;
436
434
} ) ;
437
435
}
438
436
439
437
#[ test]
440
438
fn test_set_clear ( ) {
441
439
Python :: with_gil ( |py| {
442
- let set = PySet :: new ( py, & [ 1 ] ) . unwrap ( ) ;
440
+ let set = PySet :: new_bound ( py, & [ 1 ] ) . unwrap ( ) ;
443
441
assert_eq ! ( 1 , set. len( ) ) ;
444
442
set. clear ( ) ;
445
443
assert_eq ! ( 0 , set. len( ) ) ;
@@ -449,15 +447,15 @@ mod tests {
449
447
#[ test]
450
448
fn test_set_contains ( ) {
451
449
Python :: with_gil ( |py| {
452
- let set = PySet :: new ( py, & [ 1 ] ) . unwrap ( ) ;
450
+ let set = PySet :: new_bound ( py, & [ 1 ] ) . unwrap ( ) ;
453
451
assert ! ( set. contains( 1 ) . unwrap( ) ) ;
454
452
} ) ;
455
453
}
456
454
457
455
#[ test]
458
456
fn test_set_discard ( ) {
459
457
Python :: with_gil ( |py| {
460
- let set = PySet :: new ( py, & [ 1 ] ) . unwrap ( ) ;
458
+ let set = PySet :: new_bound ( py, & [ 1 ] ) . unwrap ( ) ;
461
459
assert ! ( !set. discard( 2 ) . unwrap( ) ) ;
462
460
assert_eq ! ( 1 , set. len( ) ) ;
463
461
@@ -472,7 +470,7 @@ mod tests {
472
470
#[ test]
473
471
fn test_set_add ( ) {
474
472
Python :: with_gil ( |py| {
475
- let set = PySet :: new ( py, & [ 1 , 2 ] ) . unwrap ( ) ;
473
+ let set = PySet :: new_bound ( py, & [ 1 , 2 ] ) . unwrap ( ) ;
476
474
set. add ( 1 ) . unwrap ( ) ; // Add a dupliated element
477
475
assert ! ( set. contains( 1 ) . unwrap( ) ) ;
478
476
} ) ;
@@ -481,21 +479,21 @@ mod tests {
481
479
#[ test]
482
480
fn test_set_pop ( ) {
483
481
Python :: with_gil ( |py| {
484
- let set = PySet :: new ( py, & [ 1 ] ) . unwrap ( ) ;
482
+ let set = PySet :: new_bound ( py, & [ 1 ] ) . unwrap ( ) ;
485
483
let val = set. pop ( ) ;
486
484
assert ! ( val. is_some( ) ) ;
487
485
let val2 = set. pop ( ) ;
488
486
assert ! ( val2. is_none( ) ) ;
489
487
assert ! ( py
490
- . eval ( "print('Exception state should not be set.')" , None , None )
488
+ . eval_bound ( "print('Exception state should not be set.')" , None , None )
491
489
. is_ok( ) ) ;
492
490
} ) ;
493
491
}
494
492
495
493
#[ test]
496
494
fn test_set_iter ( ) {
497
495
Python :: with_gil ( |py| {
498
- let set = PySet :: new ( py, & [ 1 ] ) . unwrap ( ) ;
496
+ let set = PySet :: new_bound ( py, & [ 1 ] ) . unwrap ( ) ;
499
497
500
498
for el in set {
501
499
assert_eq ! ( 1i32 , el. extract:: <' _, i32 >( ) . unwrap( ) ) ;
@@ -520,9 +518,9 @@ mod tests {
520
518
#[ should_panic]
521
519
fn test_set_iter_mutation ( ) {
522
520
Python :: with_gil ( |py| {
523
- let set = PySet :: new ( py, & [ 1 , 2 , 3 , 4 , 5 ] ) . unwrap ( ) ;
521
+ let set = PySet :: new_bound ( py, & [ 1 , 2 , 3 , 4 , 5 ] ) . unwrap ( ) ;
524
522
525
- for _ in set {
523
+ for _ in & set {
526
524
let _ = set. add ( 42 ) ;
527
525
}
528
526
} ) ;
@@ -532,9 +530,9 @@ mod tests {
532
530
#[ should_panic]
533
531
fn test_set_iter_mutation_same_len ( ) {
534
532
Python :: with_gil ( |py| {
535
- let set = PySet :: new ( py, & [ 1 , 2 , 3 , 4 , 5 ] ) . unwrap ( ) ;
533
+ let set = PySet :: new_bound ( py, & [ 1 , 2 , 3 , 4 , 5 ] ) . unwrap ( ) ;
536
534
537
- for item in set {
535
+ for item in & set {
538
536
let item: i32 = item. extract ( ) . unwrap ( ) ;
539
537
let _ = set. del_item ( item) ;
540
538
let _ = set. add ( item + 10 ) ;
@@ -545,7 +543,7 @@ mod tests {
545
543
#[ test]
546
544
fn test_set_iter_size_hint ( ) {
547
545
Python :: with_gil ( |py| {
548
- let set = PySet :: new ( py, & [ 1 ] ) . unwrap ( ) ;
546
+ let set = PySet :: new_bound ( py, & [ 1 ] ) . unwrap ( ) ;
549
547
let mut iter = set. iter ( ) ;
550
548
551
549
// Exact size
0 commit comments