From 1c9bf7d91c078ccfaca67fbd49d46bfb51387da5 Mon Sep 17 00:00:00 2001 From: Bruno da Silva de Oliveira Date: Sun, 6 Apr 2003 20:47:10 +0000 Subject: [PATCH] - Generating code for the improved support of static data members of Boost.Python [SVN r18193] --- pyste/NEWS | 3 +++ pyste/example/basic.h | 19 +++++++++++++++++++ pyste/example/basic.pyste | 3 +++ pyste/src/ClassExporter.py | 14 +++++--------- pyste/src/pyste.py | 2 +- pyste/tests/example_basicUT.py | 34 +++++++++++++++++++++++++++++++++- 6 files changed, 64 insertions(+), 11 deletions(-) diff --git a/pyste/NEWS b/pyste/NEWS index 0001c93d..2dd2cbda 100644 --- a/pyste/NEWS +++ b/pyste/NEWS @@ -1,3 +1,6 @@ +06 Apr 2003 +Support for the improved static data members support of Boost.Python. + 05 Apr 2003 New option for generating the bindings: --multiple. diff --git a/pyste/example/basic.h b/pyste/example/basic.h index 93e49d36..92ff55bc 100644 --- a/pyste/example/basic.h +++ b/pyste/example/basic.h @@ -4,6 +4,7 @@ namespace basic { struct C { + C(): value(1), const_value(0) {} virtual int f(int x = 10) { return x*2; @@ -16,8 +17,16 @@ struct C const std::string& name() { return _name; } void set_name(const std::string& name) { _name = name; } std::string _name; + static int static_value; + static const int const_static_value; + + int value; + const int const_value; }; +int C::static_value = 3; +const int C::const_static_value = 100; + int call_f(C& c) { return c.f(); @@ -28,4 +37,14 @@ int call_f(C& c, int x) return c.f(x); } +int get_static() +{ + return C::static_value; +} + +int get_value(C& c) +{ + return c.value; +} + } diff --git a/pyste/example/basic.pyste b/pyste/example/basic.pyste index 90978d19..4fe0b5b3 100644 --- a/pyste/example/basic.pyste +++ b/pyste/example/basic.pyste @@ -1,2 +1,5 @@ Class('basic::C', 'basic.h') Function('basic::call_f', 'basic.h') +Function('basic::get_static', 'basic.h') +Function('basic::get_value', 'basic.h') + diff --git a/pyste/src/ClassExporter.py b/pyste/src/ClassExporter.py index f2c4f983..097c6be3 100644 --- a/pyste/src/ClassExporter.py +++ b/pyste/src/ClassExporter.py @@ -243,16 +243,12 @@ class ClassExporter(Exporter): continue name = self.info[var.name].rename or var.name fullname = var.FullName() - if var.static: - code = '%s->attr("%s") = %s;' % (self.ScopeName(), name, fullname) - self.Add('scope', code) + if var.type.const: + def_ = '.def_readonly' else: - if var.type.const: - def_ = '.def_readonly' - else: - def_ = '.def_readwrite' - code = '%s("%s", &%s)' % (def_, name, fullname) - self.Add('inside', code) + def_ = '.def_readwrite' + code = '%s("%s", &%s)' % (def_, name, fullname) + self.Add('inside', code) def ExportMethods(self): diff --git a/pyste/src/pyste.py b/pyste/src/pyste.py index 28124505..28eb3744 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.6.6' +__VERSION__ = '0.7.0' 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 37425f3f..0e9e302e 100644 --- a/pyste/tests/example_basicUT.py +++ b/pyste/tests/example_basicUT.py @@ -1,10 +1,11 @@ import unittest +from basic import * class BasicExampleTest(unittest.TestCase): def testIt(self): - from basic import C, call_f + # test virtual functions class D(C): def f(self, x=10): return x+1 @@ -20,6 +21,37 @@ class BasicExampleTest(unittest.TestCase): self.assertEqual(call_f(c, 4), 8) self.assertEqual(call_f(d), 11) self.assertEqual(call_f(d, 3), 4) + + # test data members + def testValue(value): + self.assertEqual(c.value, value) + self.assertEqual(d.value, value) + self.assertEqual(get_value(c), value) + self.assertEqual(get_value(d), value) + testValue(1) + c.value = 30 + d.value = 30 + testValue(30) + self.assertEqual(c.const_value, 0) + self.assertEqual(d.const_value, 0) + def set_const_value(): + c.const_value = 12 + self.assertRaises(AttributeError, set_const_value) + + # test static data-members + def testStatic(value): + self.assertEqual(C.static_value, value) + self.assertEqual(c.static_value, value) + self.assertEqual(D.static_value, value) + self.assertEqual(d.static_value, value) + self.assertEqual(get_static(), value) + testStatic(3) + C.static_value = 10 + testStatic(10) + self.assertEqual(C.const_static_value, 100) + def set_const_static(): + C.const_static_value = 1 + self.assertRaises(AttributeError, set_const_static) if __name__ == '__main__':