From 323f1b358de19cc9cbfd45408636aa58060adf15 Mon Sep 17 00:00:00 2001 From: Brad Allen Date: Sat, 20 Mar 2010 21:58:32 -0600 Subject: [PATCH] now loading mxODBC exception classes into module namespace while still avoiding module imports (hooking into dbapi class method) --- lib/sqlalchemy/connectors/mxodbc.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/sqlalchemy/connectors/mxodbc.py b/lib/sqlalchemy/connectors/mxodbc.py index 0c7e5ad06e..ac7075209c 100644 --- a/lib/sqlalchemy/connectors/mxodbc.py +++ b/lib/sqlalchemy/connectors/mxodbc.py @@ -31,6 +31,9 @@ class MxODBCConnector(Connector): @classmethod def dbapi(cls): + # this classmethod will normally be replaced by an instance + # attribute of the same name, so this is normally only called once. + cls._load_mx_exceptions() platform = sys.platform if platform == 'win32': from mx.ODBC import Windows as module @@ -43,6 +46,16 @@ class MxODBCConnector(Connector): raise ImportError, "Unrecognized platform for mxODBC import" return module + @classmethod + def _load_mx_exceptions(cls): + """ Import mxODBC exception classes into the module namespace, + as if they had been imported normally. This is done here + to avoid requiring all SQLAlchemy users to install mxODBC. + """ + global InterfaceError, ProgrammingError + from mx.ODBC import InterfaceError + from mx.ODBC import ProgrammingError + def on_connect(self): def connect(conn): conn.stringformat = self.dbapi.MIXED_STRINGFORMAT -- 2.47.3