<s> from io import BytesIO <EOL> from tests . py2 import mock_stdout_or_stderr <EOL> from tests . sandbox import SandboxedTestCase <EOL> class MockSubprocessTestCase ( SandboxedTestCase ) : <EOL> def mock_popen ( self , module , main_func , env ) : <EOL> """<STR_LIT>""" <EOL> PopenClass = self . _make_popen_class ( main_func , env ) <EOL> original_popen = module . Popen <EOL> module . Popen = PopenClass <EOL> self . addCleanup ( setattr , module , '<STR_LIT>' , original_popen ) <EOL> def _make_popen_class ( outer , func , env ) : <EOL> class MockPopen ( object ) : <EOL> def __init__ ( self , args , stdin = None , stdout = None , stderr = None ) : <EOL> self . args = args <EOL> self . stdin = BytesIO ( ) <EOL> self . stdout = BytesIO ( ) <EOL> self . stderr = BytesIO ( ) <EOL> self . _run ( ) <EOL> def _run ( self ) : <EOL> stdout = mock_stdout_or_stderr ( ) <EOL> stderr = mock_stdout_or_stderr ( ) <EOL> self . returncode = func ( <EOL> self . stdin , stdout , stderr , self . args , env ) <EOL> self . stdout = BytesIO ( stdout . getvalue ( ) ) <EOL> self . stderr = BytesIO ( stderr . getvalue ( ) ) <EOL> def communicate ( self , input = None ) : <EOL> return self . stdout . getvalue ( ) , self . stderr . getvalue ( ) <EOL> def wait ( self ) : <EOL> return self . returncode <EOL> return MockPopen </s>
