Skip to content

Commit

Permalink
export translation function and add translations to tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybaloney committed Oct 25, 2024
1 parent ce1e361 commit d640d98
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions Include/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ PyAPI_FUNC(PyObject *) PyErr_FormatV(
const char *format,
va_list vargs);
#endif
PyAPI_FUNC(const char*) PyErr_GetLocalizedException(const char* message);

#ifdef MS_WINDOWS
PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(
Expand Down
2 changes: 1 addition & 1 deletion Parser/tokenizer/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _syntaxerror_range(struct tok_state *tok, const char *format,
return ERRORTOKEN;
}
PyObject *errmsg, *errtext, *args;
errmsg = PyUnicode_FromFormatV(format, vargs);
errmsg = PyUnicode_FromFormatV(PyErr_GetLocalizedException(format), vargs);
if (!errmsg) {
goto error;
}
Expand Down
11 changes: 10 additions & 1 deletion Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ _PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
static const char*
_PyErr_GetLocalizedException(PyThreadState* tstate, const char* message);

static const char*
PyErr_GetLocalizedException(const char* message);

void
_PyErr_SetRaisedException(PyThreadState *tstate, PyObject *exc)
{
Expand Down Expand Up @@ -1154,7 +1157,7 @@ static const char* _PyErr_GetLocalizedException(PyThreadState *tstate, const cha
if (tstate->interp->config.language == NULL)
return message;

PyObject* exception_dict = PySys_GetObject("localized_exceptions");
PyObject* exception_dict = PySys_GetObject("localized_exceptions"); // Borrowed ref
if (exception_dict == NULL) {
return message;
}
Expand All @@ -1174,12 +1177,18 @@ static const char* _PyErr_GetLocalizedException(PyThreadState *tstate, const cha
// Get entry for string
PyObject* entry = PyDict_GetItemString(lang_dict, message);
if (entry == NULL) {
Py_DECREF(lang_dict);
return message;
}

return PyUnicode_AsUTF8(entry);
}

static const char* PyErr_GetLocalizedException(const char* message){
PyThreadState *tstate = _PyThreadState_GET();
return _PyErr_GetLocalizedException(tstate, message);
}

static PyObject *
_PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
const char *format, va_list vargs)
Expand Down

0 comments on commit d640d98

Please sign in to comment.