Skip to content

Make stdio a feature #5420

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

Closed
wants to merge 2 commits into from
Closed

Make stdio a feature #5420

wants to merge 2 commits into from

Conversation

danielstuart14
Copy link
Contributor

@danielstuart14 danielstuart14 commented Oct 3, 2024

When embedding RustPython into a graphical windows application (where windows_subsystem = "windows"), it fails to create the RustPython VM.

This happens because the VM will try to open stdin/stdout/stderr when initializing, but stdio isn't available for /SUBSYSTEM:WINDOWS apps.

This commit makes stdio a feature, so it can be disabled when needed.

https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute
https://learn.microsoft.com/en-us/cpp/build/reference/subsystem-specify-subsystem?view=msvc-170 https://asawicki.info/news_1768_ways_to_print_and_capture_text_output_of_a_process

When embedding RustPython into a graphical windows application (where
windows_subsystem = "windows"), it fails to create the RustPython VM.

This happens because the VM will try to open stdin/stdout/stderr when
initializing, but stdio isn't available for /SUBSYSTEM:WINDOWS apps.

This commit makes stdio a feature, so it can be disabled when needed.

https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute
https://learn.microsoft.com/en-us/cpp/build/reference/subsystem-specify-subsystem?view=msvc-170
https://asawicki.info/news_1768_ways_to_print_and_capture_text_output_of_a_process
@coolreader18
Copy link
Member

Probably a better solution would be to implement the _WindowsConsoleIO type as CPython does - it seems like it intercepts opening an stdio fd, and instead of using the CRT functions uses the win32 console API.

@coolreader18
Copy link
Member

Although for now, this is probably blocking you, so a feature flag is fine. Could you make it an off-by-default no-stdio flag, though? Probably just on rustpython-vm. Given this is pretty niche, it'd be good for it to only be disabled when desired, rather than whenever default-features = false is specified.

@youknowone
Copy link
Member

Since stdio fits better than no-stdio by common Rust convention, leaving a comment about the decision will be better.
@coolreader18 will you suggest the comment to add?

@danielstuart14
Copy link
Contributor Author

danielstuart14 commented Oct 3, 2024

Probably a better solution would be to implement the _WindowsConsoleIO type as CPython does - it seems like it intercepts opening an stdio fd, and instead of using the CRT functions uses the win32 console API.

It makes sense, but wouldn't it make it mandatory to create a console to run the VM? I guess we could go to this route, which would fix using RustPython with win32 GUI apps, but I think it would still be nice to have a feature to control if we want a console to be created.

For us, at least, we don't want the user to see the console, as the python code is only used to embed dynamic logic inside our app.

I'm open to do any changes you guys find necessary!

@danielstuart14 danielstuart14 force-pushed the main branch 2 times, most recently from e8d8474 to 28b97b9 Compare October 14, 2024 02:35
More in line with what CPython does
@danielstuart14
Copy link
Contributor Author

Changed it to set stdios to None, as I found that some imports would fail (ex: subprocess) if not set, and that seems to be an expected value (CPython expects stdin/stdout/stderr to be TextIO or None).

It is possible for the user to still set the stdios to a custom file if needed, after initializing the vm:
image

@youknowone
Copy link
Member

@coolreader18 could you follow this up? I am not good at windows dev

Copy link
Member

@coolreader18 coolreader18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the long delay. This seems reasonable to me; thank you!

coolreader18 pushed a commit that referenced this pull request Apr 18, 2025
@coolreader18
Copy link
Member

Did a manual squash-merge (dabd93c) - sorry again for the delay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants