[Sqlalchemy-commits] sqlalchemy: - object_session() raises the proper
Brought to you by:
zzzeek
From: <co...@sq...> - 2010-08-18 14:54:53
|
details: http://hg.sqlalchemy.org/sqlalchemy/sqlalchemy/rev/230819db717a changeset: 6749:230819db717a user: zzzeek date: Wed Aug 18 10:54:40 2010 -0400 description: - object_session() raises the proper UnmappedInstanceError when presented with an unmapped instance. [ticket:1881] diffstat: CHANGES | 4 ++++ lib/sqlalchemy/orm/session.py | 12 ++++++++++-- test/orm/test_session.py | 17 ++++++++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diffs (71 lines): diff -r 664fe9afd893 -r 230819db717a CHANGES --- a/CHANGES Sat Aug 14 20:51:37 2010 -0400 +++ b/CHANGES Wed Aug 18 10:54:40 2010 -0400 @@ -100,6 +100,10 @@ in some UNION situations, would fail to propagate the inner columns completely to the outer query. [ticket:1852] + + - object_session() raises the proper + UnmappedInstanceError when presented with an + unmapped instance. [ticket:1881] - sql - Added basic math expression coercion for diff -r 664fe9afd893 -r 230819db717a lib/sqlalchemy/orm/session.py --- a/lib/sqlalchemy/orm/session.py Sat Aug 14 20:51:37 2010 -0400 +++ b/lib/sqlalchemy/orm/session.py Wed Aug 18 10:54:40 2010 -0400 @@ -1674,9 +1674,17 @@ def object_session(instance): - """Return the ``Session`` to which instance belongs, or None.""" + """Return the ``Session`` to which instance belongs. + + If the instance is not a mapped instance, an error is raised. - return _state_session(attributes.instance_state(instance)) + """ + + try: + return _state_session(attributes.instance_state(instance)) + except exc.NO_STATE: + raise exc.UnmappedInstanceError(instance) + def _state_session(state): if state.session_id: diff -r 664fe9afd893 -r 230819db717a test/orm/test_session.py --- a/test/orm/test_session.py Sat Aug 14 20:51:37 2010 -0400 +++ b/test/orm/test_session.py Wed Aug 18 10:54:40 2010 -0400 @@ -10,7 +10,8 @@ from sqlalchemy.test import engines, testing, config from sqlalchemy import Integer, String, Sequence from sqlalchemy.test.schema import Table, Column -from sqlalchemy.orm import mapper, relationship, backref, joinedload +from sqlalchemy.orm import mapper, relationship, backref, joinedload, \ + exc as orm_exc, object_session from sqlalchemy.test.testing import eq_ from test.engine import _base as engine_base from test.orm import _base, _fixtures @@ -67,6 +68,20 @@ finally: c.close() + @testing.resolve_artifact_names + def test_object_session_raises(self): + assert_raises( + orm_exc.UnmappedInstanceError, + object_session, + object() + ) + + assert_raises( + orm_exc.UnmappedInstanceError, + object_session, + User() + ) + @testing.requires.sequences def test_sequence_execute(self): seq = Sequence("some_sequence") |