8000 refactoring before multi-phase init · python/cpython@eb65e29 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb65e29

Browse files
committed
refactoring before multi-phase init
1 parent a26215d commit eb65e29

File tree

1 file changed

+103
-106
lines changed

1 file changed

+103
-106
lines changed

Modules/_datetimemodule.c

Lines changed: 103 additions & 106 deletions
< 179B td data-grid-cell-id="diff-3ee250e3806e884518fd872e9148baf532de6ec54c1cdb4e7679fbb2869d9c47-6695-6666-2" data-line-anchor="diff-3ee250e3806e884518fd872e9148baf532de6ec54c1cdb4e7679fbb2869d9c47R6666" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionLine-bgColor, var(--diffBlob-addition-bgColor-line));padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">+
us_per_minute == NULL || seconds_per_day == NULL) {
Original file line numberDiff line numberDiff line change
@@ -6494,32 +6494,9 @@ static PyDateTime_CAPI CAPI = {
64946494
new_time_ex2
64956495
};
64966496

6497-
6498-
6499-
static struct PyModuleDef datetimemodule = {
6500-
PyModuleDef_HEAD_INIT,
6501-
"_datetime",
6502-
"Fast implementation of the datetime type.",
6503-
-1,
6504-
module_methods,
6505-
NULL,
6506-
NULL,
6507-
NULL,
6508-
NULL
6509-
};
6510-
6511-
PyMODINIT_FUNC
6512-
PyInit__datetime(void)
6497+
static int
6498+
_datetime_exec(PyObject *module)
65136499
{
6514-
PyObject *m; /* a module object */
6515-
PyObject *d; /* its dict */
6516-
PyObject *x;
6517-
PyObject *delta;
6518-
6519-
m = PyModule_Create(&datetimemodule);
6520-
if (m == NULL)
6521-
return NULL;
6522-
65236500
// `&...` is not a constant expression according to a strict reading
65246501
// of C standards. Fill tp_base at run-time rather than statically.
65256502
// See https://bugs.python.org/issue40777
@@ -6537,136 +6514,131 @@ PyInit__datetime(void)
65376514
};
65386515

65396516
for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
6540-
if (PyModule_AddType(m, types[i]) < 0) {
6541-
return NULL;
6517+
if (PyModule_AddType(module, types[i]) < 0) {
6518+
return -1;
65426519
}
65436520
}
65446521

65456522
if (PyType_Ready(&PyDateTime_IsoCalendarDateType) < 0) {
6546-
return NULL;
6523+
return -1;
65476524
}
65486525
Py_INCREF(&PyDateTime_IsoCalendarDateType);
65496526

6550-
/* timedelta values */
6551-
d = PyDateTime_DeltaType.tp_dict;
65526527

6553-
x = new_delta(0, 0, 1, 0);
6554-
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
6555-
return NULL;
6556-
Py_DECREF(x);
6528+
PyObject *x = NULL;
65576529

6558-
x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0);
6559-
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
6560-
return NULL;
6561-
Py_DECREF(x);
6530+
#define DATETIME_ADD_MACRO(x, c) \
6531+
do { \
6532+
if (x == NULL) { \
6533+
return -1; \
6534+
} \
6535+
if (PyDict_SetItemString(d, c, x) < 0) { \
6536+
Py_DECREF(x); \
6537+
return -1; \
6538+
} \
6539+
Py_DECREF(x); 67E6 \
6540+
} while(0)
65626541

6542+
/* timedelta values */
6543+
PyObject *d = PyDateTime_DeltaType.tp_dict;
6544+
x = new_delta(0, 0, 1, 0);
6545+
DATETIME_ADD_MACRO(x, "resolution");
6546+
x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0);
6547+
DATETIME_ADD_MACRO(x, "min");
65636548
x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0);
6564-
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
6565-
return NULL;
6566-
Py_DECREF(x);
6549+
DATETIME_ADD_MACRO(x, "max");
65676550

65686551
/* date values */
65696552
d = PyDateTime_DateType.tp_dict;
6570-
65716553
x = new_date(1, 1, 1);
6572-
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
6573-
return NULL;
6574-
Py_DECREF(x);
6575-
6554+
DATETIME_ADD_MACRO(x, "min");
65766555
x = new_date(MAXYEAR, 12, 31);
6577-
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
6578-
return NULL;
6579-
Py_DECREF(x);
6580-
6556+
DATETIME_ADD_MACRO(x, "max");
65816557
x = new_delta(1, 0, 0, 0);
6582-
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
6583-
return NULL;
6584-
Py_DECREF(x);
6558+
DATETIME_ADD_MACRO(x, "resolution");
65856559

65866560
/* time values */
65876561
d = PyDateTime_TimeType.tp_dict;
6588-
65896562
x = new_time(0, 0, 0, 0, Py_None, 0);
6590-
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
6591-
return NULL;
6592-
Py_DECREF(x);
6593-
6563+
DATETIME_ADD_MACRO(x, "min");
65946564
x = new_time(23, 59, 59, 999999, Py_None, 0);
6595-
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
6596-
return NULL;
6597-
Py_DECREF(x);
6598-
6565+
DATETIME_ADD_MACRO(x, "max");
65996566
x = new_delta(0, 0, 1, 0);
6600-
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
6601-
return NULL;
6602-
Py_DECREF(x);
6567+
DATETIME_ADD_MACRO(x, "resolution");
66036568

66046569
/* datetime values */
66056570
d = PyDateTime_DateTimeType.tp_dict;
6606-
66076571
x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None, 0);
6608-
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
6609-
return NULL;
6610-
Py_DECREF(x);
6611-
6572+
DATETIME_ADD_MACRO(x, "min");
66126573
x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None, 0);
6613-
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
6614-
return NULL;
6615-
Py_DECREF(x);
6616-
6574+
DATETIME_ADD_MACRO(x, "max");
66176575
x = new_delta(0, 0, 1, 0);
6618-
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
6619-
return NULL;
6620-
Py_DECREF(x);
6576+
DATETIME_ADD_MACRO(x, "resolution");
66216577

66226578
/* timezone values */
66236579
d = PyDateTime_TimeZoneType.tp_dict;
6580+
PyObject *delta = new_delta(0, 0, 0, 0);
6581+
if (delta == NULL) {
6582+
return -1;
6583+
}
66246584

6625-
delta = new_delta(0, 0, 0, 0);
6626-
if (delta == NULL)
6627-
return NULL;
66286585
x = create_timezone(delta, NULL);
66296586
Py_DECREF(delta);
6630-
if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0)
6631-
return NULL;
6587+
if (x == NULL) {
6588+
return -1;
6589+
}
6590+
if (PyDict_SetItemString(d, "utc", x) < 0) {
6591+
Py_DECREF(x);
6592+
return -1;
6593+
}
6594+
66326595
PyDateTime_TimeZone_UTC = x;
66336596
CAPI.TimeZone_UTC = PyDateTime_TimeZone_UTC;
66346597

6635-
/* bpo-37642: These attributes are rounded to the nearest minute for backwards
6636-
* compatibility, even though the constructor will accept a wider range of
6637-
* values. This may change in the future.*/
6598+
/* bpo-37642: These attributes are rounded to the nearest minute for
6599+
* backwards compatibility, even though the constructor will accept a
6600+
* wider range of values. This may change in the future.
6601+
*/
66386602
delta = new_delta(-1, 60, 0, 1); /* -23:59 */
6639-
if (delta == NULL)
6640-
return NULL;
6603+
if (delta == NULL) {
6604+
return -1;
6605+
}
66416606
x = create_timezone(delta, NULL);
66426607
Py_DECREF(delta);
6643-
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
6644-
return NULL;
6645-
Py_DECREF(x);
6608+
DATETIME_ADD_MACRO(x, "min");
66466609

66476610
delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */
6648-
if (delta == NULL)
6649-
return NULL;
6611+
if (delta == NULL) {
6612+
return -1;
6613+
}
66506614
x = create_timezone(delta, NULL);
66516615
Py_DECREF(delta);
6652-
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
6653-
return NULL;
6654-
Py_DECREF(x);
6616+
DATETIME_ADD_MACRO(x, "max");
66556617

66566618
/* Epoch */
66576619
PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0,
66586620
PyDateTime_TimeZone_UTC, 0);
6659-
if (PyDateTime_Epoch == NULL)
6660-
return NULL;
6621+
if (PyDateTime_Epoch == NULL) {
6622+
return -1;
6623+
}
66616624

66626625
/* module initialization */
6663-
PyModule_AddIntMacro(m, MINYEAR);
6664-
PyModule_AddIntMacro(m, MAXYEAR);
6626+
if (PyModule_AddIntMacro(module, MINYEAR) < 0) {
6627+
return -1;
6628+
}
6629+
if (PyModule_AddIntMacro(module, MAXYEAR) < 0) {
6630+
return -1;
6631+
}
66656632

66666633
x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL);
6667-
if (x == NULL)
6668-
return NULL;
6669-
PyModule_AddObject(m, "datetime_CAPI", x);
6634+
if (x == NULL) {
6635+
return -1;
6636+
}
6637+
6638+
if (PyModule_AddObject(module, "datetime_CAPI", x) < 0) {
6639+
Py_DECREF(x);
6640+
return -1;
6641+
}
66706642

66716643
/* A 4-year cycle has an extra leap day over what we'd get from
66726644
* pasting together 4 single years.
@@ -6691,18 +6663,43 @@ PyInit__datetime(void)
66916663
us_per_minute = PyLong_FromLong(60000000);
66926664
seconds_per_day = PyLong_FromLong(24 * 3600);
66936665
if (us_per_ms == NULL || us_per_second == NULL ||
6694-
us_per_minute == NULL || seconds_per_day == NULL)
6695-
return NULL;
6666
6667+
return -1;
6668+
}
66966669

66976670
/* The rest are too big for 32-bit ints, but even
66986671
* us_per_week fits in 40 bits, so doubles should be exact.
66996672
*/
67006673
us_per_hour = PyLong_FromDouble(3600000000.0);
67016674
us_per_day = PyLong_FromDouble(86400000000.0);
67026675
us_per_week = PyLong_FromDouble(604800000000.0);
6703-
if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL)
6676+
if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) {
6677+
return -1;
6678+
}
6679+
return 0;
6680+
}
6681+
6682+
static struct PyModuleDef datetimemodule = {
6683+
PyModuleDef_HEAD_INIT,
6684+
.m_name = "_datetime",
6685+
.m_doc = "Fast implementation of the datetime type.",
6686+
.m_size = -1,
6687+
.m_methods = module_methods,
6688+
};
6689+
6690+
PyMODINIT_FUNC
6691+
PyInit__datetime(void)
6692+
{
6693+
PyObject *mod = PyModule_Create(&datetimemodule);
6694+
if (mod == NULL)
67046695
return NULL;
6705-
return m;
6696+
6697+
if (_datetime_exec(mod) < 0) {
6698+
Py_DECREF(mod);
6699+
return NULL;
6700+
}
6701+
6702+
return mod;
67066703
}
67076704

67086705
/* ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)
0