Skip to content

Commit

Permalink
Fix a bunch of PEP 8 violations
Browse files Browse the repository at this point in the history
  • Loading branch information
Xyene committed May 1, 2016
1 parent 1d3685e commit b1e6ead
Show file tree
Hide file tree
Showing 33 changed files with 140 additions and 120 deletions.
2 changes: 1 addition & 1 deletion dmoj/checkers/floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def check(process_output, judge_output, precision, **kwargs):
else:
process_float = float(process_token)
if abs(process_float - judge_float) > epsilon and \
(abs(judge_float) < epsilon or abs(1.0 - process_float / judge_float) > epsilon):
(abs(judge_float) < epsilon or abs(1.0 - process_float / judge_float) > epsilon):
return False
except:
return False
Expand Down
4 changes: 2 additions & 2 deletions dmoj/checkers/floatsrel.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def check(process_output, judge_output, precision, **kwargs):
return False
else:
process_float = float(process_token)
if ((abs(judge_float) < epsilon and abs(process_float) >= epsilon) or \
(abs(judge_float) >= epsilon and abs(1.0 - process_float / judge_float) > epsilon)):
if ((abs(judge_float) < epsilon <= abs(process_float)) or
(abs(judge_float) >= epsilon and abs(1.0 - process_float / judge_float) > epsilon)):
return False
except:
return False
Expand Down
3 changes: 2 additions & 1 deletion dmoj/cptbox/cptbox_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ def main():
print 'Return: %d' % proc._monitor()
print 'Memory usage: %d KB' % proc.max_memory


if __name__ == '__main__':
main()
main()
36 changes: 19 additions & 17 deletions dmoj/cptbox/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from _cptbox import *
from dmoj.cptbox.syscalls import translator, SYSCALL_COUNT, by_id


DISALLOW = 0
ALLOW = 1
_CALLBACK = 2
Expand Down Expand Up @@ -57,8 +56,8 @@ def file_arch(path):
return X64
return None

PYTHON_ARCH = file_arch(sys.executable)

PYTHON_ARCH = file_arch(sys.executable)

_PIPE_BUF = getattr(select, 'PIPE_BUF', 512)
_SYSCALL_INDICIES = [None] * 5
Expand Down Expand Up @@ -146,7 +145,7 @@ def r_execution_time(self):
return self._start_time and ((self._died_time or time.time()) - self._start_time)

def kill(self):
print>>sys.stderr, 'Child is requested to be killed'
print>> sys.stderr, 'Child is requested to be killed'
try:
os.kill(self.pid, signal.SIGKILL)
except OSError:
Expand All @@ -166,22 +165,25 @@ def _protection_fault(self, syscall):
if call[index] == syscall:
callname = by_id[id]
break
print>>sys.stderr, 'Protection fault on: %d (%s)' % (syscall, callname)
print>>sys.stderr, 'Arg0: 0x%016x' % self.debugger.uarg0
print>>sys.stderr, 'Arg1: 0x%016x' % self.debugger.uarg1
print>>sys.stderr, 'Arg2: 0x%016x' % self.debugger.uarg2
print>>sys.stderr, 'Arg3: 0x%016x' % self.debugger.uarg3
print>>sys.stderr, 'Arg4: 0x%016x' % self.debugger.uarg4
print>>sys.stderr, 'Arg5: 0x%016x' % self.debugger.uarg5
print>> sys.stderr, 'Protection fault on: %d (%s)' % (syscall, callname)
print>> sys.stderr, 'Arg0: 0x%016x' % self.debugger.uarg0
print>> sys.stderr, 'Arg1: 0x%016x' % self.debugger.uarg1
print>> sys.stderr, 'Arg2: 0x%016x' % self.debugger.uarg2
print>> sys.stderr, 'Arg3: 0x%016x' % self.debugger.uarg3
print>> sys.stderr, 'Arg4: 0x%016x' % self.debugger.uarg4
print>> sys.stderr, 'Arg5: 0x%016x' % self.debugger.uarg5

def _cpu_time_exceeded(self):
self._tle = True

def _run_process(self):
self._spawn(self._executable, self._args, self._env, self._chdir, self._fds)
if self._child_stdin >= 0: os.close(self._child_stdin)
if self._child_stdout >= 0: os.close(self._child_stdout)
if self._child_stderr >= 0: os.close(self._child_stderr)
if self._child_stdin >= 0:
os.close(self._child_stdin)
if self._child_stdout >= 0:
os.close(self._child_stdout)
if self._child_stderr >= 0:
os.close(self._child_stderr)
self._started.set()
self._start_time = time.time()
code = self._monitor()
Expand All @@ -197,7 +199,7 @@ def _shocker_thread(self):

while not self._exited:
if self.execution_time > self._time:
print>>sys.stderr, 'Shocker activated, ouch!'
print>> sys.stderr, 'Shocker activated, ouch!'
os.kill(self.pid, signal.SIGKILL)
self._tle = True
break
Expand All @@ -211,7 +213,7 @@ def _shocker_thread(self):

def __init_streams(self, stdin, stdout, stderr, unbuffered):
self.stdin = self.stdout = self.stderr = None

if unbuffered:
master, slave = pty.openpty()

Expand Down Expand Up @@ -279,8 +281,8 @@ def _communicate(self, input):
if not input:
self.stdin.close()

stdout = None # Return
stderr = None # Return
stdout = None # Return
stderr = None # Return
fd2file = {}
fd2output = {}

Expand Down
3 changes: 2 additions & 1 deletion dmoj/executors/FORTH.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Executor(ScriptExecutor):
fs = ['.*?\.(?:so|fs|fi|gforth-history)']

def get_cmdline(self):
return [self.get_command(), self._code, '-e', 'bye']
return [self.get_command(), self._code, '-e', 'bye']


initialize = Executor.initialize
1 change: 1 addition & 0 deletions dmoj/executors/GAS32.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ class Executor(GASExecutor):
.skip 4096
'''


initialize = Executor.initialize
4 changes: 3 additions & 1 deletion dmoj/executors/GAS64.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Executor(GASExecutor):
ld_path = env['runtime'].get('ld_x64', None)
qemu_path = env['runtime'].get('qemu_x64', None)
dynamic_linker = env['runtime'].get('ld.so_x64', '/lib64/ld-linux-x86-64.so.2')
crt_pre = env['runtime'].get('crt_pre_x64', ['/usr/lib/x86_64-linux-gnu/crt1.o', '/usr/lib/x86_64-linux-gnu/crti.o'])
crt_pre = env['runtime'].get('crt_pre_x64',
['/usr/lib/x86_64-linux-gnu/crt1.o', '/usr/lib/x86_64-linux-gnu/crti.o'])
crt_post = env['runtime'].get('crt_post_x64', ['/usr/lib/x86_64-linux-gnu/crtn.o'])
name = 'GAS64'

Expand Down Expand Up @@ -43,4 +44,5 @@ class Executor(GASExecutor):
.skip 4096
'''


initialize = Executor.initialize
4 changes: 3 additions & 1 deletion dmoj/executors/GASARM.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Executor(GASExecutor):
ld_path = env['runtime'].get('ld_arm', None)
qemu_path = env['runtime'].get('qemu_arm', None)
dynamic_linker = env['runtime'].get('ld.so_x86', '/lib/ld-linux-armhf.so.3')
crt_pre = env['runtime'].get('crt_pre_x86', ['/usr/lib/arm-linux-gnueabihf/crt1.o', '/usr/lib/arm-linux-gnueabihf/crti.o'])
crt_pre = env['runtime'].get('crt_pre_x86',
['/usr/lib/arm-linux-gnueabihf/crt1.o', '/usr/lib/arm-linux-gnueabihf/crti.o'])
crt_post = env['runtime'].get('crt_post_x86', ['/usr/lib/arm-linux-gnueabihf/crtn.o'])
name = 'GASARM'
test_program = r'''
Expand All @@ -25,4 +26,5 @@ class Executor(GASExecutor):
.ascii "echo: Hello, World!\n"
'''


initialize = Executor.initialize
10 changes: 5 additions & 5 deletions dmoj/executors/GCCExecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
else:
GCC_COMPILE.update(env['runtime'].get('gcc_compile', {}))

print CompiledExecutor

class GCCExecutor(CompiledExecutor):
defines = []
flags = []
Expand Down Expand Up @@ -59,8 +59,8 @@ def get_defines(self):

def get_compile_args(self):
return ([self.command, '-Wall'] + (['-fdiagnostics-color=always'] if self.has_color else []) + self.sources
+ self.get_defines() + ['-O2', '-lm'] + ([] if IS_ARM else ['-march=native'])
+ self.get_flags() + self.get_ldflags() + ['-s', '-o', self.get_compiled_file()])
+ self.get_defines() + ['-O2', '-lm'] + ([] if IS_ARM else ['-march=native'])
+ self.get_flags() + self.get_ldflags() + ['-s', '-o', self.get_compiled_file()])

def get_compile_env(self):
return GCC_COMPILE
Expand All @@ -78,8 +78,8 @@ def get_env(self):
def initialize(cls, sandbox=True):
try:
version = float(subprocess.Popen([cls.command, '-dumpversion'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()[0][:3])
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()[0][:3])
cls.has_color = version >= 4.9
except:
cls.has_color = False
Expand Down
4 changes: 3 additions & 1 deletion dmoj/executors/MONOFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Executor(MonoExecutor):
'''

def get_compile_args(self):
return [self.get_command(), '--nologo', '--optimize', '--tailcalls', '--out:%s' % self.get_compiled_file(), self._code]
return [self.get_command(), '--nologo', '--optimize', '--tailcalls', '--out:%s' % self.get_compiled_file(),
self._code]


initialize = Executor.initialize
2 changes: 1 addition & 1 deletion dmoj/executors/PHP.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ class Executor(PHPExecutor):
fs += [env['runtime']['phpconfdir']]


initialize = Executor.initialize
initialize = Executor.initialize
5 changes: 3 additions & 2 deletions dmoj/executors/PYPY.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Executor(PythonExecutor):
test_program = "print __import__('sys').stdin.read()"
name = 'PYPY'
fs = ['.*\.(?:so|py[co]?$)', '/proc/cpuinfo$', '/proc/meminfo$', '/etc/localtime$', '/dev/urandom$'] + [command] \
+ ([env['runtime']['pypydir']] if 'pypydir' in env['runtime'] else [])
+ ([env['runtime']['pypydir']] if 'pypydir' in env['runtime'] else [])

def get_security(self):
security = super(Executor, self).get_security()
Expand All @@ -18,12 +18,13 @@ def get_security(self):
def eaccess(debugger):
def handle_return():
debugger.result = -errno.EACCES

debugger.syscall = debugger.getpid_syscall
debugger.on_return(handle_return)
return True

security[sys_unlink] = eaccess
return security

initialize = Executor.initialize

initialize = Executor.initialize
3 changes: 2 additions & 1 deletion dmoj/executors/PYPY3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Executor(PythonExecutor):
test_program = "print(__import__('sys').stdin.read(), end='')"
name = 'PYPY3'
fs = ['.*\.(?:so|py[co]?$)', '/proc/cpuinfo$', '/proc/meminfo$', '/etc/localtime$', '/dev/urandom$'] + [command] \
+ ([env['runtime']['pypy3dir']] if 'pypy3dir' in env['runtime'] else [])
+ ([env['runtime']['pypy3dir']] if 'pypy3dir' in env['runtime'] else [])

def get_security(self):
from dmoj.cptbox.syscalls import sys_mkdir, sys_unlink
Expand All @@ -25,4 +25,5 @@ def unsafe_pypy3dir(debugger):
sec[sys_unlink] = unsafe_pypy3dir
return sec


initialize = Executor.initialize
4 changes: 3 additions & 1 deletion dmoj/executors/R.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Executor(ScriptExecutor):
ext = '.R'
name = 'R'
nproc = -1 # needs a bunch
nproc = -1 # needs a bunch
command = env['runtime'].get('Rscript')
test_program = 'writeLines(readLines(file("stdin")))'
syscalls = ['mkdir', 'setup', 'fork', 'waitpid', 'getpgrp', 'dup2', 'nanosleep',
Expand All @@ -24,6 +24,7 @@ def get_security(self):
def handle_socket(debugger):
def socket_return():
debugger.result = -errno.EACCES

debugger.syscall = debugger.getpid_syscall
debugger.on_return(socket_return)
return True
Expand All @@ -35,4 +36,5 @@ def socket_return():
def get_cmdline(self):
return [self.get_command(), '--vanilla', self._code]


initialize = Executor.initialize
3 changes: 2 additions & 1 deletion dmoj/executors/RUBY21.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ def get_security(self):
sec[sys_write] = lambda debugger: debugger.arg0 in (1, 2, 4)
return sec

initialize = Executor.initialize

initialize = Executor.initialize
3 changes: 2 additions & 1 deletion dmoj/executors/TCL.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ def get_security(self):
def handle_socket(debugger):
def socket_return():
debugger.result = -errno.EACCES

debugger.syscall = debugger.getpid_syscall
debugger.on_return(socket_return)
return True

security[sys_tgkill] = True
security[sys_write] = lambda debugger: debugger.arg0 <= 4 # TCL uses some handles internally
security[sys_write] = lambda debugger: debugger.arg0 <= 4 # TCL uses some handles internally
security[sys_socket] = handle_socket
security[sys_socketcall] = handle_socket
security[sys_connect] = True
Expand Down
3 changes: 1 addition & 2 deletions dmoj/executors/VC.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from dmoj.executors.resource_proxy import ResourceProxy
from dmoj.judgeenv import env


VC_ENV = env['runtime'].get('vc_env', {})
VC_COMPILE = os.environ.copy()

Expand All @@ -33,7 +32,7 @@ def __init__(self, problem_id, main_source, aux_sources=None):
output_file = self._file('%s.exe' % problem_id)

cl_args = ['cl', '-nologo'] + sources + ['-W4', '-DONLINE_JUDGE', '-DWIN32', '-D_CRT_SECURE_NO_WARNINGS',
'-EHsc', '-Ox', '-Fe%s' % output_file, '-link', '-stack:67108864']
'-EHsc', '-Ox', '-Fe%s' % output_file, '-link', '-stack:67108864']
cl_process = subprocess.Popen(cl_args, stderr=subprocess.PIPE, executable=env['runtime']['cl.exe'],
cwd=self._dir, env=VC_COMPILE)

Expand Down
12 changes: 5 additions & 7 deletions dmoj/executors/base_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from dmoj.judgeenv import env
from dmoj.executors.resource_proxy import ResourceProxy


try:
from dmoj.cptbox import SecurePopen, PIPE, CHROOTSecurity, ALLOW, syscalls
except ImportError:
Expand Down Expand Up @@ -75,17 +74,17 @@ def get_address_grace(self):

def get_nproc(self):
return self.nproc

def get_inject32(self):
file = self._file('dmsec32.dll')
copyfile(self.inject32, file)
return file

def get_inject64(self):
file = self._file('dmsec64.dll')
copyfile(self.inject64, file)
return file

def get_inject_func(self):
return self.inject_func

Expand Down Expand Up @@ -133,14 +132,13 @@ def run_self_test(cls, sandbox=True):
print 'Self-testing: %s executor:' % cls.name,
try:
executor = cls(cls.test_name, cls.test_program)
proc = executor.launch(time=cls.test_time, memory=cls.test_memory) \
if sandbox else executor.launch_unsafe()
proc = executor.launch(time=cls.test_time, memory=cls.test_memory) if sandbox else executor.launch_unsafe()
test_message = 'echo: Hello, World!'
stdout, stderr = proc.communicate(test_message + '\n')
res = stdout.strip() == test_message and not stderr
print ['Failed', 'Success'][res]
if stderr:
print>>sys.stderr, stderr
print>> sys.stderr, stderr
return res
except Exception:
print 'Failed'
Expand Down
Loading

0 comments on commit b1e6ead

Please sign in to comment.