Skip to content

Commit aa2ffb9

Browse files
committed
Merge fix for issue 731 from pytest-2.7
2 parents d6670bd + 7f554f5 commit aa2ffb9

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
2.7.1.dev (compared to 2.7.0)
5656
-----------------------------
5757

58+
- fix issue731: do not get confused by the braces which may be present
59+
and unbalanced in an object's repr while collapsing False
60+
explanations. Thanks Carl Meyer for the report and test case.
61+
5862
- fix issue553: properly handling inspect.getsourcelines failures in
5963
FixtureLookupError which would lead to to an internal error,
6064
obfuscating the original problem. Thanks talljosh for initial

_pytest/assertion/util.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ def _collapse_false(explanation):
4545
if where == -1:
4646
break
4747
level = 0
48+
prev_c = explanation[start]
4849
for i, c in enumerate(explanation[start:]):
49-
if c == "{":
50+
if prev_c + c == "\n{":
5051
level += 1
51-
elif c == "}":
52+
elif prev_c + c == "\n}":
5253
level -= 1
5354
if not level:
5455
break
56+
prev_c = c
5557
else:
5658
raise AssertionError("unbalanced braces: %r" % (explanation,))
5759
end = start + i

testing/test_assertrewrite.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,3 +663,24 @@ def test_loader():
663663
result.stdout.fnmatch_lines([
664664
"* 1 passed*",
665665
])
666+
667+
668+
def test_issue731(testdir):
669+
testdir.makepyfile("""
670+
class LongReprWithBraces(object):
671+
def __repr__(self):
672+
return 'LongReprWithBraces({' + ('a' * 80) + '}' + ('a' * 120) + ')'
673+
674+
def some_method(self):
675+
return False
676+
677+
def test_long_repr():
678+
obj = LongReprWithBraces()
679+
assert obj.some_method()
680+
""")
681+
result = testdir.runpytest()
682+
assert 'unbalanced braces' not in result.stdout.str()
683+
684+
685+
def test_collapse_false_unbalanced_braces():
686+
util._collapse_false('some text{ False\n{False = some more text\n}')

0 commit comments

Comments
 (0)