• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2# Usage: typeslots.py < Include/typeslots.h typeslots.inc
3
4import sys, re
5
6
7def generate_typeslots(out=sys.stdout):
8    out.write("/* Generated by typeslots.py */\n")
9    res = {}
10    for line in sys.stdin:
11        m = re.match("#define Py_([a-z_]+) ([0-9]+)", line)
12        if not m:
13            continue
14
15        member = m.group(1)
16        if member.startswith("tp_"):
17            member = f'{{-1, offsetof(PyTypeObject, {member})}}'
18        elif member.startswith("am_"):
19            member = (f'{{offsetof(PyAsyncMethods, {member}),'+
20                      ' offsetof(PyTypeObject, tp_as_async)}')
21        elif member.startswith("nb_"):
22            member = (f'{{offsetof(PyNumberMethods, {member}),'+
23                      ' offsetof(PyTypeObject, tp_as_number)}')
24        elif member.startswith("mp_"):
25            member = (f'{{offsetof(PyMappingMethods, {member}),'+
26                      ' offsetof(PyTypeObject, tp_as_mapping)}')
27        elif member.startswith("sq_"):
28            member = (f'{{offsetof(PySequenceMethods, {member}),'+
29                      ' offsetof(PyTypeObject, tp_as_sequence)}')
30        elif member.startswith("bf_"):
31            member = (f'{{offsetof(PyBufferProcs, {member}),'+
32                      ' offsetof(PyTypeObject, tp_as_buffer)}')
33        res[int(m.group(2))] = member
34
35    M = max(res.keys())+1
36    for i in range(1,M):
37        if i in res:
38            out.write("%s,\n" % res[i])
39        else:
40            out.write("{0, 0},\n")
41
42
43def main():
44    if len(sys.argv) == 2:
45        with open(sys.argv[1], "w") as f:
46            generate_typeslots(f)
47    else:
48        generate_typeslots()
49
50if __name__ == "__main__":
51    main()
52