weakref

package
v0.12.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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)

View Source
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)

View Source
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

type WeakKeyDict struct {
	objects.Header
	// contains filtered or unexported fields
}

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

func (d *WeakKeyDict) GetItem(key objects.Object) (objects.Object, error)

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

type WeakSet struct {
	objects.Header
	// contains filtered or unexported fields
}

WeakSet is the Go-side WeakSet object.

CPython: Lib/_weakrefset.py:11 WeakSet

func NewWeakSet

func NewWeakSet(data objects.Object) (*WeakSet, error)

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

func (s *WeakSet) Add(item objects.Object) error

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

func (s *WeakSet) Contains(item objects.Object) (bool, error)

Contains reports whether item is a live member.

CPython: Lib/_weakrefset.py:35 WeakSet.__contains__

func (*WeakSet) Copy

func (s *WeakSet) Copy() (*WeakSet, error)

Copy returns a fresh WeakSet with the same live members.

CPython: Lib/_weakrefset.py:51 WeakSet.copy

func (*WeakSet) Discard

func (s *WeakSet) Discard(item objects.Object) error

Discard removes item if present. Missing items are a no-op.

CPython: Lib/_weakrefset.py:67 WeakSet.discard

func (*WeakSet) Len

func (s *WeakSet) Len() int

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

func (s *WeakSet) Pop() (objects.Object, error)

Pop removes and returns one live member, or KeyError if empty.

CPython: Lib/_weakrefset.py:54 WeakSet.pop

func (*WeakSet) Remove

func (s *WeakSet) Remove(item objects.Object) error

Remove deletes item, raising KeyError if absent.

CPython: Lib/_weakrefset.py:64 WeakSet.remove

func (*WeakSet) Update

func (s *WeakSet) Update(other objects.Object) error

Update adds every element of other.

CPython: Lib/_weakrefset.py:70 WeakSet.update

type WeakValueDict

type WeakValueDict struct {
	objects.Header
	// contains filtered or unexported fields
}

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

func (d *WeakValueDict) GetItem(key objects.Object) (objects.Object, error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL