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
4b779b37
Commit
4b779b37
authored
Oct 15, 2011
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 13177: Make tracebacks more readable by avoiding chained exceptions in the lru_cache.
parent
9be57231
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
15 deletions
+39
-15
Lib/functools.py
Lib/functools.py
+19
-15
Lib/test/test_functools.py
Lib/test/test_functools.py
+16
-0
Misc/NEWS
Misc/NEWS
+4
-0
No files found.
Lib/functools.py
View file @
4b779b37
...
...
@@ -141,7 +141,7 @@ def lru_cache(maxsize=100):
hits
=
misses
=
0
kwd_mark
=
(
object
(),)
# separates positional and keyword args
lock
=
Lock
()
# needed because
ordereddicts are
n't threadsafe
lock
=
Lock
()
# needed because
OrderedDict is
n't threadsafe
if
maxsize
is
None
:
cache
=
dict
()
# simple cache without ordering or size limit
...
...
@@ -155,13 +155,15 @@ def lru_cache(maxsize=100):
try
:
result
=
cache
[
key
]
hits
+=
1
return
result
except
KeyError
:
result
=
user_function
(
*
args
,
**
kwds
)
cache
[
key
]
=
result
misses
+=
1
pass
result
=
user_function
(
*
args
,
**
kwds
)
cache
[
key
]
=
result
misses
+=
1
return
result
else
:
cache
=
OrderedDict
()
# ordered least recent to most recent
cache
=
OrderedDict
()
# ordered least recent to most recent
cache_popitem
=
cache
.
popitem
cache_renew
=
cache
.
move_to_end
...
...
@@ -171,18 +173,20 @@ def lru_cache(maxsize=100):
key
=
args
if
kwds
:
key
+=
kwd_mark
+
tuple
(
sorted
(
kwds
.
items
()))
try
:
with
lock
:
with
lock
:
try
:
result
=
cache
[
key
]
cache_renew
(
key
)
# record recent use of this key
cache_renew
(
key
)
# record recent use of this key
hits
+=
1
except
KeyError
:
result
=
user_function
(
*
args
,
**
kwds
)
with
lock
:
cache
[
key
]
=
result
# record recent use of this key
misses
+=
1
if
len
(
cache
)
>
maxsize
:
cache_popitem
(
0
)
# purge least recently used cache entry
return
result
except
KeyError
:
pass
result
=
user_function
(
*
args
,
**
kwds
)
with
lock
:
cache
[
key
]
=
result
# record recent use of this key
misses
+=
1
if
len
(
cache
)
>
maxsize
:
cache_popitem
(
0
)
# purge least recently used cache entry
return
result
def
cache_info
():
...
...
Lib/test/test_functools.py
View file @
4b779b37
...
...
@@ -655,6 +655,22 @@ class TestLRU(unittest.TestCase):
self
.
assertEqual
(
fib
.
cache_info
(),
functools
.
_CacheInfo
(
hits
=
0
,
misses
=
0
,
maxsize
=
None
,
currsize
=
0
))
def
test_lru_with_exceptions
(
self
):
# Verify that user_function exceptions get passed through without
# creating a hard-to-read chained exception.
# http://bugs.python.org/issue13177
for
maxsize
in
(
None
,
100
):
@
functools
.
lru_cache
(
maxsize
)
def
func
(
i
):
return
'abc'
[
i
]
self
.
assertEqual
(
func
(
0
),
'a'
)
with
self
.
assertRaises
(
IndexError
)
as
cm
:
func
(
15
)
self
.
assertIsNone
(
cm
.
exception
.
__context__
)
# Verify that the previous exception did not result in a cached entry
with
self
.
assertRaises
(
IndexError
):
func
(
15
)
def
test_main
(
verbose
=
None
):
test_classes
=
(
TestPartial
,
...
...
Misc/NEWS
View file @
4b779b37
...
...
@@ -46,6 +46,10 @@ Library
- Issue #13158: Fix decoding and encoding of GNU tar specific base-256 number
fields in tarfile.
- Issue #13177: Functools lru_cache() no longer calls the original function
inside an exception handler. This makes tracebacks easier to read because
chained exceptions are avoided.
- Issue #13025: mimetypes is now reading MIME types using the UTF-8 encoding,
instead of the locale encoding.
...
...
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