'Initialize the Console object.
newbuffer=1 will allocate a new buffer so the old content will be restored
on exit.'

def __init__(self, newbuffer=0):

    if newbuffer:
        self.hout = self.CreateConsoleScreenBuffer((GENERIC_READ | GENERIC_WRITE), 0, None, 1, None)
        self.SetConsoleActiveScreenBuffer(self.hout)
    else:
        self.hout = self.GetStdHandle(STD_OUTPUT_HANDLE)
    self.hin = self.GetStdHandle(STD_INPUT_HANDLE)
    self.inmode = c_int(0)
    self.GetConsoleMode(self.hin, byref(self.inmode))
    self.SetConsoleMode(self.hin, 15)
    info = CONSOLE_SCREEN_BUFFER_INFO()
    self.GetConsoleScreenBufferInfo(self.hout, byref(info))
    self.attr = info.wAttributes
    self.saveattr = info.wAttributes
    self.defaultstate = AnsiState()
    self.defaultstate.winattr = info.wAttributes
    self.ansiwriter = AnsiWriter(self.defaultstate)
    background = (self.attr & 240)
    for escape in self.escape_to_color:
        if (self.escape_to_color[escape] is not None):
            self.escape_to_color[escape] |= background
    log(('initial    attr=%x' % self.attr))
    self.softspace = 0
    self.serial = 0
    self.pythondll = CDLL(('python%s%s' % (sys.version[0], sys.version[2])))
    self.inputHookPtr = c_int.from_address(addressof(self.pythondll.PyOS_InputHook)).value
    setattr(Console, 'PyMem_Malloc', self.pythondll.PyMem_Malloc)
