]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] Fixed issue whereby attribute-based
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 14 Mar 2012 21:34:36 +0000 (14:34 -0700)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 14 Mar 2012 21:34:36 +0000 (14:34 -0700)
column access on a row would raise
AttributeError with non-C version,
NoSuchColumnError with C version.  Now
raises AttributeError in both cases.
[ticket:2398]

CHANGES
lib/sqlalchemy/cextension/resultproxy.c
lib/sqlalchemy/engine/base.py
test/sql/test_query.py

diff --git a/CHANGES b/CHANGES
index 98410bbeaab889421e7e419d30e3ec24682a16df..67e5335ee22c80cca88fa9c061ad1e7ad99c92e6 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -93,6 +93,13 @@ CHANGES
     in particular when orm query.count()
     were called.  [ticket:2427]
 
+  - [bug] Fixed issue whereby attribute-based
+    column access on a row would raise 
+    AttributeError with non-C version,
+    NoSuchColumnError with C version.  Now
+    raises AttributeError in both cases.
+    [ticket:2398]
+
   - [feature] Added support for SQL standard
     common table expressions (CTE), allowing
     SELECT objects as the CTE source (DML
index 325007c117444b842e7026848c24618b61d90d01..3494ccae65668c1dc2ae63cd585a0fbb7ef0297d 100644 (file)
@@ -353,7 +353,16 @@ BaseRowProxy_getattro(BaseRowProxy *self, PyObject *name)
     else
         return tmp;
 
-    return BaseRowProxy_subscript(self, name);
+    tmp = BaseRowProxy_subscript(self, name);
+    if (tmp == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) {
+        PyErr_Format(
+                PyExc_AttributeError, 
+                "Could not locate column in row for column '%.200s'",
+                PyString_AsString(name)
+            );
+        return NULL;
+    }
+    return tmp;
 }
 
 /***********************
index 7cc5522c41d5c683b36b96b03960a024baabcde0..d16fc9c6843501140ce6642cbe0aaf30e972ec04 100644 (file)
@@ -2623,7 +2623,6 @@ except ImportError:
 
         def __getattr__(self, name):
             try:
-                # TODO: no test coverage here
                 return self[name]
             except KeyError, e:
                 raise AttributeError(e.args[0])
index 50e7eece714fa0321b97ef8617eb5d060e97a6a1..9f0c2dab09ce7f9fe98d6e48c8abc5c29eabb4dc 100644 (file)
@@ -749,6 +749,19 @@ class QueryTest(fixtures.TestBase):
         eq_(r[users.c.user_name], 'jack')
         eq_(r.user_name, 'jack')
 
+    def test_column_accessor_err(self):
+        r = testing.db.execute(select([1])).first()
+        assert_raises_message(
+            AttributeError,
+            "Could not locate column in row for column 'foo'",
+            getattr, r, "foo"
+        )
+        assert_raises_message(
+            KeyError,
+            "Could not locate column in row for column 'foo'",
+            lambda: r['foo']
+        )
+
     @testing.requires.dbapi_lastrowid
     def test_native_lastrowid(self):
         r = testing.db.execute(