summaryrefslogtreecommitdiffstats
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2002-04-22 18:42:45 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2002-04-22 18:42:45 (GMT)
commitaa82bc0817eaebd31ddf00b4fc31eca2db2b7333 (patch)
tree2f55384f1b2aa85ac22f350330f5e679d3d3040f /Objects/stringobject.c
parent1c097b7102c6c5a1a03cbb5d36c819eedae60d5a (diff)
downloadcpython-aa82bc0817eaebd31ddf00b4fc31eca2db2b7333.zip
cpython-aa82bc0817eaebd31ddf00b4fc31eca2db2b7333.tar.gz
cpython-aa82bc0817eaebd31ddf00b4fc31eca2db2b7333.tar.bz2
Backport checkin:
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 feb5947..b4f8202 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -1037,7 +1037,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 *
@@ -1481,15 +1483,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);
@@ -1500,11 +1513,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)
@@ -1517,10 +1531,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)
@@ -1533,10 +1548,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)