Skip to content

Commit d598404

Browse files
committed
initial commit
[SVN r19449]
1 parent 32c7088 commit d598404

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

test/keywords.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#include <boost/python.hpp>
7+
#include <string>
8+
9+
struct Foo
10+
{
11+
Foo(
12+
int a = 0
13+
, double b = 0
14+
, const std::string &n = std::string()
15+
) :
16+
a_(a)
17+
, b_(b)
18+
, n_(n)
19+
{}
20+
21+
void set(int a=0, double b=0, const std::string &n=std::string())
22+
{
23+
a_ = a;
24+
b_ = b;
25+
n_ = n;
26+
}
27+
28+
int geta() const { return a_; }
29+
30+
double getb() const { return b_; }
31+
32+
std::string getn() const { return n_; }
33+
34+
private:
35+
int a_;
36+
double b_;
37+
std::string n_;
38+
};
39+
40+
struct Bar
41+
{
42+
Bar(
43+
int a = 0
44+
, double b = 0
45+
, const std::string &n = std::string()
46+
) :
47+
a_(a)
48+
, b_(b)
49+
, n_(n)
50+
{}
51+
52+
void set(int a=0, double b=0, const std::string &n=std::string())
53+
{
54+
a_ = a;
55+
b_ = b;
56+
n_ = n;
57+
}
58+
59+
int geta() const { return a_; }
60+
61+
double getb() const { return b_; }
62+
63+
std::string getn() const { return n_; }
64+
65+
private:
66+
int a_;
67+
double b_;
68+
std::string n_;
69+
};
70+
71+
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(bar_set, Bar::set, 0,3)
72+
73+
using namespace boost::python;
74+
#if BOOST_WORKAROUND(__GNUC__, == 2)
75+
using boost::python::arg;
76+
#endif
77+
BOOST_PYTHON_MODULE(keywords)
78+
{
79+
class_<Foo>("Foo" , init<
80+
int
81+
, double
82+
, const std::string &
83+
>(
84+
( arg("a") = 0
85+
, arg("b") = 0.0
86+
, arg("n") = std::string()
87+
)
88+
))
89+
90+
.def("set", &Foo::set, (arg("a") = 0, arg("b") = 0.0, arg("n") = std::string()) )
91+
.def("a", &Foo::geta)
92+
.def("b", &Foo::getb)
93+
.def("n", &Foo::getn)
94+
;
95+
96+
class_<Bar>("Bar" , init<optional<
97+
int
98+
, double
99+
, const std::string &
100+
>
101+
>()
102+
)
103+
104+
.def("set", &Bar::set, bar_set())
105+
.def("a", &Bar::geta)
106+
.def("b", &Bar::getb)
107+
.def("n", &Bar::getn)
108+
;
109+
110+
}
111+
112+
113+
114+
#include "module_tail.cpp"

test/keywords_test.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'''
2+
>>> from keywords import *
3+
>>> f = Foo()
4+
>>> f.a(), f.b(), f.n()
5+
(0, 0.0, '')
6+
>>> f = Foo(1)
7+
>>> f.a(), f.b(), f.n()
8+
(1, 0.0, '')
9+
>>> f = Foo(1,1.0)
10+
>>> f.a(), f.b(), f.n()
11+
(1, 1.0, '')
12+
>>> f = Foo(1,1.0,"1")
13+
>>> f.a(), f.b(), f.n()
14+
(1, 1.0, '1')
15+
>>> f = Foo(a=1)
16+
>>> f.a(), f.b(), f.n()
17+
(1, 0.0, '')
18+
>>> f = Foo(b=1)
19+
>>> f.a(), f.b(), f.n()
20+
(0, 1.0, '')
21+
>>> f = Foo(n="1")
22+
>>> f.a(), f.b(), f.n()
23+
(0, 0.0, '1')
24+
>>> f = Foo(1,n="1")
25+
>>> f.a(), f.b(), f.n()
26+
(1, 0.0, '1')
27+
>>> f.set()
28+
>>> f.a(), f.b(), f.n()
29+
(0, 0.0, '')
30+
>>> f.set(1)
31+
>>> f.a(), f.b(), f.n()
32+
(1, 0.0, '')
33+
>>> f.set(1,1.0)
34+
>>> f.a(), f.b(), f.n()
35+
(1, 1.0, '')
36+
>>> f.set(1,1.0,"1")
37+
>>> f.a(), f.b(), f.n()
38+
(1, 1.0, '1')
39+
>>> f.set(a=1)
40+
>>> f.a(), f.b(), f.n()
41+
(1, 0.0, '')
42+
>>> f.set(b=1)
43+
>>> f.a(), f.b(), f.n()
44+
(0, 1.0, '')
45+
>>> f.set(n="1")
46+
>>> f.a(), f.b(), f.n()
47+
(0, 0.0, '1')
48+
>>> f.set(1,n="1")
49+
>>> f.a(), f.b(), f.n()
50+
(1, 0.0, '1')
51+
52+
# lets see how badly we've broken the 'regular' functions
53+
>>> f = Bar()
54+
>>> f.a(), f.b(), f.n()
55+
(0, 0.0, '')
56+
>>> f = Bar(1)
57+
>>> f.a(), f.b(), f.n()
58+
(1, 0.0, '')
59+
>>> f = Bar(1,1.0)
60+
>>> f.a(), f.b(), f.n()
61+
(1, 1.0, '')
62+
>>> f = Bar(1,1.0,"1")
63+
>>> f.a(), f.b(), f.n()
64+
(1, 1.0, '1')
65+
>>> f.set()
66+
>>> f.a(), f.b(), f.n()
67+
(0, 0.0, '')
68+
>>> f.set(1)
69+
>>> f.a(), f.b(), f.n()
70+
(1, 0.0, '')
71+
>>> f.set(1,1.0)
72+
>>> f.a(), f.b(), f.n()
73+
(1, 1.0, '')
74+
>>> f.set(1,1.0,"1")
75+
>>> f.a(), f.b(), f.n()
76+
(1, 1.0, '1')
77+
'''
78+
79+
80+
81+
82+
def run(args = None):
83+
import sys
84+
import doctest
85+
86+
if args is not None:
87+
sys.argv = args
88+
return doctest.testmod(sys.modules.get(__name__))
89+
90+
if __name__ == '__main__':
91+
print "running..."
92+
import sys
93+
sys.exit(run()[0])
94+

0 commit comments

Comments
 (0)