Skip to content

Instantly share code, notes, and snippets.

@roolebo
Last active August 29, 2015 14:15
Show Gist options
  • Save roolebo/ca816a26cdc0a8b17226 to your computer and use it in GitHub Desktop.
Save roolebo/ca816a26cdc0a8b17226 to your computer and use it in GitHub Desktop.
assertion rewriting fails on bs4.element.Tag
# vim: set fileencoding=utf-8 :
# related: https://bitbucket.org/hpk42/pytest/issue/453/assertion-rewriting-fails-with-object
#pip install beautifulsoup4
from bs4 import BeautifulSoup
def test_unicode():
soup = BeautifulSoup('<b class="boldest">Б - Бага</b>')
assert isinstance(soup.b, unicode)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../venv/lib/python2.7/site-packages/_pytest/assertion/util.py:32: in format_explanation
result = _format_lines(lines)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
lines = ['assert isinstance(<b class="boldest">\xd0\x91 - \xd0\x91\xd0\xb0\xd0\xb3\xd0\xb0</b>', '{<b class="boldest">\xd0\x91...x91\xd0\xb0\xd
0\xb3\xd0\xb0</b> = <b class="boldest">\xd0\x91 - \xd0\x91\xd0\xb0\xd0\xb3\xd0\xb0</b>.b', '}, unicode)']
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:])
E UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 19: ordinal not in range(128)
../venv/lib/python2.7/site-packages/_pytest/assertion/util.py:104: UnicodeDecodeError
===================================================== 1 failed, 2 passed in 0.84 seconds ======================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment