mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-07-10 16:22:33 +03:00
fix NamedTupleCursor._cached_make_nt
This commit is contained in:
parent
527592a0a5
commit
842e383c0c
|
@ -372,10 +372,11 @@ class NamedTupleCursor(_cursor):
|
||||||
key = tuple(d[0] for d in self.description) if self.description else ()
|
key = tuple(d[0] for d in self.description) if self.description else ()
|
||||||
return self._cached_make_nt(key)
|
return self._cached_make_nt(key)
|
||||||
|
|
||||||
def _do_make_nt(self, key):
|
@classmethod
|
||||||
|
def _do_make_nt(cls, key):
|
||||||
fields = []
|
fields = []
|
||||||
for s in key:
|
for s in key:
|
||||||
s = self._re_clean.sub('_', s)
|
s = cls._re_clean.sub('_', s)
|
||||||
# Python identifier cannot start with numbers, namedtuple fields
|
# Python identifier cannot start with numbers, namedtuple fields
|
||||||
# cannot start with underscore. So...
|
# cannot start with underscore. So...
|
||||||
if s[0] == '_' or '0' <= s[0] <= '9':
|
if s[0] == '_' or '0' <= s[0] <= '9':
|
||||||
|
@ -385,9 +386,14 @@ class NamedTupleCursor(_cursor):
|
||||||
nt = namedtuple("Record", fields)
|
nt = namedtuple("Record", fields)
|
||||||
return nt
|
return 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
|
# Exposed for testability, and if someone wants to monkeypatch to tweak
|
||||||
# the cache size.
|
# the cache size.
|
||||||
_cached_make_nt = lru_cache(512)(_do_make_nt)
|
NamedTupleCursor._cached_make_nt = classmethod(_cached_make_nt)
|
||||||
|
|
||||||
|
|
||||||
class LoggingConnection(_connection):
|
class LoggingConnection(_connection):
|
||||||
|
|
|
@ -639,7 +639,7 @@ class NamedTupleCursorTest(ConnectingTestCase):
|
||||||
def test_max_cache(self):
|
def test_max_cache(self):
|
||||||
old_func = NamedTupleCursor._cached_make_nt
|
old_func = NamedTupleCursor._cached_make_nt
|
||||||
NamedTupleCursor._cached_make_nt = \
|
NamedTupleCursor._cached_make_nt = \
|
||||||
lru_cache(8)(NamedTupleCursor._do_make_nt)
|
lru_cache(8)(NamedTupleCursor._cached_make_nt.__wrapped__)
|
||||||
try:
|
try:
|
||||||
recs = []
|
recs = []
|
||||||
curs = self.conn.cursor()
|
curs = self.conn.cursor()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user