summaryrefslogtreecommitdiffstats
path: root/Modules/_io
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-02-24 21:43:03 (GMT)
committerGitHub <noreply@github.com>2023-02-24 21:43:03 (GMT)
commit2db23d10bf64bf7c061fd95c6a8079ddc5c9aa4b (patch)
treeaa29d63d7151d976ea301f5fadff1821333690c9 /Modules/_io
parent568fc0dee42a353f327b059a48f97c911de904b3 (diff)
downloadcpython-2db23d10bf64bf7c061fd95c6a8079ddc5c9aa4b.zip
cpython-2db23d10bf64bf7c061fd95c6a8079ddc5c9aa4b.tar.gz
cpython-2db23d10bf64bf7c061fd95c6a8079ddc5c9aa4b.tar.bz2
gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Modules/) (#102196)
Diffstat (limited to 'Modules/_io')
-rw-r--r--Modules/_io/_iomodule.c7
-rw-r--r--Modules/_io/bufferedio.c60
-rw-r--r--Modules/_io/fileio.c31
-rw-r--r--Modules/_io/iobase.c12
-rw-r--r--Modules/_io/textio.c23
-rw-r--r--Modules/_io/winconsoleio.c15
6 files changed, 77 insertions, 71 deletions
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 55b6535..d8d836b 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -437,10 +437,9 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
error:
if (result != NULL) {
- PyObject *exc, *val, *tb, *close_result;
- PyErr_Fetch(&exc, &val, &tb);
- close_result = PyObject_CallMethodNoArgs(result, &_Py_ID(close));
- _PyErr_ChainExceptions(exc, val, tb);
+ PyObject *exc = PyErr_GetRaisedException();
+ PyObject *close_result = PyObject_CallMethodNoArgs(result, &_Py_ID(close));
+ _PyErr_ChainExceptions1(exc);
Py_XDECREF(close_result);
Py_DECREF(result);
}
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 56491f0..9600267 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -472,12 +472,13 @@ buffered_closed_get(buffered *self, void *context)
static PyObject *
buffered_close(buffered *self, PyObject *args)
{
- PyObject *res = NULL, *exc = NULL, *val, *tb;
+ PyObject *res = NULL;
int r;
CHECK_INITIALIZED(self)
- if (!ENTER_BUFFERED(self))
+ if (!ENTER_BUFFERED(self)) {
return NULL;
+ }
r = buffered_closed(self);
if (r < 0)
@@ -497,12 +498,16 @@ buffered_close(buffered *self, PyObject *args)
/* flush() will most probably re-take the lock, so drop it first */
LEAVE_BUFFERED(self)
res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (!ENTER_BUFFERED(self))
+ if (!ENTER_BUFFERED(self)) {
return NULL;
- if (res == NULL)
- PyErr_Fetch(&exc, &val, &tb);
- else
+ }
+ PyObject *exc = NULL;
+ if (res == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
+ else {
Py_DECREF(res);
+ }
res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close));
@@ -512,7 +517,7 @@ buffered_close(buffered *self, PyObject *args)
}
if (exc != NULL) {
- _PyErr_ChainExceptions(exc, val, tb);
+ _PyErr_ChainExceptions1(exc);
Py_CLEAR(res);
}
@@ -637,17 +642,14 @@ _set_BlockingIOError(const char *msg, Py_ssize_t written)
static Py_ssize_t *
_buffered_check_blocking_error(void)
{
- PyObject *t, *v, *tb;
- PyOSErrorObject *err;
-
- PyErr_Fetch(&t, &v, &tb);
- if (v == NULL || !PyErr_GivenExceptionMatches(v, PyExc_BlockingIOError)) {
- PyErr_Restore(t, v, tb);
+ PyObject *exc = PyErr_GetRaisedException();
+ if (exc == NULL || !PyErr_GivenExceptionMatches(exc, PyExc_BlockingIOError)) {
+ PyErr_SetRaisedException(exc);
return NULL;
}
- err = (PyOSErrorObject *) v;
+ PyOSErrorObject *err = (PyOSErrorObject *)exc;
/* TODO: sanity check (err->written >= 0) */
- PyErr_Restore(t, v, tb);
+ PyErr_SetRaisedException(exc);
return &err->written;
}
@@ -749,13 +751,11 @@ _buffered_init(buffered *self)
int
_PyIO_trap_eintr(void)
{
- PyObject *typ, *val, *tb;
- PyOSErrorObject *env_err;
- if (!PyErr_ExceptionMatches(PyExc_OSError))
+ if (!PyErr_ExceptionMatches(PyExc_OSError)) {
return 0;
- PyErr_Fetch(&typ, &val, &tb);
- PyErr_NormalizeException(&typ, &val, &tb);
- env_err = (PyOSErrorObject *) val;
+ }
+ PyObject *exc = PyErr_GetRaisedException();
+ PyOSErrorObject *env_err = (PyOSErrorObject *)exc;
assert(env_err != NULL);
if (env_err->myerrno != NULL) {
assert(EINTR > 0 && EINTR < INT_MAX);
@@ -764,14 +764,12 @@ _PyIO_trap_eintr(void)
int myerrno = PyLong_AsLongAndOverflow(env_err->myerrno, &overflow);
PyErr_Clear();
if (myerrno == EINTR) {
- Py_DECREF(typ);
- Py_DECREF(val);
- Py_XDECREF(tb);
+ Py_DECREF(exc);
return 1;
}
}
/* This silences any error set by PyObject_RichCompareBool() */
- PyErr_Restore(typ, val, tb);
+ PyErr_SetRaisedException(exc);
return 0;
}
@@ -2228,15 +2226,17 @@ bufferedrwpair_writable(rwpair *self, PyObject *Py_UNUSED(ignored))
static PyObject *
bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored))
{
- PyObject *exc = NULL, *val, *tb;
+ PyObject *exc = NULL;
PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL);
- if (ret == NULL)
- PyErr_Fetch(&exc, &val, &tb);
- else
+ if (ret == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
+ else {
Py_DECREF(ret);
+ }
ret = _forward_call(self->reader, &_Py_ID(close), NULL);
if (exc != NULL) {
- _PyErr_ChainExceptions(exc, val, tb);
+ _PyErr_ChainExceptions1(exc);
Py_CLEAR(ret);
}
return ret;
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index f424fb8..35a498c 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -88,14 +88,13 @@ static PyObject *
fileio_dealloc_warn(fileio *self, PyObject *source)
{
if (self->fd >= 0 && self->closefd) {
- PyObject *exc, *val, *tb;
- PyErr_Fetch(&exc, &val, &tb);
+ PyObject *exc = PyErr_GetRaisedException();
if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
/* Spurious errors can appear at shutdown */
if (PyErr_ExceptionMatches(PyExc_Warning))
PyErr_WriteUnraisable((PyObject *) self);
}
- PyErr_Restore(exc, val, tb);
+ PyErr_SetRaisedException(exc);
}
Py_RETURN_NONE;
}
@@ -140,7 +139,7 @@ _io_FileIO_close_impl(fileio *self)
/*[clinic end generated code: output=7737a319ef3bad0b input=f35231760d54a522]*/
{
PyObject *res;
- PyObject *exc, *val, *tb;
+ PyObject *exc;
int rc;
res = PyObject_CallMethodOneArg((PyObject*)&PyRawIOBase_Type,
&_Py_ID(close), (PyObject *)self);
@@ -148,20 +147,25 @@ _io_FileIO_close_impl(fileio *self)
self->fd = -1;
return res;
}
- if (res == NULL)
- PyErr_Fetch(&exc, &val, &tb);
+ if (res == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
if (self->finalizing) {
PyObject *r = fileio_dealloc_warn(self, (PyObject *) self);
- if (r)
+ if (r) {
Py_DECREF(r);
- else
+ }
+ else {
PyErr_Clear();
+ }
}
rc = internal_close(self);
- if (res == NULL)
- _PyErr_ChainExceptions(exc, val, tb);
- if (rc < 0)
+ if (res == NULL) {
+ _PyErr_ChainExceptions1(exc);
+ }
+ if (rc < 0) {
Py_CLEAR(res);
+ }
return res;
}
@@ -487,10 +491,9 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
if (!fd_is_own)
self->fd = -1;
if (self->fd >= 0) {
- PyObject *exc, *val, *tb;
- PyErr_Fetch(&exc, &val, &tb);
+ PyObject *exc = PyErr_GetRaisedException();
internal_close(self);
- _PyErr_ChainExceptions(exc, val, tb);
+ _PyErr_ChainExceptions1(exc);
}
done:
diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c
index 7b9391e..682ed00 100644
--- a/Modules/_io/iobase.c
+++ b/Modules/_io/iobase.c
@@ -220,7 +220,6 @@ static PyObject *
_io__IOBase_close_impl(PyObject *self)
/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
{
- PyObject *res, *exc, *val, *tb;
int rc, closed = iobase_is_closed(self);
if (closed < 0) {
@@ -230,11 +229,11 @@ _io__IOBase_close_impl(PyObject *self)
Py_RETURN_NONE;
}
- res = PyObject_CallMethodNoArgs(self, &_Py_ID(flush));
+ PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(flush));
- PyErr_Fetch(&exc, &val, &tb);
+ PyObject *exc = PyErr_GetRaisedException();
rc = PyObject_SetAttr(self, &_Py_ID(__IOBase_closed), Py_True);
- _PyErr_ChainExceptions(exc, val, tb);
+ _PyErr_ChainExceptions1(exc);
if (rc < 0) {
Py_CLEAR(res);
}
@@ -252,11 +251,10 @@ static void
iobase_finalize(PyObject *self)
{
PyObject *res;
- PyObject *error_type, *error_value, *error_traceback;
int closed;
/* Save the current exception, if any. */
- PyErr_Fetch(&error_type, &error_value, &error_traceback);
+ PyObject *exc = PyErr_GetRaisedException();
/* If `closed` doesn't exist or can't be evaluated as bool, then the
object is probably in an unusable state, so ignore. */
@@ -297,7 +295,7 @@ iobase_finalize(PyObject *self)
}
/* Restore the saved exception. */
- PyErr_Restore(error_type, error_value, error_traceback);
+ PyErr_SetRaisedException(exc);
}
int
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index fbf0bf4..3ff84cb 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -2827,11 +2827,10 @@ finally:
fail:
if (saved_state) {
- PyObject *type, *value, *traceback;
- PyErr_Fetch(&type, &value, &traceback);
+ PyObject *exc = PyErr_GetRaisedException();
res = PyObject_CallMethodOneArg(
self->decoder, &_Py_ID(setstate), saved_state);
- _PyErr_ChainExceptions(type, value, traceback);
+ _PyErr_ChainExceptions1(exc);
Py_DECREF(saved_state);
Py_XDECREF(res);
}
@@ -3028,24 +3027,28 @@ _io_TextIOWrapper_close_impl(textio *self)
Py_RETURN_NONE; /* stream already closed */
}
else {
- PyObject *exc = NULL, *val, *tb;
+ PyObject *exc = NULL;
if (self->finalizing) {
res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(_dealloc_warn),
(PyObject *)self);
- if (res)
+ if (res) {
Py_DECREF(res);
- else
+ }
+ else {
PyErr_Clear();
+ }
}
res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush));
- if (res == NULL)
- PyErr_Fetch(&exc, &val, &tb);
- else
+ if (res == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
+ else {
Py_DECREF(res);
+ }
res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(close));
if (exc != NULL) {
- _PyErr_ChainExceptions(exc, val, tb);
+ _PyErr_ChainExceptions1(exc);
Py_CLEAR(res);
}
return res;
diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c
index e913d83..de07b50 100644
--- a/Modules/_io/winconsoleio.c
+++ b/Modules/_io/winconsoleio.c
@@ -192,7 +192,7 @@ _io__WindowsConsoleIO_close_impl(winconsoleio *self)
/*[clinic end generated code: output=27ef95b66c29057b input=68c4e5754f8136c2]*/
{
PyObject *res;
- PyObject *exc, *val, *tb;
+ PyObject *exc;
int rc;
res = PyObject_CallMethodOneArg((PyObject*)&PyRawIOBase_Type,
&_Py_ID(close), (PyObject*)self);
@@ -200,13 +200,16 @@ _io__WindowsConsoleIO_close_impl(winconsoleio *self)
self->fd = -1;
return res;
}
- if (res == NULL)
- PyErr_Fetch(&exc, &val, &tb);
+ if (res == NULL) {
+ exc = PyErr_GetRaisedException();
+ }
rc = internal_close(self);
- if (res == NULL)
- _PyErr_ChainExceptions(exc, val, tb);
- if (rc < 0)
+ if (res == NULL) {
+ _PyErr_ChainExceptions1(exc);
+ }
+ if (rc < 0) {
Py_CLEAR(res);
+ }
return res;
}