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 _common_mod to global state
  • Loading branch information
erlend-aasland committed Nov 7, 2022
commit 8639a03d6ac51aad7213534a2645ab7846d0b82b
37 changes: 19 additions & 18 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class zoneinfo.ZoneInfo "PyObject *" "PyTypeObject *"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d12c73c0eef36df8]*/

// Imports
static PyObject *_common_mod = NULL;

typedef struct TransitionRuleType TransitionRuleType;
typedef struct StrongCacheNode StrongCacheNode;
Expand Down Expand Up @@ -94,6 +92,7 @@ typedef struct {
// Imports
PyObject *io_open;
PyObject *_tzpath_find_tzfile;
PyObject *_common_mod;
} zoneinfo_state;

// Globals
Expand Down Expand Up @@ -122,7 +121,8 @@ static const int SOURCE_FILE = 2;

// Forward declarations
static int
load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj);
load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self,
PyObject *file_obj);
static void
utcoff_to_dstoff(size_t *trans_idx, long *utcoffs, long *dstoffs,
unsigned char *isdsts, size_t num_transitions,
Expand Down Expand Up @@ -218,7 +218,8 @@ zoneinfo_new_instance(zoneinfo_state *state, PyTypeObject *type, PyObject *key)
return NULL;
}
else if (file_path == Py_None) {
file_obj = PyObject_CallMethod(_common_mod, "load_tzdata", "O", key);
PyObject *meth = state->_common_mod;
file_obj = PyObject_CallMethod(meth, "load_tzdata", "O", key);
if (file_obj == NULL) {
Py_DECREF(file_path);
return NULL;
Expand All @@ -238,7 +239,7 @@ zoneinfo_new_instance(zoneinfo_state *state, PyTypeObject *type, PyObject *key)
}
}

if (load_data((PyZoneInfo_ZoneInfo *)self, file_obj)) {
if (load_data(state, (PyZoneInfo_ZoneInfo *)self, file_obj)) {
goto error;
}

Expand Down Expand Up @@ -390,6 +391,7 @@ zoneinfo_dealloc(PyObject *obj_self)
@classmethod
zoneinfo.ZoneInfo.from_file

cls: defining_class
file_obj: object
/
key: object = None
Expand All @@ -398,9 +400,9 @@ Create a ZoneInfo file from a file object.
[clinic start generated code]*/

static PyObject *
zoneinfo_ZoneInfo_from_file_impl(PyTypeObject *type, PyObject *file_obj,
PyObject *key)
/*[clinic end generated code: output=68ed2022404ae5be input=ccfe73708133d2e4]*/
zoneinfo_ZoneInfo_from_file_impl(PyTypeObject *type, PyTypeObject *cls,
PyObject *file_obj, PyObject *key)
/*[clinic end generated code: output=77887d1d56a48324 input=d26111f29eed6863]*/
{
PyObject *file_repr = NULL;
PyZoneInfo_ZoneInfo *self = NULL;
Expand All @@ -416,7 +418,8 @@ zoneinfo_ZoneInfo_from_file_impl(PyTypeObject *type, PyObject *file_obj,
goto error;
}

if (load_data(self, file_obj)) {
zoneinfo_state *state = zoneinfo_get_state_by_cls(cls);
if (load_data(state, self, file_obj)) {
goto error;
}

Expand Down Expand Up @@ -899,7 +902,7 @@ ttinfo_eq(const _ttinfo *const tti0, const _ttinfo *const tti1)
* the object only needs to be freed / deallocated if this succeeds.
*/
static int
load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
{
PyObject *data_tuple = NULL;

Expand All @@ -917,7 +920,8 @@ load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj)

size_t ttinfos_allocated = 0;

data_tuple = PyObject_CallMethod(_common_mod, "load_data", "O", file_obj);
data_tuple = PyObject_CallMethod(state->_common_mod, "load_data", "O",
file_obj);

if (data_tuple == NULL) {
goto error;
Expand Down Expand Up @@ -2683,12 +2687,9 @@ module_free(void *m)
{
zoneinfo_state *state = zoneinfo_get_state();

Py_CLEAR(state->_tzpath_find_tzfile);

Py_XDECREF(_common_mod);
_common_mod = NULL;

Py_CLEAR(state->io_open);
Py_CLEAR(state->_tzpath_find_tzfile);
Py_CLEAR(state->_common_mod);

xdecref_ttinfo(&NO_TTINFO);

Expand Down Expand Up @@ -2741,8 +2742,8 @@ zoneinfomodule_exec(PyObject *m)
goto error;
}

_common_mod = PyImport_ImportModule("zoneinfo._common");
if (_common_mod == NULL) {
state->_common_mod = PyImport_ImportModule("zoneinfo._common");
if (state->_common_mod == NULL) {
goto error;
}

Expand Down
12 changes: 6 additions & 6 deletions Modules/clinic/_zoneinfo.c.h

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