2010-12-03 21:14:31 +01:00
|
|
|
#!/usr/bin/python
|
2016-07-26 12:48:08 +02:00
|
|
|
# Usage: typeslots.py < Include/typeslots.h typeslots.inc
|
2010-12-03 21:14:31 +01:00
|
|
|
|
|
|
|
import sys, re
|
|
|
|
|
2020-11-10 21:53:46 +01:00
|
|
|
|
2016-07-26 12:48:08 +02:00
|
|
|
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
|
2020-11-10 21:53:46 +01:00
|
|
|
|
2016-07-26 12:48:08 +02:00
|
|
|
member = m.group(1)
|
|
|
|
if member.startswith("tp_"):
|
2020-11-10 21:53:46 +01:00
|
|
|
member = f'{{-1, offsetof(PyTypeObject, {member})}}'
|
2016-07-26 12:48:08 +02:00
|
|
|
elif member.startswith("am_"):
|
2020-11-10 21:53:46 +01:00
|
|
|
member = (f'{{offsetof(PyAsyncMethods, {member}),'+
|
|
|
|
' offsetof(PyTypeObject, tp_as_async)}')
|
2016-07-26 12:48:08 +02:00
|
|
|
elif member.startswith("nb_"):
|
2020-11-10 21:53:46 +01:00
|
|
|
member = (f'{{offsetof(PyNumberMethods, {member}),'+
|
|
|
|
' offsetof(PyTypeObject, tp_as_number)}')
|
2016-07-26 12:48:08 +02:00
|
|
|
elif member.startswith("mp_"):
|
2020-11-10 21:53:46 +01:00
|
|
|
member = (f'{{offsetof(PyMappingMethods, {member}),'+
|
|
|
|
' offsetof(PyTypeObject, tp_as_mapping)}')
|
2016-07-26 12:48:08 +02:00
|
|
|
elif member.startswith("sq_"):
|
2020-11-10 21:53:46 +01:00
|
|
|
member = (f'{{offsetof(PySequenceMethods, {member}),'+
|
|
|
|
' offsetof(PyTypeObject, tp_as_sequence)}')
|
2016-07-26 12:48:08 +02:00
|
|
|
elif member.startswith("bf_"):
|
2020-11-10 21:53:46 +01:00
|
|
|
member = (f'{{offsetof(PyBufferProcs, {member}),'+
|
|
|
|
' offsetof(PyTypeObject, tp_as_buffer)}')
|
2016-07-26 12:48:08 +02:00
|
|
|
res[int(m.group(2))] = member
|
2010-12-03 21:14:31 +01:00
|
|
|
|
2016-07-26 12:48:08 +02:00
|
|
|
M = max(res.keys())+1
|
|
|
|
for i in range(1,M):
|
|
|
|
if i in res:
|
2020-11-10 21:53:46 +01:00
|
|
|
out.write("%s,\n" % res[i])
|
2016-07-26 12:48:08 +02:00
|
|
|
else:
|
2020-11-10 21:53:46 +01:00
|
|
|
out.write("{0, 0},\n")
|
|
|
|
|
2016-07-26 12:48:08 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) == 2:
|
|
|
|
with open(sys.argv[1], "w") as f:
|
|
|
|
generate_typeslots(f)
|
2011-01-06 20:26:21 +01:00
|
|
|
else:
|
2016-07-26 12:48:08 +02:00
|
|
|
generate_typeslots()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|