summaryrefslogtreecommitdiff
path: root/blessings
diff options
context:
space:
mode:
authorjquast <contact@jeffquast.com>2013-11-04 21:27:41 -0800
committerjquast <contact@jeffquast.com>2013-11-04 21:27:41 -0800
commitc7abd20192b9381b464a649898f7bae3b8eb0122 (patch)
treee613d1f6ccc8115d15b2f3e130ecd9f725d9685a /blessings
parentb24c3737b3733ca5cc07a7ca201c9703407c210e (diff)
downloadblessings-c7abd20192b9381b464a649898f7bae3b8eb0122.tar.gz
resolve any 'must call (at least) setupterm() first' errors
avoid calling tparm when self.does_styling is False, which resolves issues with attempting to use things (such as nosetests progressive) where the terminal is not a tty. its also a "pokemon exception" and is emitted for a good reason, we certainly should not be calling tparm without calling setupterm() first !
Diffstat (limited to 'blessings')
-rw-r--r--blessings/__init__.py18
1 files changed, 7 insertions, 11 deletions
diff --git a/blessings/__init__.py b/blessings/__init__.py
index 7df617d..9d37889 100644
--- a/blessings/__init__.py
+++ b/blessings/__init__.py
@@ -294,7 +294,8 @@ class Terminal(object):
:arg num: The number, 0-15, of the color
"""
- return ParametrizingString(self._foreground_color, self.normal)
+ return (ParametrizingString(self._foreground_color, self.normal)
+ if self.does_styling else NullCallableString())
@property
def on_color(self):
@@ -303,7 +304,8 @@ class Terminal(object):
See ``color()``.
"""
- return ParametrizingString(self._background_color, self.normal)
+ return (ParametrizingString(self._background_color, self.normal)
+ if self.does_styling else NullCallableString())
@property
def number_of_colors(self):
@@ -325,11 +327,11 @@ class Terminal(object):
# don't name it after the underlying capability, because we deviate
# slightly from its behavior, and we might someday wish to give direct
# access to it.
- colors = tigetnum('colors') # Returns -1 if no color support, -2 if no
- # such cap.
+ # Returns -1 if no color support, -2 if no such capability.
+ colors = self.does_styling and tigetnum('colors') or -1
# self.__dict__['colors'] = ret # Cache it. It's not changing.
# (Doesn't work.)
- return colors if colors >= 0 else 0
+ return max(0, colors)
def _resolve_formatter(self, attr):
"""Resolve a sugary or plain capability name, color, or compound
@@ -439,12 +441,6 @@ class ParametrizingString(unicode):
parametrized = tparm(self.encode('utf-8'), *args).decode('utf-8')
return (parametrized if self._normal is None else
FormattingString(parametrized, self._normal))
- except curses.error:
- # Catch "must call (at least) setupterm() first" errors, as when
- # running simply `nosetests` (without progressive) on nose-
- # progressive. Perhaps the terminal has gone away between calling
- # tigetstr and calling tparm.
- return u''
except TypeError:
# If the first non-int (i.e. incorrect) arg was a string, suggest
# something intelligent: