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