summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2018-03-25 13:03:10 (GMT)
committerGitHub <noreply@github.com>2018-03-25 13:03:10 (GMT)
commitd5d9e02dd3c6df06a8dd9ce75ee9b52976420a8b (patch)
treec3c02b573a85d3a5caa61e1dd5188ba6bec36392 /Python
parentbc77eff8b96be4f035e665ab35c1d06e22f46491 (diff)
downloadcpython-d5d9e02dd3c6df06a8dd9ce75ee9b52976420a8b.zip
cpython-d5d9e02dd3c6df06a8dd9ce75ee9b52976420a8b.tar.gz
cpython-d5d9e02dd3c6df06a8dd9ce75ee9b52976420a8b.tar.bz2
bpo-33053: -m now adds *starting* directory to sys.path (GH-6231)
Historically, -m added the empty string as sys.path zero, meaning it resolved imports against the current working directory, the same way -c and the interactive prompt do. This changes the sys.path initialisation to add the *starting* working directory as sys.path[0] instead, such that changes to the working directory while the program is running will have no effect on imports when using the -m switch.
Diffstat (limited to 'Python')
-rw-r--r--Python/pathconfig.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/Python/pathconfig.c b/Python/pathconfig.c
index 6de5481..07aa01c 100644
--- a/Python/pathconfig.c
+++ b/Python/pathconfig.c
@@ -3,6 +3,7 @@
#include "Python.h"
#include "osdefs.h"
#include "internal/pystate.h"
+#include <wchar.h>
#ifdef __cplusplus
extern "C" {
@@ -255,11 +256,6 @@ Py_GetProgramName(void)
return _Py_path_config.program_name;
}
-
-#define _HAVE_SCRIPT_ARGUMENT(argc, argv) \
- (argc > 0 && argv0 != NULL && \
- wcscmp(argv0, L"-c") != 0 && wcscmp(argv0, L"-m") != 0)
-
/* Compute argv[0] which will be prepended to sys.argv */
PyObject*
_PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
@@ -267,6 +263,8 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
wchar_t *argv0;
wchar_t *p = NULL;
Py_ssize_t n = 0;
+ int have_script_arg = 0;
+ int have_module_arg = 0;
#ifdef HAVE_READLINK
wchar_t link[MAXPATHLEN+1];
wchar_t argv0copy[2*MAXPATHLEN+1];
@@ -278,11 +276,25 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
wchar_t fullpath[MAX_PATH];
#endif
-
argv0 = argv[0];
+ if (argc > 0 && argv0 != NULL) {
+ have_module_arg = (wcscmp(argv0, L"-m") == 0);
+ have_script_arg = !have_module_arg && (wcscmp(argv0, L"-c") != 0);
+ }
+
+ if (have_module_arg) {
+ #if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
+ _Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath));
+ argv0 = fullpath;
+ n = wcslen(argv0);
+ #else
+ argv0 = L".";
+ n = 1;
+ #endif
+ }
#ifdef HAVE_READLINK
- if (_HAVE_SCRIPT_ARGUMENT(argc, argv))
+ if (have_script_arg)
nr = _Py_wreadlink(argv0, link, MAXPATHLEN);
if (nr > 0) {
/* It's a symlink */
@@ -310,7 +322,7 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
#if SEP == '\\'
/* Special case for Microsoft filename syntax */
- if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
+ if (have_script_arg) {
wchar_t *q;
#if defined(MS_WINDOWS)
/* Replace the first element in argv with the full path. */
@@ -334,7 +346,7 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
}
}
#else /* All other filename syntaxes */
- if (_HAVE_SCRIPT_ARGUMENT(argc, argv)) {
+ if (have_script_arg) {
#if defined(HAVE_REALPATH)
if (_Py_wrealpath(argv0, fullpath, Py_ARRAY_LENGTH(fullpath))) {
argv0 = fullpath;