Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit729b785

Browse files
committed
gh-108765: Python.h no longer includes <ctype.h>
Remove <ctype.h> in C files which don't use it; only sre.c and_decimal.c still use it.Remove _PY_PORT_CTYPE_UTF8_ISSUE code from pyport.h:* Code added by commitb5047fd in 2004 for MacOSX and FreeBSD.* Test removed by commit52ddaef in 2007, since Python str type now uses locale independent functions like Py_ISALPHA() and Py_TOLOWER() and the Unicode database.Modules/_sre/sre.c replaces _PY_PORT_CTYPE_UTF8_ISSUE with newfunctions:* sre_isalnum()* sre_tolower()* sre_toupper()Remove unused includes:* _localemodule.c: remove <stdio.h>.* getargs.c: remove <float.h>.* dynload_win.c: remove <direct.h>, it no longer calls _getcwd() since commitfb1f68e (in 2001).
1 parentf373c6b commit729b785

22 files changed

+86
-114
lines changed

‎Doc/whatsnew/3.13.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,13 @@ Porting to Python 3.13
942942
and ``setitimer()`` functions.
943943
(Contributed by Victor Stinner in:gh:`108765`.)
944944

945+
* ``Python.h`` no longer includes the ``<ctype.h>`` standard header file. If
946+
needed, it should now be included explicitly. For example, it provides
947+
``isalpha()`` and ``tolower()`` functions which are locale dependent. Python
948+
provides locale independent functions, like:c:func:`!Py_ISALPHA` and
949+
:c:func:`!Py_TOLOWER`.
950+
(Contributed by Victor Stinner in:gh:`108765`.)
951+
945952
Deprecated
946953
----------
947954

‎Include/Python.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
// Include standard header files
1919
#include<assert.h>// assert()
20-
#include<ctype.h>// tolower()
2120
#include<inttypes.h>// uintptr_t
2221
#include<limits.h>// INT_MAX
2322
#include<math.h>// HUGE_VAL

‎Include/pyport.h

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -392,44 +392,6 @@ extern "C" {
392392
#definePy_NO_INLINE
393393
#endif
394394

395-
/* On 4.4BSD-descendants, ctype functions serves the whole range of
396-
* wchar_t character set rather than single byte code points only.
397-
* This characteristic can break some operations of string object
398-
* including str.upper() and str.split() on UTF-8 locales. This
399-
* workaround was provided by Tim Robbins of FreeBSD project.
400-
*/
401-
402-
#if defined(__APPLE__)
403-
#define_PY_PORT_CTYPE_UTF8_ISSUE
404-
#endif
405-
406-
#ifdef _PY_PORT_CTYPE_UTF8_ISSUE
407-
#ifndef __cplusplus
408-
/* The workaround below is unsafe in C++ because
409-
* the <locale> defines these symbols as real functions,
410-
* with a slightly different signature.
411-
* See issue #10910
412-
*/
413-
#include<ctype.h>
414-
#include<wctype.h>
415-
#undef isalnum
416-
#defineisalnum(c) iswalnum(btowc(c))
417-
#undef isalpha
418-
#defineisalpha(c) iswalpha(btowc(c))
419-
#undef islower
420-
#defineislower(c) iswlower(btowc(c))
421-
#undef isspace
422-
#defineisspace(c) iswspace(btowc(c))
423-
#undef isupper
424-
#defineisupper(c) iswupper(btowc(c))
425-
#undef tolower
426-
#definetolower(c) towlower(btowc(c))
427-
#undef toupper
428-
#definetoupper(c) towupper(btowc(c))
429-
#endif
430-
#endif
431-
432-
433395
/* Declarations for symbol visibility.
434396
435397
PyAPI_FUNC(type): Declares a public Python API function and return type
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``Python.h`` no longer includes the ``<ctype.h>`` standard header file. If
2+
needed, it should now be included explicitly. For example, it provides
3+
``isalpha()`` and ``tolower()`` functions which are locale dependent. Python
4+
provides locale independent functions, like:c:func:`!Py_ISALPHA` and
5+
:c:func:`!Py_TOLOWER`. Patch by Victor Stinner.

‎Modules/_localemodule.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,25 @@ This software comes with no warranty. Use at your own risk.
1010
******************************************************************/
1111

1212
#include"Python.h"
13-
#include"pycore_fileutils.h"
14-
#include"pycore_pymem.h"// _PyMem_Strdup
15-
16-
#include<stdio.h>
17-
#include<locale.h>
18-
#include<string.h>
19-
#include<ctype.h>
13+
#include"pycore_fileutils.h"// _Py_GetLocaleconvNumeric()
14+
#include"pycore_pymem.h"// _PyMem_Strdup()
2015

16+
#include<locale.h>// setlocale()
17+
#include<string.h>// strlen()
2118
#ifdefHAVE_ERRNO_H
22-
#include<errno.h>
19+
#include<errno.h>// errno
2320
#endif
24-
2521
#ifdefHAVE_LANGINFO_H
26-
#include<langinfo.h>
22+
#include<langinfo.h>// nl_langinfo()
2723
#endif
28-
2924
#ifdefHAVE_LIBINTL_H
30-
#include<libintl.h>
31-
#endif
32-
33-
#ifdefHAVE_WCHAR_H
34-
#include<wchar.h>
25+
# include<libintl.h>
3526
#endif
36-
37-
#if defined(MS_WINDOWS)
38-
#ifndefWIN32_LEAN_AND_MEAN
39-
#defineWIN32_LEAN_AND_MEAN
40-
#endif
41-
#include<windows.h>
27+
#ifdefMS_WINDOWS
28+
# ifndefWIN32_LEAN_AND_MEAN
29+
# defineWIN32_LEAN_AND_MEAN
30+
# endif
31+
# include<windows.h>
4232
#endif
4333

4434
PyDoc_STRVAR(locale__doc__,"Support for POSIX locales.");

‎Modules/_sre/sre.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,40 @@ static const char copyright[] =
4343
#include"pycore_long.h"// _PyLong_GetZero()
4444
#include"pycore_moduleobject.h"// _PyModule_GetState()
4545

46+
#include"sre.h"// SRE_CODE
4647

47-
#include"sre.h"
48+
#include<ctype.h>// tolower(), toupper(), isalnum()
4849

4950
#defineSRE_CODE_BITS (8 * sizeof(SRE_CODE))
5051

51-
#include<ctype.h>
52+
// On macOS, use the wide character ctype API using btowc()
53+
#if defined(__APPLE__)
54+
# defineUSE_CTYPE_WINT_T
55+
#endif
56+
57+
staticintsre_isalnum(unsignedintch) {
58+
#ifdefUSE_CTYPE_WINT_T
59+
return (unsignedint)iswalnum(btowc((int)ch));
60+
#else
61+
return (unsignedint)isalnum((int)ch);
62+
#endif
63+
}
64+
65+
staticunsignedintsre_tolower(unsignedintch) {
66+
#ifdefUSE_CTYPE_WINT_T
67+
return (unsignedint)towlower(btowc((int)ch));
68+
#else
69+
return (unsignedint)tolower((int)ch);
70+
#endif
71+
}
72+
73+
staticunsignedintsre_toupper(unsignedintch) {
74+
#ifdefUSE_CTYPE_WINT_T
75+
return (unsignedint)towupper(btowc((int)ch));
76+
#else
77+
return (unsignedint)toupper((int)ch);
78+
#endif
79+
}
5280

5381
/* Defining this one controls tracing:
5482
* 0 -- disabled
@@ -114,17 +142,17 @@ static unsigned int sre_lower_ascii(unsigned int ch)
114142
/* locale-specific character predicates */
115143
/* !(c & ~N) == (c < N+1) for any unsigned c, this avoids
116144
* warnings when c's type supports only numbers < N+1 */
117-
#defineSRE_LOC_IS_ALNUM(ch) (!((ch) & ~255) ?isalnum((ch)) : 0)
145+
#defineSRE_LOC_IS_ALNUM(ch) (!((ch) & ~255) ?sre_isalnum((ch)) : 0)
118146
#defineSRE_LOC_IS_WORD(ch) (SRE_LOC_IS_ALNUM((ch)) || (ch) == '_')
119147

120148
staticunsignedintsre_lower_locale(unsignedintch)
121149
{
122-
return ((ch)<256 ? (unsignedint)tolower((ch)) :ch);
150+
return ((ch)<256 ? (unsignedint)sre_tolower((ch)) :ch);
123151
}
124152

125153
staticunsignedintsre_upper_locale(unsignedintch)
126154
{
127-
return ((ch)<256 ? (unsignedint)toupper((ch)) :ch);
155+
return ((ch)<256 ? (unsignedint)sre_toupper((ch)) :ch);
128156
}
129157

130158
/* unicode-specific character predicates */

‎Modules/_struct.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include"pycore_long.h"// _PyLong_AsByteArray()
1313
#include"pycore_moduleobject.h"// _PyModule_GetState()
1414

15-
#include<ctype.h>
1615
#include<stddef.h>// offsetof()
1716

1817
/*[clinic input]

‎Modules/_tkinter.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ Copyright (C) 1994 Steen Lumholt.
2626
#endif
2727

2828
#include"Python.h"
29-
#include<ctype.h>
3029
#ifdefMS_WINDOWS
3130
# include"pycore_fileutils.h"// _Py_stat()
3231
#endif
3332

34-
#include"pycore_long.h"
33+
#include"pycore_long.h"// _PyLong_IsNegative()
3534

3635
#ifdefMS_WINDOWS
37-
#include<windows.h>
36+
#include<windows.h>
3837
#endif
3938

4039
#defineCHECK_SIZE(size,elemsize) \
@@ -46,11 +45,11 @@ Copyright (C) 1994 Steen Lumholt.
4645
#defineTCL_THREADS
4746

4847
#ifdefTK_FRAMEWORK
49-
#include<Tcl/tcl.h>
50-
#include<Tk/tk.h>
48+
#include<Tcl/tcl.h>
49+
#include<Tk/tk.h>
5150
#else
52-
#include<tcl.h>
53-
#include<tk.h>
51+
#include<tcl.h>
52+
#include<tk.h>
5453
#endif
5554

5655
#include"tkinter.h"

‎Modules/_zoneinfo.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
#include"pycore_long.h"// _PyLong_GetOne()
77
#include"pycore_pyerrors.h"// _PyErr_ChainExceptions1()
88

9-
#include<ctype.h>
10-
#include<stddef.h>
11-
#include<stdint.h>
9+
#include"datetime.h"// PyDateTime_TZInfo
1210

13-
#include"datetime.h"
11+
#include<stddef.h>// offsetof()
12+
#include<stdint.h>
1413

1514
#include"clinic/_zoneinfo.c.h"
1615
/*[clinic input]

‎Modules/pyexpat.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include"pycore_pyhash.h"// _Py_HashSecret
88
#include"pycore_traceback.h"// _PyTraceback_Add()
99

10-
#include<ctype.h>
1110
#include<stddef.h>// offsetof()
1211
#include"expat.h"
1312
#include"pyexpat.h"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp