@@ -20,7 +20,7 @@ use crate::tools::py_err;
20
20
#[ cfg_attr( debug_assertions, derive( Debug ) ) ]
21
21
pub enum EitherDate < ' a > {
22
22
Raw ( Date ) ,
23
- Py ( & ' a PyDate ) ,
23
+ Py ( Py2 < ' a , PyDate > ) ,
24
24
}
25
25
26
26
impl < ' a > From < Date > for EitherDate < ' a > {
@@ -29,13 +29,13 @@ impl<'a> From<Date> for EitherDate<'a> {
29
29
}
30
30
}
31
31
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 {
34
34
Self :: Py ( date)
35
35
}
36
36
}
37
37
38
- pub fn pydate_as_date ( py_date : & PyAny ) -> PyResult < Date > {
38
+ pub fn pydate_as_date ( py_date : & Py2 < ' _ , PyAny > ) -> PyResult < Date > {
39
39
let py = py_date. py ( ) ;
40
40
Ok ( Date {
41
41
year : py_date. getattr ( intern ! ( py, "year" ) ) ?. extract ( ) ?,
@@ -55,7 +55,7 @@ impl<'a> EitherDate<'a> {
55
55
pub fn try_into_py ( self , py : Python < ' _ > ) -> PyResult < PyObject > {
56
56
let date = match self {
57
57
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 ) ,
59
59
} ?;
60
60
Ok ( date. into_py ( py) )
61
61
}
@@ -64,7 +64,7 @@ impl<'a> EitherDate<'a> {
64
64
#[ cfg_attr( debug_assertions, derive( Debug ) ) ]
65
65
pub enum EitherTime < ' a > {
66
66
Raw ( Time ) ,
67
- Py ( & ' a PyTime ) ,
67
+ Py ( Py2 < ' a , PyTime > ) ,
68
68
}
69
69
70
70
impl < ' a > From < Time > for EitherTime < ' a > {
@@ -73,17 +73,17 @@ impl<'a> From<Time> for EitherTime<'a> {
73
73
}
74
74
}
75
75
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 {
78
78
Self :: Py ( time)
79
79
}
80
80
}
81
81
82
82
#[ cfg_attr( debug_assertions, derive( Debug ) ) ]
83
83
pub enum EitherTimedelta < ' a > {
84
84
Raw ( Duration ) ,
85
- PyExact ( & ' a PyDelta ) ,
86
- PySubclass ( & ' a PyDelta ) ,
85
+ PyExact ( Py2 < ' a , PyDelta > ) ,
86
+ PySubclass ( Py2 < ' a , PyDelta > ) ,
87
87
}
88
88
89
89
impl < ' a > From < Duration > for EitherTimedelta < ' a > {
@@ -101,29 +101,29 @@ impl<'a> EitherTimedelta<'a> {
101
101
}
102
102
}
103
103
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 > > {
105
105
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) ,
109
109
}
110
110
}
111
111
}
112
112
113
- impl < ' a > TryFrom < & ' a PyAny > for EitherTimedelta < ' a > {
113
+ impl < ' a > TryFrom < & ' _ Py2 < ' a , PyAny > > for EitherTimedelta < ' a > {
114
114
type Error = PyErr ;
115
115
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 ( ) ) )
119
119
} else {
120
120
let dt = value. downcast :: < PyDelta > ( ) ?;
121
- Ok ( EitherTimedelta :: PySubclass ( dt) )
121
+ Ok ( EitherTimedelta :: PySubclass ( dt. clone ( ) ) )
122
122
}
123
123
}
124
124
}
125
125
126
- pub fn pytimedelta_exact_as_duration ( py_timedelta : & PyDelta ) -> Duration {
126
+ pub fn pytimedelta_exact_as_duration ( py_timedelta : & Py2 < ' _ , PyDelta > ) -> Duration {
127
127
// see https://docs.python.org/3/c-api/datetime.html#c.PyDateTime_DELTA_GET_DAYS
128
128
// days can be negative, but seconds and microseconds are always positive.
129
129
let mut days = py_timedelta. get_days ( ) ; // -999999999 to 999999999
@@ -146,7 +146,7 @@ pub fn pytimedelta_exact_as_duration(py_timedelta: &PyDelta) -> Duration {
146
146
Duration :: new ( positive, days as u32 , seconds as u32 , microseconds as u32 ) . unwrap ( )
147
147
}
148
148
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 > {
150
150
let total_seconds: f64 = py_timedelta
151
151
. call_method0 ( intern ! ( py_timedelta. py( ) , "total_seconds" ) ) ?
152
152
. extract ( ) ?;
@@ -173,16 +173,16 @@ pub fn duration_as_pytimedelta<'py>(py: Python<'py>, duration: &Duration) -> PyR
173
173
)
174
174
}
175
175
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 > {
177
177
let py = py_time. py ( ) ;
178
178
179
179
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 ) {
181
181
None
182
182
} else {
183
183
let offset_delta = tzinfo. call_method1 ( intern ! ( py, "utcoffset" ) , ( py_dt, ) ) ?;
184
184
// as per the docs, utcoffset() can return None
185
- if offset_delta . is_none ( ) {
185
+ if PyAnyMethods :: is_none ( & offset_delta ) {
186
186
None
187
187
} else {
188
188
let offset_seconds: f64 = offset_delta. call_method0 ( intern ! ( py, "total_seconds" ) ) ?. extract ( ) ?;
@@ -217,8 +217,7 @@ impl<'a> EitherTime<'a> {
217
217
time. second ,
218
218
time. microsecond ,
219
219
time_as_tzinfo ( py, & time) ?. as_ref ( ) ,
220
- )
221
- . map ( Py2 :: into_gil_ref) ,
220
+ ) ,
222
221
} ?;
223
222
Ok ( time. into_py ( py) )
224
223
}
@@ -237,7 +236,7 @@ fn time_as_tzinfo<'py>(py: Python<'py>, time: &Time) -> PyResult<Option<Py2<'py,
237
236
#[ cfg_attr( debug_assertions, derive( Debug ) ) ]
238
237
pub enum EitherDateTime < ' a > {
239
238
Raw ( DateTime ) ,
240
- Py ( & ' a PyDateTime ) ,
239
+ Py ( Py2 < ' a , PyDateTime > ) ,
241
240
}
242
241
243
242
impl < ' a > From < DateTime > for EitherDateTime < ' a > {
@@ -246,13 +245,13 @@ impl<'a> From<DateTime> for EitherDateTime<'a> {
246
245
}
247
246
}
248
247
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 {
251
250
Self :: Py ( dt)
252
251
}
253
252
}
254
253
255
- pub fn pydatetime_as_datetime ( py_dt : & PyAny ) -> PyResult < DateTime > {
254
+ pub fn pydatetime_as_datetime ( py_dt : & Py2 < ' _ , PyAny > ) -> PyResult < DateTime > {
256
255
Ok ( DateTime {
257
256
date : pydate_as_date ( py_dt) ?,
258
257
time : pytime_as_time ( py_dt, Some ( py_dt) ) ?,
@@ -279,9 +278,8 @@ impl<'a> EitherDateTime<'a> {
279
278
datetime. time . second ,
280
279
datetime. time . microsecond ,
281
280
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 ( ) ,
285
283
} ;
286
284
Ok ( dt. into_py ( py) )
287
285
}
@@ -405,7 +403,7 @@ pub fn date_as_datetime(date: &PyDate) -> PyResult<EitherDateTime> {
405
403
0 ,
406
404
None ,
407
405
) ?;
408
- Ok ( dt. into_gil_ref ( ) . into ( ) )
406
+ Ok ( dt. into ( ) )
409
407
}
410
408
411
409
const MAX_U32 : i64 = u32:: MAX as i64 ;
0 commit comments