diff options
author | Paul Ganssle <pganssle@users.noreply.github.com> | 2018-01-24 22:29:30 (GMT) |
---|---|---|
committer | Alexander Belopolsky <abalkin@users.noreply.github.com> | 2018-01-24 22:29:30 (GMT) |
commit | 04af5b1ba9eb546a29735fac6cb5298159069b53 (patch) | |
tree | 12e50adcc6ee03a3a4a80cb0a9b37bff4b58901b /Modules/_testcapimodule.c | |
parent | ccbe5818af20f8c12043f5c30c277a74714405e0 (diff) | |
download | cpython-04af5b1ba9eb546a29735fac6cb5298159069b53.zip cpython-04af5b1ba9eb546a29735fac6cb5298159069b53.tar.gz cpython-04af5b1ba9eb546a29735fac6cb5298159069b53.tar.bz2 |
bpo-10381: Add timezone to datetime C API (#5032)
* Add timezone to datetime C API
* Add documentation for timezone C API macros
* Add dedicated tests for datetime type check macros
* Remove superfluous C API test
* Drop support for TimeZoneType in datetime C API
* Expose UTC singleton to the datetime C API
* Update datetime C-API documentation to include links
* Add reference count information for timezone constructors
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r-- | Modules/_testcapimodule.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index e3be7d3..3f41134 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2220,12 +2220,92 @@ test_datetime_capi(PyObject *self, PyObject *args) { } test_run_counter++; PyDateTime_IMPORT; + if (PyDateTimeAPI) Py_RETURN_NONE; else return NULL; } +/* Functions exposing the C API type checking for testing */ +#define MAKE_DATETIME_CHECK_FUNC(check_method, exact_method) \ + PyObject *obj; \ + int exact = 0; \ + if (!PyArg_ParseTuple(args, "O|p", &obj, &exact)) { \ + return NULL; \ + } \ + int rv = exact?exact_method(obj):check_method(obj); \ + if (rv) { \ + Py_RETURN_TRUE; \ + } else { \ + Py_RETURN_FALSE; \ + } + +static PyObject * +datetime_check_date(PyObject *self, PyObject *args) { + MAKE_DATETIME_CHECK_FUNC(PyDate_Check, PyDate_CheckExact) +} + +static PyObject * +datetime_check_time(PyObject *self, PyObject *args) { + MAKE_DATETIME_CHECK_FUNC(PyTime_Check, PyTime_CheckExact) +} + +static PyObject * +datetime_check_datetime(PyObject *self, PyObject *args) { + MAKE_DATETIME_CHECK_FUNC(PyDateTime_Check, PyDateTime_CheckExact) +} + +static PyObject * +datetime_check_delta(PyObject *self, PyObject *args) { + MAKE_DATETIME_CHECK_FUNC(PyDelta_Check, PyDelta_CheckExact) +} + +static PyObject * +datetime_check_tzinfo(PyObject *self, PyObject *args) { + MAKE_DATETIME_CHECK_FUNC(PyTZInfo_Check, PyTZInfo_CheckExact) +} + + +/* Makes three variations on timezone representing UTC-5: + 1. timezone with offset and name from PyDateTimeAPI + 2. timezone with offset and name from PyTimeZone_FromOffsetAndName + 3. timezone with offset (no name) from PyTimeZone_FromOffset +*/ +static PyObject * +make_timezones_capi(PyObject *self, PyObject *args) { + PyObject *offset = PyDelta_FromDSU(0, -18000, 0); + PyObject *name = PyUnicode_FromString("EST"); + + PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name); + PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name); + PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset); + + Py_DecRef(offset); + Py_DecRef(name); + + PyObject *rv = PyTuple_New(3); + + PyTuple_SET_ITEM(rv, 0, est_zone_capi); + PyTuple_SET_ITEM(rv, 1, est_zone_macro); + PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname); + + return rv; +} + +static PyObject * +get_timezone_utc_capi(PyObject* self, PyObject *args) { + int macro = 0; + if (!PyArg_ParseTuple(args, "|p", ¯o)) { + return NULL; + } + if (macro) { + return PyDateTime_TimeZone_UTC; + } else { + return PyDateTimeAPI->TimeZone_UTC; + } +} + /* test_thread_state spawns a thread of its own, and that thread releases * `thread_done` when it's finished. The driver code has to know when the @@ -4452,6 +4532,13 @@ static PyMethodDef TestMethods[] = { {"test_config", (PyCFunction)test_config, METH_NOARGS}, {"test_sizeof_c_types", (PyCFunction)test_sizeof_c_types, METH_NOARGS}, {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, + {"datetime_check_date", datetime_check_date, METH_VARARGS}, + {"datetime_check_time", datetime_check_time, METH_VARARGS}, + {"datetime_check_datetime", datetime_check_datetime, METH_VARARGS}, + {"datetime_check_delta", datetime_check_delta, METH_VARARGS}, + {"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS}, + {"make_timezones_capi", make_timezones_capi, METH_NOARGS}, + {"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS}, {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS}, |