From 20c5f21c24509d10bd35b8f87a67651d91f3df4c Mon Sep 17 00:00:00 2001 From: Erik Rose Date: Sun, 20 Nov 2011 22:07:13 -0800 Subject: 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. --- blessings/__init__.py | 15 ++++++++++++--- 1 file 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. -- cgit v1.2.1