cpython: db968ac2b82c (original) (raw)

Mercurial > cpython

changeset 75291:db968ac2b82c

#14081: The sep and maxsplit parameter to str.split, bytes.split, and bytearray.split may now be passed as keyword arguments. [#14081]

Ezio Melotti ezio.melotti@gmail.com
date Sun, 26 Feb 2012 09:39:55 +0200
parents be4ae700c877
children b299c4f31ff2
files Doc/library/stdtypes.rst Lib/test/string_tests.py Lib/test/test_bytes.py Misc/NEWS Objects/bytearrayobject.c Objects/bytesobject.c Objects/unicodeobject.c
diffstat 7 files changed, 82 insertions(+), 29 deletions(-)[+] [-] Doc/library/stdtypes.rst 4 Lib/test/string_tests.py 24 Lib/test/test_bytes.py 16 Misc/NEWS 3 Objects/bytearrayobject.c 20 Objects/bytesobject.c 20 Objects/unicodeobject.c 24

line wrap: on

line diff

--- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -1301,7 +1301,7 @@ functions based on regular expressions. two empty strings, followed by the string itself. -.. method:: str.rsplit([sep[, maxsplit]]) +.. method:: str.rsplit(sep=None, maxsplit=-1) Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost @@ -1323,7 +1323,7 @@ functions based on regular expressions. 'mississ' -.. method:: str.split([sep[, maxsplit]]) +.. method:: str.split(sep=None, maxsplit=-1) Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus,

--- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -56,7 +56,7 @@ class BaseTest(unittest.TestCase): result = self.fixtype(result) obj = self.fixtype(obj) args = self.fixtype(args)

@@ -389,6 +389,17 @@ class BaseTest(unittest.TestCase): self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4], 'split', 'BLAH', 18)

+ # argument type self.checkraises(TypeError, 'hello', 'split', 42, 42, 42) @@ -446,6 +457,17 @@ class BaseTest(unittest.TestCase): self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 18)

+ # argument type self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)

--- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -435,6 +435,14 @@ class BaseBytesTest(unittest.TestCase): self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi']) self.assertEqual(b.split(b'w'), [b])

def test_split_whitespace(self): for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', @@ -463,6 +471,14 @@ class BaseBytesTest(unittest.TestCase): self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi']) self.assertEqual(b.rsplit(b'w'), [b])

def test_rsplit_whitespace(self): for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -223,6 +223,9 @@ Core and Builtins

--- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2039,7 +2039,7 @@ bytearray_replace(PyByteArrayObject *sel } PyDoc_STRVAR(split__doc__, -"B.split([sep[, maxsplit]]) -> list of bytearrays\n[](#l5.7) +"B.split(sep=None, maxsplit=-1) -> list of bytearrays\n[](#l5.8) \n[](#l5.9) Return a list of the sections in B, using sep as the delimiter.\n[](#l5.10) If sep is not given, B is split on ASCII whitespace characters\n[](#l5.11) @@ -2047,15 +2047,17 @@ If sep is not given, B is split on ASCII If maxsplit is given, at most maxsplit splits are done."); static PyObject * -bytearray_split(PyByteArrayObject *self, PyObject *args) +bytearray_split(PyByteArrayObject *self, PyObject *args, PyObject *kwds) {

@@ -2131,7 +2133,7 @@ bytearray_rpartition(PyByteArrayObject * } PyDoc_STRVAR(rsplit__doc__, -"B.rsplit(sep[, maxsplit]) -> list of bytearrays\n[](#l5.36) +"B.rsplit(sep=None, maxsplit=-1) -> list of bytearrays\n[](#l5.37) \n[](#l5.38) Return a list of the sections in B, using sep as the delimiter,\n[](#l5.39) starting at the end of B and working to the front.\n[](#l5.40) @@ -2140,15 +2142,17 @@ If sep is not given, B is split on ASCII If maxsplit is given, at most maxsplit splits are done."); static PyObject * -bytearray_rsplit(PyByteArrayObject *self, PyObject *args) +bytearray_rsplit(PyByteArrayObject *self, PyObject *args, PyObject *kwds) {

@@ -2869,9 +2873,9 @@ bytearray_methods[] = { {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, rpartition__doc__},

--- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -972,7 +972,7 @@ static const char *stripformat[] = {"|O: #define STRIPNAME(i) (stripformat[i]+3) PyDoc_STRVAR(split__doc__, -"B.split([sep[, maxsplit]]) -> list of bytes\n[](#l6.7) +"B.split(sep=None, maxsplit=-1) -> list of bytes\n[](#l6.8) \n[](#l6.9) Return a list of the sections in B, using sep as the delimiter.\n[](#l6.10) If sep is not specified or is None, B is split on ASCII whitespace\n[](#l6.11) @@ -980,15 +980,17 @@ characters (space, tab, return, newline, If maxsplit is given, at most maxsplit splits are done."); static PyObject * -bytes_split(PyBytesObject *self, PyObject *args) +bytes_split(PyBytesObject *self, PyObject *args, PyObject *kwds) {

@@ -1060,7 +1062,7 @@ bytes_rpartition(PyBytesObject *self, Py } PyDoc_STRVAR(rsplit__doc__, -"B.rsplit([sep[, maxsplit]]) -> list of bytes\n[](#l6.36) +"B.rsplit(sep=None, maxsplit=-1) -> list of bytes\n[](#l6.37) \n[](#l6.38) Return a list of the sections in B, using sep as the delimiter,\n[](#l6.39) starting at the end of B and working to the front.\n[](#l6.40) @@ -1070,15 +1072,17 @@ If maxsplit is given, at most maxsplit s static PyObject * -bytes_rsplit(PyBytesObject *self, PyObject *args) +bytes_rsplit(PyBytesObject *self, PyObject *args, PyObject *kwds) {

@@ -2470,9 +2474,9 @@ bytes_methods[] = { {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, {"rpartition", (PyCFunction)bytes_rpartition, METH_O, rpartition__doc__},

--- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -12499,7 +12499,7 @@ PyUnicode_Split(PyObject *s, PyObject *s } PyDoc_STRVAR(split__doc__,

\n[](#l7.9) Return a list of the words in S, using sep as the\n[](#l7.10) delimiter string. If maxsplit is given, at most maxsplit\n[](#l7.11) @@ -12508,12 +12508,14 @@ whitespace string is a separator and emp removed from the result."); static PyObject* -unicode_split(PyObject *self, PyObject *args) -{ +unicode_split(PyObject *self, PyObject *args, PyObject *kwds) +{

if (substring == Py_None) @@ -12722,7 +12724,7 @@ PyUnicode_RSplit(PyObject *s, PyObject * } PyDoc_STRVAR(rsplit__doc__,

\n[](#l7.36) Return a list of the words in S, using sep as the\n[](#l7.37) delimiter string, starting at the end of the string and\n[](#l7.38) @@ -12731,12 +12733,14 @@ splits are done. If sep is not specified is a separator."); static PyObject* -unicode_rsplit(PyObject *self, PyObject *args) -{ +unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds) +{

if (substring == Py_None) @@ -13167,8 +13171,8 @@ static PyMethodDef unicode_methods[] = { {"encode", (PyCFunction) unicode_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, {"replace", (PyCFunction) unicode_replace, METH_VARARGS, replace__doc__},