Skip to content

Backport PR #25088 on branch 0.24.x (Fixes Formatting Exception) #25343

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Bug Fixes

**I/O**

- Better handling of terminal printing when the terminal dimensions are not known (:issue:`25080`);
- Bug in reading a HDF5 table-format ``DataFrame`` created in Python 2, in Python 3 (:issue:`24925`)
- Bug in reading a JSON with ``orient='table'`` generated by :meth:`DataFrame.to_json` with ``index=False`` (:issue:`25170`)
- Bug where float indexes could have misaligned values when printing (:issue:`25061`)
Expand Down
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:
# 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
19 changes: 19 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 # noqa: F401

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,19 @@ 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(monkeypatch, size):
mock = pytest.importorskip("unittest.mock")

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

monkeypatch.setattr('subprocess.Popen', mock.Mock())
monkeypatch.setattr('subprocess.Popen.return_value.returncode', None)
monkeypatch.setattr(
'subprocess.Popen.return_value.communicate', communicate)
result = _get_terminal_size_tput()

assert result is None