Skip to content

Commit b452419

Browse files
lazkanaveen521kk
andcommitted
Don't change os.sep with an empty MSYSTEM env var, not just a missing one
Up until now this didn't really happen when calling from cygwin because empty env vars were removed before Python would run. But msys2/msys2-runtime#101 changed that. To avoid breaking users that did something like MSYSTEM= python ... not only check that MSYSTEM isn't set but also that it isn't empty when deciding if os.sep/os.altsep should be switched. Also, guard the msystem env check to execute only on MINGW Co-authored-by: Naveen M K <[email protected]>
1 parent cf8362d commit b452419

File tree

6 files changed

+16
-14
lines changed

6 files changed

+16
-14
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
else:
4444
path_separators = ['/']
4545

46-
if 'MSYSTEM' in _os.environ:
46+
if _os.environ.get('MSYSTEM', ''):
4747
path_separators = path_separators[::-1]
4848

4949
# Assumption made in _path_join()

Lib/ntpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import genericpath
2222
from genericpath import *
2323

24-
if sys.platform == "win32" and "MSYSTEM" in os.environ:
24+
if sys.platform == "win32" and os.environ.get("MSYSTEM", ""):
2525
sep = '/'
2626
altsep = '\\'
2727
else:

Lib/pathlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class _WindowsFlavour(_Flavour):
115115

116116
sep = '\\'
117117
altsep = '/'
118-
if 'MSYSTEM' in os.environ:
118+
if os.environ.get('MSYSTEM', ''):
119119
sep, altsep = altsep, sep
120120
has_drv = True
121121
pathmod = ntpath

Python/pathconfig.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "Python.h"
44
#include "marshal.h" // PyMarshal_ReadObjectFromString
5-
#include "osdefs.h" // DELIM
5+
#include "osdefs.h" // DELIM, SEP
66
#include "pycore_initconfig.h"
77
#include "pycore_fileutils.h"
88
#include "pycore_pathconfig.h"
@@ -50,7 +50,6 @@ Py_StartsWithW(const wchar_t * str, const wchar_t * prefix)
5050
char
5151
Py_GetSepA(const char *name)
5252
{
53-
char* msystem = (char*)2; /* So that non Windows use / as sep */
5453
static char sep = '\0';
5554
#ifdef _WIN32
5655
/* https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247%28v=vs.85%29.aspx
@@ -65,12 +64,14 @@ Py_GetSepA(const char *name)
6564
if (sep != '\0')
6665
return sep;
6766
#if defined(__MINGW32__)
68-
msystem = Py_GETENV("MSYSTEM");
69-
#endif
70-
if (msystem != NULL)
67+
char* msystem = getenv("MSYSTEM");
68+
if (msystem != NULL && strcmp(msystem, "") != 0)
7169
sep = '/';
7270
else
7371
sep = '\\';
72+
#else
73+
sep = SEP;
74+
#endif
7475
return sep;
7576
}
7677

@@ -103,7 +104,6 @@ Py_NormalizeSepsA(char *name)
103104
wchar_t
104105
Py_GetSepW(const wchar_t *name)
105106
{
106-
char* msystem = (char*)2; /* So that non Windows use / as sep */
107107
static wchar_t sep = L'\0';
108108
#ifdef _WIN32
109109
/* https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247%28v=vs.85%29.aspx
@@ -118,12 +118,14 @@ Py_GetSepW(const wchar_t *name)
118118
if (sep != L'\0')
119119
return sep;
120120
#if defined(__MINGW32__)
121-
msystem = Py_GETENV("MSYSTEM");
122-
#endif
123-
if (msystem != NULL)
121+
char* msystem = getenv("MSYSTEM");
122+
if (msystem != NULL && strcmp(msystem, "") != 0)
124123
sep = L'/';
125124
else
126125
sep = L'\\';
126+
#else
127+
sep = SEP;
128+
#endif
127129
return sep;
128130
}
129131

mingw_smoketests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import unittest
3030
import sysconfig
3131

32-
if "MSYSTEM" in os.environ:
32+
if os.environ.get("MSYSTEM", ""):
3333
SEP = "/"
3434
else:
3535
SEP = "\\"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_platform():
8181
# as otherwise all the io redirection will fail.
8282
# Arguably, this could happen inside the real os.system
8383
# rather than this monkey patch.
84-
if sys.platform == "win32" and "MSYSTEM" in os.environ:
84+
if sys.platform == "win32" and os.environ.get("MSYSTEM", ""):
8585
os_system = os.system
8686
def msys_system(command):
8787
command_in_sh = 'sh.exe -c "%s"' % command.replace("\\", "\\\\")

0 commit comments

Comments
 (0)