TypeView#
- class einspect.views.view_type.TypeView(obj, ref=True)#
- alloc_mode(mode)#
Context manager to temporarily set the type’s allocation mode.
- alloc_slot(*slot, subclasses=True)#
Allocate slot method structs. Defaults to all the following: :rtype:
None
tp_as_async (PyAsyncMethods)
tp_as_number (PyNumberMethods)
tp_as_mapping (PyMappingMethods)
tp_as_sequence (PySequenceMethods)
- as_mutable()#
Context manager to temporarily set the type as mutable.
- drop()#
Drop all references to the base object.
- Return type:
Notes
This is useful for when you want to drop the reference to the base object, but still want to access the view.
- gc_may_be_tracked()#
Return True if the object may be tracked by the GC in the future, or already is.
- Return type:
- info(types=True, arr_max=64)#
Return a formatted info string of the view struct.
- is_gc()#
Returns True if the object implements the Garbage Collector protocol.
If True, a PyGC_HEAD struct will precede the object struct in memory.
- Return type:
- move_from(other)#
Moves data at other Viewable to this View.
- move_to(dst, start=8)#
Copy the object to another view’s location.
- restore(*names)#
Restore named attribute(s) on type.
- swap(other)#
Swaps data at other Viewable with this View.
- unsafe()#
Context manager to enter an unsafe context.
- Return type:
ContextManager
[Self
]
Examples
>>> from einspect import view >>> v = view(100) >>> with v.unsafe(): >>> v.size += 1
>>> with view(100).unsafe() as v: >>> v.size -= 1
- property base: _T#
Returns the base object of the view.
Requires either the View to be created with (ref=True) or the object to support weakrefs.
Notes
If neither ref nor weakref are available, and called within an unsafe context, returns an object via memory address cast. The result of the cast is undefined behavior, and could cause a segmentation fault.
- Returns:
The base object of the view.
- Raises:
AttributeError – If ref=False and base does not support weakrefs.
MovedError – If weak-ref of base is garbage collected.
- property tp_flags: TpFlags#
Return the type’s flags.
- @einspect.views.view_type.impl(*cls, alloc=None, detach=False)#
Decorator for implementing methods on types.
Supports methods decorated with property, classmethod, or staticmethod.
- Parameters:
cls (
Union
[Type
[TypeVar
(_T
)],type
, ]) – The type(s) or Union(s) to implement the method on.alloc (
Optional
[Literal
['mapping'
,'sequence'
,'all'
]]) – The PyMethod allocation mode. Default of None will automatically allocate PyMethod structs as needed. If “sequence” or “mapping”, will prefer the respective PySequenceMethods or PyMappingMethods in cases of ambiguious slot names. (e.g. “__getitem__” or “__len__”). If “all”, will allocate all PyMethod structs.detach (
bool
) – If True, will remove the implemented method from the type when the decorated function is garbage collected. This will hold a reference to the type(s) for the lifetime of the function. Requires function to support weakrefs.
- Return type:
Callable
[[TypeVar
(_Fn
, bound=Callable
)],TypeVar
(_Fn
, bound=Callable
)]- Returns:
The original function after it has been implemented on the types, allows chaining of multiple impl decorators.
Examples
>>> @impl(int) ... def is_even(self): ... return self % 2 == 0
>>> @impl(int | float) # or @impl(int, float) ... @classmethod ... def try_from(cls, value): ... try: ... return cls(value) ... except ValueError: ... return None