-
-
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
Fix exception formatting while importing test modules #2337
Fix exception formatting while importing test modules #2337
Conversation
_pytest/compat.py
Outdated
@@ -237,5 +237,7 @@ def safe_str(v): | |||
try: | |||
return str(v) | |||
except UnicodeError: | |||
if not isinstance(v, unicode): | |||
v = unicode(v) | |||
errors = 'replace' | |||
return v.encode('ascii', errors) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems good for me, the only thing is that I find it a bit weird that safe_str looses information if it has non-ascii chars (i.e.: usually the v.encode() would be something as v.encode('utf-8', 'replace') or v.encode(sys.getdefaultencoding(), 'replace'))
Anyways, this isn't related to this patch at all, so, +1 from the current patch from me ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You got a point, I think it is safe to convert to a utf-8
byte-stream, because it is ascii
compatible anyway.
Updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a bad idea - things like the Window console will throw exceptions when trying to print UTF-8 with Python < 3.6.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm perhaps it's something on my machine, but I did this test:
# encoding: utf-8
from _pytest.compat import safe_str
print(safe_str(u'oh noes ☺'))
(.env27) C:\Users\bruno\pytest>python foo.py
oh noes Γÿ║
(.env27) C:\Users\bruno\pytest>python --version
Python 2.7.9 :: Continuum Analytics, Inc.
p.s.: Thanks for the speedy fix ;) |
This way we don't lose information and the returned string is ascii-compatible anyway
Actually, I thought that safe_str would be used for things internally, not really printing... For printing you shouldn't use utf-8, you should use the value defined in the PYTHOIOENCODING env var -- a default of ascii could be used for being "safe" (there should probably be a special function dedicated to printing which makes sure that whatever is being printed can be encoded to PYTHONIOENCODING -- i.e.: it's always possible that printing a non-ascii value can give an error). This value should also be acessible through sys.stdout.encoding. ie.:
So, to be on the safe side, you can first try to print directly, but if it fails, try to encode to the related encoding first -- and if that still fails, go for ascii, as I believe ascii shouldn't ever fail (if that still fails, I think there's nothing more you can do)... if you expect those strings to end up in the console stderr, maybe using sys.stderr.encoding would be better (and the user/IDE should control PYTHONIOENCODING as needed). |
Yeah I hear you. Ideally we should be using unicode for everything internally, but unfortunately that's not the case for pytest at the moment. For example, the usage for So the current support for pytest regarding unicode filenames or unicode system/user exceptions is very fragile. 😞 In order to actually implement proper and complete unicode support for Python 2, we would have to revisit the majority of the code base (and that includes That's my opinion on the current state of things at least, don't know about the other contributors and would love to hear their opinions on the subject. |
Fix #2336
cc @fabioz