-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_defbase_ctypinit.py
More file actions
1012 lines (781 loc) · 31.3 KB
/
Copy path_defbase_ctypinit.py
File metadata and controls
1012 lines (781 loc) · 31.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from ctypes import *
import sys
defined_Py_TRACE_REFS = hasattr(sys, 'getobjects')
if not hasattr(sys, '_is_gil_enabled'):
GIL_ENABLED = True
else:
GIL_ENABLED = sys._is_gil_enabled()
from typing import (TypeVar, Generic, Optional,
Mapping, Any, Self, Dict,
AnyStr, Sequence, Type,
Iterable)
_CWT = TypeVar('_WT')
class IArray(Generic[_CWT]):
def __getitem__(self, index: int) -> _CWT: ...
def __setitem__(self, index: int, value: _CWT) -> _CWT: ...
class IPointer(IArray[_CWT]):
contents: _CWT
if sys.version_info < (3, 10):
raise RuntimeError('Versions before 3.10 are not supported.')
if sys.version_info >= (3, 10) and sys.version_info < (3, 12):
class PyObject(Structure, Generic[_CWT]):
fields_extra = []
if defined_Py_TRACE_REFS:
fields_extra = [
('unusedPtr', c_void_p),
('unusedPtr2', c_void_p)
]
_fields_ = fields_extra + [
('unusedSizeT', c_ssize_t),
('_ob_type', c_void_p)
]
@property
def ob_type(self) -> IPointer['PyTypeObject']:
return cast(self._ob_type, PyTypeObject_PTR)
@ob_type.setter
def ob_type(self, ob_type: type):
self._ob_type = id(ob_type)
def __repr__(self) -> str:
return repr(cast(byref(self), py_object).value)
@property
def object(self) -> _CWT:
return cast(byref(self), py_object).value
@classmethod
def offset(cls, field: str) -> int:
return getattr(cls, field).offset
@property
def refcnt(self) -> int:
return self.unusedSizeT
@refcnt.setter
def refcnt(self, refcnt: int):
self.unusedSizeT = refcnt
class PyVarObject(PyObject[_CWT]):
_fields_ = [
('unusedSizeT', c_ssize_t)
]
elif sys.version_info >= (3, 12) and sys.version_info < (3, 15):
if sys.version_info != (3, 14):
class PyObject(Structure, Generic[_CWT]):
fields_extra = []
if defined_Py_TRACE_REFS:
fields_extra = [
('unusedPtr', c_void_p),
('unusedPtr2', c_void_p)
]
_fields_ = fields_extra + [
('unusedInt64', c_ssize_t),
('_ob_type', c_void_p)
]
@property
def ob_type(self) -> IPointer['PyTypeObject']:
return cast(self._ob_type, PyTypeObject_PTR)
@ob_type.setter
def ob_type(self, ob_type: type):
self._ob_type = id(ob_type)
def __repr__(self) -> str:
return repr(self.object)
@property
def object(self) -> _CWT:
return cast(byref(self), py_object).value
@classmethod
def offset(cls, field: str) -> int:
return getattr(cls, field).offset
@property
def refcnt(self) -> int:
return self.unusedInt64
@refcnt.setter
def refcnt(self, refcnt: int):
self.unusedInt64 = refcnt
else:
if GIL_ENABLED:
class PyObject(Structure, Generic[_CWT]):
class _Refcnt(Union):
_fields_ = [
('ob_flags', c_uint16),
('ob_overflow', c_uint16),
('ob_refcnt', c_uint32)
]
_fields_ = [
('ob_refcnt', _Refcnt),
('_ob_type', c_void_p)
]
@property
def ob_type(self) -> IPointer['PyTypeObject']:
return cast(self._ob_type, PyTypeObject_PTR)
@ob_type.setter
def ob_type(self, ob_type: type):
self._ob_type = id(ob_type)
def __repr__(self) -> str:
return repr(self.object)
@property
def object(self) -> _CWT:
return cast(byref(self), py_object).value
@classmethod
def offset(cls, field: str) -> int:
return getattr(cls, field).offset
@property
def refcnt(self) -> int:
return self.unusedInt64
@refcnt.setter
def refcnt(self, refcnt: int):
self.unusedInt64 = refcnt
else:
class PyObject(Structure, Generic[_CWT]):
_fields_ = [
('ob_tid', c_int64),
('ob_flags', c_uint16),
('ob_mutex', c_uint8),
('ob_gc_bits', c_uint8),
('ob_ref_local', c_uint32),
('ob_ref_shared', c_ssize_t),
('_ob_type', c_void_p)
]
@property
def ob_type(self) -> IPointer['PyTypeObject']:
return cast(self._ob_type, PyTypeObject_PTR)
@ob_type.setter
def ob_type(self, ob_type: type):
self._ob_type = id(ob_type)
def __repr__(self) -> str:
return repr(self.object)
@property
def object(self) -> _CWT:
return cast(byref(self), py_object).value
@classmethod
def offset(cls, field: str) -> int:
return getattr(cls, field).offset
@property
def refcnt(self) -> int:
return self.ob_ref_local
@refcnt.setter
def refcnt(self, refcnt: int):
self.ob_ref_local = refcnt
class PyVarObject(PyObject[_CWT]):
_fields_ = [
('unusedSizeT', c_ssize_t)
]
PyObject_PTR = POINTER(PyObject)
def _wrap_flag(typ: type, name: str, flag: int, method_suffix: str = 'Flag'):
has_flag = f'Has{method_suffix}'
set_flag = f'Set{method_suffix}'
remove_flag = f'Remove{method_suffix}'
def getter(self) -> bool:
return getattr(self, has_flag)(flag)
getter.__name__ = name
getter_prop = property(getter)
def setter(self, value: bool):
if value:
getattr(self, set_flag)(flag)
else:
getattr(self, remove_flag)(flag)
setter.__name__ = name
setter_prop = getter_prop.setter(setter)
setattr(typ, name, getter_prop)
setattr(typ, name, setter_prop)
class PyMemberDef(Structure):
_fields_ = [
('name', c_char_p),
('unusedInt', c_int),
('unusedSizeT', c_ssize_t),
('flags', c_int),
('unusedPtr', c_void_p)
]
name: bytes
flags: int
def SetFlag(self, flag: int):
if self.flags & flag: return
self.flags |= flag
def RemoveFlag(self, flag: int):
if not self.flags & flag: return
self.flags &= (~flag)
def HasFlag(self, flag: int) -> bool:
return (self.flags & flag) != 0
def EnumFlags(self) -> Sequence[str]:
result: list[str] = []
if self.HasFlag(Py_READONLY):
result.append('Py_READONLY')
if self.HasFlag(Py_AUDIT_READ):
result.append('Py_AUDIT_READ')
if self.HasFlag(_Py_WRITE_RESTRICTED):
result.append('_Py_WRITE_RESTRICTED')
if self.HasFlag(Py_RELATIVE_OFFSET):
result.append('Py_RELATIVE_OFFSET')
return tuple(result)
def __repr__(self) -> str:
repr_string: str = f"<member '{self.name.decode()}'"
if self.HasFlag(Py_READONLY):
repr_string += ', read-only'
repr_string += '>'
return repr_string
is_relative_offset: bool
_write_restricted: bool
audit_read: bool
readonly: bool
def __init__(self, name: AnyStr=None,
type: int=0, offset: int=0,
flags: int=0, doc: AnyStr=None):
if isinstance(name, str): name = name.encode()
if isinstance(doc, str): doc = doc.encode()
super().__init__(name, type, offset, flags, doc)
@classmethod
def array(self, members: Iterable['PyMemberDef']) -> IArray['PyMemberDef']:
if not isinstance(members, list): members = list(members)
members.append(PyMemberDef())
members_array = (PyMemberDef * len(members))(*members)
PyObject_CAST_DEREF(members_array).refcnt = 0xffffffff
return members_array
PyMemberDef_PTR = POINTER(PyMemberDef)
getter = CFUNCTYPE(c_void_p, PyObject_PTR, c_void_p)
setter = CFUNCTYPE(c_int, PyObject_PTR, PyObject_PTR, c_void_p)
class PyGetSetDef(Structure):
_fields_ = [
('name', c_char_p),
('getter', getter),
('setter', setter),
('doc', c_char_p),
('closure', c_void_p)
]
PyGetSetDef_PTR = POINTER(PyGetSetDef)
# Types
Py_T_SHORT = 0
Py_T_INT = 1
Py_T_LONG = 2
Py_T_FLOAT = 3
Py_T_DOUBLE = 4
Py_T_STRING = 5
_Py_T_OBJECT = 6 # Deprecated, use Py_T_OBJECT_EX instead
# the ordering here is weird for binary compatibility
Py_T_CHAR = 7 # 1-character string
Py_T_BYTE = 8 # 8-bit signed int
# unsigned variants:
Py_T_UBYTE = 9
Py_T_USHORT = 10
Py_T_UINT = 11
Py_T_ULONG = 12
# Added by Jack: strings contained in the structure
Py_T_STRING_INPLACE = 13
# Added by Lillo: bools contained in the structure (assumed char)
Py_T_BOOL = 14
Py_T_OBJECT_EX = 16
Py_T_LONGLONG = 17
Py_T_ULONGLONG = 18
Py_T_PYSSIZET = 19 # Py_ssize_t
_Py_T_NONE = 20 # Deprecated. Value is always None.
Py_READONLY = 1
Py_AUDIT_READ = 2 # Added in 3.10, harmless no-op before that
_Py_WRITE_RESTRICTED = 4 # Deprecated, no-op. Do not reuse the value.
Py_RELATIVE_OFFSET = 8
_wrap_flag(PyMemberDef, 'readonly', Py_READONLY)
_wrap_flag(PyMemberDef, 'audit_read', Py_AUDIT_READ)
_wrap_flag(PyMemberDef, '_write_restricted', _Py_WRITE_RESTRICTED)
_wrap_flag(PyMemberDef, 'is_relative_offset', Py_RELATIVE_OFFSET)
class MProxy(PyObject[_CWT]):
_fields_ = [
('mapping', PyObject_PTR)
]
mapping: IPointer[PyObject[_CWT]]
PyMappingProxyObject_PTR = POINTER(MProxy)
def PyMappingProxyObject_CAST(obj: _CWT) -> MProxy[_CWT]:
return cast(id(obj), PyMappingProxyObject_PTR)
_Py_TPFLAGS_STATIC_BUILTIN = (1 << 1)
Py_TPFLAGS_MANAGED_WEAKREF = (1 << 3)
"""
Placement of dict (and values) pointers are managed by the VM, not by the type.
The VM will automatically set tp_dictoffset.
"""
Py_TPFLAGS_MANAGED_DICT = (1 << 4)
Py_TPFLAGS_PREHEADER = (Py_TPFLAGS_MANAGED_WEAKREF | Py_TPFLAGS_MANAGED_DICT)
# Set if instances of the type object are treated as sequences for pattern matching
Py_TPFLAGS_SEQUENCE = (1 << 5)
# Set if instances of the type object are treated as mappings for pattern matching
Py_TPFLAGS_MAPPING = (1 << 6)
"""
Disallow creating instances of the type: set tp_new to NULL and don't create
the "__new__" key in the type dictionary.
"""
Py_TPFLAGS_DISALLOW_INSTANTIATION = (1 << 7)
Py_TPFLAGS_IMMUTABLETYPE = (1 << 8)
Py_TPFLAGS_HEAPTYPE = (1 << 9)
Py_TPFLAGS_BASETYPE = (1 << 10)
Py_TPFLAGS_HAVE_VECTORCALL = (1 << 11)
Py_TPFLAGS_READY = (1 << 12)
Py_TPFLAGS_READYING = (1 << 13)
Py_TPFLAGS_HAVE_GC = (1 << 14)
# These two bits are preserved for Stackless Python, next after this is 17
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION = (3 << 15)
# Objects behave like an unbound method
Py_TPFLAGS_METHOD_DESCRIPTOR = (1 << 17)
# Object has up-to-date type attribute cache
Py_TPFLAGS_VALID_VERSION_TAG = (1 << 19)
# Type is abstract and cannot be instantiated
Py_TPFLAGS_IS_ABSTRACT = (1 << 20)
# This undocumented flag gives certain built-ins their unique pattern-matching
# behavior, which allows a single positional subpattern to match against the
# subject itself (rather than a mapped attribute on it):
_Py_TPFLAGS_MATCH_SELF = (1 << 22)
# Items (ob_size*tp_itemsize) are found at the end of an instance's memory
Py_TPFLAGS_ITEMS_AT_END = (1 << 23)
# These flags are used to determine if a type is a subclass.
Py_TPFLAGS_LONG_SUBCLASS = (1 << 24)
Py_TPFLAGS_LIST_SUBCLASS = (1 << 25)
Py_TPFLAGS_TUPLE_SUBCLASS = (1 << 26)
Py_TPFLAGS_BYTES_SUBCLASS = (1 << 27)
Py_TPFLAGS_UNICODE_SUBCLASS = (1 << 28)
Py_TPFLAGS_DICT_SUBCLASS = (1 << 29)
Py_TPFLAGS_BASE_EXC_SUBCLASS = (1 << 30)
Py_TPFLAGS_TYPE_SUBCLASS = (1 << 31)
Py_TPFLAGS_DEFAULT = ( \
Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
0)
"""
NOTE: Some of the following flags reuse lower bits (removed as part of the
Python 3.0 transition).
"""
"""
The following flags are kept for compatibility; in previous
versions they indicated presence of newer tp_* fields on the
type struct.
Starting with 3.8, binary compatibility of C extensions across
feature releases of Python is not supported anymore (except when
using the stable ABI, in which all classes are created dynamically,
using the interpreter's memory layout.)
Note that older extensions using the stable ABI set these flags,
so the bits must not be repurposed.
"""
Py_TPFLAGS_HAVE_FINALIZE = (1 << 0)
Py_TPFLAGS_HAVE_VERSION_TAG = (1 << 18)
def _wrap_tpflag(name: str, flag: int):
_wrap_flag(PyTypeObject, name, flag, method_suffix='TPFLAG')
class PyTypeObject(PyVarObject[_CWT]):
# только нужные
_fields_ = [
('tp_name', c_char_p),
('tp_basicsize', c_ssize_t),
('unusedSizeT', c_ssize_t),
('tp_dealloc', c_void_p),
('unusedSizeT3', c_ssize_t),
('unusedPtrs', c_void_p * 13),
('tp_flags', c_ulong),
('unusedPtrs2', c_void_p * 8),
('tp_members', PyMemberDef_PTR),
('tp_getset', PyGetSetDef_PTR),
('_tp_base', c_void_p),
('tp_dict', PyMappingProxyObject_PTR)
]
@property
def tp_base(self) -> IPointer['PyTypeObject']:
return cast(self._tp_base, PyTypeObject_PTR)
tp_members: IPointer[PyMemberDef]
tp_dict: IPointer[MProxy[dict]]
tp_dealloc: c_void_p
tp_basicsize: int
tp_flags: int
@property
def type(self) -> Type[_CWT]:
return To_PyType_DEREFERENCED(self)
@property
def object(self) -> Type[_CWT]:
return To_PyObject_DEREFERENCED(self)
def SetTPFLAG(self, tp_flag: int):
if self.tp_flags & tp_flag: return
self.tp_flags |= tp_flag
def RemoveTPFLAG(self, tp_flag: int):
if not self.tp_flags & tp_flag: return
self.tp_flags &= (~tp_flag)
def HasTPFLAG(self, tp_flag: int) -> bool:
return (self.tp_flags & tp_flag) != 0
def EnumTPFLAGS(self) -> Sequence[str]:
result: list[str] = []
if self.HasTPFLAG(_Py_TPFLAGS_STATIC_BUILTIN):
result.append('_Py_TPFLAGS_STATIC_BUILTIN')
if self.HasTPFLAG(Py_TPFLAGS_MANAGED_WEAKREF):
if self.HasTPFLAG(Py_TPFLAGS_MANAGED_DICT):
result.append('Py_TPFLAGS_PREHEADER')
else:
result.append('Py_TPFLAGS_MANAGED_WEAKREF')
elif self.HasTPFLAG(Py_TPFLAGS_MANAGED_DICT):
result.append('Py_TPFLAGS_MANAGED_DICT')
if self.HasTPFLAG(Py_TPFLAGS_SEQUENCE):
result.append('Py_TPFLAGS_SEQUENCE')
if self.HasTPFLAG(Py_TPFLAGS_MAPPING):
result.append('Py_TPFLAGS_MAPPING')
if self.HasTPFLAG(Py_TPFLAGS_DISALLOW_INSTANTIATION):
result.append('Py_TPFLAGS_DISALLOW_INSTANTIATION')
if self.HasTPFLAG(Py_TPFLAGS_IMMUTABLETYPE):
result.append('Py_TPFLAGS_IMMUTABLETYPE')
if self.HasTPFLAG(Py_TPFLAGS_HEAPTYPE):
result.append('Py_TPFLAGS_HEAPTYPE')
if self.HasTPFLAG(Py_TPFLAGS_BASETYPE):
result.append('Py_TPFLAGS_BASETYPE')
if self.HasTPFLAG(Py_TPFLAGS_HAVE_VECTORCALL):
result.append('Py_TPFLAGS_HAVE_VECTORCALL')
if self.HasTPFLAG(Py_TPFLAGS_READY):
result.append('Py_TPFLAGS_READY')
if self.HasTPFLAG(Py_TPFLAGS_READYING):
result.append('Py_TPFLAGS_READYING')
if self.HasTPFLAG(Py_TPFLAGS_HAVE_GC):
result.append('Py_TPFLAGS_HAVE_GC')
if self.HasTPFLAG(Py_TPFLAGS_HAVE_STACKLESS_EXTENSION):
result.append('Py_TPFLAGS_HAVE_STACKLESS_EXTENSION')
if self.HasTPFLAG(Py_TPFLAGS_METHOD_DESCRIPTOR):
result.append('Py_TPFLAGS_METHOD_DESCRIPTOR')
if self.HasTPFLAG(Py_TPFLAGS_VALID_VERSION_TAG):
result.append('Py_TPFLAGS_VALID_VERSION_TAG')
if self.HasTPFLAG(Py_TPFLAGS_IS_ABSTRACT):
result.append('Py_TPFLAGS_IS_ABSTRACT')
if self.HasTPFLAG(_Py_TPFLAGS_MATCH_SELF):
result.append('_Py_TPFLAGS_MATCH_SELF')
if self.HasTPFLAG(Py_TPFLAGS_ITEMS_AT_END):
result.append('Py_TPFLAGS_ITEMS_AT_END')
if self.HasTPFLAG(Py_TPFLAGS_LONG_SUBCLASS):
result.append('Py_TPFLAGS_LONG_SUBCLASS')
if self.HasTPFLAG(Py_TPFLAGS_LIST_SUBCLASS):
result.append('Py_TPFLAGS_LIST_SUBCLASS')
if self.HasTPFLAG(Py_TPFLAGS_TUPLE_SUBCLASS):
result.append('Py_TPFLAGS_TUPLE_SUBCLASS')
if self.HasTPFLAG(Py_TPFLAGS_BYTES_SUBCLASS):
result.append('Py_TPFLAGS_BYTES_SUBCLASS')
if self.HasTPFLAG(Py_TPFLAGS_UNICODE_SUBCLASS):
result.append('Py_TPFLAGS_UNICODE_SUBCLASS')
if self.HasTPFLAG(Py_TPFLAGS_DICT_SUBCLASS):
result.append('Py_TPFLAGS_DICT_SUBCLASS')
if self.HasTPFLAG(Py_TPFLAGS_BASE_EXC_SUBCLASS):
result.append('Py_TPFLAGS_BASE_EXC_SUBCLASS')
if self.HasTPFLAG(Py_TPFLAGS_TYPE_SUBCLASS):
result.append('Py_TPFLAGS_TYPE_SUBCLASS')
return tuple(result)
def __repr__(self) -> str:
return repr(self.type)
@property
def members(self) -> Sequence[PyMemberDef]:
return tuple(self.GetMemberIter())
@members.setter
def members(self, members: Iterable[PyMemberDef]):
self.tp_members = PyMemberDef.array(members)
def Reload(self) -> bool:
self.RemoveTPFLAG(Py_TPFLAGS_READY)
return PyType_Ready_DEREFERENCED(self) == 0
def GetMemberIter(self) -> 'PyMemberDef_Iterator':
return PyMemberDef_Iterator(self.tp_members)
is_static_builtin: bool
is_managed_weakref: bool
is_managed_dict: bool
is_preheader: bool
is_sequence: bool
is_mapping: bool
disallow_instantiation: bool
is_heap_type: bool
is_base_type: bool
immutable: bool
have_vectorcall: bool
readying: bool
ready: bool
have_stackless_extension: bool
have_stackless_ext: bool
have_gc: bool
is_method_descriptor: bool
abstract: bool
is_long_subclass: bool
is_tuple_subclass: bool
is_list_subclass: bool
is_unicode_subclass: bool
is_bytes_subclass: bool
is_base_exc_subclass: bool
is_dict_subclass: bool
is_type_subclass: bool
_wrap_tpflag('is_static_builtin', _Py_TPFLAGS_STATIC_BUILTIN)
_wrap_tpflag('is_managed_weakref', Py_TPFLAGS_MANAGED_WEAKREF)
_wrap_tpflag('is_managed_dict', Py_TPFLAGS_MANAGED_DICT)
_wrap_tpflag('is_preheader', Py_TPFLAGS_PREHEADER)
_wrap_tpflag('is_sequence', Py_TPFLAGS_SEQUENCE)
_wrap_tpflag('is_mapping', Py_TPFLAGS_MAPPING)
_wrap_tpflag('disallow_instantiation', Py_TPFLAGS_DISALLOW_INSTANTIATION)
_wrap_tpflag('immutable', Py_TPFLAGS_IMMUTABLETYPE)
_wrap_tpflag('is_heap_type', Py_TPFLAGS_HEAPTYPE)
_wrap_tpflag('is_base_type', Py_TPFLAGS_BASETYPE)
_wrap_tpflag('have_vectorcall', Py_TPFLAGS_HAVE_VECTORCALL)
_wrap_tpflag('readying', Py_TPFLAGS_READYING)
_wrap_tpflag('ready', Py_TPFLAGS_READY)
_wrap_tpflag('have_stackless_extension', Py_TPFLAGS_HAVE_STACKLESS_EXTENSION)
_wrap_tpflag('have_stackless_ext', Py_TPFLAGS_HAVE_STACKLESS_EXTENSION)
_wrap_tpflag('have_gc', Py_TPFLAGS_HAVE_GC)
_wrap_tpflag('is_method_descriptor', Py_TPFLAGS_METHOD_DESCRIPTOR)
_wrap_tpflag('abstract', Py_TPFLAGS_IS_ABSTRACT)
_wrap_tpflag('is_long_subclass', Py_TPFLAGS_LONG_SUBCLASS)
_wrap_tpflag('is_tuple_subclass', Py_TPFLAGS_TUPLE_SUBCLASS)
_wrap_tpflag('is_list_subclass', Py_TPFLAGS_LIST_SUBCLASS)
_wrap_tpflag('is_unicode_subclass', Py_TPFLAGS_UNICODE_SUBCLASS)
_wrap_tpflag('is_bytes_subclass', Py_TPFLAGS_BYTES_SUBCLASS)
_wrap_tpflag('is_base_exc_subclass', Py_TPFLAGS_BASE_EXC_SUBCLASS)
_wrap_tpflag('is_dict_subclass', Py_TPFLAGS_DICT_SUBCLASS)
_wrap_tpflag('is_type_subclass', Py_TPFLAGS_TYPE_SUBCLASS)
PyTypeObject_PTR = POINTER(PyTypeObject)
class PyMethodDef(Structure):
_fields_ = [
('ml_name', c_char_p),
('ml_meth', c_void_p),
('ml_flags', c_int),
('ml_doc', c_char_p)
]
ml_name: str
ml_meth: int
ml_flags: int
ml_doc: str
PyMethodDef_PTR = POINTER(PyMethodDef)
class PyCFunctionObject(PyObject[_CWT]):
_fields_ = [
('m_ml', PyMethodDef_PTR),
('m_self', PyObject_PTR),
('m_module', PyObject_PTR),
('m_weakreflist', PyObject_PTR),
('vectorcall', c_void_p)
]
m_ml: IPointer[PyMethodDef]
m_self: IPointer[PyObject]
m_module: IPointer[PyObject]
m_weakreflist: IPointer[PyObject]
vectorcall: int
PyCFunctionObject_PTR = POINTER(PyCFunctionObject)
class PyCMethodObject(Structure):
_fields_ = [
('func', PyCFunctionObject),
('mm_class', PyTypeObject_PTR)
]
func: PyCFunctionObject
mm_class: IPointer[PyTypeObject]
PyCMethodObject_PTR = POINTER(PyCMethodObject)
class PyDescrObject(PyObject[_CWT]):
_fields_ = [
('d_type', PyTypeObject_PTR),
('d_name', PyObject_PTR),
('d_qualname', PyObject_PTR)
]
d_type: IPointer[PyTypeObject]
d_name: IPointer[PyObject]
d_qualname: IPointer[PyObject]
PyDescrObject_PTR = POINTER(PyDescrObject)
class PyMethodDescrObject(PyDescrObject[_CWT]):
_fields_ = [
('d_method', PyMethodDef_PTR),
('vectorcall', c_void_p)
]
d_method: IPointer[PyMethodDef]
vectorcall: int
PyMethodDescrObject_PTR = POINTER(PyMethodDescrObject)
def PyCFunctionObject_CAST(obj: _CWT) -> IPointer[PyCFunctionObject[_CWT]]:
return cast(id(obj), PyCFunctionObject_PTR)
def PyCFunctionObject_CAST_DEREF(obj: _CWT) -> PyCFunctionObject[_CWT]:
return cast(id(obj), PyCFunctionObject_PTR).contents
def PyCMethodObject_CAST(obj: _CWT) -> IPointer[PyCMethodObject]:
return cast(id(obj), PyCMethodObject_PTR)
def PyCMethodObject_CAST_DEREF(obj: _CWT) -> PyCMethodObject:
return cast(id(obj), PyCMethodObject_PTR).contents
def PyDescrObject_CAST(obj: _CWT) -> IPointer[PyDescrObject]:
return cast(id(obj), PyDescrObject_PTR)
def PyDescrObject_CAST_DEREF(obj: _CWT) -> PyDescrObject:
return cast(id(obj), PyDescrObject_PTR).contents
def PyMethodDescrObject_CAST(obj: _CWT) -> IPointer[PyMethodDescrObject[_CWT]]:
return cast(id(obj), PyMethodDescrObject_PTR)
def PyMethodDescrObject_CAST_DEREF(obj: _CWT) -> PyMethodDescrObject[_CWT]:
return cast(id(obj), PyMethodDescrObject_PTR).contents
class PyCArgObject(PyObject[_CWT]):
class U(Union):
_fields_ = [
('c', c_char),
('b', c_byte),
('h', c_short),
('i', c_int),
('l', c_long),
('q', c_longlong),
('g', c_longdouble),
('d', c_double),
('f', c_float),
('p', c_void_p),
('D', c_double * 2),
('F', c_float * 2),
('G', c_longdouble * 2)
]
p: c_void_p
_fields_ = [
('pffi_type', c_void_p),
('tag', c_char),
('value', U),
('obj', POINTER(PyObject)),
('size', c_ssize_t)
]
obj: IPointer[PyObject[_CWT]]
pffi_type: c_void_p
tag: c_char
value: U
PyCArgObject_PTR = POINTER(PyCArgObject)
class PyCDataObject_HEADLESS(PyObject):
_fields_ = [
('b_ptr', c_char_p)
]
b_ptr: c_char_p
PyCDataObject_HEADLESS_PTR = POINTER(PyCDataObject_HEADLESS)
pythonapi._PyObject_GC_New.argtypes = [c_void_p]
pythonapi._PyObject_GC_New.restype = PyObject_PTR
def PyObject_GC_New(typ: type[_CWT], typeobj) -> IPointer[_CWT]:
return cast(pythonapi._PyObject_GC_New(id(typeobj)), POINTER(typ))
def NewObject(typeobj: type[_CWT]) -> _CWT:
obj = cast(pythonapi._PyObject_GC_New(id(typeobj)), py_object).value
PyObject_GC_Track(obj)
return obj
pythonapi.PyObject_GC_Track.argtypes = [c_void_p]
pythonapi.PyObject_GC_Track.restype = None
def PyObject_GC_Track(obj):
pythonapi.PyObject_GC_Track(id(obj))
pythonapi.PyType_Ready.argtypes = [PyTypeObject_PTR]
pythonapi.PyType_Ready.restype = c_int
def PyType_Ready(typeobj: IPointer[PyTypeObject]) -> int: ...
PyType_Ready = pythonapi.PyType_Ready
def PyType_Ready_DEREFERENCED(typeobj: PyTypeObject) -> int:
return PyType_Ready(byref(typeobj))
pythonapi.Py_IncRef.argtypes = [c_void_p]
pythonapi.Py_IncRef.restype = None
def Py_INCREF(obj):
pythonapi.Py_IncRef(id(obj))
pythonapi.PyObject_GC_UnTrack.argtypes = [c_void_p]
pythonapi.PyObject_GC_UnTrack.restype = None
def PyObject_GC_UnTrack(obj):
pythonapi.PyObject_GC_UnTrack(id(obj))
class ICArgObject: ...
class ICData:
_b_base_: int
_b_needsfree_: bool
_objects: Mapping[Any, int] | None
def __buffer__(self, flags: int, /) -> memoryview: ...
def __ctypes_from_outparam__(self, /) -> Self: ...
if sys.version_info >= (3, 14):
__pointer_type__: type
ICARG_CWT = TypeVar('ICARG_CWT', bound=ICArgObject)
def New_PyCArgObject(t: type[ICARG_CWT]) -> Optional[IPointer[PyCArgObject[ICARG_CWT]]]:
p = PyObject_GC_New(PyCArgObject, PyTypeObject_PTR)
if not p: return None
p.contents.pffi_type = None
p.contents.tag = b'\x00'
p.contents.obj = None
memset(byref(p.contents.value), 0, sizeof(p.contents.value))
pythonapi.PyObject_GC_Track(p)
return p
ffi_pointer_type = c_int(8)
def Init_PyCArgObject(parg: IPointer[PyCArgObject[_CWT]], obj: ICData) -> _CWT:
arg = cast(parg, py_object).value
parg.contents.tag = b'P'
parg.contents.pffi_type = cast(byref(ffi_pointer_type), c_void_p)
arg._obj = obj
parg.contents.value.p = cast(cast(id(obj), PyCDataObject_HEADLESS_PTR).contents.b_ptr, c_void_p)
return arg
def PyCArgObject_CAST(obj: ICArgObject) -> IPointer[PyCArgObject[ICArgObject]]:
return cast(id(obj), PyCArgObject_PTR)
def PyCArgObject_CAST_DEREF(obj: ICArgObject) -> PyCArgObject[ICArgObject]:
return cast(id(obj), PyCArgObject_PTR).contents
msvcrt = CDLL('msvcrt.dll')
pythonapi.PyObject_Malloc.argtypes = [c_size_t]
pythonapi.PyObject_Malloc.restype = c_void_p
msvcrt.memcpy.argtypes = [c_void_p, c_void_p, c_size_t]
msvcrt.memcpy.restype = c_void_p
def PyObject_Malloc(size: int) -> c_void_p: ...
def memcpy(dest: c_void_p, src: c_void_p, n: int) -> c_void_p: ...
PyObject_Malloc = pythonapi.PyObject_Malloc
memcpy = msvcrt.memcpy
import sys
def CopyType(type: type) -> type:
size: int = sys.getsizeof(type)
new_type = PyObject_Malloc(size)
memcpy(new_type, id(type), size)
py_new_type = cast(new_type, py_object).value
return py_new_type
def CopyTypeIntoType(type: type, new_type: type):
memcpy(id(type), id(new_type), sys.getsizeof(type))
def ReloadType(type: type) -> bool:
t_type: IPointer[PyTypeObject] = PyType_CAST(type)
t_type.contents.ready = False
return PyType_Ready(t_type) == 0
CArgObject = type(byref(c_int()))
t_PyCArgObject = cast(id(CArgObject), PyTypeObject_PTR)
class PyMemberDef_Iterator:
_members: IPointer[PyMemberDef]
_member: PyMemberDef
_index: int
def __init__(self, tp_members: IPointer[PyMemberDef]):
if tp_members:
self._member = tp_members[0]
self._index = 1
self._members = tp_members
def __iter__(self):
return self
def __next__(self) -> PyMemberDef:
if not self._members:
raise StopIteration
member = self._member
if not member.name:
raise StopIteration
self._index += 1
self._member = self._members[self._index]
return member
def Get_PyMemberDef(typ: type, name: AnyStr) -> PyMemberDef:
if isinstance(name, str):
name = name.encode()
for member in PyType_CAST_DEREF(typ).GetMemberIter():
if member.name == name: return member
return None
def Get_Members(typ: type) -> IPointer[PyMemberDef]:
tp_members = PyType_CAST_DEREF(typ).tp_members
if not tp_members: return None
return tp_members
def SetTPFLAG(typ: type, tp_flag: int):
t_typ = PyType_CAST_DEREF(typ)
if t_typ.tp_flags & tp_flag: return
t_typ.tp_flags |= tp_flag
def RemoveTPFLAG(typ: type, tp_flag: int):
t_typ = PyType_CAST_DEREF(typ)
if not t_typ.tp_flags & tp_flag: return
t_typ.tp_flags &= (~tp_flag)
def HasTPFLAG(typ: type, tp_flag: int) -> bool:
return (PyType_CAST_DEREF(typ).tp_flags & tp_flag) != 0
def EnumTPFLAGS(typ: type) -> Sequence[str]:
return PyType_CAST_DEREF(typ).EnumTPFLAGS()
def PyType_CAST(typ: type[_CWT]) -> IPointer[PyTypeObject[_CWT]]:
return cast(id(typ), PyTypeObject_PTR)
def PyObject_CAST(obj: _CWT) -> IPointer[PyObject[_CWT]]:
return cast(id(obj), PyObject_PTR)
def PyType_CAST_DEREF(typ: type[_CWT]) -> PyTypeObject[_CWT]:
return cast(id(typ), PyTypeObject_PTR).contents
def PyObject_CAST_DEREF(obj: _CWT) -> PyObject[_CWT]:
return cast(id(obj), PyObject_PTR).contents
def To_PyObject(obj: IPointer[PyObject[_CWT]]) -> _CWT:
return obj.contents.object
def To_PyType(typ: IPointer[PyTypeObject[_CWT]]) -> type[_CWT]:
return typ.contents.type
def To_PyObject_DEREFERENCED(obj: PyObject[_CWT]) -> _CWT:
return cast(byref(obj), py_object).value
def To_PyType_DEREFERENCED(typ: PyTypeObject[_CWT]) -> type[_CWT]:
return cast(byref(typ), py_object).value
def offsetof(typ: type[Structure], field: str):
return getattr(getattr(typ, field), 'offset')
mappingproxy = type(type.__dict__)
def Init():
SetTPFLAG(CArgObject, Py_TPFLAGS_BASETYPE)
SetTPFLAG(bool, Py_TPFLAGS_BASETYPE)
Get_PyMemberDef(CArgObject, '_obj').readonly = False
t_mappingproxy = PyType_CAST_DEREF(mappingproxy)
t_mappingproxy.members = [PyMemberDef('m_proxy', _Py_T_OBJECT,
MProxy.offset('mapping'))]
t_mappingproxy.Reload()
t_codetype = PyType_CAST_DEREF(type(HasTPFLAG.__code__))
for member in t_codetype.members:
member.readonly = False
#t_frametype = PyType_CAST_DEREF(type(sys._getframe()))
#for member in t_frametype.members:
# member.readonly = False
"""