-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Do we remember why utils.io.IOStream exists? #9141
Comments
I may have partially answered my own question 2 minutes after asking it. 😑 On Windows, we plug in pyreadline's ANSI-escape convertor at Now, is there a reason why we can't just do |
colorama, which I'm using in #9118, installs ansi->win converting streams to sys.stdout/stderr. So we should be able to get rid of these once that work lands, at least. |
I think you're right, in that we introduced that indirection layer to assist with Windows issues. I honestly don't 100% remember, though: I wrote most of that code, but it was a very long time ago, and I don't recall the specific rationale. Going through the cobwebs in my brain, though, I'm pretty sure that it gave us hooks we needed to play nice with the windows PyReadline machinery. It's perfectly conceivable that we could do without all that today, given changes to the surrounding tools. If that's the case, it would be great to get rid of that layer. We should, however, test pretty extensively on Windows before jettisoning it. |
May I propose a crazy idea. Instead of actually replacing Not even sure it is possible, and not even sure it is a good idea. |
Well, that's definitively a bad idea, but it works. from types import ModuleType
class Sys(ModuleType):
def __init__(self, mod):
setattr(self,'_mod', mod)
super().__init__(mod.__name__, mod.__doc__)
def __getattr__(self, name):
if name in {'stdin','stdout','stderr'}:
print('So we are getting to that?')
# you dont' want to recurse on `_mod` do you?
return object.__getattribute__(object.__getattribute__(self,'_mod'), name)
def __setattr__(self, name, value):
if name in {'stdin', 'stdout','stderr'}:
print('Or so you think...')
return
if name == '_mod':
object.__setattr__(self, name, value)
setattr(self._mod, name, value)
def __dir__(self):
return dir(self._mod)
def patchsys(sys):
if isinstance(sys, Sys):
print('Nop, not twice')
return
import sys as _sys
_sys.modules['sys'] = Sys(_sys) |
You're quite right, I do not like it. ;-) The sys module is pretty fundamental to Python, and I don't want to find out what we'd break by doing that. I also don't think it would work reliably - anything that had done Kudos for working out that it's possible, though. For more entertainment, have a look at this code that James Powell showed off at PyData London: https://github.com/dutc/rwatch |
+1 to not messing around with |
Yes, I was aware that was a bad idea. Sidenote, we have that in
So it seem that iostream does have a useful usage. |
I think that's more working around our use of IOStream than a reason for it ;-) |
IOStream has been deprecated pushing that for 6.0 for potential removal. |
@takluyver do you want to have a look at it, 6.0 material or bump to 7+ ? |
Bumped. It's not hard to leave the code there for another release cycle. |
BTW, I suspect this could be done now and all that cleaned up... Whatever we were doing to help Windows ~20y ago isn't needed anymore, I'm nearly certain :) |
Marked as Good First issue. |
IPython.utils.io
has stdin, stdout and stderr attributes which wrap the corresponding stream objects fromsys
in anIOStream
instance. Then certain odd places around the codebase, such as oinspect, doprint(..., file=io.stdout)
, while other places don't.I assume that this was originally supposed to provide an indirection layer so stdout/stderr could easily be redirected. However:
sys
- they have to, because not all printing is directed through theIPython.utils.io
objects. There are backup copies of the original objects as e.g.sys.__stdout__
, so replacingsys.stdout
is not destructive.sys.std*
is replaced,io.std*
still has a reference to the originalsys.std*
objects, so it doesn't respect the redirection. See issue Support changing sys.std* streams #8669, and I also just ran into this trying to use colorama in Terminal interface based on prompt_toolkit #9118.I'm proposing that we deprecate
io.std*
, and move all uses of it in our own code tosys.std*
. Does anyone know of a reason not to do this?Ping @fperez, because I'm pretty sure this code has been around longer than I've been on the project.
The text was updated successfully, but these errors were encountered: