diff options
author | Sylvain <syt@logilab.fr> | 2008-01-10 13:06:34 +0100 |
---|---|---|
committer | Sylvain <syt@logilab.fr> | 2008-01-10 13:06:34 +0100 |
commit | 66d38b17019659f950a2924fb389f4e05c7527bb (patch) | |
tree | 88f8ac38c1725616950880284367b488b827ca84 | |
parent | 4c13f730d22d0e32eb4412fb550d409c00e2ca50 (diff) | |
download | logilab-common-66d38b17019659f950a2924fb389f4e05c7527bb.tar.gz |
textutils: fix a bug in normalize_[rest_]paragraph which may cause
infinite loop if an indent string containing some spaces is given
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | test/unittest_textutils.py | 16 | ||||
-rw-r--r-- | textutils.py | 16 |
3 files changed, 28 insertions, 8 deletions
@@ -4,7 +4,9 @@ ChangeLog for logilab.common 2008-01-08 -- 0.26.1 * optparser: support --version at main command level * testlib: added man page for pytest - + * textutils: fix a bug in normalize_[rest_]paragraph which may cause + infinite loop if an indent string containing some spaces is given + 2008-01-07 -- 0.26.0 * db: binarywrap support * modutils: new LazyObject class diff --git a/test/unittest_textutils.py b/test/unittest_textutils.py index 2043e84..535052b 100644 --- a/test/unittest_textutils.py +++ b/test/unittest_textutils.py @@ -70,6 +70,22 @@ aller discuter avec les autres si c'est utile ou necessaire.""") self.assertEquals(ulines(tu.normalize_rest_paragraph("""**nico**: toto""")), """**nico**: toto""") + def test_normalize_rest_paragraph2(self): + self.assertEquals(ulines(tu.normalize_rest_paragraph(""".. _tdm: http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Table-des-matieres/.20_adaa41fb-c125-4919-aece-049601e81c8e_0_0.pdf +.. _extrait: http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Extrait-du-livre/.20_d6eed0be-0d36-4384-be59-2dd09e081012_0_0.pdf""", indent='> ')), + """> .. _tdm: +> http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Table-des-matieres/.20_adaa41fb-c125-4919-aece-049601e81c8e_0_0.pdf +> .. _extrait: +> http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Extrait-du-livre/.20_d6eed0be-0d36-4384-be59-2dd09e081012_0_0.pdf""") + + def test_normalize_paragraph2(self): + self.assertEquals(ulines(tu.normalize_paragraph(""".. _tdm: http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Table-des-matieres/.20_adaa41fb-c125-4919-aece-049601e81c8e_0_0.pdf +.. _extrait: http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Extrait-du-livre/.20_d6eed0be-0d36-4384-be59-2dd09e081012_0_0.pdf""", indent='> ')), + """> .. _tdm: +> http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Table-des-matieres/.20_adaa41fb-c125-4919-aece-049601e81c8e_0_0.pdf +> .. _extrait: +> http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Extrait-du-livre/.20_d6eed0be-0d36-4384-be59-2dd09e081012_0_0.pdf""") + class NormalizeParagraphTC(TestCase): def test_known_values(self): diff --git a/textutils.py b/textutils.py index cf73f8d..88167a8 100644 --- a/textutils.py +++ b/textutils.py @@ -13,7 +13,7 @@ """Some text manipulation utility functions. :author: Logilab -:copyright: 2003-2007 LOGILAB S.A. (Paris, FRANCE) +:copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE) :contact: http://www.logilab.fr/ -- mailto:python-projects@logilab.org :group text formatting: normalize_text, normalize_paragraph, pretty_match,\ @@ -156,10 +156,11 @@ def normalize_paragraph(text, line_len=80, indent=''): indentation string """ text = _NORM_SPACES_RGX.sub(' ', text) + line_len = line_len - len(indent) lines = [] while text: - aline, text = splittext(indent + text.strip(), line_len) - lines.append(aline) + aline, text = splittext(text.strip(), line_len) + lines.append(indent + aline) return linesep.join(lines) def normalize_rest_paragraph(text, line_len=80, indent=''): @@ -185,20 +186,21 @@ def normalize_rest_paragraph(text, line_len=80, indent=''): """ toreport = '' lines = [] + line_len = line_len - len(indent) for line in text.splitlines(): - line = indent + toreport + _NORM_SPACES_RGX.sub(' ', line.strip()) + line = toreport + _NORM_SPACES_RGX.sub(' ', line.strip()) toreport = '' while len(line) > line_len: # too long line, need split line, toreport = splittext(line, line_len) - lines.append(line) + lines.append(indent + line) if toreport: - line = indent + toreport + ' ' + line = toreport + ' ' toreport = '' else: line = '' if line: - lines.append(line.strip()) + lines.append(indent + line.strip()) return linesep.join(lines) def splittext(text, line_len): |