'Mock system environment for InteractiveConsole'

def mock_sys(self):

    stack = ExitStack()
    self.addCleanup(stack.close)
    self.infunc = stack.enter_context(mock.patch('code.input', create=True))
    self.stdout = stack.enter_context(mock.patch('code.sys.stdout'))
    self.stderr = stack.enter_context(mock.patch('code.sys.stderr'))
    prepatch = mock.patch('code.sys', wraps=code.sys, spec=code.sys)
    self.sysmod = stack.enter_context(prepatch)
    if (sys.excepthook is sys.__excepthook__):
        self.sysmod.excepthook = self.sysmod.__excepthook__
