comparison upreckon/unix.py @ 221:d9eb7d958b6d

Added usage of Python 3.3 library additions to the unix module
author Oleg Oshmyan <chortos@inbox.lv>
date Fri, 16 Mar 2012 18:24:56 +0000
parents 65b5c9390010
children acd70a60bc17
comparison
equal deleted inserted replaced
220:9d21cef40e5a 221:d9eb7d958b6d
6 from .exceptions import * 6 from .exceptions import *
7 7
8 from subprocess import Popen 8 from subprocess import Popen
9 import os, sys, time 9 import os, sys, time
10 10
11 if sys.platform.startswith('java'): 11 try:
12 from time import clock 12 from time import steady as clock
13 else: 13 except ImportError:
14 from time import time as clock 14 if sys.platform.startswith('java'):
15 from time import clock
16 else:
17 from time import time as clock
15 18
16 try: 19 try:
17 from signal import SIGTERM, SIGKILL 20 from signal import SIGTERM, SIGKILL
18 except ImportError: 21 except ImportError:
19 SIGTERM = 15 22 SIGTERM = 15
26 from signal import SIGCHLD, SIG_DFL, signal, set_wakeup_fd 29 from signal import SIGCHLD, SIG_DFL, signal, set_wakeup_fd
27 from select import select, error as SelectError 30 from select import select, error as SelectError
28 from errno import EAGAIN, EINTR 31 from errno import EAGAIN, EINTR
29 from fcntl import fcntl, F_SETFD, F_GETFD, F_SETFL, F_GETFL 32 from fcntl import fcntl, F_SETFD, F_GETFD, F_SETFL, F_GETFL
30 from os import O_NONBLOCK 33 from os import O_NONBLOCK
34 try:
35 from os import O_CLOEXEC, pipe2
36 except ImportError:
37 from os import pipe
31 try: 38 try:
32 import cPickle as pickle 39 import cPickle as pickle
33 except ImportError: 40 except ImportError:
34 import pickle 41 import pickle
35 except ImportError: 42 except ImportError:
59 raise WallTimeLimitExceeded 66 raise WallTimeLimitExceeded
60 else: 67 else:
61 time.sleep(.001) 68 time.sleep(.001)
62 else: 69 else:
63 try: 70 try:
64 from fcntl import FD_CLOEXEC
65 except ImportError:
66 FD_CLOEXEC = 1
67
68 try:
69 from signal import siginterrupt 71 from signal import siginterrupt
70 except ImportError: 72 except ImportError:
71 # Sucks. 73 # Sucks.
72 siginterrupt = lambda signalnum, flag: None 74 siginterrupt = lambda signalnum, flag: None
73 75
88 except ImportError: 90 except ImportError:
89 from resource import RLIMIT_VMEM as RLIMIT_AS 91 from resource import RLIMIT_VMEM as RLIMIT_AS
90 except ImportError: 92 except ImportError:
91 setrlimit = None 93 setrlimit = None
92 94
95 try:
96 pipe2
97 except NameError:
98 try:
99 from fcntl import FD_CLOEXEC
100 except ImportError:
101 FD_CLOEXEC = 1
102 # Pick an arbitrary unique value for O_CLOEXEC
103 O_CLOEXEC = 1 if O_NONBLOCK != 1 else 2
104 def pipe2(flags):
105 r, w = pipe()
106 if flags & O_NONBLOCK:
107 fcntl(r, F_SETFL, fcntl(r, F_GETFL) | O_NONBLOCK)
108 fcntl(w, F_SETFL, fcntl(w, F_GETFL) | O_NONBLOCK)
109 if flags & O_CLOEXEC:
110 fcntl(r, F_SETFD, fcntl(r, F_GETFD) | FD_CLOEXEC)
111 fcntl(w, F_SETFD, fcntl(w, F_GETFD) | FD_CLOEXEC)
112 return r, w
113
93 # Make SIGCHLD interrupt sleep() and select() 114 # Make SIGCHLD interrupt sleep() and select()
94 sigchld_pipe_read, sigchld_pipe_write = os.pipe() 115 sigchld_pipe_read, sigchld_pipe_write = pipe2(O_NONBLOCK)
95 fcntl(sigchld_pipe_read, F_SETFL,
96 fcntl(sigchld_pipe_read, F_GETFL) | O_NONBLOCK)
97 fcntl(sigchld_pipe_write, F_SETFL,
98 fcntl(sigchld_pipe_write, F_GETFL) | O_NONBLOCK)
99 def bury_child(signum, frame): 116 def bury_child(signum, frame):
100 try: 117 try:
101 bury_child.case.time_stopped = clock() 118 bury_child.case.time_stopped = clock()
102 except Exception: 119 except Exception:
103 pass 120 pass
113 130
114 # If you want this to work portably, don't set any stdio argument to PIPE 131 # If you want this to work portably, don't set any stdio argument to PIPE
115 def call(*args, **kwargs): 132 def call(*args, **kwargs):
116 global last_rusage 133 global last_rusage
117 bury_child.case = case = kwargs.pop('case') 134 bury_child.case = case = kwargs.pop('case')
118 read, write = os.pipe() 135 read, write = pipe2(O_CLOEXEC)
119 fcntl(write, F_SETFD, fcntl(write, F_GETFD) | FD_CLOEXEC)
120 def preexec_fn(): 136 def preexec_fn():
121 os.close(read) 137 os.close(read)
122 if setrlimit and case.maxmemory: 138 if setrlimit and case.maxmemory:
123 maxmemory = ceil(case.maxmemory * 1048576) 139 maxmemory = ceil(case.maxmemory * 1048576)
124 setrlimit(RLIMIT_AS, (maxmemory, maxmemory)) 140 setrlimit(RLIMIT_AS, (maxmemory, maxmemory))