]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- object_session() raises the proper
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 18 Aug 2010 14:54:40 +0000 (10:54 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 18 Aug 2010 14:54:40 +0000 (10:54 -0400)
UnmappedInstanceError when presented with an
unmapped instance.  [ticket:1881]

CHANGES
lib/sqlalchemy/orm/session.py
test/orm/test_session.py

diff --git a/CHANGES b/CHANGES
index 4f60857ef8c03e920561dda13ed04f4ee6f07849..aae1139a14ba16e9d7b390f7721b53c14b696a5c 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -100,6 +100,10 @@ CHANGES
     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 
index c67dc5553f1fc08882a6c3f4c6d9089ecf3526e5..86d5dd7730d5769f246c550d2bd95fe6a8dabff4 100644 (file)
@@ -1674,9 +1674,17 @@ def make_transient(instance):
     
     
 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:
index 22de94e2b3bdf9b3474f26a86d35ee6c47844af2..779db304e2b485c9300ed8811e059324a8625800 100644 (file)
@@ -10,7 +10,8 @@ import sqlalchemy as sa
 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 @@ class SessionTest(_fixtures.FixtureTest):
         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")