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
9a65d583
Kaydet (Commit)
9a65d583
authored
Tem 02, 2005
tarafından
Georg Brandl
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add doctest for examples in libweakref.tex to test_weakref.
üst
f0af0e7a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
2 deletions
+90
-2
libweakref.tex
Doc/lib/libweakref.tex
+4
-2
test_weakref.py
Lib/test/test_weakref.py
+86
-0
No files found.
Doc/lib/libweakref.tex
Dosyayı görüntüle @
9a65d583
...
...
@@ -10,6 +10,8 @@
\versionadded
{
2.1
}
% When making changes to the examples in this file, be sure to update
% Lib/test/test_weakref.py::libreftest too!
The
\module
{
weakref
}
module allows the Python programmer to create
\dfn
{
weak references
}
to objects.
...
...
@@ -228,7 +230,7 @@ this pattern:
o = r()
if o is None:
# referent has been garbage collected
print "Object has been allocated; can't frobnicate."
print "Object has been
de
allocated; can't frobnicate."
else:
print "Object is still live!"
o.do
_
something
_
useful()
...
...
@@ -265,7 +267,7 @@ class ExtendedRef(weakref.ref):
"""Return a pair containing the referent and the number of
times the reference has been called.
"""
ob = super(ExtendedRef, self)()
ob = super(ExtendedRef, self)
.
__
call
__
()
if ob is not None:
self.
__
counter += 1
ob = (ob, self.
__
counter)
...
...
Lib/test/test_weakref.py
Dosyayı görüntüle @
9a65d583
...
...
@@ -1001,6 +1001,91 @@ class WeakKeyDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
def
_reference
(
self
):
return
self
.
__ref
.
copy
()
libreftest
=
""" Doctest for examples in the library reference: libweakref.tex
>>> import weakref
>>> class Dict(dict):
... pass
...
>>> obj = Dict(red=1, green=2, blue=3) # this object is weak referencable
>>> r = weakref.ref(obj)
>>> print r()
{'blue': 3, 'green': 2, 'red': 1}
>>> import weakref
>>> class Object:
... pass
...
>>> o = Object()
>>> r = weakref.ref(o)
>>> o2 = r()
>>> o is o2
True
>>> del o, o2
>>> print r()
None
>>> import weakref
>>> class ExtendedRef(weakref.ref):
... def __init__(self, ob, callback=None, **annotations):
... super(ExtendedRef, self).__init__(ob, callback)
... self.__counter = 0
... for k, v in annotations.iteritems():
... setattr(self, k, v)
... def __call__(self):
... '''Return a pair containing the referent and the number of
... times the reference has been called.
... '''
... ob = super(ExtendedRef, self).__call__()
... if ob is not None:
... self.__counter += 1
... ob = (ob, self.__counter)
... return ob
...
>>> class A: # not in docs from here, just testing the ExtendedRef
... pass
...
>>> a = A()
>>> r = ExtendedRef(a, foo=1, bar="baz")
>>> r.foo
1
>>> r.bar
'baz'
>>> r()[1]
1
>>> r()[1]
2
>>> r()[0] is a
True
>>> import weakref
>>> _id2obj_dict = weakref.WeakValueDictionary()
>>> def remember(obj):
... oid = id(obj)
... _id2obj_dict[oid] = obj
... return oid
...
>>> def id2obj(oid):
... return _id2obj_dict[oid]
...
>>> a = A() # from here, just testing
>>> a_id = remember(a)
>>> id2obj(a_id) is a
True
>>> del a
>>> try:
... id2obj(a_id)
... except KeyError:
... print 'OK'
... else:
... print 'WeakValueDictionary error'
OK
"""
__test__
=
{
'libreftest'
:
libreftest
}
def
test_main
():
test_support
.
run_unittest
(
ReferencesTestCase
,
...
...
@@ -1008,6 +1093,7 @@ def test_main():
WeakValueDictionaryTestCase
,
WeakKeyDictionaryTestCase
,
)
test_support
.
run_doctest
(
sys
.
modules
[
__name__
])
if
__name__
==
"__main__"
:
...
...
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