14
14
15
15
/// Attempt to convert a Python object to an instance of `T`
16
16
#[ deprecated( since = "0.22.0" , note = "use `depythonize` instead" ) ]
17
- pub fn depythonize_bound < ' py , T > ( obj : Bound < ' py , PyAny > ) -> Result < T >
17
+ pub fn depythonize_bound < T > ( obj : Bound < PyAny > ) -> Result < T >
18
18
where
19
19
T : DeserializeOwned ,
20
20
{
@@ -46,10 +46,10 @@ impl<'a, 'py> Depythonizer<'a, 'py> {
46
46
47
47
fn set_access ( & self ) -> Result < PySetAsSequence < ' py > > {
48
48
match self . input . downcast :: < PySet > ( ) {
49
- Ok ( set) => Ok ( PySetAsSequence :: from_set ( & set) ) ,
49
+ Ok ( set) => Ok ( PySetAsSequence :: from_set ( set) ) ,
50
50
Err ( e) => {
51
51
if let Ok ( f) = self . input . downcast :: < PyFrozenSet > ( ) {
52
- Ok ( PySetAsSequence :: from_frozenset ( & f) )
52
+ Ok ( PySetAsSequence :: from_frozenset ( f) )
53
53
} else {
54
54
Err ( e. into ( ) )
55
55
}
@@ -387,13 +387,13 @@ struct PySetAsSequence<'py> {
387
387
impl < ' py > PySetAsSequence < ' py > {
388
388
fn from_set ( set : & Bound < ' py , PySet > ) -> Self {
389
389
Self {
390
- iter : PyIterator :: from_bound_object ( & set) . expect ( "set is always iterable" ) ,
390
+ iter : PyIterator :: from_object ( set) . expect ( "set is always iterable" ) ,
391
391
}
392
392
}
393
393
394
394
fn from_frozenset ( set : & Bound < ' py , PyFrozenSet > ) -> Self {
395
395
Self {
396
- iter : PyIterator :: from_bound_object ( & set) . expect ( "frozenset is always iterable" ) ,
396
+ iter : PyIterator :: from_object ( set) . expect ( "frozenset is always iterable" ) ,
397
397
}
398
398
}
399
399
}
@@ -415,8 +415,8 @@ impl<'de> de::SeqAccess<'de> for PySetAsSequence<'_> {
415
415
}
416
416
417
417
struct PyMappingAccess < ' py > {
418
- keys : Bound < ' py , PySequence > ,
419
- values : Bound < ' py , PySequence > ,
418
+ keys : Bound < ' py , PyList > ,
419
+ values : Bound < ' py , PyList > ,
420
420
key_idx : usize ,
421
421
val_idx : usize ,
422
422
len : usize ,
@@ -524,18 +524,21 @@ impl<'de> de::VariantAccess<'de> for PyEnumAccess<'_, '_> {
524
524
525
525
#[ cfg( test) ]
526
526
mod test {
527
+ use std:: ffi:: CStr ;
528
+
527
529
use super :: * ;
528
530
use crate :: error:: ErrorImpl ;
529
531
use maplit:: hashmap;
530
- use pyo3:: { IntoPy , Python } ;
532
+ use pyo3:: ffi:: c_str;
533
+ use pyo3:: { IntoPyObject , Python } ;
531
534
use serde_json:: { json, Value as JsonValue } ;
532
535
533
- fn test_de < T > ( code : & str , expected : & T , expected_json : & JsonValue )
536
+ fn test_de < T > ( code : & CStr , expected : & T , expected_json : & JsonValue )
534
537
where
535
538
T : de:: DeserializeOwned + PartialEq + std:: fmt:: Debug ,
536
539
{
537
540
Python :: with_gil ( |py| {
538
- let obj = py. eval_bound ( code, None , None ) . unwrap ( ) ;
541
+ let obj = py. eval ( code, None , None ) . unwrap ( ) ;
539
542
let actual: T = depythonize ( & obj) . unwrap ( ) ;
540
543
assert_eq ! ( & actual, expected) ;
541
544
let actual_json: JsonValue = depythonize ( & obj) . unwrap ( ) ;
@@ -554,7 +557,7 @@ mod test {
554
557
555
558
let expected = Empty ;
556
559
let expected_json = json ! ( null) ;
557
- let code = "None" ;
560
+ let code = c_str ! ( "None" ) ;
558
561
test_de ( code, & expected, & expected_json) ;
559
562
}
560
563
@@ -580,7 +583,7 @@ mod test {
580
583
"baz" : 45.23 ,
581
584
"qux" : true
582
585
} ) ;
583
- let code = "{'foo': 'Foo', 'bar': 8, 'baz': 45.23, 'qux': True}" ;
586
+ let code = c_str ! ( "{'foo': 'Foo', 'bar': 8, 'baz': 45.23, 'qux': True}" ) ;
584
587
test_de ( code, & expected, & expected_json) ;
585
588
}
586
589
@@ -592,13 +595,11 @@ mod test {
592
595
bar : usize ,
593
596
}
594
597
595
- let code = "{'foo': 'Foo'}" ;
598
+ let code = c_str ! ( "{'foo': 'Foo'}" ) ;
596
599
597
600
Python :: with_gil ( |py| {
598
- let locals = PyDict :: new_bound ( py) ;
599
- py. run_bound ( & format ! ( "obj = {}" , code) , None , Some ( & locals) )
600
- . unwrap ( ) ;
601
- let obj = locals. get_item ( "obj" ) . unwrap ( ) . unwrap ( ) ;
601
+ let locals = PyDict :: new ( py) ;
602
+ let obj = py. eval ( code, None , Some ( & locals) ) . unwrap ( ) ;
602
603
assert ! ( matches!(
603
604
* depythonize:: <Struct >( & obj) . unwrap_err( ) . inner,
604
605
ErrorImpl :: Message ( msg) if msg == "missing field `bar`"
@@ -613,7 +614,7 @@ mod test {
613
614
614
615
let expected = TupleStruct ( "cat" . to_string ( ) , -10.05 ) ;
615
616
let expected_json = json ! ( [ "cat" , -10.05 ] ) ;
616
- let code = "('cat', -10.05)" ;
617
+ let code = c_str ! ( "('cat', -10.05)" ) ;
617
618
test_de ( code, & expected, & expected_json) ;
618
619
}
619
620
@@ -622,13 +623,11 @@ mod test {
622
623
#[ derive( Debug , Deserialize , PartialEq ) ]
623
624
struct TupleStruct ( String , f64 ) ;
624
625
625
- let code = "('cat', -10.05, 'foo')" ;
626
+ let code = c_str ! ( "('cat', -10.05, 'foo')" ) ;
626
627
627
628
Python :: with_gil ( |py| {
628
- let locals = PyDict :: new_bound ( py) ;
629
- py. run_bound ( & format ! ( "obj = {}" , code) , None , Some ( & locals) )
630
- . unwrap ( ) ;
631
- let obj = locals. get_item ( "obj" ) . unwrap ( ) . unwrap ( ) ;
629
+ let locals = PyDict :: new ( py) ;
630
+ let obj = py. eval ( code, None , Some ( & locals) ) . unwrap ( ) ;
632
631
assert ! ( matches!(
633
632
* depythonize:: <TupleStruct >( & obj) . unwrap_err( ) . inner,
634
633
ErrorImpl :: IncorrectSequenceLength { expected, got } if expected == 2 && got == 3
@@ -643,63 +642,63 @@ mod test {
643
642
644
643
let expected = TupleStruct ( "cat" . to_string ( ) , -10.05 ) ;
645
644
let expected_json = json ! ( [ "cat" , -10.05 ] ) ;
646
- let code = "['cat', -10.05]" ;
645
+ let code = c_str ! ( "['cat', -10.05]" ) ;
647
646
test_de ( code, & expected, & expected_json) ;
648
647
}
649
648
650
649
#[ test]
651
650
fn test_tuple ( ) {
652
651
let expected = ( "foo" . to_string ( ) , 5 ) ;
653
652
let expected_json = json ! ( [ "foo" , 5 ] ) ;
654
- let code = "('foo', 5)" ;
653
+ let code = c_str ! ( "('foo', 5)" ) ;
655
654
test_de ( code, & expected, & expected_json) ;
656
655
}
657
656
658
657
#[ test]
659
658
fn test_tuple_from_pylist ( ) {
660
659
let expected = ( "foo" . to_string ( ) , 5 ) ;
661
660
let expected_json = json ! ( [ "foo" , 5 ] ) ;
662
- let code = "['foo', 5]" ;
661
+ let code = c_str ! ( "['foo', 5]" ) ;
663
662
test_de ( code, & expected, & expected_json) ;
664
663
}
665
664
666
665
#[ test]
667
666
fn test_vec_from_pyset ( ) {
668
667
let expected = vec ! [ "foo" . to_string( ) ] ;
669
668
let expected_json = json ! ( [ "foo" ] ) ;
670
- let code = "{'foo'}" ;
669
+ let code = c_str ! ( "{'foo'}" ) ;
671
670
test_de ( code, & expected, & expected_json) ;
672
671
}
673
672
674
673
#[ test]
675
674
fn test_vec_from_pyfrozenset ( ) {
676
675
let expected = vec ! [ "foo" . to_string( ) ] ;
677
676
let expected_json = json ! ( [ "foo" ] ) ;
678
- let code = "frozenset({'foo'})" ;
677
+ let code = c_str ! ( "frozenset({'foo'})" ) ;
679
678
test_de ( code, & expected, & expected_json) ;
680
679
}
681
680
682
681
#[ test]
683
682
fn test_vec ( ) {
684
683
let expected = vec ! [ 3 , 2 , 1 ] ;
685
684
let expected_json = json ! ( [ 3 , 2 , 1 ] ) ;
686
- let code = "[3, 2, 1]" ;
685
+ let code = c_str ! ( "[3, 2, 1]" ) ;
687
686
test_de ( code, & expected, & expected_json) ;
688
687
}
689
688
690
689
#[ test]
691
690
fn test_vec_from_tuple ( ) {
692
691
let expected = vec ! [ 3 , 2 , 1 ] ;
693
692
let expected_json = json ! ( [ 3 , 2 , 1 ] ) ;
694
- let code = "(3, 2, 1)" ;
693
+ let code = c_str ! ( "(3, 2, 1)" ) ;
695
694
test_de ( code, & expected, & expected_json) ;
696
695
}
697
696
698
697
#[ test]
699
698
fn test_hashmap ( ) {
700
699
let expected = hashmap ! { "foo" . to_string( ) => 4 } ;
701
700
let expected_json = json ! ( { "foo" : 4 } ) ;
702
- let code = "{'foo': 4}" ;
701
+ let code = c_str ! ( "{'foo': 4}" ) ;
703
702
test_de ( code, & expected, & expected_json) ;
704
703
}
705
704
@@ -712,7 +711,7 @@ mod test {
712
711
713
712
let expected = Foo :: Variant ;
714
713
let expected_json = json ! ( "Variant" ) ;
715
- let code = "'Variant'" ;
714
+ let code = c_str ! ( "'Variant'" ) ;
716
715
test_de ( code, & expected, & expected_json) ;
717
716
}
718
717
@@ -725,7 +724,7 @@ mod test {
725
724
726
725
let expected = Foo :: Tuple ( 12 , "cat" . to_string ( ) ) ;
727
726
let expected_json = json ! ( { "Tuple" : [ 12 , "cat" ] } ) ;
728
- let code = "{'Tuple': [12, 'cat']}" ;
727
+ let code = c_str ! ( "{'Tuple': [12, 'cat']}" ) ;
729
728
test_de ( code, & expected, & expected_json) ;
730
729
}
731
730
@@ -738,7 +737,7 @@ mod test {
738
737
739
738
let expected = Foo :: NewType ( "cat" . to_string ( ) ) ;
740
739
let expected_json = json ! ( { "NewType" : "cat" } ) ;
741
- let code = "{'NewType': 'cat'}" ;
740
+ let code = c_str ! ( "{'NewType': 'cat'}" ) ;
742
741
test_de ( code, & expected, & expected_json) ;
743
742
}
744
743
@@ -754,7 +753,7 @@ mod test {
754
753
bar : 25 ,
755
754
} ;
756
755
let expected_json = json ! ( { "Struct" : { "foo" : "cat" , "bar" : 25 } } ) ;
757
- let code = "{'Struct': {'foo': 'cat', 'bar': 25}}" ;
756
+ let code = c_str ! ( "{'Struct': {'foo': 'cat', 'bar': 25}}" ) ;
758
757
test_de ( code, & expected, & expected_json) ;
759
758
}
760
759
#[ test]
@@ -767,7 +766,7 @@ mod test {
767
766
768
767
let expected = Foo :: Tuple ( 12.0 , 'c' ) ;
769
768
let expected_json = json ! ( [ 12.0 , 'c' ] ) ;
770
- let code = "[12.0, 'c']" ;
769
+ let code = c_str ! ( "[12.0, 'c']" ) ;
771
770
test_de ( code, & expected, & expected_json) ;
772
771
}
773
772
@@ -781,7 +780,7 @@ mod test {
781
780
782
781
let expected = Foo :: NewType ( "cat" . to_string ( ) ) ;
783
782
let expected_json = json ! ( "cat" ) ;
784
- let code = "'cat'" ;
783
+ let code = c_str ! ( "'cat'" ) ;
785
784
test_de ( code, & expected, & expected_json) ;
786
785
}
787
786
@@ -798,7 +797,7 @@ mod test {
798
797
bar : [ 2 , 5 , 3 , 1 ] ,
799
798
} ;
800
799
let expected_json = json ! ( { "foo" : [ "a" , "b" , "c" ] , "bar" : [ 2 , 5 , 3 , 1 ] } ) ;
801
- let code = "{'foo': ['a', 'b', 'c'], 'bar': [2, 5, 3, 1]}" ;
800
+ let code = c_str ! ( "{'foo': ['a', 'b', 'c'], 'bar': [2, 5, 3, 1]}" ) ;
802
801
test_de ( code, & expected, & expected_json) ;
803
802
}
804
803
@@ -831,46 +830,47 @@ mod test {
831
830
} ;
832
831
let expected_json =
833
832
json ! ( { "name" : "SomeFoo" , "bar" : { "value" : 13 , "variant" : { "Tuple" : [ -1.5 , 8 ] } } } ) ;
834
- let code = "{'name': 'SomeFoo', 'bar': {'value': 13, 'variant': {'Tuple': [-1.5, 8]}}}" ;
833
+ let code =
834
+ c_str ! ( "{'name': 'SomeFoo', 'bar': {'value': 13, 'variant': {'Tuple': [-1.5, 8]}}}" ) ;
835
835
test_de ( code, & expected, & expected_json) ;
836
836
}
837
837
838
838
#[ test]
839
839
fn test_int_limits ( ) {
840
840
Python :: with_gil ( |py| {
841
841
// serde_json::Value supports u64 and i64 as maxiumum sizes
842
- let _: serde_json:: Value = depythonize ( & u8:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
843
- let _: serde_json:: Value = depythonize ( & u8:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
844
- let _: serde_json:: Value = depythonize ( & i8:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
845
- let _: serde_json:: Value = depythonize ( & i8:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
846
-
847
- let _: serde_json:: Value = depythonize ( & u16:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
848
- let _: serde_json:: Value = depythonize ( & u16:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
849
- let _: serde_json:: Value = depythonize ( & i16:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
850
- let _: serde_json:: Value = depythonize ( & i16:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
851
-
852
- let _: serde_json:: Value = depythonize ( & u32:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
853
- let _: serde_json:: Value = depythonize ( & u32:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
854
- let _: serde_json:: Value = depythonize ( & i32:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
855
- let _: serde_json:: Value = depythonize ( & i32:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
856
-
857
- let _: serde_json:: Value = depythonize ( & u64:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
858
- let _: serde_json:: Value = depythonize ( & u64:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
859
- let _: serde_json:: Value = depythonize ( & i64:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
860
- let _: serde_json:: Value = depythonize ( & i64:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
861
-
862
- let _: u128 = depythonize ( & u128:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
863
- let _: i128 = depythonize ( & u128:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
864
-
865
- let _: i128 = depythonize ( & i128:: MAX . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
866
- let _: i128 = depythonize ( & i128:: MIN . into_py ( py) . into_bound ( py ) ) . unwrap ( ) ;
842
+ let _: serde_json:: Value = depythonize ( & u8:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
843
+ let _: serde_json:: Value = depythonize ( & u8:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
844
+ let _: serde_json:: Value = depythonize ( & i8:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
845
+ let _: serde_json:: Value = depythonize ( & i8:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
846
+
847
+ let _: serde_json:: Value = depythonize ( & u16:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
848
+ let _: serde_json:: Value = depythonize ( & u16:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
849
+ let _: serde_json:: Value = depythonize ( & i16:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
850
+ let _: serde_json:: Value = depythonize ( & i16:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
851
+
852
+ let _: serde_json:: Value = depythonize ( & u32:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
853
+ let _: serde_json:: Value = depythonize ( & u32:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
854
+ let _: serde_json:: Value = depythonize ( & i32:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
855
+ let _: serde_json:: Value = depythonize ( & i32:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
856
+
857
+ let _: serde_json:: Value = depythonize ( & u64:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
858
+ let _: serde_json:: Value = depythonize ( & u64:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
859
+ let _: serde_json:: Value = depythonize ( & i64:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
860
+ let _: serde_json:: Value = depythonize ( & i64:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
861
+
862
+ let _: u128 = depythonize ( & u128:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
863
+ let _: i128 = depythonize ( & u128:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
864
+
865
+ let _: i128 = depythonize ( & i128:: MAX . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
866
+ let _: i128 = depythonize ( & i128:: MIN . into_pyobject ( py) . unwrap ( ) ) . unwrap ( ) ;
867
867
} ) ;
868
868
}
869
869
870
870
#[ test]
871
871
fn test_deserialize_bytes ( ) {
872
872
Python :: with_gil ( |py| {
873
- let obj = PyBytes :: new_bound ( py, "hello" . as_bytes ( ) ) ;
873
+ let obj = PyBytes :: new ( py, "hello" . as_bytes ( ) ) ;
874
874
let actual: Vec < u8 > = depythonize ( & obj) . unwrap ( ) ;
875
875
assert_eq ! ( actual, b"hello" ) ;
876
876
} )
@@ -880,15 +880,15 @@ mod test {
880
880
fn test_char ( ) {
881
881
let expected = 'a' ;
882
882
let expected_json = json ! ( "a" ) ;
883
- let code = "'a'" ;
883
+ let code = c_str ! ( "'a'" ) ;
884
884
test_de ( code, & expected, & expected_json) ;
885
885
}
886
886
887
887
#[ test]
888
888
fn test_unknown_type ( ) {
889
889
Python :: with_gil ( |py| {
890
890
let obj = py
891
- . import_bound ( "decimal" )
891
+ . import ( "decimal" )
892
892
. unwrap ( )
893
893
. getattr ( "Decimal" )
894
894
. unwrap ( )
0 commit comments