From 39dacf6e5b7909f3d6ca8cfc0af41950bc5a23de Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Thu, 1 Feb 2018 23:21:18 +0100 Subject: [PATCH] Fix Python 2 support. --- src/utils/str.py | 5 ++++- test/test_ircutils.py | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/utils/str.py b/src/utils/str.py index 2c990e352..142a950df 100644 --- a/src/utils/str.py +++ b/src/utils/str.py @@ -309,7 +309,10 @@ def perlVariableSubstitute(vars, text): def byteTextWrap(text, size, break_on_hyphens=False, break_long_words=True): """Similar to textwrap.wrap(), but considers the size of strings (in bytes) instead of their length (in characters).""" - words = textwrap.TextWrapper()._split_chunks(text) + try: + words = textwrap.TextWrapper()._split_chunks(text) + except AttributeError: # Python 2 + words = textwrap.TextWrapper()._split(text) words.reverse() # use it as a stack if sys.version_info[0] >= 3: words = [w.encode() for w in words] diff --git a/test/test_ircutils.py b/test/test_ircutils.py index ed90fc8ad..bcf43c52b 100644 --- a/test/test_ircutils.py +++ b/test/test_ircutils.py @@ -184,7 +184,12 @@ class FunctionsTestCase(SupyTestCase): self.assertTrue(max(map(pred, r)) <= 100) self.assertEqual(''.join(r), s) - s = (''.join([chr(0x1f527), chr(0x1f527), chr(0x1f527), ' ']) * 100)\ + if sys.version_info[0] < 3: + chr = unichr + u = lambda s: s.decode('utf8') + else: + u = lambda x: x + s = (u('').join([chr(0x1f527), chr(0x1f527), chr(0x1f527), u(' ')]) * 100)\ [0:-1] r = ircutils.wrap(s, 20)