From 86a44efeac98595dc0b319fdd42135fe32540194 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 22 Sep 2022 17:51:33 +0200
Subject: [PATCH 072/N] 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 https://github.com/msys2/msys2-runtime/pull/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 <naveen521kk@gmail.com>
---
 Lib/importlib/_bootstrap_external.py |  2 +-
 Lib/ntpath.py                        |  2 +-
 Python/pathconfig.c                  | 20 +++++++++++---------
 mingw_smoketests.py                  |  2 +-
 4 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py
index e9a7a84..520025e 100644
--- a/Lib/importlib/_bootstrap_external.py
+++ b/Lib/importlib/_bootstrap_external.py
@@ -43,7 +43,7 @@ if _MS_WINDOWS:
 else:
     path_separators = ['/']
 
-if 'MSYSTEM' in _os.environ:
+if _os.environ.get('MSYSTEM', ''):
     path_separators = path_separators[::-1]
 
 # Assumption made in _path_join()
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index 07ca7aa..0d4205e 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -20,7 +20,7 @@ import sys
 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:
diff --git a/Python/pathconfig.c b/Python/pathconfig.c
index 7a3711d..efa670a 100644
--- a/Python/pathconfig.c
+++ b/Python/pathconfig.c
@@ -8,7 +8,7 @@
 #include <wchar.h>
 
 #include "marshal.h"              // PyMarshal_ReadObjectFromString
-#include "osdefs.h"               // DELIM
+#include "osdefs.h"               // DELIM, SEP
 
 #ifdef MS_WINDOWS
 #  include <windows.h>            // GetFullPathNameW(), MAX_PATH
@@ -48,7 +48,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
@@ -63,12 +62,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;
 }
 
@@ -101,7 +102,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
@@ -116,12 +116,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;
 }
 
diff --git a/mingw_smoketests.py b/mingw_smoketests.py
index aa76659..2a8bd16 100644
--- a/mingw_smoketests.py
+++ b/mingw_smoketests.py
@@ -29,7 +29,7 @@ import os
 import unittest
 import sysconfig
 
-if "MSYSTEM" in os.environ:
+if os.environ.get("MSYSTEM", ""):
     SEP = "/"
 else:
     SEP = "\\"
