[Python-checkins] r74935 - in python/branches/py3k: 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

benjamin.peterson python-checkins at python.org
Fri Sep 18 23:42:35 CEST 2009


Author: benjamin.peterson
Date: Fri Sep 18 23:42:35 2009
New Revision: 74935

Log:
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
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Doc/library/stdtypes.rst
   python/branches/py3k/Lib/test/test_bytes.py
   python/branches/py3k/Lib/test/test_unicode.py
   python/branches/py3k/Misc/ACKS
   python/branches/py3k/Objects/bytearrayobject.c
   python/branches/py3k/Objects/bytesobject.c
   python/branches/py3k/Objects/unicodeobject.c

Modified: python/branches/py3k/Doc/library/stdtypes.rst
==============================================================================
--- python/branches/py3k/Doc/library/stdtypes.rst	(original)
+++ python/branches/py3k/Doc/library/stdtypes.rst	Fri Sep 18 23:42:35 2009
@@ -788,11 +788,10 @@
 
 .. 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 @@
    :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 @@
    :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:
 

Modified: python/branches/py3k/Lib/test/test_bytes.py
==============================================================================
--- python/branches/py3k/Lib/test/test_bytes.py	(original)
+++ python/branches/py3k/Lib/test/test_bytes.py	Fri Sep 18 23:42:35 2009
@@ -186,6 +186,8 @@
         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)

Modified: python/branches/py3k/Lib/test/test_unicode.py
==============================================================================
--- python/branches/py3k/Lib/test/test_unicode.py	(original)
+++ python/branches/py3k/Lib/test/test_unicode.py	Fri Sep 18 23:42:35 2009
@@ -955,6 +955,10 @@
         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')

Modified: python/branches/py3k/Misc/ACKS
==============================================================================
--- python/branches/py3k/Misc/ACKS	(original)
+++ python/branches/py3k/Misc/ACKS	Fri Sep 18 23:42:35 2009
@@ -87,6 +87,7 @@
 Eric Bouck
 Thierry Bousch
 Sebastian Boving
+Jeff Bradberry
 Monty Brandenberg
 Georg Brandl
 Christopher Brannon

Modified: python/branches/py3k/Objects/bytearrayobject.c
==============================================================================
--- python/branches/py3k/Objects/bytearrayobject.c	(original)
+++ python/branches/py3k/Objects/bytearrayobject.c	Fri Sep 18 23:42:35 2009
@@ -2877,12 +2877,13 @@
 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 @@
      _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__},

Modified: python/branches/py3k/Objects/bytesobject.c
==============================================================================
--- python/branches/py3k/Objects/bytesobject.c	(original)
+++ python/branches/py3k/Objects/bytesobject.c	Fri Sep 18 23:42:35 2009
@@ -2725,12 +2725,13 @@
 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 @@
 	 _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,

Modified: python/branches/py3k/Objects/unicodeobject.c
==============================================================================
--- python/branches/py3k/Objects/unicodeobject.c	(original)
+++ python/branches/py3k/Objects/unicodeobject.c	Fri Sep 18 23:42:35 2009
@@ -7141,13 +7141,15 @@
 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 @@
     /* 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 @@
     {"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__},


More information about the Python-checkins mailing list