]> granicus.if.org Git - python/commitdiff
Merged revisions 74929 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Fri, 18 Sep 2009 21:42:35 +0000 (21:42 +0000)
committerBenjamin Peterson <benjamin@python.org>
Fri, 18 Sep 2009 21:42:35 +0000 (21:42 +0000)
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
Lib/test/test_bytes.py
Lib/test/test_unicode.py
Misc/ACKS
Objects/bytearrayobject.c
Objects/bytesobject.c
Objects/unicodeobject.c

index c9e7287d46ad76d5e00240ca30f69799d819380d..a11c2d3093d51af77512540ff279efc551500b16 100644 (file)
@@ -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:
 
index dd01b936b26f9bb25656e489db085c4e789f4b24..ad11686fce1c819a1eee0f513279949d50d13641 100644 (file)
@@ -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)
index 7f87b438ea537653cd36c9463fcd922d7cce1255..66368f4e2329fd5d0a19a636af6bc7470737fde1 100644 (file)
@@ -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')
index defc3555f7b4b1c86a522e0bb6009117a0f2690c..b2ada2bdca5b8ae10f389b59a984781b7f0ed598 100644 (file)
--- 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
index 835244abe74a4b2f609319b12eacd26653dc9b1c..c09ccde7432f1c011e07744f2cb24b6f3387e3cb 100644 (file)
@@ -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__},
index fb4a845b40d2a56995ed77f6668348cb0d05f803..27d4f95f06d2af187d97fe265300f107f9143695 100644 (file)
@@ -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,
index 758d05489e3a9672fcfccc61b88784654be3b2ef..78ef7e1c6ebacddbbf31ec71012564d55132f3f8 100644 (file)
@@ -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__},