|
180 | 180 | from postgres.cursors import SimpleTupleCursor, SimpleNamedTupleCursor
|
181 | 181 | from postgres.cursors import SimpleDictCursor, SimpleCursorBase
|
182 | 182 | from postgres.orm import Model
|
| 183 | +from psycopg2 import DataError |
183 | 184 | from psycopg2.extras import register_composite, CompositeCaster
|
184 | 185 | from psycopg2.pool import ThreadedConnectionPool as ConnectionPool
|
185 | 186 |
|
@@ -825,29 +826,18 @@ def make_DelegatingCaster(postgres):
|
825 | 826 | """
|
826 | 827 | class DelegatingCaster(CompositeCaster):
|
827 | 828 |
|
828 |
| - def parse(self, s, curs, retry=False): |
| 829 | + def parse(self, s, curs, retry=True): |
829 | 830 | # Override to protect against race conditions:
|
830 | 831 | # https://github.com/gratipay/postgres.py/issues/26
|
831 | 832 |
|
832 |
| - if s is None: |
833 |
| - return None |
834 |
| - |
835 |
| - tokens = self.tokenize(s) |
836 |
| - if len(tokens) != len(self.atttypes): |
837 |
| - # The number of columns has changed, re-fetch the type info |
838 |
| - self._refetch_type_info(curs) |
839 |
| - |
840 | 833 | try:
|
841 |
| - values = [ curs.cast(oid, token) |
842 |
| - for oid, token in zip(self.atttypes, tokens) ] |
843 |
| - except ValueError: |
844 |
| - # The type of a column has changed, re-fetch it and retry once |
845 |
| - if retry: |
| 834 | + return super(DelegatingCaster, self).parse(s, curs) |
| 835 | + except (DataError, ValueError): |
| 836 | + if not retry: |
846 | 837 | raise
|
| 838 | + # Re-fetch the type info and retry once |
847 | 839 | self._refetch_type_info(curs)
|
848 |
| - return self.parse(s, curs, True) |
849 |
| - |
850 |
| - return self.make(values) |
| 840 | + return self.parse(s, curs, False) |
851 | 841 |
|
852 | 842 | def make(self, values):
|
853 | 843 | # Override to delegate to the model registry.
|
|
0 commit comments