summaryrefslogtreecommitdiff
path: root/Modules/_io
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2016-06-08 17:54:43 -0400
committerBarry Warsaw <barry@python.org>2016-06-08 17:54:43 -0400
commit6c3005329acb3a684eebfe8dffa08516313326cf (patch)
tree71841758809bd7aa5f01d84dc3bd53b4e0f6dbf6 /Modules/_io
parent54512b6e84565c83e19bcf94d186fd533e92445b (diff)
parent9b459ec20e055bc75da059b05c8b091ff9e4c208 (diff)
downloadcpython-6c3005329acb3a684eebfe8dffa08516313326cf.tar.gz
Issue #27066: Fixed SystemError if a custom opener (for open()) returns a
negative number without setting an exception.
Diffstat (limited to 'Modules/_io')
-rw-r--r--Modules/_io/_iomodule.c61
-rw-r--r--Modules/_io/_iomodule.h2
-rw-r--r--Modules/_io/bufferedio.c2
-rw-r--r--Modules/_io/bytesio.c6
-rw-r--r--Modules/_io/fileio.c10
-rw-r--r--Modules/_io/iobase.c2
-rw-r--r--Modules/_io/stringio.c2
-rw-r--r--Modules/_io/textio.c26
8 files changed, 63 insertions, 48 deletions
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index ec68170258..85b1813c92 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -238,20 +238,33 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
int text = 0, binary = 0, universal = 0;
char rawmode[6], *m;
- int line_buffering, isatty;
+ int line_buffering, is_number;
+ long isatty;
- PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL;
+ PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL;
_Py_IDENTIFIER(_blksize);
_Py_IDENTIFIER(isatty);
_Py_IDENTIFIER(mode);
_Py_IDENTIFIER(close);
- if (!PyUnicode_Check(file) &&
- !PyBytes_Check(file) &&
- !PyNumber_Check(file)) {
+ is_number = PyNumber_Check(file);
+
+ if (is_number) {
+ path_or_fd = file;
+ Py_INCREF(path_or_fd);
+ } else {
+ path_or_fd = PyOS_FSPath(file);
+ if (path_or_fd == NULL) {
+ return NULL;
+ }
+ }
+
+ if (!is_number &&
+ !PyUnicode_Check(path_or_fd) &&
+ !PyBytes_Check(path_or_fd)) {
PyErr_Format(PyExc_TypeError, "invalid file: %R", file);
- return NULL;
+ goto error;
}
/* Decode mode */
@@ -292,7 +305,7 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
if (strchr(mode+i+1, c)) {
invalid_mode:
PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode);
- return NULL;
+ goto error;
}
}
@@ -307,54 +320,57 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
/* Parameters validation */
if (universal) {
- if (writing || appending) {
+ if (creating || writing || appending || updating) {
PyErr_SetString(PyExc_ValueError,
- "can't use U and writing mode at once");
- return NULL;
+ "mode U cannot be combined with x', 'w', 'a', or '+'");
+ goto error;
}
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"'U' mode is deprecated", 1) < 0)
- return NULL;
+ goto error;
reading = 1;
}
if (text && binary) {
PyErr_SetString(PyExc_ValueError,
"can't have text and binary mode at once");
- return NULL;
+ goto error;
}
if (creating + reading + writing + appending > 1) {
PyErr_SetString(PyExc_ValueError,
"must have exactly one of create/read/write/append mode");
- return NULL;
+ goto error;
}
if (binary && encoding != NULL) {
PyErr_SetString(PyExc_ValueError,
"binary mode doesn't take an encoding argument");
- return NULL;
+ goto error;
}
if (binary && errors != NULL) {
PyErr_SetString(PyExc_ValueError,
"binary mode doesn't take an errors argument");
- return NULL;
+ goto error;
}
if (binary && newline != NULL) {
PyErr_SetString(PyExc_ValueError,
"binary mode doesn't take a newline argument");
- return NULL;
+ goto error;
}
/* Create the Raw file stream */
raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type,
- "OsiO", file, rawmode, closefd, opener);
+ "OsiO", path_or_fd, rawmode, closefd, opener);
if (raw == NULL)
- return NULL;
+ goto error;
result = raw;
+ Py_DECREF(path_or_fd);
+ path_or_fd = NULL;
+
modeobj = PyUnicode_FromString(mode);
if (modeobj == NULL)
goto error;
@@ -437,10 +453,10 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
/* wraps into a TextIOWrapper */
wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
- "Osssi",
- buffer,
- encoding, errors, newline,
- line_buffering);
+ "Osssi",
+ buffer,
+ encoding, errors, newline,
+ line_buffering);
if (wrapper == NULL)
goto error;
result = wrapper;
@@ -460,6 +476,7 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
Py_XDECREF(close_result);
Py_DECREF(result);
}
+ Py_XDECREF(path_or_fd);
Py_XDECREF(modeobj);
return NULL;
}
diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h
index 0c6eae26b7..3c48ff3aac 100644
--- a/Modules/_io/_iomodule.h
+++ b/Modules/_io/_iomodule.h
@@ -60,7 +60,7 @@ extern PyObject *_PyIncrementalNewlineDecoder_decode(
* Otherwise, the line ending is specified by readnl, a str object */
extern Py_ssize_t _PyIO_find_line_ending(
int translated, int universal, PyObject *readnl,
- int kind, char *start, char *end, Py_ssize_t *consumed);
+ int kind, const char *start, const char *end, Py_ssize_t *consumed);
/* Return 1 if an EnvironmentError with errno == EINTR is set (and then
clears the error indicator), 0 otherwise.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 6d67751c7d..cbe7425eae 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -659,7 +659,7 @@ _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len);
/* Sets the current error to BlockingIOError */
static void
-_set_BlockingIOError(char *msg, Py_ssize_t written)
+_set_BlockingIOError(const char *msg, Py_ssize_t written)
{
PyObject *err;
PyErr_Clear();
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index 9e5d78b166..a1ba121e26 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -87,7 +87,7 @@ scan_eol(bytesio *self, Py_ssize_t len)
static int
unshare_buffer(bytesio *self, size_t size)
{
- PyObject *new_buf, *old_buf;
+ PyObject *new_buf;
assert(SHARED_BUF(self));
assert(self->exports == 0);
assert(size >= (size_t)self->string_size);
@@ -96,9 +96,7 @@ unshare_buffer(bytesio *self, size_t size)
return -1;
memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf),
self->string_size);
- old_buf = self->buf;
- self->buf = new_buf;
- Py_DECREF(old_buf);
+ Py_SETREF(self->buf, new_buf);
return 0;
}
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 3d41d81179..12e5156fbb 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -92,8 +92,7 @@ fileio_dealloc_warn(fileio *self, PyObject *source)
if (self->fd >= 0 && self->closefd) {
PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb);
- if (PyErr_WarnFormat(PyExc_ResourceWarning, 1,
- "unclosed file %R", source)) {
+ if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
/* Spurious errors can appear at shutdown */
if (PyErr_ExceptionMatches(PyExc_Warning))
PyErr_WriteUnraisable((PyObject *) self);
@@ -423,7 +422,8 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
Py_DECREF(fdobj);
if (self->fd < 0) {
if (!PyErr_Occurred()) {
- /* The opener returned -1. See issue #27066 */
+ /* The opener returned a negative but didn't set an
+ exception. See issue #27066 */
PyErr_Format(PyExc_ValueError,
"opener returned %d", self->fd);
}
@@ -545,7 +545,7 @@ err_closed(void)
}
static PyObject *
-err_mode(char *action)
+err_mode(const char *action)
{
_PyIO_State *state = IO_STATE();
if (state != NULL)
@@ -1048,7 +1048,7 @@ _io_FileIO_truncate_impl(fileio *self, PyObject *posobj)
}
#endif /* HAVE_FTRUNCATE */
-static char *
+static const char *
mode_string(fileio *self)
{
if (self->created) {
diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c
index 212b0ddcfc..f07a0ca295 100644
--- a/Modules/_io/iobase.c
+++ b/Modules/_io/iobase.c
@@ -828,7 +828,7 @@ PyTypeObject PyIOBase_Type = {
0, /* tp_weaklist */
0, /* tp_del */
0, /* tp_version_tag */
- (destructor)iobase_finalize, /* tp_finalize */
+ iobase_finalize, /* tp_finalize */
};
diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c
index 06b4144578..ecf6dc1963 100644
--- a/Modules/_io/stringio.c
+++ b/Modules/_io/stringio.c
@@ -438,7 +438,7 @@ stringio_iternext(stringio *self)
_PyIO_str_readline, NULL);
if (line && !PyUnicode_Check(line)) {
PyErr_Format(PyExc_IOError,
- "readline() should have returned an str object, "
+ "readline() should have returned a str object, "
"not '%.200s'", Py_TYPE(line)->tp_name);
Py_DECREF(line);
return NULL;
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 063caa6067..96c8e7b453 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -772,7 +772,7 @@ typedef struct {
encodefunc_t encodefunc;
} encodefuncentry;
-static encodefuncentry encodefuncs[] = {
+static const encodefuncentry encodefuncs[] = {
{"ascii", (encodefunc_t) ascii_encode},
{"iso8859-1", (encodefunc_t) latin1_encode},
{"utf-8", (encodefunc_t) utf8_encode},
@@ -1021,7 +1021,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
goto error;
}
else if (PyUnicode_Check(res)) {
- encodefuncentry *e = encodefuncs;
+ const encodefuncentry *e = encodefuncs;
while (e->name != NULL) {
if (!PyUnicode_CompareWithASCIIString(res, e->name)) {
self->encodefunc = e->encodefunc;
@@ -1644,8 +1644,8 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
/* NOTE: `end` must point to the real end of the Py_UCS4 storage,
that is to the NUL character. Otherwise the function will produce
incorrect results. */
-static char *
-find_control_char(int kind, char *s, char *end, Py_UCS4 ch)
+static const char *
+find_control_char(int kind, const char *s, const char *end, Py_UCS4 ch)
{
if (kind == PyUnicode_1BYTE_KIND) {
assert(ch < 256);
@@ -1665,13 +1665,13 @@ find_control_char(int kind, char *s, char *end, Py_UCS4 ch)
Py_ssize_t
_PyIO_find_line_ending(
int translated, int universal, PyObject *readnl,
- int kind, char *start, char *end, Py_ssize_t *consumed)
+ int kind, const char *start, const char *end, Py_ssize_t *consumed)
{
Py_ssize_t len = ((char*)end - (char*)start)/kind;
if (translated) {
/* Newlines are already translated, only search for \n */
- char *pos = find_control_char(kind, start, end, '\n');
+ const char *pos = find_control_char(kind, start, end, '\n');
if (pos != NULL)
return (pos - start)/kind + 1;
else {
@@ -1683,7 +1683,7 @@ _PyIO_find_line_ending(
/* Universal newline search. Find any of \r, \r\n, \n
* The decoder ensures that \r\n are not split in two pieces
*/
- char *s = start;
+ const char *s = start;
for (;;) {
Py_UCS4 ch;
/* Fast path for non-control chars. The loop always ends
@@ -1713,21 +1713,21 @@ _PyIO_find_line_ending(
/* Assume that readnl is an ASCII character. */
assert(PyUnicode_KIND(readnl) == PyUnicode_1BYTE_KIND);
if (readnl_len == 1) {
- char *pos = find_control_char(kind, start, end, nl[0]);
+ const char *pos = find_control_char(kind, start, end, nl[0]);
if (pos != NULL)
return (pos - start)/kind + 1;
*consumed = len;
return -1;
}
else {
- char *s = start;
- char *e = end - (readnl_len - 1)*kind;
- char *pos;
+ const char *s = start;
+ const char *e = end - (readnl_len - 1)*kind;
+ const char *pos;
if (e < s)
e = s;
while (s < e) {
Py_ssize_t i;
- char *pos = find_control_char(kind, s, end, nl[0]);
+ const char *pos = find_control_char(kind, s, end, nl[0]);
if (pos == NULL || pos >= e)
break;
for (i = 1; i < readnl_len; i++) {
@@ -2689,7 +2689,7 @@ textiowrapper_iternext(textio *self)
_PyIO_str_readline, NULL);
if (line && !PyUnicode_Check(line)) {
PyErr_Format(PyExc_IOError,
- "readline() should have returned an str object, "
+ "readline() should have returned a str object, "
"not '%.200s'", Py_TYPE(line)->tp_name);
Py_DECREF(line);
return NULL;