From 6f4167700cf500554061b517818eebb7c5352a88 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Tue, 14 Dec 2004 03:33:30 +0000 Subject: [PATCH] tests for raw constructors [SVN r26493] --- test/raw_ctor.cpp | 43 +++++++++++++++++++++++++++ test/raw_ctor.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100755 test/raw_ctor.cpp create mode 100644 test/raw_ctor.py diff --git a/test/raw_ctor.cpp b/test/raw_ctor.cpp new file mode 100755 index 00000000..a825ae01 --- /dev/null +++ b/test/raw_ctor.cpp @@ -0,0 +1,43 @@ +// Copyright David Abrahams 2004. Distributed under the Boost +// Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include +#include + +using namespace boost::python; + +class Foo +{ + public: + Foo(tuple args, dict kw) + : args(args), kw(kw) {} + + tuple args; + dict kw; +}; + +object init_foo(tuple args, dict kw) +{ + tuple rest(args.slice(1,_)); + return args[0].attr("__init__")(rest, kw); +} + +BOOST_PYTHON_MODULE(raw_ctor_ext) +{ + // using no_init postpones defining __init__ function until after + // raw_function for proper overload resolution order, since later + // defs get higher priority. + class_("Foo", no_init) + .def("__init__", raw_function(&init_foo)) + .def(init()) + .def_readwrite("args", &Foo::args) + .def_readwrite("kw", &Foo::kw) + ; +} + +#include "module_tail.cpp" diff --git a/test/raw_ctor.py b/test/raw_ctor.py new file mode 100644 index 00000000..686948ff --- /dev/null +++ b/test/raw_ctor.py @@ -0,0 +1,76 @@ +# Copyright David Abrahams 2004. Distributed under the Boost +# Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +""" +>>> 'importing' +'importing' +>>> from raw_ctor_ext import * +>>> 'imported' +'imported' +>>> import sys +>>> sys.stdout.flush() +>>> f = Foo(1, 2, 'a', bar = 3, baz = 4) +>>> f.args +(1, 2, 'a') +>>> f.kw.items() +[('bar', 3), ('baz', 4)] +""" +def run(args = None): + import sys + import doctest + + if args is not None: + sys.argv = args + return doctest.testmod(sys.modules.get(__name__)) + +if __name__ == '__main__': + print "running..." + import sys + status = run()[0] + if (status == 0): print "Done." + sys.exit(status) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +