Skip to content
Merged
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
Add back assertion in get_locale_state
  • Loading branch information
encukou committed Feb 25, 2020
commit f3a1d08ac4192d83b35391bb11127132dc8ea1d7
16 changes: 9 additions & 7 deletions Modules/_localemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static inline _locale_state*
get_locale_state(PyObject *m)
{
void *state = PyModule_GetState(m);
assert(state != NULL);
return (_locale_state *)state;
}

Expand Down Expand Up @@ -776,19 +777,20 @@ static struct PyModuleDef_Slot _locale_slots[] = {
static int
locale_traverse(PyObject *m, visitproc visit, void *arg)
{
_locale_state *state = get_locale_state(m);
if (state == NULL) {
return -1;
_locale_state *state = (_locale_state*)PyModule_GetState(m);
if (state) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I asked on your two other PRs (binascii, audioop), I don't think that state can be NULL here. Same remark in locale_clear().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, victor. There have some description info about traverse slot in https://docs.python.org/3/c-api/module.html?highlight=pymoduledef#c.PyModuleDef:

A traversal function to call during GC traversal of the module object, or NULL if not needed. This function may be called before module state is allocated (PyModule_GetState() may return NULL), and before the Py_mod_exec function is executed.

So this behavior is a planned behavior, isn't it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss that in https://bugs.python.org/issue39824

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The discussion won't be over before Nick Coghlan is satisfied (and that's a good thing!)

Meanwhile, this PR is correct according to current behavior and documentation. I don't think bpo-39824 should block it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. I expected https://bugs.python.org/issue39824 to be resolved quick, but I'm ok to merge @shihai1991 PR's first, and revisit the code later if we decided that m_clear/m_free cannot be called with a NULL state.

Py_VISIT(state->Error);
}
Py_VISIT(state->Error);
return 0;
}

static int
locale_clear(PyObject *m)
{
_locale_state *state = get_locale_state(m);
Py_CLEAR(state->Error);
_locale_state *state = (_locale_state*)PyModule_GetState(m);
if (state) {
Py_CLEAR(state->Error);
}
return 0;
}

Expand All @@ -807,7 +809,7 @@ static struct PyModuleDef _localemodule = {
_locale_slots,
locale_traverse,
locale_clear,
(freefunc)locale_free
(freefunc)locale_free,
};

PyMODINIT_FUNC
Expand Down