summaryrefslogtreecommitdiffstats
path: root/Objects/typeslots.py
blob: 8ab05f91be12b0db4249347c1264680a4d15e1f1 (plain)
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()