# HG changeset patch # User Oleg Oshmyan # Date 1331922296 0 # Node ID d9eb7d958b6dad0cc1aef7401104bbec650d5921 # Parent 9d21cef40e5a70e094ed226ac1b0e31408f04346 Added usage of Python 3.3 library additions to the unix module diff -r 9d21cef40e5a -r d9eb7d958b6d upreckon/unix.py --- a/upreckon/unix.py Fri Nov 11 01:13:46 2011 +0000 +++ b/upreckon/unix.py Fri Mar 16 18:24:56 2012 +0000 @@ -8,10 +8,13 @@ from subprocess import Popen import os, sys, time -if sys.platform.startswith('java'): - from time import clock -else: - from time import time as clock +try: + from time import steady as clock +except ImportError: + if sys.platform.startswith('java'): + from time import clock + else: + from time import time as clock try: from signal import SIGTERM, SIGKILL @@ -29,6 +32,10 @@ from fcntl import fcntl, F_SETFD, F_GETFD, F_SETFL, F_GETFL from os import O_NONBLOCK try: + from os import O_CLOEXEC, pipe2 + except ImportError: + from os import pipe + try: import cPickle as pickle except ImportError: import pickle @@ -61,11 +68,6 @@ time.sleep(.001) else: try: - from fcntl import FD_CLOEXEC - except ImportError: - FD_CLOEXEC = 1 - - try: from signal import siginterrupt except ImportError: # Sucks. @@ -90,12 +92,27 @@ except ImportError: setrlimit = None + try: + pipe2 + except NameError: + try: + from fcntl import FD_CLOEXEC + except ImportError: + FD_CLOEXEC = 1 + # Pick an arbitrary unique value for O_CLOEXEC + O_CLOEXEC = 1 if O_NONBLOCK != 1 else 2 + def pipe2(flags): + r, w = pipe() + if flags & O_NONBLOCK: + fcntl(r, F_SETFL, fcntl(r, F_GETFL) | O_NONBLOCK) + fcntl(w, F_SETFL, fcntl(w, F_GETFL) | O_NONBLOCK) + if flags & O_CLOEXEC: + fcntl(r, F_SETFD, fcntl(r, F_GETFD) | FD_CLOEXEC) + fcntl(w, F_SETFD, fcntl(w, F_GETFD) | FD_CLOEXEC) + return r, w + # Make SIGCHLD interrupt sleep() and select() - sigchld_pipe_read, sigchld_pipe_write = os.pipe() - fcntl(sigchld_pipe_read, F_SETFL, - fcntl(sigchld_pipe_read, F_GETFL) | O_NONBLOCK) - fcntl(sigchld_pipe_write, F_SETFL, - fcntl(sigchld_pipe_write, F_GETFL) | O_NONBLOCK) + sigchld_pipe_read, sigchld_pipe_write = pipe2(O_NONBLOCK) def bury_child(signum, frame): try: bury_child.case.time_stopped = clock() @@ -115,8 +132,7 @@ def call(*args, **kwargs): global last_rusage bury_child.case = case = kwargs.pop('case') - read, write = os.pipe() - fcntl(write, F_SETFD, fcntl(write, F_GETFD) | FD_CLOEXEC) + read, write = pipe2(O_CLOEXEC) def preexec_fn(): os.close(read) if setrlimit and case.maxmemory: