2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-21 05:02:17 +00:00

initial commit

[SVN r19449]
This commit is contained in:
Dave Abrahams
2003-08-04 23:52:01 +00:00
parent 32c7088600
commit d598404c48
2 changed files with 208 additions and 0 deletions

114
test/keywords.cpp Executable file
View File

@@ -0,0 +1,114 @@
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#include <boost/python.hpp>
#include <string>
struct Foo
{
Foo(
int a = 0
, double b = 0
, const std::string &n = std::string()
) :
a_(a)
, b_(b)
, n_(n)
{}
void set(int a=0, double b=0, const std::string &n=std::string())
{
a_ = a;
b_ = b;
n_ = n;
}
int geta() const { return a_; }
double getb() const { return b_; }
std::string getn() const { return n_; }
private:
int a_;
double b_;
std::string n_;
};
struct Bar
{
Bar(
int a = 0
, double b = 0
, const std::string &n = std::string()
) :
a_(a)
, b_(b)
, n_(n)
{}
void set(int a=0, double b=0, const std::string &n=std::string())
{
a_ = a;
b_ = b;
n_ = n;
}
int geta() const { return a_; }
double getb() const { return b_; }
std::string getn() const { return n_; }
private:
int a_;
double b_;
std::string n_;
};
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(bar_set, Bar::set, 0,3)
using namespace boost::python;
#if BOOST_WORKAROUND(__GNUC__, == 2)
using boost::python::arg;
#endif
BOOST_PYTHON_MODULE(keywords)
{
class_<Foo>("Foo" , init<
int
, double
, const std::string &
>(
( arg("a") = 0
, arg("b") = 0.0
, arg("n") = std::string()
)
))
.def("set", &Foo::set, (arg("a") = 0, arg("b") = 0.0, arg("n") = std::string()) )
.def("a", &Foo::geta)
.def("b", &Foo::getb)
.def("n", &Foo::getn)
;
class_<Bar>("Bar" , init<optional<
int
, double
, const std::string &
>
>()
)
.def("set", &Bar::set, bar_set())
.def("a", &Bar::geta)
.def("b", &Bar::getb)
.def("n", &Bar::getn)
;
}
#include "module_tail.cpp"

94
test/keywords_test.py Normal file
View File

@@ -0,0 +1,94 @@
'''
>>> from keywords import *
>>> f = Foo()
>>> f.a(), f.b(), f.n()
(0, 0.0, '')
>>> f = Foo(1)
>>> f.a(), f.b(), f.n()
(1, 0.0, '')
>>> f = Foo(1,1.0)
>>> f.a(), f.b(), f.n()
(1, 1.0, '')
>>> f = Foo(1,1.0,"1")
>>> f.a(), f.b(), f.n()
(1, 1.0, '1')
>>> f = Foo(a=1)
>>> f.a(), f.b(), f.n()
(1, 0.0, '')
>>> f = Foo(b=1)
>>> f.a(), f.b(), f.n()
(0, 1.0, '')
>>> f = Foo(n="1")
>>> f.a(), f.b(), f.n()
(0, 0.0, '1')
>>> f = Foo(1,n="1")
>>> f.a(), f.b(), f.n()
(1, 0.0, '1')
>>> f.set()
>>> f.a(), f.b(), f.n()
(0, 0.0, '')
>>> f.set(1)
>>> f.a(), f.b(), f.n()
(1, 0.0, '')
>>> f.set(1,1.0)
>>> f.a(), f.b(), f.n()
(1, 1.0, '')
>>> f.set(1,1.0,"1")
>>> f.a(), f.b(), f.n()
(1, 1.0, '1')
>>> f.set(a=1)
>>> f.a(), f.b(), f.n()
(1, 0.0, '')
>>> f.set(b=1)
>>> f.a(), f.b(), f.n()
(0, 1.0, '')
>>> f.set(n="1")
>>> f.a(), f.b(), f.n()
(0, 0.0, '1')
>>> f.set(1,n="1")
>>> f.a(), f.b(), f.n()
(1, 0.0, '1')
# lets see how badly we've broken the 'regular' functions
>>> f = Bar()
>>> f.a(), f.b(), f.n()
(0, 0.0, '')
>>> f = Bar(1)
>>> f.a(), f.b(), f.n()
(1, 0.0, '')
>>> f = Bar(1,1.0)
>>> f.a(), f.b(), f.n()
(1, 1.0, '')
>>> f = Bar(1,1.0,"1")
>>> f.a(), f.b(), f.n()
(1, 1.0, '1')
>>> f.set()
>>> f.a(), f.b(), f.n()
(0, 0.0, '')
>>> f.set(1)
>>> f.a(), f.b(), f.n()
(1, 0.0, '')
>>> f.set(1,1.0)
>>> f.a(), f.b(), f.n()
(1, 1.0, '')
>>> f.set(1,1.0,"1")
>>> f.a(), f.b(), f.n()
(1, 1.0, '1')
'''
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
sys.exit(run()[0])