Skip to content

Commit 98b31ed

Browse files
committed
use BOOST_PYTHON_MODULE_INIT, update getting_started2 for better documentation
[SVN r9472]
1 parent 00cea4f commit 98b31ed

6 files changed

Lines changed: 38 additions & 37 deletions

File tree

example/getting_started1.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ namespace python = boost::python;
1212

1313
// Python requires an exported function called init<module-name> in every
1414
// extension module. This is where we build the module contents.
15-
extern "C"
16-
DL_EXPORT(void)
17-
initgetting_started1()
15+
BOOST_PYTHON_MODULE_INIT(getting_started1)
1816
{
1917
try
2018
{

example/getting_started2.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
#include <iostream>
22
#include <string>
3-
#include <boost/python/class_builder.hpp>
4-
namespace python = boost::python;
53

64
namespace { // Avoid cluttering the global namespace.
75

86
// A friendly class.
9-
class world
7+
class hello
108
{
9+
public:
10+
hello(const std::string& country) { this->country = country; }
11+
std::string greet() const { return "Hello from " + country; }
1112
private:
1213
std::string country;
13-
public:
14-
world(const std::string& country) { this->country = country; }
15-
std::string greet() const { return "Hello from " + country + "!"; }
1614
};
1715

18-
// A function taking a world object as an argument.
19-
std::string invite(const world& w) {
20-
return w.greet() + " Please come soon!";
16+
// A function taking a hello object as an argument.
17+
std::string invite(const hello& w) {
18+
return w.greet() + "! Please come soon!";
2119
}
2220
}
2321

24-
extern "C"
25-
DL_EXPORT(void)
26-
initgetting_started2()
22+
#include <boost/python/class_builder.hpp>
23+
namespace python = boost::python;
24+
25+
BOOST_PYTHON_MODULE_INIT(getting_started2)
2726
{
2827
try
2928
{
3029
// Create an object representing this extension module.
3130
python::module_builder this_module("getting_started2");
3231

3332
// Create the Python type object for our extension class.
34-
python::class_builder<world> world_class(this_module, "world");
33+
python::class_builder<hello> hello_class(this_module, "hello");
3534

3635
// Add the __init__ function.
37-
world_class.def(python::constructor<std::string>());
36+
hello_class.def(python::constructor<std::string>());
3837
// Add a regular member function.
39-
world_class.def(&world::greet, "greet");
38+
hello_class.def(&hello::greet, "greet");
4039

4140
// Add invite() as a regular function to the module.
4241
this_module.def(invite, "invite");
4342

44-
// Even better, invite() can also be made a member of world_class!!!
45-
world_class.def(invite, "invite");
43+
// Even better, invite() can also be made a member of hello_class!!!
44+
hello_class.def(invite, "invite");
4645
}
4746
catch(...)
4847
{

example/getting_started3.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ namespace { // Avoid cluttering the global namespace.
9191
}
9292
}
9393

94-
extern "C"
95-
DL_EXPORT(void)
96-
initgetting_started3()
94+
BOOST_PYTHON_MODULE_INIT(getting_started3)
9795
{
9896
try
9997
{

example/getting_started4.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ namespace { // Avoid cluttering the global namespace.
7474
}
7575
}
7676

77-
extern "C"
78-
DL_EXPORT(void)
79-
initgetting_started4()
77+
BOOST_PYTHON_MODULE_INIT(getting_started4)
8078
{
8179
try
8280
{

example/getting_started5.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
103103

104104
BOOST_PYTHON_END_CONVERSION_NAMESPACE
105105

106-
extern "C"
107-
DL_EXPORT(void)
108-
initgetting_started5()
106+
BOOST_PYTHON_MODULE_INIT(getting_started5)
109107
{
110108
try
111109
{

example/test_getting_started2.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
r'''>>> import getting_started2
2-
>>> w = getting_started2.world('California')
3-
>>> print w.greet()
4-
Hello from California!
5-
>>> print getting_started2.invite(w)
6-
Hello from California! Please come soon!
7-
>>> print w.invite()
8-
Hello from California! Please come soon!
1+
r'''>>> from getting_started2 import *
2+
>>> hi = hello('California')
3+
>>> hi.greet()
4+
'Hello from California'
5+
>>> invite(hi)
6+
'Hello from California! Please come soon!'
7+
>>> hi.invite()
8+
'Hello from California! Please come soon!'
9+
10+
>>> class wordy(hello):
11+
... def greet(self):
12+
... return hello.greet(self) + ', where the weather is fine'
13+
...
14+
>>> hi2 = wordy('Florida')
15+
>>> hi2.greet()
16+
'Hello from Florida, where the weather is fine'
17+
>>> invite(hi2)
18+
'Hello from Florida! Please come soon!'
919
'''
1020

1121
def run(args = None):

0 commit comments

Comments
 (0)