Kaydet (Commit) 0b2cacb4 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #20335: bytes constructor now raises TypeError when encoding or errors

is specified with non-string argument.  Based on patch by Renaud Blanch.
...@@ -100,6 +100,14 @@ class BaseBytesTest: ...@@ -100,6 +100,14 @@ class BaseBytesTest:
self.assertRaises(TypeError, self.type2test, [0.0]) self.assertRaises(TypeError, self.type2test, [0.0])
self.assertRaises(TypeError, self.type2test, [None]) self.assertRaises(TypeError, self.type2test, [None])
self.assertRaises(TypeError, self.type2test, [C()]) 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): def test_constructor_value_errors(self):
self.assertRaises(ValueError, self.type2test, [-1]) self.assertRaises(ValueError, self.type2test, [-1])
......
...@@ -10,6 +10,9 @@ Release date: TBA ...@@ -10,6 +10,9 @@ Release date: TBA
Core and Builtins 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 - Issue #22834: If the current working directory ends up being set to a
non-existent directory then import will no longer raise FileNotFoundError. non-existent directory then import will no longer raise FileNotFoundError.
......
...@@ -3039,6 +3039,13 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -3039,6 +3039,13 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return new; 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 /* We'd like to call PyObject_Bytes here, but we need to check for an
integer argument before deferring to PyBytes_FromObject, something integer argument before deferring to PyBytes_FromObject, something
PyObject_Bytes doesn't do. */ PyObject_Bytes doesn't do. */
...@@ -3078,13 +3085,6 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -3078,13 +3085,6 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return new; 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); return PyBytes_FromObject(x);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment