Skip to content

Commit 363123f

Browse files
committed
call CompositeCaster.parse() instead of duplicating its code
1 parent 0f422eb commit 363123f

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

postgres/__init__.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
from postgres.cursors import SimpleTupleCursor, SimpleNamedTupleCursor
181181
from postgres.cursors import SimpleDictCursor, SimpleCursorBase
182182
from postgres.orm import Model
183+
from psycopg2 import DataError
183184
from psycopg2.extras import register_composite, CompositeCaster
184185
from psycopg2.pool import ThreadedConnectionPool as ConnectionPool
185186

@@ -825,29 +826,18 @@ def make_DelegatingCaster(postgres):
825826
"""
826827
class DelegatingCaster(CompositeCaster):
827828

828-
def parse(self, s, curs, retry=False):
829+
def parse(self, s, curs, retry=True):
829830
# Override to protect against race conditions:
830831
# https://github.com/gratipay/postgres.py/issues/26
831832

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-
840833
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:
846837
raise
838+
# Re-fetch the type info and retry once
847839
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)
851841

852842
def make(self, values):
853843
# Override to delegate to the model registry.

0 commit comments

Comments
 (0)