Skip to content

Commit da24674

Browse files
Consolidate into init_state().
1 parent 5c25927 commit da24674

File tree

1 file changed

+19
-44
lines changed

1 file changed

+19
-44
lines changed

Modules/_datetimemodule.c

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6968,8 +6968,9 @@ create_timezone_from_delta(int days, int sec, int ms, int normalize)
69686968
}
69696969

69706970
static int
6971-
init_heap_types(datetime_state *st, PyObject *module)
6971+
init_state(datetime_state *st, PyObject *module, PyObject *old_module)
69726972
{
6973+
/* Each module gets its own heap types. */
69736974
#define ADD_TYPE(FIELD, SPEC, BASE) \
69746975
do { \
69756976
PyObject *cls = PyType_FromModuleAndSpec( \
@@ -6983,14 +6984,22 @@ init_heap_types(datetime_state *st, PyObject *module)
69836984
ADD_TYPE(isocalendar_date_type, &isocal_spec, &PyTuple_Type);
69846985
#undef ADD_TYPE
69856986

6986-
return 0;
6987-
}
6988-
6989-
static int
6990-
init_state(datetime_state *st)
6991-
{
6992-
/* st->isocalendar_date_type was set via init_heap_types(). */
6993-
assert(st->isocalendar_date_type != NULL);
6987+
if (old_module != NULL) {
6988+
assert(old_module != module);
6989+
datetime_state *st_old = get_module_state(old_module);
6990+
*st = (datetime_state){
6991+
.isocalendar_date_type = st->isocalendar_date_type,
6992+
.us_per_ms = Py_NewRef(st_old->us_per_ms),
6993+
.us_per_second = Py_NewRef(st_old->us_per_second),
6994+
.us_per_minute = Py_NewRef(st_old->us_per_minute),
6995+
.us_per_hour = Py_NewRef(st_old->us_per_hour),
6996+
.us_per_day = Py_NewRef(st_old->us_per_day),
6997+
.us_per_week = Py_NewRef(st_old->us_per_week),
6998+
.seconds_per_day = Py_NewRef(st_old->seconds_per_day),
6999+
.epoch = Py_NewRef(st_old->epoch),
7000+
};
7001+
return 0;
7002+
}
69947003

69957004
st->us_per_ms = PyLong_FromLong(1000);
69967005
if (st->us_per_ms == NULL) {
@@ -7035,28 +7044,6 @@ init_state(datetime_state *st)
70357044
return 0;
70367045
}
70377046

7038-
static int
7039-
copy_state(datetime_state *st, datetime_state *from)
7040-
{
7041-
/* isocalendar_date_type is set via init_heap_types(). */
7042-
assert(st->isocalendar_date_type != NULL);
7043-
assert(st->isocalendar_date_type != from->isocalendar_date_type);
7044-
7045-
*st = (datetime_state){
7046-
.isocalendar_date_type = st->isocalendar_date_type,
7047-
.us_per_ms = Py_NewRef(from->us_per_ms),
7048-
.us_per_second = Py_NewRef(from->us_per_second),
7049-
.us_per_minute = Py_NewRef(from->us_per_minute),
7050-
.us_per_hour = Py_NewRef(from->us_per_hour),
7051-
.us_per_day = Py_NewRef(from->us_per_day),
7052-
.us_per_week = Py_NewRef(from->us_per_week),
7053-
.seconds_per_day = Py_NewRef(from->seconds_per_day),
7054-
.epoch = Py_NewRef(from->epoch),
7055-
};
7056-
7057-
return 0;
7058-
}
7059-
70607047
static int
70617048
traverse_state(datetime_state *st, visitproc visit, void *arg)
70627049
{
@@ -7117,22 +7104,10 @@ _datetime_exec(PyObject *module)
71177104
}
71187105
}
71197106

7120-
if (init_heap_types(st, module) < 0) {
7107+
if (init_state(st, module, old_module) < 0) {
71217108
goto error;
71227109
}
71237110

7124-
if (old_module != NULL) {
7125-
datetime_state *st_old = get_module_state(old_module);
7126-
if (copy_state(st, st_old) < 0) {
7127-
goto error;
7128-
}
7129-
}
7130-
else {
7131-
if (init_state(st) < 0) {
7132-
goto error;
7133-
}
7134-
}
7135-
71367111
/* For now we only set the objects on the static types once.
71377112
* We will relax that once each types __dict__ is per-interpreter. */
71387113
#define DATETIME_ADD_MACRO(dict, c, value_expr) \

0 commit comments

Comments
 (0)