Skip to content
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Bug Fixes

**I/O**

-
- Better handling of terminal printing when the terminal dimensions are not known (#25080);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use (:issue:`25080`)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Man, I'm extremely sorry, I only saw this now! Making changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There. Are there any other changes I should make?

-
-

Expand Down Expand Up @@ -96,4 +96,4 @@ Bug Fixes
Contributors
~~~~~~~~~~~~

.. contributors:: v0.24.1..v0.24.2
.. contributors:: v0.24.1..v0.24.2
20 changes: 14 additions & 6 deletions pandas/io/formats/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import os
import shutil
import subprocess

from pandas.compat import PY3

Expand Down Expand Up @@ -94,22 +95,29 @@ def _get_terminal_size_tput():
# get terminal width
# src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width
# -height-of-a-terminal-window

try:
import subprocess
proc = subprocess.Popen(["tput", "cols"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
output = proc.communicate(input=None)
cols = int(output[0])
output_cols = proc.communicate(input=None)
proc = subprocess.Popen(["tput", "lines"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
output = proc.communicate(input=None)
rows = int(output[0])
return (cols, rows)
output_rows = proc.communicate(input=None)
except OSError:
return None

try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a blank line & a comment here on what you are doing

# Some terminals (e.g. spyder) may report a terminal size of '',
# making the `int` fail.

cols = int(output_cols[0])
rows = int(output_rows[0])
return cols, rows
except (ValueError, IndexError):
return None


def _get_terminal_size_linux():
def ioctl_GWINSZ(fd):
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/io/formats/test_console.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import subprocess

import pytest

from pandas.io.formats.console import detect_console_encoding
from pandas.io.formats.terminal import _get_terminal_size_tput


class MockEncoding(object): # TODO(py27): replace with mock
Expand Down Expand Up @@ -72,3 +75,17 @@ def test_detect_console_encoding_fallback_to_default(monkeypatch, std, locale):
context.setattr('sys.stdout', MockEncoding(std))
context.setattr('sys.getdefaultencoding', lambda: 'sysDefaultEncoding')
assert detect_console_encoding() == 'sysDefaultEncoding'


@pytest.mark.parametrize("size", ['', ['']])
def test_terminal_unknown_dimensions(size):
mock = pytest.importorskip("unittest.mock")

def communicate(*args, **kwargs):
return size

with mock.patch.object(subprocess.Popen, "communicate",
side_effect=communicate):
result = _get_terminal_size_tput()

assert result is None