@@ -187,26 +187,22 @@ impl<'a> Input<'a> for PyAny {
187
187
}
188
188
189
189
fn strict_str ( & ' a self ) -> ValResult < EitherString < ' a > > {
190
- if let Ok ( py_str) = self . downcast :: < PyString > ( ) {
191
- if is_builtin_str ( py_str) {
192
- Ok ( py_str. into ( ) )
193
- } else {
194
- Err ( ValError :: new ( ErrorType :: StringSubType , self ) )
195
- }
190
+ if let Ok ( py_str) = <PyString as PyTryFrom >:: try_from_exact ( self ) {
191
+ Ok ( py_str. into ( ) )
192
+ } else if PyString :: is_type_of ( self ) {
193
+ Err ( ValError :: new ( ErrorType :: StringSubType , self ) )
196
194
} else {
197
195
Err ( ValError :: new ( ErrorType :: StringType , self ) )
198
196
}
199
197
}
200
198
201
199
fn lax_str ( & ' a self ) -> ValResult < EitherString < ' a > > {
202
- if let Ok ( py_str) = self . downcast :: < PyString > ( ) {
203
- if is_builtin_str ( py_str) {
204
- Ok ( py_str. into ( ) )
205
- } else {
206
- // force to a rust string to make sure behaviour is consistent whether or not we go via a
207
- // rust string in StrConstrainedValidator - e.g. to_lower
208
- Ok ( py_string_str ( py_str) ?. into ( ) )
209
- }
200
+ if let Ok ( py_str) = <PyString as PyTryFrom >:: try_from_exact ( self ) {
201
+ Ok ( py_str. into ( ) )
202
+ } else if let Ok ( py_str) = self . downcast :: < PyString > ( ) {
203
+ // force to a rust string to make sure behaviour is consistent whether or not we go via a
204
+ // rust string in StrConstrainedValidator - e.g. to_lower
205
+ Ok ( py_string_str ( py_str) ?. into ( ) )
210
206
} else if let Ok ( bytes) = self . downcast :: < PyBytes > ( ) {
211
207
let str = match from_utf8 ( bytes. as_bytes ( ) ) {
212
208
Ok ( s) => s,
@@ -647,10 +643,6 @@ fn maybe_as_string(v: &PyAny, unicode_error: ErrorType) -> ValResult<Option<Cow<
647
643
}
648
644
}
649
645
650
- fn is_builtin_str ( py_str : & PyString ) -> bool {
651
- py_str. get_type ( ) . is ( PyString :: type_object ( py_str. py ( ) ) )
652
- }
653
-
654
646
#[ cfg( PyPy ) ]
655
647
static DICT_KEYS_TYPE : pyo3:: once_cell:: GILOnceCell < Py < PyType > > = pyo3:: once_cell:: GILOnceCell :: new ( ) ;
656
648
0 commit comments