[Python-checkins] cpython (merge 3.4 -> default): Issue #20335: bytes constructor now raises TypeError when encoding or errors

serhiy.storchaka python-checkins at python.org
Tue Dec 2 08:35:19 CET 2014


https://hg.python.org/cpython/rev/8d6b27837c69
changeset:   93699:8d6b27837c69
parent:      93697:15b35fc21995
parent:      93698:8badbd65840e
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Tue Dec 02 09:26:14 2014 +0200
summary:
  Issue #20335: bytes constructor now raises TypeError when encoding or errors
is specified with non-string argument.  Based on patch by Renaud Blanch.

files:
  Lib/test/test_bytes.py |   8 ++++++++
  Misc/NEWS              |   3 +++
  Objects/bytesobject.c  |  14 +++++++-------
  3 files changed, 18 insertions(+), 7 deletions(-)


diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -100,6 +100,14 @@
         self.assertRaises(TypeError, self.type2test, [0.0])
         self.assertRaises(TypeError, self.type2test, [None])
         self.assertRaises(TypeError, self.type2test, [C()])
+        self.assertRaises(TypeError, self.type2test, 0, 'ascii')
+        self.assertRaises(TypeError, self.type2test, b'', 'ascii')
+        self.assertRaises(TypeError, self.type2test, 0, errors='ignore')
+        self.assertRaises(TypeError, self.type2test, b'', errors='ignore')
+        self.assertRaises(TypeError, self.type2test, '')
+        self.assertRaises(TypeError, self.type2test, '', errors='ignore')
+        self.assertRaises(TypeError, self.type2test, '', b'ascii')
+        self.assertRaises(TypeError, self.type2test, '', 'ascii', b'ignore')
 
     def test_constructor_value_errors(self):
         self.assertRaises(ValueError, self.type2test, [-1])
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #20335: bytes constructor now raises TypeError when encoding or errors
+  is specified with non-string argument.  Based on patch by Renaud Blanch.
+
 - Issue #22834: If the current working directory ends up being set to a
   non-existent directory then import will no longer raise FileNotFoundError.
 
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -3039,6 +3039,13 @@
         return new;
     }
 
+    /* If it's not unicode, there can't be encoding or errors */
+    if (encoding != NULL || errors != NULL) {
+        PyErr_SetString(PyExc_TypeError,
+            "encoding or errors without a string argument");
+        return NULL;
+    }
+
     /* We'd like to call PyObject_Bytes here, but we need to check for an
        integer argument before deferring to PyBytes_FromObject, something
        PyObject_Bytes doesn't do. */
@@ -3078,13 +3085,6 @@
         return new;
     }
 
-    /* If it's not unicode, there can't be encoding or errors */
-    if (encoding != NULL || errors != NULL) {
-        PyErr_SetString(PyExc_TypeError,
-            "encoding or errors without a string argument");
-        return NULL;
-    }
-
     return PyBytes_FromObject(x);
 }
 

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


More information about the Python-checkins mailing list