From 223e5ed0c7e5076407913b090e67aa2ab3571ea9 Mon Sep 17 00:00:00 2001 From: David Howells Date: Thu, 9 Jan 2014 14:41:04 +0000 Subject: [PATCH] rxgen: Turn the py module emitter into a perl subroutine Turn the python module defining code emitter into a perl subroutine within the main rxgen script. Signed-off-by: David Howells --- rxgen/rxgen.pl | 150 ++++++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/rxgen/rxgen.pl b/rxgen/rxgen.pl index 54f617b..c5e00c8 100755 --- a/rxgen/rxgen.pl +++ b/rxgen/rxgen.pl @@ -581,96 +581,96 @@ foreach $func (sort keys %funcs) { ############################################################################### # -# Emit python structure wrapper static method table +# Emit python module definition. # ############################################################################### +sub emit_py_module() { + # Emit python structure wrapper static method table + print PYOUT "\n"; + print PYOUT "/*\n"; + print PYOUT " * The static methods.\n"; + print PYOUT " */\n"; + print PYOUT "static PyMethodDef module_methods[] = {\n"; -print PYOUT "\n"; -print PYOUT "/*\n"; -print PYOUT " * The static methods.\n"; -print PYOUT " */\n"; -print PYOUT "static PyMethodDef module_methods[] = {\n"; + foreach my $s (@structs) { + my $struct = @{$s}[0]; + print PYOUT "\t{\"new_$struct\", (PyCFunction)kafs_new_py_$struct, METH_NOARGS,\n"; + print PYOUT "\t \"Create a new $struct record.\"\n"; + print PYOUT "\t},\n"; + } -foreach my $s (@structs) { - my $struct = @{$s}[0]; - print PYOUT "\t{\"new_$struct\", (PyCFunction)kafs_new_py_$struct, METH_NOARGS,\n"; - print PYOUT "\t \"Create a new $struct record.\"\n"; - print PYOUT "\t},\n"; -} + foreach $func (sort keys %funcs) { + my @params = @{$funcs{$func}}; + print PYOUT "\t{\"$func\", (PyCFunction)kafs_$func, METH_VARARGS, \"\" },\n"; + } -foreach $func (sort keys %funcs) { - my @params = @{$funcs{$func}}; - print PYOUT "\t{\"$func\", (PyCFunction)kafs_$func, METH_VARARGS, \"\" },\n"; -} + print PYOUT "\t{\"rx_new_connection\", (PyCFunction)kafs_py_rx_new_connection, METH_VARARGS,\n"; + print PYOUT "\t\"\" },\n"; -print PYOUT "\t{\"rx_new_connection\", (PyCFunction)kafs_py_rx_new_connection, METH_VARARGS,\n"; -print PYOUT "\t\"\" },\n"; + print PYOUT "\t{}\n"; + print PYOUT "};\n"; -print PYOUT "\t{}\n"; -print PYOUT "};\n"; + # Emit python structure wrapper loader + print PYOUT "\n"; -############################################################################### -# -# Emit python structure wrapper loader -# -############################################################################### -print PYOUT "\n"; - -print PYOUT "static PyModuleDef kafs_module = {\n"; -print PYOUT "\t.m_base = PyModuleDef_HEAD_INIT,\n"; -print PYOUT "\t.m_name = \"kafs\",\n"; -print PYOUT "\t.m_doc = \"AFS stuff.\",\n"; -print PYOUT "\t.m_size = -1,\n"; -print PYOUT "\t.m_methods = module_methods,\n"; -print PYOUT "};\n"; - -print PYHDR "\n"; -print PYHDR "extern PyObject *pykafs_load_wrappers(void);\n"; - -print PYOUT "\n"; -print PYOUT "PyObject *pykafs_load_wrappers(void)\n"; -print PYOUT "{\n"; -print PYOUT "\tPyObject *m;\n"; - -# Load types -if (@structs) { - print PYOUT "\tif ("; - print PYOUT "PyType_Ready(&py_rx_connectionType) < 0"; - my $first = 0; - foreach my $s (@structs) { - my @members = @{$s}; - my $struct = $members[0]; - print PYOUT " ||\n\t " unless ($first); - print PYOUT "PyType_Ready(&py_", $struct, "Type) < 0"; - $first = 0; - } - print PYOUT ")\n"; - print PYOUT "\t\treturn NULL;\n"; -} + print PYOUT "static PyModuleDef kafs_module = {\n"; + print PYOUT "\t.m_base = PyModuleDef_HEAD_INIT,\n"; + print PYOUT "\t.m_name = \"kafs\",\n"; + print PYOUT "\t.m_doc = \"AFS stuff.\",\n"; + print PYOUT "\t.m_size = -1,\n"; + print PYOUT "\t.m_methods = module_methods,\n"; + print PYOUT "};\n"; -print PYOUT "\n"; -print PYOUT "\tm = PyModule_Create(&kafs_module);\n"; -print PYOUT "\tif (!m)\n"; -print PYOUT "\t\treturn NULL;\n"; + print PYHDR "\n"; + print PYHDR "extern PyObject *pykafs_load_wrappers(void);\n"; -if (%constants) { print PYOUT "\n"; - foreach my $c (sort keys %constants) { - print PYOUT "\tPyModule_AddIntConstant(m, \"$c\", $c);\n"; + print PYOUT "PyObject *pykafs_load_wrappers(void)\n"; + print PYOUT "{\n"; + print PYOUT "\tPyObject *m;\n"; + + # Load types + if (@structs) { + print PYOUT "\tif ("; + print PYOUT "PyType_Ready(&py_rx_connectionType) < 0"; + my $first = 0; + foreach my $s (@structs) { + my @members = @{$s}; + my $struct = $members[0]; + print PYOUT " ||\n\t " unless ($first); + print PYOUT "PyType_Ready(&py_", $struct, "Type) < 0"; + $first = 0; + } + print PYOUT ")\n"; + print PYOUT "\t\treturn NULL;\n"; } -} -if (@structs) { print PYOUT "\n"; - foreach my $s (@structs) { - my @members = @{$s}; - my $struct = $members[0]; - print PYOUT "\tPy_INCREF(&py_", $struct, "Type);\n"; - print PYOUT "\tPyModule_AddObject(m, \"$struct\", (PyObject *)&py_", $struct, "Type);\n"; + print PYOUT "\tm = PyModule_Create(&kafs_module);\n"; + print PYOUT "\tif (!m)\n"; + print PYOUT "\t\treturn NULL;\n"; + + if (%constants) { + print PYOUT "\n"; + foreach my $c (sort keys %constants) { + print PYOUT "\tPyModule_AddIntConstant(m, \"$c\", $c);\n"; + } } - print PYOUT "\n"; - print PYOUT "\treturn m;\n"; + if (@structs) { + print PYOUT "\n"; + foreach my $s (@structs) { + my @members = @{$s}; + my $struct = $members[0]; + print PYOUT "\tPy_INCREF(&py_", $struct, "Type);\n"; + print PYOUT "\tPyModule_AddObject(m, \"$struct\", (PyObject *)&py_", $struct, "Type);\n"; + } + + print PYOUT "\n"; + print PYOUT "\treturn m;\n"; + } + + print PYOUT "}\n"; } -print PYOUT "}\n"; +emit_py_module(); -- 2.49.0