summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChangaco <changaco@changaco.oy.lc>2019-06-04 14:29:06 +0200
committerChangaco <changaco@changaco.oy.lc>2019-06-04 14:30:30 +0200
commit842e383c0c0e61f03ab6aa822fdf14c61c43d394 (patch)
treec558ef1393a1b1fe7824b47c06b75fdbaf2af273
parent527592a0a52b8c6a09535e785dac63c36cf25ada (diff)
downloadpsycopg2-842e383c0c0e61f03ab6aa822fdf14c61c43d394.tar.gz
fix `NamedTupleCursor._cached_make_nt`
-rw-r--r--lib/extras.py16
-rwxr-xr-xtests/test_extras_dictcursor.py2
2 files changed, 12 insertions, 6 deletions
diff --git a/lib/extras.py b/lib/extras.py
index 3fdaaf0..257153e 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -372,10 +372,11 @@ class NamedTupleCursor(_cursor):
key = tuple(d[0] for d in self.description) if self.description else ()
return self._cached_make_nt(key)
- def _do_make_nt(self, key):
+ @classmethod
+ def _do_make_nt(cls, key):
fields = []
for s in key:
- s = self._re_clean.sub('_', s)
+ s = cls._re_clean.sub('_', s)
# Python identifier cannot start with numbers, namedtuple fields
# cannot start with underscore. So...
if s[0] == '_' or '0' <= s[0] <= '9':
@@ -385,9 +386,14 @@ class NamedTupleCursor(_cursor):
nt = namedtuple("Record", fields)
return nt
- # Exposed for testability, and if someone wants to monkeypatch to tweak
- # the cache size.
- _cached_make_nt = lru_cache(512)(_do_make_nt)
+
+@lru_cache(512)
+def _cached_make_nt(cls, key):
+ return cls._do_make_nt(key)
+
+# Exposed for testability, and if someone wants to monkeypatch to tweak
+# the cache size.
+NamedTupleCursor._cached_make_nt = classmethod(_cached_make_nt)
class LoggingConnection(_connection):
diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py
index a4f45c2..5f205ca 100755
--- a/tests/test_extras_dictcursor.py
+++ b/tests/test_extras_dictcursor.py
@@ -639,7 +639,7 @@ class NamedTupleCursorTest(ConnectingTestCase):
def test_max_cache(self):
old_func = NamedTupleCursor._cached_make_nt
NamedTupleCursor._cached_make_nt = \
- lru_cache(8)(NamedTupleCursor._do_make_nt)
+ lru_cache(8)(NamedTupleCursor._cached_make_nt.__wrapped__)
try:
recs = []
curs = self.conn.cursor()