Documentation
¶
Overview ¶
Package weakref ports the pure-Python weak collection classes from CPython's Lib/weakref.py and Lib/_weakrefset.py: WeakSet, WeakValueDictionary, and WeakKeyDictionary. They sit on top of the objects.Weakref scaffolding and rely on the gc package to fire the per-weakref callback when a referent goes unreachable.
CPython models these as Python classes that subclass set / dict and install a tiny "_remove" closure as the weakref callback so dead referents drop out of the underlying container automatically. gopy implements them as Go types that wrap an internal map keyed by *objects.Weakref; the weakref callback is an objects.BuiltinFunction whose Fn closure captures the container and removes the matching entry when the referent dies.
Lifetime caveat: each container's remover closure captures the container strongly, and every weakref it issues holds the closure strongly through its callback slot. That cycle is invisible to the gopy cycle collector (Go closures are opaque to TpTraverse), so an abandoned WeakSet / WeakValueDictionary / WeakKeyDictionary leaks until the items it weakly referenced have all died and dropped their weakrefs. CPython sidesteps this by capturing weakref(self) in the closure instead of self; matching that requires a TpTraverse-aware closure shape and lands with 1613-S (collector-visible Go closures).
CPython: Lib/_weakrefset.py:11 WeakSet CPython: Lib/weakref.py:92 WeakValueDictionary CPython: Lib/weakref.py:298 WeakKeyDictionary
Index ¶
- Variables
- type WeakKeyDict
- func (d *WeakKeyDict) Contains(key objects.Object) (bool, error)
- func (d *WeakKeyDict) DelItem(key objects.Object) error
- func (d *WeakKeyDict) Get(key, def objects.Object) objects.Object
- func (d *WeakKeyDict) GetItem(key objects.Object) (objects.Object, error)
- func (d *WeakKeyDict) Items() [][2]objects.Object
- func (d *WeakKeyDict) Keys() []objects.Object
- func (d *WeakKeyDict) Len() int
- func (d *WeakKeyDict) SetItem(key, value objects.Object) error
- func (d *WeakKeyDict) Update(other objects.Object) error
- func (d *WeakKeyDict) Values() []objects.Object
- type WeakSet
- func (s *WeakSet) Add(item objects.Object) error
- func (s *WeakSet) Clear()
- func (s *WeakSet) Contains(item objects.Object) (bool, error)
- func (s *WeakSet) Copy() (*WeakSet, error)
- func (s *WeakSet) Discard(item objects.Object) error
- func (s *WeakSet) Len() int
- func (s *WeakSet) Pop() (objects.Object, error)
- func (s *WeakSet) Remove(item objects.Object) error
- func (s *WeakSet) Update(other objects.Object) error
- type WeakValueDict
- func (d *WeakValueDict) Contains(key objects.Object) (bool, error)
- func (d *WeakValueDict) DelItem(key objects.Object) error
- func (d *WeakValueDict) Get(key, def objects.Object) objects.Object
- func (d *WeakValueDict) GetItem(key objects.Object) (objects.Object, error)
- func (d *WeakValueDict) Items() [][2]objects.Object
- func (d *WeakValueDict) Keys() []objects.Object
- func (d *WeakValueDict) Len() int
- func (d *WeakValueDict) SetItem(key, value objects.Object) error
- func (d *WeakValueDict) Update(other objects.Object) error
- func (d *WeakValueDict) Values() []objects.Object
Constants ¶
This section is empty.
Variables ¶
var WeakKeyDictType = objects.NewType("WeakKeyDictionary", []*objects.Type{objects.ObjectType()})
WeakKeyDictType is the type singleton for weakref.WeakKeyDictionary.
CPython: Lib/weakref.py:298 WeakKeyDictionary (class object)
var WeakSetType = objects.NewType("WeakSet", []*objects.Type{objects.ObjectType()})
WeakSetType is the type singleton for weakref.WeakSet.
CPython: Lib/_weakrefset.py:11 WeakSet (class object)
var WeakValueDictType = objects.NewType("WeakValueDictionary", []*objects.Type{objects.ObjectType()})
WeakValueDictType is the type singleton for weakref.WeakValueDictionary.
CPython: Lib/weakref.py:92 WeakValueDictionary (class object)
Functions ¶
This section is empty.
Types ¶
type WeakKeyDict ¶
WeakKeyDict is the Go-side WeakKeyDictionary object.
CPython: Lib/weakref.py:298 WeakKeyDictionary
func NewWeakKeyDict ¶
func NewWeakKeyDict(other objects.Object) (*WeakKeyDict, error)
NewWeakKeyDict builds an empty WeakKeyDictionary. Pass a *Dict (or nil) to seed via update().
CPython: Lib/weakref.py:309 WeakKeyDictionary.__init__
func (*WeakKeyDict) Contains ¶
func (d *WeakKeyDict) Contains(key objects.Object) (bool, error)
Contains reports whether key resolves to an entry.
CPython: Lib/weakref.py:359 WeakKeyDictionary.__contains__
func (*WeakKeyDict) DelItem ¶
func (d *WeakKeyDict) DelItem(key objects.Object) error
DelItem removes the entry for key.
CPython: Lib/weakref.py:322 WeakKeyDictionary.__delitem__
func (*WeakKeyDict) Get ¶
func (d *WeakKeyDict) Get(key, def objects.Object) objects.Object
Get returns the value for key or default.
CPython: Lib/weakref.py:356 WeakKeyDictionary.get
func (*WeakKeyDict) GetItem ¶
GetItem returns the value for key, or KeyError.
CPython: Lib/weakref.py:325 WeakKeyDictionary.__getitem__
func (*WeakKeyDict) Items ¶
func (d *WeakKeyDict) Items() [][2]objects.Object
Items returns (key, value) snapshots for live entries.
CPython: Lib/weakref.py:366 WeakKeyDictionary.items
func (*WeakKeyDict) Keys ¶
func (d *WeakKeyDict) Keys() []objects.Object
Keys returns the live keys in non-deterministic order.
CPython: Lib/weakref.py:372 WeakKeyDictionary.keys
func (*WeakKeyDict) Len ¶
func (d *WeakKeyDict) Len() int
Len returns the entry count.
CPython: Lib/weakref.py:328 WeakKeyDictionary.__len__
func (*WeakKeyDict) SetItem ¶
func (d *WeakKeyDict) SetItem(key, value objects.Object) error
SetItem inserts or replaces key->value, dedup-ing by Python equality against existing live keys.
CPython: Lib/weakref.py:334 WeakKeyDictionary.__setitem__
func (*WeakKeyDict) Update ¶
func (d *WeakKeyDict) Update(other objects.Object) error
Update copies entries from a *objects.Dict or another WeakKeyDict.
CPython: Lib/weakref.py:410 WeakKeyDictionary.update
func (*WeakKeyDict) Values ¶
func (d *WeakKeyDict) Values() []objects.Object
Values returns a snapshot of values whose keys are still live.
CPython: Lib/weakref.py:380 WeakKeyDictionary.values
type WeakSet ¶
WeakSet is the Go-side WeakSet object.
CPython: Lib/_weakrefset.py:11 WeakSet
func NewWeakSet ¶
NewWeakSet builds an empty WeakSet. data, when non-nil, is iterated and its members added (matching Lib/_weakrefset.py:21 WeakSet.update).
CPython: Lib/_weakrefset.py:12 WeakSet.__init__
func (*WeakSet) Add ¶
Add inserts item, dedup-ing by Python equality against the current live members.
CPython: Lib/_weakrefset.py:45 WeakSet.add
func (*WeakSet) Clear ¶
func (s *WeakSet) Clear()
Clear drops every member.
CPython: Lib/_weakrefset.py:48 WeakSet.clear
func (*WeakSet) Contains ¶
Contains reports whether item is a live member.
CPython: Lib/_weakrefset.py:35 WeakSet.__contains__
func (*WeakSet) Copy ¶
Copy returns a fresh WeakSet with the same live members.
CPython: Lib/_weakrefset.py:51 WeakSet.copy
func (*WeakSet) Discard ¶
Discard removes item if present. Missing items are a no-op.
CPython: Lib/_weakrefset.py:67 WeakSet.discard
func (*WeakSet) Len ¶
Len returns the number of stored weakrefs (live or not). Mirrors Lib/_weakrefset.py:32 WeakSet.__len__: CPython returns len(self.data) without filtering dead refs because the _remove callback prunes them promptly.
CPython: Lib/_weakrefset.py:32 WeakSet.__len__
func (*WeakSet) Pop ¶
Pop removes and returns one live member, or KeyError if empty.
CPython: Lib/_weakrefset.py:54 WeakSet.pop
type WeakValueDict ¶
WeakValueDict is the Go-side WeakValueDictionary object.
CPython: Lib/weakref.py:92 WeakValueDictionary
func NewWeakValueDict ¶
func NewWeakValueDict(other objects.Object) (*WeakValueDict, error)
NewWeakValueDict builds an empty WeakValueDictionary. Pass nil for the no-arg form; pass a dict to seed via the Lib/weakref.py:113 self.update(other) entry path.
CPython: Lib/weakref.py:104 WeakValueDictionary.__init__
func (*WeakValueDict) Contains ¶
func (d *WeakValueDict) Contains(key objects.Object) (bool, error)
Contains reports whether key resolves to a live value.
CPython: Lib/weakref.py:128 WeakValueDictionary.__contains__
func (*WeakValueDict) DelItem ¶
func (d *WeakValueDict) DelItem(key objects.Object) error
DelItem removes key.
CPython: Lib/weakref.py:122 WeakValueDictionary.__delitem__
func (*WeakValueDict) Get ¶
func (d *WeakValueDict) Get(key, def objects.Object) objects.Object
Get returns the live value or default. Mirrors dict.get's no-throw shape.
CPython: Lib/weakref.py:160 WeakValueDictionary.get
func (*WeakValueDict) GetItem ¶
GetItem returns the live value or KeyError.
CPython: Lib/weakref.py:115 WeakValueDictionary.__getitem__
func (*WeakValueDict) Items ¶
func (d *WeakValueDict) Items() [][2]objects.Object
Items returns (key, value) snapshots for live entries.
CPython: Lib/weakref.py:173 WeakValueDictionary.items
func (*WeakValueDict) Keys ¶
func (d *WeakValueDict) Keys() []objects.Object
Keys returns a snapshot of the live keys.
CPython: Lib/weakref.py:179 WeakValueDictionary.keys
func (*WeakValueDict) Len ¶
func (d *WeakValueDict) Len() int
Len returns the entry count without filtering dead refs.
CPython: Lib/weakref.py:125 WeakValueDictionary.__len__
func (*WeakValueDict) SetItem ¶
func (d *WeakValueDict) SetItem(key, value objects.Object) error
SetItem stores key -> value, wrapping value in a weakref whose callback prunes the entry when value dies.
CPython: Lib/weakref.py:138 WeakValueDictionary.__setitem__
func (*WeakValueDict) Update ¶
func (d *WeakValueDict) Update(other objects.Object) error
Update copies entries from other (a dict-like with .Items() or an iterable of (k, v) pairs). gopy's port handles the common case of a *objects.Dict.
CPython: Lib/weakref.py:235 WeakValueDictionary.update
func (*WeakValueDict) Values ¶
func (d *WeakValueDict) Values() []objects.Object
Values returns a snapshot of live values, in key-insertion order.
CPython: Lib/weakref.py:198 WeakValueDictionary.values