Skip to content

Commit

Permalink
fix: parsing setup.py error if it outputs to stdout (#1998)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Jun 11, 2023
1 parent bdfc380 commit 328ca5b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions news/1995.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix an error parsing `setup.py` if it prints something to stdout.
8 changes: 6 additions & 2 deletions src/pdm/models/in_process/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import json
import os
import subprocess
import tempfile
from typing import Any, Generator

from pdm.compat import resources_path
Expand Down Expand Up @@ -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)
8 changes: 7 additions & 1 deletion src/pdm/models/in_process/parse_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ def parse_setup(path: str) -> Dict[str, Any]:
return parsed


def json_default(o):
return "<unserializable object>"


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)

0 comments on commit 328ca5b

Please sign in to comment.