[python-sybase] How to deal with DatabaseError

Greg Ward gward-pysybase at python.net
Thu Jul 20 02:12:50 EST 2006


On 19 July 2006, I said:
> Obviously this is no good unless all drivers that you want to use support
> it.  I'll go see if I can whip up a patch for python-sybase.  In the worst
> case, I could monkey-patch those attributes in to every Connection in my
> abstraction layer.  (Blech.)

Patch attached.

        Greg
-------------- next part --------------
--- Sybase.py.2.checkready	2006-07-07 09:10:29.000000000 -0400
+++ Sybase.py	2006-07-19 12:08:28.000000000 -0400
@@ -865,6 +865,26 @@
         if not delay_connect:
             self.connect()
 
+    def __getattr__(self, name):
+        # Expose exception classes via the Connection object so
+        # programmers don't have to tie their code to this module with
+        # "from Sybase import DatabaseError" all over the place.  See
+        # PEP 249, "Optional DB API Extensions".
+        names = ('Error',
+                 'InterfaceError',
+                 'DatabaseError',
+                 'DataError',
+                 'OperationalError',
+                 'IntegrityError',
+                 'InternalError',
+                 'ProgrammingError',
+                 'NotSupportedError',
+                )
+        if name in names:
+            return getattr(sys.modules[self.__module__], name)
+        else:
+            raise AttributeError(name)
+
     def _lock(self):
         if self._do_locking:
             self._connlock.acquire()


More information about the Python-sybase mailing list