Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parsing setup.py error if it outputs to stdout #1998

Merged
merged 1 commit into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)