Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
d8886fc8
Kaydet (Commit)
d8886fc8
authored
Eki 16, 2011
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
Merge
üst
e6069831
4b779b37
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
15 deletions
+35
-15
functools.py
Lib/functools.py
+19
-15
test_functools.py
Lib/test/test_functools.py
+16
-0
No files found.
Lib/functools.py
Dosyayı görüntüle @
d8886fc8
...
...
@@ -146,7 +146,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
...
...
@@ -160,13 +160,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
...
...
@@ -176,18 +178,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
Dosyayı görüntüle @
d8886fc8
...
...
@@ -718,6 +718,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
,
...
...
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