summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2006-06-08 14:50:21 (GMT)
committerGeorg Brandl <georg@python.org>2006-06-08 14:50:21 (GMT)
commit98b40ad5902bc2e06db2f680774f3e3c2a3908d9 (patch)
tree1ee495dbf44b5bad5c32e70860ebe0b03c2642b1 /Objects
parent676725db928dae9f963b3a8389c0a9124e1eacbd (diff)
downloadcpython-98b40ad5902bc2e06db2f680774f3e3c2a3908d9.zip
cpython-98b40ad5902bc2e06db2f680774f3e3c2a3908d9.tar.gz
cpython-98b40ad5902bc2e06db2f680774f3e3c2a3908d9.tar.bz2
Bug #1502805: don't alias file.__exit__ to file.close since the
latter can return something that's true.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/fileobject.c19
1 files changed, 18 insertions, 1 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
index 29a5d4a..5e7f461 100644
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -1635,6 +1635,20 @@ file_self(PyFileObject *f)
return (PyObject *)f;
}
+static PyObject *
+file_exit(PyFileObject *f, PyObject *args)
+{
+ PyObject *ret = file_close(f);
+ if (!ret)
+ /* If error occurred, pass through */
+ return NULL;
+ Py_DECREF(ret);
+ /* We cannot return the result of close since a true
+ * value will be interpreted as "yes, swallow the
+ * exception if one was raised inside the with block". */
+ Py_RETURN_NONE;
+}
+
PyDoc_STRVAR(readline_doc,
"readline([size]) -> next line from the file, as a string.\n"
"\n"
@@ -1722,6 +1736,9 @@ PyDoc_STRVAR(isatty_doc,
PyDoc_STRVAR(enter_doc,
"__enter__() -> self.");
+PyDoc_STRVAR(exit_doc,
+ "__exit__(*excinfo) -> None. Closes the file.");
+
static PyMethodDef file_methods[] = {
{"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
{"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
@@ -1740,7 +1757,7 @@ static PyMethodDef file_methods[] = {
{"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
{"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
{"__enter__", (PyCFunction)file_self, METH_NOARGS, enter_doc},
- {"__exit__", (PyCFunction)file_close, METH_VARARGS, close_doc},
+ {"__exit__", (PyCFunction)file_exit, METH_VARARGS, exit_doc},
{NULL, NULL} /* sentinel */
};