Blob Blame History Raw
From 1f55c9e6eeaf8501732e8a5c7306eecae43c26fb Mon Sep 17 00:00:00 2001
From: Arun Babu Neelicattu <arun.neelicattu@gmail.com>
Date: Mon, 14 Jun 2021 14:10:31 +0200
Subject: [PATCH] env: ignore warnings when executing python scripts

When executing python scripts with an intent to deserialise via
`json.loads` we need to ensure we ignore warnings. This is required
because, warnings can cause a `JSONDecodeError`.

https://github.com/python-poetry/poetry/pull/3895
---
 poetry/utils/env.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/poetry/utils/env.py b/poetry/utils/env.py
index 681027a..3563e62 100644
--- a/poetry/utils/env.py
+++ b/poetry/utils/env.py
@@ -1042,6 +1042,10 @@ class Env(object):
         cmd = pip + list(args)
         return self._run(cmd, **kwargs)
 
+    def run_python_script(self, content: str, **kwargs: Any) -> str:
+                return self.run("python", "-W", "ignore", "-", input_=content,
+                                **kwargs)
+
     def _run(self, cmd, **kwargs):
         """
         Run a command inside the Python environment.
@@ -1244,16 +1248,16 @@ class VirtualEnv(Env):
         # In this case we need to get sys.base_prefix
         # from inside the virtualenv.
         if base is None:
-            self._base = Path(self.run("python", "-", input_=GET_BASE_PREFIX).strip())
+            self._base = Path(self.run_python_script(GET_BASE_PREFIX).strip())
 
     @property
     def sys_path(self):  # type: () -> List[str]
-        output = self.run("python", "-", input_=GET_SYS_PATH)
+        output = self.run_python_script(GET_SYS_PATH)
 
         return json.loads(output)
 
     def get_version_info(self):  # type: () -> Tuple[int]
-        output = self.run("python", "-", input_=GET_PYTHON_VERSION)
+        output = self.run_python_script(GET_PYTHON_VERSION)
 
         return tuple([int(s) for s in output.strip().split(".")])
 
@@ -1291,12 +1295,12 @@ class VirtualEnv(Env):
             """
         )
 
-        output = self.run("python", "-", input_=script)
+        output = self.run_python_script(script)
 
         return [Tag(*t) for t in json.loads(output)]
 
     def get_marker_env(self):  # type: () -> Dict[str, Any]
-        output = self.run("python", "-", input_=GET_ENVIRONMENT_INFO)
+        output = self.run_python_script(GET_ENVIRONMENT_INFO)
 
         return json.loads(output)
 
@@ -1309,7 +1313,7 @@ class VirtualEnv(Env):
         return Version.parse(m.group(1))
 
     def get_paths(self):  # type: () -> Dict[str, str]
-        output = self.run("python", "-", input_=GET_PATHS)
+        output = self.run_python_script(GET_PATHS)
 
         return json.loads(output)
 
-- 
2.31.1