From 4ae06c5337e01bdde28bce57b6b9166ad50947e3 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 12 Dec 2017 13:55:04 +0200 Subject: [PATCH] bpo-32241: Add the const qualifire to declarations of umodifiable strings. (#4748) --- Doc/c-api/init.rst | 4 +- Include/pylifecycle.h | 4 +- .../2017-12-07-15-58-15.bpo-32241.LbyQt6.rst | 2 + Modules/getaddrinfo.c | 2 +- Modules/getpath.c | 2 +- Modules/main.c | 46 +++++++++---------- Objects/obmalloc.c | 2 +- PC/getpathp.c | 22 ++++----- Python/bootstrap_hash.c | 8 ++-- Python/dynamic_annotations.c | 2 +- Python/frozenmain.c | 2 +- Python/pathconfig.c | 4 +- Python/pylifecycle.c | 7 +-- Python/sysmodule.c | 6 +-- Python/thread.c | 2 +- 15 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2017-12-07-15-58-15.bpo-32241.LbyQt6.rst diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index a3113a390fd..4ee61e8a1cf 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -338,7 +338,7 @@ Process-wide parameters .. versionadded:: 3.4 -.. c:function:: void Py_SetProgramName(wchar_t *name) +.. c:function:: void Py_SetProgramName(const wchar_t *name) .. index:: single: Py_Initialize() @@ -605,7 +605,7 @@ Process-wide parameters .. versionchanged:: 3.4 The *updatepath* value depends on :option:`-I`. -.. c:function:: void Py_SetPythonHome(wchar_t *home) +.. c:function:: void Py_SetPythonHome(const wchar_t *home) Set the default "home" directory, that is, the location of the standard Python libraries. See :envvar:`PYTHONHOME` for the meaning of the diff --git a/Include/pylifecycle.h b/Include/pylifecycle.h index bda51f84c3c..542306004d2 100644 --- a/Include/pylifecycle.h +++ b/Include/pylifecycle.h @@ -37,10 +37,10 @@ typedef struct { #endif -PyAPI_FUNC(void) Py_SetProgramName(wchar_t *); +PyAPI_FUNC(void) Py_SetProgramName(const wchar_t *); PyAPI_FUNC(wchar_t *) Py_GetProgramName(void); -PyAPI_FUNC(void) Py_SetPythonHome(wchar_t *); +PyAPI_FUNC(void) Py_SetPythonHome(const wchar_t *); PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void); #ifndef Py_LIMITED_API diff --git a/Misc/NEWS.d/next/C API/2017-12-07-15-58-15.bpo-32241.LbyQt6.rst b/Misc/NEWS.d/next/C API/2017-12-07-15-58-15.bpo-32241.LbyQt6.rst new file mode 100644 index 00000000000..71fa8baa807 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2017-12-07-15-58-15.bpo-32241.LbyQt6.rst @@ -0,0 +1,2 @@ +:c:func:`Py_SetProgramName` and :c:func:`Py_SetPythonHome` now take the +``const wchar *`` arguments instead of ``wchar *``. diff --git a/Modules/getaddrinfo.c b/Modules/getaddrinfo.c index 06e87bfc8ce..5aaa6e7c814 100644 --- a/Modules/getaddrinfo.c +++ b/Modules/getaddrinfo.c @@ -251,7 +251,7 @@ getaddrinfo(const char*hostname, const char*servname, if (firsttime) { /* translator hack */ { - char *q = getenv("GAI"); + const char *q = getenv("GAI"); if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1) translate = YES; } diff --git a/Modules/getpath.c b/Modules/getpath.c index fc2b5442ce2..11babf0a00b 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -910,7 +910,7 @@ calculate_init(PyCalculatePath *calculate, const _PyMainInterpreterConfig *main_config) { size_t len; - char *path = getenv("PATH"); + const char *path = getenv("PATH"); if (path) { calculate->path_env = Py_DecodeLocale(path, &len); if (!calculate->path_env) { diff --git a/Modules/main.c b/Modules/main.c index e536492379d..68ee616da62 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -154,10 +154,10 @@ pymain_usage(int error, const wchar_t* program) } -static char* +static const char* pymain_get_env_var(const char *name) { - char *var = Py_GETENV(name); + const char *var = Py_GETENV(name); if (var && var[0] != '\0') { return var; } @@ -170,7 +170,7 @@ pymain_get_env_var(const char *name) static void pymain_run_startup(PyCompilerFlags *cf) { - char *startup = pymain_get_env_var("PYTHONSTARTUP"); + const char *startup = pymain_get_env_var("PYTHONSTARTUP"); if (startup == NULL) { return; } @@ -542,7 +542,7 @@ error: static wchar_t* -pymain_wstrdup(_PyMain *pymain, wchar_t *str) +pymain_wstrdup(_PyMain *pymain, const wchar_t *str) { wchar_t *str2 = _PyMem_RawWcsdup(str); if (str2 == NULL) { @@ -554,7 +554,7 @@ pymain_wstrdup(_PyMain *pymain, wchar_t *str) static int -pymain_optlist_append(_PyMain *pymain, _Py_OptList *list, wchar_t *str) +pymain_optlist_append(_PyMain *pymain, _Py_OptList *list, const wchar_t *str) { wchar_t *str2 = pymain_wstrdup(pymain, str); if (str2 == NULL) { @@ -802,7 +802,7 @@ pymain_warnings_envvar(_PyMain *pymain) } #ifdef MS_WINDOWS - wchar_t *wp; + const wchar_t *wp; if ((wp = _wgetenv(L"PYTHONWARNINGS")) && *wp != L'\0') { wchar_t *warning, *context = NULL; @@ -824,7 +824,7 @@ pymain_warnings_envvar(_PyMain *pymain) PyMem_RawFree(buf); } #else - char *p = pymain_get_env_var("PYTHONWARNINGS"); + const char *p = pymain_get_env_var("PYTHONWARNINGS"); if (p != NULL) { char *buf, *oldloc; @@ -909,7 +909,7 @@ config_get_program_name(_PyMainInterpreterConfig *config) assert(config->program_name == NULL); /* If Py_SetProgramName() was called, use its value */ - wchar_t *program_name = _Py_path_config.program_name; + const wchar_t *program_name = _Py_path_config.program_name; if (program_name != NULL) { config->program_name = _PyMem_RawWcsdup(program_name); if (config->program_name == NULL) { @@ -927,7 +927,7 @@ config_get_program_name(_PyMainInterpreterConfig *config) so the actual executable path is passed in an environment variable. See Lib/plat-mac/bundlebuiler.py for details about the bootstrap script. */ - char *p = pymain_get_env_var("PYTHONEXECUTABLE"); + const char *p = pymain_get_env_var("PYTHONEXECUTABLE"); if (p != NULL) { size_t len; wchar_t* program_name = Py_DecodeLocale(p, &len); @@ -939,7 +939,7 @@ config_get_program_name(_PyMainInterpreterConfig *config) } #ifdef WITH_NEXT_FRAMEWORK else { - char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__"); + const char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__"); if (pyvenv_launcher && *pyvenv_launcher) { /* Used by Mac/Tools/pythonw.c to forward * the argv0 of the stub executable @@ -1289,7 +1289,7 @@ pymain_parse_cmdline(_PyMain *pymain) } -static wchar_t* +static const wchar_t* pymain_get_xoption(_PyMain *pymain, wchar_t *name) { _Py_OptList *list = &pymain->cmdline.xoptions; @@ -1312,11 +1312,11 @@ pymain_get_xoption(_PyMain *pymain, wchar_t *name) static int -pymain_str_to_int(char *str, int *result) +pymain_str_to_int(const char *str, int *result) { errno = 0; - char *endptr = str; - long value = strtol(str, &endptr, 10); + const char *endptr = str; + long value = strtol(str, (char **)&endptr, 10); if (*endptr != '\0' || errno == ERANGE) { return -1; } @@ -1330,11 +1330,11 @@ pymain_str_to_int(char *str, int *result) static int -pymain_wstr_to_int(wchar_t *wstr, int *result) +pymain_wstr_to_int(const wchar_t *wstr, int *result) { errno = 0; - wchar_t *endptr = wstr; - long value = wcstol(wstr, &endptr, 10); + const wchar_t *endptr = wstr; + long value = wcstol(wstr, (wchar_t **)&endptr, 10); if (*endptr != '\0' || errno == ERANGE) { return -1; } @@ -1353,7 +1353,7 @@ pymain_init_tracemalloc(_PyMain *pymain) int nframe; int valid; - char *env = pymain_get_env_var("PYTHONTRACEMALLOC"); + const char *env = pymain_get_env_var("PYTHONTRACEMALLOC"); if (env) { if (!pymain_str_to_int(env, &nframe)) { valid = (nframe >= 1); @@ -1369,9 +1369,9 @@ pymain_init_tracemalloc(_PyMain *pymain) pymain->core_config.tracemalloc = nframe; } - wchar_t *xoption = pymain_get_xoption(pymain, L"tracemalloc"); + const wchar_t *xoption = pymain_get_xoption(pymain, L"tracemalloc"); if (xoption) { - wchar_t *sep = wcschr(xoption, L'='); + const wchar_t *sep = wcschr(xoption, L'='); if (sep) { if (!pymain_wstr_to_int(sep + 1, &nframe)) { valid = (nframe >= 1); @@ -1398,7 +1398,7 @@ pymain_init_tracemalloc(_PyMain *pymain) static void pymain_set_flag_from_env(int *flag, const char *name) { - char *var = pymain_get_env_var(name); + const char *var = pymain_get_env_var(name); if (!var) { return; } @@ -1449,7 +1449,7 @@ config_get_env_var_dup(wchar_t **dest, wchar_t *wname, char *name) } #ifdef MS_WINDOWS - wchar_t *var = _wgetenv(wname); + const wchar_t *var = _wgetenv(wname); if (!var || var[0] == '\0') { *dest = NULL; return 0; @@ -1462,7 +1462,7 @@ config_get_env_var_dup(wchar_t **dest, wchar_t *wname, char *name) *dest = copy; #else - char *var = getenv(name); + const char *var = getenv(name); if (!var || var[0] == '\0') { *dest = NULL; return 0; diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index c065541458b..0b8816cc54f 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -1167,7 +1167,7 @@ new_arena(void) static int debug_stats = -1; if (debug_stats == -1) { - char *opt = Py_GETENV("PYTHONMALLOCSTATS"); + const char *opt = Py_GETENV("PYTHONMALLOCSTATS"); debug_stats = (opt != NULL && *opt != '\0'); } if (debug_stats) diff --git a/PC/getpathp.c b/PC/getpathp.c index 08ed8ccc83f..e142e365052 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -118,8 +118,8 @@ #endif typedef struct { - wchar_t *path_env; /* PATH environment variable */ - wchar_t *home; /* PYTHONHOME environment variable */ + const wchar_t *path_env; /* PATH environment variable */ + const wchar_t *home; /* PYTHONHOME environment variable */ /* Registry key "Software\Python\PythonCore\PythonPath" */ 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) static int -exists(wchar_t *filename) +exists(const wchar_t *filename) { return GetFileAttributesW(filename) != 0xFFFFFFFF; } @@ -286,7 +286,7 @@ gotlandmark(wchar_t *prefix, const wchar_t *landmark) /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd. assumption provided by only caller, calculate_path_impl() */ static int -search_for_prefix(wchar_t *prefix, wchar_t *argv0_path, const wchar_t *landmark) +search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark) { /* Search from argv0_path, until landmark is found */ wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path); @@ -523,9 +523,9 @@ get_program_full_path(const _PyMainInterpreterConfig *main_config, wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN); } else if (calculate->path_env) { - wchar_t *path = calculate->path_env; + const wchar_t *path = calculate->path_env; while (1) { - wchar_t *delim = wcschr(path, DELIM); + const wchar_t *delim = wcschr(path, DELIM); if (delim) { size_t len = delim - path; @@ -845,7 +845,7 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config, /* Calculate size of return buffer */ size_t bufsz = 0; if (calculate->home != NULL) { - wchar_t *p; + const wchar_t *p; bufsz = 1; for (p = PYTHONPATH; *p; p++) { if (*p == DELIM) { @@ -922,8 +922,8 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config, *buf++ = DELIM; } } else { - wchar_t *p = PYTHONPATH; - wchar_t *q; + const wchar_t *p = PYTHONPATH; + const wchar_t *q; size_t n; for (;;) { q = wcschr(p, DELIM); @@ -967,10 +967,10 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config, */ if (prefix[0] == L'\0') { wchar_t lookBuf[MAXPATHLEN+1]; - wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */ + const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */ while (1) { Py_ssize_t nchars; - wchar_t *lookEnd = look; + const wchar_t *lookEnd = look; /* 'look' will end up one character before the start of the path in question - even if this is one character before the start of the buffer diff --git a/Python/bootstrap_hash.c b/Python/bootstrap_hash.c index 610541d810b..2762f4656e2 100644 --- a/Python/bootstrap_hash.c +++ b/Python/bootstrap_hash.c @@ -533,16 +533,16 @@ _PyOS_URandomNonblock(void *buffer, Py_ssize_t size) return pyurandom(buffer, size, 0, 1); } -int Py_ReadHashSeed(char *seed_text, +int Py_ReadHashSeed(const char *seed_text, int *use_hash_seed, unsigned long *hash_seed) { Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc)); /* Convert a text seed to a numeric one */ if (seed_text && *seed_text != '\0' && strcmp(seed_text, "random") != 0) { - char *endptr = seed_text; + const char *endptr = seed_text; unsigned long seed; - seed = strtoul(seed_text, &endptr, 10); + seed = strtoul(seed_text, (char **)&endptr, 10); if (*endptr != '\0' || seed > 4294967295UL || (errno == ERANGE && seed == ULONG_MAX)) @@ -604,7 +604,7 @@ init_hash_secret(int use_hash_seed, _PyInitError _Py_HashRandomization_Init(_PyCoreConfig *core_config) { - char *seed_text; + const char *seed_text; int use_hash_seed = core_config->use_hash_seed; unsigned long hash_seed = core_config->hash_seed; diff --git a/Python/dynamic_annotations.c b/Python/dynamic_annotations.c index 10511da4661..7febaa09df1 100644 --- a/Python/dynamic_annotations.c +++ b/Python/dynamic_annotations.c @@ -120,7 +120,7 @@ static int GetRunningOnValgrind(void) { #endif #ifndef _MSC_VER - char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND"); + const char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND"); if (running_on_valgrind_str) { return strcmp(running_on_valgrind_str, "0") != 0; } diff --git a/Python/frozenmain.c b/Python/frozenmain.c index 77602d70fa8..a3b619671bd 100644 --- a/Python/frozenmain.c +++ b/Python/frozenmain.c @@ -23,7 +23,7 @@ Py_FrozenMain(int argc, char **argv) exit(1); } - char *p; + const char *p; int i, n, sts = 1; int inspect = 0; int unbuffered = 0; diff --git a/Python/pathconfig.c b/Python/pathconfig.c index 6a03f7dca1b..53ddfc997da 100644 --- a/Python/pathconfig.c +++ b/Python/pathconfig.c @@ -168,7 +168,7 @@ Py_SetPath(const wchar_t *path) void -Py_SetPythonHome(wchar_t *home) +Py_SetPythonHome(const wchar_t *home) { if (home == NULL) { return; @@ -189,7 +189,7 @@ Py_SetPythonHome(wchar_t *home) void -Py_SetProgramName(wchar_t *program_name) +Py_SetProgramName(const wchar_t *program_name) { if (program_name == NULL || program_name[0] == L'\0') { return; diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 0b3aa98ba2e..fdb09d910da 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -414,7 +414,7 @@ static _LocaleCoercionTarget _TARGET_LOCALES[] = { {NULL} }; -static char * +static const char * get_default_standard_stream_error_handler(void) { const char *ctype_loc = setlocale(LC_CTYPE, NULL); @@ -440,7 +440,7 @@ get_default_standard_stream_error_handler(void) } #ifdef PY_COERCE_C_LOCALE -static const char *_C_LOCALE_COERCION_WARNING = +static const char _C_LOCALE_COERCION_WARNING[] = "Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale " "or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\n"; @@ -1757,7 +1757,8 @@ init_sys_streams(void) PyObject *std = NULL; int fd; PyObject * encoding_attr; - char *pythonioencoding = NULL, *encoding, *errors; + char *pythonioencoding = NULL; + const char *encoding, *errors; _PyInitError res = _Py_INIT_OK(); /* Hack to avoid a nasty recursion issue when Python is invoked diff --git a/Python/sysmodule.c b/Python/sysmodule.c index eeeaa7240e7..f10099b5232 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -100,7 +100,7 @@ static PyObject * sys_breakpointhook(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *keywords) { assert(!PyErr_Occurred()); - char *envar = Py_GETENV("PYTHONBREAKPOINT"); + const char *envar = Py_GETENV("PYTHONBREAKPOINT"); if (envar == NULL || strlen(envar) == 0) { envar = "pdb.set_trace"; @@ -109,8 +109,8 @@ sys_breakpointhook(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject * /* The breakpoint is explicitly no-op'd. */ Py_RETURN_NONE; } - char *last_dot = strrchr(envar, '.'); - char *attrname = NULL; + const char *last_dot = strrchr(envar, '.'); + const char *attrname = NULL; PyObject *modulepath = NULL; if (last_dot == NULL) { diff --git a/Python/thread.c b/Python/thread.c index 7eac836dc16..07743840693 100644 --- a/Python/thread.c +++ b/Python/thread.c @@ -61,7 +61,7 @@ void PyThread_init_thread(void) { #ifdef Py_DEBUG - char *p = Py_GETENV("PYTHONTHREADDEBUG"); + const char *p = Py_GETENV("PYTHONTHREADDEBUG"); if (p) { if (*p)