Skip to content
Prev Previous commit
Merge branch 'master' into pymodule-add
  • Loading branch information
serhiy-storchaka committed Nov 21, 2020
commit 981b580022c37fca247c4c40df0768dcbf95a3dc
209 changes: 82 additions & 127 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6514,13 +6514,13 @@ _datetime_exec(PyObject *module)
};

for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
if (PyModule_AddType(m, types[i]) < 0) {
goto error;
if (PyModule_AddType(module, types[i]) < 0) {
return -1;
}
}

if (PyType_Ready(&PyDateTime_IsoCalendarDateType) < 0) {
goto error;
return -1;
}

#define DATETIME_ADD_MACRO(dict, c, value_expr) \
Expand All @@ -6537,100 +6537,31 @@ _datetime_exec(PyObject *module)
} while(0)

/* timedelta values */
d = PyDateTime_DeltaType.tp_dict;

x = new_delta(0, 0, 1, 0);
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);
PyObject *d = PyDateTime_DeltaType.tp_dict;
DATETIME_ADD_MACRO(d, "resolution", new_delta(0, 0, 1, 0));
DATETIME_ADD_MACRO(d, "min", new_delta(-MAX_DELTA_DAYS, 0, 0, 0));
DATETIME_ADD_MACRO(d, "max",
new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0));

/* date values */
d = PyDateTime_DateType.tp_dict;

x = new_date(1, 1, 1);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_date(MAXYEAR, 12, 31);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(1, 0, 0, 0);
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);
DATETIME_ADD_MACRO(d, "min", new_date(1, 1, 1));
DATETIME_ADD_MACRO(d, "max", new_date(MAXYEAR, 12, 31));
DATETIME_ADD_MACRO(d, "resolution", new_delta(1, 0, 0, 0));

/* time values */
d = PyDateTime_TimeType.tp_dict;

x = new_time(0, 0, 0, 0, Py_None, 0);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_time(23, 59, 59, 999999, Py_None, 0);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(0, 0, 1, 0);
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);
DATETIME_ADD_MACRO(d, "min", new_time(0, 0, 0, 0, Py_None, 0));
DATETIME_ADD_MACRO(d, "max", new_time(23, 59, 59, 999999, Py_None, 0));
DATETIME_ADD_MACRO(d, "resolution", new_delta(0, 0, 1, 0));

/* datetime values */
d = PyDateTime_DateTimeType.tp_dict;

x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None, 0);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None, 0);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(0, 0, 1, 0);
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);
DATETIME_ADD_MACRO(d, "min",
new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None, 0));
DATETIME_ADD_MACRO(d, "max", new_datetime(MAXYEAR, 12, 31, 23, 59, 59,
999999, Py_None, 0));
DATETIME_ADD_MACRO(d, "resolution", new_delta(0, 0, 1, 0));

/* timezone values */
d = PyDateTime_TimeZoneType.tp_dict;
Expand All @@ -6639,61 +6570,64 @@ _datetime_exec(PyObject *module)
return -1;
}

delta = new_delta(0, 0, 0, 0);
if (delta == NULL)
goto error;
x = create_timezone(delta, NULL);
PyObject *x = create_timezone(delta, NULL);
Py_DECREF(delta);
if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) {
Py_XDECREF(x);
goto error;
if (x == NULL) {
return -1;
}
if (PyDict_SetItemString(d, "utc", x) < 0) {
Py_DECREF(x);
return -1;
}

PyDateTime_TimeZone_UTC = x;
CAPI.TimeZone_UTC = PyDateTime_TimeZone_UTC;

/* 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;
if (delta == NULL) {
return -1;
}

x = create_timezone(delta, NULL);
Py_DECREF(delta);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);
DATETIME_ADD_MACRO(d, "min", x);

delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */
if (delta == NULL)
goto error;
if (delta == NULL) {
return -1;
}

x = create_timezone(delta, NULL);
Py_DECREF(delta);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);
DATETIME_ADD_MACRO(d, "max", x);

/* Epoch */
PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0,
PyDateTime_TimeZone_UTC, 0);
if (PyDateTime_Epoch == NULL)
goto error;
if (PyDateTime_Epoch == NULL) {
return -1;
}

/* module initialization */
if (PyModule_AddIntMacro(m, MINYEAR) < 0) {
goto error;
if (PyModule_AddIntMacro(module, MINYEAR) < 0) {
return -1;
}
if (PyModule_AddIntMacro(m, MAXYEAR) < 0) {
goto error;
if (PyModule_AddIntMacro(module, MAXYEAR) < 0) {
return -1;
}

if (PyModule_Add(m, "datetime_CAPI",
PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL)) < 0) {
goto error;
};
x = PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL);
if (x == NULL) {
return -1;
}

if (PyModule_AddObject(module, "datetime_CAPI", x) < 0) {
Py_DECREF(x);
return -1;
}

/* A 4-year cycle has an extra leap day over what we'd get from
* pasting together 4 single years.
Expand All @@ -6718,22 +6652,43 @@ _datetime_exec(PyObject *module)
us_per_minute = PyLong_FromLong(60000000);
seconds_per_day = PyLong_FromLong(24 * 3600);
if (us_per_ms == NULL || us_per_second == NULL ||
us_per_minute == NULL || seconds_per_day == NULL)
goto error;
us_per_minute == NULL || 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.
*/
us_per_hour = PyLong_FromDouble(3600000000.0);
us_per_day = PyLong_FromDouble(86400000000.0);
us_per_week = PyLong_FromDouble(604800000000.0);
if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL)
goto error;
return m;
if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) {
return -1;
}
return 0;
}

error:
Py_DECREF(m);
return NULL;
static struct PyModuleDef datetimemodule = {
PyModuleDef_HEAD_INIT,
.m_name = "_datetime",
.m_doc = "Fast implementation of the datetime type.",
.m_size = -1,
.m_methods = module_methods,
};

PyMODINIT_FUNC
PyInit__datetime(void)
{
PyObject *mod = PyModule_Create(&datetimemodule);
if (mod == NULL)
return NULL;

if (_datetime_exec(mod) < 0) {
Py_DECREF(mod);
return NULL;
}

return mod;
}

/* ---------------------------------------------------------------------------
Expand Down
43 changes: 1 addition & 42 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,46 +645,5 @@ static struct PyModuleDef _randommodule = {
PyMODINIT_FUNC
PyInit__random(void)
{
PyObject *m;

PyObject *Random_Type = PyType_FromSpec(&Random_Type_spec);
if (Random_Type == NULL) {
return NULL;
}

m = PyModule_Create(&_randommodule);
if (m == NULL) {
Py_DECREF(Random_Type);
return NULL;
}
get_random_state(m)->Random_Type = Random_Type;

Py_INCREF(Random_Type);
if (PyModule_Add(m, "Random", Random_Type) < 0) {
Py_DECREF(m);
return NULL;
}

/* Look up and save int.__abs__, which is needed in random_seed(). */
PyObject *longval = NULL, *longtype = NULL;
longval = PyLong_FromLong(0);
if (longval == NULL) goto fail;

longtype = PyObject_Type(longval);
if (longtype == NULL) goto fail;

PyObject *abs = PyObject_GetAttrString(longtype, "__abs__");
if (abs == NULL) goto fail;

Py_DECREF(longtype);
Py_DECREF(longval);
get_random_state(m)->Long___abs__ = abs;

return m;

fail:
Py_XDECREF(longtype);
Py_XDECREF(longval);
Py_DECREF(m);
return NULL;
return PyModuleDef_Init(&_randommodule);
}
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.