Skip to content

Commit 9394424

Browse files
committed
refactor schema build to use Py2
1 parent 33f4ad9 commit 9394424

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+689
-678
lines changed

src/build_tools.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::tools::SchemaDict;
1212
use crate::ValidationError;
1313

1414
pub fn schema_or_config<'py, T>(
15-
schema: &'py PyDict,
16-
config: Option<&'py PyDict>,
15+
schema: &Py2<'py, PyDict>,
16+
config: Option<&Py2<'py, PyDict>>,
1717
schema_key: &PyString,
1818
config_key: &PyString,
1919
) -> PyResult<Option<T>>
@@ -30,8 +30,8 @@ where
3030
}
3131

3232
pub fn schema_or_config_same<'py, T>(
33-
schema: &'py PyDict,
34-
config: Option<&'py PyDict>,
33+
schema: &Py2<'py, PyDict>,
34+
config: Option<&Py2<'py, PyDict>>,
3535
key: &PyString,
3636
) -> PyResult<Option<T>>
3737
where
@@ -40,7 +40,7 @@ where
4040
schema_or_config(schema, config, key, key)
4141
}
4242

43-
pub fn is_strict(schema: &PyDict, config: Option<&PyDict>) -> PyResult<bool> {
43+
pub fn is_strict(schema: &Py2<'_, PyDict>, config: Option<&Py2<'_, PyDict>>) -> PyResult<bool> {
4444
let py = schema.py();
4545
Ok(schema_or_config_same(schema, config, intern!(py, "strict"))?.unwrap_or(false))
4646
}
@@ -174,18 +174,18 @@ pub(crate) enum ExtraBehavior {
174174
impl ExtraBehavior {
175175
pub fn from_schema_or_config(
176176
py: Python,
177-
schema: &PyDict,
178-
config: Option<&PyDict>,
177+
schema: &Py2<'_, PyDict>,
178+
config: Option<&Py2<'_, PyDict>>,
179179
default: Self,
180180
) -> PyResult<Self> {
181-
let extra_behavior = schema_or_config::<Option<&str>>(
181+
let extra_behavior = schema_or_config::<Option<Py2<'_, PyString>>>(
182182
schema,
183183
config,
184184
intern!(py, "extra_behavior"),
185185
intern!(py, "extra_fields_behavior"),
186186
)?
187187
.flatten();
188-
let res = match extra_behavior {
188+
let res = match extra_behavior.as_ref().map(|s| s.to_str()).transpose()? {
189189
Some("allow") => Self::Allow,
190190
Some("ignore") => Self::Ignore,
191191
Some("forbid") => Self::Forbid,

src/input/datetime.rs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::tools::py_err;
2020
#[cfg_attr(debug_assertions, derive(Debug))]
2121
pub enum EitherDate<'a> {
2222
Raw(Date),
23-
Py(&'a PyDate),
23+
Py(Py2<'a, PyDate>),
2424
}
2525

2626
impl<'a> From<Date> for EitherDate<'a> {
@@ -29,13 +29,13 @@ impl<'a> From<Date> for EitherDate<'a> {
2929
}
3030
}
3131

32-
impl<'a> From<&'a PyDate> for EitherDate<'a> {
33-
fn from(date: &'a PyDate) -> Self {
32+
impl<'a> From<Py2<'a, PyDate>> for EitherDate<'a> {
33+
fn from(date: Py2<'a, PyDate>) -> Self {
3434
Self::Py(date)
3535
}
3636
}
3737

38-
pub fn pydate_as_date(py_date: &PyAny) -> PyResult<Date> {
38+
pub fn pydate_as_date(py_date: &Py2<'_, PyAny>) -> PyResult<Date> {
3939
let py = py_date.py();
4040
Ok(Date {
4141
year: py_date.getattr(intern!(py, "year"))?.extract()?,
@@ -55,7 +55,7 @@ impl<'a> EitherDate<'a> {
5555
pub fn try_into_py(self, py: Python<'_>) -> PyResult<PyObject> {
5656
let date = match self {
5757
Self::Py(date) => Ok(date),
58-
Self::Raw(date) => PyDate::new2(py, date.year.into(), date.month, date.day).map(Py2::into_gil_ref),
58+
Self::Raw(date) => PyDate::new2(py, date.year.into(), date.month, date.day),
5959
}?;
6060
Ok(date.into_py(py))
6161
}
@@ -64,7 +64,7 @@ impl<'a> EitherDate<'a> {
6464
#[cfg_attr(debug_assertions, derive(Debug))]
6565
pub enum EitherTime<'a> {
6666
Raw(Time),
67-
Py(&'a PyTime),
67+
Py(Py2<'a, PyTime>),
6868
}
6969

7070
impl<'a> From<Time> for EitherTime<'a> {
@@ -73,17 +73,17 @@ impl<'a> From<Time> for EitherTime<'a> {
7373
}
7474
}
7575

76-
impl<'a> From<&'a PyTime> for EitherTime<'a> {
77-
fn from(time: &'a PyTime) -> Self {
76+
impl<'a> From<Py2<'a, PyTime>> for EitherTime<'a> {
77+
fn from(time: Py2<'a, PyTime>) -> Self {
7878
Self::Py(time)
7979
}
8080
}
8181

8282
#[cfg_attr(debug_assertions, derive(Debug))]
8383
pub enum EitherTimedelta<'a> {
8484
Raw(Duration),
85-
PyExact(&'a PyDelta),
86-
PySubclass(&'a PyDelta),
85+
PyExact(Py2<'a, PyDelta>),
86+
PySubclass(Py2<'a, PyDelta>),
8787
}
8888

8989
impl<'a> From<Duration> for EitherTimedelta<'a> {
@@ -101,29 +101,29 @@ impl<'a> EitherTimedelta<'a> {
101101
}
102102
}
103103

104-
pub fn try_into_py(&self, py: Python<'a>) -> PyResult<&'a PyDelta> {
104+
pub fn try_into_py(&self, py: Python<'a>) -> PyResult<Py2<'a, PyDelta>> {
105105
match self {
106-
Self::PyExact(timedelta) => Ok(*timedelta),
107-
Self::PySubclass(timedelta) => Ok(*timedelta),
108-
Self::Raw(duration) => duration_as_pytimedelta(py, duration).map(Py2::into_gil_ref),
106+
Self::PyExact(timedelta) => Ok(timedelta.clone()),
107+
Self::PySubclass(timedelta) => Ok(timedelta.clone()),
108+
Self::Raw(duration) => duration_as_pytimedelta(py, duration),
109109
}
110110
}
111111
}
112112

113-
impl<'a> TryFrom<&'a PyAny> for EitherTimedelta<'a> {
113+
impl<'a> TryFrom<&'_ Py2<'a, PyAny>> for EitherTimedelta<'a> {
114114
type Error = PyErr;
115115

116-
fn try_from(value: &'a PyAny) -> PyResult<Self> {
117-
if let Ok(dt) = <PyDelta as PyTryFrom>::try_from_exact(value) {
118-
Ok(EitherTimedelta::PyExact(dt))
116+
fn try_from(value: &Py2<'a, PyAny>) -> PyResult<Self> {
117+
if let Ok(dt) = value.downcast() {
118+
Ok(EitherTimedelta::PyExact(dt.clone()))
119119
} else {
120120
let dt = value.downcast::<PyDelta>()?;
121-
Ok(EitherTimedelta::PySubclass(dt))
121+
Ok(EitherTimedelta::PySubclass(dt.clone()))
122122
}
123123
}
124124
}
125125

126-
pub fn pytimedelta_exact_as_duration(py_timedelta: &PyDelta) -> Duration {
126+
pub fn pytimedelta_exact_as_duration(py_timedelta: &Py2<'_, PyDelta>) -> Duration {
127127
// see https://docs.python.org/3/c-api/datetime.html#c.PyDateTime_DELTA_GET_DAYS
128128
// days can be negative, but seconds and microseconds are always positive.
129129
let mut days = py_timedelta.get_days(); // -999999999 to 999999999
@@ -146,7 +146,7 @@ pub fn pytimedelta_exact_as_duration(py_timedelta: &PyDelta) -> Duration {
146146
Duration::new(positive, days as u32, seconds as u32, microseconds as u32).unwrap()
147147
}
148148

149-
pub fn pytimedelta_subclass_as_duration(py_timedelta: &PyDelta) -> PyResult<Duration> {
149+
pub fn pytimedelta_subclass_as_duration(py_timedelta: &Py2<'_, PyDelta>) -> PyResult<Duration> {
150150
let total_seconds: f64 = py_timedelta
151151
.call_method0(intern!(py_timedelta.py(), "total_seconds"))?
152152
.extract()?;
@@ -173,16 +173,16 @@ pub fn duration_as_pytimedelta<'py>(py: Python<'py>, duration: &Duration) -> PyR
173173
)
174174
}
175175

176-
pub fn pytime_as_time(py_time: &PyAny, py_dt: Option<&PyAny>) -> PyResult<Time> {
176+
pub fn pytime_as_time(py_time: &Py2<'_, PyAny>, py_dt: Option<&Py2<'_, PyAny>>) -> PyResult<Time> {
177177
let py = py_time.py();
178178

179179
let tzinfo = py_time.getattr(intern!(py, "tzinfo"))?;
180-
let tz_offset: Option<i32> = if tzinfo.is_none() {
180+
let tz_offset: Option<i32> = if PyAnyMethods::is_none(&tzinfo) {
181181
None
182182
} else {
183183
let offset_delta = tzinfo.call_method1(intern!(py, "utcoffset"), (py_dt,))?;
184184
// as per the docs, utcoffset() can return None
185-
if offset_delta.is_none() {
185+
if PyAnyMethods::is_none(&offset_delta) {
186186
None
187187
} else {
188188
let offset_seconds: f64 = offset_delta.call_method0(intern!(py, "total_seconds"))?.extract()?;
@@ -217,8 +217,7 @@ impl<'a> EitherTime<'a> {
217217
time.second,
218218
time.microsecond,
219219
time_as_tzinfo(py, &time)?.as_ref(),
220-
)
221-
.map(Py2::into_gil_ref),
220+
),
222221
}?;
223222
Ok(time.into_py(py))
224223
}
@@ -237,7 +236,7 @@ fn time_as_tzinfo<'py>(py: Python<'py>, time: &Time) -> PyResult<Option<Py2<'py,
237236
#[cfg_attr(debug_assertions, derive(Debug))]
238237
pub enum EitherDateTime<'a> {
239238
Raw(DateTime),
240-
Py(&'a PyDateTime),
239+
Py(Py2<'a, PyDateTime>),
241240
}
242241

243242
impl<'a> From<DateTime> for EitherDateTime<'a> {
@@ -246,13 +245,13 @@ impl<'a> From<DateTime> for EitherDateTime<'a> {
246245
}
247246
}
248247

249-
impl<'a> From<&'a PyDateTime> for EitherDateTime<'a> {
250-
fn from(dt: &'a PyDateTime) -> Self {
248+
impl<'a> From<Py2<'a, PyDateTime>> for EitherDateTime<'a> {
249+
fn from(dt: Py2<'a, PyDateTime>) -> Self {
251250
Self::Py(dt)
252251
}
253252
}
254253

255-
pub fn pydatetime_as_datetime(py_dt: &PyAny) -> PyResult<DateTime> {
254+
pub fn pydatetime_as_datetime(py_dt: &Py2<'_, PyAny>) -> PyResult<DateTime> {
256255
Ok(DateTime {
257256
date: pydate_as_date(py_dt)?,
258257
time: pytime_as_time(py_dt, Some(py_dt))?,
@@ -279,9 +278,8 @@ impl<'a> EitherDateTime<'a> {
279278
datetime.time.second,
280279
datetime.time.microsecond,
281280
time_as_tzinfo(py, &datetime.time)?.as_ref(),
282-
)?
283-
.into_gil_ref(),
284-
Self::Py(dt) => dt,
281+
)?,
282+
Self::Py(dt) => dt.clone(),
285283
};
286284
Ok(dt.into_py(py))
287285
}
@@ -405,7 +403,7 @@ pub fn date_as_datetime(date: &PyDate) -> PyResult<EitherDateTime> {
405403
0,
406404
None,
407405
)?;
408-
Ok(dt.into_gil_ref().into())
406+
Ok(dt.into())
409407
}
410408

411409
const MAX_U32: i64 = u32::MAX as i64;

0 commit comments

Comments
 (0)