@@ -6968,27 +6968,8 @@ create_timezone_from_delta(int days, int sec, int ms, int normalize)
6968
6968
}
6969
6969
6970
6970
static int
6971
- copy_state (datetime_state * st , datetime_state * from )
6972
- {
6973
- * st = (datetime_state ){
6974
- .isocalendar_date_type
6975
- = (PyTypeObject * )Py_NewRef (from -> isocalendar_date_type ),
6976
- .us_per_ms = Py_NewRef (from -> us_per_ms ),
6977
- .us_per_second = Py_NewRef (from -> us_per_second ),
6978
- .us_per_minute = Py_NewRef (from -> us_per_minute ),
6979
- .us_per_hour = Py_NewRef (from -> us_per_hour ),
6980
- .us_per_day = Py_NewRef (from -> us_per_day ),
6981
- .us_per_week = Py_NewRef (from -> us_per_week ),
6982
- .seconds_per_day = Py_NewRef (from -> seconds_per_day ),
6983
- .epoch = Py_NewRef (from -> epoch ),
6984
- };
6985
- return 0 ;
6986
- }
6987
-
6988
- static int
6989
- init_state (datetime_state * st , PyObject * module )
6971
+ init_heap_types (datetime_state * st , PyObject * module )
6990
6972
{
6991
- /* Per-module heap types. */
6992
6973
#define ADD_TYPE (FIELD , SPEC , BASE ) \
6993
6974
do { \
6994
6975
PyObject *cls = PyType_FromModuleAndSpec( \
@@ -7002,6 +6983,15 @@ init_state(datetime_state *st, PyObject *module)
7002
6983
ADD_TYPE (isocalendar_date_type , & isocal_spec , & PyTuple_Type );
7003
6984
#undef ADD_TYPE
7004
6985
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 );
6994
+
7005
6995
st -> us_per_ms = PyLong_FromLong (1000 );
7006
6996
if (st -> us_per_ms == NULL ) {
7007
6997
return -1 ;
@@ -7045,6 +7035,28 @@ init_state(datetime_state *st, PyObject *module)
7045
7035
return 0 ;
7046
7036
}
7047
7037
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
+
7048
7060
static int
7049
7061
traverse_state (datetime_state * st , visitproc visit , void * arg )
7050
7062
{
@@ -7105,14 +7117,18 @@ _datetime_exec(PyObject *module)
7105
7117
}
7106
7118
}
7107
7119
7120
+ if (init_heap_types (st , module ) < 0 ) {
7121
+ goto error ;
7122
+ }
7123
+
7108
7124
if (old_module != NULL ) {
7109
7125
datetime_state * st_old = get_module_state (old_module );
7110
7126
if (copy_state (st , st_old ) < 0 ) {
7111
7127
goto error ;
7112
7128
}
7113
7129
}
7114
7130
else {
7115
- if (init_state (st , module ) < 0 ) {
7131
+ if (init_state (st ) < 0 ) {
7116
7132
goto error ;
7117
7133
}
7118
7134
}
0 commit comments