From 308d637c94b9ed88c386b83891ad38b2131ebd12 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Fri, 18 Sep 2009 21:42:35 +0000 Subject: Merged revisions 74929 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74929 | benjamin.peterson | 2009-09-18 16:14:55 -0500 (Fri, 18 Sep 2009) | 1 line add keyword arguments support to str/unicode encode and decode #6300 ........ --- Doc/library/stdtypes.rst | 12 ++++++++---- Lib/test/test_bytes.py | 2 ++ Lib/test/test_unicode.py | 4 ++++ Misc/ACKS | 1 + Objects/bytearrayobject.c | 7 ++++--- Objects/bytesobject.c | 7 ++++--- Objects/unicodeobject.c | 9 ++++++--- 7 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index c9e7287..a11c2d3 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -788,11 +788,10 @@ String Methods .. index:: pair: string; methods -String objects support the methods listed below. Note that none of these -methods take keyword arguments. +String objects support the methods listed below. -In addition, Python's strings support the sequence type methods described in -the :ref:`typesseq` section. To output formatted strings, see the +In addition, Python's strings support the sequence type methods described in the +:ref:`typesseq` section. To output formatted strings, see the :ref:`string-formatting` section. Also, see the :mod:`re` module for string functions based on regular expressions. @@ -825,6 +824,8 @@ functions based on regular expressions. :func:`codecs.register_error`, see section :ref:`codec-base-classes`. For a list of possible encodings, see section :ref:`standard-encodings`. + .. versionchanged:: 3.1 + Added support for keyword arguments added. .. method:: str.endswith(suffix[, start[, end]]) @@ -1539,6 +1540,9 @@ Wherever one of these methods needs to interpret the bytes as characters :func:`codecs.register_error`, see section :ref:`codec-base-classes`. For a list of possible encodings, see section :ref:`standard-encodings`. + .. versionchanged:: 3.1 + Added support for keyword arguments. + The bytes and bytearray types have an additional class method: diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index dd01b93..ad11686 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -186,6 +186,8 @@ class BaseBytesTest(unittest.TestCase): b = self.type2test(sample, "latin1") self.assertRaises(UnicodeDecodeError, b.decode, "utf8") self.assertEqual(b.decode("utf8", "ignore"), "Hello world\n") + self.assertEqual(b.decode(errors="ignore", encoding="utf8"), + "Hello world\n") def test_from_int(self): b = self.type2test(0) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 7f87b43..66368f4 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -955,6 +955,10 @@ class UnicodeTest( self.assertRaises(UnicodeError, 'Andr\202 x'.encode, 'ascii','strict') self.assertEqual('Andr\202 x'.encode('ascii','ignore'), b"Andr x") self.assertEqual('Andr\202 x'.encode('ascii','replace'), b"Andr? x") + self.assertEqual('Andr\202 x'.encode('ascii', 'replace'), + 'Andr\202 x'.encode('ascii', errors='replace')) + self.assertEqual('Andr\202 x'.encode('ascii', 'ignore'), + 'Andr\202 x'.encode(encoding='ascii', errors='ignore')) # Error handling (decoding) self.assertRaises(UnicodeError, str, b'Andr\202 x', 'ascii') diff --git a/Misc/ACKS b/Misc/ACKS index defc355..b2ada2b 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -87,6 +87,7 @@ Peter Bosch Eric Bouck Thierry Bousch Sebastian Boving +Jeff Bradberry Monty Brandenberg Georg Brandl Christopher Brannon diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 835244a..c09ccde 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2877,12 +2877,13 @@ as well as any other name registered with codecs.register_error that is\n\ able to handle UnicodeDecodeErrors."); static PyObject * -bytearray_decode(PyObject *self, PyObject *args) +bytearray_decode(PyObject *self, PyObject *args, PyObject *kwargs) { const char *encoding = NULL; const char *errors = NULL; + static char *kwlist[] = {"encoding", "errors", 0}; - if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encoding, &errors)) return NULL; if (encoding == NULL) encoding = PyUnicode_GetDefaultEncoding(); @@ -3112,7 +3113,7 @@ bytearray_methods[] = { _Py_capitalize__doc__}, {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, - {"decode", (PyCFunction)bytearray_decode, METH_VARARGS, decode_doc}, + {"decode", (PyCFunction)bytearray_decode, METH_VARARGS | METH_KEYWORDS, decode_doc}, {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__}, {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, expandtabs__doc__}, diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index fb4a845..27d4f95 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -2725,12 +2725,13 @@ as well as any other name registerd with codecs.register_error that is\n\ able to handle UnicodeDecodeErrors."); static PyObject * -bytes_decode(PyObject *self, PyObject *args) +bytes_decode(PyObject *self, PyObject *args, PyObject *kwargs) { const char *encoding = NULL; const char *errors = NULL; + static char *kwlist[] = {"encoding", "errors", 0}; - if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encoding, &errors)) return NULL; if (encoding == NULL) encoding = PyUnicode_GetDefaultEncoding(); @@ -2831,7 +2832,7 @@ bytes_methods[] = { _Py_capitalize__doc__}, {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__}, - {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode__doc__}, + {"decode", (PyCFunction)bytes_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__}, {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, endswith__doc__}, {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 758d054..78ef7e1 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -7141,13 +7141,15 @@ a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ codecs.register_error that can handle UnicodeEncodeErrors."); static PyObject * -unicode_encode(PyUnicodeObject *self, PyObject *args) +unicode_encode(PyUnicodeObject *self, PyObject *args, PyObject *kwargs) { + static char *kwlist[] = {"encoding", "errors", 0}; char *encoding = NULL; char *errors = NULL; PyObject *v; - if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", + kwlist, &encoding, &errors)) return NULL; v = PyUnicode_AsEncodedString((PyObject *)self, encoding, errors); if (v == NULL) @@ -8804,7 +8806,7 @@ static PyMethodDef unicode_methods[] = { /* Order is according to common usage: often used methods should appear first, since lookup is done sequentially. */ - {"encode", (PyCFunction) unicode_encode, METH_VARARGS, encode__doc__}, + {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__}, {"split", (PyCFunction) unicode_split, METH_VARARGS, split__doc__}, {"rsplit", (PyCFunction) unicode_rsplit, METH_VARARGS, rsplit__doc__}, @@ -8820,6 +8822,7 @@ static PyMethodDef unicode_methods[] = { {"ljust", (PyCFunction) unicode_ljust, METH_VARARGS, ljust__doc__}, {"lower", (PyCFunction) unicode_lower, METH_NOARGS, lower__doc__}, {"lstrip", (PyCFunction) unicode_lstrip, METH_VARARGS, lstrip__doc__}, +/* {"maketrans", (PyCFunction) unicode_maketrans, METH_VARARGS, maketrans__doc__}, */ {"rfind", (PyCFunction) unicode_rfind, METH_VARARGS, rfind__doc__}, {"rindex", (PyCFunction) unicode_rindex, METH_VARARGS, rindex__doc__}, {"rjust", (PyCFunction) unicode_rjust, METH_VARARGS, rjust__doc__}, -- cgit v0.12