Source

cpython-withatomic / Lib / popen2.py

import os
import sys
import string

MAXFD = 100	# Max number of file descriptors (os.getdtablesize()???)

def _popen2(cmd, capturestderr=0):
	cmd = ['/bin/sh', '-c', cmd]
	p2cread, p2cwrite = os.pipe()
	c2pread, c2pwrite = os.pipe()
	if capturestderr:
		errout, errin = os.pipe()
	pid = os.fork()
	if pid == 0:
		# Child
		os.close(0)
		os.close(1)
		if os.dup(p2cread) <> 0:
			sys.stderr.write('popen2: bad read dup\n')
		if os.dup(c2pwrite) <> 1:
			sys.stderr.write('popen2: bad write dup\n')
		if capturestderr:
			os.close(2)
			if os.dup(errin) <> 2: pass
		for i in range(3, MAXFD):
			try:
				os.close(i)
			except: pass
		try:
			os.execv(cmd[0], cmd)
		finally:
			os._exit(1)
		# Shouldn't come here, I guess
		os._exit(1)
	os.close(p2cread)
	tochild = os.fdopen(p2cwrite, 'w')
	os.close(c2pwrite)
	fromchild = os.fdopen(c2pread, 'r')
	if capturestderr:
		os.close(errin)
		childerr = os.fdopen(errout, 'r')
		return fromchild, tochild, childerr
	else:
		return fromchild, tochild, None

def popen2(cmd):
	return _popen2(cmd, 0)[:2]

def popen3(cmd):
	return _popen2(cmd, 1)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.