Skip to content

Commit ab2dc88

Browse files
committed
Move getdoc from utils.misc to utils.text
1 parent 3fe6e42 commit ab2dc88

File tree

5 files changed

+65
-65
lines changed

5 files changed

+65
-65
lines changed

src/robot/utils/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from .markupwriters import HtmlWriter, XmlWriter, NullMarkupWriter
5151
from .importer import Importer
5252
from .match import eq, Matcher, MultiMatcher
53-
from .misc import (getdoc, plural_or_not, printable_name, roundup, seq2str,
53+
from .misc import (plural_or_not, printable_name, roundup, seq2str,
5454
seq2str2)
5555
from .normalizing import lower, normalize, NormalizedDict
5656
from .platform import (IRONPYTHON, JYTHON, PY2, PY3, PYPY, PYTHON, UNIXY,
@@ -70,7 +70,7 @@
7070
from .setter import setter, SetterAwareType
7171
from .sortable import Sortable
7272
from .text import (cut_long_message, format_assign_message,
73-
pad_console_length, get_console_length, split_tags_from_doc,
74-
split_args_from_name_or_path)
73+
get_console_length, getdoc, pad_console_length,
74+
split_tags_from_doc, split_args_from_name_or_path)
7575
from .unic import prepr, unic
7676
from .utf8reader import Utf8Reader

src/robot/utils/misc.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515

1616
from __future__ import division
1717

18-
import inspect
1918
from operator import add, sub
2019

2120
from .platform import PY2
22-
from .robottypes import is_integer, is_unicode
21+
from .robottypes import is_integer
2322
from .unic import unic
2423

2524

@@ -125,13 +124,3 @@ def seq2str2(sequence):
125124
if not sequence:
126125
return '[ ]'
127126
return '[ %s ]' % ' | '.join(unic(item) for item in sequence)
128-
129-
130-
def getdoc(item):
131-
doc = inspect.getdoc(item) or u''
132-
if is_unicode(doc):
133-
return doc
134-
try:
135-
return doc.decode('UTF-8')
136-
except UnicodeDecodeError:
137-
return unic(doc)

src/robot/utils/text.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import inspect
1617
import os.path
1718
import re
1819

1920
from .charwidth import get_char_width
2021
from .misc import seq2str2
22+
from .robottypes import is_unicode
2123
from .unic import unic
2224

2325

@@ -149,3 +151,13 @@ def split_tags_from_doc(doc):
149151
doc = '\n'.join(lines[:-1]).rstrip()
150152
tags = [tag.strip() for tag in match.group(1).split(',')]
151153
return doc, tags
154+
155+
156+
def getdoc(item):
157+
doc = inspect.getdoc(item) or u''
158+
if is_unicode(doc):
159+
return doc
160+
try:
161+
return doc.decode('UTF-8')
162+
except UnicodeDecodeError:
163+
return unic(doc)

utest/utils/test_misc.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import unittest
22

3-
from robot.utils import IRONPYTHON, PY2
43
from robot.utils.asserts import assert_equal
5-
from robot.utils.misc import getdoc, printable_name, seq2str, roundup
4+
from robot.utils.misc import printable_name, seq2str, roundup
65

76

87
class TestRoundup(unittest.TestCase):
@@ -141,50 +140,5 @@ def test_printable_name_with_code_style(self):
141140
assert_equal(printable_name(inp, code_style=True), exp)
142141

143142

144-
class TestGetdoc(unittest.TestCase):
145-
146-
def test_no_doc(self):
147-
def func():
148-
pass
149-
assert_equal(getdoc(func), '')
150-
151-
def test_one_line_doc(self):
152-
def func():
153-
"""My documentation."""
154-
assert_equal(getdoc(func), 'My documentation.')
155-
156-
def test_multiline_doc(self):
157-
class Class:
158-
"""My doc.
159-
160-
In multiple lines.
161-
"""
162-
assert_equal(getdoc(Class), 'My doc.\n\nIn multiple lines.')
163-
assert_equal(getdoc(Class), getdoc(Class()))
164-
165-
def test_unicode_doc(self):
166-
class Class:
167-
def meth(self):
168-
u"""Hyv\xe4 \xe4iti!"""
169-
assert_equal(getdoc(Class.meth), u'Hyv\xe4 \xe4iti!')
170-
assert_equal(getdoc(Class.meth), getdoc(Class().meth))
171-
172-
if PY2:
173-
174-
def test_non_ascii_doc_in_utf8(self):
175-
def func():
176-
"""Hyv\xc3\xa4 \xc3\xa4iti!"""
177-
expected = u'Hyv\xe4 \xe4iti!' \
178-
if not IRONPYTHON else u'Hyv\xc3\xa4 \xc3\xa4iti!'
179-
assert_equal(getdoc(func), expected)
180-
181-
def test_non_ascii_doc_not_in_utf8(self):
182-
def func():
183-
"""Hyv\xe4 \xe4iti!"""
184-
expected = 'Hyv\\xe4 \\xe4iti!' \
185-
if not IRONPYTHON else u'Hyv\xe4 \xe4iti!'
186-
assert_equal(getdoc(func), expected)
187-
188-
189143
if __name__ == "__main__":
190144
unittest.main()

utest/utils/test_text.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import os
33
from os.path import abspath
44

5-
from robot.utils.asserts import *
6-
7-
from robot.utils.text import (cut_long_message, get_console_length,
5+
from robot.utils.asserts import assert_equal, assert_true
6+
from robot.utils import IRONPYTHON, PY2
7+
from robot.utils.text import (cut_long_message, get_console_length, getdoc,
88
pad_console_length, split_tags_from_doc,
99
split_args_from_name_or_path,
1010
_count_line_lengths, _MAX_ERROR_LINES,
@@ -275,5 +275,50 @@ def test_existing_path_with_colons(self):
275275
os.rmdir(path)
276276

277277

278+
class TestGetdoc(unittest.TestCase):
279+
280+
def test_no_doc(self):
281+
def func():
282+
pass
283+
assert_equal(getdoc(func), '')
284+
285+
def test_one_line_doc(self):
286+
def func():
287+
"""My documentation."""
288+
assert_equal(getdoc(func), 'My documentation.')
289+
290+
def test_multiline_doc(self):
291+
class Class:
292+
"""My doc.
293+
294+
In multiple lines.
295+
"""
296+
assert_equal(getdoc(Class), 'My doc.\n\nIn multiple lines.')
297+
assert_equal(getdoc(Class), getdoc(Class()))
298+
299+
def test_unicode_doc(self):
300+
class Class:
301+
def meth(self):
302+
u"""Hyv\xe4 \xe4iti!"""
303+
assert_equal(getdoc(Class.meth), u'Hyv\xe4 \xe4iti!')
304+
assert_equal(getdoc(Class.meth), getdoc(Class().meth))
305+
306+
if PY2:
307+
308+
def test_non_ascii_doc_in_utf8(self):
309+
def func():
310+
"""Hyv\xc3\xa4 \xc3\xa4iti!"""
311+
expected = u'Hyv\xe4 \xe4iti!' \
312+
if not IRONPYTHON else u'Hyv\xc3\xa4 \xc3\xa4iti!'
313+
assert_equal(getdoc(func), expected)
314+
315+
def test_non_ascii_doc_not_in_utf8(self):
316+
def func():
317+
"""Hyv\xe4 \xe4iti!"""
318+
expected = 'Hyv\\xe4 \\xe4iti!' \
319+
if not IRONPYTHON else u'Hyv\xe4 \xe4iti!'
320+
assert_equal(getdoc(func), expected)
321+
322+
278323
if __name__ == '__main__':
279324
unittest.main()

0 commit comments

Comments
 (0)