1
1
use pyo3:: intern;
2
2
use pyo3:: prelude:: * ;
3
- use pyo3:: types:: PyDict ;
3
+ use pyo3:: types:: { PyDict , PyInt } ;
4
4
5
5
use crate :: build_tools:: is_strict;
6
6
use crate :: errors:: { ErrorType , ValError , ValResult } ;
@@ -72,11 +72,11 @@ impl Validator for IntValidator {
72
72
#[ derive( Debug , Clone ) ]
73
73
pub struct ConstrainedIntValidator {
74
74
strict : bool ,
75
- multiple_of : Option < i64 > ,
76
- le : Option < i64 > ,
77
- lt : Option < i64 > ,
78
- ge : Option < i64 > ,
79
- gt : Option < i64 > ,
75
+ multiple_of : Option < Py < PyInt > > ,
76
+ le : Option < Py < PyInt > > ,
77
+ lt : Option < Py < PyInt > > ,
78
+ ge : Option < Py < PyInt > > ,
79
+ gt : Option < Py < PyInt > > ,
80
80
}
81
81
82
82
impl Validator for ConstrainedIntValidator {
@@ -89,38 +89,62 @@ impl Validator for ConstrainedIntValidator {
89
89
_recursion_guard : & ' s mut RecursionGuard ,
90
90
) -> ValResult < ' data , PyObject > {
91
91
let either_int = input. validate_int ( extra. strict . unwrap_or ( self . strict ) ) ?;
92
- let int: i64 = either_int. try_into ( ) ?;
93
- if let Some ( multiple_of) = self . multiple_of {
94
- if int % multiple_of != 0 {
92
+ let int_obj = either_int. into_py ( py) ;
93
+ let int = int_obj. as_ref ( py) ;
94
+
95
+ if let Some ( ref multiple_of) = self . multiple_of {
96
+ let rem: i64 = int. call_method1 ( intern ! ( py, "__mod__" ) , ( multiple_of, ) ) ?. extract ( ) ?;
97
+ if rem != 0 {
95
98
return Err ( ValError :: new (
96
99
ErrorType :: MultipleOf {
97
- multiple_of : multiple_of. into ( ) ,
100
+ multiple_of : multiple_of. extract :: < i64 > ( py ) ? . into ( ) ,
98
101
} ,
99
102
input,
100
103
) ) ;
101
104
}
102
105
}
103
- if let Some ( le) = self . le {
104
- if int > le {
105
- return Err ( ValError :: new ( ErrorType :: LessThanEqual { le : le. into ( ) } , input) ) ;
106
+
107
+ if let Some ( ref le) = self . le {
108
+ if !int. le ( le) ? {
109
+ return Err ( ValError :: new (
110
+ ErrorType :: LessThanEqual {
111
+ le : le. extract :: < i64 > ( py) ?. into ( ) ,
112
+ } ,
113
+ input,
114
+ ) ) ;
106
115
}
107
116
}
108
- if let Some ( lt) = self . lt {
109
- if int >= lt {
110
- return Err ( ValError :: new ( ErrorType :: LessThan { lt : lt. into ( ) } , input) ) ;
117
+ if let Some ( ref lt) = self . lt {
118
+ if !int. lt ( lt) ? {
119
+ return Err ( ValError :: new (
120
+ ErrorType :: LessThan {
121
+ lt : lt. extract :: < i64 > ( py) ?. into ( ) ,
122
+ } ,
123
+ input,
124
+ ) ) ;
111
125
}
112
126
}
113
- if let Some ( ge) = self . ge {
114
- if int < ge {
115
- return Err ( ValError :: new ( ErrorType :: GreaterThanEqual { ge : ge. into ( ) } , input) ) ;
127
+ if let Some ( ref ge) = self . ge {
128
+ if !int. ge ( ge) ? {
129
+ return Err ( ValError :: new (
130
+ ErrorType :: GreaterThanEqual {
131
+ ge : ge. extract :: < i64 > ( py) ?. into ( ) ,
132
+ } ,
133
+ input,
134
+ ) ) ;
116
135
}
117
136
}
118
- if let Some ( gt) = self . gt {
119
- if int <= gt {
120
- return Err ( ValError :: new ( ErrorType :: GreaterThan { gt : gt. into ( ) } , input) ) ;
137
+ if let Some ( ref gt) = self . gt {
138
+ if !int. gt ( gt) ? {
139
+ return Err ( ValError :: new (
140
+ ErrorType :: GreaterThan {
141
+ gt : gt. extract :: < i64 > ( py) ?. into ( ) ,
142
+ } ,
143
+ input,
144
+ ) ) ;
121
145
}
122
146
}
123
- Ok ( int . into_py ( py ) )
147
+ Ok ( int_obj )
124
148
}
125
149
126
150
fn different_strict_behavior (
0 commit comments