Skip to content

Commit

Permalink
Do not fdopen() fds 1, 2 but use sys.* instead
Browse files Browse the repository at this point in the history
Apparently fdopening stdout and stderr is a bad idea and can cause
the interpreter to crash.  Use sys.stdout or sys.stderr, respectively,
when these fds are passed.

Bug: open-vcpkg/python-registry#10
Signed-off-by: Michał Górny <[email protected]>
  • Loading branch information
mgorny committed May 16, 2024
1 parent cc88681 commit 45db827
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions gpep517/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import functools
import importlib
import importlib.util
import io
import json
import logging
import os
Expand Down Expand Up @@ -35,8 +36,19 @@ def get_toml(path: Path):
return {}


def open_output(fd: int) -> io.FileIO:
"""Safely return file open for outputting into fd"""
if fd == 0:
raise RuntimeError("--output-fd 0 invalid")
elif fd == 1:
return contextlib.nullcontext(sys.stdout)
elif fd == 2:
return contextlib.nullcontext(sys.stderr)
return open(fd, "w")


def get_backend(args):
with os.fdopen(args.output_fd, "w") as out:
with open_output(args.output_fd) as out:
print(get_toml(args.pyproject_toml)
.get("build-system", {})
.get("build-backend", ""),
Expand Down Expand Up @@ -211,7 +223,7 @@ def safe_samefile(path, cwd):


def build_wheel(args):
with os.fdopen(args.output_fd, "w") as out:
with open_output(args.output_fd) as out:
print(build_wheel_impl(args, args.wheel_dir), file=out)
return 0

Expand Down

0 comments on commit 45db827

Please sign in to comment.