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
414e15a8
Kaydet (Commit)
414e15a8
authored
Eki 05, 2014
tarafından
Antoine Pitrou
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Sade Fark
Closes #21173: Fix len() on a WeakKeyDictionary when .clear() was called with an iterator alive.
üst
861470c8
1bf974dc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
0 deletions
+52
-0
test_weakref.py
Lib/test/test_weakref.py
+36
-0
weakref.py
Lib/weakref.py
+13
-0
NEWS
Misc/NEWS
+3
-0
No files found.
Lib/test/test_weakref.py
Dosyayı görüntüle @
414e15a8
...
@@ -1310,6 +1310,36 @@ class MappingTestCase(TestBase):
...
@@ -1310,6 +1310,36 @@ class MappingTestCase(TestBase):
dict
.
clear
()
dict
.
clear
()
self
.
assertEqual
(
len
(
dict
),
0
)
self
.
assertEqual
(
len
(
dict
),
0
)
def
check_weak_del_and_len_while_iterating
(
self
,
dict
,
testcontext
):
# Check that len() works when both iterating and removing keys
# explicitly through various means (.pop(), .clear()...), while
# implicit mutation is deferred because an iterator is alive.
# (each call to testcontext() should schedule one item for removal
# for this test to work properly)
o
=
Object
(
123456
)
with
testcontext
():
n
=
len
(
dict
)
dict
.
popitem
()
self
.
assertEqual
(
len
(
dict
),
n
-
1
)
dict
[
o
]
=
o
self
.
assertEqual
(
len
(
dict
),
n
)
with
testcontext
():
self
.
assertEqual
(
len
(
dict
),
n
-
1
)
dict
.
pop
(
next
(
dict
.
keys
()))
self
.
assertEqual
(
len
(
dict
),
n
-
2
)
with
testcontext
():
self
.
assertEqual
(
len
(
dict
),
n
-
3
)
del
dict
[
next
(
dict
.
keys
())]
self
.
assertEqual
(
len
(
dict
),
n
-
4
)
with
testcontext
():
self
.
assertEqual
(
len
(
dict
),
n
-
5
)
dict
.
popitem
()
self
.
assertEqual
(
len
(
dict
),
n
-
6
)
with
testcontext
():
dict
.
clear
()
self
.
assertEqual
(
len
(
dict
),
0
)
self
.
assertEqual
(
len
(
dict
),
0
)
def
test_weak_keys_destroy_while_iterating
(
self
):
def
test_weak_keys_destroy_while_iterating
(
self
):
# Issue #7105: iterators shouldn't crash when a key is implicitly removed
# Issue #7105: iterators shouldn't crash when a key is implicitly removed
dict
,
objects
=
self
.
make_weak_keyed_dict
()
dict
,
objects
=
self
.
make_weak_keyed_dict
()
...
@@ -1331,6 +1361,10 @@ class MappingTestCase(TestBase):
...
@@ -1331,6 +1361,10 @@ class MappingTestCase(TestBase):
it
=
None
# should commit all removals
it
=
None
# should commit all removals
gc
.
collect
()
gc
.
collect
()
self
.
check_weak_destroy_and_mutate_while_iterating
(
dict
,
testcontext
)
self
.
check_weak_destroy_and_mutate_while_iterating
(
dict
,
testcontext
)
# Issue #21173: len() fragile when keys are both implicitly and
# explicitly removed.
dict
,
objects
=
self
.
make_weak_keyed_dict
()
self
.
check_weak_del_and_len_while_iterating
(
dict
,
testcontext
)
def
test_weak_values_destroy_while_iterating
(
self
):
def
test_weak_values_destroy_while_iterating
(
self
):
# Issue #7105: iterators shouldn't crash when a key is implicitly removed
# Issue #7105: iterators shouldn't crash when a key is implicitly removed
...
@@ -1354,6 +1388,8 @@ class MappingTestCase(TestBase):
...
@@ -1354,6 +1388,8 @@ class MappingTestCase(TestBase):
it
=
None
# should commit all removals
it
=
None
# should commit all removals
gc
.
collect
()
gc
.
collect
()
self
.
check_weak_destroy_and_mutate_while_iterating
(
dict
,
testcontext
)
self
.
check_weak_destroy_and_mutate_while_iterating
(
dict
,
testcontext
)
dict
,
objects
=
self
.
make_weak_valued_dict
()
self
.
check_weak_del_and_len_while_iterating
(
dict
,
testcontext
)
def
test_make_weak_keyed_dict_from_dict
(
self
):
def
test_make_weak_keyed_dict_from_dict
(
self
):
o
=
Object
(
3
)
o
=
Object
(
3
)
...
...
Lib/weakref.py
Dosyayı görüntüle @
414e15a8
...
@@ -322,6 +322,7 @@ class WeakKeyDictionary(collections.MutableMapping):
...
@@ -322,6 +322,7 @@ class WeakKeyDictionary(collections.MutableMapping):
# A list of dead weakrefs (keys to be removed)
# A list of dead weakrefs (keys to be removed)
self
.
_pending_removals
=
[]
self
.
_pending_removals
=
[]
self
.
_iterating
=
set
()
self
.
_iterating
=
set
()
self
.
_dirty_len
=
False
if
dict
is
not
None
:
if
dict
is
not
None
:
self
.
update
(
dict
)
self
.
update
(
dict
)
...
@@ -338,13 +339,23 @@ class WeakKeyDictionary(collections.MutableMapping):
...
@@ -338,13 +339,23 @@ class WeakKeyDictionary(collections.MutableMapping):
except
KeyError
:
except
KeyError
:
pass
pass
def
_scrub_removals
(
self
):
d
=
self
.
data
self
.
_pending_removals
=
[
k
for
k
in
self
.
_pending_removals
if
k
in
d
]
self
.
_dirty_len
=
False
def
__delitem__
(
self
,
key
):
def
__delitem__
(
self
,
key
):
self
.
_dirty_len
=
True
del
self
.
data
[
ref
(
key
)]
del
self
.
data
[
ref
(
key
)]
def
__getitem__
(
self
,
key
):
def
__getitem__
(
self
,
key
):
return
self
.
data
[
ref
(
key
)]
return
self
.
data
[
ref
(
key
)]
def
__len__
(
self
):
def
__len__
(
self
):
if
self
.
_dirty_len
and
self
.
_pending_removals
:
# self._pending_removals may still contain keys which were
# explicitly removed, we have to scrub them (see issue #21173).
self
.
_scrub_removals
()
return
len
(
self
.
data
)
-
len
(
self
.
_pending_removals
)
return
len
(
self
.
data
)
-
len
(
self
.
_pending_removals
)
def
__repr__
(
self
):
def
__repr__
(
self
):
...
@@ -417,6 +428,7 @@ class WeakKeyDictionary(collections.MutableMapping):
...
@@ -417,6 +428,7 @@ class WeakKeyDictionary(collections.MutableMapping):
return
list
(
self
.
data
)
return
list
(
self
.
data
)
def
popitem
(
self
):
def
popitem
(
self
):
self
.
_dirty_len
=
True
while
True
:
while
True
:
key
,
value
=
self
.
data
.
popitem
()
key
,
value
=
self
.
data
.
popitem
()
o
=
key
()
o
=
key
()
...
@@ -424,6 +436,7 @@ class WeakKeyDictionary(collections.MutableMapping):
...
@@ -424,6 +436,7 @@ class WeakKeyDictionary(collections.MutableMapping):
return
o
,
value
return
o
,
value
def
pop
(
self
,
key
,
*
args
):
def
pop
(
self
,
key
,
*
args
):
self
.
_dirty_len
=
True
return
self
.
data
.
pop
(
ref
(
key
),
*
args
)
return
self
.
data
.
pop
(
ref
(
key
),
*
args
)
def
setdefault
(
self
,
key
,
default
=
None
):
def
setdefault
(
self
,
key
,
default
=
None
):
...
...
Misc/NEWS
Dosyayı görüntüle @
414e15a8
...
@@ -166,6 +166,9 @@ Core and Builtins
...
@@ -166,6 +166,9 @@ Core and Builtins
Library
Library
-------
-------
-
Issue
#
21173
:
Fix
len
()
on
a
WeakKeyDictionary
when
.
clear
()
was
called
with
an
iterator
alive
.
-
Issue
#
11866
:
Eliminated
race
condition
in
the
computation
of
names
-
Issue
#
11866
:
Eliminated
race
condition
in
the
computation
of
names
for
new
threads
.
for
new
threads
.
...
...
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