This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: redirect_* should also redirect C-level streams
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: xmorel
Priority: normal Keywords:

Created on 2021-05-17 12:32 by xmorel, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg393810 - (view) Author: Xavier Morel (xmorel) * Date: 2021-05-17 12:32
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.
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88323
2021-05-17 12:32:33xmorelcreate