ImportErrors caused by a line containing unicode text cause UnicodeError when attempting to log trace
See original GitHub issueI’m using green 2.0.1 in Python 2.7.6. I had a bug in a line of my code that contained unicode characters. When I attempted to run green, I got the following error:
Traceback (most recent call last):
File "/home/ayrton/.venv/whosays/bin/green", line 11, in <module>
sys.exit(main())
File "/home/ayrton/.venv/whosays/local/lib/python2.7/site-packages/green/cmdline.py", line 66, in main
test_suite = loadTargets(args.targets, file_pattern = args.file_pattern)
File "/home/ayrton/.venv/whosays/local/lib/python2.7/site-packages/green/loader.py", line 298, in loadTargets
suite = loadTarget(target, file_pattern)
File "/home/ayrton/.venv/whosays/local/lib/python2.7/site-packages/green/loader.py", line 353, in loadTarget
tests = discover(candidate, file_pattern=file_pattern)
File "/home/ayrton/.venv/whosays/local/lib/python2.7/site-packages/green/loader.py", line 266, in discover
subdir_suite = discover(path, file_pattern=file_pattern)
File "/home/ayrton/.venv/whosays/local/lib/python2.7/site-packages/green/loader.py", line 266, in discover
subdir_suite = discover(path, file_pattern=file_pattern)
File "/home/ayrton/.venv/whosays/local/lib/python2.7/site-packages/green/loader.py", line 266, in discover
subdir_suite = discover(path, file_pattern=file_pattern)
File "/home/ayrton/.venv/whosays/local/lib/python2.7/site-packages/green/loader.py", line 278, in discover
module_suite = loadFromModuleFilename(path)
File "/home/ayrton/.venv/whosays/local/lib/python2.7/site-packages/green/loader.py", line 230, in loadFromModuleFilename
dotted_module, filename, traceback.format_exc())
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 504: ordinal not in range(128)
The offending lines are 229 & 230 in green/loader.py
:
message = ('Failed to import {} computed from filename {}\n{}').format(
dotted_module, filename, traceback.format_exc())
It seems that traceback.format_exc()
cannot process unicode characters. Removing traceback.format_exc()
fixes the problem but obviously removes the traceback of the exception, which is not desirable.
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Python gives unicode error when writing to file - Stack Overflow
Open the file with utf-8 encoding, like this: with open(filename, 'w', encoding='utf-8') as element:.
Read more >UnicodeEncodeErrors with celery built-in console logging #427
Colored celery console logger fails to display unicode strings containing non-ascii letters (e.g. django's sql query log with non-ascii data):.
Read more >Working of Unicode Error in Python with Examples - eduCBA
In Python, it cannot detect Unicode characters, and therefore it throws an encoding error as it cannot encode the given Unicode string. Code:...
Read more >CS 111: Python Errors
Python Errors. This page contains a listing of common errors students find in their code. Feel free to use this list as a...
Read more >UnicodeEncodeError - Python Wiki
The UnicodeEncodeError normally happens when encoding a unicode string into a certain coding. Since codings map only a limited number of unicode ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@wonderb0lt The bug is fixed in 2.7.3 (just released).
@MinchinWeb I couldn’t find a way to fix this problem with unidecode. I found a way to fix it with some manual decoding, though. If you can find a cleaner way with unidecode, go ahead and change it. You can use the
green.test.test_result.TestGreenTestResult.test_printErrors_Py2Unicode
test to duplicate @wonderb0lt’s error condition.