upgrade to python3

This commit is contained in:
2026-02-08 11:23:34 +08:00
parent 487c041148
commit 9b20887cc2
9 changed files with 139 additions and 195 deletions

View File

@@ -14,6 +14,31 @@ from engineconfig import getConfig
from entity import Submit, Problem, TestCase, PresetCode, DataFile
def _to_bytes(value, encoding='utf-8', errors='replace'):
if value is None:
return b''
if isinstance(value, xmlrpclib.Binary):
return value.data
if isinstance(value, bytes):
return value
if isinstance(value, str):
return value.encode(encoding, errors)
return str(value).encode(encoding, errors)
def _to_text(value, encoding='utf-8', errors='replace'):
if value is None:
return ''
if isinstance(value, xmlrpclib.Binary):
value = value.data
if isinstance(value, bytes):
return value.decode(encoding, errors)
if isinstance(value, str):
return value
return str(value)
class DataSourceError(Exception):
pass
@@ -244,9 +269,10 @@ class XmlRpcDataSource:
try:
submits = self.server.oj.get_submits(judgeid, limit)
for submit in submits:
if not isinstance(submit['code'], str):
submit['code'] = submit['code'].__str__()
if not isinstance(submit.get('code'), str):
submit['code'] = _to_text(submit.get('code'))
return submits
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error as e:
@@ -268,12 +294,13 @@ class XmlRpcDataSource:
try:
tests = self.server.oj.get_tests(problemid, full)
for test in tests:
if not isinstance(test['input'], str):
test['input'] = test['input'].__str__()
if not isinstance(test['output'], str):
test['output'] = test['output'].__str__()
if not isinstance(test.get('input'), str):
test['input'] = _to_text(test.get('input'))
if not isinstance(test.get('output'), str):
test['output'] = _to_text(test.get('output'))
self.logger.debug('Got %d test case(s)', len(tests))
return tests
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error as e:
@@ -284,13 +311,16 @@ class XmlRpcDataSource:
while True:
try:
test = self.server.oj.get_gztest(testid)
if not isinstance(test['input'], str):
test['input'] = test['input'].__str__()
if not isinstance(test['output'], str):
test['output'] = test['output'].__str__()
test['input'] = bz2.decompress(test['input'])
test['output'] = bz2.decompress(test['output'])
try:
input_data = bz2.decompress(_to_bytes(test.get('input')))
output_data = bz2.decompress(_to_bytes(test.get('output')))
except OSError:
self.logger.exception('Failed to decompress test data for %s', testid)
raise DataSourceError('Invalid bz2 data for test %s' % testid)
test['input'] = _to_text(input_data)
test['output'] = _to_text(output_data)
return test
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error as e:
@@ -302,10 +332,11 @@ class XmlRpcDataSource:
try:
codes = self.server.oj.get_presetcodes(problemid, lang)
for code in codes:
if not isinstance(code['code'], str):
code['code'] = code['code'].__str__()
if not isinstance(code.get('code'), str):
code['code'] = _to_text(code.get('code'))
self.logger.debug('Got %d presetcodes', len(codes))
return codes
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error as e:
@@ -328,7 +359,8 @@ class XmlRpcDataSource:
while True:
try:
data = self.server.oj.get_datafile_data(datafileid)
return str(data)
return _to_bytes(data)
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error as e:
@@ -336,7 +368,8 @@ class XmlRpcDataSource:
time.sleep(self.config.retry_wait)
def update_submit_compilemessage(self, id, compilemsg):
compilemsg = xmlrpclib.Binary(compilemsg)
compilemsg = xmlrpclib.Binary(_to_bytes(compilemsg))
while True:
try:
return self.server.oj.update_submit_compilemessage(
@@ -349,10 +382,11 @@ class XmlRpcDataSource:
def update_submit_test_results(self, id, results):
for r in results:
if not isinstance(r['stdout'], str): r['stdout'] = ''
if not isinstance(r['stderr'], str): r['stderr'] = ''
r['stdout'] = xmlrpclib.Binary(r['stdout'])
r['stderr'] = xmlrpclib.Binary(r['stderr'])
stdout = r.get('stdout', '')
stderr = r.get('stderr', '')
r['stdout'] = xmlrpclib.Binary(_to_bytes(stdout))
r['stderr'] = xmlrpclib.Binary(_to_bytes(stderr))
while True:
try:
return self.server.oj.update_submit_test_results(id, results)
@@ -389,10 +423,8 @@ class XmlRpcDataSource:
while True:
try:
msg = self.server.oj.get_submit_compilemessage(sid)
if isinstance(msg, str):
return msg
else:
return msg.__str__()
return _to_text(msg)
except xmlrpclib.Error as e:
return DataSourceError(e)
except socket.error as e:
@@ -411,7 +443,16 @@ class XmlRpcDataSource:
class DataSourceTest(unittest.TestCase):
def testByteTextConversion(self):
raw = b'hello'
self.assertEqual(_to_text(raw), 'hello')
self.assertEqual(_to_bytes('hello'), b'hello')
binval = xmlrpclib.Binary(b'world')
self.assertEqual(_to_text(binval), 'world')
self.assertEqual(_to_bytes(binval), b'world')
def setUp(self):
exec(open(os.path.join('..', 'testdata', 'test_config.py')).read())
self.config = getConfig()
self.datasource = self.config.datasources[0]

View File

@@ -5,6 +5,17 @@ import unittest
from engineconfig import getConfig
from judgescript import InternalJudge, ExternalJudge
def _to_text(value, encoding='utf-8', errors='replace'):
if value is None:
return ''
if isinstance(value, bytes):
return value.decode(encoding, errors)
if isinstance(value, str):
return value
return str(value)
class Problem:
def __init__(self, datasource, row):
@@ -17,12 +28,13 @@ class Problem:
self.timemodified = row['timemodified']
self.vcode = row['validator_code']
if not isinstance(self.vcode, str):
self.vcode = self.vcode.__str__()
self.vcode = _to_text(self.vcode)
self.vtype = row['validator_type']
self.vlang = row['validator_lang']
self.gcode = row['generator_code']
if not isinstance(self.gcode, str):
self.gcode = self.vcode.__str__()
self.gcode = _to_text(self.gcode)
self.gtype = row['generator_type']
self.standard_code = row['standard_code']
@@ -187,8 +199,9 @@ class TestCase:
if inmtime <= self.timemodified or outmtime <= self.timemodified:
logger.debug('Creating input/output file %s and %s' % (self.infile, self.outfile))
row = datasource.get_test(self.id)
input = string.replace(row['input'], '\r\n', '\n')
output = string.replace(row['output'], '\r\n', '\n')
input = row['input'].replace('\r\n', '\n')
output = row['output'].replace('\r\n', '\n')
with open(self.infile, 'w') as f:
f.write(input)
@@ -239,6 +252,7 @@ class DataFile:
# Save datafile
config = getConfig()
logger = logging.getLogger('main')
testdir = os.path.join(config.datadir, 'testcase')
if not os.path.exists(testdir): os.mkdir(testdir)
self.absolute_path = os.path.join(
@@ -251,16 +265,28 @@ class DataFile:
mtime = os.stat(self.absolute_path)[stat.ST_MTIME]
if mtime < self.timemodified:
data = datasource.get_datafile_data(self.id)
data = bz2.decompress(data)
try:
data = bz2.decompress(data)
except OSError:
logger.exception('Failed to decompress datafile %s', self.id)
DataFile.write_lock.release()
raise
if self.type == 'text':
if isinstance(data, (bytes, bytearray)):
text = data.decode('utf-8', errors='replace')
else:
text = str(data)
with open(self.absolute_path, 'w') as f:
f.write(string.replace(data, '\r\n', '\n'))
f.write(text.replace('\r\n', '\n'))
else:
if isinstance(data, str):
data = data.encode('utf-8', errors='replace')
with open(self.absolute_path, 'wb') as f:
f.write(data)
DataFile.write_lock.release()
if __name__ == '__main__':
unittest.main()

View File

@@ -80,10 +80,11 @@ class ExternalJudge:
self.comparecmd = tester.comparecmd
self.codefile = os.path.abspath(os.path.join(datadir, tester.source))
with open(self.codefile, 'w') as f:
f.write(string.replace(vcode, '\r\n', '\n'))
f.write(vcode.replace('\r\n', '\n'))
if len(vcode) > 0 and vcode[-1] != '\n':
f.write('\n')
self.logger.debug("Save validator code as %s" % self.codefile)
def judge(self, sid, tid, tin, tout, result, errfile, rundir = None):
@@ -116,9 +117,10 @@ class ExternalJudge:
pid = os.fork()
if pid == 0:
os.close(1)
os.open(rfile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0666)
os.open(rfile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o666)
os.close(2)
os.open(errfile, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0666)
os.open(errfile, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o666)
if rundir: os.chdir(rundir)
os.execv(cmd[0], cmd)

View File

@@ -298,8 +298,9 @@ class SimpleTester(TesterBase):
shutil.copyfile(os.path.join(rundir, submit_output_filename),
outfile)
except IOError:
f = file(outfile, 'w')
f.close()
with open(outfile, 'w') as f:
f.write('')
ret = [testcase.id, exitcode, sig, outfile, errfile, timeused, memused]
if timeused > testcase.timelimit:
@@ -484,21 +485,19 @@ class SimpleTesterTestCase(OJTestCase):
self.assertEqual(r[1], 'AC')
self.assertEqual(r[2], 0)
self.assertEqual(r[3], 0)
f = file(r[4], 'r')
o = string.join(f.readlines(), '\n')
f.close()
with open(r[4], 'rb') as f:
o = f.read().decode('latin1')
self.assertEqual(o, '3\n')
f = file(r[5], 'r')
o = string.join(f.readlines(), '\n')
f.close()
with open(r[5], 'rb') as f:
o = f.read().decode('latin1')
self.assertEqual(o, '')
r = self.st.run(submit, testcases[1])
f = file(r[4], 'r')
o = string.join(f.readlines(), '\n')
f.close()
with open(r[4], 'rb') as f:
o = f.read().decode('latin1')
self.assertEqual(o, '4\n')
self.st.cleanup(submit)
self.assertFalse(not self.config.no_cleanup and os.path.exists(datadir))
@@ -621,11 +620,11 @@ class SimpleTesterTestCase(OJTestCase):
self.assertTrue(r)
testcases = submit.get_testcases()
r = self.st.run(submit, testcases[0])
f = file(os.path.join(datadir, '0000000001.out'))
o = string.join(f.readlines(), '\n')
f.close()
with open(os.path.join(datadir, '0000000001.out'), 'rb') as f:
o = f.read().decode('latin1')
self.assertEqual(o, '3\0\n')
self.st.cleanup(submit)
self.assertFalse(not self.config.no_cleanup and os.path.exists(datadir))
@@ -640,11 +639,11 @@ class SimpleTesterTestCase(OJTestCase):
self.assertTrue(r)
testcases = submit.get_testcases()
r = self.st.run(submit, testcases[0])
f = file(os.path.join(datadir, '0000000001.out'))
o = string.join(f.readlines(), '\n')
f.close()
with open(os.path.join(datadir, '0000000001.out'), 'rb') as f:
o = f.read().decode('latin1')
self.assertEqual(o, '\xbb')
self.st.cleanup(submit)
self.assertFalse(not self.config.no_cleanup and os.path.exists(datadir))