annotate upreckon/unix.py @ 233:54cdc583ab77

Fixed crashing on validator output on Python 2 (regression in f94f9724c543)
author Oleg Oshmyan <chortos@inbox.lv>
date Sat, 20 Oct 2012 21:03:44 +0100
parents d9eb7d958b6d
children acd70a60bc17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
1 # Copyright (c) 2010-2011 Chortos-2 <chortos@inbox.lv>
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
2
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
3 from __future__ import division, with_statement
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
4
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
5 from .compat import *
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
6 from .exceptions import *
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
7
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
8 from subprocess import Popen
86
8cd7a732f2f3 Fixed a crash in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 85
diff changeset
9 import os, sys, time
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
10
221
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
11 try:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
12 from time import steady as clock
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
13 except ImportError:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
14 if sys.platform.startswith('java'):
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
15 from time import clock
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
16 else:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
17 from time import time as clock
85
741ae3391b61 Moved clock/time detection into platform-specific modules and testcases
Oleg Oshmyan <chortos@inbox.lv>
parents: 82
diff changeset
18
741ae3391b61 Moved clock/time detection into platform-specific modules and testcases
Oleg Oshmyan <chortos@inbox.lv>
parents: 82
diff changeset
19 try:
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
20 from signal import SIGTERM, SIGKILL
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
21 except ImportError:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
22 SIGTERM = 15
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
23 SIGKILL = 9
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
24
134
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 129
diff changeset
25 __all__ = 'call', 'kill', 'pause', 'clock'
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
26
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
27
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
28 try:
118
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
29 from signal import SIGCHLD, SIG_DFL, signal, set_wakeup_fd
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
30 from select import select, error as SelectError
118
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
31 from errno import EAGAIN, EINTR
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
32 from fcntl import fcntl, F_SETFD, F_GETFD, F_SETFL, F_GETFL
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
33 from os import O_NONBLOCK
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
34 try:
221
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
35 from os import O_CLOEXEC, pipe2
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
36 except ImportError:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
37 from os import pipe
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
38 try:
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
39 import cPickle as pickle
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
40 except ImportError:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
41 import pickle
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42 except ImportError:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
43 def call(*args, **kwargs):
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
44 case = kwargs.pop('case')
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
45 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
46 case.process = Popen(*args, **kwargs)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
47 except OSError:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
48 raise CannotStartTestee(sys.exc_info()[1])
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49 case.time_started = clock()
100
995502cdafc6 Fixed the generic implementation of call to use maxwalltime
Oleg Oshmyan <chortos@inbox.lv>
parents: 94
diff changeset
50 if not case.maxwalltime:
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
51 while True:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
52 exitcode, now = case.process.poll(), clock()
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
53 if exitcode is not None:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
54 case.time_stopped = now
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
55 break
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
56 else:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
57 time.sleep(.001)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
58 else:
100
995502cdafc6 Fixed the generic implementation of call to use maxwalltime
Oleg Oshmyan <chortos@inbox.lv>
parents: 94
diff changeset
59 time_end = case.time_started + case.maxwalltime
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
60 while True:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
61 exitcode, now = case.process.poll(), clock()
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
62 if exitcode is not None:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
63 case.time_stopped = now
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
64 break
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
65 elif now >= time_end:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
66 raise WallTimeLimitExceeded
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
67 else:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
68 time.sleep(.001)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
69 else:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
70 try:
127
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
71 from signal import siginterrupt
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
72 except ImportError:
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
73 # Sucks.
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
74 siginterrupt = lambda signalnum, flag: None
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
75
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
76 try:
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
77 from resource import getrusage, RUSAGE_SELF, RUSAGE_CHILDREN
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
78 except ImportError:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
79 from time import clock as cpuclock
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
80 getrusage = lambda who: None
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
81 else:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
82 def cpuclock():
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
83 rusage = getrusage(RUSAGE_SELF)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
84 return rusage.ru_utime + rusage.ru_stime
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
85
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
86 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
87 from resource import setrlimit
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
88 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
89 from resource import RLIMIT_AS
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
90 except ImportError:
127
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
91 from resource import RLIMIT_VMEM as RLIMIT_AS
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
92 except ImportError:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
93 setrlimit = None
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
94
221
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
95 try:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
96 pipe2
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
97 except NameError:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
98 try:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
99 from fcntl import FD_CLOEXEC
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
100 except ImportError:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
101 FD_CLOEXEC = 1
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
102 # Pick an arbitrary unique value for O_CLOEXEC
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
103 O_CLOEXEC = 1 if O_NONBLOCK != 1 else 2
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
104 def pipe2(flags):
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
105 r, w = pipe()
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
106 if flags & O_NONBLOCK:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
107 fcntl(r, F_SETFL, fcntl(r, F_GETFL) | O_NONBLOCK)
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
108 fcntl(w, F_SETFL, fcntl(w, F_GETFL) | O_NONBLOCK)
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
109 if flags & O_CLOEXEC:
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
110 fcntl(r, F_SETFD, fcntl(r, F_GETFD) | FD_CLOEXEC)
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
111 fcntl(w, F_SETFD, fcntl(w, F_GETFD) | FD_CLOEXEC)
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
112 return r, w
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
113
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
114 # Make SIGCHLD interrupt sleep() and select()
221
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
115 sigchld_pipe_read, sigchld_pipe_write = pipe2(O_NONBLOCK)
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
116 def bury_child(signum, frame):
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
117 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
118 bury_child.case.time_stopped = clock()
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
119 except Exception:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
120 pass
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
121 signal(SIGCHLD, bury_child)
118
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
122 set_wakeup_fd(sigchld_pipe_write)
108
218b8c28549c Fixed a crash due to SIGCHLD interrupting validator output pipe reads
Oleg Oshmyan <chortos@inbox.lv>
parents: 100
diff changeset
123 class SignalIgnorer(object):
218b8c28549c Fixed a crash due to SIGCHLD interrupting validator output pipe reads
Oleg Oshmyan <chortos@inbox.lv>
parents: 100
diff changeset
124 def __enter__(self):
218b8c28549c Fixed a crash due to SIGCHLD interrupting validator output pipe reads
Oleg Oshmyan <chortos@inbox.lv>
parents: 100
diff changeset
125 signal(SIGCHLD, SIG_DFL)
218b8c28549c Fixed a crash due to SIGCHLD interrupting validator output pipe reads
Oleg Oshmyan <chortos@inbox.lv>
parents: 100
diff changeset
126 def __exit__(self, exc_type, exc_value, traceback):
218b8c28549c Fixed a crash due to SIGCHLD interrupting validator output pipe reads
Oleg Oshmyan <chortos@inbox.lv>
parents: 100
diff changeset
127 signal(SIGCHLD, bury_child)
218b8c28549c Fixed a crash due to SIGCHLD interrupting validator output pipe reads
Oleg Oshmyan <chortos@inbox.lv>
parents: 100
diff changeset
128 signal_ignorer = SignalIgnorer()
218b8c28549c Fixed a crash due to SIGCHLD interrupting validator output pipe reads
Oleg Oshmyan <chortos@inbox.lv>
parents: 100
diff changeset
129 __all__ += 'signal_ignorer',
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
130
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
131 # If you want this to work portably, don't set any stdio argument to PIPE
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
132 def call(*args, **kwargs):
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
133 global last_rusage
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
134 bury_child.case = case = kwargs.pop('case')
221
d9eb7d958b6d Added usage of Python 3.3 library additions to the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 218
diff changeset
135 read, write = pipe2(O_CLOEXEC)
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
136 def preexec_fn():
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
137 os.close(read)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
138 if setrlimit and case.maxmemory:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
139 maxmemory = ceil(case.maxmemory * 1048576)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
140 setrlimit(RLIMIT_AS, (maxmemory, maxmemory))
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
141 # I would also set a CPU time limit but I do not want the time
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
142 # passing between the calls to fork and exec to be counted in
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
143 os.write(write, pickle.dumps((clock(), cpuclock()), 1))
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
144 kwargs['preexec_fn'] = preexec_fn
127
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
145 # So how the hell do I actually make use of pass_fds?
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
146 # On 3.1-, calling Popen with pass_fds prints an exception
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
147 # from Popen.__del__ to stderr. On 3.2, Popen without close_fds
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
148 # or pass_fds creates a child and fails but that of course
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
149 # generates a SIGCHLD, which causes problems, and I have
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
150 # no process ID to wait upon to negate the changes made
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
151 # by the SIGCHLD handler.
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
152 kwargs['close_fds'] = False
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
153 old_rusage = getrusage(RUSAGE_CHILDREN)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
154 last_rusage = None
118
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
155 while True:
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
156 try:
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
157 os.read(sigchld_pipe_read, 512)
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
158 except OSError:
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
159 if sys.exc_info()[1].errno == EAGAIN:
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
160 break
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
161 else:
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
162 raise
124
19c42a3cd962 Very fast testees no longer break Popen on some UNIX systems on Python 2.6+
Oleg Oshmyan <chortos@inbox.lv>
parents: 123
diff changeset
163 siginterrupt(SIGCHLD, False)
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
164 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
165 case.process = Popen(*args, **kwargs)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
166 except OSError:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
167 os.close(read)
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
168 raise CannotStartTestee(sys.exc_info()[1])
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
169 finally:
124
19c42a3cd962 Very fast testees no longer break Popen on some UNIX systems on Python 2.6+
Oleg Oshmyan <chortos@inbox.lv>
parents: 123
diff changeset
170 siginterrupt(SIGCHLD, True)
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
171 os.close(write)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
172 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
173 if not catch_escape:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
174 if case.maxwalltime:
118
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
175 try:
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
176 select((sigchld_pipe_read,), (), (), case.maxwalltime)
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
177 except SelectError:
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
178 if sys.exc_info()[1].args[0] != EINTR:
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
179 raise
129
580f0f4687c3 Fixed EINTR fatally breaking another poll() call on Python 2.6-
Oleg Oshmyan <chortos@inbox.lv>
parents: 128
diff changeset
180 # subprocess in Python 2.6- is not guarded against EINTR
580f0f4687c3 Fixed EINTR fatally breaking another poll() call on Python 2.6-
Oleg Oshmyan <chortos@inbox.lv>
parents: 128
diff changeset
181 try:
580f0f4687c3 Fixed EINTR fatally breaking another poll() call on Python 2.6-
Oleg Oshmyan <chortos@inbox.lv>
parents: 128
diff changeset
182 if case.process.poll() is None:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
183 raise WallTimeLimitExceeded
129
580f0f4687c3 Fixed EINTR fatally breaking another poll() call on Python 2.6-
Oleg Oshmyan <chortos@inbox.lv>
parents: 128
diff changeset
184 except OSError:
580f0f4687c3 Fixed EINTR fatally breaking another poll() call on Python 2.6-
Oleg Oshmyan <chortos@inbox.lv>
parents: 128
diff changeset
185 if sys.exc_info()[1].errno != EINTR:
580f0f4687c3 Fixed EINTR fatally breaking another poll() call on Python 2.6-
Oleg Oshmyan <chortos@inbox.lv>
parents: 128
diff changeset
186 raise
580f0f4687c3 Fixed EINTR fatally breaking another poll() call on Python 2.6-
Oleg Oshmyan <chortos@inbox.lv>
parents: 128
diff changeset
187 else:
580f0f4687c3 Fixed EINTR fatally breaking another poll() call on Python 2.6-
Oleg Oshmyan <chortos@inbox.lv>
parents: 128
diff changeset
188 case.process.poll()
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
189 else:
128
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
190 wait(case.process)
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
191 else:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
192 if not case.maxwalltime:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
193 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
194 while case.process.poll() is None:
118
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
195 s = select((sys.stdin, sigchld_pipe_read), (), ())
127
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
196 if (s[0] == [sys.stdin] and
118
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
197 sys.stdin.read(1) == '\33'):
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
198 raise CanceledByUser
128
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
199 except (SelectError, IOError, OSError):
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
200 if sys.exc_info()[1].args[0] != EINTR:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
201 raise
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
202 else:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
203 case.process.poll()
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
204 else:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
205 time_end = clock() + case.maxwalltime
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
206 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
207 while case.process.poll() is None:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
208 remaining = time_end - clock()
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
209 if remaining > 0:
118
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
210 s = select((sys.stdin, sigchld_pipe_read),
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
211 (), (), remaining)
127
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
212 if (s[0] == [sys.stdin] and
118
16fe21d6582e Fixed a few race conditions in unix.call triggered by very fast testees
Oleg Oshmyan <chortos@inbox.lv>
parents: 108
diff changeset
213 sys.stdin.read(1) == '\33'):
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
214 raise CanceledByUser
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
215 else:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
216 raise WallTimeLimitExceeded
128
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
217 except (SelectError, IOError, OSError):
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
218 if sys.exc_info()[1].args[0] != EINTR:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
219 raise
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
220 else:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
221 case.process.poll()
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
222 finally:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
223 case.time_started, cpustart = pickle.loads(os.read(read, 512))
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
224 os.close(read)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
225 del bury_child.case
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
226 new_rusage = getrusage(RUSAGE_CHILDREN)
119
0b265fe9c81f Wall-clock time limit is now enforced on the output wall-clock time
Oleg Oshmyan <chortos@inbox.lv>
parents: 118
diff changeset
227 if (case.maxwalltime and
0b265fe9c81f Wall-clock time limit is now enforced on the output wall-clock time
Oleg Oshmyan <chortos@inbox.lv>
parents: 118
diff changeset
228 case.time_stopped - case.time_started > case.maxwalltime):
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
229 raise WallTimeLimitExceeded
123
90c002c960cb Fixed CPU time display on UNIX
Oleg Oshmyan <chortos@inbox.lv>
parents: 119
diff changeset
230 if new_rusage:
90c002c960cb Fixed CPU time display on UNIX
Oleg Oshmyan <chortos@inbox.lv>
parents: 119
diff changeset
231 time_started = old_rusage.ru_utime + old_rusage.ru_stime + cpustart
90c002c960cb Fixed CPU time display on UNIX
Oleg Oshmyan <chortos@inbox.lv>
parents: 119
diff changeset
232 time_stopped = new_rusage.ru_utime + new_rusage.ru_stime
127
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
233 # Yes, this actually happens
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
234 if time_started > time_stopped:
f5b8a0c0e3cb Multiple bug fixes in the unix module
Oleg Oshmyan <chortos@inbox.lv>
parents: 126
diff changeset
235 time_started = time_stopped
123
90c002c960cb Fixed CPU time display on UNIX
Oleg Oshmyan <chortos@inbox.lv>
parents: 119
diff changeset
236 if case.maxcputime or not case.maxwalltime:
90c002c960cb Fixed CPU time display on UNIX
Oleg Oshmyan <chortos@inbox.lv>
parents: 119
diff changeset
237 case.time_started = time_started
90c002c960cb Fixed CPU time display on UNIX
Oleg Oshmyan <chortos@inbox.lv>
parents: 119
diff changeset
238 case.time_stopped = time_stopped
90c002c960cb Fixed CPU time display on UNIX
Oleg Oshmyan <chortos@inbox.lv>
parents: 119
diff changeset
239 case.time_limit_string = case.cpu_time_limit_string
90c002c960cb Fixed CPU time display on UNIX
Oleg Oshmyan <chortos@inbox.lv>
parents: 119
diff changeset
240 if (case.maxcputime and
90c002c960cb Fixed CPU time display on UNIX
Oleg Oshmyan <chortos@inbox.lv>
parents: 119
diff changeset
241 time_stopped - time_started > case.maxcputime):
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
242 raise CPUTimeLimitExceeded
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
243 if case.maxmemory:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
244 if sys.platform != 'darwin':
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
245 maxrss = case.maxmemory * 1024
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
246 else:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
247 maxrss = case.maxmemory * 1048576
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
248 if last_rusage and last_rusage.ru_maxrss > maxrss:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
249 raise MemoryLimitExceeded
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
250 elif (new_rusage and
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
251 new_rusage.ru_maxrss > old_rusage.ru_maxrss and
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
252 new_rusage.ru_maxrss > maxrss):
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
253 raise MemoryLimitExceeded
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
254
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
255 # Emulate memory limits on platforms compatible with 4.3BSD but not XSI
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
256 # I say 'emulate' because the OS will allow excessive memory usage
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
257 # anyway; Upreckon will just treat the test case as not passed.
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
258 # To do this, we not only require os.wait4 to be present but also
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
259 # assume things about the implementation of subprocess.Popen.
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
260 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
261 def waitpid_emu(pid, options, _wait4=os.wait4):
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
262 global last_rusage
128
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
263 pid, status, last_rusage = _wait4(pid, options)
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
264 return pid, status
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
265 _waitpid = os.waitpid
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
266 os.waitpid = waitpid_emu
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
267 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
268 defaults = Popen._internal_poll.__func__.__defaults__
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
269 except AttributeError:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
270 # Python 2.5
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
271 defaults = Popen._internal_poll.im_func.func_defaults
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
272 i = defaults.index(_waitpid)
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
273 defaults = defaults[:i] + (waitpid_emu,) + defaults[i+1:]
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
274 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
275 Popen._internal_poll.__func__.__defaults__ = defaults
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
276 except AttributeError:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
277 # Python 2.5 again
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
278 Popen._internal_poll.im_func.func_defaults = defaults
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
279 except (AttributeError, ValueError):
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
280 pass
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
281
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
282
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
283 def kill(process):
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
284 try:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
285 process.kill()
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
286 except AttributeError:
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
287 os.kill(process.pid, SIGKILL)
134
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 129
diff changeset
288 wait(process)
128
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
289
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
290
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
291 # subprocess in Python 2.6- is not guarded against EINTR
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
292 try:
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
293 from errno import EINTR
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
294 except ImportError:
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
295 wait = Popen.wait
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
296 else:
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
297 def wait(process):
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
298 while True:
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
299 try:
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
300 return process.wait()
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
301 except OSError:
42c8f5c152a5 Fixed EINTR fatally breaking poll/wait on Python 2.6- (for real this time)
Oleg Oshmyan <chortos@inbox.lv>
parents: 127
diff changeset
302 if sys.exc_info()[1].errno != EINTR:
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
303 raise
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
304
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
305
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
306 try:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 136
diff changeset
307 from ._unix import *
136
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
308 except ImportError:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
309 if not sys.stdin.isatty():
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
310 pause = lambda: sys.stdin.read(1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
311 catch_escape = False
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
312 else:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
313 try:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
314 from select import select
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
315 import termios, tty, atexit
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
316 except ImportError:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
317 pause = lambda: sys.stdin.read(1)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
318 catch_escape = False
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
319 else:
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
320 catch_escape = True
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
321 def cleanup(old=termios.tcgetattr(sys.stdin.fileno())):
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
322 termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, old)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
323 atexit.register(cleanup)
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
324 tty.setcbreak(sys.stdin.fileno())
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
325 def pause():
ed4035661b85 Added a C implementation of the unix module (called _unix)
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
326 sys.stdin.read(1)
218
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
327 else:
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
328 try:
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
329 from signal import signal, SIGIO, SIG_DFL
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
330 from select import select
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
331 except ImportError:
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
332 pass
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
333 else:
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
334 def sigio_handler(signum, frame):
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
335 if select((sys.stdin,), (), (), 0)[0]:
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
336 if os.read(sys.stdin.fileno(), 1) == '\33'.encode('ascii'):
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
337 remove_escape_handler()
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
338 raise CanceledByUser
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
339 def install_escape_handler():
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
340 signal(SIGIO, sigio_handler)
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
341 sigio_handler(SIGIO, None)
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
342 def remove_escape_handler():
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
343 signal(SIGIO, SIG_DFL)
65b5c9390010 With _unix, Escape presses now cancel test data unarchiving
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
344 __all__ += 'install_escape_handler', 'remove_escape_handler'