File: typeslots.py

package info (click to toggle)
python3.13 3.13.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,328 kB
  • sloc: python: 704,014; ansic: 653,914; xml: 31,250; sh: 5,844; cpp: 4,326; makefile: 1,981; objc: 787; lisp: 502; javascript: 213; asm: 75; csh: 12
file content (51 lines) | stat: -rwxr-xr-x 1,692 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python
# Usage: typeslots.py < Include/typeslots.h typeslots.inc

import sys, re


def generate_typeslots(out=sys.stdout):
    out.write("/* Generated by typeslots.py */\n")
    res = {}
    for line in sys.stdin:
        m = re.match("#define Py_([a-z_]+) ([0-9]+)", line)
        if not m:
            continue

        member = m.group(1)
        if member.startswith("tp_"):
            member = f'{{-1, offsetof(PyTypeObject, {member})}}'
        elif member.startswith("am_"):
            member = (f'{{offsetof(PyAsyncMethods, {member}),'+
                      ' offsetof(PyTypeObject, tp_as_async)}')
        elif member.startswith("nb_"):
            member = (f'{{offsetof(PyNumberMethods, {member}),'+
                      ' offsetof(PyTypeObject, tp_as_number)}')
        elif member.startswith("mp_"):
            member = (f'{{offsetof(PyMappingMethods, {member}),'+
                      ' offsetof(PyTypeObject, tp_as_mapping)}')
        elif member.startswith("sq_"):
            member = (f'{{offsetof(PySequenceMethods, {member}),'+
                      ' offsetof(PyTypeObject, tp_as_sequence)}')
        elif member.startswith("bf_"):
            member = (f'{{offsetof(PyBufferProcs, {member}),'+
                      ' offsetof(PyTypeObject, tp_as_buffer)}')
        res[int(m.group(2))] = member

    M = max(res.keys())+1
    for i in range(1,M):
        if i in res:
            out.write("%s,\n" % res[i])
        else:
            out.write("{0, 0},\n")


def main():
    if len(sys.argv) == 2:
        with open(sys.argv[1], "w") as f:
            generate_typeslots(f)
    else:
        generate_typeslots()

if __name__ == "__main__":
    main()