Title: [970] trunk/pg.py: Avoid raising AttributeError
Revision
970
Author
cito
Date
2019-04-19 09:39:56 -0400 (Fri, 19 Apr 2019)

Log Message

Avoid raising AttributeError

This might be slightly better in Python 3.
Also, handle the case when an AttributeError is raised
inside the pg_repr or pg_str method.

Modified Paths

Diff

Modified: trunk/pg.py (969 => 970)


--- trunk/pg.py	2019-04-19 13:35:23 UTC (rev 969)
+++ trunk/pg.py	2019-04-19 13:39:56 UTC (rev 970)
@@ -544,10 +544,9 @@
                 simple = self.get_simple_name(typ)
             else:
                 typ = simple = self.guess_simple_type(value) or 'text'
-            try:
-                value = value.__pg_str__(typ)
-            except AttributeError:
-                pass
+            pg_str = getattr(value, '__pg_str__', None)
+            if pg_str:
+                value = pg_str(typ)
             if simple == 'text':
                 pass
             elif simple == 'record':
@@ -659,11 +658,11 @@
         if isinstance(value, tuple):
             q = self.adapt_inline
             return '(%s)' % ','.join(str(q(v)) for v in value)
-        try:
-            value = value.__pg_repr__()
-        except AttributeError:
+        pg_repr = getattr(value, '__pg_repr__', None)
+        if not pg_repr:
             raise InterfaceError(
                 'Do not know how to adapt type %s' % type(value))
+        value = pg_repr()
         if isinstance(value, (tuple, list)):
             value = self.adapt_inline(value)
         return value
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo/pygresql

Reply via email to