diff options
author | Erik Rose <erik@mozilla.com> | 2011-11-20 22:07:13 -0800 |
---|---|---|
committer | Erik Rose <erik@mozilla.com> | 2011-11-20 22:07:13 -0800 |
commit | 20c5f21c24509d10bd35b8f87a67651d91f3df4c (patch) | |
tree | 65acf3a3d1687ba1d59e4abbc2f7b2c93a93c2f4 | |
parent | 23d42b6ef30408a7558f565e247be452d68aeb35 (diff) | |
download | blessings-20c5f21c24509d10bd35b8f87a67651d91f3df4c.tar.gz |
fileno() exists on files all the time in Python 3, even if there is no file number. If there is none, it throws an exception when called.
-rw-r--r-- | blessings/__init__.py | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/blessings/__init__.py b/blessings/__init__.py index be71d0b..333d68e 100644 --- a/blessings/__init__.py +++ b/blessings/__init__.py @@ -2,6 +2,12 @@ from collections import defaultdict import curses from curses import tigetstr, setupterm, tparm from fcntl import ioctl +try: + from io import UnsupportedOperation as IOUnsupportedOperation +except ImportError: + class IOUnsupportedOperation(Exception): + """A dummy exception to take the place of Python 3's io.UnsupportedOperation one in Python 2""" + pass from os import isatty, environ import struct import sys @@ -59,9 +65,12 @@ class Terminal(object): """ if stream is None: stream = sys.__stdout__ - stream_descriptor = (stream.fileno() if hasattr(stream, 'fileno') - and callable(stream.fileno) - else None) + try: + stream_descriptor = (stream.fileno() if hasattr(stream, 'fileno') + and callable(stream.fileno) + else None) + except IOUnsupportedOperation: + stream_descriptor = None self.is_a_tty = stream_descriptor is not None and isatty(stream_descriptor) if self.is_a_tty or force_styling: # The desciptor to direct terminal initialization sequences to. |