diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2025-04-25 14:43:38 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-25 14:43:38 -0600 |
commit | cd9536a0872046cc7c151b61b457975e7718274a (patch) | |
tree | 97aeb24d67a984a1eedd1ef0944463a4b2a3ef05 /Python/errors.c | |
parent | 4c20f46fa011df57190cc19b21bafde1f65e73a7 (diff) | |
download | cpython-cd9536a0872046cc7c151b61b457975e7718274a.tar.gz cpython-cd9536a0872046cc7c151b61b457975e7718274a.zip |
gh-132781: Cleanup Code Related to NotShareableError (gh-132782)
The following are added to the internal C-API:
* _PyErr_FormatV()
* _PyErr_SetModuleNotFoundError()
* _PyXIData_GetNotShareableErrorType()
* _PyXIData_FormatNotShareableError()
We also drop _PyXIData_lookup_context_t and _PyXIData_GetLookupContext().
Diffstat (limited to 'Python/errors.c')
-rw-r--r-- | Python/errors.c | 76 |
1 files changed, 54 insertions, 22 deletions
diff --git a/Python/errors.c b/Python/errors.c index 2b088e2f388..81f267b043a 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -21,11 +21,6 @@ #endif -/* Forward declarations */ -static PyObject * -_PyErr_FormatV(PyThreadState *tstate, PyObject *exception, - const char *format, va_list vargs); - void _PyErr_SetRaisedException(PyThreadState *tstate, PyObject *exc) { @@ -699,12 +694,11 @@ _PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb) The caller is responsible for ensuring that this call won't create any cycles in the exception context chain. */ void -_PyErr_ChainExceptions1(PyObject *exc) +_PyErr_ChainExceptions1Tstate(PyThreadState *tstate, PyObject *exc) { if (exc == NULL) { return; } - PyThreadState *tstate = _PyThreadState_GET(); if (_PyErr_Occurred(tstate)) { PyObject *exc2 = _PyErr_GetRaisedException(tstate); PyException_SetContext(exc2, exc); @@ -715,6 +709,13 @@ _PyErr_ChainExceptions1(PyObject *exc) } } +void +_PyErr_ChainExceptions1(PyObject *exc) +{ + PyThreadState *tstate = _PyThreadState_GET(); + _PyErr_ChainExceptions1Tstate(tstate, exc); +} + /* If the current thread is handling an exception (exc_info is ), set this exception as the context of the current raised exception. @@ -1061,15 +1062,14 @@ PyObject *PyErr_SetFromWindowsErrWithFilename( #endif /* MS_WINDOWS */ static PyObject * -_PyErr_SetImportErrorSubclassWithNameFrom( - PyObject *exception, PyObject *msg, +new_importerror( + PyThreadState *tstate, PyObject *exctype, PyObject *msg, PyObject *name, PyObject *path, PyObject* from_name) { - PyThreadState *tstate = _PyThreadState_GET(); - int issubclass; - PyObject *kwargs, *error; + PyObject *exc = NULL; + PyObject *kwargs = NULL; - issubclass = PyObject_IsSubclass(exception, PyExc_ImportError); + int issubclass = PyObject_IsSubclass(exctype, PyExc_ImportError); if (issubclass < 0) { return NULL; } @@ -1095,29 +1095,38 @@ _PyErr_SetImportErrorSubclassWithNameFrom( from_name = Py_None; } - kwargs = PyDict_New(); if (kwargs == NULL) { return NULL; } if (PyDict_SetItemString(kwargs, "name", name) < 0) { - goto done; + goto finally; } if (PyDict_SetItemString(kwargs, "path", path) < 0) { - goto done; + goto finally; } if (PyDict_SetItemString(kwargs, "name_from", from_name) < 0) { - goto done; + goto finally; } + exc = PyObject_VectorcallDict(exctype, &msg, 1, kwargs); + +finally: + Py_DECREF(kwargs); + return exc; +} - error = PyObject_VectorcallDict(exception, &msg, 1, kwargs); +static PyObject * +_PyErr_SetImportErrorSubclassWithNameFrom( + PyObject *exception, PyObject *msg, + PyObject *name, PyObject *path, PyObject* from_name) +{ + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *error = new_importerror( + tstate, exception, msg, name, path, from_name); if (error != NULL) { _PyErr_SetObject(tstate, (PyObject *)Py_TYPE(error), error); Py_DECREF(error); } - -done: - Py_DECREF(kwargs); return NULL; } @@ -1141,6 +1150,29 @@ PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path); } +int +_PyErr_SetModuleNotFoundError(PyObject *name) +{ + PyThreadState *tstate = _PyThreadState_GET(); + if (name == NULL) { + _PyErr_SetString(tstate, PyExc_TypeError, "expected a name argument"); + return -1; + } + PyObject *msg = PyUnicode_FromFormat("%S module not found", name); + if (msg == NULL) { + return -1; + } + PyObject *exctype = PyExc_ModuleNotFoundError; + PyObject *exc = new_importerror(tstate, exctype, msg, name, NULL, NULL); + Py_DECREF(msg); + if (exc == NULL) { + return -1; + } + _PyErr_SetObject(tstate, exctype, exc); + Py_DECREF(exc); + return 0; +} + void _PyErr_BadInternalCall(const char *filename, int lineno) { @@ -1164,7 +1196,7 @@ PyErr_BadInternalCall(void) #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) -static PyObject * +PyObject * _PyErr_FormatV(PyThreadState *tstate, PyObject *exception, const char *format, va_list vargs) { |