Skip to content

output abnormal after reload(sys) #567

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
twz915 opened this issue Sep 25, 2015 · 10 comments
Closed

output abnormal after reload(sys) #567

twz915 opened this issue Sep 25, 2015 · 10 comments
Labels

Comments

@twz915
Copy link

twz915 commented Sep 25, 2015

>>> import sys
>>> id(sys)
4545665968
>>> reload(sys)
>>> id(sys)
>>> 

bpython output is missing or abnormal. I've tested that original Python shell or IPython, both of them work fine.

@thomasballinger
Copy link
Member

Whoa, pretty weird! Looks like reloading sys resets sys.stdout, so it's no longer our faked stdout.

@thomasballinger
Copy link
Member

@twz915 Is there a use case for which this is important? It's the goal of bpython to behave the same as the interactive Python interpreter, so this is a bug, but one that would take some effort to fix.

If it's important, we could use a fake reload() function that sets sys.stdout and sys.stderr again, though this makes the magic we do less transparent.

@twz915
Copy link
Author

twz915 commented Nov 30, 2015

Not that important, but useful when setdefaultencoding

    >>> import sys
    >>> sys.setdefaultencoding('gbk')
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    AttributeError: 'module' object has no attribute 'setdefaultencoding'
    >>> reload(sys)
    >>> sys.setdefaultencoding('gbk')
    >>> 

I like bpython very much, thanks for your hard work!

@twz915
Copy link
Author

twz915 commented Nov 30, 2015

You could design the fake reload() just when reload(sys) sets sys.out and sys.stderr again. To other module except sys, it is just a wrapper which return original reload function.

@thomasballinger
Copy link
Member

I'm on the fence, I think monkeypatching in a new reload function is reasonable, but found in the reload docs is says

It is generally not very useful to reload built-in or dynamically loaded modules. Reloading sys, main, builtins and other key modules is not recommended. In many cases extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded.

#593 fixes this in the way described above.

@thomasballinger
Copy link
Member

@sebastinas Any possible issues with this?

@twz915 I don't understand some of this: why is setdefaultencoding not available until sys is reloaded? Because I'm somewhat ignorant about encoding issues, could you describe why this is useful to you? Does #593 look good to you?

@twz915
Copy link
Author

twz915 commented Dec 23, 2015

@thomasballinger Thank you for all your efforts at first.
why is setdefaultencoding not available until sys is reloaded copy from this answer:

This function is only available at Python start-up time, when Python scans the environment. It has to be called in a system-wide module, sitecustomize.py, After this module has been evaluated, the setdefaultencoding() function is removed from the sys module.

Why I met the issue is that I want to learn how reload() works in Python, for example,

>>> import os
>>> id(os)
4326972336
>>> reload(os)
<module 'os' from '/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/V
ersions/2.7/lib/python2.7/os.pyc'>
>>> id(os)
4326972336
>>> 

Notice that the result of id(module) is not changed after reload(module).

Generally speaking, we rarely use reload(sys). A case is that we want to change default encoding is not utf-8 on some Operating System to utf-8 temporarily.
It seems that Python 3 has removed reload() feature entirely.

Python 3.5.1 (default, Dec  9 2015, 09:26:35) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> reload
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'reload' is not defined
>>> 

I think it's better everything is OK after reload(sys), although we seldom use it.

@thomasballinger
Copy link
Member

In Python 3 this isn't a problem -- the attributes of sys are preserved on importlib.reload(sys), and setdefaultencoding is gone anyway. Because of this I'm only monkeypatching reload in python 2.

@thomasballinger
Copy link
Member

Current status of this: PR 593 fixes this, but we're not sure it's worth the complexity.

@thomasballinger
Copy link
Member

closing as won't fix: this is only an issue in Python 2 when doing something not recommended by the docs, and my fix added too much complexity to the code. Thanks for reporting though!

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

Successfully merging a pull request may close this issue.

2 participants