Skip to content

Commit 0398e3c

Browse files
committed
Expose UTC singleton to the datetime C API
1 parent 2c20871 commit 0398e3c

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

Doc/c-api/datetime.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ the module initialisation function. The macro puts a pointer to a C structure
1313
into a static variable, :c:data:`PyDateTimeAPI`, that is used by the following
1414
macros.
1515

16+
Macro for access to the UTC singleton:
17+
18+
.. c:var:: PyObject* PyDateTime_TimeZone_UTC
19+
20+
Returns the time zone singleton representing UTC, the same object as
21+
:attr:``datetime.timezone.utc``.
22+
23+
.. versionadded:: 3.7
24+
25+
1626
Type-check macros:
1727

1828
.. c:function:: int PyDate_Check(PyObject *ob)

Include/datetime.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ static PyDateTime_CAPI *PyDateTimeAPI = NULL;
206206
#define PyDateTime_IMPORT \
207207
PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
208208

209+
/* Macro for access to the UTC singleton */
210+
#define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
211+
209212
/* Macros for type checking when not building the Python core. */
210213
#define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
211214
#define PyDate_CheckExact(op) (Py_TYPE(op) == PyDateTimeAPI->DateType)

Lib/test/datetimetester.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5457,9 +5457,11 @@ def setUp(self):
54575457
_testcapi.test_datetime_capi()
54585458

54595459
def test_utc_capi(self):
5460-
capi_utc = _testcapi.get_timezone_utc_capi()
5460+
for use_macro in (True, False):
5461+
capi_utc = _testcapi.get_timezone_utc_capi(use_macro)
54615462

5462-
self.assertIs(capi_utc, timezone.utc)
5463+
with self.subTest(use_macro=use_macro):
5464+
self.assertIs(capi_utc, timezone.utc)
54635465

54645466
def test_timezones_capi(self):
54655467
est_capi, est_macro, est_macro_nn = _testcapi.make_timezones_capi()

Modules/_testcapimodule.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,7 +2295,15 @@ make_timezones_capi(PyObject *self, PyObject *args) {
22952295

22962296
static PyObject *
22972297
get_timezone_utc_capi(PyObject* self, PyObject *args) {
2298-
return PyDateTimeAPI->TimeZone_UTC;
2298+
int macro = 0;
2299+
if (!PyArg_ParseTuple(args, "|p", &macro)) {
2300+
return NULL;
2301+
}
2302+
if (macro) {
2303+
return PyDateTime_TimeZone_UTC;
2304+
} else {
2305+
return PyDateTimeAPI->TimeZone_UTC;
2306+
}
22992307
}
23002308

23012309

@@ -4523,7 +4531,7 @@ static PyMethodDef TestMethods[] = {
45234531
{"datetime_check_delta", datetime_check_delta, METH_VARARGS},
45244532
{"datetime_check_tzinfo", datetime_check_tzinfo, METH_VARARGS},
45254533
{"make_timezones_capi", make_timezones_capi, METH_NOARGS},
4526-
{"get_timezone_utc_capi", get_timezone_utc_capi, METH_NOARGS},
4534+
{"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS},
45274535
{"test_list_api", (PyCFunction)test_list_api, METH_NOARGS},
45284536
{"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS},
45294537
{"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},

0 commit comments

Comments
 (0)