From: Mike Bayer Date: Tue, 6 Dec 2011 17:49:39 +0000 (-0500) Subject: - [bug] the @compiles decorator raises an X-Git-Tag: rel_0_7_4~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c9d53fb02effed81329ca7964f99777f2ab6ec4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - [bug] the @compiles decorator raises an informative error message when no "default" compilation handler is present, rather than KeyError. --- diff --git a/CHANGES b/CHANGES index 06c55158f2..3988ed22a4 100644 --- a/CHANGES +++ b/CHANGES @@ -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 cb126c374f..daa9e15518 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 0f53f2cb0c..318a1e76c4 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, + " " + "construct has no default compilation handler.", + str, MyThingy('x') + ) + def test_annotations(self): """test that annotated clause constructs use the decorated class' compiler.