diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-08-22 16:01:23 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-08-22 16:01:23 -0400 |
commit | 03e47a8255476d9ed50c5aec73e6c272761d67eb (patch) | |
tree | 14a66e0932e21bc011bf642fe55436e60e418743 | |
parent | 8712ef2f81498fe59b9636ba150833d779e60781 (diff) | |
download | sqlalchemy-03e47a8255476d9ed50c5aec73e6c272761d67eb.tar.gz |
- repair the inspection hook in sqltypes to not be fooled
by mock and other __getattr__ impostors
-rw-r--r-- | lib/sqlalchemy/orm/state.py | 2 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 7 | ||||
-rw-r--r-- | test/sql/test_types.py | 10 |
3 files changed, 17 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/state.py b/lib/sqlalchemy/orm/state.py index 3cbeed0b4..6c9f07bff 100644 --- a/lib/sqlalchemy/orm/state.py +++ b/lib/sqlalchemy/orm/state.py @@ -14,6 +14,7 @@ defines a large part of the ORM's interactivity. import weakref from .. import util +from .. import inspection from . import exc as orm_exc, interfaces from .path_registry import PathRegistry from .base import PASSIVE_NO_RESULT, SQL_OK, NEVER_SET, ATTR_WAS_SET, \ @@ -21,6 +22,7 @@ from .base import PASSIVE_NO_RESULT, SQL_OK, NEVER_SET, ATTR_WAS_SET, \ from . import base +@inspection._self_inspects class InstanceState(interfaces.InspectionAttr): """tracks state information at the instance level. diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index b5c575143..92a0628da 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -1745,7 +1745,12 @@ def _resolve_value_to_type(value): # use inspect() to detect SQLAlchemy built-in # objects. insp = inspection.inspect(value, False) - if insp is not None: + if ( + insp is not None and + # foil mock.Mock() and other impostors by ensuring + # the inspection target itself self-inspects + insp.__class__ in inspection._registrars + ): raise exc.ArgumentError( "Object %r is not legal as a SQL literal value" % value) return NULLTYPE diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 90fac97c2..d562c83ce 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -26,7 +26,7 @@ from sqlalchemy.testing import AssertsCompiledSQL, AssertsExecutionResults, \ from sqlalchemy.testing.util import picklers from sqlalchemy.testing.util import round_decimal from sqlalchemy.testing import fixtures - +from sqlalchemy.testing import mock class AdaptTest(fixtures.TestBase): @@ -1668,6 +1668,14 @@ class ExpressionTest( types.NULLTYPE ) + def test_detect_coercion_not_fooled_by_mock(self): + m1 = mock.Mock() + is_( + bindparam('x', m1).type, + types.NULLTYPE + ) + + class CompileTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = 'default' |