From 74f6568bbd3e70806ea3219e8bacb386ad802ccf Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 15 Mar 2019 15:08:05 +0100 Subject: bpo-36301: Add _PyWstrList structure (GH-12343) Replace messy _Py_wstrlist_xxx() functions with a new clean _PyWstrList structure and new _PyWstrList_xxx() functions. Changes: * Add _PyCoreConfig.use_module_search_paths to decide if _PyCoreConfig.module_search_paths should be computed or not, to support empty search path list. * _PyWstrList_Clear() sets length to 0 and items to NULL, whereas _Py_wstrlist_clear() only freed memory. * _PyWstrList_Append() returns an int, whereas _Py_wstrlist_append() returned _PyInitError. * _PyWstrList uses Py_ssize_t for the length, instead of int. * Replace (int, wchar_t**) with _PyWstrList in: * _PyPreConfig * _PyCoreConfig * _PyPreCmdline * _PyCmdline * Replace "int orig_argv; wchar_t **orig_argv;" with "_PyWstrList orig_argv". * _PyCmdline and _PyPreCmdline now also copy wchar_argv. * Rename _PyArgv_Decode() to _PyArgv_AsWstrList(). * PySys_SetArgvEx() now pass the fixed (argc, argv) to _PyPathConfig_ComputeArgv0() (don't pass negative argc or NULL argv). * _PyOS_GetOpt() uses Py_ssize_t --- Python/sysmodule.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'Python/sysmodule.c') diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 99fd460ff5..b3330a01f7 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2739,35 +2739,35 @@ PySys_SetPath(const wchar_t *path) } static PyObject * -makeargvobject(int argc, wchar_t **argv) +make_sys_argv(int argc, wchar_t * const * argv) { - PyObject *av; - if (argc <= 0 || argv == NULL) { - /* Ensure at least one (empty) argument is seen */ - static wchar_t *empty_argv[1] = {L""}; - argv = empty_argv; - argc = 1; + PyObject *list = PyList_New(argc); + if (list == NULL) { + return NULL; } - av = PyList_New(argc); - if (av != NULL) { - int i; - for (i = 0; i < argc; i++) { - PyObject *v = PyUnicode_FromWideChar(argv[i], -1); - if (v == NULL) { - Py_DECREF(av); - av = NULL; - break; - } - PyList_SET_ITEM(av, i, v); + + for (Py_ssize_t i = 0; i < argc; i++) { + PyObject *v = PyUnicode_FromWideChar(argv[i], -1); + if (v == NULL) { + Py_DECREF(list); + return NULL; } + PyList_SET_ITEM(list, i, v); } - return av; + return list; } void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) { - PyObject *av = makeargvobject(argc, argv); + if (argc < 1 || argv == NULL) { + /* Ensure at least one (empty) argument is seen */ + wchar_t* empty_argv[1] = {L""}; + argv = empty_argv; + argc = 1; + } + + PyObject *av = make_sys_argv(argc, argv); if (av == NULL) { Py_FatalError("no mem for sys.argv"); } @@ -2780,7 +2780,8 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) if (updatepath) { /* If argv[0] is not '-c' nor '-m', prepend argv[0] to sys.path. If argv[0] is a symlink, use the real path. */ - PyObject *argv0 = _PyPathConfig_ComputeArgv0(argc, argv); + const _PyWstrList argv_list = {.length = argc, .items = argv}; + PyObject *argv0 = _PyPathConfig_ComputeArgv0(&argv_list); if (argv0 == NULL) { Py_FatalError("can't compute path0 from argv"); } -- cgit v1.2.1