Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
ZODB
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kirill Smelkov
ZODB
Commits
7687a555
Commit
7687a555
authored
Oct 12, 2008
by
Christophe Combelles
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed the preformatted text markers, and added a __docformat__
parent
3828451c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
22 deletions
+24
-22
src/persistent/wref.py
src/persistent/wref.py
+24
-22
No files found.
src/persistent/wref.py
View file @
7687a555
...
@@ -16,6 +16,8 @@
...
@@ -16,6 +16,8 @@
$Id$
$Id$
"""
"""
__docformat__
=
"reStructuredText"
from
persistent
import
Persistent
from
persistent
import
Persistent
WeakRefMarker
=
object
()
WeakRefMarker
=
object
()
...
@@ -28,7 +30,7 @@ class WeakRef(object):
...
@@ -28,7 +30,7 @@ class WeakRef(object):
object to be called when the object is removed from the database.
object to be called when the object is removed from the database.
Here's an example. We'll start by creating a persistent object and
Here's an example. We'll start by creating a persistent object and
a reference to it:
:
a reference to it:
>>> import persistent, ZODB.tests.MinPO
>>> import persistent, ZODB.tests.MinPO
>>> import ZODB.tests.util
>>> import ZODB.tests.util
...
@@ -37,12 +39,12 @@ class WeakRef(object):
...
@@ -37,12 +39,12 @@ class WeakRef(object):
>>> ref() is ob
>>> ref() is ob
True
True
The hash of the ref if the same as the hash of the referenced object:
:
The hash of the ref if the same as the hash of the referenced object:
>>> hash(ref) == hash(ob)
>>> hash(ref) == hash(ob)
True
True
Two refs to the same object are equal:
:
Two refs to the same object are equal:
>>> WeakRef(ob) == ref
>>> WeakRef(ob) == ref
True
True
...
@@ -51,7 +53,7 @@ class WeakRef(object):
...
@@ -51,7 +53,7 @@ class WeakRef(object):
>>> WeakRef(ob2) == ref
>>> WeakRef(ob2) == ref
False
False
Lets save the reference and the referenced object in a database:
:
Lets save the reference and the referenced object in a database:
>>> db = ZODB.tests.util.DB()
>>> db = ZODB.tests.util.DB()
...
@@ -60,7 +62,7 @@ class WeakRef(object):
...
@@ -60,7 +62,7 @@ class WeakRef(object):
>>> conn1.root()['ref'] = ref
>>> conn1.root()['ref'] = ref
>>> ZODB.tests.util.commit()
>>> ZODB.tests.util.commit()
If we open a new connection, we can use the reference:
:
If we open a new connection, we can use the reference:
>>> conn2 = db.open()
>>> conn2 = db.open()
>>> conn2.root()['ref']() is conn2.root()['ob']
>>> conn2.root()['ref']() is conn2.root()['ob']
...
@@ -68,13 +70,13 @@ class WeakRef(object):
...
@@ -68,13 +70,13 @@ class WeakRef(object):
>>> hash(conn2.root()['ref']) == hash(conn2.root()['ob'])
>>> hash(conn2.root()['ref']) == hash(conn2.root()['ob'])
True
True
But if we delete the referenced object and pack:
:
But if we delete the referenced object and pack:
>>> del conn2.root()['ob']
>>> del conn2.root()['ob']
>>> ZODB.tests.util.commit()
>>> ZODB.tests.util.commit()
>>> ZODB.tests.util.pack(db)
>>> ZODB.tests.util.pack(db)
And then look in a new connection:
:
And then look in a new connection:
>>> conn3 = db.open()
>>> conn3 = db.open()
>>> conn3.root()['ob']
>>> conn3.root()['ob']
...
@@ -82,18 +84,18 @@ class WeakRef(object):
...
@@ -82,18 +84,18 @@ class WeakRef(object):
...
...
KeyError: 'ob'
KeyError: 'ob'
Trying to dereference the reference returns None:
:
Trying to dereference the reference returns None:
>>> conn3.root()['ref']()
>>> conn3.root()['ref']()
Trying to get a hash, raises a type error:
:
Trying to get a hash, raises a type error:
>>> hash(conn3.root()['ref'])
>>> hash(conn3.root()['ref'])
Traceback (most recent call last):
Traceback (most recent call last):
...
...
TypeError: Weakly-referenced object has gone away
TypeError: Weakly-referenced object has gone away
Always explicitly close databases :) :
:
Always explicitly close databases :) :
>>> db.close()
>>> db.close()
...
@@ -142,7 +144,7 @@ class PersistentWeakKeyDictionary(Persistent):
...
@@ -142,7 +144,7 @@ class PersistentWeakKeyDictionary(Persistent):
of items is extremely lazy. See below.
of items is extremely lazy. See below.
We'll start by creating a PersistentWeakKeyDictionary and adding
We'll start by creating a PersistentWeakKeyDictionary and adding
some persistent objects to it:
:
some persistent objects to it:
>>> d = PersistentWeakKeyDictionary()
>>> d = PersistentWeakKeyDictionary()
>>> import ZODB.tests.util
>>> import ZODB.tests.util
...
@@ -153,23 +155,23 @@ class PersistentWeakKeyDictionary(Persistent):
...
@@ -153,23 +155,23 @@ class PersistentWeakKeyDictionary(Persistent):
>>> d[p2] = 2
>>> d[p2] = 2
>>> d[p3] = 3
>>> d[p3] = 3
We'll create an extra persistent object that's not in the dict:
:
We'll create an extra persistent object that's not in the dict:
>>> p4 = ZODB.tests.util.P('p4')
>>> p4 = ZODB.tests.util.P('p4')
Now we'll excercise iteration and item access:
:
Now we'll excercise iteration and item access:
>>> l = [(str(k), d[k], d.get(k)) for k in d]
>>> l = [(str(k), d[k], d.get(k)) for k in d]
>>> l.sort()
>>> l.sort()
>>> l
>>> l
[('P(p1)', 1, 1), ('P(p2)', 2, 2), ('P(p3)', 3, 3)]
[('P(p1)', 1, 1), ('P(p2)', 2, 2), ('P(p3)', 3, 3)]
And the containment operator:
:
And the containment operator:
>>> [p in d for p in [p1, p2, p3, p4]]
>>> [p in d for p in [p1, p2, p3, p4]]
[True, True, True, False]
[True, True, True, False]
We can add the dict and the referenced objects to a database:
:
We can add the dict and the referenced objects to a database:
>>> db = ZODB.tests.util.DB()
>>> db = ZODB.tests.util.DB()
...
@@ -180,7 +182,7 @@ class PersistentWeakKeyDictionary(Persistent):
...
@@ -180,7 +182,7 @@ class PersistentWeakKeyDictionary(Persistent):
>>> conn1.root()['p3'] = p3
>>> conn1.root()['p3'] = p3
>>> ZODB.tests.util.commit()
>>> ZODB.tests.util.commit()
And things still work, as before:
:
And things still work, as before:
>>> l = [(str(k), d[k], d.get(k)) for k in d]
>>> l = [(str(k), d[k], d.get(k)) for k in d]
>>> l.sort()
>>> l.sort()
...
@@ -190,7 +192,7 @@ class PersistentWeakKeyDictionary(Persistent):
...
@@ -190,7 +192,7 @@ class PersistentWeakKeyDictionary(Persistent):
[True, True, True, False]
[True, True, True, False]
Likewise, we can read the objects from another connection and
Likewise, we can read the objects from another connection and
things still work:
:
things still work:
>>> conn2 = db.open()
>>> conn2 = db.open()
>>> d = conn2.root()['d']
>>> d = conn2.root()['d']
...
@@ -205,18 +207,18 @@ class PersistentWeakKeyDictionary(Persistent):
...
@@ -205,18 +207,18 @@ class PersistentWeakKeyDictionary(Persistent):
[True, True, True, False]
[True, True, True, False]
Now, we'll delete one of the objects from the database, but *not*
Now, we'll delete one of the objects from the database, but *not*
from the dictionary:
:
from the dictionary:
>>> del conn2.root()['p2']
>>> del conn2.root()['p2']
>>> ZODB.tests.util.commit()
>>> ZODB.tests.util.commit()
And pack the database, so that the no-longer referenced p2 is
And pack the database, so that the no-longer referenced p2 is
actually removed from the database:
:
actually removed from the database:
>>> ZODB.tests.util.pack(db)
>>> ZODB.tests.util.pack(db)
Now if we access the dictionary in a new connection, it no longer
Now if we access the dictionary in a new connection, it no longer
has p2:
:
has p2:
>>> conn3 = db.open()
>>> conn3 = db.open()
>>> d = conn3.root()['d']
>>> d = conn3.root()['d']
...
@@ -229,7 +231,7 @@ class PersistentWeakKeyDictionary(Persistent):
...
@@ -229,7 +231,7 @@ class PersistentWeakKeyDictionary(Persistent):
conn1 and conn2 still have p2, because p2 is still in the caches
conn1 and conn2 still have p2, because p2 is still in the caches
for those connections.
for those connections.
Always explicitly close databases :) :
:
Always explicitly close databases :) :
>>> db.close()
>>> db.close()
...
@@ -269,7 +271,7 @@ class PersistentWeakKeyDictionary(Persistent):
...
@@ -269,7 +271,7 @@ class PersistentWeakKeyDictionary(Persistent):
del
self
.
data
[
WeakRef
(
key
)]
del
self
.
data
[
WeakRef
(
key
)]
def
get
(
self
,
key
,
default
=
None
):
def
get
(
self
,
key
,
default
=
None
):
"""D.get(k[, d]) -> D[k] if k in D, else d. :
:
"""D.get(k[, d]) -> D[k] if k in D, else d. :
>>> import ZODB.tests.util
>>> import ZODB.tests.util
>>> key = ZODB.tests.util.P("key")
>>> key = ZODB.tests.util.P("key")
...
...
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