Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c28dbd04
Commit
c28dbd04
authored
Dec 16, 2016
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Plain Diff
merge
parents
10a22dc6
af56e0e7
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
2 deletions
+20
-2
Lib/functools.py
Lib/functools.py
+4
-2
Lib/test/test_functools.py
Lib/test/test_functools.py
+13
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/functools.py
View file @
c28dbd04
...
...
@@ -574,14 +574,16 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
last
=
root
[
PREV
]
link
=
[
last
,
root
,
key
,
result
]
last
[
NEXT
]
=
root
[
PREV
]
=
cache
[
key
]
=
link
full
=
(
len
(
cache
)
>=
maxsize
)
# Use the __len__() method instead of the len() function
# which could potentially be wrapped in an lru_cache itself.
full
=
(
cache
.
__len__
()
>=
maxsize
)
misses
+=
1
return
result
def
cache_info
():
"""Report cache statistics"""
with
lock
:
return
_CacheInfo
(
hits
,
misses
,
maxsize
,
len
(
cache
))
return
_CacheInfo
(
hits
,
misses
,
maxsize
,
cache
.
__len__
(
))
def
cache_clear
():
"""Clear the cache and cache statistics"""
...
...
Lib/test/test_functools.py
View file @
c28dbd04
import
abc
import
builtins
import
collections
import
copy
from
itertools
import
permutations
...
...
@@ -1189,6 +1190,18 @@ class TestLRU:
self
.
assertEqual
(
misses
,
4
)
self
.
assertEqual
(
currsize
,
2
)
def
test_lru_reentrancy_with_len
(
self
):
# Test to make sure the LRU cache code isn't thrown-off by
# caching the built-in len() function. Since len() can be
# cached, we shouldn't use it inside the lru code itself.
old_len
=
builtins
.
len
try
:
builtins
.
len
=
self
.
module
.
lru_cache
(
4
)(
len
)
for
i
in
[
0
,
0
,
1
,
2
,
3
,
3
,
4
,
5
,
6
,
1
,
7
,
2
,
1
]:
self
.
assertEqual
(
len
(
'abcdefghijklmn'
[:
i
]),
i
)
finally
:
builtins
.
len
=
old_len
def
test_lru_type_error
(
self
):
# Regression test for issue #28653.
# lru_cache was leaking when one of the arguments
...
...
Misc/NEWS
View file @
c28dbd04
...
...
@@ -14,6 +14,9 @@ Core and Builtins
to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
os.fsencode() and os.fsdecode() which are already using UTF-8.
- Issue #28991: functools.lru_cache() was susceptible to an obscure $
bug triggerable by a monkey-patched len() function.
- Issue #28147: Fix a memory leak in split-table dictionaries: setattr()
must not convert combined table into split table. Patch written by INADA
Naoki.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment