def do_LIST(self, i=None):
        """
        Handle a LIST command.

        @type i: L{bytes} or L{None}
        @param i: A 1-based message index.

        @rtype: L{Deferred <defer.Deferred>}
        @return: A deferred which triggers after the response to the LIST
            command has been issued.
        """
        if i is None:
            d = defer.maybeDeferred(self.mbox.listMessages)
            def cbMessages(msgs):
                return self._coiterate(formatListResponse(msgs))
            def ebMessages(err):
                self.failResponse(err.getErrorMessage())
                log.msg("Unexpected do_LIST failure:")
                log.err(err)
            return self._longOperation(d.addCallbacks(cbMessages, ebMessages))
        else:
            try:
                i = int(i)
                if i < 1:
                    raise ValueError()
            except ValueError:
                if not isinstance(i, bytes):
                    i = str(i).encode("utf-8")
                self.failResponse(b"Invalid message-number: " + i)
            else:
                d = defer.maybeDeferred(self.mbox.listMessages, i - 1)
                def cbMessage(msg):
                    self.successResponse(intToBytes(i) + b' ' +
                                         intToBytes(msg))
                def ebMessage(err):
                    errcls = err.check(ValueError, IndexError)
                    if errcls is not None:
                        if errcls is IndexError:
                            # IndexError was supported for a while, but really
                            # shouldn't be.  One error condition, one exception
                            # type.  See ticket #6669.
                            warnings.warn(
                                "twisted.mail.pop3.IMailbox.listMessages may "
                                "not raise IndexError for out-of-bounds "
                                "message numbers: raise ValueError instead.",
                                PendingDeprecationWarning)
                        invalidNum = i
                        if invalidNum and not isinstance(invalidNum, bytes):
                            invalidNum = str(invalidNum).encode("utf-8")
                        self.failResponse(b"Invalid message-number: " +
                                          invalidNum)
                    else:
                        self.failResponse(err.getErrorMessage())
                        log.msg("Unexpected do_LIST failure:")
                        log.err(err)
                d.addCallbacks(cbMessage, ebMessage)
                return self._longOperation(d)

        Handle a LIST command.

        @type i: L{bytes} or L{None}
        @param i: A 1-based message index.

        @rtype: L{Deferred <defer.Deferred>}
        @return: A deferred which triggers after the response to the LIST
            command has been issued.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/mail/pop3.py