Skip to content

Commit cac7af6

Browse files
committed
Issue #5793: rationalize isdigit / isalpha / tolower, etc. Will port to py3k. Should fix Windows buildbot errors.
1 parent ec047e0 commit cac7af6

File tree

15 files changed

+338
-303
lines changed

15 files changed

+338
-303
lines changed

Include/Python.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
#include "compile.h"
135135
#include "eval.h"
136136

137+
#include "pyctype.h"
137138
#include "pystrtod.h"
138139
#include "pystrcmp.h"
139140

Include/bytes_methods.h

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,15 @@ extern const char _Py_title__doc__[];
3434
extern const char _Py_capitalize__doc__[];
3535
extern const char _Py_swapcase__doc__[];
3636

37-
#define FLAG_LOWER 0x01
38-
#define FLAG_UPPER 0x02
39-
#define FLAG_ALPHA (FLAG_LOWER|FLAG_UPPER)
40-
#define FLAG_DIGIT 0x04
41-
#define FLAG_ALNUM (FLAG_ALPHA|FLAG_DIGIT)
42-
#define FLAG_SPACE 0x08
43-
#define FLAG_XDIGIT 0x10
44-
45-
extern const unsigned int _Py_ctype_table[256];
46-
47-
#define ISLOWER(c) (_Py_ctype_table[Py_CHARMASK(c)] & FLAG_LOWER)
48-
#define ISUPPER(c) (_Py_ctype_table[Py_CHARMASK(c)] & FLAG_UPPER)
49-
#define ISALPHA(c) (_Py_ctype_table[Py_CHARMASK(c)] & FLAG_ALPHA)
50-
#define ISDIGIT(c) (_Py_ctype_table[Py_CHARMASK(c)] & FLAG_DIGIT)
51-
#define ISXDIGIT(c) (_Py_ctype_table[Py_CHARMASK(c)] & FLAG_XDIGIT)
52-
#define ISALNUM(c) (_Py_ctype_table[Py_CHARMASK(c)] & FLAG_ALNUM)
53-
#define ISSPACE(c) (_Py_ctype_table[Py_CHARMASK(c)] & FLAG_SPACE)
37+
/* These are left in for backward compatibility and will be removed
38+
in 2.8/3.2 */
39+
#define ISLOWER(c) Py_ISLOWER(c)
40+
#define ISUPPER(c) Py_ISUPPER(c)
41+
#define ISALPHA(c) Py_ISALPHA(c)
42+
#define ISDIGIT(c) Py_ISDIGIT(c)
43+
#define ISXDIGIT(c) Py_ISXDIGIT(c)
44+
#define ISALNUM(c) Py_ISALNUM(c)
45+
#define ISSPACE(c) Py_ISSPACE(c)
5446

5547
#undef islower
5648
#define islower(c) undefined_islower(c)
@@ -67,11 +59,10 @@ extern const unsigned int _Py_ctype_table[256];
6759
#undef isspace
6860
#define isspace(c) undefined_isspace(c)
6961

70-
extern const unsigned char _Py_ctype_tolower[256];
71-
extern const unsigned char _Py_ctype_toupper[256];
72-
73-
#define TOLOWER(c) (_Py_ctype_tolower[Py_CHARMASK(c)])
74-
#define TOUPPER(c) (_Py_ctype_toupper[Py_CHARMASK(c)])
62+
/* These are left in for backward compatibility and will be removed
63+
in 2.8/3.2 */
64+
#define TOLOWER(c) Py_TOLOWER(c)
65+
#define TOUPPER(c) Py_TOUPPER(c)
7566

7667
#undef tolower
7768
#define tolower(c) undefined_tolower(c)

Include/pyctype.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef PYCTYPE_H
2+
#define PYCTYPE_H
3+
4+
#define PY_CTF_LOWER 0x01
5+
#define PY_CTF_UPPER 0x02
6+
#define PY_CTF_ALPHA (PY_CTF_LOWER|PY_CTF_UPPER)
7+
#define PY_CTF_DIGIT 0x04
8+
#define PY_CTF_ALNUM (PY_CTF_ALPHA|PY_CTF_DIGIT)
9+
#define PY_CTF_SPACE 0x08
10+
#define PY_CTF_XDIGIT 0x10
11+
12+
extern const unsigned int _Py_ctype_table[256];
13+
14+
#define Py_ISLOWER(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_LOWER)
15+
#define Py_ISUPPER(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_UPPER)
16+
#define Py_ISALPHA(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_ALPHA)
17+
#define Py_ISDIGIT(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_DIGIT)
18+
#define Py_ISXDIGIT(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_XDIGIT)
19+
#define Py_ISALNUM(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_ALNUM)
20+
#define Py_ISSPACE(c) (_Py_ctype_table[Py_CHARMASK(c)] & PY_CTF_SPACE)
21+
22+
extern const unsigned char _Py_ctype_tolower[256];
23+
extern const unsigned char _Py_ctype_toupper[256];
24+
25+
#define Py_TOLOWER(c) (_Py_ctype_tolower[Py_CHARMASK(c)])
26+
#define Py_TOUPPER(c) (_Py_ctype_toupper[Py_CHARMASK(c)])
27+
28+
#endif /* !PYCTYPE_H */

Makefile.pre.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ PYTHON_OBJS= \
275275
Python/mysnprintf.o \
276276
Python/peephole.o \
277277
Python/pyarena.o \
278+
Python/pyctype.o \
278279
Python/pyfpe.o \
279280
Python/pymath.o \
280281
Python/pystate.o \
@@ -633,6 +634,7 @@ PYTHON_HEADERS= \
633634
Include/pgen.h \
634635
Include/pgenheaders.h \
635636
Include/pyarena.h \
637+
Include/pyctype.h \
636638
Include/pydebug.h \
637639
Include/pyerrors.h \
638640
Include/pyfpe.h \

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 1
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #5793: Rationalize isdigit / isalpha / tolower, etc. Includes
16+
new Py_ISDIGIT / Py_ISALPHA / Py_TOLOWER, etc. in pctypes.h.
17+
1518
- Issue #4971: Fix titlecase for characters that are their own
1619
titlecase, but not their own uppercase.
1720

Objects/bytearrayobject.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,16 +2244,16 @@ split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount)
22442244

22452245
for (i = j = 0; i < len; ) {
22462246
/* find a token */
2247-
while (i < len && ISSPACE(s[i]))
2247+
while (i < len && Py_ISSPACE(s[i]))
22482248
i++;
22492249
j = i;
2250-
while (i < len && !ISSPACE(s[i]))
2250+
while (i < len && !Py_ISSPACE(s[i]))
22512251
i++;
22522252
if (j < i) {
22532253
if (maxcount-- <= 0)
22542254
break;
22552255
SPLIT_ADD(s, j, i);
2256-
while (i < len && ISSPACE(s[i]))
2256+
while (i < len && Py_ISSPACE(s[i]))
22572257
i++;
22582258
j = i;
22592259
}
@@ -2478,16 +2478,16 @@ rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount)
24782478

24792479
for (i = j = len - 1; i >= 0; ) {
24802480
/* find a token */
2481-
while (i >= 0 && ISSPACE(s[i]))
2481+
while (i >= 0 && Py_ISSPACE(s[i]))
24822482
i--;
24832483
j = i;
2484-
while (i >= 0 && !ISSPACE(s[i]))
2484+
while (i >= 0 && !Py_ISSPACE(s[i]))
24852485
i--;
24862486
if (j > i) {
24872487
if (maxcount-- <= 0)
24882488
break;
24892489
SPLIT_ADD(s, i + 1, j + 1);
2490-
while (i >= 0 && ISSPACE(s[i]))
2490+
while (i >= 0 && Py_ISSPACE(s[i]))
24912491
i--;
24922492
j = i;
24932493
}
@@ -3054,11 +3054,11 @@ Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
30543054
static int
30553055
hex_digit_to_int(char c)
30563056
{
3057-
if (ISDIGIT(c))
3057+
if (Py_ISDIGIT(c))
30583058
return c - '0';
30593059
else {
3060-
if (ISUPPER(c))
3061-
c = TOLOWER(c);
3060+
if (Py_ISUPPER(c))
3061+
c = Py_TOLOWER(c);
30623062
if (c >= 'a' && c <= 'f')
30633063
return c - 'a' + 10;
30643064
}

0 commit comments

Comments
 (0)