mirror of
https://github.com/boostorg/python.git
synced 2026-01-31 08:22:18 +00:00
Keyword argument support
[SVN r15533]
This commit is contained in:
@@ -54,10 +54,10 @@ rule bpl-test ( name ? : files * : requirements * )
|
||||
boost-python-runtest $(name) : $(py) <pyd>$(modules) ;
|
||||
}
|
||||
|
||||
bpl-test numpy ;
|
||||
|
||||
bpl-test enum ;
|
||||
bpl-test minimal ;
|
||||
bpl-test args ;
|
||||
bpl-test numpy ;
|
||||
bpl-test enum ;
|
||||
bpl-test docstring ;
|
||||
bpl-test exception_translator ;
|
||||
bpl-test pearu1 : test_cltree.py cltree.cpp ;
|
||||
|
||||
81
test/args.cpp
Normal file
81
test/args.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
// 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/module_init.hpp>
|
||||
#include <boost/python/def.hpp>
|
||||
#include <boost/python/args.hpp>
|
||||
#include <boost/python/tuple.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
#include <boost/python/return_internal_reference.hpp>
|
||||
#include "test_class.hpp"
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
tuple f(int x = 1, double y = 4.25, char const* z = "wow")
|
||||
{
|
||||
return make_tuple(x, y, z);
|
||||
}
|
||||
|
||||
BOOST_PYTHON_FUNCTION_OVERLOADS(f_overloads, f, 0, 3)
|
||||
|
||||
typedef test_class<> Y;
|
||||
|
||||
struct X
|
||||
{
|
||||
X(int a0 = 0, int a1 = 1) : inner0(a0), inner1(a1) {}
|
||||
tuple f(int x = 1, double y = 4.25, char const* z = "wow")
|
||||
{
|
||||
return make_tuple(x, y, z);
|
||||
}
|
||||
|
||||
Y const& inner(bool n) const { return n ? inner1 : inner0; }
|
||||
|
||||
Y inner0;
|
||||
Y inner1;
|
||||
};
|
||||
|
||||
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_f_overloads, X::f, 0, 3)
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(args_ext)
|
||||
{
|
||||
def("f", f, args("x", "y", "z")
|
||||
, "This is f's docstring"
|
||||
);
|
||||
|
||||
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1200
|
||||
// MSVC6 gives a fatal error LNK1179: invalid or corrupt file:
|
||||
// duplicate comdat error if we try to re-use the exact type of f
|
||||
// here, so substitute long for int.
|
||||
tuple (*f)(long,double,char const*) = 0;
|
||||
#endif
|
||||
def("f1", f, f_overloads("f1's docstring", args("x", "y", "z")));
|
||||
def("f2", f, f_overloads(args("x", "y", "z")));
|
||||
def("f3", f, f_overloads(args("x", "y", "z"), "f3's docstring"));
|
||||
|
||||
class_<Y>("Y", init<int>())
|
||||
.def("value", &Y::value)
|
||||
;
|
||||
|
||||
class_<X>("X", "This is X's docstring")
|
||||
.def(init<int, optional<int> >(args("a0", "a1")))
|
||||
.def("f", &X::f
|
||||
, "This is X.f's docstring"
|
||||
, args("x", "y", "z"))
|
||||
|
||||
// Just to prove that all the different argument combinations work
|
||||
.def("inner0", &X::inner, return_internal_reference<>(), args("n"), "docstring")
|
||||
.def("inner1", &X::inner, return_internal_reference<>(), "docstring", args("n"))
|
||||
|
||||
.def("inner2", &X::inner, args("n"), return_internal_reference<>(), "docstring")
|
||||
.def("inner3", &X::inner, "docstring", return_internal_reference<>(), args("n"))
|
||||
|
||||
.def("inner4", &X::inner, args("n"), "docstring", return_internal_reference<>())
|
||||
.def("inner5", &X::inner, "docstring", args("n"), return_internal_reference<>())
|
||||
|
||||
.def("f1", &X::f, X_f_overloads(args("x", "y", "z")))
|
||||
;
|
||||
|
||||
def("inner", &X::inner, "docstring", args("self", "n"), return_internal_reference<>());
|
||||
}
|
||||
161
test/args.py
Normal file
161
test/args.py
Normal file
@@ -0,0 +1,161 @@
|
||||
"""
|
||||
>>> from args_ext import *
|
||||
|
||||
>>> f(x= 1, y = 3, z = 'hello')
|
||||
(1, 3.0, 'hello')
|
||||
|
||||
>>> f(z = 'hello', x = 3, y = 2.5)
|
||||
(3, 2.5, 'hello')
|
||||
|
||||
>>> f(1, z = 'hi', y = 3)
|
||||
(1, 3.0, 'hi')
|
||||
|
||||
>>> try: f(1, 2, 'hello', bar = 'baz')
|
||||
... except TypeError: pass
|
||||
... else: print 'expected an exception: unknown keyword'
|
||||
|
||||
|
||||
Exercise the functions using default stubs
|
||||
|
||||
>>> f1(z = 'nix', y = .125, x = 2)
|
||||
(2, 0.125, 'nix')
|
||||
>>> f1(y = .125, x = 2)
|
||||
(2, 0.125, 'wow')
|
||||
>>> f1(x = 2)
|
||||
(2, 4.25, 'wow')
|
||||
>>> f1()
|
||||
(1, 4.25, 'wow')
|
||||
|
||||
>>> f2(z = 'nix', y = .125, x = 2)
|
||||
(2, 0.125, 'nix')
|
||||
>>> f2(y = .125, x = 2)
|
||||
(2, 0.125, 'wow')
|
||||
>>> f2(x = 2)
|
||||
(2, 4.25, 'wow')
|
||||
>>> f2()
|
||||
(1, 4.25, 'wow')
|
||||
|
||||
>>> f3(z = 'nix', y = .125, x = 2)
|
||||
(2, 0.125, 'nix')
|
||||
>>> f3(y = .125, x = 2)
|
||||
(2, 0.125, 'wow')
|
||||
>>> f3(x = 2)
|
||||
(2, 4.25, 'wow')
|
||||
>>> f3()
|
||||
(1, 4.25, 'wow')
|
||||
|
||||
Member function tests
|
||||
|
||||
>>> q = X()
|
||||
>>> q.f(x= 1, y = 3, z = 'hello')
|
||||
(1, 3.0, 'hello')
|
||||
|
||||
>>> q.f(z = 'hello', x = 3, y = 2.5)
|
||||
(3, 2.5, 'hello')
|
||||
|
||||
>>> q.f(1, z = 'hi', y = 3)
|
||||
(1, 3.0, 'hi')
|
||||
|
||||
>>> try: q.f(1, 2, 'hello', bar = 'baz')
|
||||
... except TypeError: pass
|
||||
... else: print 'expected an exception: unknown keyword'
|
||||
|
||||
Exercise member functions using default stubs
|
||||
|
||||
>>> q.f1(z = 'nix', y = .125, x = 2)
|
||||
(2, 0.125, 'nix')
|
||||
>>> q.f1(y = .125, x = 2)
|
||||
(2, 0.125, 'wow')
|
||||
>>> q.f1(x = 2)
|
||||
(2, 4.25, 'wow')
|
||||
>>> q.f1()
|
||||
(1, 4.25, 'wow')
|
||||
|
||||
>>> X.f.__doc__
|
||||
"This is X.f's docstring"
|
||||
|
||||
>>> xfuncs = (X.inner0, X.inner1, X.inner2, X.inner3, X.inner4, X.inner5)
|
||||
>>> for f in xfuncs:
|
||||
... print f(q,1).value(),
|
||||
... print f(q, n = 1).value(),
|
||||
... print f(q, n = 0).value(),
|
||||
... print f.__doc__
|
||||
1 1 0 docstring
|
||||
1 1 0 docstring
|
||||
1 1 0 docstring
|
||||
1 1 0 docstring
|
||||
1 1 0 docstring
|
||||
1 1 0 docstring
|
||||
|
||||
>>> x = X(a1 = 44, a0 = 22)
|
||||
>>> x.inner0(0).value()
|
||||
22
|
||||
>>> x.inner0(1).value()
|
||||
44
|
||||
|
||||
>>> x = X(a0 = 7)
|
||||
>>> x.inner0(0).value()
|
||||
7
|
||||
>>> x.inner0(1).value()
|
||||
1
|
||||
|
||||
>>> inner(n = 1, self = q).value()
|
||||
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])
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#include <boost/python/def.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
|
||||
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1200 // works around a name lookup bug
|
||||
# define C C_
|
||||
#endif
|
||||
struct C {};
|
||||
|
||||
struct D {};
|
||||
|
||||
Reference in New Issue
Block a user