From 111a6e6f29918ee266bc89ecc4f27bb7a20d887e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Thu, 16 May 2024 16:35:51 +0200 Subject: [PATCH] Do not fdopen() fds 1, 2 but use sys.* instead MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/open-vcpkg/python-registry/issues/10 Signed-off-by: Michał Górny --- gpep517/__main__.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/gpep517/__main__.py b/gpep517/__main__.py index d519c75..8aa0e74 100644 --- a/gpep517/__main__.py +++ b/gpep517/__main__.py @@ -3,6 +3,7 @@ import functools import importlib import importlib.util +import io import json import logging import os @@ -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 sys.stdout + elif fd == 2: + return 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", ""), @@ -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