Skip to content

Commit

Permalink
Don't change os.sep with an empty MSYSTEM env var, not just a missing…
Browse files Browse the repository at this point in the history
… 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]>
  • Loading branch information
lazka and naveen521kk committed Aug 25, 2023
1 parent 1a17213 commit 6d4f179
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
else:
path_separators = ['/']

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

# Assumption made in _path_join()
Expand Down
2 changes: 1 addition & 1 deletion Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import genericpath
from genericpath import *

if sys.platform == "win32" and "MSYSTEM" in os.environ:
if sys.platform == "win32" and os.environ.get("MSYSTEM", ""):
sep = '/'
altsep = '\\'
else:
Expand Down
2 changes: 1 addition & 1 deletion Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class _WindowsFlavour(_Flavour):

sep = '\\'
altsep = '/'
if 'MSYSTEM' in os.environ:
if os.environ.get('MSYSTEM', ''):
sep, altsep = altsep, sep
has_drv = True
pathmod = ntpath
Expand Down
20 changes: 11 additions & 9 deletions Python/pathconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Python.h"
#include "marshal.h" // PyMarshal_ReadObjectFromString
#include "osdefs.h" // DELIM
#include "osdefs.h" // DELIM, SEP
#include "pycore_initconfig.h"
#include "pycore_fileutils.h"
#include "pycore_pathconfig.h"
Expand Down Expand Up @@ -50,7 +50,6 @@ Py_StartsWithW(const wchar_t * str, const wchar_t * prefix)
char
Py_GetSepA(const char *name)
{
char* msystem = (char*)2; /* So that non Windows use / as sep */
static char sep = '\0';
#ifdef _WIN32
/* https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247%28v=vs.85%29.aspx
Expand All @@ -65,12 +64,14 @@ Py_GetSepA(const char *name)
if (sep != '\0')
return sep;
#if defined(__MINGW32__)
msystem = Py_GETENV("MSYSTEM");
#endif
if (msystem != NULL)
char* msystem = getenv("MSYSTEM");
if (msystem != NULL && strcmp(msystem, "") != 0)
sep = '/';
else
sep = '\\';
#else
sep = SEP;
#endif
return sep;
}

Expand Down Expand Up @@ -103,7 +104,6 @@ Py_NormalizeSepsA(char *name)
wchar_t
Py_GetSepW(const wchar_t *name)
{
char* msystem = (char*)2; /* So that non Windows use / as sep */
static wchar_t sep = L'\0';
#ifdef _WIN32
/* https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247%28v=vs.85%29.aspx
Expand All @@ -118,12 +118,14 @@ Py_GetSepW(const wchar_t *name)
if (sep != L'\0')
return sep;
#if defined(__MINGW32__)
msystem = Py_GETENV("MSYSTEM");
#endif
if (msystem != NULL)
char* msystem = getenv("MSYSTEM");
if (msystem != NULL && strcmp(msystem, "") != 0)
sep = L'/';
else
sep = L'\\';
#else
sep = SEP;
#endif
return sep;
}

Expand Down
2 changes: 1 addition & 1 deletion mingw_smoketests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import unittest
import sysconfig

if "MSYSTEM" in os.environ:
if os.environ.get("MSYSTEM", ""):
SEP = "/"
else:
SEP = "\\"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_platform():
# as otherwise all the io redirection will fail.
# Arguably, this could happen inside the real os.system
# rather than this monkey patch.
if sys.platform == "win32" and "MSYSTEM" in os.environ:
if sys.platform == "win32" and os.environ.get("MSYSTEM", ""):
os_system = os.system
def msys_system(command):
command_in_sh = 'sh.exe -c "%s"' % command.replace("\\", "\\\\")
Expand Down

0 comments on commit 6d4f179

Please sign in to comment.