From a15135f1c19bb403256fe07de8c6dc426afa1484 Mon Sep 17 00:00:00 2001 From: Bruno da Silva de Oliveira Date: Mon, 14 Apr 2003 23:34:33 +0000 Subject: [PATCH] - Fixed a inheritance bug, and added a test for it. [SVN r18251] --- pyste/example/basic.h | 6 ++++++ pyste/example/inherit.h | 18 ++++++++++++++++++ pyste/example/inherit.pyste | 8 ++++++++ pyste/src/ClassExporter.py | 14 +++++++++++--- pyste/src/declarations.py | 19 ++++++++++++------- pyste/src/pyste.py | 2 +- pyste/tests/example_basicUT.py | 8 ++++++++ pyste/tests/example_inheritUT.py | 20 ++++++++++++++++++++ pyste/tests/nt_build_all.bat | 1 + pyste/tests/nt_clean.bat | 1 + 10 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 pyste/example/inherit.h create mode 100644 pyste/example/inherit.pyste create mode 100644 pyste/tests/example_inheritUT.py diff --git a/pyste/example/basic.h b/pyste/example/basic.h index 16469e95..5ab6ca58 100644 --- a/pyste/example/basic.h +++ b/pyste/example/basic.h @@ -8,6 +8,7 @@ namespace basic { struct C { + // test virtuallity C(): value(1), const_value(0) {} virtual int f(int x = 10) { @@ -21,11 +22,16 @@ struct C const std::string& name() { return _name; } void set_name(const std::string& name) { _name = name; } std::string _name; + + // test data members static int static_value; static const int const_static_value; int value; const int const_value; + + // test static functions + static int mul(int x=2, int y=3) { return x*y; } }; inline int call_f(C& c) diff --git a/pyste/example/inherit.h b/pyste/example/inherit.h new file mode 100644 index 00000000..04caddae --- /dev/null +++ b/pyste/example/inherit.h @@ -0,0 +1,18 @@ +template +class A +{ +public: + void set(T v) { mData = v; } + + T get() const { return mData; } + +private: + T mData; +}; + + +class B : public A +{ +public: + int go() { return get(); } +}; diff --git a/pyste/example/inherit.pyste b/pyste/example/inherit.pyste new file mode 100644 index 00000000..2531ba83 --- /dev/null +++ b/pyste/example/inherit.pyste @@ -0,0 +1,8 @@ +# Doesn't work: +A = Template('A', 'inherit.h') +A_int = A('int') + +Class('B', 'inherit.h') + +# Does work: +#AllFromHeader('inherit.h') diff --git a/pyste/src/ClassExporter.py b/pyste/src/ClassExporter.py index a6c3df6b..56f89313 100644 --- a/pyste/src/ClassExporter.py +++ b/pyste/src/ClassExporter.py @@ -54,6 +54,10 @@ class ClassExporter(Exporter): return makeid(self.class_.name) + def Name(self): + return self.class_.FullName() + + def SetDeclarations(self, declarations): Exporter.SetDeclarations(self, declarations) decl = self.GetDeclaration(self.info.name) @@ -269,9 +273,13 @@ class ClassExporter(Exporter): def DeclareOverloads(m): 'Declares the macro for the generation of the overloads' if not m.virtual: - func = m.name - code = 'BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(%s, %s, %i, %i)\n' - code = code % (OverloadName(m), func, m.minArgs, m.maxArgs) + if m.static: + func = m.FullName() + macro = 'BOOST_PYTHON_FUNCTION_OVERLOADS' + else: + func = m.name + macro = 'BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS' + code = '%s(%s, %s, %i, %i)\n' % (macro, OverloadName(m), func, m.minArgs, m.maxArgs) if code not in declared: declared[code] = True self.Add('declaration', code) diff --git a/pyste/src/declarations.py b/pyste/src/declarations.py index e5b1a3a5..f9eb2c27 100644 --- a/pyste/src/declarations.py +++ b/pyste/src/declarations.py @@ -209,13 +209,18 @@ class Method(Function): def PointerDeclaration(self): 'returns a declaration of a pointer to this function' - result = self.result.FullName() - params = ', '.join([x.FullName() for x in self.parameters]) - const = '' - if self.const: - const = 'const' - return '(%s (%s::*)(%s) %s)&%s' %\ - (result, self.class_, params, const, self.FullName()) + if self.static: + # static methods are like normal functions + return Function.PointerDeclaration(self) + else: + # using syntax of methods + result = self.result.FullName() + params = ', '.join([x.FullName() for x in self.parameters]) + const = '' + if self.const: + const = 'const' + return '(%s (%s::*)(%s) %s)&%s' %\ + (result, self.class_, params, const, self.FullName()) class Constructor(Method): diff --git a/pyste/src/pyste.py b/pyste/src/pyste.py index 91f48142..785bcebe 100644 --- a/pyste/src/pyste.py +++ b/pyste/src/pyste.py @@ -34,7 +34,7 @@ from policies import * from CppParser import CppParser, CppParserError import time -__VERSION__ = '0.7.2' +__VERSION__ = '0.7.3' def RecursiveIncludes(include): 'Return a list containg the include dir and all its subdirectories' diff --git a/pyste/tests/example_basicUT.py b/pyste/tests/example_basicUT.py index 0e9e302e..3b6f1d5c 100644 --- a/pyste/tests/example_basicUT.py +++ b/pyste/tests/example_basicUT.py @@ -53,6 +53,14 @@ class BasicExampleTest(unittest.TestCase): C.const_static_value = 1 self.assertRaises(AttributeError, set_const_static) + # test static function + def test_mul(result, *args): + self.assertEqual(C.mul(*args), result) + self.assertEqual(c.mul(*args), result) + test_mul(6) + test_mul(3, 1) + test_mul(16, 8, 2) + if __name__ == '__main__': unittest.main() diff --git a/pyste/tests/example_inheritUT.py b/pyste/tests/example_inheritUT.py new file mode 100644 index 00000000..43d7bb8f --- /dev/null +++ b/pyste/tests/example_inheritUT.py @@ -0,0 +1,20 @@ +import unittest +from inherit import * + +class InheritExampleTest(unittest.TestCase): + + def testIt(self): + a = A_int() + b = B() + self.assert_(isinstance(b, A_int)) + self.assert_(issubclass(B, A_int)) + a.set(10) + self.assertEqual(a.get(), 10) + b.set(1) + self.assertEqual(b.go(), 1) + self.assertEqual(b.get(), 1) + + + +if __name__ == '__main__': + unittest.main() diff --git a/pyste/tests/nt_build_all.bat b/pyste/tests/nt_build_all.bat index e93eafc7..db5bc1fd 100644 --- a/pyste/tests/nt_build_all.bat +++ b/pyste/tests/nt_build_all.bat @@ -12,3 +12,4 @@ call nt_build_pyste.bat virtual %1 call nt_build_pyste.bat virtual2 %1 call nt_build_pyste.bat wrappertest %1 call nt_build_pyste.bat opaque %1 +call nt_build_pyste.bat inherit %1 diff --git a/pyste/tests/nt_clean.bat b/pyste/tests/nt_clean.bat index ebc04f15..b305b02b 100644 --- a/pyste/tests/nt_clean.bat +++ b/pyste/tests/nt_clean.bat @@ -12,6 +12,7 @@ rm -Rf _virtual rm -Rf _virtual2 rm -Rf _wrappertest rm -Rf _opaque +rm -Rf _inherit rm -f *.cpp rm -f *.obj