Skip to content

Commit

Permalink
pythongh-108767: Replace ctype.h functions with pyctype.h functions (p…
Browse files Browse the repository at this point in the history
…ython#108772)

Replace <ctype.h> locale dependent functions with Python "pyctype.h"
locale independent functions:

* Replace isalpha() with Py_ISALPHA().
* Replace isdigit() with Py_ISDIGIT().
* Replace isxdigit() with Py_ISXDIGIT().
* Replace tolower() with Py_TOLOWER().

Leave Modules/_sre/sre.c unchanged, it uses locale dependent
functions on purpose.

Include explicitly <ctype.h> in _decimal.c to get isascii().
  • Loading branch information
vstinner authored Sep 1, 2023
1 parent 03c5a68 commit 578ebc5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
1 change: 1 addition & 0 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "complexobject.h"
#include "mpdecimal.h"

#include <ctype.h> // isascii()
#include <stdlib.h>

#include "docstrings.h"
Expand Down
14 changes: 7 additions & 7 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ parse_tz_str(zoneinfo_state *state, PyObject *tz_str_obj, _tzrule *out)
static int
parse_uint(const char *const p, uint8_t *value)
{
if (!isdigit(*p)) {
if (!Py_ISDIGIT(*p)) {
return -1;
}

Expand Down Expand Up @@ -1732,7 +1732,7 @@ parse_abbr(const char *const p, PyObject **abbr)
// '+' ) character, or the minus-sign ( '-' ) character. The std
// and dst fields in this case shall not include the quoting
// characters.
if (!isalpha(buff) && !isdigit(buff) && buff != '+' &&
if (!Py_ISALPHA(buff) && !Py_ISDIGIT(buff) && buff != '+' &&
buff != '-') {
return -1;
}
Expand All @@ -1748,7 +1748,7 @@ parse_abbr(const char *const p, PyObject **abbr)
// In the unquoted form, all characters in these fields shall be
// alphabetic characters from the portable character set in the
// current locale.
while (isalpha(*ptr)) {
while (Py_ISALPHA(*ptr)) {
ptr++;
}
str_end = ptr;
Expand Down Expand Up @@ -1802,7 +1802,7 @@ parse_tz_delta(const char *const p, long *total_seconds)
// The hour can be 1 or 2 numeric characters
for (size_t i = 0; i < 2; ++i) {
buff = *ptr;
if (!isdigit(buff)) {
if (!Py_ISDIGIT(buff)) {
if (i == 0) {
return -1;
}
Expand Down Expand Up @@ -1830,7 +1830,7 @@ parse_tz_delta(const char *const p, long *total_seconds)

for (size_t j = 0; j < 2; ++j) {
buff = *ptr;
if (!isdigit(buff)) {
if (!Py_ISDIGIT(buff)) {
return -1;
}
*(outputs[i]) *= 10;
Expand Down Expand Up @@ -1932,7 +1932,7 @@ parse_transition_rule(const char *const p, TransitionRuleType **out)
}

for (size_t i = 0; i < 3; ++i) {
if (!isdigit(*ptr)) {
if (!Py_ISDIGIT(*ptr)) {
if (i == 0) {
return -1;
}
Expand Down Expand Up @@ -2007,7 +2007,7 @@ parse_transition_time(const char *const p, int8_t *hour, int8_t *minute,

uint8_t buff = 0;
for (size_t j = 0; j < 2; j++) {
if (!isdigit(*ptr)) {
if (!Py_ISDIGIT(*ptr)) {
if (i == 0 && j > 0) {
break;
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/getaddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <unistd.h>

#include "addrinfo.h"
Expand Down Expand Up @@ -228,8 +227,9 @@ str_isnumber(const char *p)
{
unsigned char *q = (unsigned char *)p;
while (*q) {
if (! isdigit(*q))
if (!Py_ISDIGIT(*q)) {
return NO;
}
q++;
}
return YES;
Expand Down
8 changes: 4 additions & 4 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,11 +722,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
if (--fmtcnt >= 0)
c = *fmt++;
}
else if (c >= 0 && isdigit(c)) {
else if (c >= 0 && Py_ISDIGIT(c)) {
width = c - '0';
while (--fmtcnt >= 0) {
c = Py_CHARMASK(*fmt++);
if (!isdigit(c))
if (!Py_ISDIGIT(c))
break;
if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) {
PyErr_SetString(
Expand Down Expand Up @@ -761,11 +761,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
if (--fmtcnt >= 0)
c = *fmt++;
}
else if (c >= 0 && isdigit(c)) {
else if (c >= 0 && Py_ISDIGIT(c)) {
prec = c - '0';
while (--fmtcnt >= 0) {
c = Py_CHARMASK(*fmt++);
if (!isdigit(c))
if (!Py_ISDIGIT(c))
break;
if (prec > (INT_MAX - ((int)c - '0')) / 10) {
PyErr_SetString(
Expand Down
37 changes: 18 additions & 19 deletions Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()

#include <ctype.h>
#include <assert.h>

#include "tokenizer.h"
Expand Down Expand Up @@ -158,7 +157,7 @@ get_normal_name(const char *s) /* for utf-8 and latin-1 */
else if (c == '_')
buf[i] = '-';
else
buf[i] = tolower(c);
buf[i] = Py_TOLOWER(c);
}
buf[i] = '\0';
if (strcmp(buf, "utf-8") == 0 ||
Expand Down Expand Up @@ -1715,12 +1714,12 @@ tok_decimal_tail(struct tok_state *tok)
while (1) {
do {
c = tok_nextc(tok);
} while (isdigit(c));
} while (Py_ISDIGIT(c));
if (c != '_') {
break;
}
c = tok_nextc(tok);
if (!isdigit(c)) {
if (!Py_ISDIGIT(c)) {
tok_backup(tok, c);
syntaxerror(tok, "invalid decimal literal");
return 0;
Expand Down Expand Up @@ -2108,7 +2107,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
/* Period or number starting with period? */
if (c == '.') {
c = tok_nextc(tok);
if (isdigit(c)) {
if (Py_ISDIGIT(c)) {
goto fraction;
} else if (c == '.') {
c = tok_nextc(tok);
Expand All @@ -2131,7 +2130,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
}

/* Number */
if (isdigit(c)) {
if (Py_ISDIGIT(c)) {
if (c == '0') {
/* Hex, octal or binary -- maybe. */
c = tok_nextc(tok);
Expand All @@ -2142,13 +2141,13 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
if (c == '_') {
c = tok_nextc(tok);
}
if (!isxdigit(c)) {
if (!Py_ISXDIGIT(c)) {
tok_backup(tok, c);
return MAKE_TOKEN(syntaxerror(tok, "invalid hexadecimal literal"));
}
do {
c = tok_nextc(tok);
} while (isxdigit(c));
} while (Py_ISXDIGIT(c));
} while (c == '_');
if (!verify_end_of_number(tok, c, "hexadecimal")) {
return MAKE_TOKEN(ERRORTOKEN);
Expand All @@ -2162,7 +2161,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
c = tok_nextc(tok);
}
if (c < '0' || c >= '8') {
if (isdigit(c)) {
if (Py_ISDIGIT(c)) {
return MAKE_TOKEN(syntaxerror(tok,
"invalid digit '%c' in octal literal", c));
}
Expand All @@ -2175,7 +2174,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
c = tok_nextc(tok);
} while ('0' <= c && c < '8');
} while (c == '_');
if (isdigit(c)) {
if (Py_ISDIGIT(c)) {
return MAKE_TOKEN(syntaxerror(tok,
"invalid digit '%c' in octal literal", c));
}
Expand All @@ -2191,7 +2190,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
c = tok_nextc(tok);
}
if (c != '0' && c != '1') {
if (isdigit(c)) {
if (Py_ISDIGIT(c)) {
return MAKE_TOKEN(syntaxerror(tok, "invalid digit '%c' in binary literal", c));
}
else {
Expand All @@ -2203,7 +2202,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
c = tok_nextc(tok);
} while (c == '0' || c == '1');
} while (c == '_');
if (isdigit(c)) {
if (Py_ISDIGIT(c)) {
return MAKE_TOKEN(syntaxerror(tok, "invalid digit '%c' in binary literal", c));
}
if (!verify_end_of_number(tok, c, "binary")) {
Expand All @@ -2217,7 +2216,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
while (1) {
if (c == '_') {
c = tok_nextc(tok);
if (!isdigit(c)) {
if (!Py_ISDIGIT(c)) {
tok_backup(tok, c);
return MAKE_TOKEN(syntaxerror(tok, "invalid decimal literal"));
}
Expand All @@ -2228,7 +2227,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
c = tok_nextc(tok);
}
char* zeros_end = tok->cur;
if (isdigit(c)) {
if (Py_ISDIGIT(c)) {
nonzero = 1;
c = tok_decimal_tail(tok);
if (c == 0) {
Expand Down Expand Up @@ -2272,7 +2271,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
c = tok_nextc(tok);
fraction:
/* Fraction */
if (isdigit(c)) {
if (Py_ISDIGIT(c)) {
c = tok_decimal_tail(tok);
if (c == 0) {
return MAKE_TOKEN(ERRORTOKEN);
Expand All @@ -2287,11 +2286,11 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
c = tok_nextc(tok);
if (c == '+' || c == '-') {
c = tok_nextc(tok);
if (!isdigit(c)) {
if (!Py_ISDIGIT(c)) {
tok_backup(tok, c);
return MAKE_TOKEN(syntaxerror(tok, "invalid decimal literal"));
}
} else if (!isdigit(c)) {
} else if (!Py_ISDIGIT(c)) {
tok_backup(tok, c);
if (!verify_end_of_number(tok, e, "decimal")) {
return MAKE_TOKEN(ERRORTOKEN);
Expand Down Expand Up @@ -2326,7 +2325,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
}

f_string_quote:
if (((tolower(*tok->start) == 'f' || tolower(*tok->start) == 'r') && (c == '\'' || c == '"'))) {
if (((Py_TOLOWER(*tok->start) == 'f' || Py_TOLOWER(*tok->start) == 'r') && (c == '\'' || c == '"'))) {
int quote = c;
int quote_size = 1; /* 1 or 3 */

Expand Down Expand Up @@ -2377,7 +2376,7 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
switch (*tok->start) {
case 'F':
case 'f':
the_current_tok->f_string_raw = tolower(*(tok->start + 1)) == 'r';
the_current_tok->f_string_raw = Py_TOLOWER(*(tok->start + 1)) == 'r';
break;
case 'R':
case 'r':
Expand Down
8 changes: 4 additions & 4 deletions Python/pystrcmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ PyOS_mystrnicmp(const char *s1, const char *s2, Py_ssize_t size)
return 0;
p1 = (const unsigned char *)s1;
p2 = (const unsigned char *)s2;
for (; (--size > 0) && *p1 && *p2 && (tolower(*p1) == tolower(*p2));
for (; (--size > 0) && *p1 && *p2 && (Py_TOLOWER(*p1) == Py_TOLOWER(*p2));
p1++, p2++) {
;
}
return tolower(*p1) - tolower(*p2);
return Py_TOLOWER(*p1) - Py_TOLOWER(*p2);
}

int
PyOS_mystricmp(const char *s1, const char *s2)
{
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;
for (; *p1 && *p2 && (tolower(*p1) == tolower(*p2)); p1++, p2++) {
for (; *p1 && *p2 && (Py_TOLOWER(*p1) == Py_TOLOWER(*p2)); p1++, p2++) {
;
}
return (tolower(*p1) - tolower(*p2));
return (Py_TOLOWER(*p1) - Py_TOLOWER(*p2));
}

0 comments on commit 578ebc5

Please sign in to comment.