mirror of
https://github.com/boostorg/python.git
synced 2026-01-19 16:32:16 +00:00
Compare commits
1 Commits
boost-1.26
...
boost-1.25
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1e76757b5 |
@@ -73,13 +73,12 @@ if $(NT)
|
||||
PYTHON_ROOT ?= c:/tools/python ;
|
||||
PYTHON_INCLUDES ?= <include>$(PYTHON_ROOT)/include <gcc><*><include>/usr/include/python$(PYTHON_VERSION) ;
|
||||
PYTHON_LIBS ?= c:/cygnus/lib/python$(PYTHON_VERSION)/config/libpython$(PYTHON_VERSION).dll.a ;
|
||||
PYTHON_LIB_PATH ?= $(PYTHON_ROOT)/libs ;
|
||||
PYTHON_LIB_PATH = $(PYTHON_ROOT)/libs ;
|
||||
|
||||
# common properties required for compiling any Python module.
|
||||
PYTHON_PROPERTIES ?=
|
||||
<gcc><*><define>SIZEOF_LONG=4
|
||||
<gcc><*><define>USE_DL_IMPORT
|
||||
<debug><define>_DEBUG
|
||||
<runtime-link>dynamic
|
||||
;
|
||||
|
||||
@@ -91,10 +90,6 @@ else if $(UNIX)
|
||||
PYTHON_LIBS ?= /usr/lib/python$(PYTHON_VERSION)/config/libpython$(PYTHON_VERSION).a ;
|
||||
}
|
||||
|
||||
local PYTHON_VERSION_NODOT
|
||||
= [ SUBST $(PYTHON_VERSION) ([0-9]*)\.([0-9]*) $1$2 ]
|
||||
;
|
||||
|
||||
# how do we invoke python?
|
||||
local PYTHON = $(PYTHON) ;
|
||||
PYTHON ?= python ;
|
||||
@@ -112,22 +107,14 @@ local BOOST_PYTHON_INCLUDES = <include>$(BOOST_ROOT) $(PYTHON_INCLUDES) ;
|
||||
|
||||
# Base names of the source files for libboost_python
|
||||
local CPP_SOURCES =
|
||||
types classes conversions extension_class functions
|
||||
init_function module_builder objects cross_module ;
|
||||
classes conversions extension_class functions
|
||||
init_function module_builder objects types cross_module ;
|
||||
|
||||
lib libboost_python : ../src/$(CPP_SOURCES).cpp
|
||||
# requirements
|
||||
: $(BOOST_PYTHON_INCLUDES)
|
||||
<shared-linkable>true
|
||||
$(PYTHON_PROPERTIES) ;
|
||||
|
||||
lib libboost_python_d : ../src/$(CPP_SOURCES).cpp
|
||||
# requirements
|
||||
: $(BOOST_PYTHON_INCLUDES)
|
||||
<shared-linkable>true
|
||||
$(PYTHON_PROPERTIES)
|
||||
<define>BOOST_DEBUG_PYTHON
|
||||
;
|
||||
|
||||
#######################
|
||||
|
||||
@@ -136,14 +123,8 @@ lib libboost_python_d : ../src/$(CPP_SOURCES).cpp
|
||||
# Declare a boost python module. Return a list of the DLL files generated.
|
||||
rule boost-python
|
||||
{
|
||||
local debug = "" ;
|
||||
if ( <define>BOOST_DEBUG_PYTHON in $(3) ) || ( debug-python in $(BUILD) )
|
||||
{
|
||||
debug = _d ;
|
||||
}
|
||||
|
||||
# declare a DLL; add the boost python library to sources
|
||||
dll $(<) : <lib>libboost_python$(debug) $(>)
|
||||
dll $(<) : <lib>libboost_python $(>)
|
||||
|
||||
# Requirements
|
||||
: $(3) # caller-specified requirements
|
||||
@@ -151,8 +132,6 @@ rule boost-python
|
||||
# standard requirements
|
||||
$(BOOST_PYTHON_INCLUDES)
|
||||
<msvc><*><library-path>$(PYTHON_LIB_PATH)
|
||||
<intel-win32><*><library-path>$(PYTHON_LIB_PATH)
|
||||
<metrowerks><*><library-file>$(PYTHON_LIB_PATH)/python$(PYTHON_VERSION_NODOT)$(debug).lib
|
||||
<gcc><*><library-file>$(PYTHON_LIBS)
|
||||
$(PYTHON_PROPERTIES)
|
||||
|
||||
@@ -166,12 +145,9 @@ rule boost-python
|
||||
rule boost-python-test
|
||||
{
|
||||
type-DEPENDS test : $(<) ;
|
||||
type-DEPENDS test_d : $(<)_d ;
|
||||
NOTFILE test_d ;
|
||||
|
||||
local gSUPPRESS_FAKE_TARGETS = true ;
|
||||
boost-python $(1) : $(2) : $(3) : $(4) ;
|
||||
boost-python $(1)_d : $(2) : $(3) <define>BOOST_DEBUG_PYTHON : $(4) ;
|
||||
}
|
||||
|
||||
#######################
|
||||
@@ -203,9 +179,7 @@ rule python-test-target # test-target : sources :
|
||||
{
|
||||
python-runtest-aux $(<) : $(>) ;
|
||||
Clean clean : $(<) ; # remove the test-target as part of any clean operation
|
||||
local debug = [ SUBST $(<:B) (_d)$ $1 ] ;
|
||||
debug ?= "" ;
|
||||
type-DEPENDS test$(debug) : $(<) ;
|
||||
type-DEPENDS test : $(<) ;
|
||||
MakeLocate $(<) : $(LOCATE_TARGET) ;
|
||||
}
|
||||
actions python-test-target bind PYTHON
|
||||
@@ -242,7 +216,6 @@ rule python-runtest-aux # target : sources
|
||||
switch $(<)
|
||||
{
|
||||
case <*\\\\msvc\\\\*>* : ARGS on $(<) += --broken-auto-ptr ;
|
||||
case <*\\\\intel-win32\\\\*>* : ARGS on $(<) += --broken-auto-ptr ;
|
||||
}
|
||||
|
||||
# compute the PYTHONPATH environment variable that will allow the test to
|
||||
@@ -251,7 +224,6 @@ rule python-runtest-aux # target : sources
|
||||
$(gLOCATE($(>[1]))) # location of python test file
|
||||
$(gRUN_PATH($(<))) # location of module dependencies
|
||||
[ join-path $(TOP) libs python test ] # location of doctest
|
||||
$(>:D) # directory of python driver file(s)
|
||||
$(PYTHONPATH) # base PYTHONPATH from environment
|
||||
: $(SPLITPATH) ] ; # platform path separator
|
||||
|
||||
@@ -266,17 +238,12 @@ boost-python-runtest comprehensive
|
||||
: [ join-path $(DOTDOT) test comprehensive.py ]
|
||||
<lib>boost_python_test ;
|
||||
|
||||
boost-python-runtest comprehensive_d
|
||||
: [ join-path $(DOTDOT) test comprehensive.py ]
|
||||
<lib>boost_python_test_d ;
|
||||
|
||||
############# simple tests from ../example ############
|
||||
|
||||
rule boost-python-example-runtest
|
||||
{
|
||||
boost-python-test $(<) : ../example/$(<).cpp ;
|
||||
boost-python-runtest $(<) : [ join-path $(DOTDOT) example test_$(<).py ] <lib>$(<) ;
|
||||
boost-python-runtest $(<)_d : [ join-path $(DOTDOT) example test_$(<).py ] <lib>$(<)_d ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -107,6 +107,8 @@ BOOST_PYTHON_END_CONVERSION_NAMESPACE
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(do_it_yourself_convts)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
python::module_builder this_module("do_it_yourself_convts");
|
||||
|
||||
@@ -118,4 +120,9 @@ BOOST_PYTHON_MODULE_INIT(do_it_yourself_convts)
|
||||
// Add the member functions.
|
||||
ixset_class.def(&IndexingSet::add, "add");
|
||||
ixset_class.def(&IndexingSet::get, "get");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ extern "C" void (*old_translator)(unsigned int, EXCEPTION_POINTERS*)
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(dvect)
|
||||
{
|
||||
try
|
||||
{
|
||||
python::module_builder this_module("dvect");
|
||||
|
||||
python::class_builder<vects::dvect> dvect_class(this_module, "dvect");
|
||||
@@ -45,4 +47,10 @@ BOOST_PYTHON_MODULE_INIT(dvect)
|
||||
|
||||
# include "dvect_defs.cpp"
|
||||
# include "ivect_defs.cpp"
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,21 +16,32 @@ namespace hello {
|
||||
|
||||
// Python requires an exported function called init<module-name> in every
|
||||
// extension module. This is where we build the module contents.
|
||||
BOOST_PYTHON_MODULE_INIT(hello)
|
||||
extern "C"
|
||||
#ifdef _WIN32
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void inithello()
|
||||
{
|
||||
// create an object representing this extension module
|
||||
boost::python::module_builder hello("hello");
|
||||
try
|
||||
{
|
||||
// create an object representing this extension module
|
||||
boost::python::module_builder hello("hello");
|
||||
|
||||
// Create the Python type object for our extension class
|
||||
boost::python::class_builder<hello::world> world_class(hello, "world");
|
||||
// Create the Python type object for our extension class
|
||||
boost::python::class_builder<hello::world> world_class(hello, "world");
|
||||
|
||||
// Add the __init__ function
|
||||
world_class.def(boost::python::constructor<int>());
|
||||
// Add a regular member function
|
||||
world_class.def(&hello::world::get, "get");
|
||||
// Add the __init__ function
|
||||
world_class.def(boost::python::constructor<int>());
|
||||
// Add a regular member function
|
||||
world_class.def(&hello::world::get, "get");
|
||||
|
||||
// Add a regular function to the module
|
||||
hello.def(hello::length, "length");
|
||||
// Add a regular function to the module
|
||||
hello.def(hello::length, "length");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
boost::python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
// Win32 DLL boilerplate
|
||||
|
||||
@@ -16,10 +16,17 @@ namespace python = boost::python;
|
||||
// extension module. This is where we build the module contents.
|
||||
BOOST_PYTHON_MODULE_INIT(getting_started1)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
python::module_builder this_module("getting_started1");
|
||||
|
||||
// Add regular functions to the module.
|
||||
this_module.def(greet, "greet");
|
||||
this_module.def(square, "square");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ namespace python = boost::python;
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(getting_started2)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
python::module_builder this_module("getting_started2");
|
||||
|
||||
@@ -42,4 +44,9 @@ BOOST_PYTHON_MODULE_INIT(getting_started2)
|
||||
|
||||
// Even better, invite() can also be made a member of hello_class!!!
|
||||
hello_class.def(invite, "invite");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ extern "C" void (*old_translator)(unsigned int, EXCEPTION_POINTERS*)
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(ivect)
|
||||
{
|
||||
try
|
||||
{
|
||||
python::module_builder this_module("ivect");
|
||||
|
||||
python::class_builder<vects::ivect> ivect_class(this_module, "ivect");
|
||||
@@ -45,5 +47,10 @@ BOOST_PYTHON_MODULE_INIT(ivect)
|
||||
|
||||
# include "dvect_defs.cpp"
|
||||
# include "ivect_defs.cpp"
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,13 @@ namespace {
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(nested)
|
||||
{
|
||||
try
|
||||
{
|
||||
boost::python::module_builder this_module("nested");
|
||||
this_module.def(show_nested_tuples, "show_nested_tuples");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
boost::python::handle_exception();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ extern "C" void (*old_translator)(unsigned int, EXCEPTION_POINTERS*)
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(noncopyable_export)
|
||||
{
|
||||
try
|
||||
{
|
||||
python::module_builder this_module("noncopyable_export");
|
||||
|
||||
python::class_builder<store> store_class(this_module, "store");
|
||||
@@ -25,4 +27,9 @@ BOOST_PYTHON_MODULE_INIT(noncopyable_export)
|
||||
|
||||
store_class.def(python::constructor<int>());
|
||||
store_class.def(&store::recall, "recall");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,8 @@ extern "C" void (*old_translator)(unsigned int, EXCEPTION_POINTERS*)
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(noncopyable_import)
|
||||
{
|
||||
try
|
||||
{
|
||||
python::module_builder this_module("noncopyable_import");
|
||||
|
||||
python::import_converters<store>
|
||||
@@ -42,4 +44,9 @@ BOOST_PYTHON_MODULE_INIT(noncopyable_import)
|
||||
// However, to keep this example simple, we only define a
|
||||
// module-level function.
|
||||
this_module.def(add_stores, "add_stores");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,8 @@ namespace { // Avoid cluttering the global namespace.
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(pickle1)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
python::module_builder this_module("pickle1");
|
||||
|
||||
@@ -54,4 +56,9 @@ BOOST_PYTHON_MODULE_INIT(pickle1)
|
||||
|
||||
// Support for pickle.
|
||||
world_class.def(world_getinitargs, "__getinitargs__");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +73,8 @@ namespace { // Avoid cluttering the global namespace.
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(pickle2)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
python::module_builder this_module("pickle2");
|
||||
|
||||
@@ -90,4 +92,9 @@ BOOST_PYTHON_MODULE_INIT(pickle2)
|
||||
world_class.def(world_getinitargs, "__getinitargs__");
|
||||
world_class.def(world_getstate, "__getstate__");
|
||||
world_class.def(world_setstate, "__setstate__");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,8 @@ namespace { // Avoid cluttering the global namespace.
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(pickle3)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
python::module_builder this_module("pickle3");
|
||||
|
||||
@@ -83,6 +85,11 @@ BOOST_PYTHON_MODULE_INIT(pickle3)
|
||||
world_class.def_raw(world_getstate, "__getstate__");
|
||||
world_class.def_raw(world_setstate, "__setstate__");
|
||||
world_class.getstate_manages_dict();
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -77,8 +77,11 @@ namespace {
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(richcmp1)
|
||||
{
|
||||
try {
|
||||
boost::python::module_builder this_module("richcmp1");
|
||||
// The actual work is done in a separate function in order
|
||||
// to suppress a bogus VC60 warning.
|
||||
init_module(this_module);
|
||||
}
|
||||
catch (...) { boost::python::handle_exception(); }
|
||||
}
|
||||
|
||||
@@ -55,8 +55,11 @@ namespace {
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(richcmp2)
|
||||
{
|
||||
try {
|
||||
boost::python::module_builder this_module("richcmp2");
|
||||
// The actual work is done in a separate function in order
|
||||
// to suppress a bogus VC60 warning.
|
||||
init_module(this_module);
|
||||
}
|
||||
catch (...) { boost::python::handle_exception(); }
|
||||
}
|
||||
|
||||
@@ -168,8 +168,11 @@ namespace {
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(richcmp3)
|
||||
{
|
||||
try {
|
||||
boost::python::module_builder this_module("richcmp3");
|
||||
// The actual work is done in a separate function in order
|
||||
// to suppress a bogus VC60 warning.
|
||||
init_module(this_module);
|
||||
}
|
||||
catch (...) { boost::python::handle_exception(); }
|
||||
}
|
||||
|
||||
@@ -13,12 +13,29 @@ namespace python = boost::python;
|
||||
|
||||
// Python requires an exported function called init<module-name> in every
|
||||
// extension module. This is where we build the module contents.
|
||||
BOOST_PYTHON_MODULE_INIT(rwgk1)
|
||||
extern "C"
|
||||
#ifdef _WIN32
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void initrwgk1()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
python::module_builder this_module("rwgk1");
|
||||
|
||||
// Add regular functions to the module.
|
||||
this_module.def(greet, "greet");
|
||||
this_module.def(square, "square");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
// Win32 DLL boilerplate
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
extern "C" BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID) { return 1; }
|
||||
#endif // _WIN32
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace { // Avoid cluttering the global 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 + "!"; }
|
||||
};
|
||||
|
||||
// A function taking a world object as an argument.
|
||||
std::string invite(const world& w) {
|
||||
return w.greet() + " Please come soon!";
|
||||
}
|
||||
}
|
||||
|
||||
#include <py_cpp/class_wrapper.h>
|
||||
|
||||
// Python requires an exported function called init<module-name> in every
|
||||
// extension module. This is where we build the module contents.
|
||||
BOOST_PYTHON_MODULE_INIT(example2)
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
py::Module this_module("example2");
|
||||
|
||||
// Create the Python type object for our extension class.
|
||||
py::ClassWrapper<world> world_class(this_module, "world");
|
||||
|
||||
// Add the __init__ function.
|
||||
world_class.def(py::Constructor<std::string>());
|
||||
// Add a regular member function.
|
||||
world_class.def(&world::greet, "greet");
|
||||
|
||||
// Add invite() as a regular function to the module.
|
||||
this_module.def(invite, "invite");
|
||||
|
||||
// Even better, invite() can also be made a member of world_class!!!
|
||||
world_class.def(invite, "invite");
|
||||
}
|
||||
|
||||
// Win32 DLL boilerplate
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
extern "C" BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID) { return 1; }
|
||||
#endif // _WIN32
|
||||
@@ -1,101 +0,0 @@
|
||||
#include <py_cpp/class_wrapper.h>
|
||||
#include <py_cpp/objects.h>
|
||||
|
||||
#define rangei(n) for (int i = 0; i < n; i++)
|
||||
|
||||
namespace { // Avoid cluttering the global namespace.
|
||||
|
||||
// A wrapper is used to define additional constructors.
|
||||
//
|
||||
struct vector_double_wrapper: std::vector<double>
|
||||
{
|
||||
// Tell the compiler how to convert a base class object to
|
||||
// this wrapper object.
|
||||
vector_double_wrapper(PyObject*, const std::vector<double>& vd)
|
||||
: std::vector<double>(vd) {}
|
||||
|
||||
vector_double_wrapper(PyObject* self)
|
||||
: std::vector<double>() {}
|
||||
|
||||
vector_double_wrapper(PyObject* self, const int n)
|
||||
: std::vector<double>(n) {}
|
||||
|
||||
vector_double_wrapper(PyObject* self, py::Tuple tuple)
|
||||
: std::vector<double>(tuple.size())
|
||||
{
|
||||
std::vector<double>::iterator vd = begin();
|
||||
rangei(tuple.size())
|
||||
vd[i] = from_python(tuple[i].get(), py::Type<double>()); // GCC BUG
|
||||
}
|
||||
};
|
||||
|
||||
double getitem(const std::vector<double>& vd, const std::size_t key) {
|
||||
return vd[key];
|
||||
}
|
||||
|
||||
void setitem(std::vector<double>& vd, const std::size_t key,
|
||||
const double &d) {
|
||||
std::vector<double>::iterator vditer = vd.begin();
|
||||
vditer[key] = d;
|
||||
}
|
||||
|
||||
void delitem(std::vector<double>& vd, const std::size_t key) {
|
||||
std::vector<double>::iterator vditer = vd.begin();
|
||||
vd.erase(&vditer[key]);
|
||||
}
|
||||
|
||||
// Convert vector_double to a regular Python tuple.
|
||||
//
|
||||
py::Tuple as_tuple(const std::vector<double>& vd)
|
||||
{
|
||||
py::Tuple t(vd.size());
|
||||
rangei(vd.size()) t.set_item(i, py::Ptr(py::to_python(vd[i]))); // GCC BUG
|
||||
return t;
|
||||
}
|
||||
|
||||
// Function returning a vector_double object to Python.
|
||||
//
|
||||
std::vector<double> foo(const int n)
|
||||
{
|
||||
std::vector<double> vd(n);
|
||||
std::vector<double>::iterator vditer = vd.begin();
|
||||
rangei(n) vditer[i] = double(i);
|
||||
return vd;
|
||||
}
|
||||
|
||||
// Same as foo(), but avoid copying on return.
|
||||
//
|
||||
std::auto_ptr<std::vector<double> > bar(const int n)
|
||||
{
|
||||
std::auto_ptr<std::vector<double> > vdptr(new std::vector<double>(n));
|
||||
std::vector<double>::iterator vditer = vdptr->begin();
|
||||
rangei(n) vditer[i] = double(10 * i);
|
||||
return vdptr;
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(example3)
|
||||
{
|
||||
py::Module this_module("example3");
|
||||
|
||||
py::ClassWrapper<std::vector<double>, vector_double_wrapper>
|
||||
vector_double(this_module, "vector_double");
|
||||
|
||||
vector_double.def(py::Constructor<>());
|
||||
vector_double.def(py::Constructor<const int>());
|
||||
vector_double.def(py::Constructor<py::Tuple>());
|
||||
vector_double.def(&std::vector<double>::size, "__len__");
|
||||
vector_double.def(getitem, "__getitem__");
|
||||
vector_double.def(setitem, "__setitem__");
|
||||
vector_double.def(delitem, "__delitem__");
|
||||
vector_double.def(as_tuple, "as_tuple");
|
||||
|
||||
this_module.def(foo, "foo");
|
||||
this_module.def(bar, "bar");
|
||||
}
|
||||
|
||||
// Win32 DLL boilerplate
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
extern "C" BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID) { return 1; }
|
||||
#endif // _WIN32
|
||||
@@ -85,6 +85,8 @@ namespace { // Avoid cluttering the global namespace.
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(simple_vector)
|
||||
{
|
||||
try
|
||||
{
|
||||
python::module_builder this_module("simple_vector");
|
||||
|
||||
python::class_builder<std::vector<double>, vector_double_wrapper>
|
||||
@@ -101,4 +103,9 @@ BOOST_PYTHON_MODULE_INIT(simple_vector)
|
||||
|
||||
this_module.def(foo, "foo");
|
||||
this_module.def(bar, "bar");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,17 +74,17 @@ inline void xdecref_impl(PyObject* p) { Py_XDECREF(p); }
|
||||
template <class T>
|
||||
inline void decref(T* p)
|
||||
{
|
||||
char* const raw_p = reinterpret_cast<char*>(p);
|
||||
char* const p_base = raw_p - offsetof(PyObject, ob_refcnt);
|
||||
decref_impl(reinterpret_cast<PyObject*>(p_base));
|
||||
char* const raw_p = reinterpret_cast<char*>(p);
|
||||
char* const p_base = raw_p - offsetof(PyObject, ob_refcnt);
|
||||
decref_impl(reinterpret_cast<PyObject*>(p_base));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void xdecref(T* p)
|
||||
{
|
||||
char* const raw_p = reinterpret_cast<char*>(p);
|
||||
char* const p_base = raw_p - offsetof(PyObject, ob_refcnt);
|
||||
xdecref_impl(reinterpret_cast<PyObject*>(p_base));
|
||||
char* const raw_p = reinterpret_cast<char*>(p);
|
||||
char* const p_base = raw_p - offsetof(PyObject, ob_refcnt);
|
||||
xdecref_impl(reinterpret_cast<PyObject*>(p_base));
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
@@ -294,22 +294,22 @@ inline PyObject* to_python(float f)
|
||||
|
||||
inline PyObject* to_python(long l)
|
||||
{
|
||||
return PyInt_FromLong(l);
|
||||
return PyInt_FromLong(l);
|
||||
}
|
||||
|
||||
inline PyObject* to_python(int x)
|
||||
{
|
||||
return PyInt_FromLong(x);
|
||||
return PyInt_FromLong(x);
|
||||
}
|
||||
|
||||
inline PyObject* to_python(short x)
|
||||
{
|
||||
return PyInt_FromLong(x);
|
||||
return PyInt_FromLong(x);
|
||||
}
|
||||
|
||||
inline PyObject* to_python(bool b)
|
||||
{
|
||||
return PyInt_FromLong(b);
|
||||
return PyInt_FromLong(b);
|
||||
}
|
||||
|
||||
inline PyObject* to_python(void)
|
||||
@@ -319,7 +319,7 @@ inline PyObject* to_python(void)
|
||||
|
||||
inline PyObject* to_python(const char* s)
|
||||
{
|
||||
return PyString_FromString(s);
|
||||
return PyString_FromString(s);
|
||||
}
|
||||
|
||||
inline std::string from_python(PyObject* p, boost::python::type<const std::string&>)
|
||||
|
||||
@@ -169,9 +169,6 @@ struct export_converter_object_noncopyable : export_converter_object_base<T>
|
||||
PyErr_SetString(PyExc_RuntimeError,
|
||||
"to_python(const T&) converter not exported");
|
||||
throw import_error();
|
||||
#if defined(__MWERKS__) && __MWERKS__ <= 0x2406
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
virtual T* from_python_Ts(PyObject* p, boost::python::type<T*> t) {
|
||||
|
||||
@@ -48,7 +48,7 @@ base_object<PythonType>::base_object(PyTypeObject* type_obj)
|
||||
#if !defined(_MSC_VER) || defined(__STLPORT)
|
||||
std::
|
||||
#endif
|
||||
memset(bp, 0, sizeof(base_python_type));
|
||||
memset(bp, 0, sizeof(base_python_type));
|
||||
Py_INCREF(type_obj);
|
||||
PyObject_INIT(bp, type_obj);
|
||||
}
|
||||
|
||||
@@ -58,9 +58,9 @@
|
||||
# endif
|
||||
|
||||
#if defined(_WIN32) || defined(__CYGWIN__)
|
||||
# define BOOST_PYTHON_MODULE_INIT(name) void init_module_##name(); extern "C" __declspec(dllexport) void init##name() { boost::python::handle_exception(init_module_##name); } void init_module_##name()
|
||||
# define BOOST_PYTHON_MODULE_INIT(name) extern "C" __declspec(dllexport) void init##name()
|
||||
#else
|
||||
# define BOOST_PYTHON_MODULE_INIT(name) void init_module_##name(); extern "C" void init##name() { boost::python::handle_exception(init_module_##name); } void init_module_##name()
|
||||
# define BOOST_PYTHON_MODULE_INIT(name) extern "C" void init##name()
|
||||
#endif
|
||||
|
||||
#endif // CONFIG_DWA052200_H_
|
||||
|
||||
@@ -233,9 +233,6 @@ class python_extension_class_converters
|
||||
}
|
||||
boost::python::detail::report_missing_instance_data(self, boost::python::detail::class_registry<T>::class_object(), typeid(T));
|
||||
throw boost::python::argument_error();
|
||||
#if defined(__MWERKS__) && __MWERKS__ <= 0x2406
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Convert to T*
|
||||
@@ -264,9 +261,6 @@ class python_extension_class_converters
|
||||
}
|
||||
boost::python::detail::report_missing_ptr_data(self, boost::python::detail::class_registry<T>::class_object(), typeid(T));
|
||||
throw boost::python::argument_error();
|
||||
#if defined(__MWERKS__) && __MWERKS__ <= 0x2406
|
||||
return *(PtrType*)0;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Extract from obj a reference to the PtrType object which is holding a
|
||||
|
||||
@@ -59,13 +59,13 @@ class function : public python_object
|
||||
template <class R, class F>
|
||||
struct wrapped_function_pointer : function
|
||||
{
|
||||
typedef F ptr_fun; // pointer-to--function or pointer-to-member-function
|
||||
|
||||
wrapped_function_pointer(ptr_fun pf)
|
||||
typedef F ptr_fun; // pointer-to--function or pointer-to-member-function
|
||||
|
||||
wrapped_function_pointer(ptr_fun pf)
|
||||
: m_pf(pf) {}
|
||||
|
||||
private:
|
||||
PyObject* do_call(PyObject* args, PyObject* keywords) const
|
||||
PyObject* do_call(PyObject* args, PyObject* keywords) const
|
||||
{
|
||||
// This is where the boundary between the uniform Python function
|
||||
// interface and the statically-checked C++ function interface is
|
||||
@@ -77,7 +77,7 @@ struct wrapped_function_pointer : function
|
||||
{ return typeid(F).name(); }
|
||||
|
||||
private:
|
||||
const ptr_fun m_pf;
|
||||
const ptr_fun m_pf;
|
||||
};
|
||||
|
||||
// raw_arguments_function
|
||||
@@ -87,13 +87,13 @@ struct wrapped_function_pointer : function
|
||||
template <class Ret, class Args, class Keywords>
|
||||
struct raw_arguments_function : function
|
||||
{
|
||||
typedef Ret (*ptr_fun)(Args, Keywords);
|
||||
|
||||
raw_arguments_function(ptr_fun pf)
|
||||
typedef Ret (*ptr_fun)(Args, Keywords);
|
||||
|
||||
raw_arguments_function(ptr_fun pf)
|
||||
: m_pf(pf) {}
|
||||
|
||||
private:
|
||||
PyObject* do_call(PyObject* args, PyObject* keywords) const
|
||||
PyObject* do_call(PyObject* args, PyObject* keywords) const
|
||||
{
|
||||
ref dict(keywords ?
|
||||
ref(keywords, ref::increment_count) :
|
||||
@@ -108,7 +108,7 @@ struct raw_arguments_function : function
|
||||
{ return typeid(ptr_fun).name(); }
|
||||
|
||||
private:
|
||||
const ptr_fun m_pf;
|
||||
const ptr_fun m_pf;
|
||||
};
|
||||
|
||||
// virtual_function<> --
|
||||
@@ -127,19 +127,19 @@ template <class T, class R, class V, class D>
|
||||
class virtual_function : public function
|
||||
{
|
||||
public:
|
||||
virtual_function(V virtual_function_ptr, D default_implementation)
|
||||
virtual_function(V virtual_function_ptr, D default_implementation)
|
||||
: m_virtual_function_ptr(virtual_function_ptr),
|
||||
m_default_implementation(default_implementation)
|
||||
{}
|
||||
|
||||
private:
|
||||
PyObject* do_call(PyObject* args, PyObject* keywords) const;
|
||||
PyObject* do_call(PyObject* args, PyObject* keywords) const;
|
||||
|
||||
const char* description() const
|
||||
{ return typeid(V).name(); }
|
||||
|
||||
private:
|
||||
const V m_virtual_function_ptr;
|
||||
const V m_virtual_function_ptr;
|
||||
const D m_default_implementation;
|
||||
};
|
||||
|
||||
@@ -160,7 +160,7 @@ template <class F>
|
||||
inline function* new_wrapped_function(F pmf)
|
||||
{
|
||||
// Deduce the return type and pass it off to the helper function above
|
||||
return new_wrapped_function_aux(return_value(pmf), pmf);
|
||||
return new_wrapped_function_aux(return_value(pmf), pmf);
|
||||
}
|
||||
|
||||
template <class R, class Args, class keywords>
|
||||
@@ -220,7 +220,7 @@ class bound_function : public python_object
|
||||
private: // data members for allocation/deallocation optimization
|
||||
bound_function* m_free_list_link;
|
||||
|
||||
static bound_function* free_list;
|
||||
static bound_function* free_list;
|
||||
};
|
||||
|
||||
// Special functions designed to access data members of a wrapped C++ object.
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
// (C) Copyright David Abrahams 2001. Permission to copy, use, modify, sell and
|
||||
// distribute this software is granted provided this copyright notice appears
|
||||
// in all copies. This software is provided "as is" without express or implied
|
||||
// warranty, and with no claim as to its suitability for any purpose.
|
||||
|
||||
#ifndef VOID_ADAPTOR_DWA20011112_HPP
|
||||
# define VOID_ADAPTOR_DWA20011112_HPP
|
||||
|
||||
namespace boost { namespace python { namespace detail {
|
||||
|
||||
extern PyObject arbitrary_object;
|
||||
|
||||
template <class T>
|
||||
struct void_adaptor
|
||||
{
|
||||
typedef PyObject* result_type;
|
||||
|
||||
void_adaptor(T const& f)
|
||||
: m_f(f)
|
||||
{}
|
||||
|
||||
PyObject* operator()() const
|
||||
{
|
||||
m_f();
|
||||
return &arbitrary_object;
|
||||
}
|
||||
private:
|
||||
T m_f;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void_adaptor<T> make_void_adaptor(T const& f)
|
||||
{
|
||||
return void_adaptor<T>(f);
|
||||
}
|
||||
}}} // namespace boost::python::detail
|
||||
|
||||
#endif // VOID_ADAPTOR_DWA20011112_HPP
|
||||
|
||||
@@ -90,9 +90,5 @@ typedef int pid_t;
|
||||
|
||||
#if !defined(PY_MAJOR_VERSION) || PY_MAJOR_VERSION < 2
|
||||
# define PyObject_INIT(op, typeobj) \
|
||||
( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
|
||||
#endif
|
||||
|
||||
#ifdef __MWERKS__
|
||||
# pragma warn_possunwant off
|
||||
( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
|
||||
#endif
|
||||
|
||||
@@ -9,50 +9,13 @@
|
||||
#ifndef ERRORS_DWA052500_H_
|
||||
# define ERRORS_DWA052500_H_
|
||||
|
||||
# include <boost/python/detail/wrap_python.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
struct error_already_set {};
|
||||
struct argument_error : error_already_set {};
|
||||
|
||||
struct object_functor_base
|
||||
{
|
||||
typedef PyObject* result_type;
|
||||
virtual PyObject* operator()() const = 0;
|
||||
private:
|
||||
static void* operator new(std::size_t); // don't allow dynamic allocation
|
||||
void operator delete(void*);
|
||||
void operator delete(void*, size_t);
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct object_functor : object_functor_base
|
||||
{
|
||||
object_functor(T const& f)
|
||||
: m_f(f)
|
||||
{
|
||||
}
|
||||
|
||||
PyObject* operator()() const
|
||||
{
|
||||
return m_f();
|
||||
}
|
||||
private:
|
||||
T const& m_f;
|
||||
};
|
||||
|
||||
|
||||
// Handles exceptions caught just before returning to Python code.
|
||||
PyObject* handle_exception_impl(object_functor_base const& f);
|
||||
|
||||
template <class T>
|
||||
PyObject* handle_exception(T const& f)
|
||||
{
|
||||
return handle_exception_impl(object_functor<T>(f));
|
||||
}
|
||||
|
||||
void handle_exception(void (*)());
|
||||
void handle_exception();
|
||||
|
||||
template <class T>
|
||||
T* expect_non_null(T* x)
|
||||
|
||||
@@ -42,31 +42,31 @@ BOOST_PYTHON_IMPORT_CONVERSION(py_ptr_conversions);
|
||||
|
||||
template <class T>
|
||||
class reference
|
||||
: public py_ptr_conversions<reference<T>, T>
|
||||
: public py_ptr_conversions<reference<T>, T>
|
||||
{
|
||||
public:
|
||||
typedef T value_type;
|
||||
|
||||
reference(const reference& rhs)
|
||||
: m_p(rhs.m_p)
|
||||
{
|
||||
Py_XINCREF(object());
|
||||
}
|
||||
reference(const reference& rhs)
|
||||
: m_p(rhs.m_p)
|
||||
{
|
||||
Py_XINCREF(object());
|
||||
}
|
||||
|
||||
#if !defined(BOOST_MSVC6_OR_EARLIER)
|
||||
template <class T2>
|
||||
reference(const reference<T2>& rhs)
|
||||
: m_p(rhs.object())
|
||||
{
|
||||
Py_XINCREF(object());
|
||||
}
|
||||
template <class T2>
|
||||
reference(const reference<T2>& rhs)
|
||||
: m_p(rhs.object())
|
||||
{
|
||||
Py_XINCREF(object());
|
||||
}
|
||||
#endif
|
||||
|
||||
reference() : m_p(0) {}
|
||||
reference() : m_p(0) {}
|
||||
|
||||
// These are two ways of spelling the same thing, that we need to increment
|
||||
// the reference count on the pointer when we're initialized.
|
||||
enum increment_count_t { increment_count };
|
||||
enum increment_count_t { increment_count };
|
||||
|
||||
enum allow_null { null_ok };
|
||||
|
||||
@@ -77,7 +77,7 @@ public:
|
||||
template <class T2>
|
||||
reference(T2* x, increment_count_t)
|
||||
: m_p(expect_non_null(x)) { Py_INCREF(object()); }
|
||||
|
||||
|
||||
template <class T2>
|
||||
reference(T2* x, allow_null)
|
||||
: m_p(x) {}
|
||||
@@ -85,49 +85,49 @@ public:
|
||||
template <class T2>
|
||||
reference(T2* x, allow_null, increment_count_t)
|
||||
: m_p(x) { Py_XINCREF(object()); }
|
||||
|
||||
|
||||
template <class T2>
|
||||
reference(T2* x, increment_count_t, allow_null)
|
||||
: m_p(x) { Py_XINCREF(object()); }
|
||||
|
||||
|
||||
#if !defined(BOOST_MSVC6_OR_EARLIER)
|
||||
template <class T2>
|
||||
reference& operator=(const reference<T2>& rhs)
|
||||
{
|
||||
Py_XDECREF(object());
|
||||
m_p = rhs.m_p;
|
||||
Py_XINCREF(object());
|
||||
return *this;
|
||||
}
|
||||
template <class T2>
|
||||
reference& operator=(const reference<T2>& rhs)
|
||||
{
|
||||
Py_XDECREF(object());
|
||||
m_p = rhs.m_p;
|
||||
Py_XINCREF(object());
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
reference& operator=(const reference& rhs)
|
||||
{
|
||||
Py_XINCREF(static_cast<PyObject*>(rhs.m_p));
|
||||
Py_XDECREF(object());
|
||||
m_p = rhs.m_p;
|
||||
return *this;
|
||||
}
|
||||
reference& operator=(const reference& rhs)
|
||||
{
|
||||
Py_XINCREF(static_cast<PyObject*>(rhs.m_p));
|
||||
Py_XDECREF(object());
|
||||
m_p = rhs.m_p;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~reference()
|
||||
{
|
||||
Py_XDECREF(m_p);
|
||||
}
|
||||
|
||||
T& operator*() const { return *m_p; }
|
||||
~reference()
|
||||
{
|
||||
Py_XDECREF(m_p);
|
||||
}
|
||||
|
||||
T& operator*() const { return *m_p; }
|
||||
|
||||
// MSVC doesn't like boost::dereferencable unless T has a default
|
||||
// constructor, so operator-> must be defined by hand :(
|
||||
T* operator->() const { return &**this; }
|
||||
|
||||
T* get() const { return m_p; }
|
||||
T* get() const { return m_p; }
|
||||
|
||||
T* release()
|
||||
{
|
||||
T* p = m_p;
|
||||
m_p = 0;
|
||||
return p;
|
||||
}
|
||||
T* release()
|
||||
{
|
||||
T* p = m_p;
|
||||
m_p = 0;
|
||||
return p;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{ Py_XDECREF(m_p); m_p = 0; }
|
||||
@@ -139,7 +139,7 @@ public:
|
||||
template <class T2>
|
||||
void reset(T2* x, increment_count_t)
|
||||
{ Py_XDECREF(m_p); m_p = expect_non_null(x); Py_INCREF(object()); }
|
||||
|
||||
|
||||
template <class T2>
|
||||
void reset(T2* x, allow_null)
|
||||
{ Py_XDECREF(m_p); m_p = x;}
|
||||
@@ -147,11 +147,11 @@ public:
|
||||
template <class T2>
|
||||
void reset(T2* x, allow_null, increment_count_t)
|
||||
{ Py_XDECREF(m_p); m_p = x; Py_XINCREF(object()); }
|
||||
|
||||
|
||||
template <class T2>
|
||||
void reset(T2* x, increment_count_t, allow_null)
|
||||
{ Py_XDECREF(m_p); m_p = x; Py_XINCREF(object()); }
|
||||
|
||||
|
||||
#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
||||
private:
|
||||
template<typename Y> friend class shared_ptr;
|
||||
@@ -160,7 +160,7 @@ private:
|
||||
inline PyObject* object() const
|
||||
{ return as_object(m_p); }
|
||||
|
||||
T* m_p;
|
||||
T* m_p;
|
||||
};
|
||||
|
||||
typedef reference<PyObject> ref;
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
// 03 Mar 01 added: converters for [plain] char (Ralf W. Grosse-Kunstleve)
|
||||
|
||||
#include <boost/python/conversions.hpp>
|
||||
#include <boost/python/detail/void_adaptor.hpp>
|
||||
#include <typeinfo>
|
||||
#include <exception>
|
||||
#ifndef BOOST_NO_LIMITS
|
||||
@@ -24,11 +23,15 @@
|
||||
namespace boost { namespace python {
|
||||
|
||||
// IMPORTANT: this function may only be called from within a catch block!
|
||||
PyObject* handle_exception_impl(object_functor_base const& f)
|
||||
void handle_exception()
|
||||
{
|
||||
try
|
||||
{
|
||||
return f();
|
||||
try {
|
||||
// re-toss the current exception so we can find out what type it is.
|
||||
// NOTE: a heinous bug in MSVC6 causes exception objects re-thrown in
|
||||
// this way to be double-destroyed. Thus, you must only use objects that
|
||||
// can tolerate double-destruction with that compiler. Metrowerks
|
||||
// Codewarrior doesn't suffer from this problem.
|
||||
throw;
|
||||
}
|
||||
catch(const boost::python::error_already_set&)
|
||||
{
|
||||
@@ -46,13 +49,6 @@ PyObject* handle_exception_impl(object_functor_base const& f)
|
||||
{
|
||||
PyErr_SetString(PyExc_RuntimeError, "unidentifiable C++ exception");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void handle_exception(void (*f)())
|
||||
{
|
||||
handle_exception(
|
||||
boost::python::detail::make_void_adaptor(f));
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
@@ -120,7 +116,7 @@ T integer_from_python(PyObject* p, boost::python::type<T>)
|
||||
PyErr_SetString(PyExc_ValueError, buffer);
|
||||
throw boost::python::argument_error();
|
||||
}
|
||||
#if defined(__MWERKS__) && __MWERKS__ <= 0x2406
|
||||
#if defined(__MWERKS__) && __MWERKS__ <= 0x2400
|
||||
return 0; // Not smart enough to know that the catch clause always rethrows
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -10,9 +10,8 @@
|
||||
// 04 Mar 01 Use PyObject_INIT() instead of trying to hand-initialize (David Abrahams)
|
||||
|
||||
#include <boost/python/detail/extension_class.hpp>
|
||||
#include <boost/utility.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <cstring>
|
||||
#include <boost/utility.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
namespace detail {
|
||||
@@ -485,19 +484,16 @@ void operator_dispatcher_dealloc(PyObject* self)
|
||||
int operator_dispatcher_coerce(PyObject** l, PyObject** r)
|
||||
{
|
||||
Py_INCREF(*l);
|
||||
PyObject* new_r = handle_exception(
|
||||
bind(operator_dispatcher::create,
|
||||
ref(*r, ref::increment_count),
|
||||
ref()));
|
||||
if (new_r)
|
||||
try
|
||||
{
|
||||
*r = new_r;
|
||||
return 0;
|
||||
*r = operator_dispatcher::create(ref(*r, ref::increment_count), ref());
|
||||
}
|
||||
else
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -238,9 +238,6 @@ class python_extension_class_converters
|
||||
}
|
||||
boost::python::detail::report_missing_instance_data(self, boost::python::detail::class_registry<T>::class_object(), typeid(T));
|
||||
throw boost::python::argument_error();
|
||||
#if defined(__MWERKS__) && __MWERKS__ <= 0x2406
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Convert to T*
|
||||
@@ -269,10 +266,6 @@ class python_extension_class_converters
|
||||
}
|
||||
boost::python::detail::report_missing_ptr_data(self, boost::python::detail::class_registry<T>::class_object(), typeid(T));
|
||||
throw boost::python::argument_error();
|
||||
#if defined(__MWERKS__) && __MWERKS__ <= 0x2406
|
||||
PtrType x;
|
||||
return x;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Extract from obj a reference to the PtrType object which is holding a
|
||||
|
||||
264
src/types.cpp
264
src/types.cpp
@@ -8,18 +8,15 @@
|
||||
|
||||
#include <boost/python/detail/types.hpp>
|
||||
#include <boost/python/reference.hpp> // for handle_exception()
|
||||
#include <boost/python/conversions.hpp>
|
||||
#include <boost/python/module_builder.hpp>
|
||||
#include <boost/python/detail/none.hpp>
|
||||
#include <boost/python/detail/void_adaptor.hpp>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <boost/smart_ptr.hpp>
|
||||
#include <boost/python/objects.hpp>
|
||||
#include <boost/type_traits/alignment_traits.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/type_traits.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
@@ -27,168 +24,138 @@ namespace {
|
||||
|
||||
using detail::type_object_base;
|
||||
|
||||
// Define a family of forwarding functions that can be called from a
|
||||
// Define a family of forwarding functions that can be calle from a
|
||||
// PyTypeObject's slots. These functions dispatch through a (virtual) member
|
||||
// function pointer in the type_object_base, and handle exceptions in a
|
||||
// uniform way, preventing us from having to rewrite the dispatching code over
|
||||
// and over.
|
||||
|
||||
|
||||
// Given a function object f with signature
|
||||
//
|
||||
// PyObject* f(PyTypeObject*,PyObject*)
|
||||
//
|
||||
// calls f inside of handle_exception, and returns the result. If an exception
|
||||
// is thrown by f, returns 0.
|
||||
template <class F>
|
||||
PyObject* obj_call(PyObject* obj, F const& f)
|
||||
{
|
||||
return handle_exception(
|
||||
boost::bind<PyObject*>(f, static_cast<type_object_base*>(obj->ob_type), obj));
|
||||
}
|
||||
|
||||
|
||||
// int_converter<T>/value_holder<T>
|
||||
//
|
||||
// A simple function object which converts its argument to a PyObject*. We
|
||||
// need this because handle_exception needs to return a PyObject*, even if the
|
||||
// function being called is supposed to return int. It has two parts...
|
||||
|
||||
// holds the value actually returned by the underlying function
|
||||
template <class T>
|
||||
struct value_holder : PyObject
|
||||
{
|
||||
value_holder() : is_set(false), value(-1) {}
|
||||
|
||||
// Tricky constructor allows us to grab the result even if rhs == 0.
|
||||
explicit value_holder(value_holder const* rhs)
|
||||
: is_set(rhs ? rhs->is_set : false), value(rhs ? rhs->value : -1) {}
|
||||
|
||||
// true if the function object was ever called (false if an exception occurred)
|
||||
bool is_set;
|
||||
|
||||
// The returned value
|
||||
T value;
|
||||
};
|
||||
|
||||
// The function object
|
||||
template <class R>
|
||||
struct int_converter
|
||||
{
|
||||
typedef PyObject* result_type;
|
||||
|
||||
PyObject* operator()(R const& x)
|
||||
{
|
||||
m_holder.is_set = true;
|
||||
m_holder.value = x;
|
||||
return &m_holder; // returns
|
||||
}
|
||||
|
||||
value_holder<R> m_holder;
|
||||
};
|
||||
|
||||
// Call the given int-returning function object inside of handle_exception,
|
||||
// returning a value_holder. F is a function object with "signature"
|
||||
//
|
||||
// R F(PyTypeObject*, PyObject*)
|
||||
//
|
||||
// where R is an integer type.
|
||||
template <class R, class F>
|
||||
value_holder<R> int_call_holder(PyObject* obj, F f)
|
||||
{
|
||||
return value_holder<R>(
|
||||
|
||||
// The int_converter object containing the value_holder is valid
|
||||
// through the life of the full-expression, so we can construct from
|
||||
// the pointer
|
||||
static_cast<value_holder<R>*>(
|
||||
handle_exception(
|
||||
|
||||
boost::bind<PyObject*>(
|
||||
// Add an int_converter back-end to f
|
||||
int_converter<R>()
|
||||
// Bind the object's type and the object itself into f
|
||||
, boost::bind<R>(f, static_cast<type_object_base*>(obj->ob_type), obj)
|
||||
)
|
||||
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Just like int_call_holder (above), but returns the integer directly. If F
|
||||
// throws an exception, returns -1
|
||||
template <class R, class F>
|
||||
R int_call(PyObject* obj, F f)
|
||||
{
|
||||
value_holder<R> const v(int_call_holder<R>(obj, f));
|
||||
return v.value;
|
||||
}
|
||||
|
||||
// Implemented in terms of obj_call, above
|
||||
PyObject* call(PyObject* obj, PyObject* (type_object_base::*f)(PyObject*) const)
|
||||
{
|
||||
return obj_call(obj, bind(f, _1, _2));
|
||||
try
|
||||
{
|
||||
return (static_cast<type_object_base*>(obj->ob_type)->*f)(obj);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Naming this differently allows us to use it for functions returning long on
|
||||
// compilers without partial ordering
|
||||
template <class R>
|
||||
R int_call(PyObject* obj, R (type_object_base::*f)(PyObject*) const)
|
||||
{
|
||||
try
|
||||
{
|
||||
return (static_cast<type_object_base*>(obj->ob_type)->*f)(obj);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Implemented in terms of int_call, above
|
||||
int call(PyObject* obj, int (type_object_base::*f)(PyObject*) const)
|
||||
{
|
||||
return int_call<int>(obj, bind(f, _1, _2));
|
||||
return int_call(obj, f);
|
||||
}
|
||||
|
||||
template <class A1>
|
||||
PyObject* call(PyObject* obj, PyObject* (type_object_base::*f)(PyObject*, A1) const, A1 a1)
|
||||
{
|
||||
return obj_call(obj, bind(f, _1, _2, a1));
|
||||
try
|
||||
{
|
||||
return (static_cast<type_object_base*>(obj->ob_type)->*f)(obj, a1);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template <class A1>
|
||||
int call(PyObject* obj, int (type_object_base::*f)(PyObject*, A1) const, A1 a1)
|
||||
{
|
||||
return int_call<int>(obj, bind(f, _1, _2, a1));
|
||||
try
|
||||
{
|
||||
return (static_cast<type_object_base*>(obj->ob_type)->*f)(obj, a1);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
template <class A1, class A2>
|
||||
PyObject* call(PyObject* obj, PyObject* (type_object_base::*f)(PyObject*, A1, A2) const, A1 a1, A2 a2)
|
||||
{
|
||||
return obj_call(obj, bind(f, _1, _2, a1, a2));
|
||||
try
|
||||
{
|
||||
return (static_cast<type_object_base*>(obj->ob_type)->*f)(obj, a1, a2);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template <class A1, class A2>
|
||||
int call(PyObject* obj, int (type_object_base::*f)(PyObject*, A1, A2) const, A1 a1, A2 a2)
|
||||
{
|
||||
return int_call<int>(obj, bind(f, _1, _2, a1, a2));
|
||||
try
|
||||
{
|
||||
return (static_cast<type_object_base*>(obj->ob_type)->*f)(obj, a1, a2);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
template <class A1, class A2, class A3>
|
||||
int call(PyObject* obj, int (type_object_base::*f)(PyObject*, A1, A2, A3) const, A1 a1, A2 a2, A3 a3)
|
||||
{
|
||||
return int_call<int>(obj, bind(f, _1, _2, a1, a2, a3));
|
||||
try
|
||||
{
|
||||
return (static_cast<type_object_base*>(obj->ob_type)->*f)(obj, a1, a2, a3);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int call_length_function(PyObject* obj, int (type_object_base::*f)(PyObject*) const)
|
||||
{
|
||||
value_holder<int> const r(int_call_holder<int>(obj, bind(f, _1, _2)));
|
||||
|
||||
if (!r.is_set)
|
||||
try
|
||||
{
|
||||
const int outcome =
|
||||
(static_cast<type_object_base*>(obj->ob_type)->*f)(obj);
|
||||
|
||||
if (outcome < 0)
|
||||
{
|
||||
PyErr_SetString(PyExc_ValueError, "__len__() should return >= 0");
|
||||
return -1;
|
||||
}
|
||||
return outcome;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return -1;
|
||||
}
|
||||
|
||||
const int outcome = r.value;
|
||||
if (outcome >= 0)
|
||||
return outcome;
|
||||
|
||||
PyErr_SetString(PyExc_ValueError, "__len__() should return >= 0");
|
||||
return -1;
|
||||
}
|
||||
} // anonymous namespace
|
||||
|
||||
namespace detail {
|
||||
// needed by void_adaptor (see void_adaptor.hpp)
|
||||
PyObject arbitrary_object;
|
||||
}
|
||||
} // anonymous namespace
|
||||
|
||||
extern "C" {
|
||||
|
||||
@@ -235,7 +202,7 @@ static PyObject* do_instance_str(PyObject* obj)
|
||||
|
||||
static long do_instance_hash(PyObject* obj)
|
||||
{
|
||||
return int_call<long>(obj, bind(&type_object_base::instance_hash, _1, _2));
|
||||
return int_call(obj, &type_object_base::instance_hash);
|
||||
}
|
||||
|
||||
static PyObject* do_instance_call(PyObject* obj, PyObject* args, PyObject* keywords)
|
||||
@@ -245,16 +212,15 @@ static PyObject* do_instance_call(PyObject* obj, PyObject* args, PyObject* keywo
|
||||
|
||||
static void do_instance_dealloc(PyObject* obj)
|
||||
{
|
||||
PyObject* success = handle_exception(
|
||||
// translate the void return value of instance_dealloc into a PyObject*
|
||||
// that can indicate no error.
|
||||
detail::make_void_adaptor(
|
||||
bind(&type_object_base::instance_dealloc
|
||||
, static_cast<type_object_base*>(obj->ob_type)
|
||||
, obj)));
|
||||
if (!success)
|
||||
try
|
||||
{
|
||||
static_cast<type_object_base*>(obj->ob_type)
|
||||
->instance_dealloc(obj);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
assert(!"exception during destruction!");
|
||||
handle_exception();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,22 +253,28 @@ static PyObject* do_instance_mp_subscript(PyObject* obj, PyObject* index)
|
||||
|
||||
static PyObject* do_instance_sq_item(PyObject* obj, int index)
|
||||
{
|
||||
// This is an extension to standard class behavior. If sequence_length
|
||||
// is implemented and n >= sequence_length(), raise an IndexError. That
|
||||
// keeps users from having to worry about raising it themselves
|
||||
const PyTypeObject* const type = obj->ob_type;
|
||||
if (type->tp_as_sequence != 0 && type->tp_as_sequence->sq_length != 0
|
||||
&& index >= type->tp_as_sequence->sq_length(obj))
|
||||
try
|
||||
{
|
||||
PyErr_SetString(PyExc_IndexError, type->tp_name);
|
||||
const PyTypeObject* const type = obj->ob_type;
|
||||
|
||||
// This is an extension to standard class behavior. If sequence_length
|
||||
// is implemented and n >= sequence_length(), raise an IndexError. That
|
||||
// keeps users from having to worry about raising it themselves
|
||||
if (type->tp_as_sequence != 0 && type->tp_as_sequence->sq_length != 0
|
||||
&& index >= type->tp_as_sequence->sq_length(obj))
|
||||
{
|
||||
PyErr_SetString(PyExc_IndexError, type->tp_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<type_object_base*>(obj->ob_type)
|
||||
->instance_sequence_item(obj, index);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
handle_exception();
|
||||
return 0;
|
||||
}
|
||||
|
||||
return handle_exception(
|
||||
bind(&type_object_base::instance_sequence_item
|
||||
, static_cast<type_object_base*>(obj->ob_type)
|
||||
, obj
|
||||
, index));
|
||||
}
|
||||
|
||||
static int do_instance_mp_ass_subscript(PyObject* obj, PyObject* index, PyObject* value)
|
||||
@@ -425,10 +397,7 @@ static PyObject* do_instance_nb_or(PyObject* obj, PyObject* other)
|
||||
|
||||
static int do_instance_nb_coerce(PyObject**obj, PyObject**other)
|
||||
{
|
||||
// no call() overload for this oddball function, so we'll do it manually
|
||||
return int_call<int>(
|
||||
*obj, bind(
|
||||
&type_object_base::instance_number_coerce, _1, _2, obj, other));
|
||||
return call(*obj, &type_object_base::instance_number_coerce, obj, other);
|
||||
}
|
||||
static PyObject* do_instance_nb_int(PyObject* obj)
|
||||
{
|
||||
@@ -1202,3 +1171,4 @@ int main()
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ int IntPairPythonClass::getattr(const IntPair& p, const std::string& s)
|
||||
PyErr_SetString(PyExc_AttributeError, s.c_str());
|
||||
throw boost::python::error_already_set();
|
||||
}
|
||||
#if defined(__MWERKS__) && __MWERKS__ <= 0x2406
|
||||
#if defined(__MWERKS__) && __MWERKS__ <= 0x2400
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
@@ -1128,7 +1128,7 @@ PyObject* raw(const boost::python::tuple& args, const boost::python::dictionary&
|
||||
return BOOST_PYTHON_CONVERSION::to_python(first->i_ + second + third + fourth);
|
||||
}
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(boost_python_test)
|
||||
void init_module()
|
||||
{
|
||||
boost::python::module_builder boost_python_test("boost_python_test");
|
||||
init_module(boost_python_test);
|
||||
@@ -1137,6 +1137,16 @@ BOOST_PYTHON_MODULE_INIT(boost_python_test)
|
||||
boost_python_test.add(new boost::python::meta_class<boost::python::instance>);
|
||||
}
|
||||
|
||||
BOOST_PYTHON_MODULE_INIT(boost_python_test)
|
||||
{
|
||||
try {
|
||||
bpl_test::init_module();
|
||||
}
|
||||
catch(...) {
|
||||
boost::python::handle_exception();
|
||||
} // Need a way to report other errors here
|
||||
}
|
||||
|
||||
CompareIntPairPythonClass::CompareIntPairPythonClass(boost::python::module_builder& m)
|
||||
: boost::python::class_builder<CompareIntPair>(m, "CompareIntPair")
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user