From c20c13c49374d05dc9481928208a5c13bcb09f45 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 9 Dec 2019 11:09:09 +0000 Subject: [PATCH] Fixed use of cursor_factory attribute in connecion subclasses Close #1019 --- NEWS | 7 +++++++ lib/extras.py | 11 ++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index ccb07dc6..0968b1f2 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,13 @@ Current release --------------- +What's new in psycopg 2.8.5 +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Fixed use of `!connection_factory` and `!cursor_factory` together + (:ticket:`#1019`). + + What's new in psycopg 2.8.4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/lib/extras.py b/lib/extras.py index afc31c32..f6d17183 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -124,7 +124,7 @@ class DictCursorBase(_cursor): class DictConnection(_connection): """A connection that uses `DictCursor` automatically.""" def cursor(self, *args, **kwargs): - kwargs.setdefault('cursor_factory', DictCursor) + kwargs.setdefault('cursor_factory', self.cursor_factory or DictCursor) return super(DictConnection, self).cursor(*args, **kwargs) @@ -221,7 +221,7 @@ class DictRow(list): class RealDictConnection(_connection): """A connection that uses `RealDictCursor` automatically.""" def cursor(self, *args, **kwargs): - kwargs.setdefault('cursor_factory', RealDictCursor) + kwargs.setdefault('cursor_factory', self.cursor_factory or RealDictCursor) return super(RealDictConnection, self).cursor(*args, **kwargs) @@ -291,7 +291,7 @@ class RealDictRow(OrderedDict): class NamedTupleConnection(_connection): """A connection that uses `NamedTupleCursor` automatically.""" def cursor(self, *args, **kwargs): - kwargs.setdefault('cursor_factory', NamedTupleCursor) + kwargs.setdefault('cursor_factory', self.cursor_factory or NamedTupleCursor) return super(NamedTupleConnection, self).cursor(*args, **kwargs) @@ -442,7 +442,7 @@ class LoggingConnection(_connection): def cursor(self, *args, **kwargs): self._check() - kwargs.setdefault('cursor_factory', LoggingCursor) + kwargs.setdefault('cursor_factory', self.cursor_factory or LoggingCursor) return super(LoggingConnection, self).cursor(*args, **kwargs) @@ -485,7 +485,8 @@ class MinTimeLoggingConnection(LoggingConnection): return msg + _os.linesep + " (execution time: %d ms)" % t def cursor(self, *args, **kwargs): - kwargs.setdefault('cursor_factory', MinTimeLoggingCursor) + kwargs.setdefault('cursor_factory', + self.cursor_factory or MinTimeLoggingCursor) return LoggingConnection.cursor(self, *args, **kwargs)