Author: Ronan Lamy <ronan.l...@gmail.com> Branch: test-cpyext Changeset: r86964:8ed4d2de2eed Date: 2016-09-08 17:57 +0100 http://bitbucket.org/pypy/pypy/changeset/8ed4d2de2eed/
Log: Simplify import_extension() by extracting make_methods() diff --git a/pypy/module/cpyext/test/test_cpyext.py b/pypy/module/cpyext/test/test_cpyext.py --- a/pypy/module/cpyext/test/test_cpyext.py +++ b/pypy/module/cpyext/test/test_cpyext.py @@ -136,6 +136,28 @@ link_extra=link_extra, ext=get_so_suffix()) +def make_methods(functions, modname): + methods_table = [] + codes = [] + for funcname, flags, code in functions: + cfuncname = "%s_%s" % (modname, funcname) + methods_table.append( + "{\"%s\", %s, %s}," % (funcname, cfuncname, flags)) + func_code = """ + static PyObject* %s(PyObject* self, PyObject* args) + { + %s + } + """ % (cfuncname, code) + codes.append(func_code) + + body = "\n".join(codes) + """ + static PyMethodDef methods[] = { + %s + { NULL } + }; + """ % ('\n'.join(methods_table),) + return body def freeze_refcnts(self): rawrefcount._dont_free_any_more() @@ -402,26 +424,7 @@ def import_extension(space, modname, w_functions, prologue="", w_include_dirs=None, more_init="", PY_SSIZE_T_CLEAN=False): functions = space.unwrap(w_functions) - methods_table = [] - codes = [] - for funcname, flags, code in functions: - cfuncname = "%s_%s" % (modname, funcname) - methods_table.append("{\"%s\", %s, %s}," % - (funcname, cfuncname, flags)) - func_code = """ - static PyObject* %s(PyObject* self, PyObject* args) - { - %s - } - """ % (cfuncname, code) - codes.append(func_code) - - body = prologue + "\n".join(codes) + """ - static PyMethodDef methods[] = { - %s - { NULL } - }; - """ % ('\n'.join(methods_table),) + body = prologue + make_methods(functions, modname) init = """Py_InitModule("%s", methods);""" % (modname,) if more_init: init += more_init _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit