summaryrefslogtreecommitdiffstats
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2002-04-22 17:42:37 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2002-04-22 17:42:37 (GMT)
commitde02bcb2659af6acb1d44ba61c0bcf7b2d53a6ed (patch)
treebcb24d906dccf9dfd69b2f1eb69c911d68cdba45 /Objects/stringobject.c
parenta7cc43b9e8b55223ad6b711488fbe8c10df6b5c2 (diff)
downloadcpython-de02bcb2659af6acb1d44ba61c0bcf7b2d53a6ed.zip
cpython-de02bcb2659af6acb1d44ba61c0bcf7b2d53a6ed.tar.gz
cpython-de02bcb2659af6acb1d44ba61c0bcf7b2d53a6ed.tar.bz2
Apply patch diff.txt from SF feature request
http://www.python.org/sf/444708 This adds the optional argument for str.strip to unicode.strip too and makes it possible to call str.strip with a unicode argument and unicode.strip with a str argument.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r--Objects/stringobject.c40
1 files changed, 28 insertions, 12 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 6a0eece..d3c9e4b 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -1005,7 +1005,9 @@ static PyBufferProcs string_as_buffer = {
#define BOTHSTRIP 2
/* Arrays indexed by above */
-static const char *stripname[] = {"lstrip", "rstrip", "strip"};
+static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"};
+
+#define STRIPNAME(i) (stripformat[i]+3)
static PyObject *
@@ -1449,15 +1451,26 @@ do_argstrip(PyStringObject *self, int striptype, PyObject *args)
{
PyObject *sep = NULL;
- if (!PyArg_ParseTuple(args, "|O:[lr]strip", &sep))
+ if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep))
return NULL;
if (sep != NULL && sep != Py_None) {
- /* XXX What about Unicode? */
- if (!PyString_Check(sep)) {
+ if (PyString_Check(sep))
+ return do_xstrip(self, striptype, sep);
+ else if (PyUnicode_Check(sep)) {
+ PyObject *uniself = PyUnicode_FromObject((PyObject *)self);
+ PyObject *res;
+ if (uniself==NULL)
+ return NULL;
+ res = _PyUnicode_XStrip((PyUnicodeObject *)uniself,
+ striptype, sep);
+ Py_DECREF(uniself);
+ return res;
+ }
+ else {
PyErr_Format(PyExc_TypeError,
- "%s arg must be None or string",
- stripname[striptype]);
+ "%s arg must be None, str or unicode",
+ STRIPNAME(striptype));
return NULL;
}
return do_xstrip(self, striptype, sep);
@@ -1468,11 +1481,12 @@ do_argstrip(PyStringObject *self, int striptype, PyObject *args)
static char strip__doc__[] =
-"S.strip([sep]) -> string\n\
+"S.strip([sep]) -> string or unicode\n\
\n\
Return a copy of the string S with leading and trailing\n\
whitespace removed.\n\
-If sep is given and not None, remove characters in sep instead.";
+If sep is given and not None, remove characters in sep instead.\n\
+If sep is unicode, S will be converted to unicode before stripping";
static PyObject *
string_strip(PyStringObject *self, PyObject *args)
@@ -1485,10 +1499,11 @@ string_strip(PyStringObject *self, PyObject *args)
static char lstrip__doc__[] =
-"S.lstrip([sep]) -> string\n\
+"S.lstrip([sep]) -> string or unicode\n\
\n\
Return a copy of the string S with leading whitespace removed.\n\
-If sep is given and not None, remove characters in sep instead.";
+If sep is given and not None, remove characters in sep instead.\n\
+If sep is unicode, S will be converted to unicode before stripping";
static PyObject *
string_lstrip(PyStringObject *self, PyObject *args)
@@ -1501,10 +1516,11 @@ string_lstrip(PyStringObject *self, PyObject *args)
static char rstrip__doc__[] =
-"S.rstrip([sep]) -> string\n\
+"S.rstrip([sep]) -> string or unicode\n\
\n\
Return a copy of the string S with trailing whitespace removed.\n\
-If sep is given and not None, remove characters in sep instead.";
+If sep is given and not None, remove characters in sep instead.\n\
+If sep is unicode, S will be converted to unicode before stripping";
static PyObject *
string_rstrip(PyStringObject *self, PyObject *args)