From 49ec0e263b630cb97b16a1d9da55e70fef75c5d0 Mon Sep 17 00:00:00 2001 From: Dave Hunt Date: Fri, 8 Jun 2018 16:04:36 +0100 Subject: Use six to support Python 2 and 3 without using 2to3 --- blessings/__init__.py | 16 +++++++++------- blessings/tests.py | 11 ++++++----- 2 files changed, 15 insertions(+), 12 deletions(-) (limited to 'blessings') diff --git a/blessings/__init__.py b/blessings/__init__.py index 9e10ebc..0212ae9 100644 --- a/blessings/__init__.py +++ b/blessings/__init__.py @@ -1,10 +1,12 @@ """A thin, practical wrapper around terminal coloring, styling, and positioning""" +from __future__ import absolute_import from contextlib import contextmanager import curses from curses import setupterm, tigetnum, tigetstr, tparm from fcntl import ioctl +import six try: from io import UnsupportedOperation as IOUnsupportedOperation @@ -420,7 +422,7 @@ COMPOUNDABLES = (COLORS | 'shadow', 'standout', 'subscript', 'superscript'])) -class ParametrizingString(unicode): +class ParametrizingString(six.text_type): """A Unicode string which can be called to parametrize it as a terminal capability""" @@ -432,7 +434,7 @@ class ParametrizingString(unicode): "normal" capability. """ - new = unicode.__new__(cls, formatting) + new = six.text_type.__new__(cls, formatting) new._normal = normal return new @@ -461,7 +463,7 @@ class ParametrizingString(unicode): except TypeError: # If the first non-int (i.e. incorrect) arg was a string, suggest # something intelligent: - if len(args) == 1 and isinstance(args[0], basestring): + if len(args) == 1 and isinstance(args[0], six.string_types): raise TypeError( 'A native or nonexistent capability template received ' '%r when it was expecting ints. You probably misspelled a ' @@ -472,12 +474,12 @@ class ParametrizingString(unicode): raise -class FormattingString(unicode): +class FormattingString(six.text_type): """A Unicode string which can be called upon a piece of text to wrap it in formatting""" def __new__(cls, formatting, normal): - new = unicode.__new__(cls, formatting) + new = six.text_type.__new__(cls, formatting) new._normal = normal return new @@ -492,7 +494,7 @@ class FormattingString(unicode): return self + text + self._normal -class NullCallableString(unicode): +class NullCallableString(six.text_type): """A dummy callable Unicode to stand in for ``FormattingString`` and ``ParametrizingString`` @@ -500,7 +502,7 @@ class NullCallableString(unicode): """ def __new__(cls): - new = unicode.__new__(cls, u'') + new = six.text_type.__new__(cls, u'') return new def __call__(self, *args): diff --git a/blessings/tests.py b/blessings/tests.py index aff3a2f..c51b36c 100644 --- a/blessings/tests.py +++ b/blessings/tests.py @@ -10,13 +10,14 @@ xterm-256color exists. """ from __future__ import with_statement # Make 2.5-compatible +from __future__ import absolute_import from curses import tigetstr, tparm from functools import partial -from StringIO import StringIO import sys from nose import SkipTest from nose.tools import eq_ +from six import StringIO # This tests that __all__ is correct, since we use below everything that should # be imported: @@ -229,22 +230,22 @@ def test_nice_formatting_errors(): t = TestTerminal() try: t.bold_misspelled('hey') - except TypeError, e: + except TypeError as e: assert 'probably misspelled' in e.args[0] try: t.bold_misspelled(u'hey') # unicode - except TypeError, e: + except TypeError as e: assert 'probably misspelled' in e.args[0] try: t.bold_misspelled(None) # an arbitrary non-string - except TypeError, e: + except TypeError as e: assert 'probably misspelled' not in e.args[0] try: t.bold_misspelled('a', 'b') # >1 string arg - except TypeError, e: + except TypeError as e: assert 'probably misspelled' not in e.args[0] -- cgit v1.2.1