Skip to content

Commit a9b0571

Browse files
committed
Add test to fix #220
1 parent 4c0ddbe commit a9b0571

File tree

5 files changed

+268
-231
lines changed

5 files changed

+268
-231
lines changed

examples/rustapi_module/setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ def get_py_version_cfgs():
2525

2626
py3_min = 5
2727
out_cfg = []
28-
for minor in range(py3_min, version[1]+1):
28+
for minor in range(py3_min, version[1] + 1):
2929
out_cfg.append('--cfg=Py_3_%d' % minor)
3030

3131
return out_cfg
3232

33+
3334
install_requires = []
3435
tests_require = install_requires + ['pytest', 'pytest-benchmark']
3536

@@ -49,6 +50,8 @@ def get_py_version_cfgs():
4950
rust_extensions=[RustExtension('rustapi_module.othermod', 'Cargo.toml',
5051
rustc_flags=get_py_version_cfgs()),
5152
RustExtension('rustapi_module.datetime', 'Cargo.toml',
53+
rustc_flags=get_py_version_cfgs()),
54+
RustExtension('rustapi_module.subclassing', 'Cargo.toml',
5255
rustc_flags=get_py_version_cfgs())],
5356
install_requires=install_requires,
5457
tests_require=tests_require,
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
use pyo3::prelude::*;
2+
use pyo3::types::{
3+
PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess,
4+
PyTuple, PyTzInfo,
5+
};
6+
7+
#[pyfunction]
8+
fn make_date(py: Python, year: i32, month: u8, day: u8) -> PyResult<Py<PyDate>> {
9+
PyDate::new(py, year, month, day)
10+
}
11+
12+
#[pyfunction]
13+
fn get_date_tuple(py: Python, d: &PyDate) -> Py<PyTuple> {
14+
PyTuple::new(
15+
py,
16+
&[d.get_year(), d.get_month() as i32, d.get_day() as i32],
17+
)
18+
}
19+
20+
#[pyfunction]
21+
fn date_from_timestamp(py: Python, timestamp: i64) -> PyResult<Py<PyDate>> {
22+
PyDate::from_timestamp(py, timestamp)
23+
}
24+
25+
#[pyfunction]
26+
fn make_time(
27+
py: Python,
28+
hour: u8,
29+
minute: u8,
30+
second: u8,
31+
microsecond: u32,
32+
tzinfo: Option<&PyTzInfo>,
33+
) -> PyResult<Py<PyTime>> {
34+
PyTime::new(
35+
py,
36+
hour,
37+
minute,
38+
second,
39+
microsecond,
40+
tzinfo.map(|o| o.to_object(py)).as_ref(),
41+
)
42+
}
43+
44+
#[cfg(Py_3_6)]
45+
#[pyfunction]
46+
fn time_with_fold(
47+
py: Python,
48+
hour: u8,
49+
minute: u8,
50+
second: u8,
51+
microsecond: u32,
52+
tzinfo: Option<&PyTzInfo>,
53+
fold: bool,
54+
) -> PyResult<Py<PyTime>> {
55+
PyTime::new_with_fold(
56+
py,
57+
hour,
58+
minute,
59+
second,
60+
microsecond,
61+
tzinfo.map(|o| o.to_object(py)).as_ref(),
62+
fold,
63+
)
64+
}
65+
66+
#[pyfunction]
67+
fn get_time_tuple(py: Python, dt: &PyTime) -> Py<PyTuple> {
68+
PyTuple::new(
69+
py,
70+
&[
71+
dt.get_hour() as u32,
72+
dt.get_minute() as u32,
73+
dt.get_second() as u32,
74+
dt.get_microsecond(),
75+
],
76+
)
77+
}
78+
79+
#[cfg(Py_3_6)]
80+
#[pyfunction]
81+
fn get_time_tuple_fold(py: Python, dt: &PyTime) -> Py<PyTuple> {
82+
PyTuple::new(
83+
py,
84+
&[
85+
dt.get_hour() as u32,
86+
dt.get_minute() as u32,
87+
dt.get_second() as u32,
88+
dt.get_microsecond(),
89+
dt.get_fold() as u32,
90+
],
91+
)
92+
}
93+
94+
#[pyfunction]
95+
fn make_delta(py: Python, days: i32, seconds: i32, microseconds: i32) -> PyResult<Py<PyDelta>> {
96+
PyDelta::new(py, days, seconds, microseconds, true)
97+
}
98+
99+
#[pyfunction]
100+
fn get_delta_tuple(py: Python, delta: &PyDelta) -> Py<PyTuple> {
101+
PyTuple::new(
102+
py,
103+
&[
104+
delta.get_days(),
105+
delta.get_seconds(),
106+
delta.get_microseconds(),
107+
],
108+
)
109+
}
110+
111+
#[pyfunction]
112+
fn make_datetime(
113+
py: Python,
114+
year: i32,
115+
month: u8,
116+
day: u8,
117+
hour: u8,
118+
minute: u8,
119+
second: u8,
120+
microsecond: u32,
121+
tzinfo: Option<&PyTzInfo>,
122+
) -> PyResult<Py<PyDateTime>> {
123+
PyDateTime::new(
124+
py,
125+
year,
126+
month,
127+
day,
128+
hour,
129+
minute,
130+
second,
131+
microsecond,
132+
tzinfo.map(|o| (o.to_object(py))).as_ref(),
133+
)
134+
}
135+
136+
#[pyfunction]
137+
fn get_datetime_tuple(py: Python, dt: &PyDateTime) -> Py<PyTuple> {
138+
PyTuple::new(
139+
py,
140+
&[
141+
dt.get_year(),
142+
dt.get_month() as i32,
143+
dt.get_day() as i32,
144+
dt.get_hour() as i32,
145+
dt.get_minute() as i32,
146+
dt.get_second() as i32,
147+
dt.get_microsecond() as i32,
148+
],
149+
)
150+
}
151+
152+
#[cfg(Py_3_6)]
153+
#[pyfunction]
154+
fn get_datetime_tuple_fold(py: Python, dt: &PyDateTime) -> Py<PyTuple> {
155+
PyTuple::new(
156+
py,
157+
&[
158+
dt.get_year(),
159+
dt.get_month() as i32,
160+
dt.get_day() as i32,
161+
dt.get_hour() as i32,
162+
dt.get_minute() as i32,
163+
dt.get_second() as i32,
164+
dt.get_microsecond() as i32,
165+
dt.get_fold() as i32,
166+
],
167+
)
168+
}
169+
170+
#[pyfunction]
171+
fn datetime_from_timestamp(py: Python, ts: f64, tz: Option<&PyTzInfo>) -> PyResult<Py<PyDateTime>> {
172+
PyDateTime::from_timestamp(py, ts, tz)
173+
}
174+
175+
#[pyfunction]
176+
fn issue_219() -> PyResult<()> {
177+
let gil = Python::acquire_gil();
178+
let _py = gil.python();
179+
Ok(())
180+
}
181+
182+
#[pyclass(extends=PyTzInfo)]
183+
pub struct TzClass {}
184+
185+
#[pymethods]
186+
impl TzClass {
187+
#[new]
188+
fn __new__(obj: &PyRawObject) -> PyResult<()> {
189+
obj.init(|_| TzClass {})
190+
}
191+
192+
fn utcoffset(&self, py: Python, _dt: &PyDateTime) -> PyResult<Py<PyDelta>> {
193+
PyDelta::new(py, 0, 3600, 0, true)
194+
}
195+
196+
fn tzname(&self, _py: Python, _dt: &PyDateTime) -> PyResult<String> {
197+
Ok(String::from("+01:00"))
198+
}
199+
200+
fn dst(&self, _py: Python, _dt: &PyDateTime) -> PyResult<Option<&PyDelta>> {
201+
Ok(None)
202+
}
203+
}
204+
205+
#[pymodinit]
206+
fn datetime(_py: Python, m: &PyModule) -> PyResult<()> {
207+
m.add_function(wrap_function!(make_date))?;
208+
m.add_function(wrap_function!(get_date_tuple))?;
209+
m.add_function(wrap_function!(date_from_timestamp))?;
210+
m.add_function(wrap_function!(make_time))?;
211+
m.add_function(wrap_function!(get_time_tuple))?;
212+
m.add_function(wrap_function!(make_delta))?;
213+
m.add_function(wrap_function!(get_delta_tuple))?;
214+
m.add_function(wrap_function!(make_datetime))?;
215+
m.add_function(wrap_function!(get_datetime_tuple))?;
216+
m.add_function(wrap_function!(datetime_from_timestamp))?;
217+
218+
// Python 3.6+ functions
219+
#[cfg(Py_3_6)]
220+
{
221+
m.add_function(wrap_function!(time_with_fold))?;
222+
m.add_function(wrap_function!(get_time_tuple_fold))?;
223+
m.add_function(wrap_function!(get_datetime_tuple_fold))?;
224+
}
225+
226+
m.add_function(wrap_function!(issue_219))?;
227+
228+
m.add_class::<TzClass>()?;
229+
Ok(())
230+
}

0 commit comments

Comments
 (0)