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

This commit was manufactured by cvs2svn to create tag

'Version_1_21_0'.

[SVN r9525]
This commit is contained in:
nobody
2001-03-09 14:58:07 +00:00
parent 9b602d16b4
commit f9de57f922
65 changed files with 5816 additions and 886 deletions

View File

@@ -5,13 +5,17 @@
//
// The author gratefully acknowleges the support of Dragon Systems, Inc., in
// producing this work.
// Revision History:
// 04 Mar 01 Changed name of extension module so it would work with DebugPython,
// eliminated useless test that aggravated MSVC (David Abrahams)
#include "comprehensive.hpp"
#include <boost/python/class_builder.hpp>
#include <stdio.h> // used for portability on broken compilers
#include <math.h> // for pow()
#include <boost/rational.hpp>
namespace extclass_demo {
namespace bpl_test {
FooCallback::FooCallback(PyObject* self, int x)
: Foo(x), m_self(self)
@@ -238,6 +242,23 @@ boost::shared_ptr<Foo> Baz::create_foo()
return boost::shared_ptr<Foo>(new DerivedFromFoo(0));
}
// Used to check conversion to None
boost::shared_ptr<Foo> foo_factory(bool create)
{
return boost::shared_ptr<Foo>(create ? new DerivedFromFoo(0) : 0);
}
// Used to check conversion from None
bool foo_ptr_is_null(Foo* p)
{
return p == 0;
}
bool foo_shared_ptr_is_null(boost::shared_ptr<Foo> p)
{
return p.get() == 0;
}
// We can accept smart pointer parameters
int Baz::get_foo_value(boost::shared_ptr<Foo> foo)
{
@@ -404,7 +425,7 @@ static int testUpcast(Base* b)
static std::auto_ptr<Base> derived1Factory(int i)
{
return std::auto_ptr<Base>(new Derived1(i));
return std::auto_ptr<Base>(i < 0 ? 0 : new Derived1(i));
}
static std::auto_ptr<Base> derived2Factory(int i)
@@ -714,12 +735,14 @@ const Record* get_record()
return &v;
}
template class boost::python::class_builder<Record>; // explicitly instantiate
} // namespace bpl_test
} // namespace extclass_demo
namespace boost { namespace python {
template class class_builder<bpl_test::Record>; // explicitly instantiate
}} // namespace boost::python
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
inline PyObject* to_python(const extclass_demo::Record* p)
inline PyObject* to_python(const bpl_test::Record* p)
{
return to_python(*p);
}
@@ -731,7 +754,7 @@ BOOST_PYTHON_END_CONVERSION_NAMESPACE
/* */
/************************************************************/
namespace extclass_demo {
namespace bpl_test {
struct EnumOwner
{
@@ -753,8 +776,8 @@ struct EnumOwner
}
namespace boost { namespace python {
template class enum_as_int_converters<extclass_demo::EnumOwner::enum_type>;
using extclass_demo::pow;
template class enum_as_int_converters<bpl_test::EnumOwner::enum_type>;
using bpl_test::pow;
}} // namespace boost::python
// This is just a way of getting the converters instantiated
@@ -763,7 +786,7 @@ namespace boost { namespace python {
//{
//};
namespace extclass_demo {
namespace bpl_test {
/************************************************************/
/* */
@@ -813,6 +836,32 @@ namespace extclass_demo {
w.set_secret_number(number);
}
// Test plain char converters.
char get_plain_char() { return 'x'; }
std::string use_plain_char(char c) { return std::string(3, c); }
// This doesn't test anything but the compiler, since it has the same signature as the above.
// Since MSVC is broken and gets the signature wrong, we'll skip it.
std::string use_const_plain_char(
#ifndef BOOST_MSVC6_OR_EARLIER
const
#endif
char c) { return std::string(5, c); }
// Test std::complex<double> converters.
std::complex<double> dpolar(double rho, double theta) {
return std::polar(rho, theta);
}
double dreal(const std::complex<double>& c) { return c.real(); }
double dimag(std::complex<double> c) { return c.imag(); }
// Test std::complex<float> converters.
std::complex<float> fpolar(float rho, float theta) {
return std::polar(rho, theta);
}
double freal(const std::complex<float>& c) { return c.real(); }
double fimag(std::complex<float> c) { return c.imag(); }
/************************************************************/
/* */
/* init the module */
@@ -1034,9 +1083,29 @@ void init_module(boost::python::module_builder& m)
world_class.def(world_getinitargs, "__getinitargs__");
world_class.def(world_getstate, "__getstate__");
world_class.def(world_setstate, "__setstate__");
// Test plain char converters.
m.def(get_plain_char, "get_plain_char");
m.def(use_plain_char, "use_plain_char");
m.def(use_const_plain_char, "use_const_plain_char");
// Test std::complex<double> converters.
m.def(dpolar, "dpolar");
m.def(dreal, "dreal");
m.def(dimag, "dimag");
// Test std::complex<float> converters.
m.def(fpolar, "fpolar");
m.def(freal, "freal");
m.def(fimag, "fimag");
// Test new null-pointer<->None conversions
m.def(foo_factory, "foo_factory");
m.def(foo_ptr_is_null, "foo_ptr_is_null");
m.def(foo_shared_ptr_is_null, "foo_shared_ptr_is_null");
}
PyObject* raw(boost::python::tuple const& args, boost::python::dictionary const& keywords)
PyObject* raw(const boost::python::tuple& args, const boost::python::dictionary& keywords)
{
if(args.size() != 2 || keywords.size() != 2)
{
@@ -1055,21 +1124,17 @@ PyObject* raw(boost::python::tuple const& args, boost::python::dictionary const&
void init_module()
{
boost::python::module_builder demo("demo");
init_module(demo);
boost::python::module_builder boost_python_test("boost_python_test");
init_module(boost_python_test);
// Just for giggles, add a raw metaclass.
demo.add(new boost::python::meta_class<boost::python::instance>);
boost_python_test.add(new boost::python::meta_class<boost::python::instance>);
}
extern "C"
#ifdef _WIN32
__declspec(dllexport)
#endif
void initdemo()
BOOST_PYTHON_MODULE_INIT(boost_python_test)
{
try {
extclass_demo::init_module();
bpl_test::init_module();
}
catch(...) {
boost::python::handle_exception();
@@ -1083,7 +1148,7 @@ CompareIntPairPythonClass::CompareIntPairPythonClass(boost::python::module_build
def(&CompareIntPair::operator(), "__call__");
}
} // namespace extclass_demo
} // namespace bpl_test
#if defined(_WIN32)
@@ -1124,7 +1189,7 @@ BOOL WINAPI DllMain(
switch(fdwReason)
{
case DLL_PROCESS_DETACH:
assert(extclass_demo::total_Ints == 0);
assert(bpl_test::total_Ints == 0);
}
#endif

View File

@@ -6,8 +6,8 @@
// The author gratefully acknowleges the support of Dragon Systems, Inc., in
// producing this work.
#ifndef EXTCLASS_DEMO_DWA052200_H_
# define EXTCLASS_DEMO_DWA052200_H_
#ifndef BPL_TEST_DWA052200_H_
# define BPL_TEST_DWA052200_H_
//
// Example code demonstrating extension class usage
//
@@ -21,7 +21,7 @@
# include <string>
# include <map>
namespace extclass_demo {
namespace bpl_test {
//
// example: Foo, Bar, and Baz are C++ classes we want to wrap.
@@ -226,6 +226,6 @@ struct CompareIntPairPythonClass
CompareIntPairPythonClass(boost::python::module_builder&);
};
} // namespace extclass_demo
} // namespace bpl_test
#endif // EXTCLASS_DEMO_DWA052200_H_
#endif // BPL_TEST_DWA052200_H_

View File

@@ -7,6 +7,15 @@ r'''
// The author gratefully acknowleges the support of Dragon Systems, Inc., in
// producing this work.
// Revision History:
// 04 Mar 01 Changed name of extension module so it would work with DebugPython,
// fixed exception message checking to work with Python 2.0
// (Dave Abrahams)
Load up the extension module
>>> from boost_python_test import *
Automatic checking of the number and type of arguments. Foo's constructor takes
a single long parameter.
@@ -17,9 +26,9 @@ a single long parameter.
>>> try: ext = Foo('foo')
... except TypeError, err:
... assert re.match(
... '(illegal argument type for built-in operation)|(an integer is required)', str(err))
... else: print 'no exception'
... assert_integer_expected(err)
... else:
... print 'no exception'
>>> ext = Foo(1)
@@ -64,6 +73,21 @@ We can subclass Foo.
>>> b.call_pure()
'not pure anymore!'
None corresponds to a NULL pointer or smart pointer
>>> f = foo_factory(1)
>>> f.add_len('xxx')
1000
>>> foo_factory(0) is None
1
>>> foo_ptr_is_null(None)
1
>>> foo_ptr_is_null(f)
0
>>> foo_shared_ptr_is_null(None)
1
>>> foo_shared_ptr_is_null(f)
0
If no __init__ function is defined, the one from the base class takes effect, just
like in a Python class.
@@ -209,7 +233,7 @@ Polymorphism also works:
Pickling tests:
>>> world.__module__
'demo'
'boost_python_test'
>>> world.__safe_for_unpickling__
1
>>> world.__reduce__()
@@ -239,6 +263,47 @@ Pickling tests:
Hello from California! 42
Hello from California! 0
Pickle safety measures:
>>> r=Rational(3, 4)
>>> r
Rational(3, 4)
>>> try: s=pickle.dumps(r)
... except RuntimeError, err: print err[0]
...
Incomplete pickle support (__dict_defines_state__ not set)
>>> class myrational(Rational):
... __dict_defines_state__ = 1 # this is a lie but good enough for testing.
...
>>> r=myrational(3, 4)
>>> r
Rational(3, 4)
>>> s=pickle.dumps(r)
>>> class myworld(world):
... def __init__(self):
... world.__init__(self, 'anywhere')
... self.x = 1
...
>>> w = myworld()
>>> w.greet()
'Hello from anywhere!'
>>> w.__dict__
{'x': 1}
>>> try: s=pickle.dumps(w)
... except RuntimeError, err: print err[0]
...
Incomplete pickle support (__getstate_manages_dict__ not set)
>>> class myunsafeworld(myworld):
... __getstate_manages_dict__ = 1 # this is a lie but good enough for testing.
...
>>> w = myunsafeworld()
>>> w.greet()
'Hello from anywhere!'
>>> w.__dict__
{'x': 1}
>>> s=pickle.dumps(w)
Special member attributes. Tests courtesy of Barry Scott <barry@scottb.demon.co.uk>
>>> class DerivedFromFoo(Foo):
@@ -656,10 +721,11 @@ Testing interaction between callbacks, base declarations, and overloading
>>> c = CallbackTest()
>>> c.testCallback(1)
2
>>> c.testCallback('foo')
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: illegal argument type for built-in operation
>>> try: c.testCallback('foo')
... except TypeError, err: assert_integer_expected(err)
... else: print 'no exception'
>>> c.callback(1)
2
>>> c.callback('foo')
@@ -678,10 +744,11 @@ Testing interaction between callbacks, base declarations, and overloading
-1
>>> r.callback('foo')
'foo 1'
>>> r.testCallback('foo')
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: illegal argument type for built-in operation
>>> try: r.testCallback('foo')
... except TypeError, err: assert_integer_expected(err)
... else: print 'no exception'
>>> r.testCallback(1)
-1
>>> testCallback(r, 1)
@@ -1070,9 +1137,49 @@ test methodologies for wrapping functions that return a pointer
3
>>> eo.second
1
'''
from demo import *
======== test [plain] char converters ==============
>>> get_plain_char()
'x'
>>> use_plain_char('a')
'aaa'
>>> use_const_plain_char('b')
'bbbbb'
======== test std::complex converters ==============
>>> c = dpolar(3, 5)
>>> type(c)
<type 'complex'>
>>> '%.3g' % (dreal(c))
'0.851'
>>> '%.3g' % (dimag(c))
'-2.88'
>>> '%.3g' % (freal(c))
'0.851'
>>> '%.3g' % (fimag(c))
'-2.88'
>>> c = fpolar(7, 13)
>>> type(c)
<type 'complex'>
>>> '%.3g' % (fimag(c))
'2.94'
>>> '%.3g' % (freal(c))
'6.35'
>>> '%.3g' % (dimag(c))
'2.94'
>>> '%.3g' % (dreal(c))
'6.35'
'''
#'
def assert_integer_expected(err):
"""Handle a common error report which appears differently in Python 1.5.x and 2.0"""
assert isinstance(err, TypeError)
message = str(err)
assert (message == "illegal argument type for built-in operation"
or message == "an integer is required")
import string
import re
import sys
@@ -1080,8 +1187,8 @@ import sys
def run(args = None):
if args is not None:
sys.argv = args
import doctest, test_extclass
doctest.testmod(test_extclass)
import doctest, comprehensive
doctest.testmod(comprehensive)
if __name__ == '__main__':
run()

1112
test/doctest.py Normal file

File diff suppressed because it is too large Load Diff