diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-12-06 12:49:39 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-12-06 12:49:39 -0500 |
commit | 5c9d53fb02effed81329ca7964f99777f2ab6ec4 (patch) | |
tree | 3039aba6b3aaebf7a85d1a11457b426f76b7c82f | |
parent | 1dea4c1f87a43e668525b4c917cdc9eb4b56f218 (diff) | |
download | sqlalchemy-5c9d53fb02effed81329ca7964f99777f2ab6ec4.tar.gz |
- [bug] the @compiles decorator raises an
informative error message when no "default"
compilation handler is present, rather
than KeyError.
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | lib/sqlalchemy/ext/compiler.py | 8 | ||||
-rw-r--r-- | test/ext/test_compiler.py | 17 |
3 files changed, 29 insertions, 1 deletions
@@ -261,6 +261,11 @@ CHANGES case here is fairly experimental, but only adds one line of code to Query. + - [bug] the @compiles decorator raises an + informative error message when no "default" + compilation handler is present, rather + than KeyError. + - examples - [bug] Fixed bug in history_meta.py example where the "unique" flag was not removed from a diff --git a/lib/sqlalchemy/ext/compiler.py b/lib/sqlalchemy/ext/compiler.py index cb126c374..daa9e1551 100644 --- a/lib/sqlalchemy/ext/compiler.py +++ b/lib/sqlalchemy/ext/compiler.py @@ -367,6 +367,7 @@ Example usage:: ) """ +from sqlalchemy import exc def compiles(class_, *specs): def decorate(fn): @@ -399,6 +400,11 @@ class _dispatcher(object): # TODO: yes, this could also switch off of DBAPI in use. fn = self.specs.get(compiler.dialect.name, None) if not fn: - fn = self.specs['default'] + try: + fn = self.specs['default'] + except KeyError: + raise exc.CompileError( + "%s construct has no default " + "compilation handler." % type(element)) return fn(element, compiler, **kw) diff --git a/test/ext/test_compiler.py b/test/ext/test_compiler.py index 0f53f2cb0..318a1e76c 100644 --- a/test/ext/test_compiler.py +++ b/test/ext/test_compiler.py @@ -6,7 +6,9 @@ from sqlalchemy.sql.expression import ClauseElement, ColumnClause,\ from sqlalchemy.schema import DDLElement from sqlalchemy.ext.compiler import compiles +from sqlalchemy import exc from sqlalchemy.sql import table, column, visitors +from test.lib.testing import assert_raises_message from test.lib import * class UserDefinedTest(fixtures.TestBase, AssertsCompiledSQL): @@ -105,6 +107,21 @@ class UserDefinedTest(fixtures.TestBase, AssertsCompiledSQL): "FROM mytable WHERE mytable.x > :x_1)" ) + def test_no_default_message(self): + class MyThingy(ColumnClause): + pass + + @compiles(MyThingy, "psotgresql") + def visit_thingy(thingy, compiler, **kw): + return "mythingy" + + assert_raises_message( + exc.CompileError, + "<class 'test.ext.test_compiler.MyThingy'> " + "construct has no default compilation handler.", + str, MyThingy('x') + ) + def test_annotations(self): """test that annotated clause constructs use the decorated class' compiler. |