Problem with Nose testing until forking process

Jan Koprowski Jan.Koprowski at gmail.com
Mon Jan 26 17:05:17 CET 2009


Hi !

  I write application witch sometimes need fork to shell based process
(some kind of shell command). I snatch stdin, stdout, stderr and two
additional streams and fork process to run command and get results.

# -*- encoding: utf-8 -*-

import os
import sys
import subprocess

def pipes_function():

    global login_r, login_w, password_r, password_w

    try: os.fdopen(3).close()
    except OSError: pass
    try: os.fdopen(4).close()
    except OSError: pass

    os.close(login_w)
    os.close(password_w)
    os.dup2(login_r, 3)
    os.dup2(password_r, 4)

class BrokerProcess:

    __slots__ = ['username', 'password', 'command', 'stdout',
'stderr', 'returned']

    def run(self):

        global login_r, login_w, password_r, password_w

        login_r, login_w = os.pipe()
        password_r, password_w = os.pipe()

        wrapper = subprocess.Popen(args=('/usr/bin/special/wrapper'),
preexec_fn=pipes_function, bufsize=0, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        os.close(login_r)
        os.close(password_r)

        login_w = os.fdopen(login_w, 'w')
        password_w = os.fdopen(password_w, 'w')

        login_w.write(self.username)
        login_w.close()

        password_w.write(self.password)
        password_w.close()

        self.command = self.command.encode()
        wrapper.stdin.write(self.command)
        wrapper.stdin.close()
        self.stdout = wrapper.stdout.read()
        self.stderr = wrapper.stderr.read()

        self.returned = wrapper.wait()
        return self.returned


  My problem is that when I run nose test for function witch use this
broker i get followin errors (I test Pylons app):

 File "/home/users/matrix/johny/Pylons/zhradmin/zhradmin2/zhradmin2/
controllers/account.py",
line 51, in login
   account.login()
 File "/home/users/matrix/johny/Pylons/zhradmin2/zhradmin2/model/
account.py",
line 29, in login
 File "/home/users/matrix/johny/Pylons/zhradmin2/zhradmin2/lib/
brokerprocess.py",
line 41, in run
 File "/usr/local/lib/python2.5/subprocess.py", line 594, in __init__
   errread, errwrite)
 File "/usr/local/lib/python2.5/subprocess.py", line 1091, in
_execute_child
   raise child_exception
OSError: [Errno 9] Bad file descriptor

Is anyone know why I get error ?



More information about the Python-list mailing list