question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

test: UnicodeDecodeError is thrown when the outputs contain non-Unicode

See original GitHub issue

When the program to be tested prints non-Unicode strings, oj raises UnicodeDecodeError. This often happens if you write strange languages, e.g. code golf with perl, brainfuck, etc.

This happens on both of WSL and native Ubuntu.

$ oj --version
online-judge-tools 7.3.0

$ cat main.py
#!/usr/bin/env python3
import sys
sys.stdout.buffer.write(b'\xff')

$ oj t -c 'python3 main.py'
[*] 10 cases found

[*] sample-1
Traceback (most recent call last):
  File "/home/ubuntu/.local/bin/oj", line 11, in <module>
    sys.exit(main())
  File "/home/ubuntu/.local/lib/python3.6/site-packages/onlinejudge/_implementation/main.py", line 297, in main
    run_program(namespace, parser=parser)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/onlinejudge/_implementation/main.py", line 276, in run_program
    test(args)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/onlinejudge/_implementation/command/test.py", line 218, in test
    history += [test_single_case(name, paths['in'], paths.get('out'), args=args)]
  File "/home/ubuntu/.local/lib/python3.6/site-packages/onlinejudge/_implementation/command/test.py", line 142, in test_single_case
    answer = (info['answer'] or b'').decode()  # type: str
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kawacchucommented, Nov 13, 2019

I got it now.

I think UnicodeDecodeError is kind message for users in order to prevent them from WA.

We can change the code to

answer = (info['answer'] or b'').decode(encoding='utf-8', errors='replace')

but I can’t judge if that modification is good or not…

1reaction
kmykcommented, Nov 13, 2019

For example, a code a=-1;main(){write(1,&a,3);} of C language prints non-Unicode.

Of course, as far as I know, there are no problems whose correct answer contains non-Unicode. But wrong answers which we may write print non-Unicode. About the example above, if it were a=4407873;main(){write(1,&a,3);}, it would print an ASCII letters ABC. We sometimes use such techniques, and print non-Unicode unintentionally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UnicodeDecodeError with 1.3.1 and python2.7 #816 - GitHub
So the problem is caused by mixing unicode and 8-bit str output in a test that fails. StringIO code contains this comment: The...
Read more >
How to fix: "UnicodeDecodeError: 'ascii' codec can't decode ...
UnicodeDecodeError : 'ascii' codec can't decode byte generally happens when you try to convert a Python 2.x str that contains non-ASCII to a...
Read more >
Bug #69988 “UnicodeDecodeError crack in doctest”
Putting this in a doctest will result in an UnicodeDecodeError when running it: Unicode crack: >>> print u'abc' abc. >>> print u'\xe9'.encode('utf-8')
Read more >
Overcoming frustration: Correctly using unicode in python2
Sending the wrong value here will lead to a UnicodeError being thrown when the string contains non-ASCII characters. Note. There is one mitigating...
Read more >
Fix Python UnicodeEncodeError: 'ascii' codec can't encode ...
A very common Python error that is raised when working with unicode characters is the UnicodeEncodeError . UnicodeEncodeError: 'ascii' codec can't encode ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found