You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
FYI, the size of the terminal associated with the C's stdout isn't related to the IDLE shell. For example, in Linux when I run IDLE from the GUI, the associated terminal size is 0x0.
On Windows, os.get_terminal_size uses the console API GetConsoleScreenBufferInfo. This can't work given IDLE has no attached console. Also, for a GUI app the Windows C runtime leaves the standard FILE streams uninitialized to an invalid file descriptor (-1), so Python's sys.__stdout__ is None. That's why you get an AttributeError complaining that NoneType (i.e. type(None)) has no attribute 'fileno'.
Currently shutil.get_terminal_size returns the fallback size when os.get_terminal_size(sys.__stdout__.fileno()) raises NameError or OSError. I think AttributeError and ValueError should be added to this list.
If one starts IDLE from a command-line console (python -m idlelib) or Python console (import idlelib.idle), sys.__stdout__ is the TextIOWraper for that console and .fileno() returns 1. .get_terminal_size() will then return the console size. The exception occurs when IDLE is started from an icon.
Implementing David's suggestion for shutil will be easy: insert just before the fileno call
if not sys.__stdout__: raise ValueError()
This is what os.get_terminal_size() raises on IDLE. Comments in the code in posixpath.c make it clear that this is intended guis and the low_level os function.
If one starts IDLE from a command-line console (python -m idlelib) or Python console (import idlelib.idle), sys.__stdout__ is the TextIOWraper for that console and .fileno() returns 1. .get_terminal_size() will then return the console size. The exception occurs when IDLE is started from an icon.
Implementing David's suggestion for shutil will be easy: insert just before the fileno call
if not sys.__stdout__: raise ValueError()
This is what os.get_terminal_size() raises on IDLE. Comments in the code in posixpath.c make it clear that this is intended guis and the low_level os function.
Uh, sorry for the noise. I see now that shutil was already patched by Victor and you in bpo-26801, so that it already works with IDLE started from an icon. So now I don't understand your last comment, "Patching shutil will help for pandas."
Activity
IsaacLevy commentedon Aug 24, 2015
OS: windows 7, python 3.4.3, tk version 8.6.1
os.get_terminal_size also fails.
>>> shutil.get_terminal_size() Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> shutil.get_terminal_size() File "C:\Python34\lib\shutil.py", line 1058, in get_terminal_size size = os.get_terminal_size(sys.__stdout__.fileno()) AttributeError: 'NoneType' object has no attribute 'fileno' >>> os.get_terminal_size() Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> os.get_terminal_size() ValueError: bad file descriptoreryksun commentedon Aug 24, 2015
FYI, the size of the terminal associated with the C's stdout isn't related to the IDLE shell. For example, in Linux when I run IDLE from the GUI, the associated terminal size is 0x0.
On Windows, os.get_terminal_size uses the console API GetConsoleScreenBufferInfo. This can't work given IDLE has no attached console. Also, for a GUI app the Windows C runtime leaves the standard FILE streams uninitialized to an invalid file descriptor (-1), so Python's sys.__stdout__ is None. That's why you get an AttributeError complaining that NoneType (i.e. type(None)) has no attribute 'fileno'.
Currently shutil.get_terminal_size returns the fallback size when os.get_terminal_size(sys.__stdout__.fileno()) raises NameError or OSError. I think AttributeError and ValueError should be added to this list.
IsaacLevy commentedon Aug 26, 2015
I guess users need to check standard streams for None. There's not many uses of stream attributes in core libs.
Maybe catch should be Exception -- since it's documented to return a fallback on error.
bitdancer commentedon Aug 26, 2015
I think checking for None would be better. Catching Exception might mask other errors that the user would want to know about.
terryjreedy commentedon Oct 26, 2017
If one starts IDLE from a command-line console (python -m idlelib) or Python console (import idlelib.idle), sys.__stdout__ is the TextIOWraper for that console and .fileno() returns 1. .get_terminal_size() will then return the console size. The exception occurs when IDLE is started from an icon.
Implementing David's suggestion for shutil will be easy: insert just before the fileno call
if not sys.__stdout__: raise ValueError()
This is what os.get_terminal_size() raises on IDLE. Comments in the code in posixpath.c make it clear that this is intended guis and the low_level os function.
This came up today on Stackoverflow when someone tried to use matplotlib.pyplot, which calls os.get_terminal_size, on IDLE.
https://stackoverflow.com/questions/46921087/pyplot-with-idle
(Patching shutil will not help for the os call.)
serhiy-storchaka commentedon Oct 26, 2017
This issue already is fixed in 3.5 (see bpo-26801). The Stackoverflow question is related to 3.4 which is too old for getting this fix.
I'm wondering if it is worth to patch shutil.get_terminal_size() in IDLE so that it will return the size of PyShell output window (in characters).
terryjreedy commentedon Oct 26, 2017
If one starts IDLE from a command-line console (python -m idlelib) or Python console (import idlelib.idle), sys.__stdout__ is the TextIOWraper for that console and .fileno() returns 1. .get_terminal_size() will then return the console size. The exception occurs when IDLE is started from an icon.
Implementing David's suggestion for shutil will be easy: insert just before the fileno call
if not sys.__stdout__: raise ValueError()
This is what os.get_terminal_size() raises on IDLE. Comments in the code in posixpath.c make it clear that this is intended guis and the low_level os function.
This came up today on Stackoverflow when someone tried to use matplotlib.pyplot, which calls os.get_terminal_size, on IDLE.
https://stackoverflow.com/questions/46921087/pyplot-with-idle
(Patching shutil will not help for the os call.)
serhiy-storchaka commentedon Oct 26, 2017
What do you want to do Terry?
serhiy-storchaka commentedon Oct 26, 2017
Patching shutil will help for pandas.
terryjreedy commentedon Oct 26, 2017
I am preparing a PR for shutil.get_window_size. Pyplot should probably call that instead of the os version.
serhiy-storchaka commentedon Oct 26, 2017
I think it is better to open a new issue for a new feature. The bug reported in this issue two years ago already is fixed.
terryjreedy commentedon Oct 26, 2017
Uh, sorry for the noise. I see now that shutil was already patched by Victor and you in bpo-26801, so that it already works with IDLE started from an icon. So now I don't understand your last comment, "Patching shutil will help for pandas."