|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from ctypes import ( |
| 4 | + c_char, |
| 5 | + c_char_p, |
| 6 | + c_uint, |
| 7 | + c_ulong, |
| 8 | + pointer, |
| 9 | +) |
| 10 | +from typing import TypeVar |
| 11 | + |
| 12 | +from einspect.structs.include.descrobject_h import PyGetSetDef, PyMemberDef |
| 13 | +from einspect.structs.include.methodobject_h import PyMethodDef |
| 14 | +from einspect.structs.include.object_h import ( |
| 15 | + destructor, |
| 16 | + getattrfunc, |
| 17 | + setattrfunc, |
| 18 | + PyAsyncMethods, |
| 19 | + reprfunc, |
| 20 | + PyNumberMethods, |
| 21 | + PySequenceMethods, |
| 22 | + PyMappingMethods, |
| 23 | + hashfunc, |
| 24 | + ternaryfunc, |
| 25 | + getattrofunc, |
| 26 | + setattrofunc, |
| 27 | + PyBufferProcs, |
| 28 | + traverseproc, |
| 29 | + inquiry, |
| 30 | + richcmpfunc, |
| 31 | + getiterfunc, |
| 32 | + iternextfunc, |
| 33 | + descrgetfunc, |
| 34 | + descrsetfunc, |
| 35 | + initproc, |
| 36 | + allocfunc, |
| 37 | + newfunc, |
| 38 | + freefunc, |
| 39 | + vectorcallfunc, |
| 40 | +) |
| 41 | +from einspect.structs.py_object import PyObject, PyVarObject |
| 42 | +from einspect.types import ptr |
| 43 | +from typing_extensions import Annotated, Self |
| 44 | + |
| 45 | +__all__ = ("PyTypeObject",) |
| 46 | + |
| 47 | +_T = TypeVar("_T") |
| 48 | + |
| 49 | +DEFAULT = object() |
| 50 | + |
| 51 | + |
| 52 | +# noinspection PyPep8Naming |
| 53 | +class PyTypeObject(PyVarObject[_T, None, None]): |
| 54 | + """ |
| 55 | + Defines a PyTypeObject Structure. |
| 56 | +
|
| 57 | + https://github.com/python/cpython/blob/3.11/Doc/includes/typestruct.h |
| 58 | +
|
| 59 | + .. |
| 60 | + source: Include/cpython/object.h (struct _typeobject) |
| 61 | + """ |
| 62 | + |
| 63 | + tp_name: Annotated[bytes, c_char_p] |
| 64 | + # For allocation |
| 65 | + tp_basicsize: int |
| 66 | + tp_itemsize: int |
| 67 | + # Methods to implement standard operations |
| 68 | + tp_dealloc: destructor |
| 69 | + tp_vectorcall_offset: int |
| 70 | + tp_getattr: getattrfunc |
| 71 | + tp_setattr: setattrfunc |
| 72 | + # formerly known as tp_compare (Python 2) or tp_reserved (Python 3) |
| 73 | + tp_as_async: ptr[PyAsyncMethods] |
| 74 | + |
| 75 | + tp_repr: reprfunc |
| 76 | + |
| 77 | + # Method suites for standard classes |
| 78 | + tp_as_number: ptr[PyNumberMethods] |
| 79 | + tp_as_sequence: ptr[PySequenceMethods] |
| 80 | + tp_as_mapping: ptr[PyMappingMethods] |
| 81 | + |
| 82 | + # More standard operations (here for binary compatibility) |
| 83 | + tp_hash: hashfunc |
| 84 | + tp_call: ternaryfunc |
| 85 | + tp_str: reprfunc |
| 86 | + tp_getattro: getattrofunc |
| 87 | + tp_setattro: setattrofunc |
| 88 | + |
| 89 | + # Functions to access object as input/output buffer |
| 90 | + tp_as_buffer: ptr[PyBufferProcs] |
| 91 | + |
| 92 | + # Flags to define presence of optional/expanded features |
| 93 | + tp_flags: Annotated[int, c_ulong] |
| 94 | + |
| 95 | + tp_doc: Annotated[bytes, c_char_p] # Documentation string |
| 96 | + |
| 97 | + # Assigned meaning in release 2.0 |
| 98 | + # call function for all accessible objects |
| 99 | + tp_traverse: traverseproc |
| 100 | + |
| 101 | + tp_clear: inquiry # delete references to contained objects |
| 102 | + |
| 103 | + # Assigned meaning in release 2.1 |
| 104 | + # rich comparisons |
| 105 | + tp_richcompare: richcmpfunc |
| 106 | + |
| 107 | + tp_weaklistoffset: int # weak reference enabler |
| 108 | + |
| 109 | + # Iterators |
| 110 | + tp_iter: getiterfunc |
| 111 | + tp_iternext: iternextfunc |
| 112 | + |
| 113 | + # Attribute descriptor and subclassing stuff |
| 114 | + tp_methods: ptr[PyMethodDef] |
| 115 | + tp_members: ptr[PyMemberDef] |
| 116 | + tp_getset: ptr[PyGetSetDef] |
| 117 | + |
| 118 | + # Strong reference on a heap type, borrowed reference on a static type |
| 119 | + tp_base: pointer[Self] |
| 120 | + tp_dict: ptr[PyObject] |
| 121 | + tp_descr_get: descrgetfunc |
| 122 | + tp_descr_set: descrsetfunc |
| 123 | + tp_dictoffset: int |
| 124 | + tp_init: initproc |
| 125 | + tp_alloc: allocfunc |
| 126 | + tp_new: newfunc |
| 127 | + tp_free: freefunc # Low-level free-memory routine |
| 128 | + tp_is_gc: inquiry # For PyObject_IS_GC |
| 129 | + tp_bases: ptr[PyObject] |
| 130 | + tp_mro: ptr[PyObject] # method resolution order |
| 131 | + tp_cache: ptr[PyObject] |
| 132 | + tp_subclasses: ptr[PyObject] # for static builtin types this is an index |
| 133 | + tp_weaklist: ptr[PyObject] |
| 134 | + tp_del: destructor |
| 135 | + |
| 136 | + # Type attribute cache version tag. Added in version 2.6 |
| 137 | + tp_version_tag: Annotated[int, c_uint] |
| 138 | + |
| 139 | + tp_finalize: destructor |
| 140 | + tp_vectorcall: vectorcallfunc |
| 141 | + |
| 142 | + # bitset of which type-watchers care about this type |
| 143 | + tp_watched: c_char |
| 144 | + |
| 145 | + |
| 146 | +# https://github.com/python/cpython/blob/809aa9a682fc865f7502e7421da0a74d204aab6d/Objects/typevarobject.c#L29 |
| 147 | +class PyTypeVarObject(PyVarObject[_T, None, None]): |
| 148 | + name: ptr[PyObject] |
| 149 | + # not sure why but this is the only thing that works but that's fine because it's the only thing we need |
| 150 | + bound: ptr[PyObject] |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + print(PyTypeObject.from_object(type(3.14))) |
0 commit comments