Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60bc809

Browse files
committed
Marc-Andre Lemburg <[email protected]>:
Added code so that .isXXX() testing returns 0 for emtpy strings.
1 parent bddf502 commit 60bc809

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Objects/stringobject.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,10 @@ string_isspace(PyStringObject *self, PyObject *args)
19271927
isspace(*p))
19281928
return PyInt_FromLong(1);
19291929

1930+
/* Special case for empty strings */
1931+
if (PyString_GET_SIZE(self) == 0)
1932+
return PyInt_FromLong(0);
1933+
19301934
e = p + PyString_GET_SIZE(self);
19311935
for (; p < e; p++) {
19321936
if (!isspace(*p))
@@ -1956,6 +1960,10 @@ string_isdigit(PyStringObject *self, PyObject *args)
19561960
isdigit(*p))
19571961
return PyInt_FromLong(1);
19581962

1963+
/* Special case for empty strings */
1964+
if (PyString_GET_SIZE(self) == 0)
1965+
return PyInt_FromLong(0);
1966+
19591967
e = p + PyString_GET_SIZE(self);
19601968
for (; p < e; p++) {
19611969
if (!isdigit(*p))
@@ -1985,6 +1993,10 @@ string_islower(PyStringObject *self, PyObject *args)
19851993
if (PyString_GET_SIZE(self) == 1)
19861994
return PyInt_FromLong(islower(*p) != 0);
19871995

1996+
/* Special case for empty strings */
1997+
if (PyString_GET_SIZE(self) == 0)
1998+
return PyInt_FromLong(0);
1999+
19882000
e = p + PyString_GET_SIZE(self);
19892001
cased = 0;
19902002
for (; p < e; p++) {
@@ -2017,6 +2029,10 @@ string_isupper(PyStringObject *self, PyObject *args)
20172029
if (PyString_GET_SIZE(self) == 1)
20182030
return PyInt_FromLong(isupper(*p) != 0);
20192031

2032+
/* Special case for empty strings */
2033+
if (PyString_GET_SIZE(self) == 0)
2034+
return PyInt_FromLong(0);
2035+
20202036
e = p + PyString_GET_SIZE(self);
20212037
cased = 0;
20222038
for (; p < e; p++) {
@@ -2050,6 +2066,10 @@ string_istitle(PyStringObject *self, PyObject *args)
20502066
if (PyString_GET_SIZE(self) == 1)
20512067
return PyInt_FromLong(isupper(*p) != 0);
20522068

2069+
/* Special case for empty strings */
2070+
if (PyString_GET_SIZE(self) == 0)
2071+
return PyInt_FromLong(0);
2072+
20532073
e = p + PyString_GET_SIZE(self);
20542074
cased = 0;
20552075
previous_is_cased = 0;

Objects/unicodeobject.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3306,6 +3306,10 @@ unicode_islower(PyUnicodeObject *self, PyObject *args)
33063306
if (PyUnicode_GET_SIZE(self) == 1)
33073307
return PyInt_FromLong(Py_UNICODE_ISLOWER(*p) != 0);
33083308

3309+
/* Special case for empty strings */
3310+
if (PyString_GET_SIZE(self) == 0)
3311+
return PyInt_FromLong(0);
3312+
33093313
e = p + PyUnicode_GET_SIZE(self);
33103314
cased = 0;
33113315
for (; p < e; p++) {
@@ -3339,6 +3343,10 @@ unicode_isupper(PyUnicodeObject *self, PyObject *args)
33393343
if (PyUnicode_GET_SIZE(self) == 1)
33403344
return PyInt_FromLong(Py_UNICODE_ISUPPER(*p) != 0);
33413345

3346+
/* Special case for empty strings */
3347+
if (PyString_GET_SIZE(self) == 0)
3348+
return PyInt_FromLong(0);
3349+
33423350
e = p + PyUnicode_GET_SIZE(self);
33433351
cased = 0;
33443352
for (; p < e; p++) {
@@ -3374,6 +3382,10 @@ unicode_istitle(PyUnicodeObject *self, PyObject *args)
33743382
return PyInt_FromLong((Py_UNICODE_ISTITLE(*p) != 0) ||
33753383
(Py_UNICODE_ISUPPER(*p) != 0));
33763384

3385+
/* Special case for empty strings */
3386+
if (PyString_GET_SIZE(self) == 0)
3387+
return PyInt_FromLong(0);
3388+
33773389
e = p + PyUnicode_GET_SIZE(self);
33783390
cased = 0;
33793391
previous_is_cased = 0;
@@ -3418,6 +3430,10 @@ unicode_isspace(PyUnicodeObject *self, PyObject *args)
34183430
Py_UNICODE_ISSPACE(*p))
34193431
return PyInt_FromLong(1);
34203432

3433+
/* Special case for empty strings */
3434+
if (PyString_GET_SIZE(self) == 0)
3435+
return PyInt_FromLong(0);
3436+
34213437
e = p + PyUnicode_GET_SIZE(self);
34223438
for (; p < e; p++) {
34233439
if (!Py_UNICODE_ISSPACE(*p))
@@ -3446,6 +3462,10 @@ unicode_isdecimal(PyUnicodeObject *self, PyObject *args)
34463462
Py_UNICODE_ISDECIMAL(*p))
34473463
return PyInt_FromLong(1);
34483464

3465+
/* Special case for empty strings */
3466+
if (PyString_GET_SIZE(self) == 0)
3467+
return PyInt_FromLong(0);
3468+
34493469
e = p + PyUnicode_GET_SIZE(self);
34503470
for (; p < e; p++) {
34513471
if (!Py_UNICODE_ISDECIMAL(*p))
@@ -3474,6 +3494,10 @@ unicode_isdigit(PyUnicodeObject *self, PyObject *args)
34743494
Py_UNICODE_ISDIGIT(*p))
34753495
return PyInt_FromLong(1);
34763496

3497+
/* Special case for empty strings */
3498+
if (PyString_GET_SIZE(self) == 0)
3499+
return PyInt_FromLong(0);
3500+
34773501
e = p + PyUnicode_GET_SIZE(self);
34783502
for (; p < e; p++) {
34793503
if (!Py_UNICODE_ISDIGIT(*p))
@@ -3502,6 +3526,10 @@ unicode_isnumeric(PyUnicodeObject *self, PyObject *args)
35023526
Py_UNICODE_ISNUMERIC(*p))
35033527
return PyInt_FromLong(1);
35043528

3529+
/* Special case for empty strings */
3530+
if (PyString_GET_SIZE(self) == 0)
3531+
return PyInt_FromLong(0);
3532+
35053533
e = p + PyUnicode_GET_SIZE(self);
35063534
for (; p < e; p++) {
35073535
if (!Py_UNICODE_ISNUMERIC(*p))

0 commit comments

Comments
 (0)