aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/gen_py3_reg.py
diff options
context:
space:
mode:
authoralexv-smirnov <alex@ydb.tech>2023-06-13 11:05:01 +0300
committeralexv-smirnov <alex@ydb.tech>2023-06-13 11:05:01 +0300
commitbf0f13dd39ee3e65092ba3572bb5b1fcd125dcd0 (patch)
tree1d1df72c0541a59a81439842f46d95396d3e7189 /build/scripts/gen_py3_reg.py
parent8bfdfa9a9bd19bddbc58d888e180fbd1218681be (diff)
downloadydb-bf0f13dd39ee3e65092ba3572bb5b1fcd125dcd0.tar.gz
add ymake export to ydb
Diffstat (limited to 'build/scripts/gen_py3_reg.py')
-rw-r--r--build/scripts/gen_py3_reg.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/build/scripts/gen_py3_reg.py b/build/scripts/gen_py3_reg.py
new file mode 100644
index 0000000000..149c094898
--- /dev/null
+++ b/build/scripts/gen_py3_reg.py
@@ -0,0 +1,34 @@
+import sys
+
+template = '''
+struct PyObject;
+extern "C" int PyImport_AppendInittab(const char* name, PyObject* (*initfunc)());
+extern "C" PyObject* {1}();
+
+namespace {
+ struct TRegistrar {
+ inline TRegistrar() {
+ // TODO Collect all modules and call PyImport_ExtendInittab once
+ PyImport_AppendInittab("{0}", {1});
+ }
+ } REG;
+}
+'''
+
+
+def mangle(name):
+ if '.' not in name:
+ return name
+ return ''.join('{}{}'.format(len(s), s) for s in name.split('.'))
+
+if __name__ == '__main__':
+ if len(sys.argv) != 3:
+ print >>sys.stderr, 'Usage: <path/to/gen_py_reg.py> <python_module_name> <output_file>'
+ print >>sys.stderr, 'Passed: ' + ' '.join(sys.argv)
+ sys.exit(1)
+
+ with open(sys.argv[2], 'w') as f:
+ modname = sys.argv[1]
+ initname = 'PyInit_' + mangle(modname)
+ code = template.replace('{0}', modname).replace('{1}', initname)
+ f.write(code)