Python sqlalchemy.util.py3k() Examples

The following are 12 code examples of sqlalchemy.util.py3k(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.util , or try the search function .
Example #1
Source File: assertions.py    From planespotter with MIT License 5 votes vote down vote up
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 = msg
            msg = str(exception)
        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):
        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) 
Example #2
Source File: assertions.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
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, exception, *arg, **kw):
        if not issubclass(exception, exc_cls):
            return real_warn(msg, exception, *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, exception, *arg, **kw)

    with mock.patch("warnings.warn", our_warn):
        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) 
Example #3
Source File: test_utils.py    From sqlalchemy with MIT License 5 votes vote down vote up
def _assert_unorderable_types(self, callable_):
        if util.py36:
            assert_raises_message(
                TypeError, "not supported between instances of", callable_
            )
        elif util.py3k:
            assert_raises_message(TypeError, "unorderable types", callable_)
        else:
            assert_raises_message(
                TypeError, "cannot compare sets using cmp()", callable_
            ) 
Example #4
Source File: test_utils.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_tzname(self, td, expected):
        if expected == "UTC" and util.py3k and not util.py36:
            expected += "+00:00"
        eq_(timezone(td).tzname(None), expected) 
Example #5
Source File: test_logging.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_result_large_param(self):
        import random

        largeparam = "".join(chr(random.randint(52, 85)) for i in range(5000))

        self.eng.echo = "debug"
        result = exec_sql(self.eng, "SELECT ?", (largeparam,))

        row = result.first()

        eq_(
            self.buf.buffer[1].message,
            "[raw sql] ('%s ... (4702 characters truncated) ... %s',)"
            % (largeparam[0:149], largeparam[-149:]),
        )

        if util.py3k:
            eq_(
                self.buf.buffer[3].message,
                "Row ('%s ... (4702 characters truncated) ... %s',)"
                % (largeparam[0:149], largeparam[-149:]),
            )
        else:
            eq_(
                self.buf.buffer[3].message,
                "Row (u'%s ... (4703 characters truncated) ... %s',)"
                % (largeparam[0:148], largeparam[-149:]),
            )

        if util.py3k:
            eq_(
                repr(row),
                "('%s ... (4702 characters truncated) ... %s',)"
                % (largeparam[0:149], largeparam[-149:]),
            )
        else:
            eq_(
                repr(row),
                "(u'%s ... (4703 characters truncated) ... %s',)"
                % (largeparam[0:148], largeparam[-149:]),
            ) 
Example #6
Source File: test_types.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_array_of_enums(self, array_cls, enum_cls, connection):
        tbl = Table(
            "enum_table",
            self.metadata,
            Column("id", Integer, primary_key=True),
            Column(
                "enum_col",
                array_cls(enum_cls("foo", "bar", "baz", name="an_enum")),
            ),
        )

        if util.py3k:
            from enum import Enum

            class MyEnum(Enum):
                a = "aaa"
                b = "bbb"
                c = "ccc"

            tbl.append_column(
                Column("pyenum_col", array_cls(enum_cls(MyEnum)),),
            )

        self.metadata.create_all(connection)

        connection.execute(
            tbl.insert(), [{"enum_col": ["foo"]}, {"enum_col": ["foo", "bar"]}]
        )

        sel = select([tbl.c.enum_col]).order_by(tbl.c.id)
        eq_(
            connection.execute(sel).fetchall(), [(["foo"],), (["foo", "bar"],)]
        )

        if util.py3k:
            connection.execute(tbl.insert(), {"pyenum_col": [MyEnum.a]})
            sel = select([tbl.c.pyenum_col]).order_by(tbl.c.id.desc())
            eq_(connection.scalar(sel), [MyEnum.a]) 
Example #7
Source File: test_types.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_unicode_warnings_typelevel_native_unicode(self):

        unicodedata = self.data
        u = Unicode()
        dialect = default.DefaultDialect()
        dialect.supports_unicode_binds = True
        uni = u.dialect_impl(dialect).bind_processor(dialect)
        if util.py3k:
            assert_raises(exc.SAWarning, uni, b"x")
            assert isinstance(uni(unicodedata), str)
        else:
            assert_raises(exc.SAWarning, uni, "x")
            assert isinstance(uni(unicodedata), unicode)  # noqa 
Example #8
Source File: assertions.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
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 = msg
            msg = str(exception)
        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):
        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) 
Example #9
Source File: assertions.py    From android_universal with MIT License 5 votes vote down vote up
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 = msg
            msg = str(exception)
        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):
        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) 
Example #10
Source File: test_transaction.py    From sqlalchemy with MIT License 4 votes vote down vote up
def test_savepoint_release_fails_ctxmanager(self, local_connection):
        connection = local_connection
        connection.begin()

        savepoint = [None]

        def go():

            with connection.begin_nested() as sp:
                savepoint[0] = sp
                # force the "commit" of the savepoint that occurs
                # when the "with" block fails, e.g.
                # the RELEASE, to fail, because the savepoint is already
                # released.
                connection.dialect.do_release_savepoint(
                    connection, sp._savepoint
                )

        # prior to SQLAlchemy 1.4, the above release would fail
        # and then the savepoint would try to rollback, and that failed
        # also, causing a long exception chain that under Python 2
        # was particularly hard to diagnose, leading to issue
        # #2696 which eventually impacted Openstack, and we
        # had to add warnings that show what the "context" for an
        # exception was.   The SQL for the exception was
        # ROLLBACK TO SAVEPOINT, and up the exception chain would be
        # the RELEASE failing.
        #
        # now, when the savepoint "commit" fails, it sets itself as
        # inactive.   so it does not try to rollback and it cleans
        # itself out appropriately.
        #

        exc_ = assert_raises_message(
            exc.DBAPIError, r".*SQL\:.*RELEASE SAVEPOINT", go
        )
        savepoint = savepoint[0]
        assert not savepoint.is_active

        if util.py3k:
            # driver error
            assert exc_.__cause__

            # and that's it, no other context
            assert not exc_.__cause__.__context__ 
Example #11
Source File: test_mutable.py    From sqlalchemy with MIT License 4 votes vote down vote up
def test_legacy_pickle_loads(self):
        # due to an inconsistency between pickle and copy, we have to change
        # MutableList to implement a __reduce_ex__ method.   Which means we
        # have to make sure all the old pickle formats are still
        # deserializable since these can be used for persistence. these pickles
        # were all generated using a MutableList that has only __getstate__ and
        # __setstate__.

        # f1 = Foo(data=[1, 2])
        # pickles = [
        #    dumps(f1.data)
        #    for loads, dumps in picklers()
        # ]
        # print(repr(pickles))
        # return

        if util.py3k:
            pickles = [
                b"\x80\x04\x95<\x00\x00\x00\x00\x00\x00\x00\x8c\x16"
                b"sqlalchemy.ext.mutable\x94\x8c\x0bMutableList\x94\x93\x94)"
                b"\x81\x94(K\x01K\x02e]\x94(K\x01K\x02eb.",
                b"ccopy_reg\n_reconstructor\np0\n(csqlalchemy.ext.mutable\n"
                b"MutableList\np1\nc__builtin__\nlist\np2\n(lp3\nI1\naI2\n"
                b"atp4\nRp5\n(lp6\nI1\naI2\nab.",
                b"ccopy_reg\n_reconstructor\nq\x00(csqlalchemy.ext.mutable\n"
                b"MutableList\nq\x01c__builtin__\nlist\nq\x02]q\x03(K\x01K"
                b"\x02etq\x04Rq\x05]q\x06(K\x01K\x02eb.",
                b"\x80\x02csqlalchemy.ext.mutable\nMutableList\nq\x00)\x81q"
                b"\x01(K\x01K\x02e]q\x02(K\x01K\x02eb.",
                b"\x80\x03csqlalchemy.ext.mutable\nMutableList\nq\x00)\x81q"
                b"\x01(K\x01K\x02e]q\x02(K\x01K\x02eb.",
                b"\x80\x04\x95<\x00\x00\x00\x00\x00\x00\x00\x8c\x16"
                b"sqlalchemy.ext.mutable\x94\x8c\x0bMutableList\x94\x93\x94)"
                b"\x81\x94(K\x01K\x02e]\x94(K\x01K\x02eb.",
            ]
        else:
            pickles = [
                "\x80\x02csqlalchemy.ext.mutable\nMutableList\nq\x00]q\x01"
                "(K\x01K\x02e\x85q\x02Rq\x03.",
                "\x80\x02csqlalchemy.ext.mutable\nMutableList"
                "\nq\x00]q\x01(K\x01K\x02e\x85q\x02Rq\x03.",
                "csqlalchemy.ext.mutable\nMutableList\np0\n"
                "((lp1\nI1\naI2\natp2\nRp3\n.",
                "csqlalchemy.ext.mutable\nMutableList\nq\x00(]"
                "q\x01(K\x01K\x02etq\x02Rq\x03.",
                "\x80\x02csqlalchemy.ext.mutable\nMutableList"
                "\nq\x01]q\x02(K\x01K\x02e\x85Rq\x03.",
                "\x80\x02csqlalchemy.ext.mutable\nMutableList\n"
                "q\x01]q\x02(K\x01K\x02e\x85Rq\x03.",
                "csqlalchemy.ext.mutable\nMutableList\np1\n"
                "((lp2\nI1\naI2\natRp3\n.",
                "csqlalchemy.ext.mutable\nMutableList\nq\x01"
                "(]q\x02(K\x01K\x02etRq\x03.",
            ]

        for pickle_ in pickles:
            obj = pickle.loads(pickle_)
            eq_(obj, [1, 2])
            assert isinstance(obj, MutableList) 
Example #12
Source File: assertions.py    From moviegrabber with GNU General Public License v3.0 4 votes vote down vote up
def assert_compile(self, clause, result, params=None,
                        checkparams=None, dialect=None,
                        checkpositional=None,
                        use_default_dialect=False,
                        allow_dialect_select=False,
                        literal_binds=False):
        if use_default_dialect:
            dialect = default.DefaultDialect()
        elif allow_dialect_select:
            dialect = None
        else:
            if dialect is None:
                dialect = getattr(self, '__dialect__', None)

            if dialect is None:
                dialect = config.db.dialect
            elif dialect == 'default':
                dialect = default.DefaultDialect()
            elif isinstance(dialect, util.string_types):
                dialect = url.URL(dialect).get_dialect()()


        kw = {}
        compile_kwargs = {}

        if params is not None:
            kw['column_keys'] = list(params)

        if literal_binds:
            compile_kwargs['literal_binds'] = True

        if isinstance(clause, orm.Query):
            context = clause._compile_context()
            context.statement.use_labels = True
            clause = context.statement

        if compile_kwargs:
            kw['compile_kwargs'] = compile_kwargs

        c = clause.compile(dialect=dialect, **kw)

        param_str = repr(getattr(c, 'params', {}))

        if util.py3k:
            param_str = param_str.encode('utf-8').decode('ascii', 'ignore')
            print(("\nSQL String:\n" + util.text_type(c) + param_str).encode('utf-8'))
        else:
            print("\nSQL String:\n" + util.text_type(c).encode('utf-8') + param_str)


        cc = re.sub(r'[\n\t]', '', util.text_type(c))

        eq_(cc, result, "%r != %r on dialect %r" % (cc, result, dialect))

        if checkparams is not None:
            eq_(c.construct_params(params), checkparams)
        if checkpositional is not None:
            p = c.construct_params(params)
            eq_(tuple([p[x] for x in c.positiontup]), checkpositional)