From f0dc13a789da91dcf90cdfabd8081939cba61f5d Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Fri, 1 Jul 2016 16:35:20 +0200 Subject: [PATCH] python interface: add type check to __str__ methods Currently, all classes have a __str__ method, but the next commit makes this optional. It will then be possible for a subclass to not have a __str__ method while its superclass does have one. Calling the method on an object of the subclass will then end up calling the method of the superclass. This method therefore needs to check that its argument is of the correct type before calling the corresponding *_to_str function. Signed-off-by: Sven Verdoolaege --- interface/python.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/interface/python.cc b/interface/python.cc index 038a1ecd..dc991599 100644 --- a/interface/python.cc +++ b/interface/python.cc @@ -778,11 +778,15 @@ void isl_class::print_method_type(FunctionDecl *fd) * pretty print the class (e.g., when calling print(obj)) and uses __repr__ * when printing a precise representation of an object (e.g., when dumping it * in the REPL console). + * + * Check the type of the argument before calling the *_to_str function + * on it in case the method was called on an object from a subclass. */ void isl_class::print_representation(const string &python_name) { - printf(" def __str__(self):\n"); - printf(" ptr = isl.%s_to_str(self.ptr)\n", name.c_str()); + printf(" def __str__(arg0):\n"); + print_type_check(python_name, 0, false, "", "", -1); + printf(" ptr = isl.%s_to_str(arg0.ptr)\n", name.c_str()); printf(" res = str(cast(ptr, c_char_p).value)\n"); printf(" libc.free(ptr)\n"); printf(" return res\n"); -- 2.11.4.GIT