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

@@ -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()