1#!/usr/bin/python 2# Usage: typeslots.py < Include/typeslots.h typeslots.inc 3 4import sys, re 5 6def generate_typeslots(out=sys.stdout): 7 out.write("/* Generated by typeslots.py */\n") 8 res = {} 9 for line in sys.stdin: 10 m = re.match("#define Py_([a-z_]+) ([0-9]+)", line) 11 if not m: 12 continue 13 member = m.group(1) 14 if member.startswith("tp_"): 15 member = "ht_type."+member 16 elif member.startswith("am_"): 17 member = "as_async."+member 18 elif member.startswith("nb_"): 19 member = "as_number."+member 20 elif member.startswith("mp_"): 21 member = "as_mapping."+member 22 elif member.startswith("sq_"): 23 member = "as_sequence."+member 24 elif member.startswith("bf_"): 25 member = "as_buffer."+member 26 res[int(m.group(2))] = member 27 28 M = max(res.keys())+1 29 for i in range(1,M): 30 if i in res: 31 out.write("offsetof(PyHeapTypeObject, %s),\n" % res[i]) 32 else: 33 out.write("0,\n") 34 35def main(): 36 if len(sys.argv) == 2: 37 with open(sys.argv[1], "w") as f: 38 generate_typeslots(f) 39 else: 40 generate_typeslots() 41 42if __name__ == "__main__": 43 main() 44