2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00

tests for raw constructors

[SVN r26493]
This commit is contained in:
Dave Abrahams
2004-12-14 03:33:30 +00:00
parent efae35bbd8
commit 6f4167700c
2 changed files with 119 additions and 0 deletions

43
test/raw_ctor.cpp Executable file
View File

@@ -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 <boost/python/class.hpp>
#include <boost/python/raw_function.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/dict.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/module.hpp>
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>("Foo", no_init)
.def("__init__", raw_function(&init_foo))
.def(init<tuple, dict>())
.def_readwrite("args", &Foo::args)
.def_readwrite("kw", &Foo::kw)
;
}
#include "module_tail.cpp"

76
test/raw_ctor.py Normal file
View File

@@ -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)