Add failing test to create tzinfo subclass

Currently this fails at import time with undefined symbol:
PyDateTime_TZInfoType
This commit is contained in:
Paul Ganssle 2018-09-27 18:02:29 -04:00 committed by kngwyu
parent 157fd7b2cf
commit 2fa3b942f2
2 changed files with 34 additions and 2 deletions

View File

@ -186,6 +186,29 @@ fn issue_219() -> PyResult<()> {
Ok(())
}
#[pyclass(extends=PyTzInfo)]
pub struct TzClass {}
#[pymethods]
impl TzClass {
#[new]
fn __new__(obj: &PyRawObject) -> PyResult<()> {
obj.init(|_| TzClass {})
}
fn utcoffset(&self, py: Python, dt: &PyDateTime) -> PyResult<Py<PyDelta>> {
PyDelta::new(py, 0, 3600, 0, true)
}
fn tzname(&self, py: Python, dt: &PyDateTime) -> PyResult<String> {
Ok(String::from("+01:00"))
}
fn dst(&self, py: Python, dt: &PyDateTime) -> PyResult<Option<&PyDelta>> {
Ok(None)
}
}
#[pymodinit]
fn datetime(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_function!(make_date))?;
@ -209,5 +232,6 @@ fn datetime(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_function!(issue_219))?;
m.add_class::<TzClass>()?;
Ok(())
}

View File

@ -4,7 +4,6 @@ import sys
import datetime as pdt
import pytest
from hypothesis import given
from hypothesis import strategies as st
from hypothesis.strategies import dates, datetimes
@ -263,3 +262,12 @@ def test_delta_err(args, err_type):
def test_issue_219():
rdt.issue_219()
def test_tz_class():
tzi = rdt.TzClass()
dt = pdt.datetime(2018, 1, 1, tzinfo=tzi)
assert dt.tzname() == "+01:00"
assert dt.utcoffset() == pdt.timedelta(hours=1)
assert dt.dst() is None