changeset 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 9d21cef40e5a
children 2cdd966bc5fb
files upreckon/unix.py
diffstat 1 files changed, 32 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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: