Skip to content

Commit bcbd562

Browse files
committed
Revert r61969 which added casts to Py_CHARMASK to avoid compiler warnings.
Rather than sprinkle casts throughout the code, change Py_CHARMASK to always cast it's result to an unsigned char. This should ensure we do the right thing when accessing an array with the result.
1 parent c382ea1 commit bcbd562

6 files changed

Lines changed: 23 additions & 31 deletions

File tree

Include/Python.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ PyAPI_FUNC(PyObject*) _Py_Mangle(PyObject *p, PyObject *name);
148148
#ifdef __CHAR_UNSIGNED__
149149
#define Py_CHARMASK(c) (c)
150150
#else
151-
#define Py_CHARMASK(c) ((c) & 0xff)
151+
#define Py_CHARMASK(c) ((unsigned char)((c) & 0xff))
152152
#endif
153153

154154
#include "pyfpe.h"

Include/bytes_methods.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ extern const char _Py_swapcase__doc__[];
4444

4545
extern const unsigned int _Py_ctype_table[256];
4646

47-
#define ISLOWER(c) (_Py_ctype_table[(unsigned)Py_CHARMASK(c)] & FLAG_LOWER)
48-
#define ISUPPER(c) (_Py_ctype_table[(unsigned)Py_CHARMASK(c)] & FLAG_UPPER)
49-
#define ISALPHA(c) (_Py_ctype_table[(unsigned)Py_CHARMASK(c)] & FLAG_ALPHA)
50-
#define ISDIGIT(c) (_Py_ctype_table[(unsigned)Py_CHARMASK(c)] & FLAG_DIGIT)
51-
#define ISXDIGIT(c) (_Py_ctype_table[(unsigned)Py_CHARMASK(c)] & FLAG_XDIGIT)
52-
#define ISALNUM(c) (_Py_ctype_table[(unsigned)Py_CHARMASK(c)] & FLAG_ALNUM)
53-
#define ISSPACE(c) (_Py_ctype_table[(unsigned)Py_CHARMASK(c)] & FLAG_SPACE)
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)
5454

5555
#undef islower
5656
#define islower(c) undefined_islower(c)

Objects/longobject.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ long_from_binary_base(char **str, int base)
13971397
n >>= 1;
13981398
/* n <- total # of bits needed, while setting p to end-of-string */
13991399
n = 0;
1400-
while (_PyLong_DigitValue[(unsigned)Py_CHARMASK(*p)] < base)
1400+
while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
14011401
++p;
14021402
*str = p;
14031403
/* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
@@ -1418,7 +1418,7 @@ long_from_binary_base(char **str, int base)
14181418
bits_in_accum = 0;
14191419
pdigit = z->ob_digit;
14201420
while (--p >= start) {
1421-
int k = _PyLong_DigitValue[(unsigned)Py_CHARMASK(*p)];
1421+
int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
14221422
assert(k >= 0 && k < base);
14231423
accum |= (twodigits)(k << bits_in_accum);
14241424
bits_in_accum += bits_per_char;
@@ -1609,7 +1609,7 @@ digit beyond the first.
16091609

16101610
/* Find length of the string of numeric characters. */
16111611
scan = str;
1612-
while (_PyLong_DigitValue[(unsigned)Py_CHARMASK(*scan)] < base)
1612+
while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
16131613
++scan;
16141614

16151615
/* Create a long object that can contain the largest possible
@@ -1635,10 +1635,10 @@ digit beyond the first.
16351635
/* Work ;-) */
16361636
while (str < scan) {
16371637
/* grab up to convwidth digits from the input string */
1638-
c = (digit)_PyLong_DigitValue[(unsigned)Py_CHARMASK(*str++)];
1638+
c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
16391639
for (i = 1; i < convwidth && str != scan; ++i, ++str) {
16401640
c = (twodigits)(c * base +
1641-
_PyLong_DigitValue[(unsigned)Py_CHARMASK(*str)]);
1641+
_PyLong_DigitValue[Py_CHARMASK(*str)]);
16421642
assert(c < PyLong_BASE);
16431643
}
16441644

Objects/unicodeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,13 @@ PyObject *PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
480480
/* Single characters are shared when using this constructor.
481481
Restrict to ASCII, since the input must be UTF-8. */
482482
if (size == 1 && Py_CHARMASK(*u) < 128) {
483-
unicode = unicode_latin1[(unsigned)Py_CHARMASK(*u)];
483+
unicode = unicode_latin1[Py_CHARMASK(*u)];
484484
if (!unicode) {
485485
unicode = _PyUnicode_New(1);
486486
if (!unicode)
487487
return NULL;
488488
unicode->str[0] = Py_CHARMASK(*u);
489-
unicode_latin1[(unsigned)Py_CHARMASK(*u)] = unicode;
489+
unicode_latin1[Py_CHARMASK(*u)] = unicode;
490490
}
491491
Py_INCREF(unicode);
492492
return (PyObject *)unicode;

Parser/tokenizer.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ extern char *PyOS_Readline(FILE *, FILE *, char *);
2727
/* Don't ever change this -- it would break the portability of Python code */
2828
#define TABSIZE 8
2929

30-
/* Convert a possibly signed character to a nonnegative int */
31-
/* XXX This assumes characters are 8 bits wide */
32-
#ifdef __CHAR_UNSIGNED__
33-
#define Py_CHARMASK(c) (c)
34-
#else
35-
#define Py_CHARMASK(c) ((c) & 0xff)
36-
#endif
37-
3830
/* Forward */
3931
static struct tok_state *tok_new(void);
4032
static int tok_nextc(struct tok_state *tok);

Python/mystrtoul.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ PyOS_strtoul(register char *str, char **ptr, int base)
109109
++str;
110110
if (*str == 'x' || *str == 'X') {
111111
/* there must be at least one digit after 0x */
112-
if (_PyLong_DigitValue[(unsigned)Py_CHARMASK(str[1])] >= 16) {
112+
if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
113113
if (ptr)
114114
*ptr = str;
115115
return 0;
@@ -118,7 +118,7 @@ PyOS_strtoul(register char *str, char **ptr, int base)
118118
base = 16;
119119
} else if (*str == 'o' || *str == 'O') {
120120
/* there must be at least one digit after 0o */
121-
if (_PyLong_DigitValue[(unsigned)Py_CHARMASK(str[1])] >= 8) {
121+
if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
122122
if (ptr)
123123
*ptr = str;
124124
return 0;
@@ -127,7 +127,7 @@ PyOS_strtoul(register char *str, char **ptr, int base)
127127
base = 8;
128128
} else if (*str == 'b' || *str == 'B') {
129129
/* there must be at least one digit after 0b */
130-
if (_PyLong_DigitValue[(unsigned)Py_CHARMASK(str[1])] >= 2) {
130+
if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
131131
if (ptr)
132132
*ptr = str;
133133
return 0;
@@ -147,7 +147,7 @@ PyOS_strtoul(register char *str, char **ptr, int base)
147147
++str;
148148
if (*str == 'b' || *str == 'B') {
149149
/* there must be at least one digit after 0b */
150-
if (_PyLong_DigitValue[(unsigned)Py_CHARMASK(str[1])] >= 2) {
150+
if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 2) {
151151
if (ptr)
152152
*ptr = str;
153153
return 0;
@@ -162,7 +162,7 @@ PyOS_strtoul(register char *str, char **ptr, int base)
162162
++str;
163163
if (*str == 'o' || *str == 'O') {
164164
/* there must be at least one digit after 0o */
165-
if (_PyLong_DigitValue[(unsigned)Py_CHARMASK(str[1])] >= 8) {
165+
if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 8) {
166166
if (ptr)
167167
*ptr = str;
168168
return 0;
@@ -177,7 +177,7 @@ PyOS_strtoul(register char *str, char **ptr, int base)
177177
++str;
178178
if (*str == 'x' || *str == 'X') {
179179
/* there must be at least one digit after 0x */
180-
if (_PyLong_DigitValue[(unsigned)Py_CHARMASK(str[1])] >= 16) {
180+
if (_PyLong_DigitValue[Py_CHARMASK(str[1])] >= 16) {
181181
if (ptr)
182182
*ptr = str;
183183
return 0;
@@ -203,7 +203,7 @@ PyOS_strtoul(register char *str, char **ptr, int base)
203203
ovlimit = digitlimit[base];
204204

205205
/* do the conversion until non-digit character encountered */
206-
while ((c = _PyLong_DigitValue[(unsigned)Py_CHARMASK(*str)]) < base) {
206+
while ((c = _PyLong_DigitValue[Py_CHARMASK(*str)]) < base) {
207207
if (ovlimit > 0) /* no overflow check required */
208208
result = result * base + c;
209209
else { /* requires overflow check */
@@ -240,7 +240,7 @@ PyOS_strtoul(register char *str, char **ptr, int base)
240240
overflowed:
241241
if (ptr) {
242242
/* spool through remaining digit characters */
243-
while (_PyLong_DigitValue[(unsigned)Py_CHARMASK(*str)] < base)
243+
while (_PyLong_DigitValue[Py_CHARMASK(*str)] < base)
244244
++str;
245245
*ptr = str;
246246
}

0 commit comments

Comments
 (0)