diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-07 21:13:46 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-07 21:13:46 (GMT) |
commit | a2ad5c3ad1bbf6d2088ff3ab2eb3bba51d096cc2 (patch) | |
tree | 097dbbd7fe661b0da5e496ee972f0c6df5a35842 /Modules | |
parent | 0e82fd1f78a9ae07c16b1f57a0f39bc56f798b30 (diff) | |
download | cpython-a2ad5c3ad1bbf6d2088ff3ab2eb3bba51d096cc2.zip cpython-a2ad5c3ad1bbf6d2088ff3ab2eb3bba51d096cc2.tar.gz cpython-a2ad5c3ad1bbf6d2088ff3ab2eb3bba51d096cc2.tar.bz2 |
Issue #15972: Fix error messages when os functions expecting a file name or
file descriptor receive the incorrect type.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/posixmodule.c | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 177be70..25330a0 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -427,26 +427,24 @@ win32_warn_bytes_api() #endif static int -_fd_converter(PyObject *o, int *p, int default_value) { - long long_value; - if (o == Py_None) { - *p = default_value; - return 1; - } - if (PyFloat_Check(o)) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); +_fd_converter(PyObject *o, int *p, const char *allowed) +{ + int overflow; + long long_value = PyLong_AsLongAndOverflow(o, &overflow); + if (PyFloat_Check(o) || + (long_value == -1 && !overflow && PyErr_Occurred())) { + PyErr_Clear(); + PyErr_Format(PyExc_TypeError, + "argument should be %s, not %.200s", + allowed, Py_TYPE(o)->tp_name); return 0; } - long_value = PyLong_AsLong(o); - if (long_value == -1 && PyErr_Occurred()) - return 0; - if (long_value > INT_MAX) { + if (overflow > 0 || long_value > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "signed integer is greater than maximum"); return 0; } - if (long_value < INT_MIN) { + if (overflow < 0 || long_value < INT_MIN) { PyErr_SetString(PyExc_OverflowError, "signed integer is less than minimum"); return 0; @@ -456,8 +454,13 @@ _fd_converter(PyObject *o, int *p, int default_value) { } static int -dir_fd_converter(PyObject *o, void *p) { - return _fd_converter(o, (int *)p, DEFAULT_DIR_FD); +dir_fd_converter(PyObject *o, void *p) +{ + if (o == Py_None) { + *(int *)p = DEFAULT_DIR_FD; + return 1; + } + return _fd_converter(o, (int *)p, "integer"); } @@ -634,17 +637,16 @@ path_converter(PyObject *o, void *p) { } else { PyErr_Clear(); - bytes = PyBytes_FromObject(o); + if (PyObject_CheckBuffer(o)) + bytes = PyBytes_FromObject(o); + else + bytes = NULL; if (!bytes) { PyErr_Clear(); if (path->allow_fd) { int fd; - /* - * note: _fd_converter always permits None. - * but we've already done our None check. - * so o cannot be None at this point. - */ - int result = _fd_converter(o, &fd, -1); + int result = _fd_converter(o, &fd, + "string, bytes or integer"); if (result) { path->wide = NULL; path->narrow = NULL; @@ -705,15 +707,17 @@ argument_unavailable_error(char *function_name, char *argument_name) { } static int -dir_fd_unavailable(PyObject *o, void *p) { - int *dir_fd = (int *)p; - int return_value = _fd_converter(o, dir_fd, DEFAULT_DIR_FD); - if (!return_value) +dir_fd_unavailable(PyObject *o, void *p) +{ + int dir_fd; + if (!dir_fd_converter(o, &dir_fd)) return 0; - if (*dir_fd == DEFAULT_DIR_FD) - return 1; - argument_unavailable_error(NULL, "dir_fd"); - return 0; + if (dir_fd != DEFAULT_DIR_FD) { + argument_unavailable_error(NULL, "dir_fd"); + return 0; + } + *(int *)p = dir_fd; + return 1; } static int |