]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Adjusted the __contains__() method of
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 5 Jun 2011 00:57:16 +0000 (20:57 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 5 Jun 2011 00:57:16 +0000 (20:57 -0400)
a RowProxy result row such that no exception
throw is generated internally;
NoSuchColumnError() also will generate its
message regardless of whether or not the column
construct can be coerced to a string.
[ticket:2178].

CHANGES
lib/sqlalchemy/engine/base.py
lib/sqlalchemy/sql/expression.py
test/aaa_profiling/test_resultset.py
test/sql/test_query.py

diff --git a/CHANGES b/CHANGES
index 1f86681fe5011e41f2f8c09c335eecbd1e3d4f89..da822abd925c93e0b6a912631e0779c1f79aae5e 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -69,6 +69,15 @@ CHANGES
     break an ORM column_property() mapping against
     another column_property().  [ticket:2167].
 
+- engine
+  - Adjusted the __contains__() method of 
+    a RowProxy result row such that no exception
+    throw is generated internally;  
+    NoSuchColumnError() also will generate its 
+    message regardless of whether or not the column 
+    construct can be coerced to a string.  
+    [ticket:2178].
+
 - postgresql
 
   - Fixed bug affecting PG 9 whereby index reflection
index 240651c65c80be662218e4f8cc9e0f1de19c1970..a3ed5bac164fa0626ee435c070fd5977bd684b70 100644 (file)
@@ -2174,7 +2174,7 @@ class ResultMetaData(object):
             self.logger.debug(
                 "Col %r", tuple(x[0] for x in metadata))
 
-    def _key_fallback(self, key):
+    def _key_fallback(self, key, raiseerr=True):
         map = self._keymap
         result = None
         if isinstance(key, basestring):
@@ -2188,8 +2188,12 @@ class ResultMetaData(object):
             elif hasattr(key, 'name') and key.name.lower() in map:
                 result = map[key.name.lower()]
         if result is None:
-            raise exc.NoSuchColumnError(
-                "Could not locate column in row for column '%s'" % key)
+            if raiseerr:
+                raise exc.NoSuchColumnError(
+                    "Could not locate column in row for column '%s'" % 
+                        expression._string_or_unprintable(key))
+            else:
+                return None
         else:
             map[key] = result
         return result
@@ -2198,11 +2202,7 @@ class ResultMetaData(object):
         if key in self._keymap:
             return True
         else:
-            try:
-                self._key_fallback(key)
-                return True
-            except exc.NoSuchColumnError:
-                return False
+            return self._key_fallback(key, False) is not None
 
     def __len__(self):
         return len(self.keys)
index 75e33b6b4d34e8dfefbe2f5cfe17ee0a60a1b50c..30d713f2f0c64e88b5707732abe99a31f05eca0a 100644 (file)
@@ -1051,6 +1051,15 @@ def _escape_for_generated(x):
     else:
         return x.replace('%', '%%')
 
+def _string_or_unprintable(element):
+    if isinstance(element, basestring):
+        return element
+    else:
+        try:
+            return str(element)
+        except:
+            return "unprintable element %r" % element
+
 def _clone(element):
     return element._clone()
 
index bd9d3ae50f20b236e5f3ab84a7db348dec11faf1..b32492e39a51ce8f5c0b260094a5d7501d7590ea 100644 (file)
@@ -40,3 +40,11 @@ class ResultSetTest(TestBase, AssertsExecutionResults):
                                    '2.6+cextension': 409, '2.7+cextension':409})
     def test_unicode(self):
         [tuple(row) for row in t2.select().execute().fetchall()]
+
+    def test_contains_doesnt_compile(self):
+        row = t.select().execute().first()
+        c1 = Column('some column', Integer) + Column("some other column", Integer)
+        @profiling.function_call_count(9)
+        def go():
+            c1 in row
+        go()
index 2b4e7a71ae166a4990f77760732f921a465996bc..4428a65be1f92ded032fe40db158cca4e63155ea 100644 (file)
@@ -341,6 +341,25 @@ class QueryTest(TestBase):
                 assert_raises(exc.NoSuchColumnError, lambda: result[0]['fake key'])
                 assert_raises(exc.NoSuchColumnError, lambda: result[0][addresses.c.address_id])
 
+    def test_column_error_printing(self):
+        row = testing.db.execute(select([1])).first()
+        class unprintable(object):
+            def __str__(self):
+                raise ValueError("nope")
+
+        msg = r"Could not locate column in row for column '%s'"
+
+        for accessor, repl in [
+            ("x", "x"),
+            (Column("q", Integer), "q"),
+            (Column("q", Integer) + 12, r"q \+ :q_1"),
+            (unprintable(), "unprintable element.*"),
+        ]:
+            assert_raises_message(
+                exc.NoSuchColumnError, 
+                msg % repl,
+                lambda: row[accessor]
+            )
 
 
     @testing.requires.boolean_col_expressions