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
ca626b12
Commit
ca626b12
authored
Nov 17, 2013
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #19607: Use specific asserts in weakref tests.
parent
1438b987
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
74 additions
and
77 deletions
+74
-77
Lib/test/test_weakref.py
Lib/test/test_weakref.py
+74
-77
No files found.
Lib/test/test_weakref.py
View file @
ca626b12
...
...
@@ -91,11 +91,9 @@ class ReferencesTestCase(TestBase):
ref1
=
weakref
.
ref
(
o
,
self
.
callback
)
ref2
=
weakref
.
ref
(
o
,
self
.
callback
)
del
o
self
.
assertTrue
(
ref1
()
is
None
,
"expected reference to be invalidated"
)
self
.
assertTrue
(
ref2
()
is
None
,
"expected reference to be invalidated"
)
self
.
assertTrue
(
self
.
cbcalled
==
2
,
self
.
assertIsNone
(
ref1
(),
"expected reference to be invalidated"
)
self
.
assertIsNone
(
ref2
(),
"expected reference to be invalidated"
)
self
.
assertEqual
(
self
.
cbcalled
,
2
,
"callback not called the right number of times"
)
def
test_multiple_selfref_callbacks
(
self
):
...
...
@@ -129,15 +127,15 @@ class ReferencesTestCase(TestBase):
self
.
assertRaises
(
weakref
.
ReferenceError
,
check
,
ref1
)
self
.
assertRaises
(
weakref
.
ReferenceError
,
check
,
ref2
)
self
.
assertRaises
(
weakref
.
ReferenceError
,
bool
,
weakref
.
proxy
(
C
()))
self
.
assert
True
(
self
.
cbcalled
==
2
)
self
.
assert
Equal
(
self
.
cbcalled
,
2
)
def
check_basic_ref
(
self
,
factory
):
o
=
factory
()
ref
=
weakref
.
ref
(
o
)
self
.
assert
True
(
ref
()
is
not
None
,
self
.
assert
IsNotNone
(
ref
()
,
"weak reference to live object should be live"
)
o2
=
ref
()
self
.
assert
True
(
o
is
o2
,
self
.
assert
Is
(
o
,
o2
,
"<ref>() should return original object if live"
)
def
check_basic_callback
(
self
,
factory
):
...
...
@@ -145,9 +143,9 @@ class ReferencesTestCase(TestBase):
o
=
factory
()
ref
=
weakref
.
ref
(
o
,
self
.
callback
)
del
o
self
.
assert
True
(
self
.
cbcalled
==
1
,
self
.
assert
Equal
(
self
.
cbcalled
,
1
,
"callback did not properly set 'cbcalled'"
)
self
.
assert
True
(
ref
()
is
None
,
self
.
assert
IsNone
(
ref
()
,
"ref2 should be dead after deleting object reference"
)
def
test_ref_reuse
(
self
):
...
...
@@ -157,19 +155,19 @@ class ReferencesTestCase(TestBase):
# between these two; it should make no difference
proxy
=
weakref
.
proxy
(
o
)
ref2
=
weakref
.
ref
(
o
)
self
.
assert
True
(
ref1
is
ref2
,
self
.
assert
Is
(
ref1
,
ref2
,
"reference object w/out callback should be re-used"
)
o
=
C
()
proxy
=
weakref
.
proxy
(
o
)
ref1
=
weakref
.
ref
(
o
)
ref2
=
weakref
.
ref
(
o
)
self
.
assert
True
(
ref1
is
ref2
,
self
.
assert
Is
(
ref1
,
ref2
,
"reference object w/out callback should be re-used"
)
self
.
assert
True
(
weakref
.
getweakrefcount
(
o
)
==
2
,
self
.
assert
Equal
(
weakref
.
getweakrefcount
(
o
),
2
,
"wrong weak ref count for object"
)
del
proxy
self
.
assert
True
(
weakref
.
getweakrefcount
(
o
)
==
1
,
self
.
assert
Equal
(
weakref
.
getweakrefcount
(
o
),
1
,
"wrong weak ref count for object after deleting proxy"
)
def
test_proxy_reuse
(
self
):
...
...
@@ -177,7 +175,7 @@ class ReferencesTestCase(TestBase):
proxy1
=
weakref
.
proxy
(
o
)
ref
=
weakref
.
ref
(
o
)
proxy2
=
weakref
.
proxy
(
o
)
self
.
assert
True
(
proxy1
is
proxy2
,
self
.
assert
Is
(
proxy1
,
proxy2
,
"proxy object w/out callback should have been re-used"
)
def
test_basic_proxy
(
self
):
...
...
@@ -259,19 +257,19 @@ class ReferencesTestCase(TestBase):
o
=
Object
(
1
)
p1
=
makeref
(
o
,
None
)
p2
=
makeref
(
o
,
None
)
self
.
assert
True
(
p1
is
p2
,
"both callbacks were None in the C API"
)
self
.
assert
Is
(
p1
,
p2
,
"both callbacks were None in the C API"
)
del
p1
,
p2
p1
=
makeref
(
o
)
p2
=
makeref
(
o
,
None
)
self
.
assert
True
(
p1
is
p2
,
"callbacks were NULL, None in the C API"
)
self
.
assert
Is
(
p1
,
p2
,
"callbacks were NULL, None in the C API"
)
del
p1
,
p2
p1
=
makeref
(
o
)
p2
=
makeref
(
o
)
self
.
assert
True
(
p1
is
p2
,
"both callbacks were NULL in the C API"
)
self
.
assert
Is
(
p1
,
p2
,
"both callbacks were NULL in the C API"
)
del
p1
,
p2
p1
=
makeref
(
o
,
None
)
p2
=
makeref
(
o
)
self
.
assert
True
(
p1
is
p2
,
"callbacks were None, NULL in the C API"
)
self
.
assert
Is
(
p1
,
p2
,
"callbacks were None, NULL in the C API"
)
def
test_callable_proxy
(
self
):
o
=
Callable
()
...
...
@@ -279,13 +277,13 @@ class ReferencesTestCase(TestBase):
self
.
check_proxy
(
o
,
ref1
)
self
.
assert
True
(
type
(
ref1
)
is
weakref
.
CallableProxyType
,
self
.
assert
Is
(
type
(
ref1
),
weakref
.
CallableProxyType
,
"proxy is not of callable type"
)
ref1
(
'twinkies!'
)
self
.
assert
True
(
o
.
bar
==
'twinkies!'
,
self
.
assert
Equal
(
o
.
bar
,
'twinkies!'
,
"call through proxy not passed through to original"
)
ref1
(
x
=
'Splat.'
)
self
.
assert
True
(
o
.
bar
==
'Splat.'
,
self
.
assert
Equal
(
o
.
bar
,
'Splat.'
,
"call through proxy not passed through to original"
)
# expect due to too few args
...
...
@@ -296,24 +294,23 @@ class ReferencesTestCase(TestBase):
def
check_proxy
(
self
,
o
,
proxy
):
o
.
foo
=
1
self
.
assert
True
(
proxy
.
foo
==
1
,
self
.
assert
Equal
(
proxy
.
foo
,
1
,
"proxy does not reflect attribute addition"
)
o
.
foo
=
2
self
.
assert
True
(
proxy
.
foo
==
2
,
self
.
assert
Equal
(
proxy
.
foo
,
2
,
"proxy does not reflect attribute modification"
)
del
o
.
foo
self
.
assert
True
(
not
hasattr
(
proxy
,
'foo'
),
self
.
assert
False
(
hasattr
(
proxy
,
'foo'
),
"proxy does not reflect attribute removal"
)
proxy
.
foo
=
1
self
.
assert
True
(
o
.
foo
==
1
,
self
.
assert
Equal
(
o
.
foo
,
1
,
"object does not reflect attribute addition via proxy"
)
proxy
.
foo
=
2
self
.
assertTrue
(
o
.
foo
==
2
,
self
.
assertEqual
(
o
.
foo
,
2
,
"object does not reflect attribute modification via proxy"
)
del
proxy
.
foo
self
.
assert
True
(
not
hasattr
(
o
,
'foo'
),
self
.
assert
False
(
hasattr
(
o
,
'foo'
),
"object does not reflect attribute removal via proxy"
)
def
test_proxy_deletion
(
self
):
...
...
@@ -337,21 +334,21 @@ class ReferencesTestCase(TestBase):
o
=
C
()
ref1
=
weakref
.
ref
(
o
)
ref2
=
weakref
.
ref
(
o
,
self
.
callback
)
self
.
assert
True
(
weakref
.
getweakrefcount
(
o
)
==
2
,
self
.
assert
Equal
(
weakref
.
getweakrefcount
(
o
),
2
,
"got wrong number of weak reference objects"
)
proxy1
=
weakref
.
proxy
(
o
)
proxy2
=
weakref
.
proxy
(
o
,
self
.
callback
)
self
.
assert
True
(
weakref
.
getweakrefcount
(
o
)
==
4
,
self
.
assert
Equal
(
weakref
.
getweakrefcount
(
o
),
4
,
"got wrong number of weak reference objects"
)
del
ref1
,
ref2
,
proxy1
,
proxy2
self
.
assert
True
(
weakref
.
getweakrefcount
(
o
)
==
0
,
self
.
assert
Equal
(
weakref
.
getweakrefcount
(
o
),
0
,
"weak reference objects not unlinked from"
" referent when discarded."
)
# assumes ints do not support weakrefs
self
.
assert
True
(
weakref
.
getweakrefcount
(
1
)
==
0
,
self
.
assert
Equal
(
weakref
.
getweakrefcount
(
1
),
0
,
"got wrong number of weak reference objects for int"
)
def
test_getweakrefs
(
self
):
...
...
@@ -359,22 +356,22 @@ class ReferencesTestCase(TestBase):
ref1
=
weakref
.
ref
(
o
,
self
.
callback
)
ref2
=
weakref
.
ref
(
o
,
self
.
callback
)
del
ref1
self
.
assert
True
(
weakref
.
getweakrefs
(
o
)
==
[
ref2
],
self
.
assert
Equal
(
weakref
.
getweakrefs
(
o
),
[
ref2
],
"list of refs does not match"
)
o
=
C
()
ref1
=
weakref
.
ref
(
o
,
self
.
callback
)
ref2
=
weakref
.
ref
(
o
,
self
.
callback
)
del
ref2
self
.
assert
True
(
weakref
.
getweakrefs
(
o
)
==
[
ref1
],
self
.
assert
Equal
(
weakref
.
getweakrefs
(
o
),
[
ref1
],
"list of refs does not match"
)
del
ref1
self
.
assert
True
(
weakref
.
getweakrefs
(
o
)
==
[],
self
.
assert
Equal
(
weakref
.
getweakrefs
(
o
),
[],
"list of refs not cleared"
)
# assumes ints do not support weakrefs
self
.
assert
True
(
weakref
.
getweakrefs
(
1
)
==
[],
self
.
assert
Equal
(
weakref
.
getweakrefs
(
1
),
[],
"list of refs does not match for int"
)
def
test_newstyle_number_ops
(
self
):
...
...
@@ -382,8 +379,8 @@ class ReferencesTestCase(TestBase):
pass
f
=
F
(
2.0
)
p
=
weakref
.
proxy
(
f
)
self
.
assert
True
(
p
+
1.0
==
3.0
)
self
.
assert
True
(
1.0
+
p
==
3.0
)
# this used to SEGV
self
.
assert
Equal
(
p
+
1.0
,
3.0
)
self
.
assert
Equal
(
1.0
+
p
,
3.0
)
# this used to SEGV
def
test_callbacks_protected
(
self
):
# Callbacks protected from already-set exceptions?
...
...
@@ -636,7 +633,7 @@ class ReferencesTestCase(TestBase):
c
.
wr
=
weakref
.
ref
(
d
,
callback
)
# this won't trigger
d
.
wr
=
weakref
.
ref
(
callback
,
d
.
cb
)
# ditto
external_wr
=
weakref
.
ref
(
callback
,
safe_callback
)
# but this will
self
.
assert
True
(
external_wr
()
is
callback
)
self
.
assert
Is
(
external_wr
(),
callback
)
# The weakrefs attached to c and d should get cleared, so that
# C.cb is never called. But external_wr isn't part of the cyclic
...
...
@@ -808,11 +805,11 @@ class SubclassableWeakrefTestCase(TestBase):
return
super
(
MyRef
,
self
).
__call__
()
o
=
Object
(
"foo"
)
mr
=
MyRef
(
o
,
value
=
24
)
self
.
assert
True
(
mr
()
is
o
)
self
.
assert
Is
(
mr
(),
o
)
self
.
assertTrue
(
mr
.
called
)
self
.
assertEqual
(
mr
.
value
,
24
)
del
o
self
.
assert
True
(
mr
()
is
None
)
self
.
assert
IsNone
(
mr
()
)
self
.
assertTrue
(
mr
.
called
)
def
test_subclass_refs_dont_replace_standard_refs
(
self
):
...
...
@@ -821,14 +818,14 @@ class SubclassableWeakrefTestCase(TestBase):
o
=
Object
(
42
)
r1
=
MyRef
(
o
)
r2
=
weakref
.
ref
(
o
)
self
.
assert
True
(
r1
is
not
r2
)
self
.
assert
IsNot
(
r1
,
r2
)
self
.
assertEqual
(
weakref
.
getweakrefs
(
o
),
[
r2
,
r1
])
self
.
assertEqual
(
weakref
.
getweakrefcount
(
o
),
2
)
r3
=
MyRef
(
o
)
self
.
assertEqual
(
weakref
.
getweakrefcount
(
o
),
3
)
refs
=
weakref
.
getweakrefs
(
o
)
self
.
assertEqual
(
len
(
refs
),
3
)
self
.
assert
True
(
r2
is
refs
[
0
])
self
.
assert
Is
(
r2
,
refs
[
0
])
self
.
assertIn
(
r1
,
refs
[
1
:])
self
.
assertIn
(
r3
,
refs
[
1
:])
...
...
@@ -838,7 +835,7 @@ class SubclassableWeakrefTestCase(TestBase):
o
=
Object
(
42
)
r1
=
MyRef
(
o
,
id
)
r2
=
MyRef
(
o
,
str
)
self
.
assert
True
(
r1
is
not
r2
)
self
.
assert
IsNot
(
r1
,
r2
)
refs
=
weakref
.
getweakrefs
(
o
)
self
.
assertIn
(
r1
,
refs
)
self
.
assertIn
(
r2
,
refs
)
...
...
@@ -965,23 +962,23 @@ class MappingTestCase(TestBase):
#
dict
,
objects
=
self
.
make_weak_valued_dict
()
for
o
in
objects
:
self
.
assert
True
(
weakref
.
getweakrefcount
(
o
)
==
1
,
self
.
assert
Equal
(
weakref
.
getweakrefcount
(
o
),
1
,
"wrong number of weak references to %r!"
%
o
)
self
.
assert
True
(
o
is
dict
[
o
.
arg
],
self
.
assert
Is
(
o
,
dict
[
o
.
arg
],
"wrong object returned by weak dict!"
)
items1
=
dict
.
items
()
items2
=
dict
.
copy
().
items
()
items1
.
sort
()
items2
.
sort
()
self
.
assert
True
(
items1
==
items2
,
self
.
assert
Equal
(
items1
,
items2
,
"cloning of weak-valued dictionary did not work!"
)
del
items1
,
items2
self
.
assert
True
(
len
(
dict
)
==
self
.
COUNT
)
self
.
assert
Equal
(
len
(
dict
),
self
.
COUNT
)
del
objects
[
0
]
self
.
assert
True
(
len
(
dict
)
==
(
self
.
COUNT
-
1
),
self
.
assert
Equal
(
len
(
dict
),
(
self
.
COUNT
-
1
),
"deleting object did not cause dictionary update"
)
del
objects
,
o
self
.
assert
True
(
len
(
dict
)
==
0
,
self
.
assert
Equal
(
len
(
dict
),
0
,
"deleting the values did not clear the dictionary"
)
# regression on SF bug #447152:
dict
=
weakref
.
WeakValueDictionary
()
...
...
@@ -996,21 +993,21 @@ class MappingTestCase(TestBase):
#
dict
,
objects
=
self
.
make_weak_keyed_dict
()
for
o
in
objects
:
self
.
assert
True
(
weakref
.
getweakrefcount
(
o
)
==
1
,
self
.
assert
Equal
(
weakref
.
getweakrefcount
(
o
),
1
,
"wrong number of weak references to %r!"
%
o
)
self
.
assert
True
(
o
.
arg
is
dict
[
o
],
self
.
assert
Is
(
o
.
arg
,
dict
[
o
],
"wrong object returned by weak dict!"
)
items1
=
dict
.
items
()
items2
=
dict
.
copy
().
items
()
self
.
assert
True
(
set
(
items1
)
==
set
(
items2
),
self
.
assert
Equal
(
set
(
items1
),
set
(
items2
),
"cloning of weak-keyed dictionary did not work!"
)
del
items1
,
items2
self
.
assert
True
(
len
(
dict
)
==
self
.
COUNT
)
self
.
assert
Equal
(
len
(
dict
),
self
.
COUNT
)
del
objects
[
0
]
self
.
assert
True
(
len
(
dict
)
==
(
self
.
COUNT
-
1
),
self
.
assert
Equal
(
len
(
dict
),
(
self
.
COUNT
-
1
),
"deleting object did not cause dictionary update"
)
del
objects
,
o
self
.
assert
True
(
len
(
dict
)
==
0
,
self
.
assert
Equal
(
len
(
dict
),
0
,
"deleting the keys did not clear the dictionary"
)
o
=
Object
(
42
)
dict
[
o
]
=
"What is the meaning of the universe?"
...
...
@@ -1072,37 +1069,37 @@ class MappingTestCase(TestBase):
items
=
dict
.
items
()
for
item
in
dict
.
iteritems
():
items
.
remove
(
item
)
self
.
assert
True
(
len
(
items
)
==
0
,
"iteritems() did not touch all items"
)
self
.
assert
Equal
(
len
(
items
),
0
,
"iteritems() did not touch all items"
)
# key iterator, via __iter__():
keys
=
dict
.
keys
()
for
k
in
dict
:
keys
.
remove
(
k
)
self
.
assert
True
(
len
(
keys
)
==
0
,
"__iter__() did not touch all keys"
)
self
.
assert
Equal
(
len
(
keys
),
0
,
"__iter__() did not touch all keys"
)
# key iterator, via iterkeys():
keys
=
dict
.
keys
()
for
k
in
dict
.
iterkeys
():
keys
.
remove
(
k
)
self
.
assert
True
(
len
(
keys
)
==
0
,
"iterkeys() did not touch all keys"
)
self
.
assert
Equal
(
len
(
keys
),
0
,
"iterkeys() did not touch all keys"
)
# value iterator:
values
=
dict
.
values
()
for
v
in
dict
.
itervalues
():
values
.
remove
(
v
)
self
.
assert
True
(
len
(
values
)
==
0
,
self
.
assert
Equal
(
len
(
values
),
0
,
"itervalues() did not touch all values"
)
def
test_make_weak_keyed_dict_from_dict
(
self
):
o
=
Object
(
3
)
dict
=
weakref
.
WeakKeyDictionary
({
o
:
364
})
self
.
assert
True
(
dict
[
o
]
==
364
)
self
.
assert
Equal
(
dict
[
o
],
364
)
def
test_make_weak_keyed_dict_from_weak_keyed_dict
(
self
):
o
=
Object
(
3
)
dict
=
weakref
.
WeakKeyDictionary
({
o
:
364
})
dict2
=
weakref
.
WeakKeyDictionary
(
dict
)
self
.
assert
True
(
dict
[
o
]
==
364
)
self
.
assert
Equal
(
dict
[
o
],
364
)
def
make_weak_keyed_dict
(
self
):
dict
=
weakref
.
WeakKeyDictionary
()
...
...
@@ -1122,19 +1119,19 @@ class MappingTestCase(TestBase):
weakdict
=
klass
()
weakdict
[
key1
]
=
value1
weakdict
[
key2
]
=
value2
self
.
assert
True
(
len
(
weakdict
)
==
2
)
self
.
assert
Equal
(
len
(
weakdict
),
2
)
k
,
v
=
weakdict
.
popitem
()
self
.
assert
True
(
len
(
weakdict
)
==
1
)
self
.
assert
Equal
(
len
(
weakdict
),
1
)
if
k
is
key1
:
self
.
assert
True
(
v
is
value1
)
self
.
assert
Is
(
v
,
value1
)
else
:
self
.
assert
True
(
v
is
value2
)
self
.
assert
Is
(
v
,
value2
)
k
,
v
=
weakdict
.
popitem
()
self
.
assert
True
(
len
(
weakdict
)
==
0
)
self
.
assert
Equal
(
len
(
weakdict
),
0
)
if
k
is
key1
:
self
.
assert
True
(
v
is
value1
)
self
.
assert
Is
(
v
,
value1
)
else
:
self
.
assert
True
(
v
is
value2
)
self
.
assert
Is
(
v
,
value2
)
def
test_weak_valued_dict_popitem
(
self
):
self
.
check_popitem
(
weakref
.
WeakValueDictionary
,
...
...
@@ -1145,7 +1142,7 @@ class MappingTestCase(TestBase):
C
(),
"value 1"
,
C
(),
"value 2"
)
def
check_setdefault
(
self
,
klass
,
key
,
value1
,
value2
):
self
.
assert
True
(
value1
is
not
value2
,
self
.
assert
IsNot
(
value1
,
value2
,
"invalid test"
" -- value parameters must be distinct objects"
)
weakdict
=
klass
()
...
...
@@ -1204,10 +1201,10 @@ class MappingTestCase(TestBase):
o2
=
Object
(
'2'
)
d
[
o1
]
=
'something'
d
[
o2
]
=
'something'
self
.
assert
True
(
len
(
d
)
==
2
)
self
.
assert
Equal
(
len
(
d
),
2
)
del
d
[
o1
]
self
.
assert
True
(
len
(
d
)
==
1
)
self
.
assert
True
(
d
.
keys
()
==
[
o2
])
self
.
assert
Equal
(
len
(
d
),
1
)
self
.
assert
Equal
(
d
.
keys
(),
[
o2
])
def
test_weak_valued_delitem
(
self
):
d
=
weakref
.
WeakValueDictionary
()
...
...
@@ -1215,10 +1212,10 @@ class MappingTestCase(TestBase):
o2
=
Object
(
'2'
)
d
[
'something'
]
=
o1
d
[
'something else'
]
=
o2
self
.
assert
True
(
len
(
d
)
==
2
)
self
.
assert
Equal
(
len
(
d
),
2
)
del
d
[
'something'
]
self
.
assert
True
(
len
(
d
)
==
1
)
self
.
assert
True
(
d
.
items
()
==
[(
'something else'
,
o2
)])
self
.
assert
Equal
(
len
(
d
),
1
)
self
.
assert
Equal
(
d
.
items
(),
[(
'something else'
,
o2
)])
def
test_weak_keyed_bad_delitem
(
self
):
d
=
weakref
.
WeakKeyDictionary
()
...
...
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