8000 gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Python/) by iritkatriel · Pull Request #102193 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Python/) #102193

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 10 commits into from
Feb 28, 2023
Merged
Prev Previous commit
Next Next commit
Python/sysmodule.c
  • Loading branch information
iritkatriel committed Feb 24, 2023
commit 3c4bdd8f420ee7815e117db6649ffb1ea5b7f08e
26 changes: 11 additions & 15 deletions Python/sysmodule.c
< 8000 td class="blob-code blob-code-addition js-file-line">
< 8000 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Data members:
#include "pycore_namespace.h" // _PyNamespace_New()
#include "pycore_object.h" // _PyObject_IS_GC()
#include "pycore_pathconfig.h" // _PyPathConfig_ComputeSysPath0()
#include "pycore_pyerrors.h" // _PyErr_Fetch()
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
#include "pycore_pylifecycle.h" // _PyErr_WriteUnraisableDefaultHook()
#include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
Expand Down Expand Up @@ -89,12 +89,11 @@ PySys_GetObject(const char *name)
{
PyThreadState *tstate = _PyThreadState_GET();

PyObject *exc_type, *exc_value, *exc_tb;
_PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
PyObject *exc = _PyErr_GetRaisedException(tstate);
PyObject *value = _PySys_GetObject(tstate->interp, name);
/* XXX Suppress a new exception if it was raised and restore
* the old one. */
_PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
_PyErr_SetRaisedException(tstate, exc);
return value;
}

Expand Down Expand Up @@ -203,8 +202,8 @@ sys_audit_tstate(PyThreadState *ts, const char *event,

int dtrace = PyDTrace_AUDIT_ENABLED();

PyObject *exc_type, *exc_value, *exc_tb;
_PyErr_Fetch(ts, &exc_type, &exc_value, &exc_tb);
PyObject *exc = _PyErr_GetRaisedException(ts);

/* Initialize event args now */
if (argFormat && argFormat[0]) {
Expand Down Expand Up @@ -287,13 +286,11 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
Py_XDECREF(eventArgs);

if (!res) {
_PyErr_Restore(ts, exc_type, exc_value, exc_tb);
_PyErr_SetRaisedException(ts, exc);
}
else {
assert(_PyErr_Occurred(ts));
Py_XDECREF(exc_type);
Py_XDECREF(exc_value);
Py_XDECREF(exc_tb);
Py_XDECREF(exc);
}

return res;
Expand Down Expand Up @@ -3661,12 +3658,11 @@ static void
sys_write(PyObject *key, FILE *fp, const char *format, va_list va)
{
PyObject *file;
PyObject *error_type, *error_value, *error_traceback;
char buffer[1001];
int written;
PyThreadState *tstate = _PyThreadState_GET();

_PyErr_Fetch(tstate, &error_type, &error_value, &error_traceback);
PyObject *exc = _PyErr_GetRaisedException(tstate);
file = _PySys_GetAttr(tstate, key);
written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
if (sys_pyfile_write(buffer, file) != 0) {
Expand All @@ -3678,7 +3674,7 @@ sys_write(PyObject *key, FILE *fp, const char *format, va_list va)
if (sys_pyfile_write(truncated, file) != 0)
fputs(truncated, fp);
}
_PyErr_Restore(tstate, error_type, error_value, error_traceback);
_PyErr_SetRaisedException(tstate, exc);
}

void
Expand Down Expand Up @@ -3708,7 +3704,7 @@ sys_format(PyObject *key, FILE *fp, const char *format, va_list va)
const char *utf8;
PyThreadState *tstate = _PyThreadState_GET();

PyObject *error = _PyErr_GetRaisedException(tstate);
PyObject *exc = _PyErr_GetRaisedException(tstate);
file = _PySys_GetAttr(tstate, key);
message = PyUnicode_FromFormatV(format, va);
if (message != NULL) {
Expand All @@ -3720,7 +3716,7 @@ sys_format(PyObject *key, FILE *fp, const char *format, va_list va)
}
Py_DECREF(message);
}
_PyErr_SetRaisedException(tstate, error);
_PyErr_SetRaisedException(tstate, exc);
}

void
Expand Down
0