Environment Config Context

Module: cbase.env

A Cython-backed context manager for temporary environment configuration changes. Used as the base class for AllocatorConfigContext.

class cbase.env.EnvConfigContext(dict overrides=None, **kwargs)

Bases: object

__enter__()
Return type:

EnvConfigContext

__exit__(exc_type, exc_val, exc_tb)
Parameters:

exc_type (type[BaseException] | None)

Return type:

None

__init__(*args, **kwargs)
cbase.env.__reduce_cython__(self)
cbase.env.__setstate_cython__(self, __pyx_state)

Usage

EnvConfigContext tracks configuration overrides and restores originals on exit. It supports three usage patterns:

Context manager:

from cbase.env import EnvConfigContext

ctx = EnvConfigContext(debug=True, log_level=3)
with ctx:
    # debug=True, log_level=3 are active
    ...
# Original values restored

Decorator:

@EnvConfigContext(cache_size=1024)
def my_function():
    # cache_size=1024 active during this call
    ...

Composition with ``|`` (union) and ``~`` (invert):

a = EnvConfigContext(debug=True)
b = EnvConfigContext(log_level=5)

merged = a | b          # Both debug=True and log_level=5
inverted = ~a           # debug=False

Inheritance

AllocatorConfigContext (in cbase.allocator_protocol) extends EnvConfigContext to additionally propagate configuration changes to the heap and SHM allocator backends. See Allocator Protocol.

from cbase.allocator_protocol import AllocatorConfigContext

ctx = AllocatorConfigContext(locked=True, autopage_capacity=65536)
with ctx:
    # Thread-safety enabled, 64 KiB auto pages
    ...

See also