Skip to content

Commit 4ae06c5

Browse files
bpo-32241: Add the const qualifire to declarations of umodifiable strings. (#4748)
1 parent 5ce0a2a commit 4ae06c5

File tree

15 files changed

+59
-56
lines changed

15 files changed

+59
-56
lines changed

Doc/c-api/init.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ Process-wide parameters
338338
.. versionadded:: 3.4
339339
340340
341-
.. c:function:: void Py_SetProgramName(wchar_t *name)
341+
.. c:function:: void Py_SetProgramName(const wchar_t *name)
342342
343343
.. index::
344344
single: Py_Initialize()
@@ -605,7 +605,7 @@ Process-wide parameters
605605
.. versionchanged:: 3.4 The *updatepath* value depends on :option:`-I`.
606606
607607
608-
.. c:function:: void Py_SetPythonHome(wchar_t *home)
608+
.. c:function:: void Py_SetPythonHome(const wchar_t *home)
609609
610610
Set the default "home" directory, that is, the location of the standard
611611
Python libraries. See :envvar:`PYTHONHOME` for the meaning of the

Include/pylifecycle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ typedef struct {
3737
#endif
3838

3939

40-
PyAPI_FUNC(void) Py_SetProgramName(wchar_t *);
40+
PyAPI_FUNC(void) Py_SetProgramName(const wchar_t *);
4141
PyAPI_FUNC(wchar_t *) Py_GetProgramName(void);
4242

43-
PyAPI_FUNC(void) Py_SetPythonHome(wchar_t *);
43+
PyAPI_FUNC(void) Py_SetPythonHome(const wchar_t *);
4444
PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void);
4545

4646
#ifndef Py_LIMITED_API
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:c:func:`Py_SetProgramName` and :c:func:`Py_SetPythonHome` now take the
2+
``const wchar *`` arguments instead of ``wchar *``.

Modules/getaddrinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ getaddrinfo(const char*hostname, const char*servname,
251251
if (firsttime) {
252252
/* translator hack */
253253
{
254-
char *q = getenv("GAI");
254+
const char *q = getenv("GAI");
255255
if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
256256
translate = YES;
257257
}

Modules/getpath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ calculate_init(PyCalculatePath *calculate,
910910
const _PyMainInterpreterConfig *main_config)
911911
{
912912
size_t len;
913-
char *path = getenv("PATH");
913+
const char *path = getenv("PATH");
914914
if (path) {
915915
calculate->path_env = Py_DecodeLocale(path, &len);
916916
if (!calculate->path_env) {

Modules/main.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ pymain_usage(int error, const wchar_t* program)
154154
}
155155

156156

157-
static char*
157+
static const char*
158158
pymain_get_env_var(const char *name)
159159
{
160-
char *var = Py_GETENV(name);
160+
const char *var = Py_GETENV(name);
161161
if (var && var[0] != '\0') {
162162
return var;
163163
}
@@ -170,7 +170,7 @@ pymain_get_env_var(const char *name)
170170
static void
171171
pymain_run_startup(PyCompilerFlags *cf)
172172
{
173-
char *startup = pymain_get_env_var("PYTHONSTARTUP");
173+
const char *startup = pymain_get_env_var("PYTHONSTARTUP");
174174
if (startup == NULL) {
175175
return;
176176
}
@@ -542,7 +542,7 @@ pymain_run_main_from_importer(_PyMain *pymain)
542542

543543

544544
static wchar_t*
545-
pymain_wstrdup(_PyMain *pymain, wchar_t *str)
545+
pymain_wstrdup(_PyMain *pymain, const wchar_t *str)
546546
{
547547
wchar_t *str2 = _PyMem_RawWcsdup(str);
548548
if (str2 == NULL) {
@@ -554,7 +554,7 @@ pymain_wstrdup(_PyMain *pymain, wchar_t *str)
554554

555555

556556
static int
557-
pymain_optlist_append(_PyMain *pymain, _Py_OptList *list, wchar_t *str)
557+
pymain_optlist_append(_PyMain *pymain, _Py_OptList *list, const wchar_t *str)
558558
{
559559
wchar_t *str2 = pymain_wstrdup(pymain, str);
560560
if (str2 == NULL) {
@@ -802,7 +802,7 @@ pymain_warnings_envvar(_PyMain *pymain)
802802
}
803803

804804
#ifdef MS_WINDOWS
805-
wchar_t *wp;
805+
const wchar_t *wp;
806806

807807
if ((wp = _wgetenv(L"PYTHONWARNINGS")) && *wp != L'\0') {
808808
wchar_t *warning, *context = NULL;
@@ -824,7 +824,7 @@ pymain_warnings_envvar(_PyMain *pymain)
824824
PyMem_RawFree(buf);
825825
}
826826
#else
827-
char *p = pymain_get_env_var("PYTHONWARNINGS");
827+
const char *p = pymain_get_env_var("PYTHONWARNINGS");
828828
if (p != NULL) {
829829
char *buf, *oldloc;
830830

@@ -909,7 +909,7 @@ config_get_program_name(_PyMainInterpreterConfig *config)
909909
assert(config->program_name == NULL);
910910

911911
/* If Py_SetProgramName() was called, use its value */
912-
wchar_t *program_name = _Py_path_config.program_name;
912+
const wchar_t *program_name = _Py_path_config.program_name;
913913
if (program_name != NULL) {
914914
config->program_name = _PyMem_RawWcsdup(program_name);
915915
if (config->program_name == NULL) {
@@ -927,7 +927,7 @@ config_get_program_name(_PyMainInterpreterConfig *config)
927927
so the actual executable path is passed in an environment variable.
928928
See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
929929
script. */
930-
char *p = pymain_get_env_var("PYTHONEXECUTABLE");
930+
const char *p = pymain_get_env_var("PYTHONEXECUTABLE");
931931
if (p != NULL) {
932932
size_t len;
933933
wchar_t* program_name = Py_DecodeLocale(p, &len);
@@ -939,7 +939,7 @@ config_get_program_name(_PyMainInterpreterConfig *config)
939939
}
940940
#ifdef WITH_NEXT_FRAMEWORK
941941
else {
942-
char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
942+
const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__");
943943
if (pyvenv_launcher && *pyvenv_launcher) {
944944
/* Used by Mac/Tools/pythonw.c to forward
945945
* the argv0 of the stub executable
@@ -1289,7 +1289,7 @@ pymain_parse_cmdline(_PyMain *pymain)
12891289
}
12901290

12911291

1292-
static wchar_t*
1292+
static const wchar_t*
12931293
pymain_get_xoption(_PyMain *pymain, wchar_t *name)
12941294
{
12951295
_Py_OptList *list = &pymain->cmdline.xoptions;
@@ -1312,11 +1312,11 @@ pymain_get_xoption(_PyMain *pymain, wchar_t *name)
13121312

13131313

13141314
static int
1315-
pymain_str_to_int(char *str, int *result)
1315+
pymain_str_to_int(const char *str, int *result)
13161316
{
13171317
errno = 0;
1318-
char *endptr = str;
1319-
long value = strtol(str, &endptr, 10);
1318+
const char *endptr = str;
1319+
long value = strtol(str, (char **)&endptr, 10);
13201320
if (*endptr != '\0' || errno == ERANGE) {
13211321
return -1;
13221322
}
@@ -1330,11 +1330,11 @@ pymain_str_to_int(char *str, int *result)
13301330

13311331

13321332
static int
1333-
pymain_wstr_to_int(wchar_t *wstr, int *result)
1333+
pymain_wstr_to_int(const wchar_t *wstr, int *result)
13341334
{
13351335
errno = 0;
1336-
wchar_t *endptr = wstr;
1337-
long value = wcstol(wstr, &endptr, 10);
1336+
const wchar_t *endptr = wstr;
1337+
long value = wcstol(wstr, (wchar_t **)&endptr, 10);
13381338
if (*endptr != '\0' || errno == ERANGE) {
13391339
return -1;
13401340
}
@@ -1353,7 +1353,7 @@ pymain_init_tracemalloc(_PyMain *pymain)
13531353
int nframe;
13541354
int valid;
13551355

1356-
char *env = pymain_get_env_var("PYTHONTRACEMALLOC");
1356+
const char *env = pymain_get_env_var("PYTHONTRACEMALLOC");
13571357
if (env) {
13581358
if (!pymain_str_to_int(env, &nframe)) {
13591359
valid = (nframe >= 1);
@@ -1369,9 +1369,9 @@ pymain_init_tracemalloc(_PyMain *pymain)
13691369
pymain->core_config.tracemalloc = nframe;
13701370
}
13711371

1372-
wchar_t *xoption = pymain_get_xoption(pymain, L"tracemalloc");
1372+
const wchar_t *xoption = pymain_get_xoption(pymain, L"tracemalloc");
13731373
if (xoption) {
1374-
wchar_t *sep = wcschr(xoption, L'=');
1374+
const wchar_t *sep = wcschr(xoption, L'=');
13751375
if (sep) {
13761376
if (!pymain_wstr_to_int(sep + 1, &nframe)) {
13771377
valid = (nframe >= 1);
@@ -1398,7 +1398,7 @@ pymain_init_tracemalloc(_PyMain *pymain)
13981398
static void
13991399
pymain_set_flag_from_env(int *flag, const char *name)
14001400
{
1401-
char *var = pymain_get_env_var(name);
1401+
const char *var = pymain_get_env_var(name);
14021402
if (!var) {
14031403
return;
14041404
}
@@ -1449,7 +1449,7 @@ config_get_env_var_dup(wchar_t **dest, wchar_t *wname, char *name)
14491449
}
14501450

14511451
#ifdef MS_WINDOWS
1452-
wchar_t *var = _wgetenv(wname);
1452+
const wchar_t *var = _wgetenv(wname);
14531453
if (!var || var[0] == '\0') {
14541454
*dest = NULL;
14551455
return 0;
@@ -1462,7 +1462,7 @@ config_get_env_var_dup(wchar_t **dest, wchar_t *wname, char *name)
14621462

14631463
*dest = copy;
14641464
#else
1465-
char *var = getenv(name);
1465+
const char *var = getenv(name);
14661466
if (!var || var[0] == '\0') {
14671467
*dest = NULL;
14681468
return 0;

Objects/obmalloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ new_arena(void)
11671167
static int debug_stats = -1;
11681168

11691169
if (debug_stats == -1) {
1170-
char *opt = Py_GETENV("PYTHONMALLOCSTATS");
1170+
const char *opt = Py_GETENV("PYTHONMALLOCSTATS");
11711171
debug_stats = (opt != NULL && *opt != '\0');
11721172
}
11731173
if (debug_stats)

PC/getpathp.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@
118118
#endif
119119

120120
typedef struct {
121-
wchar_t *path_env; /* PATH environment variable */
122-
wchar_t *home; /* PYTHONHOME environment variable */
121+
const wchar_t *path_env; /* PATH environment variable */
122+
const wchar_t *home; /* PYTHONHOME environment variable */
123123

124124
/* Registry key "Software\Python\PythonCore\PythonPath" */
125125
wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
@@ -191,7 +191,7 @@ change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
191191

192192

193193
static int
194-
exists(wchar_t *filename)
194+
exists(const wchar_t *filename)
195195
{
196196
return GetFileAttributesW(filename) != 0xFFFFFFFF;
197197
}
@@ -286,7 +286,7 @@ gotlandmark(wchar_t *prefix, const wchar_t *landmark)
286286
/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
287287
assumption provided by only caller, calculate_path_impl() */
288288
static int
289-
search_for_prefix(wchar_t *prefix, wchar_t *argv0_path, const wchar_t *landmark)
289+
search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
290290
{
291291
/* Search from argv0_path, until landmark is found */
292292
wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
@@ -523,9 +523,9 @@ get_program_full_path(const _PyMainInterpreterConfig *main_config,
523523
wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN);
524524
}
525525
else if (calculate->path_env) {
526-
wchar_t *path = calculate->path_env;
526+
const wchar_t *path = calculate->path_env;
527527
while (1) {
528-
wchar_t *delim = wcschr(path, DELIM);
528+
const wchar_t *delim = wcschr(path, DELIM);
529529

530530
if (delim) {
531531
size_t len = delim - path;
@@ -845,7 +845,7 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
845845
/* Calculate size of return buffer */
846846
size_t bufsz = 0;
847847
if (calculate->home != NULL) {
848-
wchar_t *p;
848+
const wchar_t *p;
849849
bufsz = 1;
850850
for (p = PYTHONPATH; *p; p++) {
851851
if (*p == DELIM) {
@@ -922,8 +922,8 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
922922
*buf++ = DELIM;
923923
}
924924
} else {
925-
wchar_t *p = PYTHONPATH;
926-
wchar_t *q;
925+
const wchar_t *p = PYTHONPATH;
926+
const wchar_t *q;
927927
size_t n;
928928
for (;;) {
929929
q = wcschr(p, DELIM);
@@ -967,10 +967,10 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
967967
*/
968968
if (prefix[0] == L'\0') {
969969
wchar_t lookBuf[MAXPATHLEN+1];
970-
wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
970+
const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
971971
while (1) {
972972
Py_ssize_t nchars;
973-
wchar_t *lookEnd = look;
973+
const wchar_t *lookEnd = look;
974974
/* 'look' will end up one character before the
975975
start of the path in question - even if this
976976
is one character before the start of the buffer

Python/bootstrap_hash.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,16 +533,16 @@ _PyOS_URandomNonblock(void *buffer, Py_ssize_t size)
533533
return pyurandom(buffer, size, 0, 1);
534534
}
535535

536-
int Py_ReadHashSeed(char *seed_text,
536+
int Py_ReadHashSeed(const char *seed_text,
537537
int *use_hash_seed,
538538
unsigned long *hash_seed)
539539
{
540540
Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc));
541541
/* Convert a text seed to a numeric one */
542542
if (seed_text && *seed_text != '\0' && strcmp(seed_text, "random") != 0) {
543-
char *endptr = seed_text;
543+
const char *endptr = seed_text;
544544
unsigned long seed;
545-
seed = strtoul(seed_text, &endptr, 10);
545+
seed = strtoul(seed_text, (char **)&endptr, 10);
546546
if (*endptr != '\0'
547547
|| seed > 4294967295UL
548548
|| (errno == ERANGE && seed == ULONG_MAX))
@@ -604,7 +604,7 @@ init_hash_secret(int use_hash_seed,
604604
_PyInitError
605605
_Py_HashRandomization_Init(_PyCoreConfig *core_config)
606606
{
607-
char *seed_text;
607+
const char *seed_text;
608608
int use_hash_seed = core_config->use_hash_seed;
609609
unsigned long hash_seed = core_config->hash_seed;
610610

Python/dynamic_annotations.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static int GetRunningOnValgrind(void) {
120120
#endif
121121

122122
#ifndef _MSC_VER
123-
char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND");
123+
const char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND");
124124
if (running_on_valgrind_str) {
125125
return strcmp(running_on_valgrind_str, "0") != 0;
126126
}

Python/frozenmain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Py_FrozenMain(int argc, char **argv)
2323
exit(1);
2424
}
2525

26-
char *p;
26+
const char *p;
2727
int i, n, sts = 1;
2828
int inspect = 0;
2929
int unbuffered = 0;

Python/pathconfig.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Py_SetPath(const wchar_t *path)
168168

169169

170170
void
171-
Py_SetPythonHome(wchar_t *home)
171+
Py_SetPythonHome(const wchar_t *home)
172172
{
173173
if (home == NULL) {
174174
return;
@@ -189,7 +189,7 @@ Py_SetPythonHome(wchar_t *home)
189189

190190

191191
void
192-
Py_SetProgramName(wchar_t *program_name)
192+
Py_SetProgramName(const wchar_t *program_name)
193193
{
194194
if (program_name == NULL || program_name[0] == L'\0') {
195195
return;

Python/pylifecycle.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ static _LocaleCoercionTarget _TARGET_LOCALES[] = {
414414
{NULL}
415415
};
416416

417-
static char *
417+
static const char *
418418
get_default_standard_stream_error_handler(void)
419419
{
420420
const char *ctype_loc = setlocale(LC_CTYPE, NULL);
@@ -440,7 +440,7 @@ get_default_standard_stream_error_handler(void)
440440
}
441441

442442
#ifdef PY_COERCE_C_LOCALE
443-
static const char *_C_LOCALE_COERCION_WARNING =
443+
static const char _C_LOCALE_COERCION_WARNING[] =
444444
"Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale "
445445
"or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n";
446446

@@ -1757,7 +1757,8 @@ init_sys_streams(void)
17571757
PyObject *std = NULL;
17581758
int fd;
17591759
PyObject * encoding_attr;
1760-
char *pythonioencoding = NULL, *encoding, *errors;
1760+
char *pythonioencoding = NULL;
1761+
const char *encoding, *errors;
17611762
_PyInitError res = _Py_INIT_OK();
17621763

17631764
/* Hack to avoid a nasty recursion issue when Python is invoked

0 commit comments

Comments
 (0)