Skip to content

Commit

Permalink
bpo-32241: Add the const qualifire to declarations of umodifiable str…
Browse files Browse the repository at this point in the history
…ings. (#4748)
  • Loading branch information
serhiy-storchaka authored Dec 12, 2017
1 parent 5ce0a2a commit 4ae06c5
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Include/pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:c:func:`Py_SetProgramName` and :c:func:`Py_SetPythonHome` now take the
``const wchar *`` arguments instead of ``wchar *``.
2 changes: 1 addition & 1 deletion Modules/getaddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 23 additions & 23 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -542,7 +542,7 @@ pymain_run_main_from_importer(_PyMain *pymain)


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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions PC/getpathp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Python/bootstrap_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Python/dynamic_annotations.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/frozenmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Python/pathconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 4ae06c5

Please sign in to comment.