Skip to content

Commit 2cce828

Browse files
committed
push model_fields over to Py2
1 parent 15f1299 commit 2cce828

File tree

14 files changed

+192
-111
lines changed

14 files changed

+192
-111
lines changed

src/errors/location.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl ToPyObject for Location {
128128
match self {
129129
Self::List(loc) => PyTuple::new(py, loc.iter().rev()).to_object(py),
130130
Self::Empty => EMPTY_TUPLE
131-
.get_or_init(py, || PyTuple::empty(py).to_object(py))
131+
.get_or_init(py, || PyTuple::empty2(py).to_object(py))
132132
.clone_ref(py),
133133
}
134134
}

src/input/return_enums.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,10 @@ pub struct DictGenericIterator<'py> {
460460
}
461461

462462
impl<'py> DictGenericIterator<'py> {
463-
pub fn new(dict: &'py PyDict) -> ValResult<Self> {
464-
Ok(Self { dict_iter: dict.iter() })
463+
pub fn new(dict: &Py2<'py, PyDict>) -> ValResult<Self> {
464+
Ok(Self {
465+
dict_iter: dict.clone().into_gil_ref().iter(),
466+
})
465467
}
466468
}
467469

@@ -490,15 +492,18 @@ fn mapping_err<'py>(err: PyErr, py: Python<'py>, input: &'py impl Input<'py>) ->
490492
}
491493

492494
impl<'py> MappingGenericIterator<'py> {
493-
pub fn new(mapping: &'py PyMapping) -> ValResult<Self> {
495+
pub fn new(mapping: &Py2<'py, PyMapping>) -> ValResult<Self> {
494496
let py = mapping.py();
495-
let input: &PyAny = mapping;
497+
let input: &PyAny = (**mapping).clone().into_gil_ref();
496498
let iter = mapping
497499
.items()
498500
.map_err(|e| mapping_err(e, py, input))?
499501
.iter()
500502
.map_err(|e| mapping_err(e, py, input))?;
501-
Ok(Self { input, iter })
503+
Ok(Self {
504+
input,
505+
iter: unsafe { mapping.py().from_owned_ptr(iter.into_ptr()) },
506+
})
502507
}
503508
}
504509

@@ -528,8 +533,10 @@ pub struct StringMappingGenericIterator<'py> {
528533
}
529534

530535
impl<'py> StringMappingGenericIterator<'py> {
531-
pub fn new(dict: &'py PyDict) -> ValResult<Self> {
532-
Ok(Self { dict_iter: dict.iter() })
536+
pub fn new(dict: &Py2<'py, PyDict>) -> ValResult<Self> {
537+
Ok(Self {
538+
dict_iter: dict.clone().into_gil_ref().iter(),
539+
})
533540
}
534541
}
535542

@@ -562,10 +569,11 @@ pub struct AttributesGenericIterator<'py> {
562569
}
563570

564571
impl<'py> AttributesGenericIterator<'py> {
565-
pub fn new(py_any: &'py PyAny) -> ValResult<Self> {
572+
pub fn new(py_any: &Py2<'py, PyAny>) -> ValResult<Self> {
573+
let attributes_iterator = py_any.dir().into_gil_ref().into_iter();
566574
Ok(Self {
567-
object: py_any,
568-
attributes_iterator: py_any.dir().into_iter(),
575+
object: py_any.clone().into_gil_ref(),
576+
attributes_iterator,
569577
})
570578
}
571579
}
@@ -713,14 +721,17 @@ impl GenericJsonIterator {
713721
}
714722

715723
#[cfg_attr(debug_assertions, derive(Debug))]
716-
pub struct PyArgs<'a> {
717-
pub args: Option<&'a PyTuple>,
718-
pub kwargs: Option<&'a PyDict>,
724+
pub struct PyArgs<'py> {
725+
pub args: Option<Py2<'py, PyTuple>>,
726+
pub kwargs: Option<Py2<'py, PyDict>>,
719727
}
720728

721729
impl<'a> PyArgs<'a> {
722730
pub fn new(args: Option<&'a PyTuple>, kwargs: Option<&'a PyDict>) -> Self {
723-
Self { args, kwargs }
731+
Self {
732+
args: args.map(|args| Py2::borrowed_from_gil_ref(&args).clone()),
733+
kwargs: kwargs.map(|kwargs| Py2::borrowed_from_gil_ref(&kwargs).clone()),
734+
}
724735
}
725736
}
726737

src/lookup_key.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ impl LookupKey {
108108
}
109109
}
110110

111-
pub fn py_get_dict_item<'data, 's>(
111+
pub fn py_get_dict_item<'py, 's>(
112112
&'s self,
113-
dict: &'data PyDict,
114-
) -> ValResult<Option<(&'s LookupPath, &'data PyAny)>> {
113+
dict: &Py2<'py, PyDict>,
114+
) -> ValResult<Option<(&'s LookupPath, Py2<'py, PyAny>)>> {
115115
match self {
116116
Self::Simple { py_key, path, .. } => match dict.get_item(py_key)? {
117117
Some(value) => Ok(Some((path, value))),
@@ -134,7 +134,7 @@ impl LookupKey {
134134
for path in path_choices {
135135
// iterate over the path and plug each value into the py_any from the last step, starting with dict
136136
// this could just be a loop but should be somewhat faster with a functional design
137-
if let Some(v) = path.iter().try_fold(dict as &PyAny, |d, loc| loc.py_get_item(d)) {
137+
if let Some(v) = path.iter().try_fold((**dict).clone(), |d, loc| loc.py_get_item(&d)) {
138138
// Successfully found an item, return it
139139
return Ok(Some((path, v)));
140140
}
@@ -145,22 +145,22 @@ impl LookupKey {
145145
}
146146
}
147147

148-
pub fn py_get_string_mapping_item<'data, 's>(
148+
pub fn py_get_string_mapping_item<'py, 's>(
149149
&'s self,
150-
dict: &'data PyDict,
151-
) -> ValResult<Option<(&'s LookupPath, StringMapping<'data>)>> {
150+
dict: &Py2<'py, PyDict>,
151+
) -> ValResult<Option<(&'s LookupPath, StringMapping<'py>)>> {
152152
if let Some((path, py_any)) = self.py_get_dict_item(dict)? {
153-
let value = StringMapping::new_value(py_any)?;
153+
let value = StringMapping::new_value(py_any.into_gil_ref())?;
154154
Ok(Some((path, value)))
155155
} else {
156156
Ok(None)
157157
}
158158
}
159159

160-
pub fn py_get_mapping_item<'data, 's>(
160+
pub fn py_get_mapping_item<'py, 's>(
161161
&'s self,
162-
dict: &'data PyMapping,
163-
) -> ValResult<Option<(&'s LookupPath, &'data PyAny)>> {
162+
dict: &Py2<'py, PyMapping>,
163+
) -> ValResult<Option<(&'s LookupPath, Py2<'py, PyAny>)>> {
164164
match self {
165165
Self::Simple { py_key, path, .. } => match dict.get_item(py_key) {
166166
Ok(value) => Ok(Some((path, value))),
@@ -183,7 +183,7 @@ impl LookupKey {
183183
for path in path_choices {
184184
// iterate over the path and plug each value into the py_any from the last step, starting with dict
185185
// this could just be a loop but should be somewhat faster with a functional design
186-
if let Some(v) = path.iter().try_fold(dict as &PyAny, |d, loc| loc.py_get_item(d)) {
186+
if let Some(v) = path.iter().try_fold((**dict).clone(), |d, loc| loc.py_get_item(&d)) {
187187
// Successfully found an item, return it
188188
return Ok(Some((path, v)));
189189
}
@@ -194,28 +194,28 @@ impl LookupKey {
194194
}
195195
}
196196

197-
pub fn py_get_attr<'data, 's>(
197+
pub fn py_get_attr<'py, 's>(
198198
&'s self,
199-
obj: &'data PyAny,
200-
kwargs: Option<&'data PyDict>,
201-
) -> ValResult<Option<(&'s LookupPath, &'data PyAny)>> {
199+
obj: &Py2<'py, PyAny>,
200+
kwargs: Option<&Py2<'py, PyDict>>,
201+
) -> ValResult<Option<(&'s LookupPath, Py2<'py, PyAny>)>> {
202202
match self._py_get_attr(obj, kwargs) {
203203
Ok(v) => Ok(v),
204204
Err(err) => {
205205
let error = py_err_string(obj.py(), err);
206206
Err(ValError::new(
207207
ErrorType::GetAttributeError { error, context: None },
208-
obj,
208+
obj.as_gil_ref(),
209209
))
210210
}
211211
}
212212
}
213213

214-
pub fn _py_get_attr<'data, 's>(
214+
pub fn _py_get_attr<'py, 's>(
215215
&'s self,
216-
obj: &'data PyAny,
217-
kwargs: Option<&'data PyDict>,
218-
) -> PyResult<Option<(&'s LookupPath, &'data PyAny)>> {
216+
obj: &Py2<'py, PyAny>,
217+
kwargs: Option<&Py2<'py, PyDict>>,
218+
) -> PyResult<Option<(&'s LookupPath, Py2<'py, PyAny>)>> {
219219
if let Some(dict) = kwargs {
220220
if let Ok(Some(item)) = self.py_get_dict_item(dict) {
221221
return Ok(Some(item));
@@ -244,9 +244,9 @@ impl LookupKey {
244244
'outer: for path in path_choices {
245245
// similar to above, but using `py_get_attrs`, we can't use try_fold because of the extra Err
246246
// so we have to loop manually
247-
let mut v = obj;
247+
let mut v = obj.clone();
248248
for loc in path.iter() {
249-
v = match loc.py_get_attrs(v) {
249+
v = match loc.py_get_attrs(&v) {
250250
Ok(Some(v)) => v,
251251
Ok(None) => {
252252
continue 'outer;
@@ -446,7 +446,7 @@ impl PathItem {
446446
}
447447
}
448448

449-
pub fn py_get_item<'a>(&self, py_any: &'a PyAny) -> Option<&'a PyAny> {
449+
pub fn py_get_item<'py>(&self, py_any: &Py2<'py, PyAny>) -> Option<Py2<'py, PyAny>> {
450450
// we definitely don't want to index strings, so explicitly omit this case
451451
if py_any.downcast::<PyString>().is_ok() {
452452
None
@@ -463,7 +463,7 @@ impl PathItem {
463463
}
464464
}
465465

466-
pub fn py_get_attrs<'a>(&self, obj: &'a PyAny) -> PyResult<Option<&'a PyAny>> {
466+
pub fn py_get_attrs<'py>(&self, obj: &Py2<'py, PyAny>) -> PyResult<Option<Py2<'py, PyAny>>> {
467467
match self {
468468
Self::S(_, py_key) => {
469469
// if obj is a dict, we want to use get_item, not getattr
@@ -506,7 +506,7 @@ impl PathItem {
506506

507507
/// wrapper around `getattr` that returns `Ok(None)` for attribute errors, but returns other errors
508508
/// We don't check `try_from_attributes` because that check was performed on the top level object before we got here
509-
fn py_get_attrs<'a>(obj: &'a PyAny, attr_name: &Py<PyString>) -> PyResult<Option<&'a PyAny>> {
509+
fn py_get_attrs<'py>(obj: &Py2<'py, PyAny>, attr_name: &Py<PyString>) -> PyResult<Option<Py2<'py, PyAny>>> {
510510
match obj.getattr(attr_name.extract::<&PyString>(obj.py())?) {
511511
Ok(attr) => Ok(Some(attr)),
512512
Err(err) => {

src/validators/arguments.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ahash::AHashSet;
77
use crate::build_tools::py_schema_err;
88
use crate::build_tools::schema_or_config_same;
99
use crate::errors::{AsLocItem, ErrorTypeDefaults, ValError, ValLineError, ValResult};
10-
use crate::input::{GenericArguments, Input, ValidationMatch};
10+
use crate::input::{BorrowInput, GenericArguments, Input, ValidationMatch};
1111
use crate::lookup_key::LookupKey;
1212

1313
use crate::tools::SchemaDict;
@@ -179,15 +179,15 @@ impl Validator for ArgumentsValidator {
179179
// go through arguments getting the value from args or kwargs and validating it
180180
for (index, parameter) in self.parameters.iter().enumerate() {
181181
let mut pos_value = None;
182-
if let Some(args) = $args.args {
182+
if let Some(args) = &$args.args {
183183
if parameter.positional {
184184
pos_value = $get_macro!(args, index);
185185
}
186186
}
187187
let mut kw_value = None;
188-
if let Some(kwargs) = $args.kwargs {
188+
if let Some(kwargs) = &$args.kwargs {
189189
if let Some(ref lookup_key) = parameter.kw_lookup_key {
190-
if let Some((lookup_path, value)) = lookup_key.$get_method(kwargs)? {
190+
if let Some((lookup_path, value)) = lookup_key.$get_method(&kwargs)? {
191191
used_kwargs.insert(lookup_path.first_key());
192192
kw_value = Some((lookup_path, value));
193193
}
@@ -198,12 +198,12 @@ impl Validator for ArgumentsValidator {
198198
(Some(_), Some((_, kw_value))) => {
199199
errors.push(ValLineError::new_with_loc(
200200
ErrorTypeDefaults::MultipleArgumentValues,
201-
kw_value,
201+
kw_value.borrow_input(),
202202
parameter.name.clone(),
203203
));
204204
}
205205
(Some(pos_value), None) => {
206-
match parameter.validator.validate(py, pos_value, state)
206+
match parameter.validator.validate(py, pos_value.borrow_input(), state)
207207
{
208208
Ok(value) => output_args.push(value),
209209
Err(ValError::LineErrors(line_errors)) => {
@@ -213,7 +213,7 @@ impl Validator for ArgumentsValidator {
213213
}
214214
}
215215
(None, Some((lookup_path, kw_value))) => {
216-
match parameter.validator.validate(py, kw_value, state)
216+
match parameter.validator.validate(py, kw_value.borrow_input(), state)
217217
{
218218
Ok(value) => output_kwargs.set_item(parameter.kwarg_key.as_ref().unwrap(), value)?,
219219
Err(ValError::LineErrors(line_errors)) => {
@@ -255,7 +255,7 @@ impl Validator for ArgumentsValidator {
255255
if len > self.positional_params_count {
256256
if let Some(ref validator) = self.var_args_validator {
257257
for (index, item) in $slice_macro!(args, self.positional_params_count, len).iter().enumerate() {
258-
match validator.validate(py, item, state) {
258+
match validator.validate(py, item.borrow_input(), state) {
259259
Ok(value) => output_args.push(value),
260260
Err(ValError::LineErrors(line_errors)) => {
261261
errors.extend(
@@ -271,7 +271,7 @@ impl Validator for ArgumentsValidator {
271271
for (index, item) in $slice_macro!(args, self.positional_params_count, len).iter().enumerate() {
272272
errors.push(ValLineError::new_with_loc(
273273
ErrorTypeDefaults::UnexpectedPositionalArgument,
274-
item,
274+
item.borrow_input(),
275275
index + self.positional_params_count,
276276
));
277277
}
@@ -282,6 +282,9 @@ impl Validator for ArgumentsValidator {
282282
if let Some(kwargs) = $args.kwargs {
283283
if kwargs.len() > used_kwargs.len() {
284284
for (raw_key, value) in kwargs.iter() {
285+
// FIXME workaround for Py2 not having input yet
286+
let raw_key = raw_key.borrow_input();
287+
let value = value.borrow_input();
285288
let either_str = match raw_key.validate_str(true, false).map(ValidationMatch::into_inner) {
286289
Ok(k) => k,
287290
Err(ValError::LineErrors(line_errors)) => {

0 commit comments

Comments
 (0)