@contextlib.contextmanager
def _expect_warnings(exc_cls, messages, regex=True, assert_=True, py2konly=False):
    if regex:
        filters = [re.compile(msg, (re.I | re.S)) for msg in messages]
    else:
        filters = messages
    seen = set(filters)
    real_warn = warnings.warn

    def our_warn(msg, *arg, **kw):
        if isinstance(msg, exc_cls):
            exception = type(msg)
            msg = str(msg)
        elif arg:
            exception = arg[0]
        else:
            exception = None
        if ((not exception) or (not issubclass(exception, exc_cls))):
            return real_warn(msg, *arg, **kw)
        if (not filters):
            return
        for filter_ in filters:
            if ((regex and filter_.match(msg)) or ((not regex) and (filter_ == msg))):
                seen.discard(filter_)
                break
        else:
            real_warn(msg, *arg, **kw)
    with mock.patch('warnings.warn', our_warn), mock.patch('sqlalchemy.util.SQLALCHEMY_WARN_20', True), mock.patch('sqlalchemy.util.deprecations.SQLALCHEMY_WARN_20', True), mock.patch('sqlalchemy.engine.row.LegacyRow._default_key_style', 2):
        (yield)
    if (assert_ and ((not py2konly) or (not compat.py3k))):
        assert (not seen), ('Warnings were not seen: %s' % ', '.join((('%r' % (s.pattern if regex else s)) for s in seen)))
@contextlib.contextmanager
def _expect_warnings(exc_cls, messages, regex=True, assert_=True, py2konly=False):
    if regex:
        filters = [re.compile(msg, (re.I | re.S)) for msg in messages]
    else:
        filters = messages
    seen = set(filters)
    real_warn = warnings.warn

    def our_warn(msg, *arg, **kw):
        if isinstance(msg, exc_cls):
            exception = type(msg)
            msg = str(msg)
        elif arg:
            exception = arg[0]
        else:
            exception = None
        if ((not exception) or (not issubclass(exception, exc_cls))):
            return real_warn(msg, *arg, **kw)
        if (not filters):
            return
        for filter_ in filters:
            if ((regex and filter_.match(msg)) or ((not regex) and (filter_ == msg))):
                seen.discard(filter_)
                break
        else:
            real_warn(msg, *arg, **kw)
    with mock.patch('warnings.warn', our_warn), mock.patch('sqlalchemy.util.SQLALCHEMY_WARN_20', True), mock.patch('sqlalchemy.util.deprecations.SQLALCHEMY_WARN_20', True), mock.patch('sqlalchemy.engine.row.LegacyRow._default_key_style', 2):
        (yield)
    if (assert_ and ((not py2konly) or (not compat.py3k))):
        assert (not seen), ('Warnings were not seen: %s' % ', '.join((('%r' % (s.pattern if regex else s)) for s in seen)))