diff --git a/example/getting_started1.cpp b/example/getting_started1.cpp index 9679a2a4..7a8e9087 100644 --- a/example/getting_started1.cpp +++ b/example/getting_started1.cpp @@ -12,9 +12,7 @@ namespace python = boost::python; // Python requires an exported function called init in every // extension module. This is where we build the module contents. -extern "C" -DL_EXPORT(void) -initgetting_started1() +BOOST_PYTHON_MODULE_INIT(getting_started1) { try { diff --git a/example/getting_started2.cpp b/example/getting_started2.cpp index 82b4a6df..72b04105 100644 --- a/example/getting_started2.cpp +++ b/example/getting_started2.cpp @@ -1,29 +1,28 @@ #include #include -#include -namespace python = boost::python; namespace { // Avoid cluttering the global namespace. // A friendly class. - class world + class hello { + public: + hello(const std::string& country) { this->country = country; } + std::string greet() const { return "Hello from " + country; } 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!"; + // A function taking a hello object as an argument. + std::string invite(const hello& w) { + return w.greet() + "! Please come soon!"; } } -extern "C" -DL_EXPORT(void) -initgetting_started2() +#include +namespace python = boost::python; + +BOOST_PYTHON_MODULE_INIT(getting_started2) { try { @@ -31,18 +30,18 @@ initgetting_started2() python::module_builder this_module("getting_started2"); // Create the Python type object for our extension class. - python::class_builder world_class(this_module, "world"); + python::class_builder hello_class(this_module, "hello"); // Add the __init__ function. - world_class.def(python::constructor()); + hello_class.def(python::constructor()); // Add a regular member function. - world_class.def(&world::greet, "greet"); + hello_class.def(&hello::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"); + // Even better, invite() can also be made a member of hello_class!!! + hello_class.def(invite, "invite"); } catch(...) { diff --git a/example/getting_started3.cpp b/example/getting_started3.cpp index 7e827249..799f5cac 100644 --- a/example/getting_started3.cpp +++ b/example/getting_started3.cpp @@ -91,9 +91,7 @@ namespace { // Avoid cluttering the global namespace. } } -extern "C" -DL_EXPORT(void) -initgetting_started3() +BOOST_PYTHON_MODULE_INIT(getting_started3) { try { diff --git a/example/getting_started4.cpp b/example/getting_started4.cpp index 0c7bd7ee..199ef7a9 100644 --- a/example/getting_started4.cpp +++ b/example/getting_started4.cpp @@ -74,9 +74,7 @@ namespace { // Avoid cluttering the global namespace. } } -extern "C" -DL_EXPORT(void) -initgetting_started4() +BOOST_PYTHON_MODULE_INIT(getting_started4) { try { diff --git a/example/getting_started5.cpp b/example/getting_started5.cpp index c9f1ce36..be033224 100644 --- a/example/getting_started5.cpp +++ b/example/getting_started5.cpp @@ -103,9 +103,7 @@ BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE BOOST_PYTHON_END_CONVERSION_NAMESPACE -extern "C" -DL_EXPORT(void) -initgetting_started5() +BOOST_PYTHON_MODULE_INIT(getting_started5) { try { diff --git a/example/test_getting_started2.py b/example/test_getting_started2.py index 09215816..49cf765d 100644 --- a/example/test_getting_started2.py +++ b/example/test_getting_started2.py @@ -1,11 +1,21 @@ -r'''>>> import getting_started2 - >>> w = getting_started2.world('California') - >>> print w.greet() - Hello from California! - >>> print getting_started2.invite(w) - Hello from California! Please come soon! - >>> print w.invite() - Hello from California! Please come soon! +r'''>>> from getting_started2 import * + >>> hi = hello('California') + >>> hi.greet() + 'Hello from California' + >>> invite(hi) + 'Hello from California! Please come soon!' + >>> hi.invite() + 'Hello from California! Please come soon!' + + >>> class wordy(hello): + ... def greet(self): + ... return hello.greet(self) + ', where the weather is fine' + ... + >>> hi2 = wordy('Florida') + >>> hi2.greet() + 'Hello from Florida, where the weather is fine' + >>> invite(hi2) + 'Hello from Florida! Please come soon!' ''' def run(args = None):