diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2013-06-25 22:59:01 +0000 |
---|---|---|
committer | <> | 2013-09-27 11:49:28 +0000 |
commit | 8c4528713d907ee2cfd3bfcbbad272c749867f84 (patch) | |
tree | c09e2ce80f47b90c85cc720f5139089ad9c8cfff /libs/python/example | |
download | boost-tarball-baserock/morph.tar.gz |
Imported from /home/lorry/working-area/delta_boost-tarball/boost_1_54_0.tar.bz2.boost_1_54_0baserock/morph
Diffstat (limited to 'libs/python/example')
-rw-r--r-- | libs/python/example/Jamroot | 40 | ||||
-rw-r--r-- | libs/python/example/README | 16 | ||||
-rw-r--r-- | libs/python/example/boost-build.jam | 7 | ||||
-rw-r--r-- | libs/python/example/getting_started1.cpp | 25 | ||||
-rw-r--r-- | libs/python/example/getting_started2.cpp | 41 | ||||
-rw-r--r-- | libs/python/example/project.zip | bin | 0 -> 1469 bytes | |||
-rw-r--r-- | libs/python/example/quickstart/Jamroot | 43 | ||||
-rw-r--r-- | libs/python/example/quickstart/boost-build.jam | 7 | ||||
-rw-r--r-- | libs/python/example/quickstart/embedding.cpp | 154 | ||||
-rw-r--r-- | libs/python/example/quickstart/extending.cpp | 41 | ||||
-rw-r--r-- | libs/python/example/quickstart/script.py | 6 | ||||
-rw-r--r-- | libs/python/example/quickstart/test_extending.py | 36 | ||||
-rw-r--r-- | libs/python/example/std_pair.cpp | 49 | ||||
-rw-r--r-- | libs/python/example/test_getting_started1.py | 21 | ||||
-rw-r--r-- | libs/python/example/test_getting_started2.py | 34 | ||||
-rw-r--r-- | libs/python/example/test_std_pair.py | 6 | ||||
-rw-r--r-- | libs/python/example/tutorial/Jamroot | 48 | ||||
-rw-r--r-- | libs/python/example/tutorial/hello.cpp | 20 | ||||
-rwxr-xr-x | libs/python/example/tutorial/hello.py | 7 |
19 files changed, 601 insertions, 0 deletions
diff --git a/libs/python/example/Jamroot b/libs/python/example/Jamroot new file mode 100644 index 000000000..0d5ad9dcb --- /dev/null +++ b/libs/python/example/Jamroot @@ -0,0 +1,40 @@ +# 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) + +# Specify the path to the Boost project. If you move this project, +# adjust this path to refer to the Boost root directory. +use-project boost + : ../../.. ; + +# Set up the project-wide requirements that everything uses the +# boost_python library from the project whose global ID is +# /boost/python. +project + : requirements <library>/boost/python//boost_python ; + +# Declare the three extension modules. You can specify multiple +# source files after the colon separated by spaces. +python-extension getting_started1 : getting_started1.cpp ; +python-extension getting_started2 : getting_started2.cpp ; +python-extension std_pair_ext : std_pair.cpp ; + +# A little "rule" (function) to clean up the syntax of declaring tests +# of these extension modules. +local rule run-test ( test-name : sources + ) +{ + import testing ; + testing.make-test run-pyd : $(sources) : : $(test-name) ; +} + +# Declare test targets +run-test test1 : getting_started1 test_getting_started1.py ; +run-test test2 : getting_started2 test_getting_started2.py ; +run-test test3 : std_pair_ext test_std_pair.py ; + +# A target that runs all the tests +alias test : test1 test2 test3 ; + +# Only run tests when explicitly requested +explicit test test1 test2 test3 ; + diff --git a/libs/python/example/README b/libs/python/example/README new file mode 100644 index 000000000..29a94f67d --- /dev/null +++ b/libs/python/example/README @@ -0,0 +1,16 @@ +.. 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 get started with the Boost Python Library, use the examples +getting_started1.cpp and getting_started2.cpp. Invoking + + bjam --toolset=your-toolset test + +in this directory will build and run the examples. See +http://www.boost.org/more/getting_started.html for details about the +--toolset= option. + +If you move this example from its place in the Boost development tree +you'll need to edit the two lines indicated in Jamroot and +boost-build.jam. diff --git a/libs/python/example/boost-build.jam b/libs/python/example/boost-build.jam new file mode 100644 index 000000000..b7220e2c2 --- /dev/null +++ b/libs/python/example/boost-build.jam @@ -0,0 +1,7 @@ +# 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) + +# Edit this path to point at the tools/build/v2 subdirectory of your +# Boost installation. Absolute paths work, too. +boost-build ../../../tools/build/v2 ; diff --git a/libs/python/example/getting_started1.cpp b/libs/python/example/getting_started1.cpp new file mode 100644 index 000000000..09d681039 --- /dev/null +++ b/libs/python/example/getting_started1.cpp @@ -0,0 +1,25 @@ +// 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) + +#include <boost/python/module.hpp> +#include <boost/python/def.hpp> +#include <string> + +namespace { // Avoid cluttering the global namespace. + + // A couple of simple C++ functions that we want to expose to Python. + std::string greet() { return "hello, world"; } + int square(int number) { return number * number; } +} + +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(getting_started1) +{ + // Add regular functions to the module. + python::def("greet", greet); + python::def("square", square); +} diff --git a/libs/python/example/getting_started2.cpp b/libs/python/example/getting_started2.cpp new file mode 100644 index 000000000..ee8af32e5 --- /dev/null +++ b/libs/python/example/getting_started2.cpp @@ -0,0 +1,41 @@ +// 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) + +#include <boost/python/class.hpp> +#include <boost/python/module.hpp> +#include <boost/python/def.hpp> +#include <iostream> +#include <string> + +namespace { // Avoid cluttering the global namespace. + + // A friendly class. + class hello + { + public: + hello(const std::string& country) { this->country = country; } + std::string greet() const { return "Hello from " + country; } + private: + std::string country; + }; + + // A function taking a hello object as an argument. + std::string invite(const hello& w) { + return w.greet() + "! Please come soon!"; + } +} + +BOOST_PYTHON_MODULE(getting_started2) +{ + using namespace boost::python; + class_<hello>("hello", init<std::string>()) + // Add a regular member function. + .def("greet", &hello::greet) + // Add invite() as a member of hello! + .def("invite", invite) + ; + + // Also add invite() as a regular function to the module. + def("invite", invite); +} diff --git a/libs/python/example/project.zip b/libs/python/example/project.zip Binary files differnew file mode 100644 index 000000000..d863defdb --- /dev/null +++ b/libs/python/example/project.zip diff --git a/libs/python/example/quickstart/Jamroot b/libs/python/example/quickstart/Jamroot new file mode 100644 index 000000000..569dae131 --- /dev/null +++ b/libs/python/example/quickstart/Jamroot @@ -0,0 +1,43 @@ +# 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) + +# Specify the path to the Boost project. If you move this project, +# adjust the path to refer to the Boost root directory. +use-project boost + : ../../../.. ; + +# Set up the project-wide requirements that everything uses the +# boost_python library defined in the project whose global ID is +# /boost/python. +project boost-python-quickstart + : requirements <library>/boost/python//boost_python + ; + +# Make the definition of the python-extension rule available +import python ; + +# Declare a Python extension called hello. +python-extension extending : extending.cpp ; + +# Declare an executable called embedding that embeds Python +exe embedding : embedding.cpp /python//python ; + +import testing ; + +# Declare a test of the extension module +testing.make-test run-pyd : extending test_extending.py : : test_ext ; + +# Declare a test of the embedding application +testing.run embedding + : # any ordinary arguments + : script.py # any arguments that should be treated as relative paths + : # requirements + : test_embed ; # name of test + +# Create a "test" target that runs all the tests +alias test : test_ext test_embed ; + +# make sure the tests don't run by default +explicit test_ext test_embed test ; + diff --git a/libs/python/example/quickstart/boost-build.jam b/libs/python/example/quickstart/boost-build.jam new file mode 100644 index 000000000..a440ea9fe --- /dev/null +++ b/libs/python/example/quickstart/boost-build.jam @@ -0,0 +1,7 @@ +# 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) + +# Edit this path to point at the tools/build/v2 subdirectory of your +# Boost installation. Absolute paths work, too. +boost-build ../../../../tools/build/v2 ; diff --git a/libs/python/example/quickstart/embedding.cpp b/libs/python/example/quickstart/embedding.cpp new file mode 100644 index 000000000..65bcd16a0 --- /dev/null +++ b/libs/python/example/quickstart/embedding.cpp @@ -0,0 +1,154 @@ +// 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() +{ + std::cout << "registering extension module embedded_hello..." << std::endl; + + // 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"); + + std::cout << "defining Python class derived from Base..." << std::endl; + + // 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++!"); + + std::cout << "testing derived class from C++..." << std::endl; + + // 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!"); + + std::cout << "success!" << std::endl; +} + +void exec_file_test(std::string const &script) +{ + std::cout << "running file " << script << "..." << std::endl; + + // 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); + + std::cout << "success!" << std::endl; +} + +void exec_test_error() +{ + std::cout << "intentionally causing a python exception..." << std::endl; + + // Execute a statement that raises a python exception. + python::dict global; + python::object result = python::exec("print unknown \n", global, global); + + std::cout << "Oops! This statement should be skipped due to an exception" << std::endl; +} + +int main(int argc, char **argv) +{ + BOOST_TEST(argc == 2); + std::string script = argv[1]; + // Initialize the interpreter + Py_Initialize(); + + bool error_expected = false; + + if ( + python::handle_exception(exec_test) + || python::handle_exception(boost::bind(exec_file_test, script)) + || ( + (error_expected = true) + && python::handle_exception(exec_test_error) + ) + + ) + { + if (PyErr_Occurred()) + { + if (!error_expected) + BOOST_ERROR("Python Error detected"); + PyErr_Print(); + } + else + { + BOOST_ERROR("A C++ exception was thrown for which " + "there was no exception translator registered."); + } + } + + // Boost.Python doesn't support Py_Finalize yet, so don't call it! + return boost::report_errors(); +} diff --git a/libs/python/example/quickstart/extending.cpp b/libs/python/example/quickstart/extending.cpp new file mode 100644 index 000000000..a539d3b4b --- /dev/null +++ b/libs/python/example/quickstart/extending.cpp @@ -0,0 +1,41 @@ +// 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) + +#include <boost/python/class.hpp> +#include <boost/python/module.hpp> +#include <boost/python/def.hpp> +#include <iostream> +#include <string> + +namespace { // Avoid cluttering the global namespace. + + // A friendly class. + class hello + { + public: + hello(const std::string& country) { this->country = country; } + std::string greet() const { return "Hello from " + country; } + private: + std::string country; + }; + + // A function taking a hello object as an argument. + std::string invite(const hello& w) { + return w.greet() + "! Please come soon!"; + } +} + +BOOST_PYTHON_MODULE(extending) +{ + using namespace boost::python; + class_<hello>("hello", init<std::string>()) + // Add a regular member function. + .def("greet", &hello::greet) + // Add invite() as a member of hello! + .def("invite", invite) + ; + + // Also add invite() as a regular function to the module. + def("invite", invite); +} diff --git a/libs/python/example/quickstart/script.py b/libs/python/example/quickstart/script.py new file mode 100644 index 000000000..5a8faf79f --- /dev/null +++ b/libs/python/example/quickstart/script.py @@ -0,0 +1,6 @@ +# 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 diff --git a/libs/python/example/quickstart/test_extending.py b/libs/python/example/quickstart/test_extending.py new file mode 100644 index 000000000..14616f7c0 --- /dev/null +++ b/libs/python/example/quickstart/test_extending.py @@ -0,0 +1,36 @@ +# Copyright Ralf W. Grosse-Kunstleve 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) + +# Using the doctest module here to ensure that the results are as expected. +r'''>>> from extending 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): + if args is not None: + import sys + sys.argv = args + import doctest, test_extending + return doctest.testmod(test_extending, verbose=True) + +if __name__ == '__main__': + import sys + sys.exit(run()[0]) + diff --git a/libs/python/example/std_pair.cpp b/libs/python/example/std_pair.cpp new file mode 100644 index 000000000..edf98dd65 --- /dev/null +++ b/libs/python/example/std_pair.cpp @@ -0,0 +1,49 @@ +// 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) + +#include <boost/python/module.hpp> +#include <boost/python/def.hpp> +#include <boost/python/tuple.hpp> +#include <boost/python/to_python_converter.hpp> + +namespace { // Avoid cluttering the global namespace. + + // Converts a std::pair instance to a Python tuple. + template <typename T1, typename T2> + struct std_pair_to_tuple + { + static PyObject* convert(std::pair<T1, T2> const& p) + { + return boost::python::incref( + boost::python::make_tuple(p.first, p.second).ptr()); + } + static PyTypeObject const *get_pytype () {return &PyTuple_Type; } + }; + + // Helper for convenience. + template <typename T1, typename T2> + struct std_pair_to_python_converter + { + std_pair_to_python_converter() + { + boost::python::to_python_converter< + std::pair<T1, T2>, + std_pair_to_tuple<T1, T2>, + true //std_pair_to_tuple has get_pytype + >(); + } + }; + + // Example function returning a std::pair. + std::pair<int, int> + foo() { return std::pair<int, int>(3, 5); } + +} // namespace anonymous + +BOOST_PYTHON_MODULE(std_pair_ext) +{ + using namespace boost::python; + std_pair_to_python_converter<int, int>(); + def("foo", foo); +} diff --git a/libs/python/example/test_getting_started1.py b/libs/python/example/test_getting_started1.py new file mode 100644 index 000000000..32dc1f6eb --- /dev/null +++ b/libs/python/example/test_getting_started1.py @@ -0,0 +1,21 @@ +# Copyright Ralf W. Grosse-Kunstleve 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) +r'''>>> import getting_started1 + >>> print getting_started1.greet() + hello, world + >>> number = 11 + >>> print number, '*', number, '=', getting_started1.square(number) + 11 * 11 = 121 +''' + +def run(args = None): + if args is not None: + import sys + sys.argv = args + import doctest, test_getting_started1 + return doctest.testmod(test_getting_started1) + +if __name__ == '__main__': + import sys + sys.exit(run()[0]) diff --git a/libs/python/example/test_getting_started2.py b/libs/python/example/test_getting_started2.py new file mode 100644 index 000000000..ae86017b2 --- /dev/null +++ b/libs/python/example/test_getting_started2.py @@ -0,0 +1,34 @@ +# Copyright Ralf W. Grosse-Kunstleve 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) +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): + if args is not None: + import sys + sys.argv = args + import doctest, test_getting_started2 + return doctest.testmod(test_getting_started2) + +if __name__ == '__main__': + import sys + sys.exit(run()[0]) + diff --git a/libs/python/example/test_std_pair.py b/libs/python/example/test_std_pair.py new file mode 100644 index 000000000..64f239fea --- /dev/null +++ b/libs/python/example/test_std_pair.py @@ -0,0 +1,6 @@ +# Copyright Ralf W. Grosse-Kunstleve 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) +import std_pair_ext +assert std_pair_ext.foo() == (3, 5) +print "OK" diff --git a/libs/python/example/tutorial/Jamroot b/libs/python/example/tutorial/Jamroot new file mode 100644 index 000000000..1a70cb91a --- /dev/null +++ b/libs/python/example/tutorial/Jamroot @@ -0,0 +1,48 @@ +# 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) + +import python ; + +if ! [ python.configured ] +{ + ECHO "notice: no Python configured in user-config.jam" ; + ECHO "notice: will use default configuration" ; + using python ; +} + +# Specify the path to the Boost project. If you move this project, +# adjust this path to refer to the Boost root directory. +use-project boost + : ../../../.. ; + +# Set up the project-wide requirements that everything uses the +# boost_python library from the project whose global ID is +# /boost/python. +project + : requirements <library>/boost/python//boost_python ; + +# Declare the three extension modules. You can specify multiple +# source files after the colon separated by spaces. +python-extension hello_ext : hello.cpp ; + +# Put the extension and Boost.Python DLL in the current directory, so +# that running script by hand works. +install convenient_copy + : hello_ext + : <install-dependencies>on <install-type>SHARED_LIB <install-type>PYTHON_EXTENSION + <location>. + ; + +# A little "rule" (function) to clean up the syntax of declaring tests +# of these extension modules. +local rule run-test ( test-name : sources + ) +{ + import testing ; + testing.make-test run-pyd : $(sources) : : $(test-name) ; +} + +# Declare test targets +run-test hello : hello_ext hello.py ; + + diff --git a/libs/python/example/tutorial/hello.cpp b/libs/python/example/tutorial/hello.cpp new file mode 100644 index 000000000..d5114312b --- /dev/null +++ b/libs/python/example/tutorial/hello.cpp @@ -0,0 +1,20 @@ +// Copyright Joel de Guzman 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) +// Hello World Example from the tutorial +// [Joel de Guzman 10/9/2002] + +#include <boost/python/module.hpp> +#include <boost/python/def.hpp> + +char const* greet() +{ + return "hello, world"; +} + +BOOST_PYTHON_MODULE(hello_ext) +{ + using namespace boost::python; + def("greet", greet); +} + diff --git a/libs/python/example/tutorial/hello.py b/libs/python/example/tutorial/hello.py new file mode 100755 index 000000000..d18b1c535 --- /dev/null +++ b/libs/python/example/tutorial/hello.py @@ -0,0 +1,7 @@ +# Copyright Joel de Guzman 2002-2007. 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) +# Hello World Example from the tutorial + +import hello_ext +print hello_ext.greet() |