Skip to content

Commit 795e96b

Browse files
committed
fix decode
1 parent e0f1699 commit 795e96b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

teach/learning.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def write_py(name, code):
5050
print('Code wrote to: %s' % fpath)
5151
return fpath
5252

53+
def decode(s):
54+
try:
55+
return s.decode('utf-8')
56+
except UnicodeDecodeError:
57+
return s.decode('gbk')
58+
5359
def application(environ, start_response):
5460
host = environ.get('HTTP_HOST')
5561
method = environ.get('REQUEST_METHOD')
@@ -86,9 +92,9 @@ def application(environ, start_response):
8692
try:
8793
fpath = write_py(name, code)
8894
print('Execute: %s %s' % (EXEC, fpath))
89-
r['output'] = subprocess.check_output([EXEC, fpath], stderr=subprocess.STDOUT, timeout=5).decode('utf-8')
95+
r['output'] = decode(subprocess.check_output([EXEC, fpath], stderr=subprocess.STDOUT, timeout=5))
9096
except subprocess.CalledProcessError as e:
91-
r = dict(error='Exception', output=e.output.decode('utf-8'))
97+
r = dict(error='Exception', output=decode(e.output))
9298
except subprocess.TimeoutExpired as e:
9399
r = dict(error='Timeout', output='执行超时')
94100
except subprocess.CalledProcessError as e:

0 commit comments

Comments
 (0)