'(typ, [data]) = select(mailbox=\'INBOX\', readonly=False)
Select a mailbox. (Flushes all untagged responses.)
\'data\' is count of messages in mailbox (\'EXISTS\' response).
Mandated responses are (\'FLAGS\', \'EXISTS\', \'RECENT\', \'UIDVALIDITY\'), so
other responses should be obtained via "response(\'FLAGS\')" etc.'

def select(self, mailbox='INBOX', readonly=False, **kw):

    self.mailbox = mailbox
    self.is_readonly = bool(readonly)
    if readonly:
        name = 'EXAMINE'
    else:
        name = 'SELECT'
    try:
        rqb = self._command(name, mailbox)
        (typ, dat) = rqb.get_response(('command:    %s    =>    %%s' % rqb.name))
        if (typ != 'OK'):
            if (self.state == SELECTED):
                self.state = AUTH
            if __debug__:
                self._log(1, 'state    =>    AUTH')
            if (typ == 'BAD'):
                self._deliver_exc(self.error, ('%s    command    error:    %s    %s.    Data:    %.100s' % (name, typ, dat, mailbox)), kw)
            return self._deliver_dat(typ, dat, kw)
        self.state = SELECTED
        if __debug__:
            self._log(1, 'state    =>    SELECTED')
    finally:
        self._release_state_change()
    if (self._get_untagged_response('READ-ONLY', leave=True) and (not readonly)):
        if __debug__:
            self._dump_ur(1)
        self._deliver_exc(self.readonly, ('%s    is    not    writable' % mailbox), kw)
    (typ, dat) = self._untagged_response(typ, [None], 'EXISTS')
    return self._deliver_dat(typ, dat, kw)
