fix: prevent crash when calling pybind11::print in a subsystem:windows build#6012
fix: prevent crash when calling pybind11::print in a subsystem:windows build#6012oxine wants to merge 2 commits into
Conversation
in line: file = module_::import("sys").attr("stdout"); the file will be none in a windows application build. won't be catched since there's no exception, and crashed in later access
|
I asked Cursor Opus 4.6 1M Thinking the questions below. I think we need at least (a) and (b) in the "Bottom line" response. However, this seems actually best to me: I.e. that amounts to steering users towards:
Two easy alternatives that don't run the danger of silent data loss. What is a "subsystem:windows" build?It refers to the MSVC linker flag The crash path is:
Is it legit to silently drop the print?It's defensible, but worth scrutinizing. A few angles: In favor
Against / things to consider
Bottom lineIt's a legitimate fix for the crash, and it matches CPython behavior, so it's not wrong. But it's the minimum viable fix. A stronger version might (a) add a brief comment explaining the Windows subsystem scenario, and (b) consider distinguishing between "user explicitly passed |
henryiii
left a comment
There was a problem hiding this comment.
I'd be okay with throwing an error only if a user passes file explicitly, but I think following CPython here and not doing anything if you can't print is better:
a) It's consistent with CPython
b) You might not "own" the code doing the print, so if it throws an error, you might not be able to fix it - throwing an error is still a "crash", just a catchable one
c) Print is very often used as a debug statement, you don't want a debug statement crashing (or throwing an error) just because you can't print.
d) Where do you plan to print the error to if you can't print?
fix: prevent crash when calling pybind11::print in a subsystem:windows build
Description
Calling pybind11::print() crash in MSVC v19.44 with /subsystem:windows given.
In line:
file = module_::import("sys").attr("stdout");the file will be none in a windows application build. won't be catched since there's no exception, and crashed in later access.Add check before accessing
fileto prevent crash.