8000 gh-99138: Isolate _zoneinfo by erlend-aasland · Pull Request #99218 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99138: Isolate _zoneinfo #99218

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

Merged
merged 30 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a5d7a79
Convert zone info type to heap type and add it to global state
Nov 2, 2020
45ad0b6
Remove unneeded tp_base placeholder
erlend-aasland Nov 7, 2022
accb561
Add io_open to global state
erlend-aasland Nov 7, 2022
61530f0
Add _tzpath_find_tzfile to global state
erlend-aasland Nov 7, 2022
8639a03
Add _common_mod to global state
erlend-aasland Nov 7, 2022
e728631
Add TIMEDELTA_CACHE to global state
erlend-aasland Nov 7, 2022
96241c9
Add ZONEINFO_WEAK_CACHE to global state
erlend-aasland Nov 7, 2022
39a0b7c
Move ZONEINFO_STRONG_CACHE to global state
erlend-aasland Nov 7, 2022
4749890
Move ZONEINFO_STRONG_CACHE_MAX_SIZE to constants and make it const
erlend-aasland Nov 7, 2022
5e71905
Add NO_TTINFO to global state
erlend-aasland Nov 7, 2022
4c31528
Add module arg to zoneinfo_get_state()
erlend-aasland Nov 7, 2022
0f1f1ea
Global state to module state
erlend-aasland Nov 7, 2022
573aec1
Style nit
erlend-aasland Nov 7, 2022
773a836
Address review: add comment above cache pointers
erlend-aasland Nov 7, 2022
6e69e93
Sync with main
erlend-aasland Nov 15, 2022
3e4f429
Merge branch 'main' into isolate-zoneinfo
erlend-aasland Jan 2, 2023
d4101af
Add NEWS
erlend-aasland Jan 2, 2023
1ef23da
Sync with main
erlend-aasland Jan 23, 2023
23cbc43
Simplify cache init
erlend-aasland Jan 23, 2023
ae5c549
Purge zoneinfo from c analyzer todo
erlend-aasland Jan 23, 2023
8adb727
Sync with main
erlend-aasland Feb 1, 2023
7c8836e
Sync with main
erlend-aasland Feb 3, 2023
6d5216e
Address reviews
erlend-aasland Feb 3, 2023
b7944dd
Pull in main
erlend-aasland Feb 12, 2023
71392bb
Pull in main
erlend-aasland Feb 15, 2023
244c537
Address review: remove warning about process-wide global state
erlend-aasland Feb 15, 2023
c32d46d
Pull in main
erlend-aasland Feb 15, 2023
75f519b
Revert "Address review: remove warning about process-wide global state"
erlend-aasland Feb 15, 2023
3b460ad
Address review: document that initialize_caches() is not idempotent
erlend-aasland Feb 15, 2023
a9dbc4d
Address review: reword cache warning only slightly
erlend-aasland Feb 15, 2023
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
Add NO_TTINFO to global state
  • Loading branch information
erlend-aasland committed Nov 7, 2022
commit 5e719051a7184fb242472316d2407a9619e45280
85 changes: 56 additions & 29 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ typedef struct {
PyObject *TIMEDELTA_CACHE;
PyObject *ZONEINFO_WEAK_CACHE;
StrongCacheNode *ZONEINFO_STRONG_CACHE;
_ttinfo NO_TTINFO;
} zoneinfo_state;

// Globals
static zoneinfo_state global_state;

static _ttinfo NO_TTINFO = {NULL, NULL, NULL, 0};

// Constants
static const int EPOCHORDINAL = 719163;
static int DAYS_IN_MONTH[] = {
Expand Down Expand Up @@ -173,7 +172,7 @@ load_timedelta(zoneinfo_state *state, long seconds);
static int
get_local_timestamp(PyObject *dt, int64_t *local_ts);
static _ttinfo *
find_ttinfo(PyZoneInfo_ZoneInfo *self, PyObject *dt);
find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt);

static int
ymd_to_ord(int y, int m, int d);
Expand Down Expand Up @@ -533,32 +532,70 @@ zoneinfo_ZoneInfo_clear_cache_impl(PyTypeObject *type, PyTypeObject *cls,
Py_RETURN_NONE;
}

/*[clinic input]
zoneinfo.ZoneInfo.utcoffset

cls: defining_class
dt: object
/

Retrieve a timedelta representing the UTC offset in a zone at the given datetime.
[clinic start generated code]*/

static PyObject *
zoneinfo_utcoffset(PyObject *self, PyObject *dt)
zoneinfo_ZoneInfo_utcoffset_impl(PyObject *self, PyTypeObject *cls,
PyObject *dt)
/*[clinic end generated code: output=b71016c319ba1f91 input=2bb6c5364938f19c]*/
{
_ttinfo *tti = find_ttinfo((PyZoneInfo_ZoneInfo *)self, dt);
zoneinfo_state *state = zoneinfo_get_state_by_cls(cls);
_ttinfo *tti = find_ttinfo(state, (PyZoneInfo_ZoneInfo *)self, dt);
if (tti == NULL) {
return NULL;
}
Py_INCREF(tti->utcoff);
return tti->utcoff;
}

/*[clinic input]
zoneinfo.ZoneInfo.dst

cls: defining_class
dt: object
/

Retrieve a timedelta representing the amount of DST applied in a zone at the given datetime.
[clinic start generated code]*/

static PyObject *
zoneinfo_dst(PyObject *self, PyObject *dt)
zoneinfo_ZoneInfo_dst_impl(PyObject *self, PyTypeObject *cls, PyObject *dt)
/*[clinic end generated code: output=cb6168d7723a6ae6 input=2167fb80cf8645c6]*/
{
_ttinfo *tti = find_ttinfo((PyZoneInfo_ZoneInfo *)self, dt);
zoneinfo_state *state = zoneinfo_get_state_by_cls(cls);
_ttinfo *tti = find_ttinfo(state, (PyZoneInfo_ZoneInfo *)self, dt);
if (tti == NULL) {
return NULL;
}
Py_INCREF(tti->dstoff);
return tti->dstoff;
}

/*[clinic input]
zoneinfo.ZoneInfo.tzname

cls: defining_class
dt: object
/

Retrieve a string containing the abbreviation for the time zone that applies in a zone at a given datetime.
[clinic start generated code]*/

static PyObject *
zoneinfo_tzname(PyObject *self, PyObject *dt)
zoneinfo_ZoneInfo_tzname_impl(PyObject *self, PyTypeObject *cls,
PyObject *dt)
/*[clinic end generated code: output=3b6ae6c3053ea75a input=15a59a4f92ed1f1f]*/
{
_ttinfo *tti = find_ttinfo((PyZoneInfo_ZoneInfo *)self, dt);
zoneinfo_state *state = zoneinfo_get_state_by_cls(cls);
_ttinfo *tti = find_ttinfo(state, (PyZoneInfo_ZoneInfo *)self, dt);
if (tti == NULL) {
return NULL;
}
Expand Down Expand Up @@ -2213,7 +2250,7 @@ _bisect(const int64_t value, const int64_t *arr, size_t size)

/* Find the ttinfo rules that apply at a given local datetime. */
static _ttinfo *
find_ttinfo(PyZoneInfo_ZoneInfo *self, PyObject *dt)
find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
{
// datetime.time has a .tzinfo attribute that passes None as the dt
// argument; it only really has meaning for fixed-offset zones.
Expand All @@ -2222,7 +2259,7 @@ find_ttinfo(PyZoneInfo_ZoneInfo *self, PyObject *dt)
return &(self->tzrule_after.std);
}
else {
return &NO_TTINFO;
return &(state->NO_TTINFO);
}
}

Expand Down Expand Up @@ -2644,15 +2681,9 @@ static PyMethodDef zoneinfo_methods[] = {
ZONEINFO_ZONEINFO_CLEAR_CACHE_METHODDEF
ZONEINFO_ZONEINFO_NO_CACHE_METHODDEF
ZONEINFO_ZONEINFO_FROM_FILE_METHODDEF
{"utcoffset", (PyCFunction)zoneinfo_utcoffset, METH_O,
PyDoc_STR("Retrieve a timedelta representing the UTC offset in a zone at "
"the given datetime.")},
{"dst", (PyCFunction)zoneinfo_dst, METH_O,
PyDoc_STR("Retrieve a timedelta representing the amount of DST applied "
"in a zone at the given datetime.")},
{"tzname", (PyCFunction)zoneinfo_tzname, METH_O,
PyDoc_STR("Retrieve a string containing the abbreviation for the time "
"zone that applies in a zone at a given datetime.")},
ZONEINFO_ZONEINFO_UTCOFFSET_METHODDEF
ZONEINFO_ZONEINFO_DST_METHODDEF
ZONEINFO_ZONEINFO_TZNAME_METHODDEF
{"fromutc", (PyCFunction)zoneinfo_fromutc, METH_O,
PyDoc_STR("Given a datetime with local time in UTC, retrieve an adjusted "
"datetime in local time.")},
Expand Down Expand Up @@ -2711,7 +2742,7 @@ module_free(void *m)
Py_CLEAR(state->_tzpath_find_tzfile);
Py_CLEAR(state->_common_mod);

xdecref_ttinfo(&NO_TTINFO);
xdecref_ttinfo(&(state->NO_TTINFO));

if (state->TIMEDELTA_CACHE != NULL &&
Py_REFCNT(state->TIMEDELTA_CACHE) > 1)
Expand Down Expand Up @@ -2773,14 +2804,10 @@ zoneinfomodule_exec(PyObject *m)
goto error;
}

if (NO_TTINFO.utcoff == NULL) {
NO_TTINFO.utcoff = Py_None;
NO_TTINFO.dstoff = Py_None;
NO_TTINFO.tzname = Py_None;

for (size_t i = 0; i < 3; ++i) {
Py_INCREF(Py_None);
}
if (state->NO_TTINFO.utcoff == NULL) {
state->NO_TTINFO.utcoff = Py_NewRef(Py_None);
state->NO_TTINFO.dstoff = Py_NewRef(Py_None);
state->NO_TTINFO.tzname = Py_NewRef(Py_None);
}

if (initialize_caches(state)) {
Expand Down
133 changes: 132 additions & 1 deletion Modules/clinic/_zoneinfo.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0