From 086527bcc26d277ce1bc8a94c8e42b93340e850d Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Sun, 12 Nov 2000 18:10:46 +0000 Subject: [PATCH] Added call() function so that a regular python function (as opposed to method or other function-as-attribute) can be called. Added newlines for readability. [SVN r8172] --- callback.h | 191 +++++++++++++++++++++++++++++++++++++++++++++--- gen_callback.py | 17 ++++- 2 files changed, 195 insertions(+), 13 deletions(-) diff --git a/callback.h b/callback.h index 8142a0e6..a47b173a 100644 --- a/callback.h +++ b/callback.h @@ -24,27 +24,110 @@ template struct Callback { static R call_method(PyObject* self, const char* name) - { return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("()"))), Type()); } + { + return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("()"))), Type()); + } template static R call_method(PyObject* self, const char* name, const A1& a1) - { return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(N)"), to_python(a1))), Type()); } + { + return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(N)"), + to_python(a1))), Type()); + } template static R call_method(PyObject* self, const char* name, const A1& a1, const A2& a2) - { return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(NN)"), to_python(a1), to_python(a2))), Type()); } + { + return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(NN)"), + to_python(a1), + to_python(a2))), Type()); + } template static R call_method(PyObject* self, const char* name, const A1& a1, const A2& a2, const A3& a3) - { return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(NNN)"), to_python(a1), to_python(a2), to_python(a3))), Type()); } + { + return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(NNN)"), + to_python(a1), + to_python(a2), + to_python(a3))), Type()); + } template static R call_method(PyObject* self, const char* name, const A1& a1, const A2& a2, const A3& a3, const A4& a4) - { return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(NNNN)"), to_python(a1), to_python(a2), to_python(a3), to_python(a4))), Type()); } + { + return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(NNNN)"), + to_python(a1), + to_python(a2), + to_python(a3), + to_python(a4))), Type()); + } template static R call_method(PyObject* self, const char* name, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5) - { return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(NNNNN)"), to_python(a1), to_python(a2), to_python(a3), to_python(a4), to_python(a5))), Type()); } + { + return from_python(expect_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(NNNNN)"), + to_python(a1), + to_python(a2), + to_python(a3), + to_python(a4), + to_python(a5))), Type()); + } + + static R call(PyObject* self) + { + return from_python(expect_non_null(PyEval_CallFunction(self, const_cast("()"))), Type()); + } + + template + static R call(PyObject* self, const A1& a1) + { + return from_python(expect_non_null(PyEval_CallFunction(self, const_cast("(N)"), + to_python(a1))), Type()); + } + + template + static R call(PyObject* self, const A1& a1, const A2& a2) + { + return from_python(expect_non_null(PyEval_CallFunction(self, const_cast("(NN)"), + to_python(a1), + to_python(a2))), Type()); + } + + template + static R call(PyObject* self, const A1& a1, const A2& a2, const A3& a3) + { + return from_python(expect_non_null(PyEval_CallFunction(self, const_cast("(NNN)"), + to_python(a1), + to_python(a2), + to_python(a3))), Type()); + } + + template + static R call(PyObject* self, const A1& a1, const A2& a2, const A3& a3, const A4& a4) + { + return from_python(expect_non_null(PyEval_CallFunction(self, const_cast("(NNNN)"), + to_python(a1), + to_python(a2), + to_python(a3), + to_python(a4))), Type()); + } + + template + static R call(PyObject* self, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5) + { + return from_python(expect_non_null(PyEval_CallFunction(self, const_cast("(NNNNN)"), + to_python(a1), + to_python(a2), + to_python(a3), + to_python(a4), + to_python(a5))), Type()); + } }; @@ -55,30 +138,114 @@ template <> struct Callback { static void call_method(PyObject* self, const char* name) - { expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("()"))); } + { + expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("()"))); + } template static void call_method(PyObject* self, const char* name, const A1& a1) - { expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(N)"), to_python(a1))); } + { + expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(N)"), + to_python(a1))); + } template static void call_method(PyObject* self, const char* name, const A1& a1, const A2& a2) - { expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(NN)"), to_python(a1), to_python(a2))); } + { + expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(NN)"), + to_python(a1), + to_python(a2))); + } template static void call_method(PyObject* self, const char* name, const A1& a1, const A2& a2, const A3& a3) - { expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(NNN)"), to_python(a1), to_python(a2), to_python(a3))); } + { + expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(NNN)"), + to_python(a1), + to_python(a2), + to_python(a3))); + } template static void call_method(PyObject* self, const char* name, const A1& a1, const A2& a2, const A3& a3, const A4& a4) - { expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(NNNN)"), to_python(a1), to_python(a2), to_python(a3), to_python(a4))); } + { + expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(NNNN)"), + to_python(a1), + to_python(a2), + to_python(a3), + to_python(a4))); + } template static void call_method(PyObject* self, const char* name, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5) - { expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), const_cast("(NNNNN)"), to_python(a1), to_python(a2), to_python(a3), to_python(a4), to_python(a5))); } + { + expect_and_absorb_non_null(PyEval_CallMethod(self, const_cast(name), + const_cast("(NNNNN)"), + to_python(a1), + to_python(a2), + to_python(a3), + to_python(a4), + to_python(a5))); + } + + static void call(PyObject* self) + { + expect_and_absorb_non_null(PyEval_CallFunction(self, const_cast("()"))); + } + + template + static void call(PyObject* self, const A1& a1) + { + expect_and_absorb_non_null(PyEval_CallFunction(self, const_cast("(N)"), + to_python(a1))); + } + + template + static void call(PyObject* self, const A1& a1, const A2& a2) + { + expect_and_absorb_non_null(PyEval_CallFunction(self, const_cast("(NN)"), + to_python(a1), + to_python(a2))); + } + + template + static void call(PyObject* self, const A1& a1, const A2& a2, const A3& a3) + { + expect_and_absorb_non_null(PyEval_CallFunction(self, const_cast("(NNN)"), + to_python(a1), + to_python(a2), + to_python(a3))); + } + + template + static void call(PyObject* self, const A1& a1, const A2& a2, const A3& a3, const A4& a4) + { + expect_and_absorb_non_null(PyEval_CallFunction(self, const_cast("(NNNN)"), + to_python(a1), + to_python(a2), + to_python(a3), + to_python(a4))); + } + + template + static void call(PyObject* self, const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5) + { + expect_and_absorb_non_null(PyEval_CallFunction(self, const_cast("(NNNNN)"), + to_python(a1), + to_python(a2), + to_python(a3), + to_python(a4), + to_python(a5))); + } }; } // namespace py #endif // CALLBACK_DWA_052100_H_ + diff --git a/gen_callback.py b/gen_callback.py index 44aa6a60..3007c4fb 100644 --- a/gen_callback.py +++ b/gen_callback.py @@ -5,7 +5,20 @@ def gen_callback(args): # A template for the call_method function which we're going to generate call_method = '''%{ template <%(class A%n%:, %)> %} static %1 call_method(PyObject* self, const char* name%(, const A%n& a%n%)) - { %2PyEval_CallMethod(self, const_cast(name), const_cast("(%(N%))")%(, to_python(a%n)%))%3; } + { + %2PyEval_CallMethod(self, const_cast(name), + const_cast("(%(N%))")%(, + to_python(a%n)%))%3; + } + +''' + + call_function = '''%{ template <%(class A%n%:, %)> +%} static %1 call(PyObject* self%(, const A%n& a%n%)) + { + %2PyEval_CallFunction(self, const_cast("(%(N%))")%(, + to_python(a%n)%))%3; + } ''' non_void = ('R', 'return from_python(expect_non_null(', '), Type())') @@ -39,6 +52,7 @@ struct Callback { """ % args + gen_functions(call_method, args, 'R', 'return from_python(expect_non_null(', '), Type())') + + gen_functions(call_function, args, 'R', 'return from_python(expect_non_null(', '), Type())') + """}; @@ -50,6 +64,7 @@ struct Callback { """ + gen_functions(call_method, args, 'void', 'expect_and_absorb_non_null(', ')') + + gen_functions(call_function, args, 'void', 'expect_and_absorb_non_null(', ')') + """};