summaryrefslogtreecommitdiff
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-03-20 00:51:16 +0200
committerGitHub <noreply@github.com>2017-03-20 00:51:16 +0200
commit08612ed6a91003cdba7617dee7787c26495c50d9 (patch)
tree0fb7999cb2703c4eee4462bd5d8addcebc5f741c /Modules
parentbb67f10179011c50805a942cb6f2701c84854888 (diff)
downloadcpython-git-08612ed6a91003cdba7617dee7787c26495c50d9.tar.gz
bpo-25455: Fixed crashes in repr of recursive buffered file-like objects. (#514) (#727)
(cherry picked from commit a5af6e1af77ee0f9294c5776478a9c24d9fbab94)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_io/bufferedio.c14
-rw-r--r--Modules/_io/fileio.c16
-rw-r--r--Modules/_io/textio.c23
3 files changed, 45 insertions, 8 deletions
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 6d67751c7d..909c4fa846 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -1416,8 +1416,18 @@ buffered_repr(buffered *self)
res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
}
else {
- res = PyUnicode_FromFormat("<%s name=%R>",
- Py_TYPE(self)->tp_name, nameobj);
+ int status = Py_ReprEnter((PyObject *)self);
+ res = NULL;
+ if (status == 0) {
+ res = PyUnicode_FromFormat("<%s name=%R>",
+ Py_TYPE(self)->tp_name, nameobj);
+ Py_ReprLeave((PyObject *)self);
+ }
+ else if (status > 0) {
+ PyErr_Format(PyExc_RuntimeError,
+ "reentrant call inside %s.__repr__",
+ Py_TYPE(self)->tp_name);
+ }
Py_DECREF(nameobj);
}
return res;
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 919cf502dc..c77f05521d 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -1093,9 +1093,19 @@ fileio_repr(fileio *self)
self->fd, mode_string(self), self->closefd ? "True" : "False");
}
else {
- res = PyUnicode_FromFormat(
- "<_io.FileIO name=%R mode='%s' closefd=%s>",
- nameobj, mode_string(self), self->closefd ? "True" : "False");
+ int status = Py_ReprEnter((PyObject *)self);
+ res = NULL;
+ if (status == 0) {
+ res = PyUnicode_FromFormat(
+ "<_io.FileIO name=%R mode='%s' closefd=%s>",
+ nameobj, mode_string(self), self->closefd ? "True" : "False");
+ Py_ReprLeave((PyObject *)self);
+ }
+ else if (status > 0) {
+ PyErr_Format(PyExc_RuntimeError,
+ "reentrant call inside %s.__repr__",
+ Py_TYPE(self)->tp_name);
+ }
Py_DECREF(nameobj);
}
return res;
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 89b0798cd0..3ab11387bf 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -2483,6 +2483,7 @@ static PyObject *
textiowrapper_repr(textio *self)
{
PyObject *nameobj, *modeobj, *res, *s;
+ int status;
CHECK_INITIALIZED(self);
@@ -2490,6 +2491,15 @@ textiowrapper_repr(textio *self)
if (res == NULL)
return NULL;
+ status = Py_ReprEnter((PyObject *)self);
+ if (status != 0) {
+ if (status > 0) {
+ PyErr_Format(PyExc_RuntimeError,
+ "reentrant call inside %s.__repr__",
+ Py_TYPE(self)->tp_name);
+ }
+ goto error;
+ }
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
if (nameobj == NULL) {
if (PyErr_ExceptionMatches(PyExc_Exception))
@@ -2504,7 +2514,7 @@ textiowrapper_repr(textio *self)
goto error;
PyUnicode_AppendAndDel(&res, s);
if (res == NULL)
- return NULL;
+ goto error;
}
modeobj = _PyObject_GetAttrId((PyObject *) self, &PyId_mode);
if (modeobj == NULL) {
@@ -2520,14 +2530,21 @@ textiowrapper_repr(textio *self)
goto error;
PyUnicode_AppendAndDel(&res, s);
if (res == NULL)
- return NULL;
+ goto error;
}
s = PyUnicode_FromFormat("%U encoding=%R>",
res, self->encoding);
Py_DECREF(res);
+ if (status == 0) {
+ Py_ReprLeave((PyObject *)self);
+ }
return s;
-error:
+
+ error:
Py_XDECREF(res);
+ if (status == 0) {
+ Py_ReprLeave((PyObject *)self);
+ }
return NULL;
}