-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-71587: Establish global state in _datetime
#110475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
cacf1e8
c2eba8f
8d54ecd
360397a
4f509f9
6b14c8c
2eb3315
f5ede1e
f6ac875
7139569
26e6454
227dd2c
beb0977
8525be6
6863f76
0738205
c56b5e1
3ee48d2
0a1d770
a9e8d1a
c3ba7fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6741,6 +6741,68 @@ datetime_clear(PyObject *module) | |
return 0; | ||
} | ||
|
||
static PyObject * | ||
create_timezone_from_delta(int days, int sec, int ms, int normalize) | ||
{ | ||
PyObject *delta = new_delta(days, sec, ms, normalize); | ||
if (delta == NULL) { | ||
return NULL; | ||
} | ||
PyObject *tz = create_timezone(delta, NULL); | ||
Py_DECREF(delta); | ||
return tz; | ||
} | ||
|
||
static int | ||
init_state(datetime_state *st) | ||
{ | ||
st->us_per_ms = PyLong_FromLong(1000); | ||
if (st->us_per_ms == NULL) { | ||
return -1; | ||
} | ||
st->us_per_second = PyLong_FromLong(1000000); | ||
if (st->us_per_second == NULL) { | ||
return -1; | ||
} | ||
st->us_per_minute = PyLong_FromLong(60000000); | ||
if (st->us_per_minute == NULL) { | ||
return -1; | ||
} | ||
st->seconds_per_day = PyLong_FromLong(24 * 3600); | ||
if (st->seconds_per_day == NULL) { | ||
return -1; | ||
} | ||
|
||
/* The rest are too big for 32-bit ints, but even | ||
* us_per_week fits in 40 bits, so doubles should be exact. | ||
*/ | ||
st->us_per_hour = PyLong_FromDouble(3600000000.0); | ||
if (st->us_per_hour == NULL) { | ||
return -1; | ||
} | ||
st->us_per_day = PyLong_FromDouble(86400000000.0); | ||
if (st->us_per_day == NULL) { | ||
return -1; | ||
} | ||
st->us_per_week = PyLong_FromDouble(604800000000.0); | ||
if (st->us_per_week == NULL) { | ||
return -1; | ||
} | ||
|
||
/* Init UTC timezone */ | ||
st->utc = create_timezone_from_delta(0, 0, 0, 0); | ||
if (st->utc == NULL) { | ||
return -1; | ||
} | ||
|
||
/* Init Unix epoch */ | ||
st->epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, st->utc, 0); | ||
if (st->epoch == NULL) { | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
static int | ||
_datetime_exec(PyObject *module) | ||
{ | ||
|
@@ -6762,23 +6824,23 @@ _datetime_exec(PyObject *module) | |
|
||
for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) { | ||
if (PyModule_AddType(module, types[i]) < 0) { | ||
return -1; | ||
goto error; | ||
} | ||
} | ||
|
||
if (PyType_Ready(&PyDateTime_IsoCalendarDateType) < 0) { | ||
return -1; | ||
goto error; | ||
} | ||
|
||
#define DATETIME_ADD_MACRO(dict, c, value_expr) \ | ||
do { \ | ||
PyObject *value = (value_expr); \ | ||
if (value == NULL) { \ | ||
return -1; \ | ||
goto error; \ | ||
} \ | ||
if (PyDict_SetItemString(dict, c, value) < 0) { \ | ||
Py_DECREF(value); \ | ||
return -1; \ | ||
goto error; \ | ||
} \ | ||
Py_DECREF(value); \ | ||
} while(0) | ||
|
@@ -6810,73 +6872,53 @@ _datetime_exec(PyObject *module) | |
999999, Py_None, 0)); | ||
DATETIME_ADD_MACRO(d, "resolution", new_delta(0, 0, 1, 0)); | ||
|
||
/* timezone values */ | ||
d = PyDateTime_TimeZoneType.tp_dict; | ||
PyObject *delta = new_delta(0, 0, 0, 0); | ||
if (delta == NULL) { | ||
return -1; | ||
} | ||
|
||
datetime_state *st = STATIC_STATE(); | ||
st->utc = create_timezone(delta, NULL); | ||
Py_DECREF(delta); | ||
if (st->utc == NULL) { | ||
if (init_state(st) < 0) { | ||
goto error; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently, now exec function has two different parts:
I suggest to split the function in two parts (create a subfunction). I don't think that a second PR is needed. Maybe just add a second _datetime_init_state() function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, we can bake it into this PR. I'll get to it later tonight. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added some helpers in a9e8d1a ( |
||
} | ||
|
||
/* timezone values */ | ||
d = PyDateTime_TimeZoneType.tp_dict; | ||
if (PyDict_SetItemString(d, "utc", st->utc) < 0) { | ||
goto error; | ||
} | ||
|
||
/* bpo-37642: These attributes are rounded to the nearest minute for backwards | ||
* compatibility, even though the constructor will accept a wider range of | ||
* values. This may change in the future.*/ | ||
delta = new_delta(-1, 60, 0, 1); /* -23:59 */ | ||
if (delta == NULL) { | ||
goto error; | ||
} | ||
|
||
PyObject *x = create_timezone(delta, NULL); | ||
Py_DECREF(delta); | ||
DATETIME_ADD_MACRO(d, "min", x); | ||
|
||
delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */ | ||
if (delta == NULL) { | ||
goto error; | ||
} | ||
/* -23:59 */ | ||
PyObject *min = create_timezone_from_delta(-1, 60, 0, 1); | ||
DATETIME_ADD_MACRO(d, "min", min); | ||
|
||
x = create_timezone(delta, NULL); | ||
Py_DECREF(delta); | ||
DATETIME_ADD_MACRO(d, "max", x); | ||
/* 23:59 */ | ||
PyObject *max = create_timezone_from_delta(0, (23 * 60 + 59) * 60, 0, 0); | ||
DATETIME_ADD_MACRO(d, "max", max); | ||
|
||
/* Epoch */ | ||
st->epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0, st->utc, 0); | ||
if (st->epoch == NULL) { | ||
goto error; | ||
} | ||
|
||
/* module initialization */ | ||
/* Add module level attributes */ | ||
if (PyModule_AddIntMacro(module, MINYEAR) < 0) { | ||
goto error; | ||
} | ||
if (PyModule_AddIntMacro(module, MAXYEAR) < 0) { | ||
goto error; | ||
} | ||
if (PyModule_AddObjectRef(module, "UTC", st->utc) < 0) { | ||
goto error; | ||
} | ||
|
||
/* At last, set up and add the encapsulated C API */ | ||
PyDateTime_CAPI *capi = get_datetime_capi(); | ||
if (capi == NULL) { | ||
goto error; | ||
} | ||
x = PyCapsule_New(capi, PyDateTime_CAPSULE_NAME, datetime_destructor); | ||
if (x == NULL) { | ||
PyObject *capsule = PyCapsule_New(capi, PyDateTime_CAPSULE_NAME, | ||
datetime_destructor); | ||
if (capsule == NULL) { | ||
PyMem_Free(capi); | ||
goto error; | ||
} | ||
|
||
if (PyModule_Add(module, "datetime_CAPI", x) < 0) { | ||
goto error; | ||
} | ||
|
||
if (PyModule_AddObjectRef(module, "UTC", st->utc) < 0) { | ||
if (PyModule_Add(module, "datetime_CAPI", capsule) < 0) { | ||
PyMem_Free(capi); | ||
goto error; | ||
} | ||
|
||
|
@@ -6898,45 +6940,13 @@ _datetime_exec(PyObject *module) | |
static_assert(DI100Y == 25 * DI4Y - 1, "DI100Y"); | ||
assert(DI100Y == days_before_year(100+1)); | ||
|
||
st->us_per_ms = PyLong_FromLong(1000); | ||
if (st->us_per_ms == NULL) { | ||
goto error; | ||
} | ||
st->us_per_second = PyLong_FromLong(1000000); | ||
if (st->us_per_second == NULL) { | ||
goto error; | ||
} | ||
st->us_per_minute = PyLong_FromLong(60000000); | ||
if (st->us_per_minute == NULL) { | ||
goto error; | ||
} | ||
st->seconds_per_day = PyLong_FromLong(24 * 3600); | ||
if (st->seconds_per_day == NULL) { | ||
goto error; | ||
} | ||
|
||
/* The rest are too big for 32-bit ints, but even | ||
* us_per_week fits in 40 bits, so doubles should be exact. | ||
*/ | ||
st->us_per_hour = PyLong_FromDouble(3600000000.0); | ||
if (st->us_per_hour == NULL) { | ||
goto error; | ||
} | ||
st->us_per_day = PyLong_FromDouble(86400000000.0); | ||
if (st->us_per_day == NULL) { | ||
goto error; | ||
} | ||
st->us_per_week = PyLong_FromDouble(604800000000.0); | ||
if (st->us_per_week == NULL) { | ||
goto error; | ||
} | ||
|
||
return 0; | ||
|
||
error: | ||
datetime_clear(module); | ||
return -1; | ||
} | ||
#undef DATETIME_ADD_MACRO | ||
|
||
static struct PyModuleDef datetimemodule = { | ||
.m_base = PyModuleDef_HEAD_INIT, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to initialise the state members in the order they are declared, we'll have to move this. However, the following comment will then need to be tweaked. I'm not sure it is worth it.