Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-117398: Add datetime Module State #119640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make datetime.IsoCalendarDate a heap type.
  • Loading branch information
ericsnowcurrently committed May 27, 2024
commit a88effb28fc0a23232fba183f120245eba192b09
69 changes: 51 additions & 18 deletions 69 Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3461,17 +3461,40 @@ static PyMethodDef iso_calendar_date_methods[] = {
{NULL, NULL},
};

static PyTypeObject PyDateTime_IsoCalendarDateType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "datetime.IsoCalendarDate",
.tp_basicsize = sizeof(PyDateTime_IsoCalendarDate),
.tp_repr = (reprfunc) iso_calendar_date_repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = iso_calendar_date__doc__,
.tp_methods = iso_calendar_date_methods,
.tp_getset = iso_calendar_date_getset,
// .tp_base = &PyTuple_Type, // filled in PyInit__datetime
.tp_new = iso_calendar_date_new,
static int
iso_calendar_date_traverse(PyDateTime_IsoCalendarDate *self, visitproc visit,
void *arg)
{
Py_VISIT(Py_TYPE(self));
return PyTuple_Type.tp_traverse((PyObject *)self, visit, arg);
}

static void
iso_calendar_date_dealloc(PyDateTime_IsoCalendarDate *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyTuple_Type.tp_dealloc((PyObject *)self); // delegate GC-untrack as well
Py_DECREF(tp);
}

static PyType_Slot isocal_slots[] = {
{Py_tp_repr, iso_calendar_date_repr},
{Py_tp_doc, (void *)iso_calendar_date__doc__},
{Py_tp_methods, iso_calendar_date_methods},
{Py_tp_getset, iso_calendar_date_getset},
{Py_tp_new, iso_calendar_date_new},
{Py_tp_dealloc, iso_calendar_date_dealloc},
{Py_tp_traverse, iso_calendar_date_traverse},
{0, NULL},
};

static PyType_Spec isocal_spec = {
.name = "datetime.IsoCalendarDate",
.basicsize = sizeof(PyDateTime_IsoCalendarDate),
.flags = (Py_TPFLAGS_DEFAULT |
Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = isocal_slots,
};

/*[clinic input]
Expand Down Expand Up @@ -6843,16 +6866,18 @@ create_timezone_from_delta(int days, int sec, int ms, int normalize)
}

static int
init_state(datetime_state *st)
init_state(datetime_state *st, PyTypeObject *PyDateTime_IsoCalendarDateType)
{
/* Static types exposed by the C-API. */
st->date_type = &PyDateTime_DateType;
st->datetime_type = &PyDateTime_DateTimeType;
st->delta_type = &PyDateTime_DeltaType;
st->time_type = &PyDateTime_TimeType;
st->tzinfo_type = &PyDateTime_TZInfoType;
st->timezone_type = &PyDateTime_TimeZoneType;

st->isocalendar_date_type = &PyDateTime_IsoCalendarDateType;
/* Per-module heap types. */
st->isocalendar_date_type = PyDateTime_IsoCalendarDateType;

st->us_per_ms = PyLong_FromLong(1000);
if (st->us_per_ms == NULL) {
Expand Down Expand Up @@ -6907,7 +6932,6 @@ _datetime_exec(PyObject *module)
// `&...` is not a constant expression according to a strict reading
// of C standards. Fill tp_base at run-time rather than statically.
// See https://bugs.python.org/issue40777
PyDateTime_IsoCalendarDateType.tp_base = &PyTuple_Type;
PyDateTime_TimeZoneType.tp_base = &PyDateTime_TZInfoType;
PyDateTime_DateTimeType.tp_base = &PyDateTime_DateType;

Expand All @@ -6926,12 +6950,21 @@ _datetime_exec(PyObject *module)
}
}

if (PyType_Ready(&PyDateTime_IsoCalendarDateType) < 0) {
goto error;
}
#define CREATE_TYPE(VAR, SPEC, BASE) \
do { \
VAR = (PyTypeObject *)PyType_FromModuleAndSpec( \
module, SPEC, (PyObject *)BASE); \
if (VAR == NULL) { \
goto error; \
} \
} while (0)

PyTypeObject *PyDateTime_IsoCalendarDateType = NULL;
CREATE_TYPE(PyDateTime_IsoCalendarDateType, &isocal_spec, &PyTuple_Type);
#undef CREATE_TYPE

datetime_state *st = get_datetime_state();
if (init_state(st) < 0) {
if (init_state(st, PyDateTime_IsoCalendarDateType) < 0) {
goto error;
}

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.