From 842e383c0c0e61f03ab6aa822fdf14c61c43d394 Mon Sep 17 00:00:00 2001 From: Changaco Date: Tue, 4 Jun 2019 14:29:06 +0200 Subject: [PATCH] fix `NamedTupleCursor._cached_make_nt` --- lib/extras.py | 16 +++++++++++----- tests/test_extras_dictcursor.py | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/extras.py b/lib/extras.py index 3fdaaf0a4..257153ef8 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -372,10 +372,11 @@ def _make_nt(self): 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 @@ def _do_make_nt(self, key): 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 a4f45c29a..5f205ca90 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -639,7 +639,7 @@ def test_cache(self): 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()