diff -r 2f0716009132 Doc/library/stdtypes.rst --- a/Doc/library/stdtypes.rst Tue Jul 12 18:24:25 2016 -0400 +++ b/Doc/library/stdtypes.rst Wed Jul 13 17:41:57 2016 +0800 @@ -2648,6 +2648,9 @@ >>> b'read this short text'.translate(None, b'aeiou') b'rd ths shrt txt' + .. versionadded:: 3.6 + *delete* is now supported as keyword arguments. + The following methods on bytes and bytearray objects have default behaviours that assume the use of ASCII compatible binary formats, but can still be used diff -r 2f0716009132 Lib/test/test_bytes.py --- a/Lib/test/test_bytes.py Tue Jul 12 18:24:25 2016 -0400 +++ b/Lib/test/test_bytes.py Wed Jul 13 17:41:57 2016 +0800 @@ -1436,6 +1436,17 @@ self.assertEqual(c, b'hllo') self.assertRaises(TypeError, b.translate, None, None) self.assertRaises(TypeError, ba.translate, None, None) + self.assertRaises(ValueError, b.translate, bytes(range(255))) + self.assertRaises(ValueError, ba.translate, bytes(range(255))) + # test delete as keyword arguments + c = b.translate(rosetta) + self.assertEqual(c, b'helle') + c = ba.translate(rosetta) + self.assertEqual(c, b'helle') + c = b.translate(rosetta, delete=b'l') + self.assertEqual(c, b'hee') + c = ba.translate(rosetta, delete=b'l') + self.assertEqual(c, b'hee') def test_split_bytearray(self): self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b']) diff -r 2f0716009132 Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Tue Jul 12 18:24:25 2016 -0400 +++ b/Objects/bytearrayobject.c Wed Jul 13 17:41:57 2016 +0800 @@ -1177,10 +1177,8 @@ table: object Translation table, which must be a bytes object of length 256. - [ - deletechars: object - ] / + delete: object = NULL Return a copy with each character mapped by the given translation table. @@ -1190,8 +1188,8 @@ static PyObject * bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, - int group_right_1, PyObject *deletechars) -/*[clinic end generated code: output=2bebc86a9a1ff083 input=846a01671bccc1c5]*/ + PyObject *delete) +/*[clinic end generated code: output=374d10352682f8c8 input=69009f2dfb8a70ae]*/ { char *input, *output; const char *table_chars; @@ -1218,8 +1216,8 @@ table_chars = (const char*)vtable.buf; } - if (deletechars != NULL) { - if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) { + if (delete != NULL) { + if (PyObject_GetBuffer(delete, &vdel, PyBUF_SIMPLE) != 0) { if (table != NULL) PyBuffer_Release(&vtable); return NULL; @@ -1260,8 +1258,7 @@ for (i = inlen; --i >= 0; ) { c = Py_CHARMASK(*input++); if (trans_table[c] != -1) - if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) - continue; + *output++ = (char)trans_table[c]; } /* Fix the size of the resulting string */ if (inlen > 0) @@ -1273,7 +1270,7 @@ done: if (table != NULL) PyBuffer_Release(&vtable); - if (deletechars != NULL) + if (delete != NULL) PyBuffer_Release(&vdel); return result; } diff -r 2f0716009132 Objects/bytesobject.c --- a/Objects/bytesobject.c Tue Jul 12 18:24:25 2016 -0400 +++ b/Objects/bytesobject.c Wed Jul 13 17:41:57 2016 +0800 @@ -2045,10 +2045,8 @@ table: object Translation table, which must be a bytes object of length 256. - [ - deletechars: object - ] / + delete: object = NULL Return a copy with each character mapped by the given translation table. @@ -2057,9 +2055,8 @@ [clinic start generated code]*/ static PyObject * -bytes_translate_impl(PyBytesObject *self, PyObject *table, int group_right_1, - PyObject *deletechars) -/*[clinic end generated code: output=233df850eb50bf8d input=ca20edf39d780d49]*/ +bytes_translate_impl(PyBytesObject *self, PyObject *table, PyObject *delete) +/*[clinic end generated code: output=5770495170a36287 input=36460c033ccae26b]*/ { char *input, *output; Py_buffer table_view = {NULL, NULL}; @@ -2094,13 +2091,13 @@ return NULL; } - if (deletechars != NULL) { - if (PyBytes_Check(deletechars)) { - del_table_chars = PyBytes_AS_STRING(deletechars); - dellen = PyBytes_GET_SIZE(deletechars); + if (delete != NULL) { + if (PyBytes_Check(delete)) { + del_table_chars = PyBytes_AS_STRING(delete); + dellen = PyBytes_GET_SIZE(delete); } else { - if (PyObject_GetBuffer(deletechars, &del_table_view, PyBUF_SIMPLE) != 0) { + if (PyObject_GetBuffer(delete, &del_table_view, PyBUF_SIMPLE) != 0) { PyBuffer_Release(&table_view); return NULL; } diff -r 2f0716009132 Objects/clinic/bytearrayobject.c.h --- a/Objects/clinic/bytearrayobject.c.h Tue Jul 12 18:24:25 2016 -0400 +++ b/Objects/clinic/bytearrayobject.c.h Wed Jul 13 17:41:57 2016 +0800 @@ -39,7 +39,9 @@ } PyDoc_STRVAR(bytearray_translate__doc__, -"translate(table, [deletechars])\n" +"translate($self, table, /, delete=None)\n" +"--\n" +"\n" "Return a copy with each character mapped by the given translation table.\n" "\n" " table\n" @@ -49,37 +51,25 @@ "The remaining characters are mapped through the given translation table."); #define BYTEARRAY_TRANSLATE_METHODDEF \ - {"translate", (PyCFunction)bytearray_translate, METH_VARARGS, bytearray_translate__doc__}, + {"translate", (PyCFunction)bytearray_translate, METH_VARARGS|METH_KEYWORDS, bytearray_translate__doc__}, static PyObject * bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, - int group_right_1, PyObject *deletechars); + PyObject *delete); static PyObject * -bytearray_translate(PyByteArrayObject *self, PyObject *args) +bytearray_translate(PyByteArrayObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; + static char *_keywords[] = {"", "delete", NULL}; PyObject *table; - int group_right_1 = 0; - PyObject *deletechars = NULL; + PyObject *delete = NULL; - switch (PyTuple_GET_SIZE(args)) { - case 1: - if (!PyArg_ParseTuple(args, "O:translate", &table)) { - goto exit; - } - break; - case 2: - if (!PyArg_ParseTuple(args, "OO:translate", &table, &deletechars)) { - goto exit; - } - group_right_1 = 1; - break; - default: - PyErr_SetString(PyExc_TypeError, "bytearray.translate requires 1 to 2 arguments"); - goto exit; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:translate", _keywords, + &table, &delete)) { + goto exit; } - return_value = bytearray_translate_impl(self, table, group_right_1, deletechars); + return_value = bytearray_translate_impl(self, table, delete); exit: return return_value; @@ -716,4 +706,4 @@ { return bytearray_sizeof_impl(self); } -/*[clinic end generated code: output=a32f183ebef159cc input=a9049054013a1b77]*/ +/*[clinic end generated code: output=fdd00a8b478e4b8d input=a9049054013a1b77]*/ diff -r 2f0716009132 Objects/clinic/bytesobject.c.h --- a/Objects/clinic/bytesobject.c.h Tue Jul 12 18:24:25 2016 -0400 +++ b/Objects/clinic/bytesobject.c.h Wed Jul 13 17:41:57 2016 +0800 @@ -267,7 +267,9 @@ } PyDoc_STRVAR(bytes_translate__doc__, -"translate(table, [deletechars])\n" +"translate($self, table, /, delete=None)\n" +"--\n" +"\n" "Return a copy with each character mapped by the given translation table.\n" "\n" " table\n" @@ -277,37 +279,24 @@ "The remaining characters are mapped through the given translation table."); #define BYTES_TRANSLATE_METHODDEF \ - {"translate", (PyCFunction)bytes_translate, METH_VARARGS, bytes_translate__doc__}, + {"translate", (PyCFunction)bytes_translate, METH_VARARGS|METH_KEYWORDS, bytes_translate__doc__}, static PyObject * -bytes_translate_impl(PyBytesObject *self, PyObject *table, int group_right_1, - PyObject *deletechars); +bytes_translate_impl(PyBytesObject *self, PyObject *table, PyObject *delete); static PyObject * -bytes_translate(PyBytesObject *self, PyObject *args) +bytes_translate(PyBytesObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; + static char *_keywords[] = {"", "delete", NULL}; PyObject *table; - int group_right_1 = 0; - PyObject *deletechars = NULL; + PyObject *delete = NULL; - switch (PyTuple_GET_SIZE(args)) { - case 1: - if (!PyArg_ParseTuple(args, "O:translate", &table)) { - goto exit; - } - break; - case 2: - if (!PyArg_ParseTuple(args, "OO:translate", &table, &deletechars)) { - goto exit; - } - group_right_1 = 1; - break; - default: - PyErr_SetString(PyExc_TypeError, "bytes.translate requires 1 to 2 arguments"); - goto exit; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:translate", _keywords, + &table, &delete)) { + goto exit; } - return_value = bytes_translate_impl(self, table, group_right_1, deletechars); + return_value = bytes_translate_impl(self, table, delete); exit: return return_value; @@ -504,4 +493,4 @@ exit: return return_value; } -/*[clinic end generated code: output=6fe884a74e7d49cf input=a9049054013a1b77]*/ +/*[clinic end generated code: output=73a01a832ebb8d45 input=a9049054013a1b77]*/