New issue 453: assertion rewriting fails with object whose __repr__ contains 
'{\n'
https://bitbucket.org/hpk42/pytest/issue/453/assertion-rewriting-fails-with-object

Tim Sampson:

Consider the following:


```
#!python

        resp = self.user.service.content.get_journal(initial_sync=True)
>       assert resp.count != 0

v2/test_journal.py:144: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ 

explanation = 'assert 0\n{0 = Headers: \nContent: {\n    "count": 0,\n
"items": [],\n    "total": 0,\n    "journal_max": 2158\n}.count\n} != 0'

    def format_explanation(explanation):  
        """This formats an explanation
    
        Normally all embedded newlines are escaped, however there are
        three exceptions: \n{, \n} and \n~.  The first two are intended
        cover nested explanations, see function and attribute explanations
        for examples (.visit_Call(), visit_Attribute()).  The last one is
        for when one explanation needs to span multiple lines, e.g. when
        displaying diffs.
        """
        explanation = _collapse_false(explanation)
        lines = _split_explanation(explanation)
>       result = _format_lines(lines)

/usr/local/lib/python2.6/dist-packages/_pytest/assertion/util.py:31: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/_ _ 

lines = ['assert 0', '{0 = Headers: \\nContent: {\\n    "count": 0,\\n
"items": [],\\n    "total": 0,\\n    "journal_max": 2158', '}.count', '} !=
0']

    def _format_lines(lines):
        """Format the individual lines
    
        This will replace the '{', '}' and '~' characters of our mini
        formatting language with the proper 'where ...', 'and ...' and ' +
        ...' text, taking care of indentation along the way.
    
        Return a list of formatted lines.
        """
        result = lines[:1]
        stack = [0]
        stackcnt = [0]
        for line in lines[1:]:
            if line.startswith('{'):
                if stackcnt[-1]:
                    s = u('and   ')
                else:
                    s = u('where ')
                stack.append(len(result))
                stackcnt[-1] += 1
                stackcnt.append(0)
                result.append(u(' +') + u('  ')*(len(stack)-1) + s +
                line[1:])
            elif line.startswith('}'):
                assert line.startswith('}')
                stack.pop()
                stackcnt.pop()
>               result[stack[-1]] += line[1:]
E               IndexError: list index out of range

/usr/local/lib/python2.6/dist-packages/_pytest/assertion/util.py:108:
/IndexError
```

I'm sure how this can work. As repr(resp) contains the pretty printed json body 
i.e. it contains '{\n' and '\n}' which apparently confuses format_explanation.

I could of course get around this by setting assert=plain and that works fine, 
but the bare asserts are so nice that I'd like to keep them. Also I guess I 
could change our code to not include the problematic chars in repr but I would 
rather not do that either.



_______________________________________________
pytest-commit mailing list
pytest-commit@python.org
https://mail.python.org/mailman/listinfo/pytest-commit

Reply via email to