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

This commit was manufactured by cvs2svn to create tag

'merged_to_1_34_0'.

[SVN r35241]
This commit is contained in:
nobody
2006-09-21 03:44:00 +00:00
parent ccc56c2a4c
commit ba108f484a
656 changed files with 0 additions and 75517 deletions

View File

@@ -1,240 +0,0 @@
# Copyright David Abrahams 2006. 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)
#
# To run all tests quietly: jam test
# To run all tests with verbose output: jam -sPYTHON_TEST_ARGS=-v test
#
subproject libs/python/test ;
# bring in the rules for python and testing
import python ;
if [ check-python-config ]
{
# A bug in the Win32 intel compilers causes compilation of one of our
# tests to take forever when debug symbols are enabled. This rule
# turns them off when added to the requirements section
rule turn-off-intel-debug-symbols ( toolset variant : properties * )
{
if $(NT) && [ MATCH (.*intel.*) : $(toolset) ]
{
properties = [ difference $(properties) : <debug-symbols>on ] <debug-symbols>off ;
}
return $(properties) ;
}
# Some tests hang when compiled with HP-CXX and executed on Tru64.
rule disable-for-cxx-tru64 ( toolset variant : properties * )
{
if $(UNIX) && $(OS) = OSF
{
switch $(toolset)
{
case tru64cxx* : properties =
[ replace-properties $(properties) : <build>no ] ;
}
}
return $(properties) ;
}
template py-unit-test
:
: $(PYTHON_PROPERTIES) <define>BOOST_PYTHON_SUPPRESS_REGISTRY_INITIALIZATION
[ difference $(PYTHON_PROPERTIES) : <define>BOOST_PYTHON_DYNAMIC_LIB ] <define>BOOST_PYTHON_STATIC_LIB
;
# Convenience rule makes declaring tests faster
rule bpl-test ( name ? : files * : requirements * )
{
files ?= $(name).py $(name).cpp ;
local modules ;
local py ;
for local f in $(files)
{
if $(f:S) = .py
{
if $(py)
{
EXIT too many python drivers specified: "$(py)" "$(f)" ;
}
py = $(f) ;
}
}
name ?= $(py:S=) ;
for local f in $(files)
{
if $(f:S) != .py
{
local m = $(f:S=) ;
if $(m) = $(py:S=)
{
m = $(name) ;
if $(m) = $(py:S=)
{
m = $(m)_ext ;
}
}
extension $(m) : $(f) <template>../build/extension : $(requirements) ;
modules += $(m) ;
}
}
return [ boost-python-runtest $(name) : $(py) <pyd>$(modules) : $(requirements) : : -v ] ;
}
test-suite python
:
[
run ../test/exec.cpp <lib>../build/boost_python
: # program args
: exec.py
: # requirements
$(PYTHON_PROPERTIES)
<define>BOOST_PYTHON_STATIC_LIB
<define>BOOST_PYTHON_STATIC_MODULE
<library-path>$(PYTHON_LIB_PATH)
<$(gcc-compilers)><debug-python><library-path>$(CYGWIN_PYTHON_DEBUG_DLL_PATH)
<$(gcc-compilers)><*><library-path>$(CYGWIN_PYTHON_DLL_PATH)
<find-library>$(PYTHON_EMBEDDED_LIBRARY)
]
[
bpl-test crossmod_exception
: crossmod_exception.py crossmod_exception_a.cpp crossmod_exception_b.cpp
]
[ bpl-test injected ]
[ bpl-test properties ]
[ bpl-test return_arg ]
[ bpl-test staticmethod ]
[ bpl-test shared_ptr ]
[ bpl-test andreas_beyer ]
[ bpl-test polymorphism ]
[ bpl-test polymorphism2 ]
[ bpl-test wrapper_held_type ]
[ bpl-test polymorphism2_auto_ptr ]
[ bpl-test auto_ptr ]
[ bpl-test minimal ]
[ bpl-test args ]
[ bpl-test raw_ctor ]
[ bpl-test numpy ]
[ bpl-test enum ]
[ bpl-test exception_translator ]
[ bpl-test pearu1 : test_cltree.py cltree.cpp ]
[ bpl-test try : newtest.py m1.cpp m2.cpp ]
[ bpl-test const_argument ]
[ bpl-test keywords : keywords.cpp keywords_test.py ]
[ extension builtin_converters : test_builtin_converters.cpp <template>../build/extension ]
[ boost-python-runtest builtin_converters : test_builtin_converters.py <pyd>builtin_converters : : : -v ]
[ bpl-test test_pointer_adoption ]
[ bpl-test operators ]
[ bpl-test callbacks ]
[ bpl-test defaults ]
[ bpl-test object ]
[ bpl-test list ]
[ bpl-test long ]
[ bpl-test dict ]
[ bpl-test tuple ]
[ bpl-test str ]
[ bpl-test slice ]
[ bpl-test virtual_functions ]
[ bpl-test back_reference ]
[ bpl-test implicit ]
[ bpl-test data_members ]
[ bpl-test ben_scott1 ]
[ bpl-test bienstman1 ]
[ bpl-test bienstman2 ]
[ bpl-test bienstman3 ]
[ bpl-test multi_arg_constructor
: # files
: # requirements
turn-off-intel-debug-symbols ] # debug symbols slow the build down too much
[ bpl-test iterator : iterator.py iterator.cpp input_iterator.cpp ]
[ bpl-test stl_iterator : stl_iterator.py stl_iterator.cpp ]
[ bpl-test extract ]
[
bpl-test crossmod_opaque
: crossmod_opaque.py crossmod_opaque_a.cpp crossmod_opaque_b.cpp
]
[ bpl-test opaque ]
[ bpl-test voidptr ]
[ bpl-test pickle1 ]
[ bpl-test pickle2 ]
[ bpl-test pickle3 ]
[ bpl-test pickle4 ]
[ bpl-test nested ]
[ bpl-test docstring ]
[ bpl-test vector_indexing_suite ]
[ bpl-test pointer_vector
: # files
: # requirements
disable-for-cxx-tru64 # Runs forever when executed.
]
[ extension map_indexing_suite_ext
: map_indexing_suite.cpp int_map_indexing_suite.cpp <template>../build/extension ]
[ boost-python-runtest
map_indexing_suite : map_indexing_suite.py <pyd>map_indexing_suite_ext : : : -v ]
# if $(TEST_BIENSTMAN_NON_BUGS)
# {
# bpl-test bienstman4 ;
# bpl-test bienstman5 ;
# }
# --- unit tests of library components ---
[ compile indirect_traits_test.cpp ]
[ run destroy_test.cpp ]
[ run pointer_type_id_test.cpp <template>py-unit-test ]
[ run bases.cpp <template>py-unit-test ]
[ run if_else.cpp ]
[ run pointee.cpp <template>py-unit-test ]
[ run result.cpp ]
[ compile string_literal.cpp ]
[ compile borrowed.cpp <template>py-unit-test ]
[ compile object_manager.cpp <template>py-unit-test ]
[ compile copy_ctor_mutates_rhs.cpp <template>py-unit-test ]
[ run upcast.cpp <template>py-unit-test ]
[ compile select_holder.cpp <template>py-unit-test ]
[ run select_from_python_test.cpp ../src/converter/type_id.cpp
<template>py-unit-test ]
[ compile select_arg_to_python_test.cpp <template>py-unit-test ]
[ compile-fail ./raw_pyobject_fail1.cpp <template>py-unit-test ]
[ compile-fail ./raw_pyobject_fail2.cpp <template>py-unit-test ]
[ compile-fail ./as_to_python_function.cpp <template>py-unit-test ]
[ compile-fail ./object_fail1.cpp <template>py-unit-test ]
;
}

View File

@@ -1,194 +0,0 @@
# Copyright David Abrahams 2006. 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)
use-project /boost/python : ../build ;
project /boost/python/test ;
rule py-run ( sources * )
{
return [ run $(sources) /boost/python//boost_python /python//python
: # args
: # input files
: #requirements
<define>BOOST_PYTHON_SUPPRESS_REGISTRY_INITIALIZATION
] ;
}
rule py-compile ( sources * )
{
return [ compile $(sources) /boost/python//boost_python ] ;
}
rule py-compile-fail ( sources * )
{
return [ compile-fail $(sources) /boost/python//boost_python ] ;
}
#template py-unit-test
# :
# : $(PYTHON_PROPERTIES) <define>BOOST_PYTHON_SUPPRESS_REGISTRY_INITIALIZATION
# [ difference $(PYTHON_PROPERTIES) : <define>BOOST_PYTHON_DYNAMIC_LIB ] <define>BOOST_PYTHON_STATIC_LIB
# ;
test-suite python
:
[
run exec.cpp ../build//boost_python/<link>static /python//python
: # program args
: exec.py
: # requirements
<define>BOOST_PYTHON_STATIC_MODULE
]
[
bpl-test crossmod_exception
: crossmod_exception.py crossmod_exception_a.cpp crossmod_exception_b.cpp
]
[ bpl-test injected ]
[ bpl-test properties ]
[ bpl-test return_arg ]
[ bpl-test staticmethod ]
[ bpl-test shared_ptr ]
[ bpl-test andreas_beyer ]
[ bpl-test polymorphism ]
[ bpl-test polymorphism2 ]
[ bpl-test wrapper_held_type ]
[ bpl-test polymorphism2_auto_ptr ]
[ bpl-test auto_ptr ]
[ bpl-test minimal ]
[ bpl-test args ]
[ bpl-test raw_ctor ]
[ bpl-test numpy ]
[ bpl-test enum ]
[ bpl-test exception_translator ]
[ bpl-test pearu1 : test_cltree.py cltree.cpp ]
[ bpl-test try : newtest.py m1.cpp m2.cpp ]
[ bpl-test const_argument ]
[ bpl-test keywords : keywords.cpp keywords_test.py ]
[ python-extension builtin_converters.ext : test_builtin_converters.cpp /boost/python//boost_python ]
[ bpl-test builtin_converters : test_builtin_converters.py builtin_converters.ext ]
[ bpl-test test_pointer_adoption ]
[ bpl-test operators ]
[ bpl-test callbacks ]
[ bpl-test defaults ]
[ bpl-test object ]
[ bpl-test list ]
[ bpl-test long ]
[ bpl-test dict ]
[ bpl-test tuple ]
[ bpl-test str ]
[ bpl-test slice ]
[ bpl-test virtual_functions ]
[ bpl-test back_reference ]
[ bpl-test implicit ]
[ bpl-test data_members ]
[ bpl-test ben_scott1 ]
[ bpl-test bienstman1 ]
[ bpl-test bienstman2 ]
[ bpl-test bienstman3 ]
[ bpl-test multi_arg_constructor
: # files
: # requirements
# A bug in the Win32 intel compilers causes compilation of one of our
# tests to take forever when debug symbols are enabled. This rule
# turns them off when added to the requirements section
<toolset>intel-win:<debug-symbols>off
]
[ bpl-test iterator : iterator.py iterator.cpp input_iterator.cpp ]
[ bpl-test stl_iterator : stl_iterator.py stl_iterator.cpp ]
[ bpl-test extract ]
[
bpl-test crossmod_opaque
: crossmod_opaque.py crossmod_opaque_a.cpp crossmod_opaque_b.cpp
]
[ bpl-test opaque ]
[ bpl-test voidptr ]
[ bpl-test pickle1 ]
[ bpl-test pickle2 ]
[ bpl-test pickle3 ]
[ bpl-test pickle4 ]
[ bpl-test nested ]
[ bpl-test docstring ]
[ bpl-test vector_indexing_suite ]
[ bpl-test pointer_vector
: # files
: # requirements
# Turn off this test on HP CXX, as the test hangs when executing.
# Whenever the cause for the failure of the polymorphism test is found
# and fixed, this should be retested.
<toolset>hp_cxx:<build>no ]
[ python-extension map_indexing_suite_ext
: map_indexing_suite.cpp int_map_indexing_suite.cpp
/boost/python//boost_python ]
[ bpl-test
map_indexing_suite : map_indexing_suite.py map_indexing_suite_ext ]
# if $(TEST_BIENSTMAN_NON_BUGS)
# {
# bpl-test bienstman4 ;
# bpl-test bienstman5 ;
# }
# --- unit tests of library components ---
[ compile indirect_traits_test.cpp ]
[ run destroy_test.cpp ]
[ py-run pointer_type_id_test.cpp ]
[ py-run bases.cpp ]
[ run if_else.cpp ]
[ py-run pointee.cpp ]
[ run result.cpp ]
[ compile string_literal.cpp ]
[ py-compile borrowed.cpp ]
[ py-compile object_manager.cpp ]
[ py-compile copy_ctor_mutates_rhs.cpp ]
[ py-run upcast.cpp ]
[ py-compile select_holder.cpp ]
[ run select_from_python_test.cpp ../src/converter/type_id.cpp
:
:
: <define>BOOST_PYTHON_STATIC_LIB
<use>/python//python
]
[ py-compile select_arg_to_python_test.cpp ]
[ py-compile-fail ./raw_pyobject_fail1.cpp ]
[ py-compile-fail ./raw_pyobject_fail2.cpp ]
[ py-compile-fail ./as_to_python_function.cpp ]
[ py-compile-fail ./object_fail1.cpp ]
;

View File

@@ -1,61 +0,0 @@
// Copyright David Abrahams 2006. 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.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost;
class A : public enable_shared_from_this<A> {
public:
A() : val(0) {};
int val;
typedef shared_ptr<A> A_ptr;
A_ptr self() {
A_ptr self;
self = shared_from_this();
return self;
}
};
class B {
public:
B() {
a = A::A_ptr(new A());
}
void set(A::A_ptr a) {
this->a = a;
}
A::A_ptr get() {
return a;
}
A::A_ptr a;
};
template <class T>
void hold_python(shared_ptr<T>& x)
{
x = python::extract<shared_ptr<T> >( python::object(x) );
}
A::A_ptr get_b_a(shared_ptr<B> b)
{
hold_python(b->a);
return b->get();
}
BOOST_PYTHON_MODULE(andreas_beyer_ext) {
python::class_<A, noncopyable> ("A")
.def("self", &A::self)
.def_readwrite("val", &A::val)
;
python::register_ptr_to_python< A::A_ptr >();
python::class_<B>("B")
.def("set", &B::set)
// .def("get", &B::get)
.def("get", get_b_a)
;
}

View File

@@ -1,24 +0,0 @@
# 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)
'''
>>> from andreas_beyer_ext import *
>>> b=B()
>>> a=b.get() # let b create an A
>>> a2=b.get()
>>> assert id(a) == id(a2)
'''
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)

View File

@@ -1,98 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include "test_class.hpp"
#include <boost/python/def.hpp>
#include <boost/python/args.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/class.hpp>
#include <boost/python/overloads.hpp>
#include <boost/python/raw_function.hpp>
#include <boost/python/return_internal_reference.hpp>
using namespace boost::python;
#if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580))
# define make_tuple boost::python::make_tuple
#endif
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)
tuple raw_func(tuple args, dict kw)
{
return make_tuple(args, kw);
}
BOOST_PYTHON_MODULE(args_ext)
{
def("f", f, args("x", "y", "z")
, "This is f's docstring"
);
def("raw", raw_function(raw_func));
#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>(args("value"), "Y's docstring"))
.def("value", &Y::value)
.def("raw", raw_function(raw_func))
;
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("f2", &X::f, X_f_overloads(args("x", "y", "z"), "f2's docstring"))
;
def("inner", &X::inner, "docstring", args("self", "n"), return_internal_reference<>());
}
#include "module_tail.cpp"

View File

@@ -1,184 +0,0 @@
# 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)
"""
>>> from args_ext import *
>>> raw(3, 4, foo = 'bar', baz = 42)
((3, 4), {'foo': 'bar', 'baz': 42})
Prove that we can handle empty keywords and non-keywords
>>> raw(3, 4)
((3, 4), {})
>>> raw(foo = 'bar')
((), {'foo': 'bar'})
>>> 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')
>>> q.f2.__doc__.splitlines()[-3]
"f2's docstring"
>>> X.f.__doc__.splitlines()[:2]
["This is X.f's docstring", 'C++ signature:']
>>> 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__.splitlines()[:2]
1 1 0 ['docstring', 'C++ signature:']
1 1 0 ['docstring', 'C++ signature:']
1 1 0 ['docstring', 'C++ signature:']
1 1 0 ['docstring', 'C++ signature:']
1 1 0 ['docstring', 'C++ signature:']
1 1 0 ['docstring', 'C++ signature:']
>>> 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
>>> y = Y(value = 33)
>>> y.raw(this = 1, that = 'the other')[1]
{'this': 1, 'that': 'the other'}
"""
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)

View File

@@ -1,13 +0,0 @@
// Copyright David Abrahams 2002.
// 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/converter/as_to_python_function.hpp>
struct hopefully_illegal
{
static PyObject* convert(int&);
};
PyObject* x = boost::python::converter::as_to_python_function<int, hopefully_illegal>::convert(0);

View File

@@ -1,90 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include "test_class.hpp"
#include <boost/python/class.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/def.hpp>
#include <boost/python/implicit.hpp>
#include <boost/detail/workaround.hpp>
#include <memory>
using namespace boost::python;
typedef test_class<> X;
struct Y : X
{
Y(int n) : X(n) {};
};
int look(std::auto_ptr<X> const& x)
{
return (x.get()) ? x->value() : -1;
}
int steal(std::auto_ptr<X> x)
{
return x->value();
}
int maybe_steal(std::auto_ptr<X>& x, bool doit)
{
int n = x->value();
if (doit)
x.release();
return n;
}
std::auto_ptr<X> make()
{
return std::auto_ptr<X>(new X(77));
}
std::auto_ptr<X> callback(object f)
{
std::auto_ptr<X> x(new X(77));
return call<std::auto_ptr<X> >(f.ptr(), x);
}
std::auto_ptr<X> extract_(object o)
{
return extract<std::auto_ptr<X>&>(o)
#if BOOST_MSVC <= 1300
()
#endif
;
}
BOOST_PYTHON_MODULE(auto_ptr_ext)
{
class_<X, std::auto_ptr<X>, boost::noncopyable>("X", init<int>())
.def("value", &X::value)
;
class_<Y, std::auto_ptr<Y>, bases<X>, boost::noncopyable>("Y", init<int>())
;
// VC6 auto_ptrs do not have converting constructors
#if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, < 306)
scope().attr("broken_auto_ptr") = 1;
#else
scope().attr("broken_auto_ptr") = 0;
implicitly_convertible<std::auto_ptr<Y>, std::auto_ptr<X> >();
#endif
def("look", look);
def("steal", steal);
def("maybe_steal", maybe_steal);
def("make", make);
def("callback", callback);
def("extract", extract_);
}
#include "module_tail.cpp"

View File

@@ -1,82 +0,0 @@
# 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)
'''
>>> from auto_ptr_ext import *
>>> x = X(42)
>>> x.value()
42
>>> look(x), look(x)
(42, 42)
>>> maybe_steal(x, 0)
42
>>> look(x)
42
>>> maybe_steal(x, 1)
42
>>> broken_auto_ptr and -1 or look(x)
-1
>>> x = X(69)
>>> steal(x)
69
>>> broken_auto_ptr and -1 or look(x)
-1
>>> if not broken_auto_ptr:
... try: x.value()
... except TypeError: pass
... else: print 'expected a TypeError exception'
>>> x = make()
>>> look(x)
77
>>> z = callback(lambda z: z)
>>> z.value()
77
>>> extract(x).value()
77
#
# Test derived to base conversions
#
>>> y = Y(42)
>>> y.value()
42
>>> try: maybe_steal(y, 0)
... except TypeError: pass
... else: print 'expected a TypeError exception'
>>> y.value()
42
>>> broken_auto_ptr and 42 or steal(y)
42
>>> if not broken_auto_ptr:
... try: y.value()
... except TypeError: pass
... else: print 'expected a TypeError exception'
'''
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)

View File

@@ -1,112 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/has_back_reference.hpp>
#include <boost/python/back_reference.hpp>
#include <boost/ref.hpp>
#include <boost/utility.hpp>
#include <memory>
#define BOOST_ENABLE_ASSERT_HANDLER
#include <boost/assert.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/mpl/bool.hpp>
// This test shows that a class can be wrapped "as itself" but also
// acquire a back-reference iff has_back_reference<> is appropriately
// specialized.
using namespace boost::python;
struct X
{
explicit X(int x) : x(x), magic(7654321) { ++counter; }
X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
virtual ~X() { BOOST_ASSERT(magic == 7654321); magic = 6666666; x = 9999; --counter; }
void set(int x) { BOOST_ASSERT(magic == 7654321); this->x = x; }
int value() const { BOOST_ASSERT(magic == 7654321); return x; }
static int count() { return counter; }
private:
void operator=(X const&);
private:
int x;
long magic;
static int counter;
};
int X::counter;
struct Y : X
{
Y(PyObject* self, int x) : X(x), self(self) {}
Y(PyObject* self, Y const& rhs) : X(rhs), self(self) {}
private:
Y(Y const&);
PyObject* self;
};
struct Z : X
{
Z(PyObject* self, int x) : X(x), self(self) {}
Z(PyObject* self, Z const& rhs) : X(rhs), self(self) {}
private:
Z(Z const&);
PyObject* self;
};
Y const& copy_Y(Y const& y) { return y; }
Z const& copy_Z(Z const& z) { return z; }
namespace boost { namespace python
{
template <>
struct has_back_reference<Y>
: mpl::true_
{
};
template <>
struct has_back_reference<Z>
: mpl::true_
{
};
}}
// prove that back_references get initialized with the right PyObject*
object y_identity(back_reference<Y const&> y)
{
return y.source();
}
// prove that back_references contain the right value
bool y_equality(back_reference<Y const&> y1, Y const& y2)
{
return &y1.get() == &y2;
}
BOOST_PYTHON_MODULE(back_reference_ext)
{
def("copy_Y", copy_Y, return_value_policy<copy_const_reference>());
def("copy_Z", copy_Z, return_value_policy<copy_const_reference>());
def("x_instances", &X::count);
class_<Y>("Y", init<int>())
.def("value", &Y::value)
.def("set", &Y::set)
;
class_<Z,std::auto_ptr<Z> >("Z", init<int>())
.def("value", &Z::value)
.def("set", &Z::set)
;
def("y_identity", y_identity);
def("y_equality", y_equality);
}
#include "module_tail.cpp"

View File

@@ -1,34 +0,0 @@
# 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)
'''
>>> from back_reference_ext import *
>>> y = Y(3)
>>> z = Z(4)
>>> x_instances()
2
>>> y2 = copy_Y(y)
>>> x_instances()
3
>>> z2 = copy_Z(z)
>>> x_instances()
4
>>> assert y_identity(y) is y
>>> y_equality(y, y)
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
status = run()[0]
if (status == 0): print "Done."
sys.exit(status)

View File

@@ -1,62 +0,0 @@
// Copyright David Abrahams 2002.
// 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/bases.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/same_traits.hpp>
struct A;
struct B;
template <class X, class Y, class Z>
struct choose_bases
: boost::python::detail::select_bases<
X
, typename boost::python::detail::select_bases<
Y
, typename boost::python::detail::select_bases<Z>::type
>::type>
{
};
int main()
{
BOOST_STATIC_ASSERT((boost::python::detail::specifies_bases<
boost::python::bases<A,B> >::value));
BOOST_STATIC_ASSERT((!boost::python::detail::specifies_bases<
boost::python::bases<A,B>& >::value));
BOOST_STATIC_ASSERT((!boost::python::detail::specifies_bases<
void* >::value));
BOOST_STATIC_ASSERT((!boost::python::detail::specifies_bases<
int >::value));
BOOST_STATIC_ASSERT((!boost::python::detail::specifies_bases<
int[5] >::value));
typedef boost::python::detail::select_bases<
int
, boost::python::detail::select_bases<char*>::type > collected1;
BOOST_STATIC_ASSERT((boost::is_same<collected1::type,boost::python::bases<> >::value));
BOOST_STATIC_ASSERT((boost::is_same<choose_bases<int,char*,long>::type,boost::python::bases<> >::value));
typedef boost::python::detail::select_bases<
int
, boost::python::detail::select_bases<
boost::python::bases<A,B>
, boost::python::detail::select_bases<
A
>::type
>::type
> collected2;
BOOST_STATIC_ASSERT((boost::is_same<collected2::type,boost::python::bases<A,B> >::value));
BOOST_STATIC_ASSERT((boost::is_same<choose_bases<int,boost::python::bases<A,B>,long>::type,boost::python::bases<A,B> >::value));
return 0;
}

View File

@@ -1,54 +0,0 @@
// 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.hpp>
#include <iostream>
using namespace boost::python;
using namespace boost;
struct Product {};
typedef shared_ptr<Product> ProductPtr;
struct Creator
{
virtual ~Creator() {}
virtual ProductPtr create() = 0;
};
struct Factory
{
void reg(Creator* c) { mC = c; }
ProductPtr create()
{
std::cout << "Name: " << (typeid(*mC)).name() << std::endl;
return mC->create();
}
private:
Creator* mC;
};
struct CreatorWrap : public Creator
{
CreatorWrap(PyObject* self) : mSelf(self) {}
ProductPtr create() { return call_method<ProductPtr>(mSelf, "create"); }
PyObject* mSelf;
};
BOOST_PYTHON_MODULE(ben_scott1_ext)
{
class_<Product, ProductPtr>("Product");
class_<Creator, CreatorWrap, noncopyable>("Creator")
.def("create", &CreatorWrap::create)
;
class_<Factory>("Factory")
.def("reg", &Factory::reg, with_custodian_and_ward<1,2>())
.def("create", &Factory::create)
;
}
#include "../test/module_tail.cpp"

View File

@@ -1,17 +0,0 @@
# 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)
# This regression test checks that call_method<T>(...) where T is a
# non-reference, non-pointer type that happens to be held inside the
# result object (and thus is found as an lvalue) works.
from ben_scott1_ext import *
class CreatorImpl(Creator):
def create(self):
return Product()
factory = Factory()
c = CreatorImpl()
factory.reg(c)
a = factory.create()

View File

@@ -1,40 +0,0 @@
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/reference_existing_object.hpp>
#include <boost/python/return_value_policy.hpp>
struct A {};
struct V
{
virtual void f() = 0;
const A* inside() {return &a;}
A a;
};
const A* outside(const V& v) {return &v.a;}
BOOST_PYTHON_MODULE(bienstman1_ext)
{
using namespace boost::python;
using boost::shared_ptr;
using boost::python::return_value_policy;
using boost::python::reference_existing_object;
class_<A>("A");
class_<V, boost::noncopyable>("V", no_init)
.def("inside", &V::inside,
return_value_policy<reference_existing_object>())
.def("outside", outside,
return_value_policy<reference_existing_object>())
;
}

View File

@@ -1,23 +0,0 @@
# 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)
'''
# Try to reproduce a Numeric interaction bug if Numeric is installed.
>>> from bienstman1_ext import *
>>> try: from Numeric import *
... except: pass
'''
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)

View File

@@ -1,28 +0,0 @@
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
struct C {};
struct D {};
struct E
{
const D fe (const C&) {return D();}
const D fe2(const C&, const C&) {return D();}
};
BOOST_PYTHON_MODULE(bienstman2_ext)
{
using namespace boost::python;
class_<C>("C");
class_<D>("D");
class_<E>("E")
.def("fe", &E::fe) // this compiles.
.def("fe2", &E::fe2) // this doesn't... well, now it does ;-)
;
}

View File

@@ -1,20 +0,0 @@
# 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)
'''
>>> import bienstman2_ext
'''
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)

View File

@@ -1,25 +0,0 @@
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
struct V
{
virtual void f() = 0;
};
struct B
{
B(const V&) {}
};
BOOST_PYTHON_MODULE(bienstman3_ext)
{
using namespace boost::python;
class_<V, boost::noncopyable>("V", no_init);
class_<B>("B", init<const V&>());
}

View File

@@ -1,30 +0,0 @@
# 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)
'''
>>> from bienstman3_ext import *
>>> try:
... V()
... except RuntimeError, x:
... print x
... else:
... print 'expected an exception'
...
This class cannot be instantiated from Python
'''
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)

View File

@@ -1,39 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/implicit.hpp>
#include <boost/mpl/list.hpp>
struct Type1 {};
struct Term {Term(Type1 const&) {} };
struct Expression {void add(Term const&) {} };
BOOST_PYTHON_MODULE(bienstman4_ext)
{
using namespace boost::python;
using boost::mpl::list;
implicitly_convertible<Type1,Term>();
class_<Expression>("Expression")
.def("add", &Expression::add)
;
class_<Type1>("T1")
;
class_<Term>("Term", init<Type1&>())
;
Type1 t1;
Expression e;
e.add(t1);
}

View File

@@ -1,23 +0,0 @@
# 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)
'''
>>> from bienstman4_ext import *
>>> t1 = T1()
>>> e = Expression()
>>> e.add(t1)
'''
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)

View File

@@ -1,23 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/mpl/list.hpp>
#include <complex>
struct M {M(const std::complex<double>&) {} };
BOOST_PYTHON_MODULE(bienstman5_ext)
{
using namespace boost::python;
class_<M>("M", init<std::complex<double> const&>())
;
}

View File

@@ -1,21 +0,0 @@
# 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)
'''
>>> from bienstman5_ext import *
>>> m = M(1j)
'''
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)

View File

@@ -1,33 +0,0 @@
// Copyright David Abrahams 2002.
// 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/detail/wrap_python.hpp>
#include <boost/python/borrowed.hpp>
#include <boost/static_assert.hpp>
using namespace boost::python;
template <class T>
void assert_borrowed_ptr(T const& x)
{
BOOST_STATIC_ASSERT(boost::python::detail::is_borrowed_ptr<T>::value);
}
template <class T>
void assert_not_borrowed_ptr(T const& x)
{
BOOST_STATIC_ASSERT(!boost::python::detail::is_borrowed_ptr<T>::value);
}
int main()
{
assert_borrowed_ptr(borrowed((PyObject*)0));
assert_borrowed_ptr(borrowed((PyTypeObject*)0));
assert_borrowed_ptr((detail::borrowed<PyObject> const*)0);
assert_borrowed_ptr((detail::borrowed<PyObject> volatile*)0);
assert_borrowed_ptr((detail::borrowed<PyObject> const volatile*)0);
assert_not_borrowed_ptr((PyObject*)0);
assert_not_borrowed_ptr(0);
return 0;
}

View File

@@ -1,149 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/ref.hpp>
#include <boost/python/ptr.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/reference_existing_object.hpp>
#include <boost/python/call.hpp>
#include <boost/python/object.hpp>
#include <boost/assert.hpp>
using namespace boost::python;
BOOST_STATIC_ASSERT(converter::is_object_manager<handle<> >::value);
int apply_int_int(PyObject* f, int x)
{
return call<int>(f, x);
}
void apply_void_int(PyObject* f, int x)
{
call<void>(f, x);
}
struct X
{
explicit X(int x) : x(x), magic(7654321) { ++counter; }
X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
~X() { BOOST_ASSERT(magic == 7654321); magic = 6666666; x = 9999; --counter; }
void set(int x) { BOOST_ASSERT(magic == 7654321); this->x = x; }
int value() const { BOOST_ASSERT(magic == 7654321); return x; }
static int count() { return counter; }
private:
void operator=(X const&);
private:
int x;
long magic;
static int counter;
};
X apply_X_X(PyObject* f, X x)
{
return call<X>(f, x);
}
void apply_void_X_ref(PyObject* f, X& x)
{
call<void>(f, boost::ref(x));
}
X& apply_X_ref_handle(PyObject* f, handle<> obj)
{
return call<X&>(f, obj);
}
X* apply_X_ptr_handle_cref(PyObject* f, handle<> const& obj)
{
return call<X*>(f, obj);
}
void apply_void_X_cref(PyObject* f, X const& x)
{
call<void>(f, boost::cref(x));
}
void apply_void_X_ptr(PyObject* f, X* x)
{
call<void>(f, ptr(x));
}
void apply_void_X_deep_ptr(PyObject* f, X* x)
{
call<void>(f, x);
}
char const* apply_cstring_cstring(PyObject* f, char const* s)
{
return call<char const*>(f, s);
}
char const* apply_cstring_pyobject(PyObject* f, PyObject* s)
{
return call<char const*>(f, borrowed(s));
}
char apply_char_char(PyObject* f, char c)
{
return call<char>(f, c);
}
char const* apply_to_string_literal(PyObject* f)
{
return call<char const*>(f, "hello, world");
}
handle<> apply_to_own_type(handle<> x)
{
// Tests that we can return handle<> from a callback and that we
// can pass arbitrary handle<T>.
return call<handle<> >(x.get(), type_handle(borrowed(x->ob_type)));
}
object apply_object_object(PyObject* f, object x)
{
return call<object>(f, x);
}
int X::counter;
BOOST_PYTHON_MODULE(callbacks_ext)
{
def("apply_object_object", apply_object_object);
def("apply_to_own_type", apply_to_own_type);
def("apply_int_int", apply_int_int);
def("apply_void_int", apply_void_int);
def("apply_X_X", apply_X_X);
def("apply_void_X_ref", apply_void_X_ref);
def("apply_void_X_cref", apply_void_X_cref);
def("apply_void_X_ptr", apply_void_X_ptr);
def("apply_void_X_deep_ptr", apply_void_X_deep_ptr);
def("apply_X_ptr_handle_cref", apply_X_ptr_handle_cref
, return_value_policy<reference_existing_object>());
def("apply_X_ref_handle", apply_X_ref_handle
, return_value_policy<reference_existing_object>());
def("apply_cstring_cstring", apply_cstring_cstring);
def("apply_cstring_pyobject", apply_cstring_pyobject);
def("apply_char_char", apply_char_char);
def("apply_to_string_literal", apply_to_string_literal);
class_<X>("X", init<int>())
.def(init<X const&>())
.def("value", &X::value)
.def("set", &X::set)
;
def("x_count", &X::count);
}
#include "module_tail.cpp"

View File

@@ -1,147 +0,0 @@
# 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)
'''
>>> from callbacks_ext import *
>>> def double(x):
... return x + x
...
>>> apply_int_int(double, 42)
84
>>> apply_void_int(double, 42)
>>> def identity(x):
... return x
Once we have array conversion support, this test will fail. Er,
succeed<wink>:
>>> try: apply_to_string_literal(identity)
... except ReferenceError: pass # expected
... else: print 'expected an exception!'
>>> try: apply_X_ref_handle(lambda ignored:X(42), None)
... except ReferenceError: pass # expected
... else: print 'expected an exception!'
>>> x = X(42)
>>> x.y = X(7)
>>> apply_X_ref_handle(lambda z:z.y, x).value()
7
>>> x = apply_X_X(identity, X(42))
>>> x.value()
42
>>> x_count()
1
>>> del x
>>> x_count()
0
>>> def increment(x):
... x.set(x.value() + 1)
...
>>> x = X(42)
>>> apply_void_X_ref(increment, x)
>>> x.value()
43
>>> apply_void_X_cref(increment, x)
>>> x.value() # const-ness is not respected, sorry!
44
>>> last_x = 1
>>> def decrement(x):
... global last_x
... last_x = x
... if x is not None:
... x.set(x.value() - 1)
>>> apply_void_X_ptr(decrement, x)
>>> x.value()
43
>>> last_x.value()
43
>>> increment(last_x)
>>> x.value()
44
>>> last_x.value()
44
>>> apply_void_X_ptr(decrement, None)
>>> assert last_x is None
>>> x.value()
44
>>> last_x = 1
>>> apply_void_X_deep_ptr(decrement, None)
>>> assert last_x is None
>>> x.value()
44
>>> apply_void_X_deep_ptr(decrement, x)
>>> x.value()
44
>>> last_x.value()
43
>>> y = apply_X_ref_handle(identity, x)
>>> assert y.value() == x.value()
>>> increment(x)
>>> assert y.value() == x.value()
>>> y = apply_X_ptr_handle_cref(identity, x)
>>> assert y.value() == x.value()
>>> increment(x)
>>> assert y.value() == x.value()
>>> y = apply_X_ptr_handle_cref(identity, None)
>>> y
>>> def new_x(ignored):
... return X(666)
...
>>> try: apply_X_ref_handle(new_x, 1)
... except ReferenceError: pass
... else: print 'no error'
>>> try: apply_X_ptr_handle_cref(new_x, 1)
... except ReferenceError: pass
... else: print 'no error'
>>> try: apply_cstring_cstring(identity, 'hello')
... except ReferenceError: pass
... else: print 'no error'
>>> apply_char_char(identity, 'x')
'x'
>>> apply_cstring_pyobject(identity, 'hello')
'hello'
>>> apply_cstring_pyobject(identity, None)
>>> apply_char_char(identity, 'x')
'x'
>>> assert apply_to_own_type(identity) is type(identity)
>>> assert apply_object_object(identity, identity) is identity
'''
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)

View File

@@ -1,74 +0,0 @@
// Copyright David Abrahams 2005. 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/utility.hpp>
/* Non-modifiable definitions */
class basic {
public:
basic() { name = "cltree.basic"; }
std::string repr() { return name+"()"; }
protected:
std::string name;
};
class constant: public basic {
public:
constant() { name = "cltree.constant"; }
};
class symbol: public basic {
public:
symbol() { name = "cltree.symbol"; }
};
class variable: public basic {
public:
variable() { name = "cltree.variable"; }
};
/* EOF: Non-modifiable definitions */
class symbol_wrapper: public symbol {
public:
symbol_wrapper(PyObject* /*self*/): symbol() {
name = "cltree.wrapped_symbol";
}
};
class variable_wrapper: public variable {
public:
variable_wrapper(PyObject* /*self*/): variable() {
name = "cltree.wrapped_variable";
}
// This constructor is introduced only because cannot use
// boost::noncopyable, see below.
variable_wrapper(PyObject* /*self*/,variable v): variable(v) {}
};
BOOST_PYTHON_MODULE(cltree)
{
boost::python::class_<basic>("basic")
.def("__repr__",&basic::repr)
;
boost::python::class_<constant, boost::python::bases<basic>, boost::noncopyable>("constant")
;
boost::python::class_<symbol, symbol_wrapper, boost::noncopyable>("symbol")
;
boost::python::class_<variable, boost::python::bases<basic>, variable_wrapper>("variable")
;
}
#include "module_tail.cpp"

View File

@@ -1,38 +0,0 @@
// Copyright David Abrahams 2001.
// 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)
#ifndef COMPLICATED_DWA20011215_HPP
# define COMPLICATED_DWA20011215_HPP
# include <iostream>
# include "simple_type.hpp"
struct complicated
{
complicated(simple const&, int = 0);
~complicated();
int get_n() const;
char* s;
int n;
};
inline complicated::complicated(simple const&s, int n)
: s(s.s), n(n)
{
std::cout << "constructing complicated: " << this->s << ", " << n << std::endl;
}
inline complicated::~complicated()
{
std::cout << "destroying complicated: " << this->s << ", " << n << std::endl;
}
inline int complicated::get_n() const
{
return n;
}
#endif // COMPLICATED_DWA20011215_HPP

View File

@@ -1,30 +0,0 @@
/* Copyright 2004 Jonathan Brandmeyer
* Use, modification and distribution are subject to 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)
*
* The purpose of this test is to determine if a function can be called from
* Python with a const value type as an argument, and whether or not the
* presence of a prototype without the cv-qualifier will work around the
* compiler's bug.
*/
#include <boost/python.hpp>
#include <boost/type_traits/broken_compiler_spec.hpp>
using namespace boost::python;
BOOST_TT_BROKEN_COMPILER_SPEC( object )
#if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
bool accept_const_arg( object );
#endif
bool accept_const_arg( const object )
{
return true;
}
BOOST_PYTHON_MODULE( const_argument_ext )
{
def( "accept_const_arg", accept_const_arg );
}

View File

@@ -1,23 +0,0 @@
# Copyright Jonathan Brandmeyer, 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)
"""
>>> from const_argument_ext import *
>>> accept_const_arg(1)
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
status = run()[0]
if (status == 0): print "Done."
sys.exit(status)

View File

@@ -1,23 +0,0 @@
// Copyright David Abrahams 2003.
// 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/detail/copy_ctor_mutates_rhs.hpp>
#include <boost/static_assert.hpp>
#include <memory>
#include <string>
struct foo
{
operator std::auto_ptr<int>&() const;
};
int main()
{
using namespace boost::python::detail;
BOOST_STATIC_ASSERT(!copy_ctor_mutates_rhs<int>::value);
BOOST_STATIC_ASSERT(copy_ctor_mutates_rhs<std::auto_ptr<int> >::value);
BOOST_STATIC_ASSERT(!copy_ctor_mutates_rhs<std::string>::value);
BOOST_STATIC_ASSERT(!copy_ctor_mutates_rhs<foo>::value);
return 0;
}

View File

@@ -1,18 +0,0 @@
// Copyright (C) 2003 Rational Discovery LLC
// 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.hpp>
namespace python = boost::python;
void tossit(){
PyErr_SetString(PyExc_IndexError,"a-blah!");
throw python::error_already_set();
}
BOOST_PYTHON_MODULE(crossmod_exception_a)
{
python::def("tossit",tossit);
}

View File

@@ -1,18 +0,0 @@
// Copyright (C) 2003 Rational Discovery LLC
// 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.hpp>
namespace python = boost::python;
void tossit(){
PyErr_SetString(PyExc_IndexError,"b-blah!");
throw python::error_already_set();
}
BOOST_PYTHON_MODULE(crossmod_exception_b)
{
python::def("tossit",tossit);
}

View File

@@ -1,16 +0,0 @@
# -*- coding: iso-latin-1 -*-
# Copyright Gottfried Ganßauge 2006.
# 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)
if __name__ == '__main__':
print "running..."
import crossmod_opaque_a
import crossmod_opaque_b
crossmod_opaque_a.get()
crossmod_opaque_b.get()
print "Done."

View File

@@ -1,26 +0,0 @@
// Copyright Gottfried Ganßauge 2006.
// 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/return_opaque_pointer.hpp>
# include <boost/python/def.hpp>
# include <boost/python/module.hpp>
# include <boost/python/return_value_policy.hpp>
typedef struct opaque_ *opaque;
opaque the_op = ((opaque) 0x47110815);
opaque get() { return the_op; }
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(opaque_)
namespace bpl = boost::python;
BOOST_PYTHON_MODULE(crossmod_opaque_a)
{
bpl::def (
"get",
&::get,
bpl::return_value_policy<bpl::return_opaque_pointer>());
}

View File

@@ -1,26 +0,0 @@
// Copyright Gottfried Ganßauge 2006.
// 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/return_opaque_pointer.hpp>
# include <boost/python/def.hpp>
# include <boost/python/module.hpp>
# include <boost/python/return_value_policy.hpp>
typedef struct opaque_ *opaque;
opaque the_op = ((opaque) 0x47110815);
opaque get() { return the_op; }
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(opaque_)
namespace bpl = boost::python;
BOOST_PYTHON_MODULE(crossmod_opaque_b)
{
bpl::def (
"get",
&::get,
bpl::return_value_policy<bpl::return_opaque_pointer>());
}

View File

@@ -1,131 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/return_by_value.hpp>
#include "test_class.hpp"
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
# include <iostream> // works around a KCC intermediate code generation bug
#endif
using namespace boost::python;
typedef test_class<> X;
struct Y : test_class<1>
{
Y(int v) : test_class<1>(v) {}
Y& operator=(Y const& rhs) { x = rhs.x; return *this; }
bool q;
};
double get_fair_value(X const& x) { return x.value(); }
struct VarBase
{
VarBase(std::string name_) : name(name_) {}
std::string const name;
std::string get_name1() const { return name; }
};
struct Var : VarBase
{
Var(std::string name_) : VarBase(name_), value(), name2(name.c_str()), y(6) {}
std::string const& get_name2() const { return name; }
float value;
char const* name2;
Y y;
static int static1;
static Y static2;
};
int Var::static1 = 0;
Y Var::static2(0);
// Compilability regression tests
namespace
{
struct trivial
{
trivial() : value(123) {}
double value;
};
struct Color3
{
static const Color3 black;
};
const Color3 Color3::black
#if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
= {}
#endif
;
void compilability_test()
{
class_<trivial>("trivial")
.add_property("property", make_getter(&trivial::value, return_value_policy<return_by_value>()))
.def_readonly("readonly", &trivial::value)
;
class_< Color3 >("Color3", init< const Color3 & >())
.def_readonly("BLACK", &Color3::black) // line 17
;
}
}
BOOST_PYTHON_MODULE(data_members_ext)
{
class_<X>("X", init<int>())
.def("value", &X::value)
.def("set", &X::set)
.def_readonly("x", &X::x)
.add_property("fair_value", get_fair_value)
;
class_<Y>("Y", init<int>())
.def("value", &Y::value)
.def("set", &Y::set)
.def_readwrite("x", &Y::x)
.def_readwrite("q", &Y::q)
;
class_<Var>("Var", init<std::string>())
.def_readonly("name", &Var::name)
.def_readonly("name2", &Var::name2)
.def_readwrite("value", &Var::value)
.def_readonly("y", &Var::y)
// Test return_by_value for plain values and for
// pointers... return_by_value was implemented as a
// side-effect of implementing data member support, so it made
// sense to add the test here.
.def("get_name1", &Var::get_name1, return_value_policy<return_by_value>())
.def("get_name2", &Var::get_name2, return_value_policy<return_by_value>())
.add_property("name3", &Var::get_name1)
// Test static data members
.def_readonly("ro1a", &Var::static1)
.def_readonly("ro1b", Var::static1)
.def_readwrite("rw1a", &Var::static1)
.def_readwrite("rw1b", Var::static1)
.def_readonly("ro2a", &Var::static2)
.def_readonly("ro2b", Var::static2)
.def_readwrite("rw2a", &Var::static2)
.def_readwrite("rw2b", Var::static2)
;
}
#include "module_tail.cpp"

View File

@@ -1,215 +0,0 @@
# 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)
'''
>>> from data_members_ext import *
---- Test static data members ---
>>> v = Var('slim shady')
>>> Var.ro2a.x
0
>>> Var.ro2b.x
0
>>> Var.rw2a.x
0
>>> Var.rw2b.x
0
>>> v.ro2a.x
0
>>> v.ro2b.x
0
>>> v.rw2a.x
0
>>> v.rw2b.x
0
>>> Var.rw2a.x = 777
>>> Var.ro2a.x
777
>>> Var.ro2b.x
777
>>> Var.rw2a.x
777
>>> Var.rw2b.x
777
>>> v.ro2a.x
777
>>> v.ro2b.x
777
>>> v.rw2a.x
777
>>> v.rw2b.x
777
>>> Var.rw2b = Y(888)
>>> y = Y(99)
>>> y.q = True
>>> y.q
True
>>> y.q = False
>>> y.q
False
>>> Var.ro2a.x
888
>>> Var.ro2b.x
888
>>> Var.rw2a.x
888
>>> Var.rw2b.x
888
>>> v.ro2a.x
888
>>> v.ro2b.x
888
>>> v.rw2a.x
888
>>> v.rw2b.x
888
>>> v.rw2b.x = 999
>>> Var.ro2a.x
999
>>> Var.ro2b.x
999
>>> Var.rw2a.x
999
>>> Var.rw2b.x
999
>>> v.ro2a.x
999
>>> v.ro2b.x
999
>>> v.rw2a.x
999
>>> v.rw2b.x
999
>>> Var.ro1a
0
>>> Var.ro1b
0
>>> Var.rw1a
0
>>> Var.rw1b
0
>>> v.ro1a
0
>>> v.ro1b
0
>>> v.rw1a
0
>>> v.rw1b
0
>>> Var.rw1a = 777
>>> Var.ro1a
777
>>> Var.ro1b
777
>>> Var.rw1a
777
>>> Var.rw1b
777
>>> v.ro1a
777
>>> v.ro1b
777
>>> v.rw1a
777
>>> v.rw1b
777
>>> Var.rw1b = 888
>>> Var.ro1a
888
>>> Var.ro1b
888
>>> Var.rw1a
888
>>> Var.rw1b
888
>>> v.ro1a
888
>>> v.ro1b
888
>>> v.rw1a
888
>>> v.rw1b
888
>>> v.rw1b = 999
>>> Var.ro1a
999
>>> Var.ro1b
999
>>> Var.rw1a
999
>>> Var.rw1b
999
>>> v.ro1a
999
>>> v.ro1b
999
>>> v.rw1a
999
>>> v.rw1b
999
-----------------
>>> x = X(42)
>>> x.x
42
>>> try: x.x = 77
... except AttributeError: pass
... else: print 'no error'
>>> x.fair_value
42.0
>>> y = Y(69)
>>> y.x
69
>>> y.x = 77
>>> y.x
77
>>> v = Var("pi")
>>> v.value = 3.14
>>> v.name
'pi'
>>> v.name2
'pi'
>>> v.get_name1()
'pi'
>>> v.get_name2()
'pi'
>>> v.y.x
6
>>> v.y.x = -7
>>> v.y.x
-7
>>> v.name3
'pi'
'''
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)

View File

@@ -1,176 +0,0 @@
// Copyright David Abrahams 2002.
// 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/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/list.hpp>
#include <boost/python/overloads.hpp>
#include <boost/python/return_internal_reference.hpp>
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
# include <iostream> // works around a KCC intermediate code generation bug
#endif
using namespace boost::python;
#if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580))
# define make_tuple boost::python::make_tuple
#endif
char const* const format = "int(%s); char(%s); string(%s); double(%s); ";
///////////////////////////////////////////////////////////////////////////////
//
// Overloaded functions
//
///////////////////////////////////////////////////////////////////////////////
object
bar(int a, char b, std::string c, double d)
{
return format % make_tuple(a, b, c, d);
}
object
bar(int a, char b, std::string c)
{
return format % make_tuple(a, b, c, 0.0);
}
object
bar(int a, char b)
{
return format % make_tuple(a, b, "default", 0.0);
}
object
bar(int a)
{
return format % make_tuple(a, 'D', "default", 0.0);
}
BOOST_PYTHON_FUNCTION_OVERLOADS(bar_stubs, bar, 1, 4)
///////////////////////////////////////////////////////////////////////////////
//
// Functions with default arguments
//
///////////////////////////////////////////////////////////////////////////////
object
foo(int a, char b = 'D', std::string c = "default", double d = 0.0)
{
return format % make_tuple(a, b, c, d);
}
BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs, foo, 1, 4)
///////////////////////////////////////////////////////////////////////////////
//
// Overloaded member functions with default arguments
//
///////////////////////////////////////////////////////////////////////////////
struct Y {
Y() {}
object
get_state() const
{
return format % make_tuple(a, b, c, d);
}
int a; char b; std::string c; double d;
};
struct X {
X() {}
X(int a, char b = 'D', std::string c = "constructor", double d = 0.0)
: state(format % make_tuple(a, b, c, d))
{}
X(std::string s, bool b)
: state("Got exactly two arguments from constructor: string(%s); bool(%s); " % make_tuple(s, b*1))
{}
object
bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const
{
return format % make_tuple(a, b, c, d);
}
Y const&
bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0)
{
// tests zero arg member function and return_internal_reference policy
y.a = a;
y.b = b;
y.c = c;
y.d = d;
return y;
}
object
foo(int a, bool b=false) const
{
return "int(%s); bool(%s); " % make_tuple(a, b*1);
}
object
foo(std::string a, bool b=false) const
{
return "string(%s); bool(%s); " % make_tuple(a, b*1);
}
object
foo(list a, list b, bool c=false) const
{
return "list(%s); list(%s); bool(%s); " % make_tuple(a, b, c*1);
}
object
get_state() const
{
return state;
}
Y y;
object state;
};
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs, bar, 1, 4)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs2, bar2, 0, 4)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_2_stubs, foo, 1, 2)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3)
///////////////////////////////////////////////////////////////////////////////
BOOST_PYTHON_MODULE(defaults_ext)
{
def("foo", foo, foo_stubs());
def("bar", (object(*)(int, char, std::string, double))0, bar_stubs());
class_<Y>("Y", init<>("doc of Y init")) // this should work
.def("get_state", &Y::get_state)
;
class_<X>("X")
.def(init<int, optional<char, std::string, double> >("doc of init"))
.def(init<std::string, bool>()[default_call_policies()]) // what's a good policy here?
.def("get_state", &X::get_state)
.def("bar", &X::bar, X_bar_stubs())
.def("bar2", &X::bar2, X_bar_stubs2("doc of X::bar2")[return_internal_reference<>()])
.def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs())
.def("foo", (object(X::*)(int, bool) const)0, X_foo_2_stubs())
.def("foo", (object(X::*)(list, list, bool) const)0, X_foo_3_stubs())
;
}
#include "module_tail.cpp"

View File

@@ -1,153 +0,0 @@
# 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)
"""
# Use builtin True/False when available:
>>> try:
... assert(True == 1)
... except:
... True = 1
... False = 0
>>> from defaults_ext import *
>>> bar(1)
'int(1); char(D); string(default); double(0.0); '
>>> bar(2, 'X')
'int(2); char(X); string(default); double(0.0); '
>>> bar(3, 'Y', "Hello World")
'int(3); char(Y); string(Hello World); double(0.0); '
>>> bar(4, 'Z', "Hi There", 3.3)
'int(4); char(Z); string(Hi There); double(3.3); '
>>> foo(1)
'int(1); char(D); string(default); double(0.0); '
>>> foo(2, 'X')
'int(2); char(X); string(default); double(0.0); '
>>> foo(3, 'Y', "Hello World")
'int(3); char(Y); string(Hello World); double(0.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.0); '
>>> x.bar(2, 'X')
'int(2); char(X); string(default); double(0.0); '
>>> x.bar(3, 'Y', "Hello World")
'int(3); char(Y); string(Hello World); double(0.0); '
>>> x.bar(4, 'Z', "Hi There", 3.3)
'int(4); char(Z); string(Hi There); double(3.3); '
>>> x.foo(5)
'int(5); bool(0); '
>>> x.foo(6, 0)
'int(6); bool(0); '
>>> x.foo(7, 1)
'int(7); bool(1); '
>>> x.foo("A")
'string(A); bool(0); '
>>> x.foo("B", False)
'string(B); bool(0); '
>>> x.foo("C", True)
'string(C); bool(1); '
>>> x.foo([0,1,2], [2,3,4])
'list([0, 1, 2]); list([2, 3, 4]); bool(0); '
>>> x.foo([0,1,2], [2,3,4], False)
'list([0, 1, 2]); list([2, 3, 4]); bool(0); '
>>> x.foo([0,1,2], [2,3,4], True)
'list([0, 1, 2]); list([2, 3, 4]); bool(1); '
>>> x = X(1)
>>> x.get_state()
'int(1); char(D); string(constructor); double(0.0); '
>>> x = X(1, 'X')
>>> x.get_state()
'int(1); char(X); string(constructor); double(0.0); '
>>> x = X(1, 'X', "Yabadabadoo")
>>> x.get_state()
'int(1); char(X); string(Yabadabadoo); double(0.0); '
>>> x = X(1, 'X', "Phoenix", 3.65)
>>> x.get_state()
'int(1); char(X); string(Phoenix); double(3.65); '
>>> x.bar2().get_state()
'int(0); char(D); string(default); double(0.0); '
>>> x.bar2(1).get_state()
'int(1); char(D); string(default); double(0.0); '
>>> x.bar2(1, 'K').get_state()
'int(1); char(K); string(default); double(0.0); '
>>> x.bar2(1, 'K', "Kim").get_state()
'int(1); char(K); string(Kim); double(0.0); '
>>> x.bar2(1, 'K', "Kim", 9.9).get_state()
'int(1); char(K); string(Kim); double(9.9); '
>>> x = X("Phoenix", 1)
>>> x.get_state()
'Got exactly two arguments from constructor: string(Phoenix); bool(1); '
>>> def selected_doc(obj, *args):
... doc = obj.__doc__.splitlines()
... return "\\n".join(["|"+doc[i] for i in args])
>>> print selected_doc(X.__init__, 0, 2, 4, 6, 8, 9, 10, 12)
|C++ signature:
|C++ signature:
|C++ signature:
|C++ signature:
|
|doc of init
|C++ signature:
|C++ signature:
>>> print selected_doc(Y.__init__, 0, 1)
|doc of Y init
|C++ signature:
>>> print selected_doc(X.bar2, 0, 2, 4, 6, 8, 9, 10)
|C++ signature:
|C++ signature:
|C++ signature:
|C++ signature:
|
|doc of X::bar2
|C++ signature:
"""
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)

View File

@@ -1,55 +0,0 @@
// 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/detail/destroy.hpp>
#include <boost/detail/lightweight_test.hpp>
int count;
int marks[] = {
-1
, -1, -1
, -1, -1, -1, -1
, -1
};
int* kills = marks;
struct foo
{
foo() : n(count++) {}
~foo()
{
*kills++ = n;
}
int n;
};
void assert_destructions(int n)
{
for (int i = 0; i < n; ++i)
BOOST_TEST(marks[i] == i);
BOOST_TEST(marks[n] == -1);
}
int main()
{
assert_destructions(0);
typedef int a[2];
foo* f1 = new foo;
boost::python::detail::destroy_referent<foo const volatile&>(f1);
assert_destructions(1);
foo* f2 = new foo[2];
typedef foo x[2];
boost::python::detail::destroy_referent<x const&>(f2);
assert_destructions(3);
typedef foo y[2][2];
x* f3 = new y;
boost::python::detail::destroy_referent<y&>(f3);
assert_destructions(7);
return boost::report_errors();
}

View File

@@ -1,88 +0,0 @@
// 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/module.hpp>
#include <boost/assert.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/dict.hpp>
#include <exception>
#include <string>
using namespace boost::python;
object new_dict()
{
return dict();
}
object data_dict()
{
dict tmp1;
tmp1["key1"] = "value1";
dict tmp2;
tmp2["key2"] = "value2";
tmp1[1] = tmp2;
return tmp1;
}
object dict_from_sequence(object sequence)
{
return dict(sequence);
}
object dict_keys(dict data)
{
return data.keys();
}
object dict_values(dict data)
{
return data.values();
}
object dict_items(dict data)
{
return data.items();
}
void work_with_dict(dict data1, dict data2)
{
if (!data1.has_key("k1")) {
throw std::runtime_error("dict does not have key 'k1'");
}
data1.update(data2);
}
void test_templates(object print)
{
std::string key = "key";
dict tmp;
tmp[1] = "a test string";
print(tmp.get(1));
//print(tmp[1]);
tmp[1.5] = 13;
print(tmp.get(1.5));
print(tmp.get(44));
print(tmp);
print(tmp.get(2,"default"));
print(tmp.setdefault(3,"default"));
BOOST_ASSERT(!tmp.has_key(key));
//print(tmp[3]);
}
BOOST_PYTHON_MODULE(dict_ext)
{
def("new_dict", new_dict);
def("data_dict", data_dict);
def("dict_keys", dict_keys);
def("dict_values", dict_values);
def("dict_items", dict_items);
def("dict_from_sequence", dict_from_sequence);
def("work_with_dict", work_with_dict);
def("test_templates", test_templates);
}

View File

@@ -1,45 +0,0 @@
# 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)
"""
>>> from dict_ext import *
>>> def printer(*args):
... for x in args: print x,
... print
...
>>> print new_dict()
{}
>>> print data_dict()
{1: {'key2': 'value2'}, 'key1': 'value1'}
>>> tmp = data_dict()
>>> print dict_keys(tmp)
[1, 'key1']
>>> print dict_values(tmp)
[{'key2': 'value2'}, 'value1']
>>> print dict_items(tmp)
[(1, {'key2': 'value2'}), ('key1', 'value1')]
>>> print dict_from_sequence([(1,1),(2,2),(3,3)])
{1: 1, 2: 2, 3: 3}
>>> test_templates(printer)
a test string
13
None
{1.5: 13, 1: 'a test string'}
default
default
"""
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)

View File

@@ -1,97 +0,0 @@
// Copyright David Abrahams 2002.
// 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/operators.hpp>
#include <boost/python/class.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/docstring_options.hpp>
#include <boost/python/scope.hpp>
#include <boost/python/manage_new_object.hpp>
#include "test_class.hpp"
// Just use math.h here; trying to use std::pow() causes too much
// trouble for non-conforming compilers and libraries.
#include <math.h>
using namespace boost::python;
typedef test_class<> X;
X* create(int x)
{
return new X(x);
}
unsigned long fact(unsigned long n)
{
return n <= 1 ? n : n * fact(n - 1);
}
BOOST_PYTHON_MODULE(docstring_ext)
{
scope().attr("__doc__") =
"A simple test module for documentation strings\n"
"Exercised by docstring.py"
;
class_<X>("X",
"A simple class wrapper around a C++ int\n"
"includes some error-checking"
, init<int>(
"this is the __init__ function\n"
"its documentation has two lines."
)
)
.def("value", &X::value,
"gets the value of the object")
.def( "value", &X::value,
"also gets the value of the object")
;
def("create", create, return_value_policy<manage_new_object>(),
"creates a new X object");
def("fact", fact, "compute the factorial");
{
docstring_options doc_options;
doc_options.disable_user_defined();
def("fact_usr_off_1", fact, "usr off 1");
doc_options.enable_user_defined();
def("fact_usr_on_1", fact, "usr on 1");
doc_options.disable_user_defined();
def("fact_usr_off_2", fact, "usr off 2");
}
def("fact_usr_on_2", fact, "usr on 2");
{
docstring_options doc_options(true, false);
def("fact_sig_off_1", fact, "sig off 1");
doc_options.enable_signatures();
def("fact_sig_on_1", fact, "sig on 1");
doc_options.disable_signatures();
def("fact_sig_off_2", fact, "sig off 2");
}
def("fact_sig_on_2", fact, "sig on 2");
{
docstring_options doc_options(false);
def("fact_usr_off_sig_off_1", fact, "usr off sig off 1");
{
docstring_options nested_doc_options;
def("fact_usr_on_sig_on_1", fact, "usr on sig on 1");
nested_doc_options.disable_all();
nested_doc_options.enable_user_defined();
def("fact_usr_on_sig_off_1", fact, "usr on sig off 1");
nested_doc_options.enable_all();
def("fact_usr_on_sig_on_2", fact, "usr on sig on 2");
}
def("fact_usr_off_sig_off_2", fact, "usr off sig off 2");
}
}
#include "module_tail.cpp"

View File

@@ -1,121 +0,0 @@
# Copyright David Abrahams & Ralf W. Grosse-Kunsteve 2004-2006.
# 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)
'''
>>> from docstring_ext import *
>>> def selected_doc(obj, *args):
... doc = obj.__doc__.splitlines()
... return "\\n".join(["|"+doc[i] for i in args])
>>> print selected_doc(X.__init__, 0, 1, 2)
|this is the __init__ function
|its documentation has two lines.
|C++ signature:
>>> print selected_doc(X.value, 0, 1, 3, 4, 5)
|gets the value of the object
|C++ signature:
|
|also gets the value of the object
|C++ signature:
>>> print selected_doc(create, 0, 1)
|creates a new X object
|C++ signature:
>>> print selected_doc(fact, 0, 1)
|compute the factorial
|C++ signature:
>>> len(fact_usr_off_1.__doc__.splitlines())
2
>>> print selected_doc(fact_usr_off_1, 0)
|C++ signature:
>>> len(fact_usr_on_1.__doc__.splitlines())
3
>>> print selected_doc(fact_usr_on_1, 0, 1)
|usr on 1
|C++ signature:
>>> len(fact_usr_off_2.__doc__.splitlines())
2
>>> print selected_doc(fact_usr_off_2, 0)
|C++ signature:
>>> len(fact_usr_on_2.__doc__.splitlines())
3
>>> print selected_doc(fact_usr_on_2, 0, 1)
|usr on 2
|C++ signature:
>>> len(fact_sig_off_1.__doc__.splitlines())
1
>>> print selected_doc(fact_sig_off_1, 0)
|sig off 1
>>> len(fact_sig_on_1.__doc__.splitlines())
3
>>> print selected_doc(fact_sig_on_1, 0, 1)
|sig on 1
|C++ signature:
>>> len(fact_sig_off_2.__doc__.splitlines())
1
>>> print selected_doc(fact_sig_off_2, 0)
|sig off 2
>>> len(fact_sig_on_2.__doc__.splitlines())
3
>>> print selected_doc(fact_sig_on_2, 0, 1)
|sig on 2
|C++ signature:
>>> print fact_usr_off_sig_off_1.__doc__
None
>>> len(fact_usr_on_sig_on_1.__doc__.splitlines())
3
>>> print selected_doc(fact_usr_on_sig_on_1, 0, 1)
|usr on sig on 1
|C++ signature:
>>> len(fact_usr_on_sig_off_1.__doc__.splitlines())
1
>>> print selected_doc(fact_usr_on_sig_off_1, 0)
|usr on sig off 1
>>> len(fact_usr_on_sig_on_2.__doc__.splitlines())
3
>>> print selected_doc(fact_usr_on_sig_on_2, 0, 1)
|usr on sig on 2
|C++ signature:
>>> print fact_usr_off_sig_off_2.__doc__
None
'''
def run(args = None):
import sys
import doctest
if args is not None:
sys.argv = args
import docstring_ext
result = doctest.testmod(sys.modules.get(__name__))
import pydoc
import re
docmodule = lambda m: re.sub(".\10", "", pydoc.text.docmodule(m))
try:
print 'printing module help:'
print docmodule(docstring_ext)
except object, x:
print '********* failed **********'
print x
result = list(result)
result[0] += 1
return tuple(result)
return result
if __name__ == '__main__':
print "running..."
import sys
status = run()[0]
if (status == 0): print "Done."
sys.exit(status)

View File

@@ -1,54 +0,0 @@
// Copyright David Abrahams 2002.
// 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/enum.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#if BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
# include <boost/type_traits/is_enum.hpp>
# include <boost/mpl/bool.hpp>
#endif
using namespace boost::python;
enum color { red = 1, green = 2, blue = 4 };
#if BOOST_WORKAROUND(__MWERKS__, <= 0x2407)
namespace boost // Pro7 has a hard time detecting enums
{
template <> struct is_enum<color> : boost::mpl::true_ {};
}
#endif
color identity_(color x) { return x; }
struct colorized {
colorized() : x(red) {}
color x;
};
BOOST_PYTHON_MODULE(enum_ext)
{
enum_<color>("color")
.value("red", red)
.value("green", green)
.value("blue", blue)
.export_values()
;
def("identity", identity_);
#if BOOST_WORKAROUND(__MWERKS__, <=0x2407)
color colorized::*px = &colorized::x;
class_<colorized>("colorized")
.def_readwrite("x", px)
;
#else
class_<colorized>("colorized")
.def_readwrite("x", &colorized::x)
;
#endif
}
#include "module_tail.cpp"

View File

@@ -1,64 +0,0 @@
# 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)
'''
>>> from enum_ext import *
>>> identity(color.red)
enum_ext.color.red
>>> identity(color.green)
enum_ext.color.green
>>> identity(color.blue)
enum_ext.color.blue
>>> identity(color(1))
enum_ext.color.red
>>> identity(color(2))
enum_ext.color.green
>>> identity(color(3))
enum_ext.color(3)
>>> identity(color(4))
enum_ext.color.blue
--- check export to scope ---
>>> identity(red)
enum_ext.color.red
>>> identity(green)
enum_ext.color.green
>>> identity(blue)
enum_ext.color.blue
>>> try: identity(1)
... except TypeError: pass
... else: print 'expected a TypeError'
>>> c = colorized()
>>> c.x
enum_ext.color.red
>>> c.x = green
>>> c.x
enum_ext.color.green
'''
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)

View File

@@ -1,28 +0,0 @@
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/exception_translator.hpp>
struct error {};
void translate(error const& /*e*/)
{
PyErr_SetString(PyExc_RuntimeError, "!!!error!!!");
}
void throw_error()
{
throw error();
}
BOOST_PYTHON_MODULE(exception_translator_ext)
{
using namespace boost::python;
register_exception_translator<error>(&translate);
def("throw_error", throw_error);
}

View File

@@ -1,27 +0,0 @@
# 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)
'''
>>> from exception_translator_ext import *
>>> try:
... throw_error();
... except RuntimeError, x:
... print x
... else:
... print 'Expected a RuntimeError!'
!!!error!!!
'''
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)

View File

@@ -1,152 +0,0 @@
// Copyright Stefan Seefeld 2005.
// 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.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
namespace python = boost::python;
// An abstract base class
class Base : public boost::noncopyable
{
public:
virtual ~Base() {};
virtual std::string hello() = 0;
};
// C++ derived class
class CppDerived : public Base
{
public:
virtual ~CppDerived() {}
virtual std::string hello() { return "Hello from C++!";}
};
// Familiar Boost.Python wrapper class for Base
struct BaseWrap : Base, python::wrapper<Base>
{
virtual std::string hello()
{
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
// workaround for VC++ 6.x or 7.0, see
// http://boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions
return python::call<std::string>(this->get_override("hello").ptr());
#else
return this->get_override("hello")();
#endif
}
};
// Pack the Base class wrapper into a module
BOOST_PYTHON_MODULE(embedded_hello)
{
python::class_<BaseWrap, boost::noncopyable> base("Base");
}
void exec_test()
{
// Register the module with the interpreter
if (PyImport_AppendInittab("embedded_hello", initembedded_hello) == -1)
throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
"builtin modules");
// Retrieve the main module
python::object main = python::import("__main__");
// Retrieve the main module's namespace
python::object global(main.attr("__dict__"));
// Define the derived class in Python.
python::object result = python::exec(
"from embedded_hello import * \n"
"class PythonDerived(Base): \n"
" def hello(self): \n"
" return 'Hello from Python!' \n",
global, global);
python::object PythonDerived = global["PythonDerived"];
// Creating and using instances of the C++ class is as easy as always.
CppDerived cpp;
BOOST_TEST(cpp.hello() == "Hello from C++!");
// But now creating and using instances of the Python class is almost
// as easy!
python::object py_base = PythonDerived();
Base& py = python::extract<Base&>(py_base) BOOST_EXTRACT_WORKAROUND;
// Make sure the right 'hello' method is called.
BOOST_TEST(py.hello() == "Hello from Python!");
}
void exec_file_test(std::string const &script)
{
// Run a python script in an empty environment.
python::dict global;
python::object result = python::exec_file(script.c_str(), global, global);
// Extract an object the script stored in the global dictionary.
BOOST_TEST(python::extract<int>(global["number"]) == 42);
}
void exec_test_error()
{
// Execute a statement that raises a python exception.
python::dict global;
python::object result = python::exec("print unknown \n", global, global);
}
int main(int argc, char **argv)
{
BOOST_TEST(argc == 2);
std::string script = argv[1];
// Initialize the interpreter
Py_Initialize();
if (python::handle_exception(exec_test) ||
python::handle_exception(boost::bind(exec_file_test, script)))
{
if (PyErr_Occurred())
{
BOOST_ERROR("Python Error detected");
PyErr_Print();
}
else
{
BOOST_ERROR("A C++ exception was thrown for which "
"there was no exception handler registered.");
}
}
if (python::handle_exception(exec_test_error))
{
if (PyErr_Occurred())
{
PyErr_Print();
}
else
{
BOOST_ERROR("A C++ exception was thrown for which "
"there was no exception handler registered.");
}
}
else
{
BOOST_ERROR("Python exception expected, but not seen.");
}
// Boost.Python doesn't support Py_Finalize yet.
// Py_Finalize();
return boost::report_errors();
}
// Including this file makes sure
// that on Windows, any crashes (e.g. null pointer dereferences) invoke
// the debugger immediately, rather than being translated into structured
// exceptions that can interfere with debugging.
#include "module_tail.cpp"

View File

@@ -1,6 +0,0 @@
# Copyright Stefan Seefeld 2006. 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)
print 'Hello World !'
number = 42

View File

@@ -1,143 +0,0 @@
// Copyright David Abrahams 2002.
// 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/extract.hpp>
#include <boost/python/list.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/reference_existing_object.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/implicit.hpp>
#include <string>
#include <boost/lexical_cast.hpp>
#define BOOST_ENABLE_ASSERT_HANDLER
#include <boost/assert.hpp>
#include "test_class.hpp"
using namespace boost::python;
typedef test_class<> X;
bool extract_bool(object x) { return extract<bool>(x); }
boost::python::list extract_list(object x)
{
extract<list> get_list((x));
// Make sure we always have the right idea about whether it's a list
bool is_list_1 = get_list.check();
bool is_list_2 = PyObject_IsInstance(x.ptr(), (PyObject*)&PyList_Type);
if (is_list_1 != is_list_2) {
throw std::runtime_error("is_list_1 == is_list_2 failure.");
}
return get_list();
}
char const* extract_cstring(object x)
{
return extract<char const*>(x);
}
std::string extract_string(object x)
{
std::string s = extract<std::string>(x);
return s;
}
std::string const& extract_string_cref(object x)
{
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
# pragma warning(push)
# pragma warning(disable:4172) // msvc lies about returning a reference to temporary
#elif defined(_MSC_VER) && defined(__ICL) && __ICL <= 900
# pragma warning(push)
# pragma warning(disable:473) // intel/win32 does too
#endif
return extract<std::string const&>(x);
#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(_MSC_VER) && defined(__ICL) && __ICL <= 800
# pragma warning(pop)
#endif
}
X extract_X(object x)
{
return extract<X>(x);
}
X* extract_X_ptr(object x) { return extract<X*>(x); }
X& extract_X_ref(object x)
{
extract<X&> get_x(x);
return get_x;
}
int double_X(object n)
{
extract<X> x(n);
return x().value() + x().value();
}
bool check_bool(object x) { return extract<bool>(x).check(); }
bool check_list(object x) { return extract<list>(x).check(); }
bool check_cstring(object x) { return extract<char const*>(x).check(); }
bool check_string(object x) { return extract<std::string>(x).check(); }
bool check_string_cref(object x) { return extract<std::string const&>(x).check(); }
bool check_X(object x) { return extract<X>(x).check(); }
bool check_X_ptr(object x) { return extract<X*>(x).check(); }
bool check_X_ref(object x) { return extract<X&>(x).check(); }
std::string x_rep(X const& x)
{
return "X(" + boost::lexical_cast<std::string>(x.value()) + ")";
}
BOOST_PYTHON_MODULE(extract_ext)
{
implicitly_convertible<int, X>();
def("extract_bool", extract_bool);
def("extract_list", extract_list);
def("extract_cstring", extract_cstring);
def("extract_string", extract_string);
def("extract_string_cref", extract_string_cref, return_value_policy<reference_existing_object>());
def("extract_X", extract_X);
def("extract_X_ptr", extract_X_ptr, return_value_policy<reference_existing_object>());
def("extract_X_ref", extract_X_ref, return_value_policy<reference_existing_object>());
def("check_bool", check_bool);
def("check_list", check_list);
def("check_cstring", check_cstring);
def("check_string", check_string);
def("check_string_cref", check_string_cref);
def("check_X", check_X);
def("check_X_ptr", check_X_ptr);
def("check_X_ref", check_X_ref);
def("double_X", double_X);
def("count_Xs", &X::count);
;
object x_class(
class_<X>("X", init<int>())
.def( "__repr__", x_rep));
// Instantiate an X object through the Python interface
object x_obj = x_class(3);
// Get the C++ object out of the Python object
X const& x = extract<X&>(x_obj);
if (x.value() != 3) {
throw std::runtime_error("x.value() == 3 failure.");
}
}
#include "module_tail.cpp"

View File

@@ -1,107 +0,0 @@
# 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)
'''
>>> from extract_ext import *
Just about anything has a truth value in Python
>>> assert check_bool(None)
>>> extract_bool(None)
0
>>> assert check_bool(2)
>>> extract_bool(2)
1
>>> assert not check_bool('')
Check that object manager types work properly. These are a different
case because they wrap Python objects instead of being wrapped by them.
>>> assert not check_list(2)
>>> try: x = extract_list(2)
... except TypeError, x:
... if str(x) != 'Expecting an object of type list; got an object of type int instead':
... print x
... else:
... print 'expected an exception, got', x, 'instead'
Can't extract a list from a tuple. Use list(x) to convert a sequence
to a list:
>>> assert not check_list((1, 2, 3))
>>> assert check_list([1, 2, 3])
>>> extract_list([1, 2, 3])
[1, 2, 3]
Can get a char const* from a Python string:
>>> assert check_cstring('hello')
>>> extract_cstring('hello')
'hello'
Can't get a char const* from a Python int:
>>> assert not check_cstring(1)
>>> try: x = extract_cstring(1)
... except TypeError: pass
... else:
... print 'expected an exception, got', x, 'instead'
Extract an std::string (class) rvalue from a native Python type
>>> assert check_string('hello')
>>> extract_string('hello')
'hello'
Constant references are not treated as rvalues for the purposes of
extract:
>>> assert not check_string_cref('hello')
We can extract lvalues where appropriate:
>>> x = X(42)
>>> check_X(x)
1
>>> extract_X(x)
X(42)
>>> check_X_ptr(x)
1
>>> extract_X_ptr(x)
X(42)
>>> extract_X_ref(x)
X(42)
Demonstrate that double-extraction of an rvalue works, and all created
copies of the object are destroyed:
>>> n = count_Xs()
>>> double_X(333)
666
>>> count_Xs() - n
0
General check for cleanliness:
>>> del x
>>> count_Xs()
0
'''
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)

View File

@@ -1,44 +0,0 @@
// Copyright David Abrahams 2002.
// 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/static_assert.hpp>
#include <boost/python/detail/if_else.hpp>
#include <boost/type_traits/same_traits.hpp>
typedef char c1;
typedef char c2[2];
typedef char c3[3];
typedef char c4[4];
template <unsigned size>
struct choose
{
typedef typename boost::python::detail::if_<
(sizeof(c1) == size)
>::template then<
c1
>::template elif<
(sizeof(c2) == size)
>::template then<
c2
>::template elif<
(sizeof(c3) == size)
>::template then<
c3
>::template elif<
(sizeof(c4) == size)
>::template then<
c4
>::template else_<void*>::type type;
};
int main()
{
BOOST_STATIC_ASSERT((boost::is_same<choose<1>::type,c1>::value));
BOOST_STATIC_ASSERT((boost::is_same<choose<2>::type,c2>::value));
BOOST_STATIC_ASSERT((boost::is_same<choose<3>::type,c3>::value));
BOOST_STATIC_ASSERT((boost::is_same<choose<4>::type,c4>::value));
BOOST_STATIC_ASSERT((boost::is_same<choose<5>::type,void*>::value));
return 0;
}

View File

@@ -1,47 +0,0 @@
// Copyright David Abrahams 2002.
// 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/implicit.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include "test_class.hpp"
using namespace boost::python;
typedef test_class<> X;
int x_value(X const& x)
{
return x.value();
}
X make_x(int n) { return X(n); }
// foo/bar -- a regression for a vc7 bug workaround
struct bar {};
struct foo
{
virtual void f() = 0;
operator bar() const { return bar(); }
};
BOOST_PYTHON_MODULE(implicit_ext)
{
implicitly_convertible<foo,bar>();
implicitly_convertible<int,X>();
def("x_value", x_value);
def("make_x", make_x);
class_<X>("X", init<int>())
.def("value", &X::value)
.def("set", &X::set)
;
implicitly_convertible<X,int>();
}
#include "module_tail.cpp"

View File

@@ -1,31 +0,0 @@
# 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)
'''
>>> from implicit_ext import *
>>> x_value(X(42))
42
>>> x_value(42)
42
>>> x = make_x(X(42))
>>> x.value()
42
>>> try: make_x('fool')
... except TypeError: pass
... else: print 'no error'
'''
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)

View File

@@ -1,117 +0,0 @@
// 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 <stdio.h>
#define BOOST_ENABLE_ASSERT_HANDLER
#include <boost/assert.hpp>
#include <boost/type_traits/is_member_function_pointer.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/python/detail/indirect_traits.hpp>
#include <boost/mpl/assert.hpp>
//#define print(expr) printf("%s ==> %s\n", #expr, expr)
// not all the compilers can handle an incomplete class type here.
struct X {};
using namespace boost::python::indirect_traits;
typedef void (X::*pmf)();
BOOST_MPL_ASSERT((is_reference_to_function<int (&)()>));
BOOST_MPL_ASSERT_NOT((is_reference_to_function<int (*)()>));
BOOST_MPL_ASSERT_NOT((is_reference_to_function<int&>));
BOOST_MPL_ASSERT_NOT((is_reference_to_function<pmf>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_function<int (&)()>));
BOOST_MPL_ASSERT((is_pointer_to_function<int (*)()>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_function<int (*&)()>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_function<int (*const&)()>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_function<pmf>));
BOOST_MPL_ASSERT_NOT((is_reference_to_function_pointer<int (&)()>));
BOOST_MPL_ASSERT_NOT((is_reference_to_function_pointer<int (*)()>));
BOOST_MPL_ASSERT_NOT((is_reference_to_function_pointer<int&>));
BOOST_MPL_ASSERT((is_reference_to_function_pointer<int (*&)()>));
BOOST_MPL_ASSERT((is_reference_to_function_pointer<int (*const&)()>));
BOOST_MPL_ASSERT_NOT((is_reference_to_function_pointer<pmf>));
BOOST_MPL_ASSERT((is_reference_to_pointer<int*&>));
BOOST_MPL_ASSERT((is_reference_to_pointer<int* const&>));
BOOST_MPL_ASSERT((is_reference_to_pointer<int*volatile&>));
BOOST_MPL_ASSERT((is_reference_to_pointer<int*const volatile&>));
BOOST_MPL_ASSERT((is_reference_to_pointer<int const*&>));
BOOST_MPL_ASSERT((is_reference_to_pointer<int const* const&>));
BOOST_MPL_ASSERT((is_reference_to_pointer<int const*volatile&>));
BOOST_MPL_ASSERT((is_reference_to_pointer<int const*const volatile&>));
BOOST_MPL_ASSERT_NOT((is_reference_to_pointer<pmf>));
BOOST_MPL_ASSERT_NOT((is_reference_to_pointer<int const volatile>));
BOOST_MPL_ASSERT_NOT((is_reference_to_pointer<int>));
BOOST_MPL_ASSERT_NOT((is_reference_to_pointer<int*>));
BOOST_MPL_ASSERT_NOT((is_reference_to_const<int*&>));
BOOST_MPL_ASSERT((is_reference_to_const<int* const&>));
BOOST_MPL_ASSERT_NOT((is_reference_to_const<int*volatile&>));
BOOST_MPL_ASSERT((is_reference_to_const<int*const volatile&>));
BOOST_MPL_ASSERT_NOT((is_reference_to_const<int const volatile>));
BOOST_MPL_ASSERT_NOT((is_reference_to_const<int>));
BOOST_MPL_ASSERT_NOT((is_reference_to_const<int*>));
BOOST_MPL_ASSERT((is_reference_to_non_const<int*&>));
BOOST_MPL_ASSERT_NOT((is_reference_to_non_const<int* const&>));
BOOST_MPL_ASSERT((is_reference_to_non_const<int*volatile&>));
BOOST_MPL_ASSERT_NOT((is_reference_to_non_const<int*const volatile&>));
BOOST_MPL_ASSERT_NOT((is_reference_to_non_const<int const volatile>));
BOOST_MPL_ASSERT_NOT((is_reference_to_non_const<int>));
BOOST_MPL_ASSERT_NOT((is_reference_to_non_const<int*>));
BOOST_MPL_ASSERT_NOT((is_reference_to_volatile<int*&>));
BOOST_MPL_ASSERT_NOT((is_reference_to_volatile<int* const&>));
BOOST_MPL_ASSERT((is_reference_to_volatile<int*volatile&>));
BOOST_MPL_ASSERT((is_reference_to_volatile<int*const volatile&>));
BOOST_MPL_ASSERT_NOT((is_reference_to_volatile<int const volatile>));
BOOST_MPL_ASSERT_NOT((is_reference_to_volatile<int>));
BOOST_MPL_ASSERT_NOT((is_reference_to_volatile<int*>));
namespace tt = boost::python::indirect_traits;
BOOST_MPL_ASSERT_NOT((tt::is_reference_to_class<int>));
BOOST_MPL_ASSERT_NOT((tt::is_reference_to_class<int&>));
BOOST_MPL_ASSERT_NOT((tt::is_reference_to_class<int*>));
BOOST_MPL_ASSERT_NOT((tt::is_reference_to_class<pmf>));
BOOST_MPL_ASSERT_NOT((tt::is_reference_to_class<pmf const&>));
BOOST_MPL_ASSERT_NOT((tt::is_reference_to_class<X>));
BOOST_MPL_ASSERT((tt::is_reference_to_class<X&>));
BOOST_MPL_ASSERT((tt::is_reference_to_class<X const&>));
BOOST_MPL_ASSERT((tt::is_reference_to_class<X volatile&>));
BOOST_MPL_ASSERT((tt::is_reference_to_class<X const volatile&>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_class<int>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_class<int*>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_class<int&>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_class<X>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_class<X&>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_class<pmf>));
BOOST_MPL_ASSERT_NOT((is_pointer_to_class<pmf const>));
BOOST_MPL_ASSERT((is_pointer_to_class<X*>));
BOOST_MPL_ASSERT((is_pointer_to_class<X const*>));
BOOST_MPL_ASSERT((is_pointer_to_class<X volatile*>));
BOOST_MPL_ASSERT((is_pointer_to_class<X const volatile*>));
BOOST_MPL_ASSERT((tt::is_reference_to_member_function_pointer<pmf&>));
BOOST_MPL_ASSERT((tt::is_reference_to_member_function_pointer<pmf const&>));
BOOST_MPL_ASSERT((tt::is_reference_to_member_function_pointer<pmf volatile&>));
BOOST_MPL_ASSERT((tt::is_reference_to_member_function_pointer<pmf const volatile&>));
BOOST_MPL_ASSERT_NOT((tt::is_reference_to_member_function_pointer<pmf[2]>));
BOOST_MPL_ASSERT_NOT((tt::is_reference_to_member_function_pointer<pmf(&)[2]>));
BOOST_MPL_ASSERT_NOT((tt::is_reference_to_member_function_pointer<pmf>));

View File

@@ -1,39 +0,0 @@
// Copyright David Abrahams 2003.
// 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/module.hpp>
#include <boost/python/class.hpp>
#include "test_class.hpp"
#include <memory>
#include <boost/shared_ptr.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/args.hpp>
using namespace boost::python;
typedef test_class<> X;
X* empty() { return new X(1000); }
std::auto_ptr<X> sum(int a, int b) { return std::auto_ptr<X>(new X(a+b)); }
boost::shared_ptr<X> product(int a, int b, int c)
{
return boost::shared_ptr<X>(new X(a*b*c));
}
BOOST_PYTHON_MODULE(injected_ext)
{
class_<X>("X", init<int>())
.def("__init__", make_constructor(empty))
.def("__init__", make_constructor(sum))
.def("__init__", make_constructor(product
, default_call_policies()
, ( arg_("a"), arg_("b"), arg_("c"))
),
"this is product's docstring")
.def("value", &X::value)
;
}

View File

@@ -1,28 +0,0 @@
# 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)
'''
>>> from injected_ext import *
>>> X(3,5).value() - (3+5)
0
>>> X(a=3,b=5,c=7).value() - (3*5*7)
0
>>> X().value()
1000
'''
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)

View File

@@ -1,48 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/iterator.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <list>
using namespace boost::python;
typedef std::list<int> list_int;
// Prove that we can handle InputIterators which return rvalues.
struct doubler
{
typedef int result_type;
int operator()(int x) const { return x * 2; }
};
typedef boost::transform_iterator<doubler, list_int::iterator> doubling_iterator;
typedef std::pair<doubling_iterator,doubling_iterator> list_range2;
list_range2 range2(list_int& x)
{
return list_range2(
boost::make_transform_iterator<doubler>(x.begin(), doubler())
, boost::make_transform_iterator<doubler>(x.end(), doubler()));
}
// We do this in a separate module from iterators_ext (iterators.cpp)
// to work around an MSVC6 linker bug, which causes it to complain
// about a "duplicate comdat" if the input iterator is instantiated in
// the same module with the others.
BOOST_PYTHON_MODULE(input_iterator)
{
def("range2", &::range2);
class_<list_range2>("list_range2")
// We can wrap InputIterators which return by-value
.def("__iter__"
, range(&list_range2::first, &list_range2::second))
;
}
#include "module_tail.cpp"

View File

@@ -1,16 +0,0 @@
// 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/suite/indexing/map_indexing_suite.hpp>
void int_map_indexing_suite()
{
using namespace boost::python;
// Compile check only...
class_<std::map<int, int> >("IntMap")
.def(map_indexing_suite<std::map<int, int> >())
;
}

View File

@@ -1,137 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/return_internal_reference.hpp>
#include <boost/python/copy_non_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/iterator.hpp>
#include <list>
#include <utility>
#include <iterator>
#include <algorithm>
using namespace boost::python;
typedef std::list<int> list_int;
typedef std::list<list_int> list_list;
void push_back(list_int& x, int y)
{
x.push_back(y);
}
void push_list_back(list_list& x, list_int const& y)
{
x.push_back(y);
}
int back(list_int& x)
{
return x.back();
}
typedef std::pair<list_int::iterator,list_int::iterator> list_range;
struct list_range2 : list_range
{
list_int::iterator& begin() { return this->first; }
list_int::iterator& end() { return this->second; }
};
list_range range(list_int& x)
{
return list_range(x.begin(), x.end());
}
struct two_lists
{
two_lists()
{
int primes[] = { 2, 3, 5, 7, 11, 13 };
std::copy(primes, primes + sizeof(primes)/sizeof(*primes), std::back_inserter(one));
int evens[] = { 2, 4, 6, 8, 10, 12 };
std::copy(evens, evens + sizeof(evens)/sizeof(*evens), std::back_inserter(two));
}
struct two_start
{
typedef list_int::iterator result_type;
result_type operator()(two_lists& ll) const { return ll.two.begin(); }
};
friend struct two_start;
list_int::iterator one_begin() { return one.begin(); }
list_int::iterator two_begin() { return two.begin(); }
list_int::iterator one_end() { return one.end(); }
list_int::iterator two_end() { return two.end(); }
private:
list_int one;
list_int two;
};
BOOST_PYTHON_MODULE(iterator_ext)
{
using boost::python::iterator; // gcc 2.96 bug workaround
def("range", &::range);
class_<list_int>("list_int")
.def("push_back", push_back)
.def("back", back)
.def("__iter__", iterator<list_int>())
;
class_<list_range>("list_range")
// We can specify data members
.def("__iter__"
, range(&list_range::first, &list_range::second))
;
// No runtime tests for this one yet
class_<list_range2>("list_range2")
// We can specify member functions returning a non-const reference
.def("__iter__", range(&list_range2::begin, &list_range2::end))
;
class_<two_lists>("two_lists")
// We can spcify member functions
.add_property(
"primes"
, range(&two_lists::one_begin, &two_lists::one_end))
// Prove that we can explicitly specify call policies
.add_property(
"evens"
, range<return_value_policy<copy_non_const_reference> >(
&two_lists::two_begin, &two_lists::two_end))
// Prove that we can specify call policies and target
.add_property(
"twosies"
, range<return_value_policy<copy_non_const_reference>, two_lists>(
// And we can use adaptable function objects when
// partial specialization is available.
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
two_lists::two_start()
# else
&two_lists::two_begin
# endif
, &two_lists::two_end))
;
class_<list_list>("list_list")
.def("push_back", push_list_back)
.def("__iter__", iterator<list_list,return_internal_reference<> >())
;
}
#include "module_tail.cpp"

View File

@@ -1,77 +0,0 @@
# 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)
'''
>>> from iterator_ext import *
>>> from input_iterator import *
>>> x = list_int()
>>> x.push_back(1)
>>> x.back()
1
>>> x.push_back(3)
>>> x.push_back(5)
>>> for y in x:
... print y
1
3
5
>>> z = range(x)
>>> for y in z:
... print y
1
3
5
Range2 wraps a transform_iterator which doubles the elements it
traverses. This proves we can wrap input iterators
>>> z2 = range2(x)
>>> for y in z2:
... print y
2
6
10
>>> l2 = two_lists()
>>> for y in l2.primes:
... print y
2
3
5
7
11
13
>>> for y in l2.evens:
... print y
2
4
6
8
10
12
>>> ll = list_list()
>>> ll.push_back(x)
>>> x.push_back(7)
>>> ll.push_back(x)
>>> for a in ll:
... for b in a:
... print b,
... print
...
1 3 5
1 3 5 7
'''
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)

View File

@@ -1,118 +0,0 @@
// Copyright David Abrahams 2002.
// 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.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;
}
void seta(int a)
{
a_ = a;
}
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;
BOOST_PYTHON_MODULE(keywords)
{
#if BOOST_WORKAROUND(__GNUC__, == 2)
using boost::python::arg;
#endif
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("set2", &Foo::set, (arg("a"), "b", "n") )
.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("set2", &Bar::set, bar_set("set2's docstring"))
.def("seta", &Bar::seta, arg("a"))
.def("a", &Bar::geta)
.def("b", &Bar::getb)
.def("n", &Bar::getn)
;
}
#include "module_tail.cpp"

View File

@@ -1,104 +0,0 @@
# 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)
'''
>>> 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')
>>> f.set2(b=2.0,n="2",a=2)
>>> f.a(), f.b(), f.n()
(2, 2.0, '2')
# 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')
>>> f.set2.__doc__.splitlines()[-3]
"set2's docstring"
'''
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)

View File

@@ -1,145 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/list.hpp>
#include <boost/python/make_function.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/assert.hpp>
#include "test_class.hpp"
using namespace boost::python;
object new_list()
{
return list();
}
list listify(object x)
{
return list(x);
}
object listify_string(char const* s)
{
return list(s);
}
std::string x_rep(test_class<> const& x)
{
return "X(" + boost::lexical_cast<std::string>(x.value()) + ")";
}
object apply_object_list(object f, list x)
{
return f(x);
}
list apply_list_list(object f, list x)
{
return call<list>(f.ptr(), x);
}
void append_object(list& x, object y)
{
x.append(y);
}
void append_list(list& x, list const& y)
{
x.append(y);
}
typedef test_class<> X;
int notcmp(object const& x, object const& y)
{
return y < x ? -1 : y > x ? 1 : 0;
}
void exercise(list x, object y, object print)
{
x.append(y);
x.append(5);
x.append(X(3));
print("after append:");
print(x);
print("number of", y, "instances:", x.count(y));
print("number of 5s:", x.count(5));
x.extend("xyz");
print("after extend:");
print(x);
print("index of", y, "is:", x.index(y));
print("index of 'l' is:", x.index("l"));
x.insert(4, 666);
print("after inserting 666:");
print(x);
print("inserting with object as index:");
x.insert(x[x.index(5)], "---");
print(x);
print("popping...");
x.pop();
print(x);
x.pop(x[x.index(5)]);
print(x);
x.pop(x.index(5));
print(x);
print("removing", y);
x.remove(y);
print(x);
print("removing", 666);
x.remove(666);
print(x);
print("reversing...");
x.reverse();
print(x);
print("sorted:");
x.pop(2); // make sorting predictable
x.sort();
print(x);
print("reverse sorted:");
x.sort(&notcmp);
print(x);
list w;
w.append(5);
w.append(6);
w += "hi";
BOOST_ASSERT(w[0] == 5);
BOOST_ASSERT(w[1] == 6);
BOOST_ASSERT(w[2] == 'h');
BOOST_ASSERT(w[3] == 'i');
}
BOOST_PYTHON_MODULE(list_ext)
{
def("new_list", new_list);
def("listify", listify);
def("listify_string", listify_string);
def("apply_object_list", apply_object_list);
def("apply_list_list", apply_list_list);
def("append_object", append_object);
def("append_list", append_list);
def("exercise", exercise);
class_<X>("X", init<int>())
.def( "__repr__", x_rep)
;
}

View File

@@ -1,118 +0,0 @@
# 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)
'''
>>> from list_ext import *
>>> new_list()
[]
>>> listify((1,2,3))
[1, 2, 3]
>>> letters = listify_string('hello')
>>> letters
['h', 'e', 'l', 'l', 'o']
>>> X(22)
X(22)
>>> def identity(x):
... return x
>>> assert apply_object_list(identity, letters) is letters
5 is not convertible to a list
>>> try: result = apply_object_list(identity, 5)
... except TypeError: pass
... else: print 'expected an exception, got', result, 'instead'
>>> assert apply_list_list(identity, letters) is letters
5 is not convertible to a list as a return value
>>> try: result = apply_list_list(len, letters)
... except TypeError: pass
... else: print 'expected an exception, got', result, 'instead'
>>> append_object(letters, '.')
>>> letters
['h', 'e', 'l', 'l', 'o', '.']
tuples do not automatically convert to lists when passed as arguments
>>> try: append_list(letters, (1,2))
... except TypeError: pass
... else: print 'expected an exception'
>>> append_list(letters, [1,2])
>>> letters
['h', 'e', 'l', 'l', 'o', '.', [1, 2]]
Check that subclass functions are properly called
>>> class mylist(list):
... def append(self, o):
... list.append(self, o)
... if not hasattr(self, 'nappends'):
... self.nappends = 1
... else:
... self.nappends += 1
...
>>> l2 = mylist()
>>> append_object(l2, 'hello')
>>> append_object(l2, 'world')
>>> l2
['hello', 'world']
>>> l2.nappends
2
>>> def printer(*args):
... for x in args: print x,
... print
...
>>> y = X(42)
>>> exercise(letters, y, printer)
after append:
['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(42), 5, X(3)]
number of X(42) instances: 1
number of 5s: 1
after extend:
['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
index of X(42) is: 7
index of 'l' is: 2
after inserting 666:
['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
inserting with object as index:
['h', 'e', 'l', 'l', 666, '---', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y', 'z']
popping...
['h', 'e', 'l', 'l', 666, '---', 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y']
['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), 5, X(3), 'x', 'y']
['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(42), X(3), 'x', 'y']
removing X(42)
['h', 'e', 'l', 'l', 666, 'o', '.', [1, 2], X(3), 'x', 'y']
removing 666
['h', 'e', 'l', 'l', 'o', '.', [1, 2], X(3), 'x', 'y']
reversing...
['y', 'x', X(3), [1, 2], '.', 'o', 'l', 'l', 'e', 'h']
sorted:
[[1, 2], '.', 'e', 'h', 'l', 'l', 'o', 'x', 'y']
reverse sorted:
['y', 'x', 'o', 'l', 'l', 'h', 'e', '.', [1, 2]]
'''
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)

View File

@@ -1,63 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/long.hpp>
#include <boost/python/class.hpp>
#define BOOST_ENABLE_ASSERT_HANDLER
#include <boost/assert.hpp>
using namespace boost::python;
object new_long()
{
return long_();
}
long_ longify(object x)
{
return long_(x);
}
object longify_string(char const* s)
{
return long_(s);
}
char const* is_long1(long_& x)
{
long_ y = x;
x += 50;
BOOST_ASSERT(x == y + 50);
return "yes";
}
int is_long2(char const*)
{
return 0;
}
// tests for accepting objects (and derived classes) in constructors
// from "Milind Patil" <milind_patil-at-hotmail.com>
struct Y
{
Y(boost::python::long_) {}
};
BOOST_PYTHON_MODULE(long_ext)
{
def("new_long", new_long);
def("longify", longify);
def("longify_string", longify_string);
def("is_long", is_long1);
def("is_long", is_long2);
class_< Y >("Y", init< boost::python::long_ >())
;
}
#include "module_tail.cpp"

View File

@@ -1,33 +0,0 @@
# 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)
'''
>>> from long_ext import *
>>> new_long()
0L
>>> longify(42)
42L
>>> longify_string('300')
300L
>>> is_long(20L)
'yes'
>>> is_long('20')
0
>>> x = Y(4294967295L)
'''
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)

View File

@@ -1,343 +0,0 @@
// Copyright David Abrahams 2001.
// 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/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/lvalue_from_pytype.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/to_python_converter.hpp>
#include <boost/python/errors.hpp>
#include <boost/python/manage_new_object.hpp>
#include <string.h>
#include "simple_type.hpp"
#include "complicated.hpp"
// Declare some straightforward extension types
extern "C" void
dealloc(PyObject* self)
{
PyObject_Del(self);
}
// Noddy is a type we got from one of the Python sample files
struct NoddyObject : PyObject
{
int x;
};
PyTypeObject NoddyType = {
PyObject_HEAD_INIT(NULL)
0,
"Noddy",
sizeof(NoddyObject),
0,
dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
0, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
#if PYTHON_API_VERSION >= 1012
0 /* tp_del */
#endif
};
// Create a Noddy containing 42
PyObject* new_noddy()
{
NoddyObject* noddy = PyObject_New(NoddyObject, &NoddyType);
noddy->x = 42;
return (PyObject*)noddy;
}
// Simple is a wrapper around a struct simple, which just contains a char*
struct SimpleObject
{
PyObject_HEAD
simple x;
};
struct extract_simple_object
{
static simple& execute(SimpleObject& o) { return o.x; }
};
PyTypeObject SimpleType = {
PyObject_HEAD_INIT(NULL)
0,
"Simple",
sizeof(SimpleObject),
0,
dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
0, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
0, /* tp_is_gc */
0, /* tp_bases */
0, /* tp_mro */
0, /* tp_cache */
0, /* tp_subclasses */
0, /* tp_weaklist */
#if PYTHON_API_VERSION >= 1012
0 /* tp_del */
#endif
};
// Create a Simple containing "hello, world"
PyObject* new_simple()
{
SimpleObject* simple = PyObject_New(SimpleObject, &SimpleType);
simple->x.s = "hello, world";
return (PyObject*)simple;
}
//
// Declare some wrappers/unwrappers to test the low-level conversion
// mechanism.
//
using boost::python::to_python_converter;
// Wrap a simple by copying it into a Simple
struct simple_to_python
: to_python_converter<simple, simple_to_python>
{
static PyObject* convert(simple const& x)
{
SimpleObject* p = PyObject_New(SimpleObject, &SimpleType);
p->x = x;
return (PyObject*)p;
}
};
struct int_from_noddy
{
static int& execute(NoddyObject& p)
{
return p.x;
}
};
//
// Some C++ functions to expose to Python
//
// Returns the length of s's held string
int f(simple const& s)
{
return strlen(s.s);
}
int f_mutable_ref(simple& s)
{
return strlen(s.s);
}
int f_mutable_ptr(simple* s)
{
return strlen(s->s);
}
int f_const_ptr(simple const* s)
{
return strlen(s->s);
}
int f2(SimpleObject const& s)
{
return strlen(s.x.s);
}
// A trivial passthru function for simple objects
simple const& g(simple const& x)
{
return x;
}
struct A
{
A() : x(0) {}
virtual ~A() {}
char const* name() { return "A"; }
int x;
};
struct B : A
{
B() : x(1) {}
static char const* name(B*) { return "B"; }
int x;
};
struct C : A
{
C() : x(2) {}
char const* name() { return "C"; }
virtual ~C() {}
int x;
};
struct D : B, C
{
D() : x(3) {}
char const* name() { return "D"; }
int x;
};
A take_a(A const& a) { return a; }
B take_b(B& b) { return b; }
C take_c(C* c) { return *c; }
D take_d(D* const& d) { return *d; }
D take_d_shared_ptr(boost::shared_ptr<D> d) { return *d; }
boost::shared_ptr<A> d_factory() { return boost::shared_ptr<B>(new D); }
struct Unregistered {};
Unregistered make_unregistered(int) { return Unregistered(); }
Unregistered* make_unregistered2(int) { return new Unregistered; }
BOOST_PYTHON_MODULE(m1)
{
using namespace boost::python;
using boost::shared_ptr;
simple_to_python();
lvalue_from_pytype<int_from_noddy,&NoddyType>();
lvalue_from_pytype<
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300 // doesn't support non-type member pointer parameters
extract_member<SimpleObject, simple, &SimpleObject::x>
#else
extract_simple_object
#endif
, &SimpleType
>();
lvalue_from_pytype<extract_identity<SimpleObject>,&SimpleType>();
def("new_noddy", new_noddy);
def("new_simple", new_simple);
def("make_unregistered", make_unregistered);
def("make_unregistered2", make_unregistered2, return_value_policy<manage_new_object>());
// Expose f() in all its variations
def("f", f);
def("f_mutable_ref", f_mutable_ref);
def("f_mutable_ptr", f_mutable_ptr);
def("f_const_ptr", f_const_ptr);
def("f2", f2);
// Expose g()
def("g", g , return_value_policy<copy_const_reference>()
);
def("take_a", take_a);
def("take_b", take_b);
def("take_c", take_c);
def("take_d", take_d);
def("take_d_shared_ptr", take_d_shared_ptr);
def("d_factory", d_factory);
class_<A, shared_ptr<A> >("A")
.def("name", &A::name)
;
// sequence points don't ensure that "A" is constructed before "B"
// or "C" below if we make them part of the same chain
class_<B,bases<A> >("B")
.def("name", &B::name)
;
class_<C,bases<A> >("C")
.def("name", &C::name)
;
class_<D, bases<B,C> >("D")
.def("name", &D::name)
;
class_<complicated>("complicated",
init<simple const&,int>())
.def(init<simple const&>())
.def("get_n", &complicated::get_n)
;
}
#include "module_tail.cpp"

View File

@@ -1,98 +0,0 @@
// Copyright David Abrahams 2001.
// 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)
// This module exercises the converters exposed in m1 at a low level
// by exposing raw Python extension functions that use wrap<> and
// unwrap<> objects.
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/copy_non_const_reference.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
#include "simple_type.hpp"
// Get a simple (by value) from the argument, and return the
// string it holds.
PyObject* unwrap_simple(simple x)
{
return PyString_FromString(x.s);
}
// Likewise, but demands that its possible to get a non-const
// reference to the simple.
PyObject* unwrap_simple_ref(simple& x)
{
return PyString_FromString(x.s);
}
// Likewise, with a const reference to the simple object.
PyObject* unwrap_simple_const_ref(simple const& x)
{
return PyString_FromString(x.s);
}
// Get an int (by value) from the argument, and convert it to a
// Python Int.
PyObject* unwrap_int(int x)
{
return PyInt_FromLong(x);
}
// Get a non-const reference to an int from the argument
PyObject* unwrap_int_ref(int& x)
{
return PyInt_FromLong(x);
}
// Get a const reference to an int from the argument.
PyObject* unwrap_int_const_ref(int const& x)
{
return PyInt_FromLong(x);
}
// rewrap<T> extracts a T from the argument, then converts the T back
// to a PyObject* and returns it.
template <class T>
struct rewrap
{
static T f(T x) { return x; }
};
BOOST_PYTHON_MODULE(m2)
{
using boost::python::return_value_policy;
using boost::python::copy_const_reference;
using boost::python::copy_non_const_reference;
using boost::python::def;
def("unwrap_int", unwrap_int);
def("unwrap_int_ref", unwrap_int_ref);
def("unwrap_int_const_ref", unwrap_int_const_ref);
def("unwrap_simple", unwrap_simple);
def("unwrap_simple_ref", unwrap_simple_ref);
def("unwrap_simple_const_ref", unwrap_simple_const_ref);
def("wrap_int", &rewrap<int>::f);
def("wrap_int_ref", &rewrap<int&>::f
, return_value_policy<copy_non_const_reference>()
);
def("wrap_int_const_ref", &rewrap<int const&>::f
, return_value_policy<copy_const_reference>()
);
def("wrap_simple", &rewrap<simple>::f);
def("wrap_simple_ref", &rewrap<simple&>::f
, return_value_policy<copy_non_const_reference>()
);
def("wrap_simple_const_ref", &rewrap<simple const&>::f
, return_value_policy<copy_const_reference>()
);
}
#include "module_tail.cpp"

View File

@@ -1,131 +0,0 @@
// Copyright Joel de Guzman 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/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/implicit.hpp>
using namespace boost::python;
struct X // a container element
{
std::string s;
X():s("default") {}
X(std::string s):s(s) {}
std::string repr() const { return s; }
void reset() { s = "reset"; }
void foo() { s = "foo"; }
bool operator==(X const& x) const { return s == x.s; }
bool operator!=(X const& x) const { return s != x.s; }
};
std::string x_value(X const& x)
{
return "gotya " + x.s;
}
struct A
{
int value;
A() : value(0){};
A(int v) : value(v) {};
};
bool operator==(const A& v1, const A& v2)
{
return (v1.value == v2.value);
}
struct B
{
A a;
};
// Converter from A to python int
struct AToPython
{
static PyObject* convert(const A& s)
{
return boost::python::incref(boost::python::object((int)s.value).ptr());
}
};
// Conversion from python int to A
struct AFromPython
{
AFromPython()
{
boost::python::converter::registry::push_back(
&convertible,
&construct,
boost::python::type_id< A >());
}
static void* convertible(PyObject* obj_ptr)
{
if (!PyInt_Check(obj_ptr)) return 0;
return obj_ptr;
}
static void construct(
PyObject* obj_ptr,
boost::python::converter::rvalue_from_python_stage1_data* data)
{
void* storage = (
(boost::python::converter::rvalue_from_python_storage< A >*)
data)-> storage.bytes;
new (storage) A((int)PyInt_AsLong(obj_ptr));
data->convertible = storage;
}
};
BOOST_PYTHON_MODULE(map_indexing_suite_ext)
{
class_<X>("X")
.def(init<>())
.def(init<X>())
.def(init<std::string>())
.def("__repr__", &X::repr)
.def("reset", &X::reset)
.def("foo", &X::foo)
;
def("x_value", x_value);
implicitly_convertible<std::string, X>();
class_<std::map<std::string, X> >("XMap")
.def(map_indexing_suite<std::map<std::string, X> >())
;
void int_map_indexing_suite(); // moved to int_map_indexing_suite.cpp to
int_map_indexing_suite(); // avoid MSVC 6/7 internal structure overflow
#if 0
// Compile check only...
class_<std::map<int, int> >("IntMap")
.def(map_indexing_suite<std::map<int, int> >())
;
#endif
// Some more..
class_<std::map<std::string, boost::shared_ptr<X> > >("TestMap")
.def(map_indexing_suite<std::map<std::string, boost::shared_ptr<X> >, true>())
;
to_python_converter< A , AToPython >();
AFromPython();
class_< std::map<int, A> >("AMap")
.def(map_indexing_suite<std::map<int, A>, true >())
;
class_< B >("B")
.add_property("a", make_getter(&B::a, return_value_policy<return_by_value>()),
make_setter(&B::a, return_value_policy<return_by_value>()))
;
}
#include "module_tail.cpp"

View File

@@ -1,242 +0,0 @@
# Copyright Joel de Guzman 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)
'''
#####################################################################
# Check an object that we will use as container element
#####################################################################
>>> from map_indexing_suite_ext import *
>>> assert "map_indexing_suite_IntMap_entry" in dir()
>>> assert "map_indexing_suite_TestMap_entry" in dir()
>>> assert "map_indexing_suite_XMap_entry" in dir()
>>> assert "map_indexing_suite_AMap_entry" in dir()
>>> x = X('hi')
>>> x
hi
>>> x.reset() # a member function that modifies X
>>> x
reset
>>> x.foo() # another member function that modifies X
>>> x
foo
# test that a string is implicitly convertible
# to an X
>>> x_value('bochi bochi')
'gotya bochi bochi'
#####################################################################
# Iteration
#####################################################################
>>> def print_xmap(xmap):
... s = '[ '
... for x in xmap:
... s += repr(x)
... s += ' '
... s += ']'
... print s
#####################################################################
# Setting (adding entries)
#####################################################################
>>> xm = XMap()
>>> xm['joel'] = 'apple'
>>> xm['tenji'] = 'orange'
>>> xm['mariel'] = 'grape'
>>> xm['tutit'] = 'banana'
>>> xm['kim'] = 'kiwi'
>>> print_xmap(xm)
[ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
#####################################################################
# Changing an entry
#####################################################################
>>> xm['joel'] = 'pineapple'
>>> print_xmap(xm)
[ (joel, pineapple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
#####################################################################
# Deleting an entry
#####################################################################
>>> del xm['joel']
>>> print_xmap(xm)
[ (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
#####################################################################
# adding an entry
#####################################################################
>>> xm['joel'] = 'apple'
>>> print_xmap(xm)
[ (joel, apple) (kim, kiwi) (mariel, grape) (tenji, orange) (tutit, banana) ]
#####################################################################
# Indexing
#####################################################################
>>> len(xm)
5
>>> xm['joel']
apple
>>> xm['tenji']
orange
>>> xm['mariel']
grape
>>> xm['tutit']
banana
>>> xm['kim']
kiwi
#####################################################################
# Calling a mutating function of a container element
#####################################################################
>>> xm['joel'].reset()
>>> xm['joel']
reset
#####################################################################
# Copying a container element
#####################################################################
>>> x = X(xm['mariel'])
>>> x
grape
>>> x.foo()
>>> x
foo
>>> xm['mariel'] # should not be changed to 'foo'
grape
#####################################################################
# Referencing a container element
#####################################################################
>>> x = xm['mariel']
>>> x
grape
>>> x.foo()
>>> x
foo
>>> xm['mariel'] # should be changed to 'foo'
foo
>>> xm['mariel'] = 'grape' # take it back
>>> xm['joel'] = 'apple' # take it back
#####################################################################
# Contains
#####################################################################
>>> assert 'joel' in xm
>>> assert 'mariel' in xm
>>> assert 'tenji' in xm
>>> assert 'tutit' in xm
>>> assert 'kim' in xm
>>> assert not 'X' in xm
>>> assert not 12345 in xm
#####################################################################
# Some references to the container elements
#####################################################################
>>> z0 = xm['joel']
>>> z1 = xm['mariel']
>>> z2 = xm['tenji']
>>> z3 = xm['tutit']
>>> z4 = xm['kim']
>>> z0 # proxy
apple
>>> z1 # proxy
grape
>>> z2 # proxy
orange
>>> z3 # proxy
banana
>>> z4 # proxy
kiwi
#####################################################################
# Delete some container element
#####################################################################
>>> del xm['tenji']
>>> print_xmap(xm)
[ (joel, apple) (kim, kiwi) (mariel, grape) (tutit, banana) ]
>>> del xm['tutit']
>>> print_xmap(xm)
[ (joel, apple) (kim, kiwi) (mariel, grape) ]
#####################################################################
# Show that the references are still valid
#####################################################################
>>> z0 # proxy
apple
>>> z1 # proxy
grape
>>> z2 # proxy detached
orange
>>> z3 # proxy detached
banana
>>> z4 # proxy
kiwi
#####################################################################
# Show that iteration allows mutable access to the elements
#####################################################################
>>> for x in xm:
... x.data().reset()
>>> print_xmap(xm)
[ (joel, reset) (kim, reset) (mariel, reset) ]
#####################################################################
# Some more...
#####################################################################
>>> tm = TestMap()
>>> tm["joel"] = X("aaa")
>>> tm["kimpo"] = X("bbb")
>>> print_xmap(tm)
[ (joel, aaa) (kimpo, bbb) ]
>>> for el in tm:
... print el.key(),
... dom = el.data()
joel kimpo
#####################################################################
# Test custom converter...
#####################################################################
>>> am = AMap()
>>> am[3] = 4
>>> am[3]
4
>>> for i in am:
... i.data()
4
#####################################################################
# END....
#####################################################################
'''
def run(args = None):
import sys
import doctest
if args is not None:
sys.argxm = 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)

View File

@@ -1,16 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
# include <iostream> // works around a KCC intermediate code generation bug
#endif
BOOST_PYTHON_MODULE(minimal_ext)
{
}
#include "module_tail.cpp"

View File

@@ -1,7 +0,0 @@
# 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)
print "IMPORTING minimal_ext"
import minimal_ext
print "DONE IMPORTING minimal_ext"

View File

@@ -1,57 +0,0 @@
// Copyright David Abrahams 2001.
// 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)
#if defined(_WIN32)
# ifdef __MWERKS__
# pragma ANSI_strict off
# endif
# include <windows.h>
# ifdef __MWERKS__
# pragma ANSI_strict reset
# endif
# ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable:4297)
# pragma warning(disable:4535)
extern "C" void straight_to_debugger(unsigned int, EXCEPTION_POINTERS*)
{
throw;
}
extern "C" void (*old_translator)(unsigned, EXCEPTION_POINTERS*)
= _set_se_translator(straight_to_debugger);
# pragma warning(pop)
# endif
#endif // _WIN32
#include <exception>
#include <boost/python/extract.hpp>
#include <boost/python/str.hpp>
struct test_failure : std::exception
{
test_failure(char const* expr, char const* /*function*/, char const* file, unsigned line)
: msg(file + boost::python::str(":%s:") % line + ": Boost.Python assertion failure: " + expr)
{}
~test_failure() throw() {}
char const* what() const throw()
{
return boost::python::extract<char const*>(msg)();
}
boost::python::str msg;
};
namespace boost
{
void assertion_failed(char const * expr, char const * function, char const * file, long line)
{
throw ::test_failure(expr,function, file, line);
}
} // namespace boost

View File

@@ -1,27 +0,0 @@
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
struct A
{
A(const double, const double, const double, const double, const double
, const double, const double
, const double, const double
) {}
};
BOOST_PYTHON_MODULE(multi_arg_constructor_ext)
{
using namespace boost::python;
class_<A>(
"A"
, init<double, double, double, double, double, double, double, double, double>()
)
;
}

View File

@@ -1,21 +0,0 @@
# 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)
'''
>>> from multi_arg_constructor_ext import *
>>> a = A(1.0, 2, 3, 4, 5, 6, 7.0, 8.1, 9.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
status = run()[0]
if (status == 0): print "Done."
sys.exit(status)

View File

@@ -1,51 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/operators.hpp>
#include <boost/python/scope.hpp>
#include "test_class.hpp"
#if __GNUC__ != 2
# include <ostream>
#else
# include <ostream.h>
#endif
typedef test_class<> X;
typedef test_class<1> Y;
std::ostream& operator<<(std::ostream& s, X const& x)
{
return s << x.value();
}
std::ostream& operator<<(std::ostream& s, Y const& x)
{
return s << x.value();
}
BOOST_PYTHON_MODULE(nested_ext)
{
using namespace boost::python;
// Establish X as the current scope.
scope x_class
= class_<X>("X", init<int>())
.def(str(self))
;
// Y will now be defined in the current scope
class_<Y>("Y", init<int>())
.def(str(self))
;
}
#include "module_tail.cpp"

View File

@@ -1,40 +0,0 @@
# 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)
'''
>>> from nested_ext import *
>>> X
<class 'nested_ext.X'>
>>> X.__module__
'nested_ext'
>>> X.__name__
'X'
>>> X.Y
<class 'nested_ext.Y'>
>>> X.Y.__module__
'nested_ext'
>>> X.Y.__name__
'Y'
'''
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)

View File

@@ -1,204 +0,0 @@
# 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)
"""
>>> from m1 import *
>>> from m2 import *
Prove that we get an appropriate error from trying to return a type
for which we have no registered to_python converter
>>> def check_unregistered(f, msgprefix):
... try:
... f(1)
... except TypeError, x:
... if not str(x).startswith(msgprefix):
... print str(x)
... else:
... print 'expected a TypeError'
...
>>> check_unregistered(make_unregistered, 'No to_python (by-value) converter found for C++ type')
>>> check_unregistered(make_unregistered2, 'No Python class registered for C++ class')
>>> n = new_noddy()
>>> s = new_simple()
>>> unwrap_int(n)
42
>>> unwrap_int_ref(n)
42
>>> unwrap_int_const_ref(n)
42
>>> unwrap_simple(s)
'hello, world'
>>> unwrap_simple_ref(s)
'hello, world'
>>> unwrap_simple_const_ref(s)
'hello, world'
>>> unwrap_int(5)
5
Can't get a non-const reference to a built-in integer object
>>> try:
... unwrap_int_ref(7)
... except: pass
... else: print 'no exception'
>>> unwrap_int_const_ref(9)
9
>>> wrap_int(n)
42
try: wrap_int_ref(n)
... except: pass
... else: print 'no exception'
>>> wrap_int_const_ref(n)
42
>>> unwrap_simple_ref(wrap_simple(s))
'hello, world'
>>> unwrap_simple_ref(wrap_simple_ref(s))
'hello, world'
>>> unwrap_simple_ref(wrap_simple_const_ref(s))
'hello, world'
>>> f(s)
12
>>> unwrap_simple(g(s))
'hello, world'
>>> f(g(s))
12
>>> f_mutable_ref(g(s))
12
>>> f_const_ptr(g(s))
12
>>> f_mutable_ptr(g(s))
12
>>> f2(g(s))
12
Create an extension class which wraps "complicated" (init1 and get_n)
are a complicated constructor and member function, respectively.
>>> c1 = complicated(s, 99)
>>> c1.get_n()
99
>>> c2 = complicated(s)
>>> c2.get_n()
0
a quick regression test for a bug where None could be converted
to the target of any member function. To see it, we need to
access the __dict__ directly, to bypass the type check supplied
by the Method property which wraps the method when accessed as an
attribute.
>>> try: A.__dict__['name'](None)
... except TypeError: pass
... else: print 'expected an exception!'
>>> a = A()
>>> b = B()
>>> c = C()
>>> d = D()
>>> take_a(a).name()
'A'
>>> try:
... take_b(a)
... except: pass
... else: print 'no exception'
>>> try:
... take_c(a)
... except: pass
... else: print 'no exception'
>>> try:
... take_d(a)
... except: pass
... else: print 'no exception'
------
>>> take_a(b).name()
'A'
>>> take_b(b).name()
'B'
>>> try:
... take_c(b)
... except: pass
... else: print 'no exception'
>>> try:
... take_d(b)
... except: pass
... else: print 'no exception'
-------
>>> take_a(c).name()
'A'
>>> try:
... take_b(c)
... except: pass
... else: print 'no exception'
>>> take_c(c).name()
'C'
>>> try:
... take_d(c)
... except: pass
... else: print 'no exception'
-------
>>> take_a(d).name()
'A'
>>> take_b(d).name()
'B'
>>> take_c(d).name()
'C'
>>> take_d(d).name()
'D'
>>> take_d_shared_ptr(d).name()
'D'
>>> d_as_a = d_factory()
>>> dd = take_d(d_as_a)
>>> dd.name()
'D'
"""
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)

View File

@@ -1,125 +0,0 @@
// Copyright David Abrahams 2002.
// 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/numeric.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
#if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580))
# define make_tuple boost::python::make_tuple
#endif
// See if we can invoke array() from C++
object new_array()
{
return numeric::array(
make_tuple(
make_tuple(1,2,3)
, make_tuple(4,5,6)
, make_tuple(7,8,9)
)
);
}
// test argument conversion
void take_array(numeric::array /*x*/)
{
}
// A separate function to invoke the info() member. Must happen
// outside any doctests since this prints directly to stdout and the
// result text includes the address of the 'self' array.
void info(numeric::array const& z)
{
z.info();
}
#define CHECK(expr) \
{ \
object result; \
try { result = object(expr); } \
catch(error_already_set) \
{ \
PyObject* type, *value, *traceback; \
PyErr_Fetch(&type, &value, &traceback); \
handle<> ty(type), v(value), tr(traceback); \
str format("exception type: %s\n"); \
format += "exception value: %s\n"; \
format += "traceback:\n%s" ; \
result = format % boost::python::make_tuple(ty, v, tr); \
} \
check(result); \
}
// Tests which work on both Numeric and numarray array objects. Of
// course all of the operators "just work" since numeric::array
// inherits that behavior from object.
void exercise(numeric::array& y, object check)
{
y[make_tuple(2,1)] = 3;
CHECK(y);
CHECK(y.astype('D'));
CHECK(y.copy());
CHECK(y.typecode());
}
// numarray-specific tests. check is a callable object which we can
// use to record intermediate results, which are later compared with
// the results of corresponding python operations.
void exercise_numarray(numeric::array& y, object check)
{
CHECK(y.astype());
CHECK(y.argmax());
CHECK(y.argmax(0));
CHECK(y.argmin());
CHECK(y.argmin(0));
CHECK(y.argsort());
CHECK(y.argsort(1));
y.byteswap();
CHECK(y);
CHECK(y.diagonal());
CHECK(y.diagonal(1));
CHECK(y.diagonal(0, 1));
CHECK(y.diagonal(0, 1, 0));
CHECK(y.is_c_array());
CHECK(y.isbyteswapped());
CHECK(y.trace());
CHECK(y.trace(1));
CHECK(y.trace(0, 1));
CHECK(y.trace(0, 1, 0));
CHECK(y.new_('D'));
y.sort();
CHECK(y);
CHECK(y.type());
CHECK(y.factory(make_tuple(1.2, 3.4)));
CHECK(y.factory(make_tuple(1.2, 3.4), "Double"));
CHECK(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(1,2,1)));
CHECK(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(2,1,1), false));
CHECK(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(2), true, true));
}
BOOST_PYTHON_MODULE(numpy_ext)
{
def("new_array", new_array);
def("take_array", take_array);
def("exercise", exercise);
def("exercise_numarray", exercise_numarray);
def("set_module_and_type", &numeric::array::set_module_and_type);
def("info", info);
}
#include "module_tail.cpp"

View File

@@ -1,191 +0,0 @@
# 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)
# Unfortunately the doctest module works differently in Python versions
# 2.2, 2.3, and 2.4. Newer versions evaluate all docstrings, even that
# of objects with names starting with an underscore. To portably disable
# tests based on the availability of Numeric and numarray, the corresponding
# test functions are simply deleted below if necessary.
def numeric_tests():
'''
>>> from numpy_ext import *
>>> x = new_array()
>>> x[1,1] = 0.0
>>> try: take_array(3)
... except TypeError: pass
... else: print 'expected a TypeError'
>>> take_array(x)
>>> print x
[[1 2 3]
[4 0 6]
[7 8 9]]
>>> y = x.copy()
>>> p = _printer()
>>> check = p.check
>>> exercise(x, p)
>>> y[2,1] = 3
>>> check(y);
>>> check(y.astype('D'));
>>> check(y.copy());
>>> check(y.typecode());
>>> p.results
[]
>>> del p
'''
pass
def _numarray_tests():
'''
>>> from numpy_ext import *
>>> x = new_array()
>>> y = x.copy()
>>> p = _printer()
>>> check = p.check
>>> exercise_numarray(x, p)
>>> check(y.astype());
>>> check(y.argmax());
>>> check(y.argmax(0));
>>> check(y.argmin());
>>> check(y.argmin(0));
>>> check(y.argsort());
>>> check(y.argsort(1));
>>> y.byteswap();
>>> check(y);
>>> check(y.diagonal());
>>> check(y.diagonal(1));
>>> check(y.diagonal(0, 1));
>>> check(y.diagonal(0, 1, 0));
>>> check(y.is_c_array());
>>> check(y.isbyteswapped());
>>> check(y.trace());
>>> check(y.trace(1));
>>> check(y.trace(0, 1));
>>> check(y.trace(0, 1, 0));
>>> check(y.new('D'));
>>> y.sort();
>>> check(y);
>>> check(y.type());
>>> check(y.array((1.2, 3.4)));
>>> check(y.array((1.2, 3.4), "Double"));
>>> check(y.array((1.2, 3.4), "Double", (1,2,1)));
>>> check(y.array((1.2, 3.4), "Double", (2,1,1), false));
>>> check(y.array((1.2, 3.4), "Double", (2,), true, true));
>>> p.results
[]
>>> del p
'''
pass
false = 0;
true = 1;
class _printer(object):
def __init__(self):
self.results = [];
def __call__(self, *stuff):
self.results += [ str(x) for x in stuff ]
def check(self, x):
if self.results[0] == str(x):
del self.results[0]
else:
print ' Expected:\n %s\n but got:\n %s' % (x, self.results[0])
def _run(args = None):
import sys
import doctest
if args is not None:
sys.argv = args
# See which of the numeric modules are installed
has_numeric = 0
try:
import Numeric
m = Numeric
has_numeric = 1
except ImportError:
global numeric_tests
numeric_tests = None
has_numarray = 0
try:
import numarray
m = numarray
has_numarray = 1
except ImportError:
global _numarray_tests
_numarray_tests = None
# Bail if neither one is installed
if not (has_numeric or has_numarray):
return 0
# test the info routine outside the doctest. See numpy.cpp for an
# explanation
import numpy_ext
if (has_numarray):
numpy_ext.info(m.array((1,2,3)))
failures = 0
#
# Run tests 4 different ways if both modules are installed, just
# to show that set_module_and_type() is working properly
#
# run all the tests with default module search
print 'testing default extension module'
failures += doctest.testmod(sys.modules.get(__name__))[0]
# test against Numeric if installed
if has_numeric:
print 'testing Numeric module explicitly'
numpy_ext.set_module_and_type('Numeric', 'ArrayType')
failures += doctest.testmod(sys.modules.get(__name__))[0]
global __test__
if has_numarray:
# Add the _numarray_tests to the list of things to test in
# this case.
__test__ = { 'numarray_tests':_numarray_tests,
'numeric_tests': numeric_tests }
print 'testing numarray module explicitly'
numpy_ext.set_module_and_type('numarray', 'NDArray')
failures += doctest.testmod(sys.modules.get(__name__))[0]
del __test__
# see that we can go back to the default
print 'testing default module again'
numpy_ext.set_module_and_type('', '')
failures += doctest.testmod(sys.modules.get(__name__))[0]
return failures
if __name__ == '__main__':
print "running..."
import sys
status = _run()
if (status == 0): print "Done."
sys.exit(status)

View File

@@ -1,333 +0,0 @@
// Copyright David Abrahams 2002.
// 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/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/object.hpp>
#include <boost/python/class.hpp>
using namespace boost::python;
class NotCopyable
{
} not_copyable;
object ref_to_noncopyable()
{
return object(boost::ref(not_copyable));
}
object call_object_3(object f)
{
return f(3);
}
object message()
{
return object("hello, world!");
}
object number()
{
return object(42);
}
object obj_getattr(object x, char const* name)
{
return x.attr(name);
}
object obj_const_getattr(object const& x, char const* name)
{
return x.attr(name);
}
void obj_setattr(object x, char const* name, object value)
{
x.attr(name) = value;
}
void obj_setattr42(object x, char const* name)
{
x.attr(name) = 42;
}
void obj_moveattr(object& x, char const* src, char const* dst)
{
x.attr(dst) = x.attr(src);
}
object obj_getitem(object x, object key)
{
return x[key];
}
object obj_getitem3(object x)
{
return x[3];
}
object obj_const_getitem(object const& x, object key)
{
return x[key];
}
void obj_setitem(object x, object key, object value)
{
x[key] = value;
}
void obj_setitem42(object x, object key)
{
x[key] = 42;
}
void obj_moveitem(object& x, object src, object dst)
{
x[dst] = x[src];
}
void obj_moveitem2(object const& x_src, object k_src, object& x_dst, object k_dst)
{
x_dst[k_dst] = x_src[k_src];
}
bool test(object y)
{
return y;
}
bool test_not(object y)
{
return !y;
}
bool test_attr(object y, char* name)
{
return y.attr(name);
}
bool test_not_attr(object y, char* name)
{
return !y.attr(name);
}
bool test_item(object y, object key)
{
return y[key];
}
bool test_not_item(object y, object key)
{
return !y[key];
}
bool check_string_slice()
{
object s("hello, world");
if (s.slice(_,-3) != "hello, wo")
return false;
if (s.slice(-3,_) != "rld")
return false;
if (s.slice(_,_) != s)
return false;
if (", " != s.slice(5,7))
return false;
return s.slice(2,-1).slice(1,-1) == "lo, wor";
}
bool check_binary_operators()
{
int y;
object x(3);
#define TEST_BINARY(op) \
for (y = 1; y < 6; ++y) \
{ \
if ((x op y) != (3 op y)) \
return false; \
} \
for (y = 1; y < 6; ++y) \
{ \
if ((y op x) != (y op 3)) \
return false; \
} \
for (y = 1; y < 6; ++y) \
{ \
object oy(y); \
if ((oy op x) != (oy op 3)) \
return false; \
}
TEST_BINARY(>)
TEST_BINARY(>=)
TEST_BINARY(<)
TEST_BINARY(<=)
TEST_BINARY(==)
TEST_BINARY(!=)
TEST_BINARY(+)
TEST_BINARY(-)
TEST_BINARY(*)
TEST_BINARY(/)
TEST_BINARY(%)
TEST_BINARY(<<)
TEST_BINARY(>>)
TEST_BINARY(&)
TEST_BINARY(^)
TEST_BINARY(|)
return true;
}
bool check_inplace(object l, object o)
{
int y;
#define TEST_INPLACE(op) \
for (y = 1; y < 6; ++y) \
{ \
object x(666); \
x op##= y; \
if (x != (666 op y)) \
return false; \
} \
for (y = 1; y < 6; ++y) \
{ \
object x(666); \
x op##= object(y); \
if (!(x == (666 op y))) \
return false; \
}
TEST_INPLACE(+)
TEST_INPLACE(-)
TEST_INPLACE(*)
TEST_INPLACE(/)
TEST_INPLACE(%)
TEST_INPLACE(<<)
TEST_INPLACE(>>)
TEST_INPLACE(&)
TEST_INPLACE(^)
TEST_INPLACE(|)
l += l;
for (y = 0; y < 6; ++y)
{
if (l[y] != y % 3)
return false;
}
#define TEST_ITEM_INPLACE(index, op, n, r1, r2) \
l[index] op##= n; \
if (l[index] != r1) \
return false; \
l[index] op##= object(n); \
if (!(l[index] == r2)) \
return false;
TEST_ITEM_INPLACE(0,+,7,7,14)
TEST_ITEM_INPLACE(1,-,2,-1,-3)
TEST_ITEM_INPLACE(2,*,3,6,18)
TEST_ITEM_INPLACE(2,/,2,9,4)
TEST_ITEM_INPLACE(0,%,4,2,2)
l[0] += 1;
TEST_ITEM_INPLACE(0,<<,2,12,48)
TEST_ITEM_INPLACE(0,>>,1,24,12)
l[4] = 15;
TEST_ITEM_INPLACE(4,&,(16+4+1),5,5)
TEST_ITEM_INPLACE(0,^,1,13,12)
TEST_ITEM_INPLACE(0,|,1,13,13)
o.attr("x0") = 0;
o.attr("x1") = 1;
o.attr("x2") = 2;
o.attr("x3") = 0;
o.attr("x4") = 1;
#define TEST_ATTR_INPLACE(index, op, n, r1, r2) \
o.attr("x" #index) op##= n; \
if (o.attr("x" #index) != r1) \
return false; \
o.attr("x" #index) op##= object(n); \
if (o.attr("x" #index) != r2) \
return false;
TEST_ATTR_INPLACE(0,+,7,7,14)
TEST_ATTR_INPLACE(1,-,2,-1,-3)
TEST_ATTR_INPLACE(2,*,3,6,18)
TEST_ATTR_INPLACE(2,/,2,9,4)
TEST_ATTR_INPLACE(0,%,4,2,2)
o.attr("x0") += 1;
TEST_ATTR_INPLACE(0,<<,2,12,48)
TEST_ATTR_INPLACE(0,>>,1,24,12)
o.attr("x4") = 15;
TEST_ATTR_INPLACE(4,&,(16+4+1),5,5)
TEST_ATTR_INPLACE(0,^,1,13,12)
TEST_ATTR_INPLACE(0,|,1,13,13)
if (l[0] != o.attr("x0"))
return false;
if (l[1] != o.attr("x1"))
return false;
if (l[2] != o.attr("x2"))
return false;
if (l[3] != o.attr("x3"))
return false;
if (l[4] != o.attr("x4"))
return false;
// set item 5 to be a list, by calling l.__class__
l[5] = l.attr("__class__")();
// append an element
l[5].attr("append")(2);
// Check its value
if (l[5][0] != 2)
return false;
return true;
}
BOOST_PYTHON_MODULE(object_ext)
{
class_<NotCopyable, boost::noncopyable>("NotCopyable", no_init);
def("ref_to_noncopyable", ref_to_noncopyable);
def("call_object_3", call_object_3);
def("message", message);
def("number", number);
def("obj_getattr", obj_getattr);
def("obj_const_getattr", obj_const_getattr);
def("obj_setattr", obj_setattr);
def("obj_setattr42", obj_setattr42);
def("obj_moveattr", obj_moveattr);
def("obj_getitem", obj_getitem);
def("obj_getitem3", obj_getitem);
def("obj_const_getitem", obj_const_getitem);
def("obj_setitem", obj_setitem);
def("obj_setitem42", obj_setitem42);
def("obj_moveitem", obj_moveitem);
def("obj_moveitem2", obj_moveitem2);
def("test", test);
def("test_not", test_not);
def("test_attr", test_attr);
def("test_not_attr", test_not_attr);
def("test_item", test_item);
def("test_not_item", test_not_item);
def("check_binary_operators", check_binary_operators);
def("check_inplace", check_inplace);
def("check_string_slice", check_string_slice);
;
}
#include "module_tail.cpp"

View File

@@ -1,135 +0,0 @@
# 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)
'''
>>> from object_ext import *
>>> type(ref_to_noncopyable())
<class 'object_ext.NotCopyable'>
>>> def print1(x):
... print x
>>> call_object_3(print1)
3
>>> message()
'hello, world!'
>>> number()
42
>>> test('hi')
1
>>> test(None)
0
>>> test_not('hi')
0
>>> test_not(0)
1
Attributes
>>> class X: pass
...
>>> x = X()
>>> try: obj_getattr(x, 'foo')
... except AttributeError: pass
... else: print 'expected an exception'
>>> obj_setattr(x, 'foo', 1)
>>> x.foo
1
>>> obj_getattr(x, 'foo')
1
>>> obj_const_getattr(x, 'foo')
1
>>> obj_setattr42(x, 'foo')
>>> x.foo
42
>>> obj_moveattr(x, 'foo', 'bar')
>>> x.bar
42
>>> test_attr(x, 'foo')
1
>>> test_not_attr(x, 'foo')
0
>>> x.foo = None
>>> test_attr(x, 'foo')
0
>>> test_not_attr(x, 'foo')
1
Items
>>> d = {}
>>> obj_setitem(d, 'foo', 1)
>>> d['foo']
1
>>> obj_getitem(d, 'foo')
1
>>> obj_const_getitem(d, 'foo')
1
>>> obj_setitem42(d, 'foo')
>>> obj_getitem(d, 'foo')
42
>>> d['foo']
42
>>> obj_moveitem(d, 'foo', 'bar')
>>> d['bar']
42
>>> obj_moveitem2(d, 'bar', d, 'baz')
>>> d['baz']
42
>>> test_item(d, 'foo')
1
>>> test_not_item(d, 'foo')
0
>>> d['foo'] = None
>>> test_item(d, 'foo')
0
>>> test_not_item(d, 'foo')
1
Slices
>>> assert check_string_slice()
Operators
>>> assert check_binary_operators()
>>> class X: pass
...
>>> assert check_inplace(range(3), X())
Now make sure that object is actually managing reference counts
>>> import weakref
>>> class Z: pass
...
>>> z = Z()
>>> def death(r): print 'death'
...
>>> r = weakref.ref(z, death)
>>> z.foo = 1
>>> obj_getattr(z, 'foo')
1
>>> del z
death
'''
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)

View File

@@ -1,11 +0,0 @@
// Copyright David Abrahams 2002.
// 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/object.hpp>
int f(boost::python::object const& x)
{
x._("hello") = 1;
return 0;
}

View File

@@ -1,33 +0,0 @@
// Copyright David Abrahams 2002.
// 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/converter/object_manager.hpp>
#include <boost/python/borrowed.hpp>
#include <boost/static_assert.hpp>
#include <boost/python/handle.hpp>
using namespace boost::python;
using namespace boost::python::converter;
struct X {};
int main()
{
BOOST_STATIC_ASSERT(is_object_manager<handle<> >::value);
BOOST_STATIC_ASSERT(!is_object_manager<int>::value);
BOOST_STATIC_ASSERT(!is_object_manager<X>::value);
BOOST_STATIC_ASSERT(is_reference_to_object_manager<handle<>&>::value);
BOOST_STATIC_ASSERT(is_reference_to_object_manager<handle<> const&>::value);
BOOST_STATIC_ASSERT(is_reference_to_object_manager<handle<> volatile&>::value);
BOOST_STATIC_ASSERT(is_reference_to_object_manager<handle<> const volatile&>::value);
BOOST_STATIC_ASSERT(!is_reference_to_object_manager<handle<> >::value);
BOOST_STATIC_ASSERT(!is_reference_to_object_manager<X>::value);
BOOST_STATIC_ASSERT(!is_reference_to_object_manager<X&>::value);
BOOST_STATIC_ASSERT(!is_reference_to_object_manager<X const&>::value);
return 0;
}

View File

@@ -1,76 +0,0 @@
// Copyright David Abrahams and Gottfried Ganssauge 2003.
// 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/return_opaque_pointer.hpp>
# include <boost/python/def.hpp>
# include <boost/python/module.hpp>
# include <boost/python/return_value_policy.hpp>
typedef struct opaque_ *opaque;
typedef struct opaque2_ *opaque2;
opaque the_op = ((opaque) 0x47110815);
opaque2 the_op2 = ((opaque2) 0x08154711);
opaque get() { return the_op; }
void use(opaque op)
{
if (op != the_op)
throw std::runtime_error (std::string ("failed"));
}
int useany(opaque op)
{
return op ? 1 : 0;
}
opaque getnull()
{
return 0;
}
void failuse (opaque op)
{
if (op == the_op)
throw std::runtime_error (std::string ("success"));
}
opaque2 get2 () { return the_op2; }
void use2 (opaque2 op)
{
if (op != the_op2)
throw std::runtime_error (std::string ("failed"));
}
void failuse2 (opaque2 op)
{
if (op == the_op2)
throw std::runtime_error (std::string ("success"));
}
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(opaque_)
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(opaque2_)
namespace bpl = boost::python;
BOOST_PYTHON_MODULE(opaque_ext)
{
bpl::def (
"get", &::get, bpl::return_value_policy<bpl::return_opaque_pointer>());
bpl::def ("use", &::use);
bpl::def ("useany", &::useany);
bpl::def ("getnull", &::getnull, bpl::return_value_policy<bpl::return_opaque_pointer>());
bpl::def ("failuse", &::failuse);
bpl::def (
"get2",
&::get2,
bpl::return_value_policy<bpl::return_opaque_pointer>());
bpl::def ("use2", &::use2);
bpl::def ("failuse2", &::failuse2);
}
# include "module_tail.cpp"

View File

@@ -1,87 +0,0 @@
# -*- coding: iso-latin-1 -*-
# Copyright Gottfried Ganßauge 2003..2006. 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)
"""
>>> from opaque_ext import *
Check for correct conversion
>>> use(get())
Check that None is converted to a NULL opaque pointer
>>> useany(get())
1
>>> useany(None)
0
Check that we don't lose type information by converting NULL
opaque pointers to None
>>> assert getnull() is None
>>> useany(getnull())
0
>>> failuse(get())
Traceback (most recent call last):
...
RuntimeError: success
Check that there is no conversion from integers ...
>>> try: use(0)
... except TypeError: pass
... else: print 'expected a TypeError'
... and from strings to opaque objects
>>> try: use("")
... except TypeError: pass
... else: print 'expected a TypeError'
Now check the same for another opaque pointer type
>>> use2(get2())
>>> failuse2(get2())
Traceback (most recent call last):
...
RuntimeError: success
>>> try: use2(0)
... except TypeError: pass
... else: print 'expected a TypeError'
>>> try: use2("")
... except TypeError: pass
... else: print 'expected a TypeError'
Check that opaque types are distinct
>>> try: use(get2())
... except TypeError: pass
... else: print 'expected a TypeError'
>>> try: use2(get())
... except TypeError: pass
... else: print 'expected a TypeError'
This used to result in a segmentation violation
>>> type(get()) != type (get2())
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
status = run()[0]
if (status == 0): print "Done."
sys.exit(status)

View File

@@ -1,175 +0,0 @@
// Copyright David Abrahams 2002.
// 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/operators.hpp>
#include <boost/python/class.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include "test_class.hpp"
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/operators.hpp>
#include <boost/operators.hpp>
//#include <boost/python/str.hpp>
// Just use math.h here; trying to use std::pow() causes too much
// trouble for non-conforming compilers and libraries.
#include <math.h>
#if __GNUC__ != 2
# include <ostream>
#else
# include <ostream.h>
#endif
using namespace boost::python;
using namespace boost::python;
struct X : test_class<>
{
typedef test_class<> base_t;
X(int x) : base_t(x) {}
X const operator+(X const& r) const { return X(value() + r.value()); }
// typedef int (X::*safe_bool)() const;
// operator safe_bool() const { return value() != 0 ? &X::value : 0; }
};
X operator-(X const& l, X const& r) { return X(l.value() - r.value()); }
X operator-(int l, X const& r) { return X(l - r.value()); }
X operator-(X const& l, int r) { return X(l.value() - r); }
X operator-(X const& x) { return X(-x.value()); }
X& operator-=(X& l, X const& r) { l.set(l.value() - r.value()); return l; }
bool operator<(X const& x, X const& y) { return x.value() < y.value(); }
bool operator<(X const& x, int y) { return x.value() < y; }
bool operator<(int x, X const& y) { return x < y.value(); }
X abs(X x) { return X(x.value() < 0 ? -x.value() : x.value()); }
X pow(X x, int y)
{
return X(int(pow(double(x.value()), double(y))));
}
X pow(X x, X y)
{
return X(int(pow(double(x.value()), double(y.value()))));
}
int pow(int x, X y)
{
return int(pow(double(x), double(y.value())));
}
std::ostream& operator<<(std::ostream& s, X const& x)
{
return s << x.value();
}
struct number
: boost::integer_arithmetic<number>
{
explicit number(long x_) : x(x_) {}
operator long() const { return x; }
template <class T>
number& operator+=(T const& rhs)
{ x += rhs; return *this; }
template <class T>
number& operator-=(T const& rhs)
{ x -= rhs; return *this; }
template <class T>
number& operator*=(T const& rhs)
{ x *= rhs; return *this; }
template <class T>
number& operator/=(T const& rhs)
{ x /= rhs; return *this; }
template <class T>
number& operator%=(T const& rhs)
{ x %= rhs; return *this; }
long x;
};
BOOST_PYTHON_MODULE(operators_ext)
{
class_<X>("X", init<int>())
.def("value", &X::value)
.def(self + self)
.def(self - self)
.def(self - int())
.def(other<int>() - self)
.def(-self)
.def(self < other<int>())
.def(self < self)
.def(1 < self)
.def(self -= self)
.def(abs(self))
.def(str(self))
.def(pow(self,self))
.def(pow(self,int()))
.def(pow(int(),self))
.def(
!self
// "not self" is legal here but causes friction on a few
// nonconforming compilers; it's cute because it looks
// like python, but doing it here doesn't prove much and
// just causes tests to fail or complicated workarounds to
// be enacted.
)
;
class_<number>("number", init<long>())
// interoperate with self
.def(self += self)
.def(self + self)
.def(self -= self)
.def(self - self)
.def(self *= self)
.def(self * self)
.def(self /= self)
.def(self / self)
.def(self %= self)
.def(self % self)
// Convert to Python int
.def(int_(self))
// interoperate with long
.def(self += long())
.def(self + long())
.def(long() + self)
.def(self -= long())
.def(self - long())
.def(long() - self)
.def(self *= long())
.def(self * long())
.def(long() * self)
.def(self /= long())
.def(self / long())
.def(long() / self)
.def(self %= long())
.def(self % long())
.def(long() % self)
;
class_<test_class<1> >("Z", init<int>())
.def(int_(self))
.def(float_(self))
.def(complex_(self))
;
}
#include "module_tail.cpp"

View File

@@ -1,102 +0,0 @@
# 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)
'''
>>> from operators_ext import *
Check __nonzero__ support
>>> assert X(2)
>>> assert not X(0)
----
>>> x = X(42)
>>> x.value()
42
>>> y = x - X(5)
>>> y.value()
37
>>> y = x - 4
>>> y.value()
38
>>> y = 3 - x
>>> y.value()
-39
>>> (-y).value()
39
>>> (x + y).value()
3
>>> abs(y).value()
39
>>> x < 10
0
>>> x < 43
1
>>> 10 < x
1
>>> 43 < x
0
>>> x < y
0
>>> y < x
1
------
>>> x > 10
1
>>> x > 43
0
>>> 10 > x
0
>>> 43 > x
1
>>> x > y
1
>>> y > x
0
>>> y = x - 5
>>> x -= y
>>> x.value()
5
>>> str(x)
'5'
>>> z = Z(10)
>>> int(z)
10
>>> float(z)
10.0
>>> complex(z)
(10+0j)
>>> pow(2,x)
32
>>> pow(x,2).value()
25
>>> pow(X(2),x).value()
32
'''
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)

View File

@@ -1,62 +0,0 @@
// Copyright Ralf W. Grosse-Kunstleve 2002-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)
/*
This example shows how to make an Extension Class "pickleable".
The world class below can be fully restored by passing the
appropriate argument to the constructor. Therefore it is sufficient
to define the pickle interface method __getinitargs__.
For more information refer to boost/libs/python/doc/pickle.html.
*/
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/tuple.hpp>
#include <string>
namespace {
// A friendly class.
class world
{
private:
std::string country;
public:
world(const std::string& country) {
this->country = country;
}
std::string greet() const { return "Hello from " + country + "!"; }
std::string get_country() const { return country; }
};
struct world_pickle_suite : boost::python::pickle_suite
{
static
boost::python::tuple
getinitargs(const world& w)
{
using namespace boost::python;
return make_tuple(w.get_country());
}
};
// To support test of "pickling not enabled" error message.
struct noop {};
}
BOOST_PYTHON_MODULE(pickle1_ext)
{
using namespace boost::python;
class_<world>("world", init<const std::string&>())
.def("greet", &world::greet)
.def_pickle(world_pickle_suite())
;
// To support test of "pickling not enabled" error message.
class_<noop>("noop");
}

View File

@@ -1,41 +0,0 @@
# 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)
r'''>>> import pickle1_ext
>>> import pickle
>>> pickle1_ext.world.__module__
'pickle1_ext'
>>> pickle1_ext.world.__safe_for_unpickling__
1
>>> pickle1_ext.world.__name__
'world'
>>> pickle1_ext.world('Hello').__reduce__()
(<class 'pickle1_ext.world'>, ('Hello',))
>>> wd = pickle1_ext.world('California')
>>> pstr = pickle.dumps(wd)
>>> wl = pickle.loads(pstr)
>>> print wd.greet()
Hello from California!
>>> print wl.greet()
Hello from California!
>>> noop = pickle1_ext.noop()
>>> try: pickle.dumps(noop)
... except RuntimeError, e: print str(e)[:55]
Pickling of "pickle1_ext.noop" instances is not enabled
'''
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)

View File

@@ -1,98 +0,0 @@
// Copyright Ralf W. Grosse-Kunstleve 2002-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)
/*
This example shows how to make an Extension Class "pickleable".
The world class below contains member data (secret_number) that
cannot be restored by any of the constructors. Therefore it is
necessary to provide the __getstate__/__setstate__ pair of pickle
interface methods.
For simplicity, the __dict__ is not included in the result of
__getstate__. This is not generally recommended, but a valid
approach if it is anticipated that the object's __dict__ will
always be empty. Note that safety guards are provided to catch
the cases where this assumption is not true.
pickle3.cpp shows how to include the object's __dict__ in the
result of __getstate__.
For more information refer to boost/libs/python/doc/pickle.html.
*/
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/extract.hpp>
namespace { // Avoid cluttering the global namespace.
// A friendly class.
class world
{
public:
world(const std::string& country) : secret_number(0) {
this->country = country;
}
std::string greet() const { return "Hello from " + country + "!"; }
std::string get_country() const { return country; }
void set_secret_number(int number) { secret_number = number; }
int get_secret_number() const { return secret_number; }
private:
std::string country;
int secret_number;
};
struct world_pickle_suite : boost::python::pickle_suite
{
static
boost::python::tuple
getinitargs(const world& w)
{
using namespace boost::python;
return make_tuple(w.get_country());
}
static
boost::python::tuple
getstate(const world& w)
{
using namespace boost::python;
return make_tuple(w.get_secret_number());
}
static
void
setstate(world& w, boost::python::tuple state)
{
using namespace boost::python;
if (len(state) != 1)
{
PyErr_SetObject(PyExc_ValueError,
("expected 1-item tuple in call to __setstate__; got %s"
% state).ptr()
);
throw_error_already_set();
}
long number = extract<long>(state[0]);
if (number != 42)
w.set_secret_number(number);
}
};
}
BOOST_PYTHON_MODULE(pickle2_ext)
{
boost::python::class_<world>(
"world", boost::python::init<const std::string&>())
.def("greet", &world::greet)
.def("get_secret_number", &world::get_secret_number)
.def("set_secret_number", &world::set_secret_number)
.def_pickle(world_pickle_suite())
;
}

View File

@@ -1,50 +0,0 @@
# 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)
r'''>>> import pickle2_ext
>>> import pickle
>>> pickle2_ext.world.__module__
'pickle2_ext'
>>> pickle2_ext.world.__safe_for_unpickling__
1
>>> pickle2_ext.world.__name__
'world'
>>> pickle2_ext.world('Hello').__reduce__()
(<class 'pickle2_ext.world'>, ('Hello',), (0,))
>>> for number in (24, 42):
... wd = pickle2_ext.world('California')
... wd.set_secret_number(number)
... pstr = pickle.dumps(wd)
... wl = pickle.loads(pstr)
... print wd.greet(), wd.get_secret_number()
... print wl.greet(), wl.get_secret_number()
Hello from California! 24
Hello from California! 24
Hello from California! 42
Hello from California! 0
# Now show that the __dict__ is not taken care of.
>>> wd = pickle2_ext.world('California')
>>> wd.x = 1
>>> wd.__dict__
{'x': 1}
>>> try: pstr = pickle.dumps(wd)
... except RuntimeError, err: print err[0]
...
Incomplete pickle support (__getstate_manages_dict__ not set)
'''
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)

View File

@@ -1,110 +0,0 @@
// Copyright Ralf W. Grosse-Kunstleve 2002-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)
/*
This example shows how to make an Extension Class "pickleable".
The world class below contains member data (secret_number) that
cannot be restored by any of the constructors. Therefore it is
necessary to provide the __getstate__/__setstate__ pair of pickle
interface methods.
The object's __dict__ is included in the result of __getstate__.
This requires more code (compare with pickle2.cpp), but is
unavoidable if the object's __dict__ is not always empty.
For more information refer to boost/libs/python/doc/pickle.html.
*/
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/dict.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/back_reference.hpp>
#if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580))
# define make_tuple boost::python::make_tuple
#endif
namespace { // Avoid cluttering the global namespace.
// A friendly class.
class world
{
public:
world(const std::string& country) : secret_number(0) {
this->country = country;
}
std::string greet() const { return "Hello from " + country + "!"; }
std::string get_country() const { return country; }
void set_secret_number(int number) { secret_number = number; }
int get_secret_number() const { return secret_number; }
private:
std::string country;
int secret_number;
};
struct world_pickle_suite : boost::python::pickle_suite
{
static
boost::python::tuple
getinitargs(const world& w)
{
using namespace boost::python;
return make_tuple(w.get_country());
}
static
boost::python::tuple
getstate(boost::python::object w_obj)
{
using namespace boost::python;
world const& w = extract<world const&>(w_obj)();
return make_tuple(w_obj.attr("__dict__"), w.get_secret_number());
}
static
void
setstate(boost::python::object w_obj, boost::python::tuple state)
{
using namespace boost::python;
world& w = extract<world&>(w_obj)();
if (len(state) != 2)
{
PyErr_SetObject(PyExc_ValueError,
("expected 2-item tuple in call to __setstate__; got %s"
% state).ptr()
);
throw_error_already_set();
}
// restore the object's __dict__
dict d = extract<dict>(w_obj.attr("__dict__"))();
d.update(state[0]);
// restore the internal state of the C++ object
long number = extract<long>(state[1]);
if (number != 42)
w.set_secret_number(number);
}
static bool getstate_manages_dict() { return true; }
};
}
BOOST_PYTHON_MODULE(pickle3_ext)
{
boost::python::class_<world>(
"world", boost::python::init<const std::string&>())
.def("greet", &world::greet)
.def("get_secret_number", &world::get_secret_number)
.def("set_secret_number", &world::set_secret_number)
.def_pickle(world_pickle_suite())
;
}

View File

@@ -1,45 +0,0 @@
# 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)
r'''>>> import pickle3_ext
>>> import pickle
>>> pickle3_ext.world.__module__
'pickle3_ext'
>>> pickle3_ext.world.__safe_for_unpickling__
1
>>> pickle3_ext.world.__getstate_manages_dict__
1
>>> pickle3_ext.world.__name__
'world'
>>> pickle3_ext.world('Hello').__reduce__()
(<class 'pickle3_ext.world'>, ('Hello',), ({}, 0))
>>> for number in (24, 42):
... wd = pickle3_ext.world('California')
... wd.set_secret_number(number)
... wd.x = 2 * number
... wd.y = 'y' * number
... wd.z = 3. * number
... pstr = pickle.dumps(wd)
... wl = pickle.loads(pstr)
... print wd.greet(), wd.get_secret_number(), wd.x, wd.y, wd.z
... print wl.greet(), wl.get_secret_number(), wl.x, wl.y, wl.z
Hello from California! 24 48 yyyyyyyyyyyyyyyyyyyyyyyy 72.0
Hello from California! 24 48 yyyyyyyyyyyyyyyyyyyyyyyy 72.0
Hello from California! 42 84 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 126.0
Hello from California! 0 84 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 126.0
'''
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)

View File

@@ -1,43 +0,0 @@
// Copyright Ralf W. Grosse-Kunstleve 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)
/*
This example shows how to enable pickling without using the
pickle_suite. The pickling interface (__getinitargs__) is
implemented in Python.
For more information refer to boost/libs/python/doc/pickle.html.
*/
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <string>
namespace {
// A friendly class.
class world
{
private:
std::string country;
public:
world(const std::string& country) {
this->country = country;
}
std::string greet() const { return "Hello from " + country + "!"; }
std::string get_country() const { return country; }
};
}
BOOST_PYTHON_MODULE(pickle4_ext)
{
using namespace boost::python;
class_<world>("world", init<const std::string&>())
.enable_pickling()
.def("greet", &world::greet)
.def("get_country", &world::get_country)
;
}

View File

@@ -1,39 +0,0 @@
# 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)
r'''>>> import pickle4_ext
>>> import pickle
>>> def world_getinitargs(self):
... return (self.get_country(),)
>>> pickle4_ext.world.__getinitargs__ = world_getinitargs
>>> pickle4_ext.world.__module__
'pickle4_ext'
>>> pickle4_ext.world.__safe_for_unpickling__
1
>>> pickle4_ext.world.__name__
'world'
>>> pickle4_ext.world('Hello').__reduce__()
(<class 'pickle4_ext.world'>, ('Hello',))
>>> wd = pickle4_ext.world('California')
>>> pstr = pickle.dumps(wd)
>>> wl = pickle.loads(pstr)
>>> print wd.greet()
Hello from California!
>>> print wl.greet()
Hello from California!
'''
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)

Some files were not shown because too many files have changed in this diff Show More