]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug whereby mapper mapped to an anonymous
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 20 May 2011 18:45:20 +0000 (14:45 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 20 May 2011 18:45:20 +0000 (14:45 -0400)
alias would fail if logging were used, due to
unescaped % sign in the alias name.  [ticket:2171]

CHANGES
lib/sqlalchemy/orm/mapper.py
test/orm/test_mapper.py

diff --git a/CHANGES b/CHANGES
index 801741f25f3ee71bd126ea73142a588b21512bc7..bbf39408aaee7f25b3fc182d6f6678d092967ef1 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -46,6 +46,10 @@ CHANGES
     how join() itself determines an FK error.
     [ticket:2153]
 
+  - Fixed bug whereby mapper mapped to an anonymous
+    alias would fail if logging were used, due to 
+    unescaped % sign in the alias name.  [ticket:2171]
+
 - sql
   - Fixed bug whereby if FetchedValue was passed
     to column server_onupdate, it would not
index e9caa6da59675c936b69ffce6bffa3965bbd3b85..de0023ddf7db5f2be531cc26a095bd25289492b5 100644 (file)
@@ -876,25 +876,26 @@ class Mapper(object):
         for mapper in self.iterate_to_root():
             _memoized_compiled_property.expire_instance(mapper)
 
-    def _log(self, msg, *args):
-        self.logger.info(
-            "(" + self.class_.__name__ + 
-            "|" + 
+    @property
+    def _log_desc(self):
+        return "(" + self.class_.__name__ + \
+            "|" + \
             (self.local_table is not None and 
                 self.local_table.description or 
-                str(self.local_table)) +
-            (self.non_primary and "|non-primary" or "") + ") " + 
-            msg, *args)
+                str(self.local_table)) +\
+            (self.non_primary and 
+            "|non-primary" or "") + ")"
+
+    def _log(self, msg, *args):
+
+        self.logger.info(
+            "%s " + msg, *((self._log_desc,) + args)
+        )
 
     def _log_debug(self, msg, *args):
         self.logger.debug(
-            "(" + self.class_.__name__ + 
-            "|" + 
-            (self.local_table is not None and 
-                self.local_table.description 
-                or str(self.local_table)) + 
-            (self.non_primary and "|non-primary" or "") + ") " + 
-            msg, *args)
+            "%s " + msg, *((self._log_desc,) + args)
+        )
 
     def __repr__(self):
         return '<Mapper at 0x%x; %s>' % (
index 4c82bc50b79bd8b547f850382c399b1644432e31..fa8bd70f0446378c141c41e09d0a41ec71fd06d5 100644 (file)
@@ -15,7 +15,7 @@ from sqlalchemy.orm import defer, deferred, synonym, attributes, \
 from sqlalchemy.test.testing import eq_, AssertsCompiledSQL
 from test.orm import _base, _fixtures
 from sqlalchemy.test.assertsql import AllOf, CompiledSQL
-
+import logging
 
 class MapperTest(_fixtures.FixtureTest):
 
@@ -1281,7 +1281,34 @@ class DocumentTest(testing.TestBase):
         eq_(Bar.col1.__doc__, "primary key column")
         eq_(Bar.foo.__doc__, "foo relationship")
 
+class ORMLoggingTest(_fixtures.FixtureTest):
+    def setup(self):
+        self.buf = logging.handlers.BufferingHandler(100)
+        for log in [
+            logging.getLogger('sqlalchemy.orm'),
+        ]:
+            log.addHandler(self.buf)
+            log.setLevel(logging.DEBUG)
+
+    def teardown(self):
+        for log in [
+            logging.getLogger('sqlalchemy.orm'),
+        ]:
+            log.removeHandler(self.buf)
+
+    def _current_messages(self):
+        return [b.getMessage() for b in self.buf.buffer]
+
+    def test_mapper_info_aliased(self):
+        User, users = self.classes.User, self.tables.users
+        tb = users.select().alias()
+        mapper(User, tb)
+        s = Session()
+        s.add(User(name='ed'))
+        s.commit()
 
+        for msg in self._current_messages():
+            assert msg.startswith('(User|%%(%d anon)s) ' % id(tb))
 
 class OptionsTest(_fixtures.FixtureTest):