Skip to content

Commit 7e7b739

Browse files
committed
lazy
1 parent ae32bfd commit 7e7b739

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

Modules/_datetimemodule.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,13 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
18881888
if (st == NULL) {
18891889
return NULL;
18901890
}
1891+
if (st->time_strftime == NULL) {
1892+
st->time_strftime = PyImport_ImportModuleAttrString("time", "strftime");
1893+
if (st->time_strftime == NULL) {
1894+
RELEASE_CURRENT_STATE(st, current_mod);
1895+
return NULL;
1896+
}
1897+
}
18911898
PyObject *strftime = st->time_strftime;
18921899

18931900
/* Scan the input format, looking for %z/%Z/%f escapes, building
@@ -2070,6 +2077,13 @@ time_time(void)
20702077
if (st == NULL) {
20712078
return NULL;
20722079
}
2080+
if (st->time_time == NULL) {
2081+
st->time_time = PyImport_ImportModuleAttrString("time", "time");
2082+
if (st->time_time == NULL) {
2083+
RELEASE_CURRENT_STATE(st, current_mod);
2084+
return NULL;
2085+
}
2086+
}
20732087
PyObject *result = PyObject_CallNoArgs(st->time_time);
20742088
RELEASE_CURRENT_STATE(st, current_mod);
20752089
return result;
@@ -2086,6 +2100,13 @@ build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
20862100
if (st == NULL) {
20872101
return NULL;
20882102
}
2103+
if (st->time_struct_time == NULL) {
2104+
st->time_struct_time = PyImport_ImportModuleAttrString("time", "struct_time");
2105+
if (st->time_struct_time == NULL) {
2106+
RELEASE_CURRENT_STATE(st, current_mod);
2107+
return NULL;
2108+
}
2109+
}
20892110

20902111
PyObject *result = PyObject_CallFunction(st->time_struct_time,
20912112
"((iiiiiiiii))",
@@ -7419,9 +7440,9 @@ init_state(datetime_state *st, PyObject *module, PyObject *old_module)
74197440
.us_per_week = Py_NewRef(st_old->us_per_week),
74207441
.seconds_per_day = Py_NewRef(st_old->seconds_per_day),
74217442
.epoch = Py_NewRef(st_old->epoch),
7422-
.time_time = Py_NewRef(st_old->time_time),
7423-
.time_struct_time = Py_NewRef(st_old->time_struct_time),
7424-
.time_strftime = Py_NewRef(st_old->time_strftime),
7443+
.time_time = Py_XNewRef(st_old->time_time),
7444+
.time_struct_time = Py_XNewRef(st_old->time_struct_time),
7445+
.time_strftime = Py_XNewRef(st_old->time_strftime),
74257446
};
74267447
return 0;
74277448
}
@@ -7466,18 +7487,9 @@ init_state(datetime_state *st, PyObject *module, PyObject *old_module)
74667487
return -1;
74677488
}
74687489

7469-
st->time_time = PyImport_ImportModuleAttrString("time", "time");
7470-
if (st->time_time == NULL) {
7471-
return -1;
7472-
}
7473-
st->time_struct_time = PyImport_ImportModuleAttrString("time", "struct_time");
7474-
if (st->time_struct_time == NULL) {
7475-
return -1;
7476-
}
7477-
st->time_strftime = PyImport_ImportModuleAttrString("time", "strftime");
7478-
if (st->time_strftime == NULL) {
7479-
return -1;
7480-
}
7490+
st->time_time = NULL;
7491+
st->time_struct_time = NULL;
7492+
st->time_strftime = NULL;
74817493

74827494
return 0;
74837495
}

0 commit comments

Comments
 (0)