Skip to content

Commit f4f05b2

Browse files
authored
Merge pull request bpython#656 from bpython/fix-653
fix weird boto docstrings
2 parents 5ffd93e + ab1cbec commit f4f05b2

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

bpython/curtsiesfrontend/replpainter.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def matches_lines(rows, columns, matches, current, config, format):
7474
if m != current
7575
else highlight_color(
7676
m.ljust(max_match_width))
77-
for m in matches[i:i+words_wide])
77+
for m in matches[i:i + words_wide])
7878
for i in range(0, len(matches), words_wide)]
7979

8080
logger.debug('match: %r' % current)
@@ -161,6 +161,12 @@ def formatted_argspec(funcprops, arg_pos, columns, config):
161161

162162

163163
def formatted_docstring(docstring, columns, config):
164+
if isinstance(docstring, bytes):
165+
docstring = docstring.decode('utf8')
166+
elif isinstance(docstring, str if py3 else unicode):
167+
pass
168+
else:
169+
return []
164170
color = func_for_letter(config.color_scheme['comment'])
165171
return sum(([color(x) for x in (display_linize(line, columns) if line else
166172
fmtstr(''))]
@@ -211,7 +217,7 @@ def paint_last_events(rows, columns, names, config):
211217
return fsarray([])
212218
width = min(max(len(name) for name in names), columns - 2)
213219
output_lines = []
214-
output_lines.append(config.left_top_corner+config.top_border * width +
220+
output_lines.append(config.left_top_corner + config.top_border * width +
215221
config.right_top_corner)
216222
for name in reversed(names[max(0, len(names) - (rows - 2)):]):
217223
output_lines.append(config.left_border + name[:width].center(width) +

bpython/test/test_curtsies_painting.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# coding: utf8
22
from __future__ import unicode_literals
33
import itertools
4-
import string
54
import os
5+
import pydoc
6+
import string
67
import sys
78
from contextlib import contextmanager
89

@@ -136,6 +137,43 @@ def test_formatted_docstring(self):
136137
'Also has side effects'])
137138
self.assertFSArraysEqualIgnoringFormatting(actual, expected)
138139

140+
def test_unicode_docstrings(self):
141+
"A bit of a special case in Python 2"
142+
# issue 653
143+
144+
def foo():
145+
u"åß∂ƒ"
146+
147+
actual = replpainter.formatted_docstring(
148+
foo.__doc__, 40, config=setup_config())
149+
expected = fsarray([u'åß∂ƒ'])
150+
self.assertFSArraysEqualIgnoringFormatting(actual, expected)
151+
152+
def test_nonsense_docstrings(self):
153+
for docstring in [123, {}, [], ]:
154+
try:
155+
replpainter.formatted_docstring(
156+
docstring, 40, config=setup_config())
157+
except Exception:
158+
self.fail('bad docstring caused crash: {!r}'.format(docstring))
159+
160+
def test_weird_boto_docstrings(self):
161+
# Boto does something like this.
162+
# botocore: botocore/docs/docstring.py
163+
class WeirdDocstring(str):
164+
# a mighty hack. See botocore/docs/docstring.py
165+
def expandtabs(self, tabsize=8):
166+
return u'asdfåß∂ƒ'.expandtabs(tabsize)
167+
168+
def foo():
169+
pass
170+
171+
foo.__doc__ = WeirdDocstring()
172+
wd = pydoc.getdoc(foo)
173+
actual = replpainter.formatted_docstring(wd, 40, config=setup_config())
174+
expected = fsarray([u'asdfåß∂ƒ'])
175+
self.assertFSArraysEqualIgnoringFormatting(actual, expected)
176+
139177
def test_paint_lasts_events(self):
140178
actual = replpainter.paint_last_events(4, 100, ['a', 'b', 'c'],
141179
config=setup_config())

0 commit comments

Comments
 (0)