|
Boost-Commit : |
From: doomster_at_[hidden]
Date: 2008-04-30 10:41:45
Author: eckhardt
Date: 2008-04-30 10:41:44 EDT (Wed, 30 Apr 2008)
New Revision: 44931
URL: http://svn.boost.org/trac/boost/changeset/44931
Log:
- Add include file to compile the Python support library in-place.
- Add example files for both embedding and extending Python.
Added:
sandbox/compile-in-place/Boost_1_35_0/boost/python/compile_in_place.cpp (contents, props changed)
sandbox/compile-in-place/test-python-embed.cpp (contents, props changed)
sandbox/compile-in-place/test-python-extend.cpp (contents, props changed)
Added: sandbox/compile-in-place/Boost_1_35_0/boost/python/compile_in_place.cpp
==============================================================================
--- (empty file)
+++ sandbox/compile-in-place/Boost_1_35_0/boost/python/compile_in_place.cpp 2008-04-30 10:41:44 EDT (Wed, 30 Apr 2008)
@@ -0,0 +1,48 @@
+/* compile in-place support for Boost.Python
+
+Copyright 2008 Ulrich Eckhardt
+
+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)
+*/
+// $Id$
+
+// This file must not be included more than once. Note that this 'once' is
+// not per translation unit but per binary! Never include this in a header!
+#ifdef BOOST_PYTHON_COMPILE_IN_PLACE_CPP_INCLUDED
+# error "this file should only be included once per binary"
+#endif
+
+/* Note: this isn't a working include guard, but it's the best we can do here.
+If the user includes this in more than one translation unit they will simply
+get linker errors. */
+#define BOOST_PYTHON_COMPILE_IN_PLACE_CPP_INCLUDED
+
+#include "../../libs/python/src/dict.cpp"
+#include "../../libs/python/src/errors.cpp"
+#include "../../libs/python/src/exec.cpp"
+#include "../../libs/python/src/import.cpp"
+#include "../../libs/python/src/list.cpp"
+#include "../../libs/python/src/long.cpp"
+#include "../../libs/python/src/module.cpp"
+#include "../../libs/python/src/numeric.cpp"
+#include "../../libs/python/src/object_operators.cpp"
+#include "../../libs/python/src/object_protocol.cpp"
+#include "../../libs/python/src/slice.cpp"
+#include "../../libs/python/src/str.cpp"
+#include "../../libs/python/src/tuple.cpp"
+#include "../../libs/python/src/wrapper.cpp"
+#include "../../libs/python/src/converter/arg_to_python_base.cpp"
+#include "../../libs/python/src/converter/builtin_converters.cpp"
+#include "../../libs/python/src/converter/from_python.cpp"
+#include "../../libs/python/src/converter/registry.cpp"
+#include "../../libs/python/src/converter/type_id.cpp"
+#include "../../libs/python/src/object/class.cpp"
+#include "../../libs/python/src/object/enum.cpp"
+#include "../../libs/python/src/object/function.cpp"
+#include "../../libs/python/src/object/function_doc_signature.cpp"
+#include "../../libs/python/src/object/inheritance.cpp"
+#include "../../libs/python/src/object/life_support.cpp"
+#include "../../libs/python/src/object/pickle_support.cpp"
+#include "../../libs/python/src/object/stl_iterator.cpp"
+
Added: sandbox/compile-in-place/test-python-embed.cpp
==============================================================================
--- (empty file)
+++ sandbox/compile-in-place/test-python-embed.cpp 2008-04-30 10:41:44 EDT (Wed, 30 Apr 2008)
@@ -0,0 +1,47 @@
+/* example to demonstrate compile-in-place for embedding Python
+
+Compile with
+ $CXX -I path/to/boost_X_YY_Z -I path/to/python-includes test-python-extends.cpp -l python-libs
+
+Notes:
+
+- This was originally developed with Boost 1.34, where the support for
+embedding Python looked incomplete (see e.g. the necessary calls to Py_*).
+This might be changed with 1.35 released now, need to check that.
+
+- Compiling with g++ -Wall will give you lots of warnings that a string
+literal is converted to a non-const pointer because the Python functions are
+completely ignorant of 'const'. You should be able to ignore those warnings
+or turn them off with -Wno-write-strings.
+
+
+$Id$
+*/
+#include <boost/python.hpp>
+#include <boost/python/compile_in_place.cpp>
+#include <iostream>
+#include <ostream>
+#include <string>
+
+using namespace boost::python;
+
+int main()
+{
+ Py_Initialize();
+
+ object main_module(
+ handle<>(borrowed(PyImport_AddModule("__main__"))));
+ object main_namespace = main_module.attr("__dict__");
+
+ object res((handle<>(PyRun_String(
+ "'x'+'y'",
+ Py_eval_input,
+ main_namespace.ptr(),
+ main_namespace.ptr())
+ )));
+
+ std::string const str = extract<std::string>(res);
+ std::cout << "str=" << str << std::endl;
+
+ Py_Finalize();
+}
Added: sandbox/compile-in-place/test-python-extend.cpp
==============================================================================
--- (empty file)
+++ sandbox/compile-in-place/test-python-extend.cpp 2008-04-30 10:41:44 EDT (Wed, 30 Apr 2008)
@@ -0,0 +1,39 @@
+/* example to demonstrate compile-in-place for extending Python
+
+Compile with
+ $CXX -I path/to/boost_X_YY_Z -I path/to/python-includes test-python-extends.cpp -shared -o hello.so
+
+Notes:
+
+- This must be compiled into a shared object (hello.so) or DLL (hello.dll) so
+that you can load it from Python. The exact syntax for that probably differs
+from above commandline.
+
+- To load it, simply open an interactive interpreter in the same dir as the
+compiled module and "import hello". You can then call the exported function
+using "hello.greet()".
+
+- Compiling with g++ -Wall will give you lots of warnings that a string
+literal is converted to a non-const pointer because the Python functions are
+completely ignorant of 'const'. You should be able to ignore those warnings
+or turn them off with -Wno-write-strings.
+
+
+$Id$
+*/
+
+#include <boost/python.hpp>
+#include <boost/python/compile_in_place.cpp>
+
+using namespace boost::python;
+
+char const* greet()
+{
+ return "hello, world";
+}
+
+BOOST_PYTHON_MODULE(hello)
+{
+ def("greet", greet);
+}
+
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk