Issue #22215: Now ValueError is raised instead of TypeError when str or bytes

argument contains not permitted null character or byte.
This commit is contained in:
Serhiy Storchaka 2014-09-06 20:07:17 +03:00
parent 4a4b679515
commit d8a1447c99
14 changed files with 41 additions and 39 deletions

View File

@ -312,11 +312,11 @@ class BuiltinTest(unittest.TestCase):
self.assertRaises(TypeError, compile) self.assertRaises(TypeError, compile)
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode') self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode')
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'single', 0xff) self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'single', 0xff)
self.assertRaises(TypeError, compile, chr(0), 'f', 'exec') self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
self.assertRaises(TypeError, compile, 'pass', '?', 'exec', self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
mode='eval', source='0', filename='tmp') mode='eval', source='0', filename='tmp')
compile('print("\xe5")\n', '', 'exec') compile('print("\xe5")\n', '', 'exec')
self.assertRaises(TypeError, compile, chr(0), 'f', 'exec') self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad') self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad')
# test the optimize argument # test the optimize argument

View File

@ -361,8 +361,8 @@ class OtherFileTests(unittest.TestCase):
def testConstructorHandlesNULChars(self): def testConstructorHandlesNULChars(self):
fn_with_NUL = 'foo\0bar' fn_with_NUL = 'foo\0bar'
self.assertRaises(TypeError, _FileIO, fn_with_NUL, 'w') self.assertRaises(ValueError, _FileIO, fn_with_NUL, 'w')
self.assertRaises(TypeError, _FileIO, bytes(fn_with_NUL, 'ascii'), 'w') self.assertRaises(ValueError, _FileIO, bytes(fn_with_NUL, 'ascii'), 'w')
def testInvalidFd(self): def testInvalidFd(self):
self.assertRaises(ValueError, _FileIO, -10) self.assertRaises(ValueError, _FileIO, -10)

View File

@ -482,7 +482,7 @@ class Bytes_TestCase(unittest.TestCase):
def test_s(self): def test_s(self):
from _testcapi import getargs_s from _testcapi import getargs_s
self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9') self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9')
self.assertRaises(TypeError, getargs_s, 'nul:\0') self.assertRaises(ValueError, getargs_s, 'nul:\0')
self.assertRaises(TypeError, getargs_s, b'bytes') self.assertRaises(TypeError, getargs_s, b'bytes')
self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray')) self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview')) self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview'))
@ -509,7 +509,7 @@ class Bytes_TestCase(unittest.TestCase):
def test_z(self): def test_z(self):
from _testcapi import getargs_z from _testcapi import getargs_z
self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9') self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9')
self.assertRaises(TypeError, getargs_z, 'nul:\0') self.assertRaises(ValueError, getargs_z, 'nul:\0')
self.assertRaises(TypeError, getargs_z, b'bytes') self.assertRaises(TypeError, getargs_z, b'bytes')
self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray')) self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview')) self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview'))
@ -537,7 +537,7 @@ class Bytes_TestCase(unittest.TestCase):
from _testcapi import getargs_y from _testcapi import getargs_y
self.assertRaises(TypeError, getargs_y, 'abc\xe9') self.assertRaises(TypeError, getargs_y, 'abc\xe9')
self.assertEqual(getargs_y(b'bytes'), b'bytes') self.assertEqual(getargs_y(b'bytes'), b'bytes')
self.assertRaises(TypeError, getargs_y, b'nul:\0') self.assertRaises(ValueError, getargs_y, b'nul:\0')
self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray')) self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview')) self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview'))
self.assertRaises(TypeError, getargs_y, None) self.assertRaises(TypeError, getargs_y, None)
@ -577,7 +577,7 @@ class Unicode_TestCase(unittest.TestCase):
def test_u(self): def test_u(self):
from _testcapi import getargs_u from _testcapi import getargs_u
self.assertEqual(getargs_u('abc\xe9'), 'abc\xe9') self.assertEqual(getargs_u('abc\xe9'), 'abc\xe9')
self.assertRaises(TypeError, getargs_u, 'nul:\0') self.assertRaises(ValueError, getargs_u, 'nul:\0')
self.assertRaises(TypeError, getargs_u, b'bytes') self.assertRaises(TypeError, getargs_u, b'bytes')
self.assertRaises(TypeError, getargs_u, bytearray(b'bytearray')) self.assertRaises(TypeError, getargs_u, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview')) self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview'))
@ -595,7 +595,7 @@ class Unicode_TestCase(unittest.TestCase):
def test_Z(self): def test_Z(self):
from _testcapi import getargs_Z from _testcapi import getargs_Z
self.assertEqual(getargs_Z('abc\xe9'), 'abc\xe9') self.assertEqual(getargs_Z('abc\xe9'), 'abc\xe9')
self.assertRaises(TypeError, getargs_Z, 'nul:\0') self.assertRaises(ValueError, getargs_Z, 'nul:\0')
self.assertRaises(TypeError, getargs_Z, b'bytes') self.assertRaises(TypeError, getargs_Z, b'bytes')
self.assertRaises(TypeError, getargs_Z, bytearray(b'bytearray')) self.assertRaises(TypeError, getargs_Z, bytearray(b'bytearray'))
self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview')) self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview'))

View File

@ -363,8 +363,8 @@ class IOTest(unittest.TestCase):
def test_open_handles_NUL_chars(self): def test_open_handles_NUL_chars(self):
fn_with_NUL = 'foo\0bar' fn_with_NUL = 'foo\0bar'
self.assertRaises(TypeError, self.open, fn_with_NUL, 'w') self.assertRaises(ValueError, self.open, fn_with_NUL, 'w')
self.assertRaises(TypeError, self.open, bytes(fn_with_NUL, 'ascii'), 'w') self.assertRaises(ValueError, self.open, bytes(fn_with_NUL, 'ascii'), 'w')
def test_raw_file_io(self): def test_raw_file_io(self):
with self.open(support.TESTFN, "wb", buffering=0) as f: with self.open(support.TESTFN, "wb", buffering=0) as f:

View File

@ -147,7 +147,7 @@ class HelperFunctionsTests(unittest.TestCase):
re.escape(os.path.join(pth_dir, pth_fn))) re.escape(os.path.join(pth_dir, pth_fn)))
# XXX: ditto previous XXX comment. # XXX: ditto previous XXX comment.
self.assertRegex(err_out.getvalue(), 'Traceback') self.assertRegex(err_out.getvalue(), 'Traceback')
self.assertRegex(err_out.getvalue(), 'TypeError') self.assertRegex(err_out.getvalue(), 'ValueError')
def test_addsitedir(self): def test_addsitedir(self):
# Same tests for test_addpackage since addsitedir() essentially just # Same tests for test_addpackage since addsitedir() essentially just

View File

@ -10,6 +10,9 @@ Release date: TBA
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #22215: Now ValueError is raised instead of TypeError when str or bytes
argument contains not permitted null character or byte.
- Issue #22258: Fix the internal function set_inheritable() on Illumos. - Issue #22258: Fix the internal function set_inheritable() on Illumos.
This platform exposes the function ``ioctl(FIOCLEX)``, but calling it fails This platform exposes the function ``ioctl(FIOCLEX)``, but calling it fails
with errno is ENOTTY: "Inappropriate ioctl for device". set_inheritable() with errno is ENOTTY: "Inappropriate ioctl for device". set_inheritable()

View File

@ -256,7 +256,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
int rv = _PyUnicode_HasNULChars(nameobj); int rv = _PyUnicode_HasNULChars(nameobj);
if (rv) { if (rv) {
if (rv != -1) if (rv != -1)
PyErr_SetString(PyExc_TypeError, "embedded NUL character"); PyErr_SetString(PyExc_ValueError, "embedded null character");
return -1; return -1;
} }
widename = PyUnicode_AsUnicode(nameobj); widename = PyUnicode_AsUnicode(nameobj);

View File

@ -1417,7 +1417,7 @@ varname_converter(PyObject *in, void *_out)
} }
s = PyBytes_AsString(in); s = PyBytes_AsString(in);
if (strlen(s) != (size_t)PyBytes_Size(in)) { if (strlen(s) != (size_t)PyBytes_Size(in)) {
PyErr_SetString(PyExc_ValueError, "null byte in bytes object"); PyErr_SetString(PyExc_ValueError, "embedded null byte");
return 0; return 0;
} }
*out = s; *out = s;
@ -1434,7 +1434,7 @@ varname_converter(PyObject *in, void *_out)
return 0; return 0;
} }
if (strlen(s) != (size_t)size) { if (strlen(s) != (size_t)size) {
PyErr_SetString(PyExc_ValueError, "null character in string"); PyErr_SetString(PyExc_ValueError, "embedded null character");
return 0; return 0;
} }
*out = s; *out = s;

View File

@ -903,7 +903,7 @@ path_converter(PyObject *o, void *p) {
narrow = PyBytes_AS_STRING(bytes); narrow = PyBytes_AS_STRING(bytes);
if ((size_t)length != strlen(narrow)) { if ((size_t)length != strlen(narrow)) {
FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s"); FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Py_DECREF(bytes); Py_DECREF(bytes);
return 0; return 0;
} }

View File

@ -1273,7 +1273,7 @@ idna_converter(PyObject *obj, struct maybe_idna *data)
} }
if (strlen(data->buf) != len) { if (strlen(data->buf) != len) {
Py_CLEAR(data->obj); Py_CLEAR(data->obj);
PyErr_SetString(PyExc_TypeError, "host name must not contain NUL character"); PyErr_SetString(PyExc_TypeError, "host name must not contain null character");
return 0; return 0;
} }
return Py_CLEANUP_SUPPORTED; return Py_CLEANUP_SUPPORTED;

View File

@ -593,8 +593,8 @@ PyBytes_AsStringAndSize(PyObject *obj,
if (len != NULL) if (len != NULL)
*len = PyBytes_GET_SIZE(obj); *len = PyBytes_GET_SIZE(obj);
else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) { else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_ValueError,
"expected bytes with no null"); "embedded null byte");
return -1; return -1;
} }
return 0; return 0;

View File

@ -3247,7 +3247,7 @@ PyUnicode_EncodeLocale(PyObject *unicode, const char *errors)
wlen2 = wcslen(wstr); wlen2 = wcslen(wstr);
if (wlen2 != wlen) { if (wlen2 != wlen) {
PyMem_Free(wstr); PyMem_Free(wstr);
PyErr_SetString(PyExc_TypeError, "embedded null character"); PyErr_SetString(PyExc_ValueError, "embedded null character");
return NULL; return NULL;
} }
@ -3519,8 +3519,8 @@ PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len,
if (locale_error_handler(errors, &surrogateescape) < 0) if (locale_error_handler(errors, &surrogateescape) < 0)
return NULL; return NULL;
if (str[len] != '\0' || (size_t)len != strlen(str)) { if (str[len] != '\0' || (size_t)len != strlen(str)) {
PyErr_SetString(PyExc_TypeError, "embedded null character"); PyErr_SetString(PyExc_ValueError, "embedded null byte");
return NULL; return NULL;
} }
@ -3697,7 +3697,7 @@ PyUnicode_FSConverter(PyObject* arg, void* addr)
size = PyBytes_GET_SIZE(output); size = PyBytes_GET_SIZE(output);
data = PyBytes_AS_STRING(output); data = PyBytes_AS_STRING(output);
if ((size_t)size != strlen(data)) { if ((size_t)size != strlen(data)) {
PyErr_SetString(PyExc_TypeError, "embedded NUL character"); PyErr_SetString(PyExc_ValueError, "embedded null byte");
Py_DECREF(output); Py_DECREF(output);
return 0; return 0;
} }
@ -3741,7 +3741,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
} }
if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output), if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output),
PyUnicode_GET_LENGTH(output), 0, 1) >= 0) { PyUnicode_GET_LENGTH(output), 0, 1) >= 0) {
PyErr_SetString(PyExc_TypeError, "embedded NUL character"); PyErr_SetString(PyExc_ValueError, "embedded null character");
Py_DECREF(output); Py_DECREF(output);
return 0; return 0;
} }

View File

@ -745,8 +745,8 @@ source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf)
return NULL; return NULL;
} }
if (strlen(str) != (size_t)size) { if (strlen(str) != (size_t)size) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_ValueError,
"source code string cannot contain null bytes"); "source code string cannot contain null bytes");
return NULL; return NULL;
} }

View File

@ -872,10 +872,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
STORE_SIZE(count); STORE_SIZE(count);
format++; format++;
} else { } else {
if (strlen(*p) != (size_t)count) if (strlen(*p) != (size_t)count) {
return converterr( PyErr_SetString(PyExc_ValueError, "embedded null byte");
"bytes without null bytes", RETURN_ERR_OCCURRED;
arg, msgbuf, bufsize); }
} }
break; break;
} }
@ -948,16 +948,15 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
if (sarg == NULL) if (sarg == NULL)
return converterr(CONV_UNICODE, return converterr(CONV_UNICODE,
arg, msgbuf, bufsize); arg, msgbuf, bufsize);
if (strlen(sarg) != (size_t)len) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
RETURN_ERR_OCCURRED;
}
*p = sarg; *p = sarg;
} }
else else
return converterr(c == 'z' ? "str or None" : "str", return converterr(c == 'z' ? "str or None" : "str",
arg, msgbuf, bufsize); arg, msgbuf, bufsize);
if (*p != NULL && sarg != NULL && (Py_ssize_t) strlen(*p) != len)
return converterr(
c == 'z' ? "str without null characters or None"
: "str without null characters",
arg, msgbuf, bufsize);
} }
break; break;
} }
@ -994,10 +993,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
*p = PyUnicode_AsUnicodeAndSize(arg, &len); *p = PyUnicode_AsUnicodeAndSize(arg, &len);
if (*p == NULL) if (*p == NULL)
RETURN_ERR_OCCURRED; RETURN_ERR_OCCURRED;
if (Py_UNICODE_strlen(*p) != (size_t)len) if (Py_UNICODE_strlen(*p) != (size_t)len) {
return converterr( PyErr_SetString(PyExc_ValueError, "embedded null character");
"str without null characters or None", RETURN_ERR_OCCURRED;
arg, msgbuf, bufsize); }
} else } else
return converterr(c == 'Z' ? "str or None" : "str", return converterr(c == 'Z' ? "str or None" : "str",
arg, msgbuf, bufsize); arg, msgbuf, bufsize);