From 7ad0c8cb2b76c1de223787785cf5c77d43192db2 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 14 Mar 2012 14:34:36 -0700 Subject: [PATCH] - [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] --- CHANGES | 7 +++++++ lib/sqlalchemy/cextension/resultproxy.c | 11 ++++++++++- lib/sqlalchemy/engine/base.py | 1 - test/sql/test_query.py | 13 +++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 98410bbeaa..67e5335ee2 100644 --- 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 diff --git a/lib/sqlalchemy/cextension/resultproxy.c b/lib/sqlalchemy/cextension/resultproxy.c index 325007c117..3494ccae65 100644 --- a/lib/sqlalchemy/cextension/resultproxy.c +++ b/lib/sqlalchemy/cextension/resultproxy.c @@ -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; } /*********************** diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 7cc5522c41..d16fc9c684 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -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]) diff --git a/test/sql/test_query.py b/test/sql/test_query.py index 50e7eece71..9f0c2dab09 100644 --- a/test/sql/test_query.py +++ b/test/sql/test_query.py @@ -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( -- 2.47.2