6
6
from typing import (
7
7
List ,
8
8
Callable ,
9
- Union
9
+ Union ,
10
+ Optional
10
11
)
11
12
12
13
from .utils import (
@@ -23,25 +24,24 @@ class Case:
23
24
def __init__ (
24
25
self ,
25
26
name : str ,
26
- case_file : Union [str , None ],
27
- command : Union [str , None ],
27
+ case_file : Optional [str ],
28
+ command : Optional [str ],
28
29
pts : Union [int , float ],
29
- correct_file : Union [str , None ],
30
- verify_func : Union [Callable , None ]
30
+ correct_file : Optional [str ],
31
+ verify_func : Optional [Callable ]
31
32
):
32
33
self .name : str = name
33
- self .case_file : Union [str , None ] = case_file
34
- self .command : Union [str , None ] = command
34
+ self .case_file : Optional [str ] = case_file
35
+ self .command : Optional [str ] = command
35
36
self .pts : Union [int , float ] = pts
36
- self .correct_file : Union [str , None ] = correct_file
37
- self .verify_func : Union [Callable , None ] = verify_func
37
+ self .correct_file : Optional [str ] = correct_file
38
+ self .verify_func : Optional [Callable ] = verify_func
38
39
39
40
self .case_data : List [str ] = []
40
41
self .student_output : str = ""
41
42
self .correct_output : str = ""
42
43
43
44
def __correct_setup (self ) -> None :
44
-
45
45
# No correct file
46
46
if self .correct_file == None :
47
47
logging .debug (f"No correct file" )
@@ -61,12 +61,8 @@ def __correct_setup(self) -> None:
61
61
62
62
if correct_file .endswith (".c" ): # C correct program
63
63
logging .debug (f"C correct program runnning" )
64
-
65
64
logging .debug (f"Buidling: { correct_dir } , support_files: { support_files } " )
66
- build (
67
- folder = correct_dir ,
68
- copy_file = support_files
69
- )
65
+ build (folder = correct_dir , copy_file = support_files )
70
66
71
67
command = f"./{ correct_file } " .replace (".c" , "" )
72
68
logging .debug (f"Execute: { correct_dir } , command: { command } " )
@@ -79,23 +75,21 @@ def __correct_setup(self) -> None:
79
75
80
76
elif correct_file .endswith (".py" ): # Python correct program
81
77
logging .debug (f"Python correct program running" )
82
-
83
78
command = f"{ executable } { correct_file } "
84
79
logging .debug (f"Excute: { correct_dir } , command: { command } " )
85
80
self .correct_output = execute (
86
81
folder = correct_dir ,
87
82
command = command ,
88
83
stdin_list = self .case_data
89
84
)
90
-
91
85
logging .debug (f"Python correct program finished" )
92
86
93
87
output_filepath = os .path .join (correct_dir , f"{ self .name } .txt" )
94
88
logging .debug (f"Write correct output: { output_filepath } " )
95
89
with open (output_filepath , "w+" ) as f :
96
90
f .write (self .correct_output )
97
91
98
- def execute (self , student_dir : str ) -> Union [ None , ExecuteException ]:
92
+ def execute (self , student_dir : str ) -> Optional [ ExecuteException ]:
99
93
# setup case data and build/execute correct file
100
94
if self .case_file != None :
101
95
self .case_data = [line .strip () for line in open (self .case_file , "r" ).readlines ()]
@@ -130,23 +124,25 @@ def execute(self, student_dir: str) -> Union[None, ExecuteException]:
130
124
comment = f"{ self .name } : { comment } "
131
125
exception_signal = ExecuteException .ProgramError
132
126
133
- finally :
134
- if len (output ) > MAX_OUTPUT_SIZE :
135
- logging .warning (f"In { student_dir } , { self .name } : outputs are truncated to { MAX_OUTPUT_SIZE } characters" )
136
- output = output [:MAX_OUTPUT_SIZE ]
137
-
138
- output_filepath = os .path .join (student_dir , f"{ self .name } .txt" )
139
- logging .debug (f"Write student output: { output_filepath } " )
140
- with open (output_filepath , "w+" ) as f :
141
- f .write (output )
142
- self .student_output = output
143
-
144
- # Append to grade report
145
- logging .debug (f"Exception Signal: { exception_signal } " )
146
- if exception_signal :
147
- (lambda x : (x [1 ].append (0 ), x [2 ].append (comment ), x ))(currentframe ().f_back .f_locals ["self" ].grade_report [- 1 ])
148
-
149
- return exception_signal
127
+ except Exception as e :
128
+ logging .error (f"Exception: { str (e )} " )
129
+
130
+ if len (output ) > MAX_OUTPUT_SIZE :
131
+ logging .warning (f"In { student_dir } , { self .name } : outputs are truncated to { MAX_OUTPUT_SIZE } characters" )
132
+ output = output [:MAX_OUTPUT_SIZE ]
133
+
134
+ output_filepath = os .path .join (student_dir , f"{ self .name } .txt" )
135
+ logging .debug (f"Write student output: { output_filepath } " )
136
+ with open (output_filepath , "w+" ) as f :
137
+ f .write (output )
138
+ self .student_output = output
139
+
140
+ # Append to grade report
141
+ logging .debug (f"Exception Signal: { exception_signal } " )
142
+ if exception_signal :
143
+ (lambda x : (x [1 ].append (0 ), x [2 ].append (comment ), x ))(currentframe ().f_back .f_locals ["self" ].grade_report [- 1 ])
144
+
145
+ return exception_signal
150
146
151
147
def verify (self ) -> None :
152
148
if self .verify_func == None :
0 commit comments