mirror of https://github.com/python/cpython.git
Issue #15972: Fix error messages when os functions expecting a file name or
file descriptor receive the incorrect type.
This commit is contained in:
commit
c99b5120a7
|
@ -358,12 +358,28 @@ def test_fstat(self):
|
||||||
try:
|
try:
|
||||||
self.assertTrue(posix.fstat(fp.fileno()))
|
self.assertTrue(posix.fstat(fp.fileno()))
|
||||||
self.assertTrue(posix.stat(fp.fileno()))
|
self.assertTrue(posix.stat(fp.fileno()))
|
||||||
|
|
||||||
|
self.assertRaisesRegex(TypeError,
|
||||||
|
'should be string, bytes or integer, not',
|
||||||
|
posix.stat, float(fp.fileno()))
|
||||||
finally:
|
finally:
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
def test_stat(self):
|
def test_stat(self):
|
||||||
if hasattr(posix, 'stat'):
|
if hasattr(posix, 'stat'):
|
||||||
self.assertTrue(posix.stat(support.TESTFN))
|
self.assertTrue(posix.stat(support.TESTFN))
|
||||||
|
self.assertTrue(posix.stat(os.fsencode(support.TESTFN)))
|
||||||
|
self.assertTrue(posix.stat(bytearray(os.fsencode(support.TESTFN))))
|
||||||
|
|
||||||
|
self.assertRaisesRegex(TypeError,
|
||||||
|
'can\'t specify None for path argument',
|
||||||
|
posix.stat, None)
|
||||||
|
self.assertRaisesRegex(TypeError,
|
||||||
|
'should be string, bytes or integer, not',
|
||||||
|
posix.stat, list(support.TESTFN))
|
||||||
|
self.assertRaisesRegex(TypeError,
|
||||||
|
'should be string, bytes or integer, not',
|
||||||
|
posix.stat, list(os.fsencode(support.TESTFN)))
|
||||||
|
|
||||||
@unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
|
@unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
|
||||||
def test_mkfifo(self):
|
def test_mkfifo(self):
|
||||||
|
@ -714,6 +730,14 @@ def test_stat_dir_fd(self):
|
||||||
s1 = posix.stat(support.TESTFN)
|
s1 = posix.stat(support.TESTFN)
|
||||||
s2 = posix.stat(support.TESTFN, dir_fd=f)
|
s2 = posix.stat(support.TESTFN, dir_fd=f)
|
||||||
self.assertEqual(s1, s2)
|
self.assertEqual(s1, s2)
|
||||||
|
s2 = posix.stat(support.TESTFN, dir_fd=None)
|
||||||
|
self.assertEqual(s1, s2)
|
||||||
|
self.assertRaisesRegex(TypeError, 'should be integer, not',
|
||||||
|
posix.stat, support.TESTFN, dir_fd=posix.getcwd())
|
||||||
|
self.assertRaisesRegex(TypeError, 'should be integer, not',
|
||||||
|
posix.stat, support.TESTFN, dir_fd=float(f))
|
||||||
|
self.assertRaises(OverflowError,
|
||||||
|
posix.stat, support.TESTFN, dir_fd=10**20)
|
||||||
finally:
|
finally:
|
||||||
posix.close(f)
|
posix.close(f)
|
||||||
|
|
||||||
|
|
|
@ -206,6 +206,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #15972: Fix error messages when os functions expecting a file name or
|
||||||
|
file descriptor receive the incorrect type.
|
||||||
|
|
||||||
- Issue #8109: The ssl module now has support for server-side SNI, thanks
|
- Issue #8109: The ssl module now has support for server-side SNI, thanks
|
||||||
to a :meth:`SSLContext.set_servername_callback` method. Patch by Daniel
|
to a :meth:`SSLContext.set_servername_callback` method. Patch by Daniel
|
||||||
Black.
|
Black.
|
||||||
|
|
|
@ -396,26 +396,24 @@ win32_warn_bytes_api()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_fd_converter(PyObject *o, int *p, int default_value) {
|
_fd_converter(PyObject *o, int *p, const char *allowed)
|
||||||
long long_value;
|
{
|
||||||
if (o == Py_None) {
|
int overflow;
|
||||||
*p = default_value;
|
long long_value = PyLong_AsLongAndOverflow(o, &overflow);
|
||||||
return 1;
|
if (PyFloat_Check(o) ||
|
||||||
}
|
(long_value == -1 && !overflow && PyErr_Occurred())) {
|
||||||
if (PyFloat_Check(o)) {
|
PyErr_Clear();
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"integer argument expected, got float" );
|
"argument should be %s, not %.200s",
|
||||||
|
allowed, Py_TYPE(o)->tp_name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
long_value = PyLong_AsLong(o);
|
if (overflow > 0 || long_value > INT_MAX) {
|
||||||
if (long_value == -1 && PyErr_Occurred())
|
|
||||||
return 0;
|
|
||||||
if (long_value > INT_MAX) {
|
|
||||||
PyErr_SetString(PyExc_OverflowError,
|
PyErr_SetString(PyExc_OverflowError,
|
||||||
"signed integer is greater than maximum");
|
"signed integer is greater than maximum");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (long_value < INT_MIN) {
|
if (overflow < 0 || long_value < INT_MIN) {
|
||||||
PyErr_SetString(PyExc_OverflowError,
|
PyErr_SetString(PyExc_OverflowError,
|
||||||
"signed integer is less than minimum");
|
"signed integer is less than minimum");
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -425,8 +423,13 @@ _fd_converter(PyObject *o, int *p, int default_value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
dir_fd_converter(PyObject *o, void *p) {
|
dir_fd_converter(PyObject *o, void *p)
|
||||||
return _fd_converter(o, (int *)p, DEFAULT_DIR_FD);
|
{
|
||||||
|
if (o == Py_None) {
|
||||||
|
*(int *)p = DEFAULT_DIR_FD;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return _fd_converter(o, (int *)p, "integer");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -603,17 +606,16 @@ path_converter(PyObject *o, void *p) {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
if (PyObject_CheckBuffer(o))
|
||||||
bytes = PyBytes_FromObject(o);
|
bytes = PyBytes_FromObject(o);
|
||||||
|
else
|
||||||
|
bytes = NULL;
|
||||||
if (!bytes) {
|
if (!bytes) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
if (path->allow_fd) {
|
if (path->allow_fd) {
|
||||||
int fd;
|
int fd;
|
||||||
/*
|
int result = _fd_converter(o, &fd,
|
||||||
* note: _fd_converter always permits None.
|
"string, bytes or integer");
|
||||||
* but we've already done our None check.
|
|
||||||
* so o cannot be None at this point.
|
|
||||||
*/
|
|
||||||
int result = _fd_converter(o, &fd, -1);
|
|
||||||
if (result) {
|
if (result) {
|
||||||
path->wide = NULL;
|
path->wide = NULL;
|
||||||
path->narrow = NULL;
|
path->narrow = NULL;
|
||||||
|
@ -674,16 +676,18 @@ argument_unavailable_error(char *function_name, char *argument_name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
dir_fd_unavailable(PyObject *o, void *p) {
|
dir_fd_unavailable(PyObject *o, void *p)
|
||||||
int *dir_fd = (int *)p;
|
{
|
||||||
int return_value = _fd_converter(o, dir_fd, DEFAULT_DIR_FD);
|
int dir_fd;
|
||||||
if (!return_value)
|
if (!dir_fd_converter(o, &dir_fd))
|
||||||
return 0;
|
return 0;
|
||||||
if (*dir_fd == DEFAULT_DIR_FD)
|
if (dir_fd != DEFAULT_DIR_FD) {
|
||||||
return 1;
|
|
||||||
argument_unavailable_error(NULL, "dir_fd");
|
argument_unavailable_error(NULL, "dir_fd");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
*(int *)p = dir_fd;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
fd_specified(char *function_name, int fd) {
|
fd_specified(char *function_name, int fd) {
|
||||||
|
|
Loading…
Reference in New Issue