Documentation
¶
Index ¶
- Variables
- type Context
- func (c *Context) Copy() *Context
- func (c *Context) Enter(ts *state.Thread) error
- func (c *Context) Eq(other *Context) (bool, error)
- func (c *Context) Exit(ts *state.Thread) error
- func (c *Context) Get(cv *ContextVar) (objects.Object, bool, error)
- func (c *Context) Len() int
- func (c *Context) Run(ts *state.Thread, fn func() (objects.Object, error)) (objects.Object, error)
- type ContextVar
- func (cv *ContextVar) Default() (objects.Object, bool)
- func (cv *ContextVar) Get(ts *state.Thread) (objects.Object, error)
- func (cv *ContextVar) GetWithDefault(ts *state.Thread, def objects.Object) (objects.Object, error)
- func (cv *ContextVar) Name() string
- func (cv *ContextVar) Reset(ts *state.Thread, tok *Token) error
- func (cv *ContextVar) Set(ts *state.Thread, val objects.Object) (*Token, error)
- type Token
Constants ¶
This section is empty.
Variables ¶
var ( ContextType = objects.NewType("Context", []*objects.Type{objects.ObjectType()}) ContextVarType = objects.NewType("ContextVar", []*objects.Type{objects.ObjectType()}) TokenType = objects.NewType("Token", []*objects.Type{objects.ObjectType()}) )
ContextType, ContextVarType, TokenType are the runtime types for the three classes the _contextvars module exposes.
var Missing objects.Object = newTokenMissing()
Missing is the public Token.MISSING sentinel. Two pointers compare equal only when they reference this singleton.
CPython: Python/context.c:1338 _Py_SINGLETON(context_token_missing)
Functions ¶
This section is empty.
Types ¶
type Context ¶
Context is the immutable PEP 567 mapping from ContextVar to value. vars is the persistent HAMT; prev links the pre-Run context the thread was running in, so Exit can restore it. entered guards against double-entry, matching ctx_entered in CPython.
CPython: Python/context.c:408 PyContext
func CopyCurrent ¶
CopyCurrent snapshots the thread's current context. If the thread has no context yet, a fresh empty one is installed first, matching context_get's lazy initialisation.
CPython: Python/context.c:91 PyContext_CopyCurrent
func NewContext ¶
func NewContext() *Context
NewContext returns a fresh empty Context.
CPython: Python/context.c:439 context_new_empty
func (*Context) Copy ¶
Copy returns a shallow snapshot of c. The new Context shares the HAMT with c (the HAMT is immutable, so sharing is safe) but starts with prev=nil and entered=false.
CPython: Python/context.c:84 PyContext_Copy
func (*Context) Enter ¶
Enter pushes c onto the thread's context stack. Returns an error (RuntimeError) if c is already entered.
CPython: Python/context.c:194 _PyContext_Enter
func (*Context) Eq ¶
Eq reports whether two contexts hold the same bindings. Defers to hamt.Eq.
CPython: Python/context.c:550 context_tp_richcompare
func (*Context) Exit ¶
Exit pops c from the thread's context stack. Returns an error if c has not been entered or if the thread state currently references a different context.
CPython: Python/context.c:223 _PyContext_Exit
func (*Context) Get ¶
Get reads cv from c without going through any cache. found==false means the variable is not bound in this context.
CPython: Python/context.c:584 context_tp_subscript (lookup half)
type ContextVar ¶
ContextVar is the Python-level context variable. The cache fields match CPython's var_cached / var_cached_tsid / var_cached_tsver. hasDefault distinguishes "no default" from "default=None"; CPython uses NULL on the C side for the same purpose. cacheMu guards the cache slot: CPython relies on the GIL to serialize writes, but gopy runs goroutines truly in parallel so we need an explicit lock.
CPython: Python/context.c:861 contextvar_new
func NewContextVar ¶
func NewContextVar(name string, defaultVal objects.Object, hasDefault bool) *ContextVar
NewContextVar creates a ContextVar named name with an optional default. Pass hasDefault=false to create a variable with no default; in that case Get raises LookupError when the variable is unset.
CPython: Python/context.c:861 contextvar_new / 261 PyContextVar_New
func (*ContextVar) Default ¶
func (cv *ContextVar) Default() (objects.Object, bool)
Default returns the (defaultVal, hasDefault) pair the variable was constructed with.
func (*ContextVar) Get ¶
Get returns the value bound to cv in the thread's current context. If the variable is not bound, returns the default the ContextVar was constructed with. Raises LookupError when there is no default. The (cachedTSID, cachedVer) cache short-circuits the HAMT walk on repeat lookups.
CPython: Python/context.c:274 PyContextVar_Get
func (*ContextVar) GetWithDefault ¶
GetWithDefault matches PyContextVar_Get's `def != NULL` branch: the supplied default overrides the ContextVar's stored default and suppresses the LookupError path.
CPython: Python/context.c:325 PyContextVar_Get (def != NULL branch)
func (*ContextVar) Name ¶
func (cv *ContextVar) Name() string
Name returns the variable's name as a Go string.
func (*ContextVar) Reset ¶
func (cv *ContextVar) Reset(ts *state.Thread, tok *Token) error
Reset undoes the cv.Set call that produced tok. The token must be from the same context and must not have been used before.
CPython: Python/context.c:371 PyContextVar_Reset
type Token ¶
Token is the return value of ContextVar.Set; it carries enough state for ContextVar.Reset to undo the set. hadOld is the CPython-side `tok_oldval == NULL` distinction made explicit: hadOld==false means the variable was unset before, so Reset must delete the key rather than rebind it.
CPython: Python/context.c:1115 PyContextToken
func (*Token) OldValue ¶
OldValue returns the prior value of the variable, or the Token.MISSING singleton when the variable was previously unset.
CPython: Python/context.c:1204 token_get_old_value
func (*Token) Used ¶
Used reports whether Reset has consumed t. CPython exposes the same flag indirectly via the "<Token used ...>" repr.
CPython: Python/context.c:1296 tok->tok_used
func (*Token) Var ¶
func (t *Token) Var() *ContextVar
Var returns the ContextVar this token was minted by.
CPython: Python/context.c:1198 token_get_var