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

redirect_* should also redirect C-level streams #88323

Open
masklinn mannequin opened this issue May 17, 2021 · 1 comment
Open

redirect_* should also redirect C-level streams #88323

masklinn mannequin opened this issue May 17, 2021 · 1 comment
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@masklinn
Copy link
Mannequin

masklinn mannequin commented May 17, 2021

BPO 44157
Nosy @masklinn

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-05-17.12:32:33.510>
labels = ['type-feature', 'library', '3.9']
title = 'redirect_* should also redirect C-level streams'
updated_at = <Date 2021-05-17.12:32:33.510>
user = 'https://github.com/masklinn'

bugs.python.org fields:

activity = <Date 2021-05-17.12:32:33.510>
actor = 'xmorel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2021-05-17.12:32:33.510>
creator = 'xmorel'
dependencies = []
files = []
hgrepos = []
issue_num = 44157
keywords = []
message_count = 1.0
messages = ['393810']
nosy_count = 1.0
nosy_names = ['xmorel']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue44157'
versions = ['Python 3.9']

@masklinn
Copy link
Mannequin Author

masklinn mannequin commented May 17, 2021

In 3.4 (resp. 3.5), redirect_stdout and redirect_stderr were added to provide easy and reentrant stream redirection. Although that is documented, it seems like a waste that they only redirect the high-level sys.std* streams: those are already pretty easy to redirect by simply setting the corresponding attribute, which is essentially what the contextmanager does. However,

  • that does not work if the writer has imported the original stream object (from sys import stderr)
  • that does not work if the writer bypasses or is not aware of the Python layer e.g. a native library

In that case, the user still has to deal with dup/dup2 dances in order to redirect the underlying fds, which is significantly more error-prone (and verbose) than intercepting the high-level sys.std* ever was.

On the other hand, it's also less expressive as it requires an actual fd, rather than a Python-level file-like object. So I can understand not doing it by default.

But still, I think I feel like it'd be very useful to either expand the existing context managers to perform fd redirection, or have a separate context manager able to redirect arbitrary fds.

@masklinn masklinn mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 17, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

0 participants