]> 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:52:30 +0000 (20:52 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 5 Jun 2011 00:52:30 +0000 (20:52 -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].  Also in 0.6.8.

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 22e6a5d2b972ae6a3c8dfc117201738561b68ed6..2014abf94e309c4b876e7c505a647dc17cb68e18 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -44,6 +44,14 @@ CHANGES
     and are redundant:  reflecttable(), create(), 
     drop(), text(), engine.func
 
+  - 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].  Also in 0.6.8.
+
 - sqlite
   - Accept None from cursor.fetchone() when
     "PRAGMA read_uncommitted" is called to determine
index e23465c224c1ccfa3d34175e1b84a333830034c7..04636b5e577916cd3a30cc2d3725e3632439d191 100644 (file)
@@ -2578,7 +2578,7 @@ class ResultMetaData(object):
         if self._keymap.setdefault(name, rec) is not rec:
             self._keymap[name] = (processor, None)
 
-    def _key_fallback(self, key):
+    def _key_fallback(self, key, raiseerr=True):
         map = self._keymap
         result = None
         if isinstance(key, basestring):
@@ -2592,8 +2592,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
@@ -2602,11 +2606,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 __getstate__(self):
         return {
index 38eb71da87530ce16079adddc8853647d3937068..ec08014053545eb941f268193fea5ff9b23d162c 100644 (file)
@@ -1207,6 +1207,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 e2c6ecd500c26ee233b513763d4ece57fc4e2f0a..451912c8f09d108093ccba660039a501f23cbc23 100644 (file)
@@ -52,6 +52,14 @@ class ResultSetTest(fixtures.TestBase, AssertsExecutionResults):
     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()
+
 class ExecutionTest(fixtures.TestBase):
     __requires__ = 'cpython',
     __only_on__ = 'sqlite'
index c295cd282584cff4447a39794c43e7b9a3b49c03..359123f57ce092b627c027fdf49842d8ebd7ea6a 100644 (file)
@@ -341,6 +341,25 @@ class QueryTest(fixtures.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