Turns the name, signature, code in functions into complete functions and lists them in a methods_table. Then turns the methods_table into a ``PyMethodDef`` structure and returns the resulting code fragment ready for compilation
(functions, modname)
| 127 | |
| 128 | |
| 129 | def _make_methods(functions, modname): |
| 130 | """ Turns the name, signature, code in functions into complete functions |
| 131 | and lists them in a methods_table. Then turns the methods_table into a |
| 132 | ``PyMethodDef`` structure and returns the resulting code fragment ready |
| 133 | for compilation |
| 134 | """ |
| 135 | methods_table = [] |
| 136 | codes = [] |
| 137 | for funcname, flags, code in functions: |
| 138 | cfuncname = f"{modname}_{funcname}" |
| 139 | if 'METH_KEYWORDS' in flags: |
| 140 | signature = '(PyObject *self, PyObject *args, PyObject *kwargs)' |
| 141 | else: |
| 142 | signature = '(PyObject *self, PyObject *args)' |
| 143 | methods_table.append(f'{{"{funcname}", (PyCFunction){cfuncname}, {flags}}},') |
| 144 | func_code = f""" |
| 145 | static PyObject* {cfuncname}{signature} |
| 146 | {{ |
| 147 | {code} |
| 148 | }} |
| 149 | """ |
| 150 | codes.append(func_code) |
| 151 | |
| 152 | methods_str = '\n'.join(methods_table) |
| 153 | body = "\n".join(codes) + f""" |
| 154 | static PyMethodDef methods[] = {{ |
| 155 | {methods_str} |
| 156 | {{ NULL }} |
| 157 | }}; |
| 158 | static struct PyModuleDef moduledef = {{ |
| 159 | PyModuleDef_HEAD_INIT, |
| 160 | "{modname}", /* m_name */ |
| 161 | NULL, /* m_doc */ |
| 162 | -1, /* m_size */ |
| 163 | methods, /* m_methods */ |
| 164 | }}; |
| 165 | """ |
| 166 | return body |
| 167 | |
| 168 | |
| 169 | def _make_source(name, init, body): |
no test coverage detected
searching dependent graphs…