[Python-checkins] cpython: Issue #22215: Now ValueError is raised instead of TypeError when str or bytes

serhiy.storchaka python-checkins at python.org
Sat Sep 6 19:08:26 CEST 2014


http://hg.python.org/cpython/rev/25032ec29315
changeset:   92365:25032ec29315
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Sep 06 20:07:17 2014 +0300
summary:
  Issue #22215: Now ValueError is raised instead of TypeError when str or bytes
argument contains not permitted null character or byte.

files:
  Lib/test/test_builtin.py  |   4 ++--
  Lib/test/test_fileio.py   |   4 ++--
  Lib/test/test_getargs2.py |  10 +++++-----
  Lib/test/test_io.py       |   4 ++--
  Lib/test/test_site.py     |   2 +-
  Misc/NEWS                 |   3 +++
  Modules/_io/fileio.c      |   2 +-
  Modules/_tkinter.c        |   4 ++--
  Modules/posixmodule.c     |   2 +-
  Modules/socketmodule.c    |   2 +-
  Objects/bytesobject.c     |   4 ++--
  Objects/unicodeobject.c   |  10 +++++-----
  Python/bltinmodule.c      |   4 ++--
  Python/getargs.c          |  25 ++++++++++++-------------
  14 files changed, 41 insertions(+), 39 deletions(-)


diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -312,11 +312,11 @@
         self.assertRaises(TypeError, compile)
         self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode')
         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',
                           mode='eval', source='0', filename='tmp')
         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')
 
         # test the optimize argument
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -361,8 +361,8 @@
 
     def testConstructorHandlesNULChars(self):
         fn_with_NUL = 'foo\0bar'
-        self.assertRaises(TypeError, _FileIO, fn_with_NUL, 'w')
-        self.assertRaises(TypeError, _FileIO, bytes(fn_with_NUL, 'ascii'), 'w')
+        self.assertRaises(ValueError, _FileIO, fn_with_NUL, 'w')
+        self.assertRaises(ValueError, _FileIO, bytes(fn_with_NUL, 'ascii'), 'w')
 
     def testInvalidFd(self):
         self.assertRaises(ValueError, _FileIO, -10)
diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py
--- a/Lib/test/test_getargs2.py
+++ b/Lib/test/test_getargs2.py
@@ -482,7 +482,7 @@
     def test_s(self):
         from _testcapi import getargs_s
         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, bytearray(b'bytearray'))
         self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview'))
@@ -509,7 +509,7 @@
     def test_z(self):
         from _testcapi import getargs_z
         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, bytearray(b'bytearray'))
         self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview'))
@@ -537,7 +537,7 @@
         from _testcapi import getargs_y
         self.assertRaises(TypeError, getargs_y, 'abc\xe9')
         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, memoryview(b'memoryview'))
         self.assertRaises(TypeError, getargs_y, None)
@@ -577,7 +577,7 @@
     def test_u(self):
         from _testcapi import getargs_u
         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, bytearray(b'bytearray'))
         self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview'))
@@ -595,7 +595,7 @@
     def test_Z(self):
         from _testcapi import getargs_Z
         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, bytearray(b'bytearray'))
         self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview'))
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -363,8 +363,8 @@
 
     def test_open_handles_NUL_chars(self):
         fn_with_NUL = 'foo\0bar'
-        self.assertRaises(TypeError, self.open, fn_with_NUL, 'w')
-        self.assertRaises(TypeError, self.open, bytes(fn_with_NUL, 'ascii'), 'w')
+        self.assertRaises(ValueError, self.open, fn_with_NUL, 'w')
+        self.assertRaises(ValueError, self.open, bytes(fn_with_NUL, 'ascii'), 'w')
 
     def test_raw_file_io(self):
         with self.open(support.TESTFN, "wb", buffering=0) as f:
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -147,7 +147,7 @@
             re.escape(os.path.join(pth_dir, pth_fn)))
         # XXX: ditto previous XXX comment.
         self.assertRegex(err_out.getvalue(), 'Traceback')
-        self.assertRegex(err_out.getvalue(), 'TypeError')
+        self.assertRegex(err_out.getvalue(), 'ValueError')
 
     def test_addsitedir(self):
         # Same tests for test_addpackage since addsitedir() essentially just
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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.
   This platform exposes the function ``ioctl(FIOCLEX)``, but calling it fails
   with errno is ENOTTY: "Inappropriate ioctl for device". set_inheritable()
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -256,7 +256,7 @@
         int rv = _PyUnicode_HasNULChars(nameobj);
         if (rv) {
             if (rv != -1)
-                PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+                PyErr_SetString(PyExc_ValueError, "embedded null character");
             return -1;
         }
         widename = PyUnicode_AsUnicode(nameobj);
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -1417,7 +1417,7 @@
         }
         s = PyBytes_AsString(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;
         }
         *out = s;
@@ -1434,7 +1434,7 @@
             return 0;
         }
         if (strlen(s) != (size_t)size) {
-            PyErr_SetString(PyExc_ValueError, "null character in string");
+            PyErr_SetString(PyExc_ValueError, "embedded null character");
             return 0;
         }
         *out = s;
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -903,7 +903,7 @@
 
     narrow = PyBytes_AS_STRING(bytes);
     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);
         return 0;
     }
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1273,7 +1273,7 @@
     }
     if (strlen(data->buf) != len) {
         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 Py_CLEANUP_SUPPORTED;
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -593,8 +593,8 @@
     if (len != NULL)
         *len = PyBytes_GET_SIZE(obj);
     else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "expected bytes with no null");
+        PyErr_SetString(PyExc_ValueError,
+                        "embedded null byte");
         return -1;
     }
     return 0;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -3247,7 +3247,7 @@
     wlen2 = wcslen(wstr);
     if (wlen2 != wlen) {
         PyMem_Free(wstr);
-        PyErr_SetString(PyExc_TypeError, "embedded null character");
+        PyErr_SetString(PyExc_ValueError, "embedded null character");
         return NULL;
     }
 
@@ -3519,8 +3519,8 @@
     if (locale_error_handler(errors, &surrogateescape) < 0)
         return NULL;
 
-    if (str[len] != '\0' || (size_t)len != strlen(str)) {
-        PyErr_SetString(PyExc_TypeError, "embedded null character");
+    if (str[len] != '\0' || (size_t)len != strlen(str))  {
+        PyErr_SetString(PyExc_ValueError, "embedded null byte");
         return NULL;
     }
 
@@ -3697,7 +3697,7 @@
     size = PyBytes_GET_SIZE(output);
     data = PyBytes_AS_STRING(output);
     if ((size_t)size != strlen(data)) {
-        PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+        PyErr_SetString(PyExc_ValueError, "embedded null byte");
         Py_DECREF(output);
         return 0;
     }
@@ -3741,7 +3741,7 @@
     }
     if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output),
                  PyUnicode_GET_LENGTH(output), 0, 1) >= 0) {
-        PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+        PyErr_SetString(PyExc_ValueError, "embedded null character");
         Py_DECREF(output);
         return 0;
     }
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -745,8 +745,8 @@
         return NULL;
     }
 
-    if (strlen(str) != (size_t)size) {
-        PyErr_SetString(PyExc_TypeError,
+    if (strlen(str) != (size_t)size)  {
+        PyErr_SetString(PyExc_ValueError,
                         "source code string cannot contain null bytes");
         return NULL;
     }
diff --git a/Python/getargs.c b/Python/getargs.c
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -872,10 +872,10 @@
             STORE_SIZE(count);
             format++;
         } else {
-            if (strlen(*p) != (size_t)count)
-                return converterr(
-                    "bytes without null bytes",
-                    arg, msgbuf, bufsize);
+            if (strlen(*p) != (size_t)count) {
+                PyErr_SetString(PyExc_ValueError, "embedded null byte");
+                RETURN_ERR_OCCURRED;
+            }
         }
         break;
     }
@@ -948,16 +948,15 @@
                 if (sarg == NULL)
                     return converterr(CONV_UNICODE,
                                       arg, msgbuf, bufsize);
+                if (strlen(sarg) != (size_t)len) {
+                    PyErr_SetString(PyExc_ValueError, "embedded null character");
+                    RETURN_ERR_OCCURRED;
+                }
                 *p = sarg;
             }
             else
                 return converterr(c == 'z' ? "str or None" : "str",
                                   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;
     }
@@ -994,10 +993,10 @@
                 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
                 if (*p == NULL)
                     RETURN_ERR_OCCURRED;
-                if (Py_UNICODE_strlen(*p) != (size_t)len)
-                    return converterr(
-                        "str without null characters or None",
-                        arg, msgbuf, bufsize);
+                if (Py_UNICODE_strlen(*p) != (size_t)len) {
+                    PyErr_SetString(PyExc_ValueError, "embedded null character");
+                    RETURN_ERR_OCCURRED;
+                }
             } else
                 return converterr(c == 'Z' ? "str or None" : "str",
                                   arg, msgbuf, bufsize);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list