@@ -9,8 +9,8 @@ use pyo3::types::{PyDict, PyString};
9
9
10
10
use _pydantic_core:: { validate_core_schema, SchemaValidator } ;
11
11
12
- fn build_schema_validator_with_globals ( py : Python , code : & str , globals : Option < & PyDict > ) -> SchemaValidator {
13
- let mut schema = py. eval ( code, globals, None ) . unwrap ( ) . extract ( ) . unwrap ( ) ;
12
+ fn build_schema_validator_with_globals ( py : Python , code : & str , globals : Option < & Bound < ' _ , PyDict > > ) -> SchemaValidator {
13
+ let mut schema = py. eval_bound ( code, globals, None ) . unwrap ( ) . extract ( ) . unwrap ( ) ;
14
14
schema = validate_core_schema ( & schema, None ) . unwrap ( ) . extract ( ) . unwrap ( ) ;
15
15
SchemaValidator :: py_new ( py, & schema, None ) . unwrap ( )
16
16
}
@@ -71,7 +71,7 @@ fn list_int_input(py: Python<'_>) -> (SchemaValidator, PyObject) {
71
71
( 0 ..100 ) . map( |x| x. to_string( ) ) . collect:: <Vec <String >>( ) . join( "," )
72
72
) ;
73
73
74
- let input = py. eval ( & code, None , None ) . unwrap ( ) ;
74
+ let input = py. eval_bound ( & code, None , None ) . unwrap ( ) ;
75
75
( validator, input. to_object ( py) )
76
76
}
77
77
@@ -117,7 +117,7 @@ fn list_error_json(bench: &mut Bencher) {
117
117
match validator. validate_json ( py, & json ( py, & code) , None , None , None ) {
118
118
Ok ( _) => panic ! ( "unexpectedly valid" ) ,
119
119
Err ( e) => {
120
- let v = e. value ( py) ;
120
+ let v = e. value_bound ( py) ;
121
121
// println!("error: {}", v.to_string());
122
122
assert_eq ! ( v. getattr( "title" ) . unwrap( ) . to_string( ) , "list[int]" ) ;
123
123
let error_count: i64 = v. call_method0 ( "error_count" ) . unwrap ( ) . extract ( ) . unwrap ( ) ;
@@ -144,12 +144,12 @@ fn list_error_python_input(py: Python<'_>) -> (SchemaValidator, PyObject) {
144
144
. join( ", " )
145
145
) ;
146
146
147
- let input = py. eval ( & code, None , None ) . unwrap ( ) . extract ( ) . unwrap ( ) ;
147
+ let input = py. eval_bound ( & code, None , None ) . unwrap ( ) . extract ( ) . unwrap ( ) ;
148
148
149
149
match validator. validate_python ( py, & input, None , None , None , None ) {
150
150
Ok ( _) => panic ! ( "unexpectedly valid" ) ,
151
151
Err ( e) => {
152
- let v = e. value ( py) ;
152
+ let v = e. value_bound ( py) ;
153
153
// println!("error: {}", v.to_string());
154
154
assert_eq ! ( v. getattr( "title" ) . unwrap( ) . to_string( ) , "list[int]" ) ;
155
155
let error_count: i64 = v. call_method0 ( "error_count" ) . unwrap ( ) . extract ( ) . unwrap ( ) ;
@@ -211,7 +211,7 @@ fn list_any_python(bench: &mut Bencher) {
211
211
"[{}]" ,
212
212
( 0 ..100 ) . map( |x| x. to_string( ) ) . collect:: <Vec <String >>( ) . join( "," )
213
213
) ;
214
- let input = py. eval ( & code, None , None ) . unwrap ( ) . to_object ( py) ;
214
+ let input = py. eval_bound ( & code, None , None ) . unwrap ( ) . to_object ( py) ;
215
215
let input = black_box ( input. bind ( py) ) ;
216
216
bench. iter ( || {
217
217
let v = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
@@ -263,7 +263,7 @@ fn dict_python(bench: &mut Bencher) {
263
263
. collect:: <Vec <String >>( )
264
264
. join( ", " )
265
265
) ;
266
- let input = py. eval ( & code, None , None ) . unwrap ( ) . to_object ( py) ;
266
+ let input = py. eval_bound ( & code, None , None ) . unwrap ( ) . to_object ( py) ;
267
267
let input = black_box ( input. bind ( py) ) ;
268
268
bench. iter ( || {
269
269
let v = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
@@ -292,12 +292,12 @@ fn dict_value_error(bench: &mut Bencher) {
292
292
. join( ", " )
293
293
) ;
294
294
295
- let input = py. eval ( & code, None , None ) . unwrap ( ) . to_object ( py) . into_bound ( py) ;
295
+ let input = py. eval_bound ( & code, None , None ) . unwrap ( ) . to_object ( py) . into_bound ( py) ;
296
296
297
297
match validator. validate_python ( py, & input, None , None , None , None ) {
298
298
Ok ( _) => panic ! ( "unexpectedly valid" ) ,
299
299
Err ( e) => {
300
- let v = e. value ( py) ;
300
+ let v = e. value_bound ( py) ;
301
301
// println!("error: {}", v.to_string());
302
302
assert_eq ! ( v. getattr( "title" ) . unwrap( ) . to_string( ) , "dict[str,constrained-int]" ) ;
303
303
let error_count: i64 = v. call_method0 ( "error_count" ) . unwrap ( ) . extract ( ) . unwrap ( ) ;
@@ -370,7 +370,7 @@ fn typed_dict_python(bench: &mut Bencher) {
370
370
) ;
371
371
372
372
let code = r#"{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7, "h": 8, "i": 9, "j": 0}"# . to_string ( ) ;
373
- let input = py. eval ( & code, None , None ) . unwrap ( ) . to_object ( py) ;
373
+ let input = py. eval_bound ( & code, None , None ) . unwrap ( ) . to_object ( py) ;
374
374
let input = black_box ( input. bind ( py) ) ;
375
375
bench. iter ( || {
376
376
let v = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
@@ -410,13 +410,13 @@ fn typed_dict_deep_error(bench: &mut Bencher) {
410
410
411
411
let code = "{'field_a': '1', 'field_b': {'field_c': '2', 'field_d': {'field_e': '4', 'field_f': 'xx'}}}" ;
412
412
413
- let input = py. eval ( code, None , None ) . unwrap ( ) . to_object ( py) ;
413
+ let input = py. eval_bound ( code, None , None ) . unwrap ( ) . to_object ( py) ;
414
414
let input = black_box ( input. bind ( py) ) ;
415
415
416
416
match validator. validate_python ( py, & input, None , None , None , None ) {
417
417
Ok ( _) => panic ! ( "unexpectedly valid" ) ,
418
418
Err ( e) => {
419
- let v = e. value ( py) ;
419
+ let v = e. value_bound ( py) ;
420
420
// println!("error: {}", v.to_string());
421
421
assert_eq ! ( v. getattr( "title" ) . unwrap( ) . to_string( ) , "typed-dict" ) ;
422
422
let error_count: i64 = v. call_method0 ( "error_count" ) . unwrap ( ) . extract ( ) . unwrap ( ) ;
@@ -438,7 +438,7 @@ fn typed_dict_deep_error(bench: &mut Bencher) {
438
438
#[ bench]
439
439
fn complete_model ( bench : & mut Bencher ) {
440
440
Python :: with_gil ( |py| {
441
- let sys_path = py. import ( "sys" ) . unwrap ( ) . getattr ( "path" ) . unwrap ( ) ;
441
+ let sys_path = py. import_bound ( "sys" ) . unwrap ( ) . getattr ( "path" ) . unwrap ( ) ;
442
442
sys_path. call_method1 ( "append" , ( "./tests/benchmarks/" , ) ) . unwrap ( ) ;
443
443
444
444
let complete_schema = py. import_bound ( "complete_schema" ) . unwrap ( ) ;
@@ -458,7 +458,7 @@ fn complete_model(bench: &mut Bencher) {
458
458
#[ bench]
459
459
fn nested_model_using_definitions ( bench : & mut Bencher ) {
460
460
Python :: with_gil ( |py| {
461
- let sys_path = py. import ( "sys" ) . unwrap ( ) . getattr ( "path" ) . unwrap ( ) ;
461
+ let sys_path = py. import_bound ( "sys" ) . unwrap ( ) . getattr ( "path" ) . unwrap ( ) ;
462
462
sys_path. call_method1 ( "append" , ( "./tests/benchmarks/" , ) ) . unwrap ( ) ;
463
463
464
464
let complete_schema = py. import_bound ( "nested_schema" ) . unwrap ( ) ;
@@ -480,7 +480,7 @@ fn nested_model_using_definitions(bench: &mut Bencher) {
480
480
#[ bench]
481
481
fn nested_model_inlined ( bench : & mut Bencher ) {
482
482
Python :: with_gil ( |py| {
483
- let sys_path = py. import ( "sys" ) . unwrap ( ) . getattr ( "path" ) . unwrap ( ) ;
483
+ let sys_path = py. import_bound ( "sys" ) . unwrap ( ) . getattr ( "path" ) . unwrap ( ) ;
484
484
sys_path. call_method1 ( "append" , ( "./tests/benchmarks/" , ) ) . unwrap ( ) ;
485
485
486
486
let complete_schema = py. import_bound ( "nested_schema" ) . unwrap ( ) ;
@@ -520,7 +520,7 @@ fn literal_strings_few_small_python(bench: &mut Bencher) {
520
520
Python :: with_gil ( |py| {
521
521
let validator = build_schema_validator ( py, "{'type': 'literal', 'expected': [f'{idx}' for idx in range(5)]}" ) ;
522
522
523
- let input = py. eval ( "'4'" , None , None ) . unwrap ( ) ;
523
+ let input = py. eval_bound ( "'4'" , None , None ) . unwrap ( ) ;
524
524
let input = input. to_object ( py) . into_bound ( py) ;
525
525
let input_str: String = input. extract ( ) . unwrap ( ) ;
526
526
let result = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
@@ -540,7 +540,7 @@ fn literal_strings_few_large_python(bench: &mut Bencher) {
540
540
"{'type': 'literal', 'expected': ['a' * 25 + f'{idx}' for idx in range(5)]}" ,
541
541
) ;
542
542
543
- let input = py. eval ( "'a' * 25 + '4'" , None , None ) . unwrap ( ) ;
543
+ let input = py. eval_bound ( "'a' * 25 + '4'" , None , None ) . unwrap ( ) ;
544
544
let input = input. to_object ( py) . into_bound ( py) ;
545
545
let input_str: String = input. extract ( ) . unwrap ( ) ;
546
546
let result = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
@@ -556,7 +556,7 @@ fn literal_strings_few_large_python(bench: &mut Bencher) {
556
556
fn literal_enums_few_python ( bench : & mut Bencher ) {
557
557
Python :: with_gil ( |py| {
558
558
let globals = PyDict :: new_bound ( py) ;
559
- py. run (
559
+ py. run_bound (
560
560
r#"
561
561
from enum import Enum
562
562
@@ -566,18 +566,18 @@ class Foo(Enum):
566
566
v3 = object()
567
567
v4 = object()
568
568
"# ,
569
- Some ( globals. as_gil_ref ( ) ) ,
569
+ Some ( & globals) ,
570
570
None ,
571
571
)
572
572
. unwrap ( ) ;
573
573
574
574
let validator = build_schema_validator_with_globals (
575
575
py,
576
576
"{'type': 'literal', 'expected': [Foo.v1, Foo.v2, Foo.v3, Foo.v4]}" ,
577
- Some ( globals. as_gil_ref ( ) ) ,
577
+ Some ( & globals) ,
578
578
) ;
579
579
580
- let input = py. eval ( "Foo.v4" , Some ( globals. as_gil_ref ( ) ) , None ) . unwrap ( ) ;
580
+ let input = py. eval_bound ( "Foo.v4" , Some ( & globals) , None ) . unwrap ( ) ;
581
581
let input = input. to_object ( py) . into_bound ( py) ;
582
582
let result = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
583
583
assert ! ( input. eq( result) . unwrap( ) ) ;
@@ -607,7 +607,7 @@ fn literal_strings_many_small_python(bench: &mut Bencher) {
607
607
Python :: with_gil ( |py| {
608
608
let validator = build_schema_validator ( py, "{'type': 'literal', 'expected': [f'{idx}' for idx in range(100)]}" ) ;
609
609
610
- let input = py. eval ( "'99'" , None , None ) . unwrap ( ) ;
610
+ let input = py. eval_bound ( "'99'" , None , None ) . unwrap ( ) ;
611
611
let input = input. to_object ( py) . into_bound ( py) ;
612
612
let input_str: String = input. extract ( ) . unwrap ( ) ;
613
613
let result = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
@@ -627,7 +627,7 @@ fn literal_strings_many_large_python(bench: &mut Bencher) {
627
627
"{'type': 'literal', 'expected': ['a' * 25 + f'{idx}' for idx in range(100)]}" ,
628
628
) ;
629
629
630
- let input = py. eval ( "'a' * 25 + '99'" , None , None ) . unwrap ( ) ;
630
+ let input = py. eval_bound ( "'a' * 25 + '99'" , None , None ) . unwrap ( ) ;
631
631
let input = input. to_object ( py) . into_bound ( py) ;
632
632
let input_str: String = input. extract ( ) . unwrap ( ) ;
633
633
let result = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
@@ -644,7 +644,7 @@ fn literal_ints_many_json(bench: &mut Bencher) {
644
644
Python :: with_gil ( |py| {
645
645
let validator = build_schema_validator ( py, "{'type': 'literal', 'expected': list(range(100))}" ) ;
646
646
647
- let input_json = py. eval ( "'99'" , None , None ) . unwrap ( ) ;
647
+ let input_json = py. eval_bound ( "'99'" , None , None ) . unwrap ( ) ;
648
648
let input_json = input_json. to_object ( py) . into_bound ( py) ;
649
649
let result = validator. validate_json ( py, & input_json, None , None , None ) . unwrap ( ) ;
650
650
let result_int: i64 = result. extract ( py) . unwrap ( ) ;
@@ -663,9 +663,9 @@ fn literal_strings_many_large_json(bench: &mut Bencher) {
663
663
"{'type': 'literal', 'expected': ['a' * 25 + f'{idx}' for idx in range(100)]}" ,
664
664
) ;
665
665
666
- let input = py. eval ( "'a' * 25 + '99'" , None , None ) . unwrap ( ) ;
666
+ let input = py. eval_bound ( "'a' * 25 + '99'" , None , None ) . unwrap ( ) ;
667
667
let input = input. to_object ( py) . into_bound ( py) ;
668
- let input_json = py. eval ( "'\" ' + 'a' * 25 + '99' + '\" '" , None , None ) . unwrap ( ) ;
668
+ let input_json = py. eval_bound ( "'\" ' + 'a' * 25 + '99' + '\" '" , None , None ) . unwrap ( ) ;
669
669
let input_json = input_json. to_object ( py) . into_bound ( py) ;
670
670
let input_str: String = input. extract ( ) . unwrap ( ) ;
671
671
let result = validator. validate_json ( py, & input_json, None , None , None ) . unwrap ( ) ;
@@ -681,7 +681,7 @@ fn literal_strings_many_large_json(bench: &mut Bencher) {
681
681
fn literal_mixed_few_python ( bench : & mut Bencher ) {
682
682
Python :: with_gil ( |py| {
683
683
let globals = PyDict :: new_bound ( py) ;
684
- py. run (
684
+ py. run_bound (
685
685
r#"
686
686
from enum import Enum
687
687
@@ -691,19 +691,19 @@ class Foo(Enum):
691
691
v3 = object()
692
692
v4 = object()
693
693
"# ,
694
- Some ( globals. as_gil_ref ( ) ) ,
694
+ Some ( & globals) ,
695
695
None ,
696
696
)
697
697
. unwrap ( ) ;
698
698
let validator = build_schema_validator_with_globals (
699
699
py,
700
700
"{'type': 'literal', 'expected': [None, 'null', -1, Foo.v4]}" ,
701
- Some ( globals. as_gil_ref ( ) ) ,
701
+ Some ( & globals) ,
702
702
) ;
703
703
704
704
// String
705
705
{
706
- let input = py. eval ( "'null'" , None , None ) . unwrap ( ) ;
706
+ let input = py. eval_bound ( "'null'" , None , None ) . unwrap ( ) ;
707
707
let input = input. to_object ( py) . into_bound ( py) ;
708
708
let input_str: String = input. extract ( ) . unwrap ( ) ;
709
709
let result = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
@@ -716,7 +716,7 @@ class Foo(Enum):
716
716
717
717
// Int
718
718
{
719
- let input = py. eval ( "-1" , None , None ) . unwrap ( ) ;
719
+ let input = py. eval_bound ( "-1" , None , None ) . unwrap ( ) ;
720
720
let input = input. to_object ( py) . into_bound ( py) ;
721
721
let input_int: i64 = input. extract ( ) . unwrap ( ) ;
722
722
let result = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
@@ -729,7 +729,7 @@ class Foo(Enum):
729
729
730
730
// None
731
731
{
732
- let input = py. eval ( "None" , None , None ) . unwrap ( ) ;
732
+ let input = py. eval_bound ( "None" , None , None ) . unwrap ( ) ;
733
733
let input = input. to_object ( py) . into_bound ( py) ;
734
734
let result = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
735
735
assert ! ( input. eq( result) . unwrap( ) ) ;
@@ -740,7 +740,7 @@ class Foo(Enum):
740
740
741
741
// Enum
742
742
{
743
- let input = py. eval ( "Foo.v4" , Some ( globals. as_gil_ref ( ) ) , None ) . unwrap ( ) ;
743
+ let input = py. eval_bound ( "Foo.v4" , Some ( & globals) , None ) . unwrap ( ) ;
744
744
let input = input. to_object ( py) . into_bound ( py) ;
745
745
let result = validator. validate_python ( py, & input, None , None , None , None ) . unwrap ( ) ;
746
746
assert ! ( input. eq( result) . unwrap( ) ) ;
0 commit comments