Skip to content

Commit 21e0da2

Browse files
committed
remove some usage of Py_UNICODE_TOUPPER/LOWER
1 parent 22ef4fa commit 21e0da2

8 files changed

Lines changed: 21 additions & 27 deletions

File tree

Objects/stringlib/asciilib.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1616
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
1717
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
18-
#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER
19-
#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER
2018
#define STRINGLIB_STR PyUnicode_1BYTE_DATA
2119
#define STRINGLIB_LEN PyUnicode_GET_LENGTH
2220
#define STRINGLIB_NEW unicode_fromascii

Objects/stringlib/stringdefs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
1919
#define STRINGLIB_ISDECIMAL(x) ((x >= '0') && (x <= '9'))
2020
#define STRINGLIB_TODECIMAL(x) (STRINGLIB_ISDECIMAL(x) ? (x - '0') : -1)
21-
#define STRINGLIB_TOUPPER Py_TOUPPER
22-
#define STRINGLIB_TOLOWER Py_TOLOWER
2321
#define STRINGLIB_STR PyBytes_AS_STRING
2422
#define STRINGLIB_LEN PyBytes_GET_SIZE
2523
#define STRINGLIB_NEW PyBytes_FromStringAndSize

Objects/stringlib/ucs1lib.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1616
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
1717
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
18-
#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER
19-
#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER
2018
#define STRINGLIB_STR PyUnicode_1BYTE_DATA
2119
#define STRINGLIB_LEN PyUnicode_GET_LENGTH
2220
#define STRINGLIB_NEW _PyUnicode_FromUCS1

Objects/stringlib/ucs2lib.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1616
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
1717
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
18-
#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER
19-
#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER
2018
#define STRINGLIB_STR PyUnicode_2BYTE_DATA
2119
#define STRINGLIB_LEN PyUnicode_GET_LENGTH
2220
#define STRINGLIB_NEW _PyUnicode_FromUCS2

Objects/stringlib/ucs4lib.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1616
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
1717
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
18-
#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER
19-
#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER
2018
#define STRINGLIB_STR PyUnicode_4BYTE_DATA
2119
#define STRINGLIB_LEN PyUnicode_GET_LENGTH
2220
#define STRINGLIB_NEW _PyUnicode_FromUCS4

Objects/stringlib/unicodedefs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1919
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
2020
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
21-
#define STRINGLIB_TOUPPER Py_UNICODE_TOUPPER
22-
#define STRINGLIB_TOLOWER Py_UNICODE_TOLOWER
2321
#define STRINGLIB_STR PyUnicode_AS_UNICODE
2422
#define STRINGLIB_LEN PyUnicode_GET_SIZE
2523
#define STRINGLIB_NEW PyUnicode_FromUnicode

Python/_warnings.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,14 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
510510
kind = PyUnicode_KIND(*filename);
511511
data = PyUnicode_DATA(*filename);
512512

513+
#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
513514
/* if filename.lower().endswith((".pyc", ".pyo")): */
514515
if (len >= 4 &&
515516
PyUnicode_READ(kind, data, len-4) == '.' &&
516-
Py_UNICODE_TOLOWER(PyUnicode_READ(kind, data, len-3)) == 'p' &&
517-
Py_UNICODE_TOLOWER(PyUnicode_READ(kind, data, len-2)) == 'y' &&
518-
(Py_UNICODE_TOLOWER(PyUnicode_READ(kind, data, len-1)) == 'c' ||
519-
Py_UNICODE_TOLOWER(PyUnicode_READ(kind, data, len-1)) == 'o'))
517+
ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
518+
ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
519+
(ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c' ||
520+
ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'o'))
520521
{
521522
*filename = PyUnicode_Substring(*filename, 0,
522523
PyUnicode_GET_LENGTH(*filename)-1);

Python/formatter_unicode.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,14 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
561561
return -1;
562562
if (toupper) {
563563
Py_ssize_t t;
564-
/* XXX if the upper-case prefix is wider than the target
565-
buffer, the caller should have allocated a wider string,
566-
but currently doesn't. */
567-
for (t = 0; t < spec->n_prefix; ++t)
568-
PyUnicode_WRITE(kind, data, pos + t,
569-
Py_UNICODE_TOUPPER(
570-
PyUnicode_READ(kind, data, pos + t)));
564+
for (t = 0; t < spec->n_prefix; t++) {
565+
Py_UCS4 c = PyUnicode_READ(kind, data, pos + t);
566+
if (c > 127) {
567+
PyErr_SetString(PyExc_SystemError, "prefix not ASCII");
568+
return -1;
569+
}
570+
PyUnicode_WRITE(kind, data, pos + t, Py_TOUPPER(c));
571+
}
571572
}
572573
pos += spec->n_prefix;
573574
}
@@ -607,10 +608,14 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
607608
}
608609
if (toupper) {
609610
Py_ssize_t t;
610-
for (t = 0; t < spec->n_grouped_digits; ++t)
611-
PyUnicode_WRITE(kind, data, pos + t,
612-
Py_UNICODE_TOUPPER(
613-
PyUnicode_READ(kind, data, pos + t)));
611+
for (t = 0; t < spec->n_grouped_digits; t++) {
612+
Py_UCS4 c = PyUnicode_READ(kind, data, pos + t);
613+
if (c > 127) {
614+
PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
615+
return -1;
616+
}
617+
PyUnicode_WRITE(kind, data, pos + t, Py_TOUPPER(c));
618+
}
614619
}
615620
pos += spec->n_grouped_digits;
616621

0 commit comments

Comments
 (0)