Skip to content

Commit

Permalink
fix NamedTupleCursor._cached_make_nt
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Jun 4, 2019
1 parent 527592a commit 842e383
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions lib/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_extras_dictcursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 842e383

Please sign in to comment.