-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spurious backslashes in the "diff" output #1347
Comments
at first glance this looks like a difference in escaping |
I haven't looked properly yet, but the backslashes are inserted here on 2.7: pytest/_pytest/assertion/util.py Line 170 in 8639bf7
cc @flub |
Since that fix introduced by @flub, we've implemented _escape_strings, which which attempts to escape non-ascii strings, but keeps ascii strings intact. |
At the PyConUK sprint @mattduck was looking at this and we discussed this in detail and came to a similar approach about trying to decode it and only if it fails to escape strings. I wasn't aware of
Main difference is that it tries to use the system encoding instead of just ascii in the initial unicode conversion attempt. Otherwise I don't really understand Secondly I wasn't aware of the |
It should also be pointed out that while the bug is valid as far as pytest is concerned the test case is actually just invalid. You can't and should not compare a unicode string to a bytestring. In py3 it would rightfully blow up. If this comparison is important to you then you should first convert one of the two sides to the type of the other using an explicit encoding or decoding step with the desired codec. |
@flub thanks for summarising! I hadn't had the chance yet. I agree that it's not the best test case. In python3 any bytes vs unicode comparison returns But in 2.7 a bytes vs unicode comparision returns pytest/_pytest/assertion/util.py Line 171 in 8639bf7
One other thing that @flub and I discussed is whether it's worthwhile adding a warning in the case of bytes vs str in 2.7, making it clear that the diff could be misleading if the bytestring contains non-ASCII bytes. Because the pytest diff could highlight characters that were added in its attempt to represent the bytes (which is what happens in this issue with the backslash). |
Just confirming that this still happens under pytest 3.9.1 on Python 2.7.15 |
FTR: created |
hello, first off thank you for the issue! python 2.x support has ended for pytest core. we've decided in #7296 to close the python-2-specific issues to free up some space in our backlog. however, in accordance to our python 2.7 and 3.4 support community patches will still be accepted to the |
How to demonstrate a bug in pytest==2.8.7
virtualenv venv2.Darwin
./venv2.Darwin/bin/pip install --upgrade pip
./venv2.Darwin/bin/pip install pytest
./venv2.Darwin/bin/py.test test_repro_pytest_bug.py
Note the output:
The bug is in lines four through six of the output above. Those lines suggest that the
truncated
string contains backslashes whereas theoriginal
string doesn’t. But in fact, neither string contains backslashes.repro.zip
The text was updated successfully, but these errors were encountered: