Skip to content

Commit 3de95cf

Browse files
committed
Optimized various newline related problems under windows
1 parent 59baa1b commit 3de95cf

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

cyaron/compare.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from __future__ import absolute_import
12
from cyaron import IO
3+
from cyaron.utils import *
24
from cyaron.consts import *
35
from cyaron.graders import CYaRonGraders
46
import subprocess
57
import sys
6-
import io
8+
from io import open
79

810

911
class Compare:
@@ -20,12 +22,12 @@ def __compare_two(name, content, std, grader, **kwargs):
2022
if stop_on_incorrect and not result:
2123
if custom_dump_data:
2224
(dump_name, dump_lambda) = custom_dump_data
23-
with open(dump_name, "w") as f:
25+
with open(dump_name, "w", newline='\n') as f:
2426
f.write(dump_lambda())
2527

26-
with open("std.out", "w") as f:
28+
with open("std.out", "w", newline='\n') as f:
2729
f.write(std)
28-
with open("%s.out" % name, "w") as f:
30+
with open("%s.out" % name, "w", newline='\n') as f:
2931
f.write(content)
3032

3133
print("Relevant files dumped.")
@@ -40,7 +42,7 @@ def __process_file(file):
4042
file.output_file.seek(0)
4143
return file.output_filename, file.output_file.read()
4244
else:
43-
with open(file, "r") as f:
45+
with open(file, "r", newline='\n') as f:
4446
return file, f.read()
4547

4648
@staticmethod
@@ -77,7 +79,7 @@ def program(*args, **kwargs):
7779
raise Exception("You must specify a std or a std_program.")
7880
else:
7981
if "std_program" in kwargs:
80-
std = subprocess.check_output(kwargs['std_program'], shell=True, stdin=input.input_file).decode('ascii')
82+
std = make_unicode(subprocess.check_output(kwargs['std_program'], shell=True, stdin=input.input_file, universal_newlines=True))
8183
else:
8284
(_, std) = Compare.__process_file(kwargs["std"])
8385

@@ -86,7 +88,7 @@ def program(*args, **kwargs):
8688

8789
for program_name in args:
8890
input.input_file.seek(0)
89-
content = subprocess.check_output(program_name, shell=True, stdin=input.input_file)
91+
content = make_unicode(subprocess.check_output(program_name, shell=True, stdin=input.input_file, universal_newlines=True))
9092

9193
input.input_file.seek(0)
9294
Compare.__compare_two(program_name, content, std, grader,

cyaron/graders/fulltext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
@CYaRonGraders.grader("FullText")
55
def fulltext(content, std):
6-
content_hash = hashlib.sha256(content).hexdigest()
7-
std_hash = hashlib.sha256(std).hexdigest()
6+
content_hash = hashlib.sha256(content.encode('utf-8')).hexdigest()
7+
std_hash = hashlib.sha256(std.encode('utf-8')).hexdigest()
88
return (True, None) if content_hash == std_hash else (False, "Hash mismatch: read %s, expected %s" % (content_hash, std_hash))
99

cyaron/io.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from __future__ import absolute_import
12
from .utils import *
3+
from io import open
24
import subprocess
35
import tempfile
46
import os
@@ -54,8 +56,8 @@ def __init__(self, *args, **kwargs):
5456
else:
5557
raise Exception("Invalid argument count")
5658

57-
self.input_file = open(self.input_filename, 'w+')
58-
self.output_file = open(self.output_filename, 'w+') if self.output_filename else None
59+
self.input_file = open(self.input_filename, 'w+', newline='\n')
60+
self.output_file = open(self.output_filename, 'w+', newline='\n') if self.output_filename else None
5961
self.is_first_char = dict()
6062
if self.file_flag != 0:
6163
print("Processing %s" % self.input_filename)
@@ -104,9 +106,9 @@ def __write(self, file, *args, **kwargs):
104106
self.__write(file, *arg, **kwargs)
105107
else:
106108
if arg != "\n" and not self.is_first_char.get(file, True):
107-
file.write(separator)
109+
file.write(make_unicode(separator))
108110
self.is_first_char[file] = False
109-
file.write(str(arg))
111+
file.write(make_unicode(arg))
110112
if arg == "\n":
111113
self.is_first_char[file] = True
112114

@@ -135,7 +137,7 @@ def output_gen(self, shell_cmd):
135137
"""
136138
self.input_file.close()
137139
with open(self.input_filename, 'r') as f:
138-
self.output_file.write(subprocess.check_output(shell_cmd, shell=True, stdin=f).decode('ascii'))
140+
self.output_file.write(make_unicode(subprocess.check_output(shell_cmd, shell=True, stdin=f, universal_newlines=True)))
139141

140142
self.input_file = open(self.input_filename, 'a+')
141143
print(self.output_filename, " done")

cyaron/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ def strtolines(str):
2020

2121
while len(lines) > 0 and len(lines[len(lines) - 1]) == 0:
2222
del lines[len(lines) - 1]
23-
return lines
23+
return lines
24+
25+
26+
def make_unicode(data):
27+
try:
28+
return unicode(data)
29+
except NameError:
30+
return str(data)

0 commit comments

Comments
 (0)