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
Next Next commit
add get_locale_state()
  • Loading branch information
shihai1991 committed Feb 17, 2020
commit 3aaf4881148600747055e06d9c268f9bb2e740d9
48 changes: 19 additions & 29 deletions Modules/_localemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ typedef struct _locale_state {
PyObject *Error;
} _locale_state;

static inline _locale_state*
get_locale_state(PyObject *m)
{
void *state = PyModule_GetState(m);
assert(state != NULL);
return (_locale_state *)state;
}

/* support functions for formatting floating point numbers */

PyDoc_STRVAR(setlocale__doc__,
Expand Down Expand Up @@ -89,19 +97,15 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
int category;
char *locale = NULL, *result;
PyObject *result_object;
_locale_state *state;

if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))
return NULL;

#if defined(MS_WINDOWS)
if (category < LC_MIN || category > LC_MAX)
{
state = PyModule_GetState(self);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Error, "invalid locale category");
PyErr_SetString(get_locale_state(self)->Error,
"invalid locale category");
Comment thread
shihai1991 marked this conversation as resolved.
Outdated
return NULL;
}
#endif
Expand All @@ -111,11 +115,8 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
result = setlocale(category, locale);
if (!result) {
/* operation failed, no setting was changed */
state = PyModule_GetState(self);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Error, "unsupported locale setting");
PyErr_SetString(get_locale_state(self)->Error,
"unsupported locale setting");
return NULL;
}
result_object = PyUnicode_DecodeLocale(result, NULL);
Expand All @@ -125,11 +126,8 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
/* get locale */
result = setlocale(category, NULL);
if (!result) {
state = PyModule_GetState(self);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Error, "locale query failed");
PyErr_SetString(get_locale_state(self)->Error,
"locale query failed");
return NULL;
}
result_object = PyUnicode_DecodeLocale(result, NULL);
Expand Down Expand Up @@ -641,16 +639,12 @@ PyIntl_bindtextdomain(PyObject* self, PyObject*args)
{
char *domain, *dirname, *current_dirname;
PyObject *dirname_obj, *dirname_bytes = NULL, *result;
_locale_state *state;

if (!PyArg_ParseTuple(args, "sO", &domain, &dirname_obj))
return 0;
if (!strlen(domain)) {
state = PyModule_GetState(self);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Error, "domain must be a non-empty string");
PyErr_SetString(get_locale_state(self)->Error,
"domain must be a non-empty string");
return 0;
}
if (dirname_obj != Py_None) {
Expand Down Expand Up @@ -737,10 +731,6 @@ _locale_exec(PyObject *m)
#ifdef HAVE_LANGINFO_H
int i;
#endif
_locale_state *state = PyModule_GetState(m);
if (state == NULL) {
return -1;
}

PyModule_AddIntMacro(m, LC_CTYPE);
PyModule_AddIntMacro(m, LC_TIME);
Expand All @@ -755,11 +745,11 @@ _locale_exec(PyObject *m)
PyModule_AddIntMacro(m, LC_ALL);
PyModule_AddIntMacro(m, CHAR_MAX);

state->Error = PyErr_NewException("locale.Error", NULL, NULL);
if (state->Error == NULL) {
get_locale_state(m)->Error = PyErr_NewException("locale.Error", NULL, NULL);
Comment thread
shihai1991 marked this conversation as resolved.
Outdated
if (get_locale_state(m)->Error == NULL) {
return -1;
}
if (PyModule_AddObject(m, "Error", state->Error) < 0) {
if (PyModule_AddObject(m, "Error", get_locale_state(m)->Error) < 0) {
return -1;
}

Expand Down