mirror of
https://github.com/boostorg/python.git
synced 2026-01-26 18:52:26 +00:00
Default Arguments Handling
[SVN r14786]
This commit is contained in:
64
test/defaults.cpp
Normal file
64
test/defaults.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
// 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.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
#include <sstream>
|
||||
|
||||
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
|
||||
# include <iostream> // works around a KCC intermediate code generation bug
|
||||
#endif
|
||||
|
||||
using namespace boost::python;
|
||||
using namespace std;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::string
|
||||
foo(int a, char b = 'D', std::string c = "default", double d = 0.0)
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "int(" << a << "); ";
|
||||
stream << "char(" << b << "); ";
|
||||
stream << "string(" << c << "); ";
|
||||
stream << "double(" << d << "); ";
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
BOOST_PYTHON_FUNCTION_GEN(foo_stubs, foo, 1, 4)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct X {
|
||||
|
||||
std::string
|
||||
bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream << "int(" << a << "); ";
|
||||
stream << "char(" << b << "); ";
|
||||
stream << "string(" << c << "); ";
|
||||
stream << "double(" << d << "); ";
|
||||
return stream.str();
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_PYTHON_MEMBER_FUNCTION_GEN(X_bar_stubs, bar, 1, 4)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(defaults_ext)
|
||||
{
|
||||
module m("defaults_ext");
|
||||
m.def(foo_stubs(), args<std::string, int, char, std::string, double>());
|
||||
|
||||
class_<X> xc("X");
|
||||
m.add(xc);
|
||||
|
||||
xc.def_init();
|
||||
xc.def(X_bar_stubs(), args<std::string, X const&, int, char, std::string, double>());
|
||||
}
|
||||
|
||||
#include "module_tail.cpp"
|
||||
82
test/defaults.py
Normal file
82
test/defaults.py
Normal file
@@ -0,0 +1,82 @@
|
||||
"""
|
||||
|
||||
>>> from defaults_ext import *
|
||||
>>> foo(1)
|
||||
'int(1); char(D); string(default); double(0); '
|
||||
>>> foo(2, 'X')
|
||||
'int(2); char(X); string(default); double(0); '
|
||||
>>> foo(3, 'Y', "Hello World")
|
||||
'int(3); char(Y); string(Hello World); double(0); '
|
||||
>>> foo(4, 'Z', "Hi There", 3.3)
|
||||
'int(4); char(Z); string(Hi There); double(3.3); '
|
||||
>>> x = X()
|
||||
>>> x.bar(1)
|
||||
'int(1); char(D); string(default); double(0); '
|
||||
>>> x.bar(2, 'X')
|
||||
'int(2); char(X); string(default); double(0); '
|
||||
>>> x.bar(3, 'Y', "Hello World")
|
||||
'int(3); char(Y); string(Hello World); double(0); '
|
||||
>>> x.bar(4, 'Z', "Hi There", 3.3)
|
||||
'int(4); char(Z); string(Hi There); double(3.3); '
|
||||
>>>
|
||||
|
||||
|
||||
|
||||
"""
|
||||
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])
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user