Skip to content

Commit 8e04e37

Browse files
EternalLearner42Pingviinituutti
authored andcommitted
Fixes Formatting Exception (pandas-dev#25088)
1 parent 383d46b commit 8e04e37

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

doc/source/whatsnew/v0.24.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Bug Fixes
5353

5454
**I/O**
5555

56+
- Better handling of terminal printing when the terminal dimensions are not known (:issue:`25080`);
5657
- Bug in reading a HDF5 table-format ``DataFrame`` created in Python 2, in Python 3 (:issue:`24925`)
5758
- Bug in reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` (:issue:`25170`)
5859
- Bug where float indexes could have misaligned values when printing (:issue:`25061`)

pandas/io/formats/terminal.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import os
1717
import shutil
18+
import subprocess
1819

1920
from pandas.compat import PY3
2021

@@ -94,22 +95,29 @@ def _get_terminal_size_tput():
9495
# get terminal width
9596
# src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width
9697
# -height-of-a-terminal-window
98+
9799
try:
98-
import subprocess
99100
proc = subprocess.Popen(["tput", "cols"],
100101
stdin=subprocess.PIPE,
101102
stdout=subprocess.PIPE)
102-
output = proc.communicate(input=None)
103-
cols = int(output[0])
103+
output_cols = proc.communicate(input=None)
104104
proc = subprocess.Popen(["tput", "lines"],
105105
stdin=subprocess.PIPE,
106106
stdout=subprocess.PIPE)
107-
output = proc.communicate(input=None)
108-
rows = int(output[0])
109-
return (cols, rows)
107+
output_rows = proc.communicate(input=None)
110108
except OSError:
111109
return None
112110

111+
try:
112+
# Some terminals (e.g. spyder) may report a terminal size of '',
113+
# making the `int` fail.
114+
115+
cols = int(output_cols[0])
116+
rows = int(output_rows[0])
117+
return cols, rows
118+
except (ValueError, IndexError):
119+
return None
120+
113121

114122
def _get_terminal_size_linux():
115123
def ioctl_GWINSZ(fd):

pandas/tests/io/formats/test_console.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import subprocess # noqa: F401
2+
13
import pytest
24

35
from pandas.io.formats.console import detect_console_encoding
6+
from pandas.io.formats.terminal import _get_terminal_size_tput
47

58

69
class MockEncoding(object): # TODO(py27): replace with mock
@@ -72,3 +75,19 @@ def test_detect_console_encoding_fallback_to_default(monkeypatch, std, locale):
7275
context.setattr('sys.stdout', MockEncoding(std))
7376
context.setattr('sys.getdefaultencoding', lambda: 'sysDefaultEncoding')
7477
assert detect_console_encoding() == 'sysDefaultEncoding'
78+
79+
80+
@pytest.mark.parametrize("size", ['', ['']])
81+
def test_terminal_unknown_dimensions(monkeypatch, size):
82+
mock = pytest.importorskip("unittest.mock")
83+
84+
def communicate(*args, **kwargs):
85+
return size
86+
87+
monkeypatch.setattr('subprocess.Popen', mock.Mock())
88+
monkeypatch.setattr('subprocess.Popen.return_value.returncode', None)
89+
monkeypatch.setattr(
90+
'subprocess.Popen.return_value.communicate', communicate)
91+
result = _get_terminal_size_tput()
92+
93+
assert result is None

0 commit comments

Comments
 (0)