From 328ca5b94c145e33da886324cdcc2e02a68f960c Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Sun, 11 Jun 2023 11:28:54 +0800 Subject: [PATCH] fix: parsing setup.py error if it outputs to stdout (#1998) --- news/1995.bugfix.md | 1 + src/pdm/models/in_process/__init__.py | 8 ++++++-- src/pdm/models/in_process/parse_setup.py | 8 +++++++- 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 news/1995.bugfix.md diff --git a/news/1995.bugfix.md b/news/1995.bugfix.md new file mode 100644 index 0000000000..f773c99c9a --- /dev/null +++ b/news/1995.bugfix.md @@ -0,0 +1 @@ +Fix an error parsing `setup.py` if it prints something to stdout. diff --git a/src/pdm/models/in_process/__init__.py b/src/pdm/models/in_process/__init__.py index 7dd5a6aa2a..de00088acf 100644 --- a/src/pdm/models/in_process/__init__.py +++ b/src/pdm/models/in_process/__init__.py @@ -8,6 +8,7 @@ import json import os import subprocess +import tempfile from typing import Any, Generator from pdm.compat import resources_path @@ -47,5 +48,8 @@ def get_pep508_environment(executable: str) -> dict[str, str]: def parse_setup_py(executable: str, path: str) -> dict[str, Any]: """Parse setup.py and return the kwargs""" with _in_process_script("parse_setup.py") as script: - cmd = [executable, "-Es", script, path] - return json.loads(subprocess.check_output(cmd)) + _, outfile = tempfile.mkstemp(suffix=".json") + cmd = [executable, "-Es", script, path, outfile] + subprocess.check_call(cmd) + with open(outfile, "rb") as fp: + return json.load(fp) diff --git a/src/pdm/models/in_process/parse_setup.py b/src/pdm/models/in_process/parse_setup.py index 7e46cba9fd..a4d2ff3287 100644 --- a/src/pdm/models/in_process/parse_setup.py +++ b/src/pdm/models/in_process/parse_setup.py @@ -199,7 +199,13 @@ def parse_setup(path: str) -> Dict[str, Any]: return parsed +def json_default(o): + return "" + + if __name__ == "__main__": import json - print(json.dumps(parse_setup(sys.argv[1]))) + outfile = sys.argv[2] + with open(outfile, "w") as f: + json.dump(parse_setup(sys.argv[1]), f, default=json_default)