mirror of
https://github.com/boostorg/python.git
synced 2026-01-31 08:22:18 +00:00
Compare commits
190 Commits
boost-1.33
...
svn-branch
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4fa60a293 | ||
|
|
04e54d670c | ||
|
|
dd7c0a7f3d | ||
|
|
9de994c0d1 | ||
|
|
e9caacc428 | ||
|
|
5070e84f70 | ||
|
|
fe23d9885f | ||
|
|
e7ee17b71b | ||
|
|
5edb63d01c | ||
|
|
f4b3aab7d4 | ||
|
|
6af67d1a4c | ||
|
|
16d975ba5c | ||
|
|
4fc5cafd40 | ||
|
|
1b5cd10f7c | ||
|
|
0f91872518 | ||
|
|
dc6b2979e4 | ||
|
|
50034140c4 | ||
|
|
08a6f35ec2 | ||
|
|
67236ffbad | ||
|
|
96ab7a80a4 | ||
|
|
d8c3ff199e | ||
|
|
0c4ebef579 | ||
|
|
8fe9d41b58 | ||
|
|
8a4590b2ef | ||
|
|
d67b040683 | ||
|
|
2db61657f2 | ||
|
|
6d2ee96ba3 | ||
|
|
a74c8e3da3 | ||
|
|
9f4d39d9fe | ||
|
|
31c19644ed | ||
|
|
600d444136 | ||
|
|
c3bd0fcbad | ||
|
|
991a7c198a | ||
|
|
9b67f0447d | ||
|
|
b714f6cc23 | ||
|
|
479a6ba4fc | ||
|
|
d78836b828 | ||
|
|
7a59131d37 | ||
|
|
5ab00bc9c8 | ||
|
|
94500ae36d | ||
|
|
5e5d34cc36 | ||
|
|
c6f2aa4ef2 | ||
|
|
c7fb2f7047 | ||
|
|
afedc1cd9a | ||
|
|
070e02d7d5 | ||
|
|
ccc56c2a4c | ||
|
|
e00a88ff49 | ||
|
|
e527bc860f | ||
|
|
921e306b9a | ||
|
|
bed1d26904 | ||
|
|
41a342f026 | ||
|
|
cee8e07046 | ||
|
|
0806e89964 | ||
|
|
f5421ca6b2 | ||
|
|
eea7697175 | ||
|
|
777ce7b561 | ||
|
|
864ece5539 | ||
|
|
2610eb9acb | ||
|
|
567a2c7b89 | ||
|
|
05070faf12 | ||
|
|
ad8069314d | ||
|
|
9366c48351 | ||
|
|
5a14319753 | ||
|
|
279a4f7888 | ||
|
|
d3418d494c | ||
|
|
c839427246 | ||
|
|
dd3a136b18 | ||
|
|
48696918de | ||
|
|
88be35ddc2 | ||
|
|
9ee0d36a1d | ||
|
|
f240e0bab6 | ||
|
|
4081605e4b | ||
|
|
f332ff2d89 | ||
|
|
ec77608840 | ||
|
|
f5a69a1dab | ||
|
|
a1e865061c | ||
|
|
596e92404a | ||
|
|
2640f5af94 | ||
|
|
0605e9fdcf | ||
|
|
cf68da0b19 | ||
|
|
d3c474b295 | ||
|
|
c9300e07c2 | ||
|
|
cab94a7bba | ||
|
|
caa9cb8268 | ||
|
|
66ac61450e | ||
|
|
59f81def56 | ||
|
|
92862028b7 | ||
|
|
b0ba7dfc50 | ||
|
|
fe3abeda9f | ||
|
|
3fdfb30e33 | ||
|
|
cdcf8633bb | ||
|
|
e7927ef4ea | ||
|
|
8c1f04bd4c | ||
|
|
136587c96f | ||
|
|
23664ec448 | ||
|
|
6b1a3c4489 | ||
|
|
fa4ebe5c53 | ||
|
|
28ef4a9e38 | ||
|
|
5d1053552c | ||
|
|
0a38ca1660 | ||
|
|
5791e3b58e | ||
|
|
2bfd2fa0fd | ||
|
|
0793267bf0 | ||
|
|
eca25c0b7d | ||
|
|
a8bad65556 | ||
|
|
6ef31ba33a | ||
|
|
c15216b385 | ||
|
|
a1ff35769b | ||
|
|
22e82ae30f | ||
|
|
2d117bc4ad | ||
|
|
6347ca8065 | ||
|
|
c39836ddc8 | ||
|
|
fb35a82bf1 | ||
|
|
44c5c18f45 | ||
|
|
e0cceeb88c | ||
|
|
4a5f6f2e24 | ||
|
|
8b1748fea0 | ||
|
|
82919f0d5c | ||
|
|
1f6ded7b4e | ||
|
|
a0d2873156 | ||
|
|
0519d54229 | ||
|
|
c181874335 | ||
|
|
203a42c35f | ||
|
|
8eba0eb25b | ||
|
|
152e76220a | ||
|
|
8897cc9ce6 | ||
|
|
335cd02c2d | ||
|
|
758d92b33e | ||
|
|
28eef45d28 | ||
|
|
d8790a34d3 | ||
|
|
3b058185c6 | ||
|
|
2261e7eedc | ||
|
|
19a196493f | ||
|
|
d10b5e8d1a | ||
|
|
1cacefc226 | ||
|
|
efcd2833f1 | ||
|
|
2f9323d9e9 | ||
|
|
8b2f4b4ce0 | ||
|
|
ab046dc634 | ||
|
|
ef3f9b15f0 | ||
|
|
68463e2fd2 | ||
|
|
f75eca94e0 | ||
|
|
a23030b83e | ||
|
|
321cf2502a | ||
|
|
4996f912b4 | ||
|
|
09e24cb17d | ||
|
|
ac32d13e10 | ||
|
|
b0496d1207 | ||
|
|
a076239fc8 | ||
|
|
7cf0f9090f | ||
|
|
479f068673 | ||
|
|
9b326f15fa | ||
|
|
f094a5b9eb | ||
|
|
4367850e5d | ||
|
|
f44a4d6468 | ||
|
|
5206dd55d2 | ||
|
|
988bf849a1 | ||
|
|
6fee43fc6f | ||
|
|
6ec4387ea1 | ||
|
|
e2f59ef548 | ||
|
|
92a6fafd20 | ||
|
|
4721f5f9af | ||
|
|
3864838da2 | ||
|
|
8e77df69d5 | ||
|
|
12770b03e8 | ||
|
|
c8a692b4b4 | ||
|
|
2571ebb0c2 | ||
|
|
283dbfb593 | ||
|
|
83f227034f | ||
|
|
c5f514a4e6 | ||
|
|
9fb15f631e | ||
|
|
3d8f4c90ba | ||
|
|
5597dcb321 | ||
|
|
ab2912e3c2 | ||
|
|
b705931ff0 | ||
|
|
2974286209 | ||
|
|
1cec514b39 | ||
|
|
8ecd49cbf0 | ||
|
|
67a7669ff4 | ||
|
|
e80545a7d3 | ||
|
|
6afe0d4732 | ||
|
|
f8280b0e1a | ||
|
|
aa20ce7d2c | ||
|
|
6074a23242 | ||
|
|
9ceac3ff8f | ||
|
|
126a3efb92 | ||
|
|
9205f507b0 | ||
|
|
bff975f08c | ||
|
|
262bcee750 | ||
|
|
a3f12b18b1 |
Binary file not shown.
@@ -1,96 +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.
|
|
||||||
#
|
|
||||||
# Boost.Python library Jamfile
|
|
||||||
|
|
||||||
|
|
||||||
# declare the location of this subproject relative to the root
|
|
||||||
subproject libs/python/build ;
|
|
||||||
|
|
||||||
# bring in the rules for python
|
|
||||||
import python ;
|
|
||||||
|
|
||||||
if [ check-python-config ]
|
|
||||||
{
|
|
||||||
|
|
||||||
local bpl-linkflags ;
|
|
||||||
|
|
||||||
if $(UNIX) && ( $(OS) = AIX )
|
|
||||||
{
|
|
||||||
bpl-linkflags = <linkflags>"-e initlibboost_python" ;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Enabling intrinsics (/0i) or maximize speed (/02) seem to cause
|
|
||||||
# internal compiler errors with this toolset.
|
|
||||||
local msvc-stlport-workarounds
|
|
||||||
= <optimization>off "<cxxflags>-Ogty -O1 -Gs" ;
|
|
||||||
|
|
||||||
local sources =
|
|
||||||
numeric.cpp
|
|
||||||
list.cpp
|
|
||||||
long.cpp
|
|
||||||
dict.cpp
|
|
||||||
tuple.cpp
|
|
||||||
str.cpp
|
|
||||||
slice.cpp
|
|
||||||
|
|
||||||
aix_init_module.cpp
|
|
||||||
converter/from_python.cpp
|
|
||||||
converter/registry.cpp
|
|
||||||
converter/type_id.cpp
|
|
||||||
object/enum.cpp
|
|
||||||
object/class.cpp
|
|
||||||
object/function.cpp
|
|
||||||
object/inheritance.cpp
|
|
||||||
object/life_support.cpp
|
|
||||||
object/pickle_support.cpp
|
|
||||||
errors.cpp
|
|
||||||
module.cpp
|
|
||||||
converter/builtin_converters.cpp
|
|
||||||
converter/arg_to_python_base.cpp
|
|
||||||
object/iterator.cpp
|
|
||||||
object_protocol.cpp
|
|
||||||
object_operators.cpp
|
|
||||||
wrapper.cpp
|
|
||||||
;
|
|
||||||
|
|
||||||
dll boost_python
|
|
||||||
: ../src/$(sources)
|
|
||||||
: $(BOOST_PYTHON_V2_PROPERTIES)
|
|
||||||
<define>BOOST_PYTHON_SOURCE
|
|
||||||
$(bpl-linkflags)
|
|
||||||
<msvc-stlport><release>$(msvc-stlport-workarounds)
|
|
||||||
<darwin><*><linkflags>-bind_at_load
|
|
||||||
<gcc-3_3-darwin><*><linkflags>-bind_at_load
|
|
||||||
;
|
|
||||||
|
|
||||||
template extension
|
|
||||||
: <dll>boost_python
|
|
||||||
: <sysinclude>../../..
|
|
||||||
;
|
|
||||||
|
|
||||||
lib boost_python
|
|
||||||
: # sources
|
|
||||||
../src/$(sources)
|
|
||||||
|
|
||||||
: # requirements
|
|
||||||
$(BOOST_PYTHON_V2_PROPERTIES)
|
|
||||||
<define>BOOST_PYTHON_SOURCE
|
|
||||||
<define>BOOST_STATIC_LIB
|
|
||||||
$(bpl-linkflags)
|
|
||||||
<msvc-stlport><release>$(msvc-stlport-workarounds)
|
|
||||||
;
|
|
||||||
|
|
||||||
stage bin-stage : <dll>boost_python <lib>boost_python
|
|
||||||
: <tag><debug>"_debug"
|
|
||||||
<tag><debug-python>"_pydebug"
|
|
||||||
:
|
|
||||||
debug release
|
|
||||||
;
|
|
||||||
|
|
||||||
install python lib
|
|
||||||
: <dll>boost_python <lib>boost_python
|
|
||||||
;
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
# Copyright David Abrahams 2001-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 os ;
|
import os ;
|
||||||
import modules ;
|
import modules ;
|
||||||
|
|
||||||
@@ -8,25 +12,10 @@ if [ python.configured ] {
|
|||||||
|
|
||||||
project boost/python
|
project boost/python
|
||||||
: source-location ../src
|
: source-location ../src
|
||||||
: requirements
|
|
||||||
#<include>$(PYTHON_PATH)/include
|
|
||||||
# $(lib_condition)<library-path>$(PYTHON_PATH)/libs
|
|
||||||
# <link>shared:<library>$(PYTHON_LIB)
|
|
||||||
# <define>$(defines)
|
|
||||||
#: usage-requirements # requirement that will be propageted to *users* of this library
|
|
||||||
# <include>$(PYTHON_PATH)/include
|
|
||||||
|
|
||||||
# We have a bug which causes us to conclude that conditionalized
|
|
||||||
# properties in this section are not free.
|
|
||||||
# $(lib_condition)<library-path>$(PYTHON_PATH)/lib/python2.2/config
|
|
||||||
# <shared>true:<find-library>$(PYTHON_LIB)
|
|
||||||
|
|
||||||
# <library-path>$(PYTHON_PATH)/lib/python2.2/config
|
|
||||||
# <library>$(PYTHON_LIB)
|
|
||||||
;
|
;
|
||||||
|
|
||||||
lib boost_python
|
lib boost_python
|
||||||
:
|
: # sources
|
||||||
numeric.cpp
|
numeric.cpp
|
||||||
list.cpp
|
list.cpp
|
||||||
long.cpp
|
long.cpp
|
||||||
@@ -50,13 +39,35 @@ lib boost_python
|
|||||||
converter/builtin_converters.cpp
|
converter/builtin_converters.cpp
|
||||||
converter/arg_to_python_base.cpp
|
converter/arg_to_python_base.cpp
|
||||||
object/iterator.cpp
|
object/iterator.cpp
|
||||||
|
object/stl_iterator.cpp
|
||||||
object_protocol.cpp
|
object_protocol.cpp
|
||||||
object_operators.cpp
|
object_operators.cpp
|
||||||
wrapper.cpp
|
wrapper.cpp
|
||||||
: <link>static:<define>BOOST_PYTHON_STATIC_LIB
|
import.cpp
|
||||||
|
exec.cpp
|
||||||
|
: # requirements
|
||||||
|
<link>static:<define>BOOST_PYTHON_STATIC_LIB
|
||||||
<define>BOOST_PYTHON_SOURCE
|
<define>BOOST_PYTHON_SOURCE
|
||||||
<library>/python//python
|
|
||||||
: <link>shared
|
# On Windows, all code using Python has to link to the Python
|
||||||
|
# import library.
|
||||||
|
#
|
||||||
|
# On *nix we never link libboost_python to libpython. When
|
||||||
|
# extending Python, all Python symbols are provided by the
|
||||||
|
# Python interpreter executable. When embedding Python, the
|
||||||
|
# client executable is expected to explicitly link to
|
||||||
|
# /python//python (the target representing libpython) itself.
|
||||||
|
#
|
||||||
|
# python_for_extensions is a target defined by Boost.Build to
|
||||||
|
# provide the Python include paths, and on Windows, the Python
|
||||||
|
# import library, as usage requirements.
|
||||||
|
<library>/python//python_for_extensions
|
||||||
|
|
||||||
|
: # default build
|
||||||
|
<link>shared
|
||||||
|
: # usage requirements
|
||||||
|
<link>static:<define>BOOST_PYTHON_STATIC_LIB
|
||||||
|
<link>shared:<define>BOOST_PYTHON_DYNAMIC_LIB
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -179,6 +179,10 @@ SOURCE=..\..\src\slice.cpp
|
|||||||
# End Source File
|
# End Source File
|
||||||
# Begin Source File
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\..\src\object\stl_iterator.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
SOURCE=..\..\src\str.cpp
|
SOURCE=..\..\src\str.cpp
|
||||||
# End Source File
|
# End Source File
|
||||||
# Begin Source File
|
# Begin Source File
|
||||||
@@ -193,6 +197,14 @@ SOURCE=..\..\src\converter\type_id.cpp
|
|||||||
|
|
||||||
SOURCE=..\..\src\wrapper.cpp
|
SOURCE=..\..\src\wrapper.cpp
|
||||||
# End Source File
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\..\src\import.cpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\..\src\exec.cpp
|
||||||
|
# End Source File
|
||||||
# End Group
|
# End Group
|
||||||
# Begin Group "Header Files"
|
# Begin Group "Header Files"
|
||||||
|
|
||||||
@@ -594,6 +606,10 @@ SOURCE=..\..\..\..\boost\python\object\select_holder.hpp
|
|||||||
# End Source File
|
# End Source File
|
||||||
# Begin Source File
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\..\..\..\boost\python\object\stl_iterator_core.hpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
SOURCE=..\..\..\..\boost\python\object\value_holder.hpp
|
SOURCE=..\..\..\..\boost\python\object\value_holder.hpp
|
||||||
# End Source File
|
# End Source File
|
||||||
# Begin Source File
|
# Begin Source File
|
||||||
@@ -851,6 +867,10 @@ SOURCE=..\..\..\..\boost\python\slice_nil.hpp
|
|||||||
# End Source File
|
# End Source File
|
||||||
# Begin Source File
|
# Begin Source File
|
||||||
|
|
||||||
|
SOURCE=..\..\..\..\boost\python\stl_iterator.hpp
|
||||||
|
# End Source File
|
||||||
|
# Begin Source File
|
||||||
|
|
||||||
SOURCE=..\..\..\..\boost\python\str.hpp
|
SOURCE=..\..\..\..\boost\python\str.hpp
|
||||||
# End Source File
|
# End Source File
|
||||||
# Begin Source File
|
# Begin Source File
|
||||||
|
|||||||
23
doc/Jamfile
Normal file
23
doc/Jamfile
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# 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 docutils ;
|
||||||
|
|
||||||
|
import path ;
|
||||||
|
sources = building.rst ;
|
||||||
|
bases = $(sources:S=) ;
|
||||||
|
|
||||||
|
# This is a path relative to the html/ subdirectory where the
|
||||||
|
# generated output will eventually be moved.
|
||||||
|
stylesheet = "--stylesheet=../../../rst.css" ;
|
||||||
|
|
||||||
|
for local b in $(bases)
|
||||||
|
{
|
||||||
|
html $(b) : $(b).rst :
|
||||||
|
|
||||||
|
<docutils-html>"-gdt --source-url="./$(b).rst" --link-stylesheet --traceback --trim-footnote-reference-space --footnote-references=superscript "$(stylesheet)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
alias htmls : $(bases) ;
|
||||||
|
stage . : $(bases) ;
|
||||||
@@ -1,5 +1,11 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1 +1,5 @@
|
|||||||
|
.. 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)
|
||||||
|
|
||||||
This file has been moved to http://www.boost-consulting.com/writing/bpl.txt.
|
This file has been moved to http://www.boost-consulting.com/writing/bpl.txt.
|
||||||
|
|||||||
@@ -1,3 +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)
|
||||||
|
|
||||||
.. This is a comment. Note how any initial comments are moved by
|
.. This is a comment. Note how any initial comments are moved by
|
||||||
transforms to after the document title, subtitle, and docinfo.
|
transforms to after the document title, subtitle, and docinfo.
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
:version: $Revision$
|
:version: $Revision$
|
||||||
:copyright: This stylesheet has been placed in the public domain.
|
:copyright: This stylesheet has been placed in the public domain.
|
||||||
|
|
||||||
|
boostinspect:nolicense
|
||||||
|
|
||||||
Default cascading style sheet for the HTML output of Docutils.
|
Default cascading style sheet for the HTML output of Docutils.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +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)
|
||||||
|
*/
|
||||||
H1
|
H1
|
||||||
{
|
{
|
||||||
FONT-SIZE: 200%
|
FONT-SIZE: 200%
|
||||||
|
|||||||
@@ -1,454 +1,415 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
<html>
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||||
"HTML Tidy for Cygwin (vers 1st April 2002), see www.w3.org">
|
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<title>Boost C++ Libraries: Boost.Python Build and Test HOWTO</title>
|
||||||
<link rel="stylesheet" type="text/css" href="boost.css">
|
<link rel="stylesheet" href="../../../rst.css" type="text/css" />
|
||||||
|
</head>
|
||||||
<title>Boost.Python - Building and Testing</title>
|
<body>
|
||||||
</head>
|
<div class="document" id="logo-boost-python-build-and-test-howto">
|
||||||
|
<h1 class="title"><a class="reference" href="../index.htm"><img alt="Boost C++ Libraries:" class="boost-logo" src="../boost.png" /></a> Boost.Python Build and Test HOWTO</h1>
|
||||||
<body link="#0000ff" vlink="#800080">
|
|
||||||
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
<!-- Copyright David Abrahams 2006. Distributed under the Boost -->
|
||||||
"header">
|
<!-- Software License, Version 1.0. (See accompanying -->
|
||||||
<tr>
|
<!-- file LICENSE_1_0.txt or copy at -->
|
||||||
<td valign="top" width="300">
|
<!-- http://www.boost.org/LICENSE_1_0.txt) -->
|
||||||
<h3><a href="../../../index.htm"><img height="86" width="277" alt=
|
<div class="contents sidebar small topic">
|
||||||
"C++ Boost" src="../../../boost.png" border="0"></a></h3>
|
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
|
||||||
</td>
|
<ul class="auto-toc simple">
|
||||||
|
<li><a class="reference" href="#requirements" id="id20" name="id20">1 Requirements</a></li>
|
||||||
<td valign="top">
|
<li><a class="reference" href="#background" id="id21" name="id21">2 Background</a></li>
|
||||||
<h1 align="center"><a href="index.html">Boost.Python</a></h1>
|
<li><a class="reference" href="#getting-boost-python-binaries" id="id22" name="id22">3 Getting Boost.Python Binaries</a><ul class="auto-toc">
|
||||||
|
<li><a class="reference" href="#no-install-quickstart" id="id23" name="id23">3.1 No-Install Quickstart</a></li>
|
||||||
<h2 align="center">Building and Testing</h2>
|
<li><a class="reference" href="#installing-boost-python-on-your-system" id="id24" name="id24">3.2 Installing Boost.Python on your System</a></li>
|
||||||
</td>
|
</ul>
|
||||||
</tr>
|
</li>
|
||||||
</table>
|
<li><a class="reference" href="#configuring-boost-build" id="id25" name="id25">4 Configuring Boost.Build</a></li>
|
||||||
<hr>
|
<li><a class="reference" href="#building-an-extension-module" id="id26" name="id26">5 Building an Extension Module</a></li>
|
||||||
|
<li><a class="reference" href="#testing" id="id27" name="id27">6 Testing</a></li>
|
||||||
<h2>Contents</h2>
|
<li><a class="reference" href="#advanced-configuration" id="id28" name="id28">7 Advanced Configuration</a><ul class="auto-toc">
|
||||||
|
<li><a class="reference" href="#python-configuration-parameters" id="id29" name="id29">7.1 Python Configuration Parameters</a></li>
|
||||||
<dl class="Reference">
|
<li><a class="reference" href="#examples" id="id30" name="id30">7.2 Examples</a></li>
|
||||||
<dt><a href="#requirements">Requirements</a></dt>
|
</ul>
|
||||||
|
</li>
|
||||||
<dt><a href="#building">Building Boost.Python</a></dt>
|
<li><a class="reference" href="#choosing-a-boost-python-library-binary" id="id31" name="id31">8 Choosing a Boost.Python Library Binary</a><ul class="auto-toc">
|
||||||
|
<li><a class="reference" href="#the-dynamic-binary" id="id32" name="id32">8.1 The Dynamic Binary</a></li>
|
||||||
<dd>
|
<li><a class="reference" href="#the-static-binary" id="id33" name="id33">8.2 The Static Binary</a></li>
|
||||||
<dl class="index">
|
</ul>
|
||||||
<dt><a href="#configuration">Configuration</a></dt>
|
</li>
|
||||||
|
<li><a class="reference" href="#notes-for-mingw-and-cygwin-with-mno-cygwin-gcc-users" id="id34" name="id34">9 Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users</a></li>
|
||||||
<dt><a href="#cygwin_configuration">Configuration for Cygwin GCC
|
</ul>
|
||||||
from a Windows prompt</a></dt>
|
</div>
|
||||||
|
<div class="section">
|
||||||
<dt><a href="#results">Results</a></dt>
|
<h1><a class="toc-backref" href="#id20" id="requirements" name="requirements">1 Requirements</a></h1>
|
||||||
|
<p>Boost.Python requires <a class="reference" href="http://www.python.org/2.2">Python 2.2</a><a class="footnote-reference" href="#id16" id="id2" name="id2"><sup>1</sup></a> <em>or</em> <a class="reference" href="http://www.python.org"><em>newer</em></a>.</p>
|
||||||
<dt><a href="#cygwin">Notes for Cygwin GCC Users</a></dt>
|
</div>
|
||||||
|
<div class="section">
|
||||||
<dt><a href="#mingw">Notes for MinGW (and Cygwin with -mno-cygwin)
|
<h1><a class="toc-backref" href="#id21" id="background" name="background">2 Background</a></h1>
|
||||||
GCC Users</a></dt>
|
<p>There are two basic models for combining C++ and Python:</p>
|
||||||
|
<ul class="simple">
|
||||||
<dt><a href="#testing">Testing</a></dt>
|
<li><a class="reference" href="http://www.python.org/doc/current/ext/intro.html">extending</a>, in which the end-user launches the Python interpreter
|
||||||
</dl>
|
executable and imports Python “extension modules” written in C++.
|
||||||
</dd>
|
Think of taking a library written in C++ and giving it a Python
|
||||||
|
interface so Python programmers can use it. From Python, these
|
||||||
<dt><a href="#building_ext">Building your Extension Module</a></dt>
|
modules look just like regular Python modules.</li>
|
||||||
|
<li><a class="reference" href="http://www.python.org/doc/current/ext/embedding.html">embedding</a>, in which the end-user launches a program written
|
||||||
<dt><a href="#variants">Build Variants</a></dt>
|
in C++ that in turn invokes the Python interpreter as a library
|
||||||
|
subroutine. Think of adding scriptability to an existing
|
||||||
<dt><a href="#VisualStudio">Building Using the Microsoft Visual Studio
|
application.</li>
|
||||||
IDE</a></dt>
|
</ul>
|
||||||
</dl>
|
<p>The key distinction between extending and embedding is the location
|
||||||
<hr>
|
of C++' <tt class="docutils literal"><span class="pre">main()</span></tt> function: in the Python interpreter executable,
|
||||||
|
or in some other program, respectively. Note that even when
|
||||||
<h2><a name="requirements">Requirements</a></h2>
|
embedding Python in another program, <a class="reference" href="http://www.python.org/doc/current/ext/extending-with-embedding.html">extension modules are often
|
||||||
<b>Boost.Python</b> version 2 requires <a href=
|
the best way to make C/C++ functionality accessible to Python
|
||||||
"http://www.python.org/2.2">Python 2.2</a> <i>or <a href=
|
code</a>, so the use of extension modules is really at the heart of
|
||||||
"http://www.python.org">newer</a></i>. An unsupported archive of
|
both models.</p>
|
||||||
Boost.Python version 1, which works with versions of Python since 1.5.2,
|
<p>Except in rare cases, extension modules are built as
|
||||||
is available <a href="../build/python_v1.zip">here</a>.
|
dynamically-loaded libraries with a single entry point, which means
|
||||||
|
you can change them without rebuilding either the other extension
|
||||||
<h2><a name="building">Building Boost.Python</a></h2>
|
modules or the executable containing <tt class="docutils literal"><span class="pre">main()</span></tt>.</p>
|
||||||
|
</div>
|
||||||
<p>Normally, Boost.Python extension modules must be linked with the
|
<div class="section">
|
||||||
<code>boost_python</code> shared library. In special circumstances you
|
<h1><a class="toc-backref" href="#id22" id="getting-boost-python-binaries" name="getting-boost-python-binaries">3 Getting Boost.Python Binaries</a></h1>
|
||||||
may want to link to a static version of the <code>boost_python</code>
|
<p>Since Boost.Python is a separately-compiled (as opposed to
|
||||||
library, but if multiple Boost.Python extension modules are used
|
<a class="reference" href="../../../more/getting_started/windows.html#header-only-libraries">header-only</a>) library, its user relies on the services of a
|
||||||
together, it will prevent sharing of types across extension modules, and
|
Boost.Python library binary.</p>
|
||||||
consume extra code space. To build <code>boost_python</code>, use <a
|
<div class="section">
|
||||||
href="../../../tools/build/v1/build_system.htm">Boost.Build</a> in the
|
<h2><a class="toc-backref" href="#id23" id="no-install-quickstart" name="no-install-quickstart">3.1 No-Install Quickstart</a></h2>
|
||||||
usual way from the <code>libs/python/build</code> subdirectory of your
|
<p>If you just want to get started quickly building and testing
|
||||||
boost installation (if you have already built boost from the top level
|
Boost.Python extension modules, or embedding Python in an
|
||||||
this may have no effect, since the work is already done).</p>
|
executable, you don't need to worry about installing Boost.Python
|
||||||
|
binaries explicitly. These instructions use <a class="reference" href="../../../tools/build">Boost.Build</a> projects,
|
||||||
<h3><a name="configuration">Basic Configuration</a></h3>
|
which will build those binaries as soon as they're needed. Your
|
||||||
You may need to configure the following variables to point Boost.Build at
|
first tests may take a little longer while you wait for
|
||||||
your Python installation:
|
Boost.Python to build, but doing things this way will save you from
|
||||||
|
worrying about build intricacies like which library binaries to use
|
||||||
<table border="1" summary="build configuration variables">
|
for a specific compiler configuration.</p>
|
||||||
<tr>
|
<div class="note">
|
||||||
<th>Variable Name</th>
|
<p class="first admonition-title">Note</p>
|
||||||
|
<p>Of course it's possible to use other build systems to
|
||||||
<th>Semantics</th>
|
build Boost.Python and its extensions, but they are not
|
||||||
|
officially supported by Boost and <strong>99% of all “I can't build
|
||||||
<th>Default</th>
|
Boost.Python” problems come from trying to use another build
|
||||||
|
system</strong>.</p>
|
||||||
<th>Notes</th>
|
<p class="last">If you want to use another system anyway, we suggest that you
|
||||||
</tr>
|
follow these instructions, and then invoke <tt class="docutils literal"><span class="pre">bjam</span></tt> with the
|
||||||
|
<tt class="docutils literal"><span class="pre">-a</span> <span class="pre">-o</span></tt><em>filename</em> option to dump the build commands it executes
|
||||||
<tr>
|
to a file, so you can see what your build system needs to do.</p>
|
||||||
<td><code>PYTHON_ROOT</code></td>
|
</div>
|
||||||
|
</div>
|
||||||
<td>The root directory of your Python installation</td>
|
<div class="section">
|
||||||
|
<h2><a class="toc-backref" href="#id24" id="installing-boost-python-on-your-system" name="installing-boost-python-on-your-system">3.2 Installing Boost.Python on your System</a></h2>
|
||||||
<td>Windows: <code>c:/tools/python</code>
|
<p>If you need a regular, installation of the Boost.Python library
|
||||||
Unix: <code>/usr/local</code></td>
|
binaries on your system, the Boost <a class="reference" href="../../../more/getting_started/index.html">Getting Started Guide</a> will
|
||||||
|
walk you through the steps of installing one. If building binaries
|
||||||
<td>On Unix, this is the <code>--with-prefix=</code> directory used
|
from source, you might want to supply the <tt class="docutils literal"><span class="pre">--with-python</span></tt>
|
||||||
to configure Python</td>
|
argument to <tt class="docutils literal"><span class="pre">bjam</span></tt> (or the <tt class="docutils literal"><span class="pre">--with-libraries=python</span></tt> argument
|
||||||
</tr>
|
to <tt class="docutils literal"><span class="pre">configure</span></tt>), so only the Boost.Python binary will be built,
|
||||||
|
rather than all the Boost binaries.</p>
|
||||||
<tr>
|
</div>
|
||||||
<td><code>PYTHON_VERSION</code></td>
|
</div>
|
||||||
|
<div class="section">
|
||||||
<td>The The 2-part python Major.Minor version number</td>
|
<h1><a class="toc-backref" href="#id25" id="configuring-boost-build" name="configuring-boost-build">4 Configuring Boost.Build</a></h1>
|
||||||
|
<p>As described in the <a class="reference" href="http://www.boost.orgdoc/html/bbv2/advanced.html#bbv2.advanced.configuration">Boost.Build reference manual</a>, a file called
|
||||||
<td><code>2.2</code></td>
|
<tt class="docutils literal"><span class="pre">user-config.jam</span></tt> in your home
|
||||||
|
directory<a class="footnote-reference" href="#home-dir" id="id5" name="id5"><sup>7</sup></a> is used to
|
||||||
<td>Be sure not to include a third number, e.g. <b>not</b>
|
describe the build resources available to the build system. You'll
|
||||||
"<code>2.2.1</code>", even if that's the version you have.</td>
|
need to tell it about your Python installation.</p>
|
||||||
</tr>
|
<div class="admonition-users-of-unix-variant-oses admonition">
|
||||||
|
<p class="first admonition-title">Users of Unix-Variant OSes</p>
|
||||||
<tr>
|
<p class="last">If you are using a unix-variant OS and you ran Boost's
|
||||||
<td><code>PYTHON_INCLUDES</code></td>
|
<tt class="docutils literal"><span class="pre">configure</span></tt> script, it may have generated a
|
||||||
|
<tt class="docutils literal"><span class="pre">user-config.jam</span></tt> for you.<a class="footnote-reference" href="#overwrite" id="id7" name="id7"><sup>4</sup></a> If your <tt class="docutils literal"><span class="pre">configure</span></tt>/<tt class="docutils literal"><span class="pre">make</span></tt> sequence was successful and Boost.Python binaries
|
||||||
<td>path to Python <code>#include</code> directories</td>
|
were built, your <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> file is probably already
|
||||||
|
correct.</p>
|
||||||
<td>Autoconfigured from <code>PYTHON_ROOT</code>. Try the default
|
</div>
|
||||||
before attempting to set it yourself.</td>
|
<p>If you have a fairly “standard” python installation for your
|
||||||
</tr>
|
platform, there's very little you need to do to describe it.
|
||||||
|
Simply having</p>
|
||||||
<tr>
|
<pre class="literal-block">
|
||||||
<td><code>PYTHON_LIB_PATH</code></td>
|
import toolset : using ;
|
||||||
|
using python ;
|
||||||
<td>path to Python library object.</td>
|
|
||||||
|
|
||||||
<td>Autoconfigured from <code>PYTHON_ROOT</code>. Try the default
|
|
||||||
before attempting to set it yourself.</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h3><a name="cygwin_configuration">Configuration for Cygwin GCC from a
|
|
||||||
Windows prompt</a></h3>
|
|
||||||
The following settings may be useful when building with <a href=
|
|
||||||
"http://www.cygwin.com">Cygwin</a> GCC (not MinGW) from a Windows command
|
|
||||||
shell using a Windows build of <code>bjam</code>. <b>If
|
|
||||||
"<code>bjam -v</code>" does not report "<code>OS=NT</code>", these
|
|
||||||
settings do not apply to you</b>; you should use the <a href=
|
|
||||||
"#configuration">normal configuration</a> variables instead. They are
|
|
||||||
only useful when building and testing with multiple toolsets on Windows
|
|
||||||
using a single build command, since Cygwin GCC requires a different build
|
|
||||||
of Python.
|
|
||||||
|
|
||||||
<table border="1" summary=
|
|
||||||
"Cygwin GCC under NT build configuration variables">
|
|
||||||
<tr>
|
|
||||||
<th>Variable Name</th>
|
|
||||||
|
|
||||||
<th>Semantics</th>
|
|
||||||
|
|
||||||
<th>Default</th>
|
|
||||||
</tr>
|
|
||||||
|
|
||||||
<tr>
|
|
||||||
<td><code>CYGWIN_PYTHON_[DEBUG_]VERSION</code></td>
|
|
||||||
|
|
||||||
<td>The version of python being used under Cygwin.</td>
|
|
||||||
|
|
||||||
<td>$(PYTHON_VERSION)</td>
|
|
||||||
</tr>
|
|
||||||
|
|
||||||
<tr>
|
|
||||||
<td><code>CYGWIN_PYTHON_[DEBUG_]ROOT</code></td>
|
|
||||||
|
|
||||||
<td>unix-style path containing the <code>include/</code> directory
|
|
||||||
containing
|
|
||||||
<code>python$(CYGWIN_PYTHON_[DEBUG_]VERSION)/python.h</code>.</td>
|
|
||||||
|
|
||||||
<td>$(PYTHON_ROOT)</td>
|
|
||||||
</tr>
|
|
||||||
|
|
||||||
<tr>
|
|
||||||
<td><code>CYGWIN_PYTHON_[DEBUG_]LIB_PATH</code></td>
|
|
||||||
|
|
||||||
<td>path containing the user's Cygwin Python import lib
|
|
||||||
<code>libpython$(CYGWIN_PYTHON_[DEBUG_]VERSION).dll.a</code></td>
|
|
||||||
|
|
||||||
<td>Autoconfigured from <code>CYGWIN_PYTHON_ROOT</code></td>
|
|
||||||
</tr>
|
|
||||||
|
|
||||||
<tr>
|
|
||||||
<td><code>CYGWIN_PYTHON_[DEBUG_]DLL_PATH</code></td>
|
|
||||||
|
|
||||||
<td>path containing the user's Cygwin Python dll
|
|
||||||
(<code>libpython$(CYGWIN_PYTHON_[DEBUG_]VERSION).dll</code>)</td>
|
|
||||||
|
|
||||||
<td><code>/bin</code></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h3><a name="cygwin">Notes for Cygwin GCC Users</a></h3>
|
|
||||||
|
|
||||||
<p>If you are using Cygwin GCC to build extension modules, you must use a
|
|
||||||
Cygwin build of Python. The regular Win32 Python installation that you
|
|
||||||
can download from <a href="http://www.python.org">python.org</a> will not
|
|
||||||
work with your compiler because the dynamic linking conventions are
|
|
||||||
different (you can use <a href="http://www.mingw.org/">MinGW</a> GCC if
|
|
||||||
you want to build extension modules which are compatible with a stock
|
|
||||||
Win32 Python). The Cygwin installer may be able to install an appropriate
|
|
||||||
version of Python, or you can follow the traditional <a href=
|
|
||||||
"http://www.python.org/download/download_source.html">Unix installation
|
|
||||||
process</a> to build Python from source.</p>
|
|
||||||
|
|
||||||
<p>The special build configuration variables listed <a href=
|
|
||||||
"#cygwin_configuration">above</a> make it possible to use a regular Win32
|
|
||||||
build of bjam to build and test Boost.Python and Boost.Python extensions
|
|
||||||
using Cygwin GCC and targeting a Cygwin build of Python.</p>
|
|
||||||
|
|
||||||
<h3><a name="mingw">Notes for MinGW (and Cygwin with -mno-cygwin) GCC
|
|
||||||
Users</a></h3>
|
|
||||||
|
|
||||||
<p>You will need to create a MinGW-compatible version of the Python
|
|
||||||
library; the one shipped with Python will only work with a
|
|
||||||
Microsoft-compatible linker. Follow the instructions in the
|
|
||||||
"Non-Microsoft" section of the "Building Extensions: Tips And Tricks"
|
|
||||||
chapter in <a href=
|
|
||||||
"http://www.python.org/doc/current/inst/index.html">Installing Python
|
|
||||||
Modules</a> to create <code>libpythonXX.a</code>, where <code>XX</code>
|
|
||||||
corresponds to the major and minor version numbers of your Python
|
|
||||||
installation.</p>
|
|
||||||
|
|
||||||
<h3><a name="results">Results</a></h3>
|
|
||||||
|
|
||||||
<p>The build process will create a
|
|
||||||
<code>libs/python/build/bin-stage</code> subdirectory of the boost root
|
|
||||||
(or of <code>$(ALL_LOCATE_TARGET)</code>, if you have set that variable),
|
|
||||||
containing the built libraries. The libraries are actually built to
|
|
||||||
unique directories for each toolset and variant elsewhere in the
|
|
||||||
filesystem, and copied to the <code>bin-stage</code> directory as a
|
|
||||||
convenience, so if you build with multiple toolsets at once, the product
|
|
||||||
of later toolsets will overwrite that of earlier toolsets in
|
|
||||||
<code>bin-stage</code>.</p>
|
|
||||||
|
|
||||||
<h3><a name="testing">Testing</a></h3>
|
|
||||||
|
|
||||||
<p>To build and test Boost.Python, start from the
|
|
||||||
<code>libs/python/test</code> directory and invoke</p>
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
bjam -sTOOLS=<i><a href=
|
|
||||||
"../../../more/getting_started.html#Tools">toolset</a></i> test
|
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
<p>in a <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> file in your home directory<a class="footnote-reference" href="#home-dir" id="id8" name="id8"><sup>7</sup></a>
|
||||||
This will update all of the Boost.Python v1 test and example targets. The
|
should be enough.<a class="footnote-reference" href="#user-config-jam" id="id9" name="id9"><sup>6</sup></a> For more complicated setups,
|
||||||
tests are relatively verbose by default. To get less-verbose output, you
|
see <a class="reference" href="#advanced-configuration">Advanced Configuration</a>.</p>
|
||||||
might try
|
<div class="note">
|
||||||
|
<p class="first admonition-title">Note</p>
|
||||||
|
<p class="last">You might want to pass the <tt class="docutils literal"><span class="pre">--debug-configuration</span></tt>
|
||||||
|
option to <tt class="docutils literal"><span class="pre">bjam</span></tt> the first few times you invoke it, to make
|
||||||
|
sure that Boost.Build is correctly locating all the parts of
|
||||||
|
your Python installation. If it isn't, consider passing some of
|
||||||
|
the optional <a class="reference" href="#python-configuration-parameters">Python configuration parameters</a> detailed below.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<h1><a class="toc-backref" href="#id26" id="building-an-extension-module" name="building-an-extension-module">5 Building an Extension Module</a></h1>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<h1><a class="toc-backref" href="#id27" id="testing" name="testing">6 Testing</a></h1>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<h1><a class="toc-backref" href="#id28" id="advanced-configuration" name="advanced-configuration">7 Advanced Configuration</a></h1>
|
||||||
|
<p>If you have several versions of Python installed, or Python is
|
||||||
|
installed in an unusual way, you may want to supply any or all of
|
||||||
|
the following optional parameters to <tt class="docutils literal"><span class="pre">using</span> <span class="pre">python</span></tt>.</p>
|
||||||
|
<div class="section">
|
||||||
|
<h2><a class="toc-backref" href="#id29" id="python-configuration-parameters" name="python-configuration-parameters">7.1 Python Configuration Parameters</a></h2>
|
||||||
|
<dl class="docutils">
|
||||||
|
<dt>version</dt>
|
||||||
|
<dd>the version of Python to use. Should be in Major.Minor
|
||||||
|
format, for example, <tt class="docutils literal"><span class="pre">2.3</span></tt>. Do not include the subminor
|
||||||
|
version (i.e. <em>not</em> <tt class="docutils literal"><span class="pre">2.5.1</span></tt>). If you have multiple Python
|
||||||
|
versions installed, the version will usually be the only
|
||||||
|
additional argument required.</dd>
|
||||||
|
<dt>cmd-or-prefix</dt>
|
||||||
|
<dd>preferably, a command that invokes a Python
|
||||||
|
interpreter. Alternatively, the installation prefix for Python
|
||||||
|
libraries and header files. Use the alternative formulation if
|
||||||
|
there is no appropriate Python executable available.</dd>
|
||||||
|
<dt>includes</dt>
|
||||||
|
<dd>the <tt class="docutils literal"><span class="pre">#include</span></tt> path for Python headers.</dd>
|
||||||
|
<dt>libraries</dt>
|
||||||
|
<dd>the path to Python library binaries. On MacOS/Darwin,
|
||||||
|
you can also pass the path of the Python framework.</dd>
|
||||||
|
<dt>condition</dt>
|
||||||
|
<dd>if specified, should be a set of Boost.Build
|
||||||
|
properties that are matched against the build configuration when
|
||||||
|
Boost.Build selects a Python configuration to use.</dd>
|
||||||
|
<dt>extension-suffix</dt>
|
||||||
|
<dd>A string to append to the name of extension
|
||||||
|
modules before the true filename extension. You almost certainly
|
||||||
|
don't need to use this. Usually this suffix is only used when
|
||||||
|
targeting a Windows debug build of Python, and will be set
|
||||||
|
automatically for you based on the value of the
|
||||||
|
<tt class="docutils literal"><span class="pre"><python-debugging></span></tt> feature. However, at least one Linux
|
||||||
|
distribution (Ubuntu Feisty Fawn) has a specially configured
|
||||||
|
<a class="reference" href="https://wiki.ubuntu.com/PyDbgBuilds">python-dbg</a> package that claims to use such a suffix.</dd>
|
||||||
|
</dl>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<h2><a class="toc-backref" href="#id30" id="examples" name="examples">7.2 Examples</a></h2>
|
||||||
|
<p>Note that in the examples below, case and <em>especially whitespace</em> are
|
||||||
|
significant.</p>
|
||||||
|
<ul>
|
||||||
|
<li><p class="first">If you have both python 2.5 and python 2.4 installed,
|
||||||
|
<tt class="docutils literal"><span class="pre">user-config.jam</span></tt> might contain:</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
using python : 2.5 ; # Make both versions of Python available
|
||||||
|
|
||||||
<blockquote>
|
using python : 2.4 ; # To build with python 2.4, add python=2.4
|
||||||
<pre>
|
# to your command line.
|
||||||
bjam -sTOOLS=<i><a href=
|
|
||||||
"../../../more/getting_started.html#Tools">toolset</a></i> -sPYTHON_TEST_ARGS= test
|
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
<p>The first version configured (2.5) becomes the default. To build
|
||||||
By default, <code>PYTHON_TEST_ARGS</code> is set to <code>-v</code>.
|
against python 2.4, add <tt class="docutils literal"><span class="pre">python=2.4</span></tt> to the <tt class="docutils literal"><span class="pre">bjam</span></tt> command line.</p>
|
||||||
|
</li>
|
||||||
<h2><a name="building_ext">Building your Extension Module</a></h2>
|
<li><p class="first">If you have python installed in an unusual location, you might
|
||||||
Though there are other approaches, the smoothest and most reliable way to
|
supply the path to the interpreter in the <tt class="docutils literal"><span class="pre">cmd-or-prefix</span></tt>
|
||||||
build an extension module using Boost.Python is with Boost.Build. If you
|
parameter:</p>
|
||||||
have to use another build system, you should use Boost.Build at least
|
<pre class="literal-block">
|
||||||
once with the "<code><b>-n</b></code>" option so you can see the
|
using python : : /usr/local/python-2.6-beta/bin/python ;
|
||||||
command-lines it uses, and replicate them. You are likely to run into
|
|
||||||
compilation or linking problems otherwise.
|
|
||||||
|
|
||||||
<p>The files required to build a Boost.Python extension module using bjam
|
|
||||||
are the "local" files <tt>Jamfile</tt>, <tt>Jamrules</tt>, and
|
|
||||||
<tt>boost_build.jam</tt>, and the <tt>boost/</tt>
|
|
||||||
and <tt>tools/build/v1/</tt> subdirectories of your Boost
|
|
||||||
tree. The latter directory contains the source code of the
|
|
||||||
Boost.Build system, which is used to generate the correct build
|
|
||||||
commands for your extension module. The '<tt>v1</tt>' refers to
|
|
||||||
Boost.Build version 1. Version 2 is pre-release and currently not
|
|
||||||
ready for general use.
|
|
||||||
|
|
||||||
<p>
|
|
||||||
The <tt>libs/python/example/</tt> project we're going to build is
|
|
||||||
set up to automatically rebuild the Boost.Python library in place
|
|
||||||
whenever it's out-of-date rather than just reusing an existing
|
|
||||||
library, so you'll also need the Boost.Python library sources in
|
|
||||||
<tt>boost/python/src/</tt>.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<b>Note:</b> Third-party package and distribution maintainers
|
|
||||||
for various operating systems sometimes split up Boost's
|
|
||||||
structure or omit parts of it, so if you didn't download an
|
|
||||||
official <a href=
|
|
||||||
"http://sourceforge.net/project/showfiles.php?group_id=7586">Boost
|
|
||||||
release</a> you might want to <a href=
|
|
||||||
"http://cvs.sourceforge.net/viewcvs.py/boost/boost/">browse our CVS
|
|
||||||
structure</a> to make sure you have everything you need, and in the
|
|
||||||
right places.
|
|
||||||
</blockquote>
|
|
||||||
|
|
||||||
<p>The <code><a href="../example">libs/python/example</a></code>
|
|
||||||
subdirectory of your boost installation contains a small example which
|
|
||||||
builds and tests two extensions. To build your own extensions copy the
|
|
||||||
example subproject and make the following two edits:</p>
|
|
||||||
|
|
||||||
<ol>
|
|
||||||
<li>
|
|
||||||
<code><a href=
|
|
||||||
"../example/boost-build.jam"><b>boost-build.jam</b></a></code> - edit
|
|
||||||
the line which reads
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
boost-build ../../../tools/build/v1 ;
|
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</li>
|
||||||
so that the path refers to the <code>tools/build/v1</code>
|
<li><p class="first">If you have a separate build of Python for use with a particular
|
||||||
subdirectory of your Boost installation.
|
toolset, you might supply that toolset in the <tt class="docutils literal"><span class="pre">condition</span></tt>
|
||||||
</li>
|
parameter:</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
using python ; # use for most toolsets
|
||||||
|
|
||||||
<li>
|
# Use with Intel C++ toolset
|
||||||
<code><a href="../example/Jamrules"><b>Jamrules</b></a></code> - edit
|
using python
|
||||||
the line which reads
|
: # version
|
||||||
|
: c:\\Devel\\Python-2.5-IntelBuild\\PCBuild\\python # cmd-or-prefix
|
||||||
<blockquote>
|
: # includes
|
||||||
<pre>
|
: # libraries
|
||||||
path-global BOOST_ROOT : ../../.. ;
|
: <toolset>intel # condition
|
||||||
|
;
|
||||||
</pre>
|
</pre>
|
||||||
</blockquote>
|
</li>
|
||||||
so that the path refers to the root directory of your Boost
|
<li><p class="first">You can set up your user-config.jam so a bjam built under Windows
|
||||||
installation.
|
can build/test both Windows and <a class="reference" href="http://cygwin.com">Cygwin</a> python extensions. Just pass
|
||||||
</li>
|
<tt class="docutils literal"><span class="pre"><target-os>cygwin</span></tt> in the <tt class="docutils literal"><span class="pre">condition</span></tt> parameter
|
||||||
</ol>
|
for the cygwin python installation:</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
# windows installation
|
||||||
|
using python ;
|
||||||
|
|
||||||
<p>The instructions <a href="#testing">above</a> for testing Boost.Python
|
# cygwin installation
|
||||||
apply equally to your new extension modules in this subproject.</p>
|
using python : : c:\\cygwin\\bin\\python2.5 : : : <target-os>cygwin ;
|
||||||
|
</pre>
|
||||||
|
<p>when you put target-os=cygwin in your build request, it should build
|
||||||
|
with the cygwin version of python:<a class="footnote-reference" href="#flavor" id="id11" name="id11"><sup>5</sup></a></p>
|
||||||
|
<blockquote>
|
||||||
|
<p>bjam target-os=cygwin toolset=gcc</p>
|
||||||
|
</blockquote>
|
||||||
|
<p>This is supposed to work the other way, too (targeting windows
|
||||||
|
python with a <a class="reference" href="http://cygwin.com">Cygwin</a> bjam) but it seems as though the support in
|
||||||
|
Boost.Build's toolsets for building that way is broken at the
|
||||||
|
time of this writing.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first">Note that because of <a class="reference" href="http://zigzag.cs.msu.su/boost.build/wiki/AlternativeSelection">the way Boost.Build currently selects target
|
||||||
|
alternatives</a>, you might have be very explicit in your build
|
||||||
|
requests. For example, given:</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
using python : 2.5 ; # a regular windows build
|
||||||
|
using python : 2.4 : : : : <target-os>cygwin ;
|
||||||
|
</pre>
|
||||||
|
<p>building with</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
bjam target-os=cygwin
|
||||||
|
</pre>
|
||||||
|
<p>will yield an error. Instead, you'll need to write:</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
bjam target-os=cygwin/python=2.4
|
||||||
|
</pre>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<h1><a class="toc-backref" href="#id31" id="choosing-a-boost-python-library-binary" name="choosing-a-boost-python-library-binary">8 Choosing a Boost.Python Library Binary</a></h1>
|
||||||
|
<p>If—instead of letting Boost.Build construct and link withthe right
|
||||||
|
libraries automatically—you choose to use a pre-built Boost.Python
|
||||||
|
library, you'll need to think about which one to link with. The
|
||||||
|
Boost.Python binary comes in both static and dynamic flavors. Take
|
||||||
|
care to choose the right flavor for your application.<a class="footnote-reference" href="#naming" id="id13" name="id13"><sup>2</sup></a></p>
|
||||||
|
<div class="section">
|
||||||
|
<h2><a class="toc-backref" href="#id32" id="the-dynamic-binary" name="the-dynamic-binary">8.1 The Dynamic Binary</a></h2>
|
||||||
|
<p>The dynamic library is the safest and most-versatile choice:</p>
|
||||||
|
<ul class="simple">
|
||||||
|
<li>A single copy of the library code is used by all extension
|
||||||
|
modules built with a given toolset.<a class="footnote-reference" href="#toolset-specific" id="id14" name="id14"><sup>3</sup></a></li>
|
||||||
|
<li>The library contains a type conversion registry. Because one
|
||||||
|
registry is shared among all extension modules, instances of a
|
||||||
|
class exposed to Python in one dynamically-loaded extension
|
||||||
|
module can be passed to functions exposed in another such module.</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<h2><a class="toc-backref" href="#id33" id="the-static-binary" name="the-static-binary">8.2 The Static Binary</a></h2>
|
||||||
|
<p>It might be appropriate to use the static Boost.Python library in
|
||||||
|
any of the following cases:</p>
|
||||||
|
<ul class="simple">
|
||||||
|
<li>You are <a class="reference" href="http://www.python.org/doc/current/ext/intro.html">extending</a> python and the types exposed in your
|
||||||
|
dynamically-loaded extension module don't need to be used by any
|
||||||
|
other Boost.Python extension modules, and you don't care if the
|
||||||
|
core library code is duplicated among them.</li>
|
||||||
|
<li>You are <a class="reference" href="http://www.python.org/doc/current/ext/embedding.html">embedding</a> python in your application and either:<ul>
|
||||||
|
<li>You are targeting a Unix variant OS other than MacOS or AIX,
|
||||||
|
where the dynamically-loaded extension modules can “see” the
|
||||||
|
Boost.Python library symbols that are part of the executable.</li>
|
||||||
|
<li>Or, you have statically linked some Boost.Python extension
|
||||||
|
modules into your application and you don't care if any
|
||||||
|
dynamically-loaded Boost.Python extension modules are able to
|
||||||
|
use the types exposed by your statically-linked extension
|
||||||
|
modules (and vice-versa).</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="section">
|
||||||
|
<h1><a class="toc-backref" href="#id34" id="notes-for-mingw-and-cygwin-with-mno-cygwin-gcc-users" name="notes-for-mingw-and-cygwin-with-mno-cygwin-gcc-users">9 Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users</a></h1>
|
||||||
|
<p>If you are using a version of Python prior to 2.4.1 with a MinGW
|
||||||
|
prior to 3.0.0 (with binutils-2.13.90-20030111-1), you will need to
|
||||||
|
create a MinGW-compatible version of the Python library; the one
|
||||||
|
shipped with Python will only work with a Microsoft-compatible
|
||||||
|
linker. Follow the instructions in the “Non-Microsoft” section of
|
||||||
|
the “Building Extensions: Tips And Tricks” chapter in <a class="reference" href="http://www.python.org/doc/current/inst/index.html">Installing
|
||||||
|
Python Modules</a> to create <tt class="docutils literal"><span class="pre">libpythonXX.a</span></tt>, where <tt class="docutils literal"><span class="pre">XX</span></tt>
|
||||||
|
corresponds to the major and minor version numbers of your Python
|
||||||
|
installation.</p>
|
||||||
|
<hr class="docutils" />
|
||||||
|
<table class="docutils footnote" frame="void" id="id16" rules="none">
|
||||||
|
<colgroup><col class="label" /><col /></colgroup>
|
||||||
|
<tbody valign="top">
|
||||||
|
<tr><td class="label"><a class="fn-backref" href="#id2" name="id16">[1]</a></td><td>Note that although we tested earlier versions of
|
||||||
|
Boost.Python with Python 2.2, and we don't <em>think</em> we've done
|
||||||
|
anything to break compatibility, this release of Boost.Python
|
||||||
|
may not have been tested with versions of Python earlier than
|
||||||
|
2.4, so we're not 100% sure that python 2.2 and 2.3 are
|
||||||
|
supported.</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table class="docutils footnote" frame="void" id="naming" rules="none">
|
||||||
|
<colgroup><col class="label" /><col /></colgroup>
|
||||||
|
<tbody valign="top">
|
||||||
|
<tr><td class="label"><a class="fn-backref" href="#id13" name="naming">[2]</a></td><td><p class="first">Information about how to identify the
|
||||||
|
static and dynamic builds of Boost.Python:</p>
|
||||||
|
<ul class="simple">
|
||||||
|
<li><a class="reference" href="../../../more/getting_started/windows.html#library-naming">on Windows</a></li>
|
||||||
|
<li><a class="reference" href="../../../more/getting_started/unix-variants.html#library-naming">on Unix variants</a></li>
|
||||||
|
</ul>
|
||||||
|
<p class="last">Be sure to read this section even if your compiler supports
|
||||||
|
auto-linking, as Boost.Python does not yet take advantage of
|
||||||
|
that feature.</p>
|
||||||
|
</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table class="docutils footnote" frame="void" id="toolset-specific" rules="none">
|
||||||
|
<colgroup><col class="label" /><col /></colgroup>
|
||||||
|
<tbody valign="top">
|
||||||
|
<tr><td class="label"><a class="fn-backref" href="#id14" name="toolset-specific">[3]</a></td><td>Because of the way most *nix platforms
|
||||||
|
share symbols among dynamically-loaded objects, I'm not
|
||||||
|
certainextension modules built with different compiler toolsets
|
||||||
|
will always use different copies of the Boost.Python library
|
||||||
|
when loaded into the same Python instance. Not using different
|
||||||
|
libraries could be a good thing if the compilers have compatible
|
||||||
|
ABIs, because extension modules built with the two libraries
|
||||||
|
would be interoperable. Otherwise, it could spell disaster,
|
||||||
|
since an extension module and the Boost.Python library would
|
||||||
|
have different ideas of such things as class layout. I would
|
||||||
|
appreciate someone doing the experiment to find out what
|
||||||
|
happens.</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table class="docutils footnote" frame="void" id="overwrite" rules="none">
|
||||||
|
<colgroup><col class="label" /><col /></colgroup>
|
||||||
|
<tbody valign="top">
|
||||||
|
<tr><td class="label"><a class="fn-backref" href="#id7" name="overwrite">[4]</a></td><td><tt class="docutils literal"><span class="pre">configure</span></tt> overwrites the existing
|
||||||
|
<tt class="docutils literal"><span class="pre">user-config.jam</span></tt> in your home directory
|
||||||
|
(if any) after making a backup of the old version.</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table class="docutils footnote" frame="void" id="flavor" rules="none">
|
||||||
|
<colgroup><col class="label" /><col /></colgroup>
|
||||||
|
<tbody valign="top">
|
||||||
|
<tr><td class="label"><a class="fn-backref" href="#id11" name="flavor">[5]</a></td><td>Note that the <tt class="docutils literal"><span class="pre"><target-os>cygwin</span></tt> feature is
|
||||||
|
different from the <tt class="docutils literal"><span class="pre"><flavor>cygwin</span></tt> subfeature of the <tt class="docutils literal"><span class="pre">gcc</span></tt>
|
||||||
|
toolset, and you might need handle both explicitly if you also
|
||||||
|
have a MinGW GCC installed.</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table class="docutils footnote" frame="void" id="user-config-jam" rules="none">
|
||||||
|
<colgroup><col class="label" /><col /></colgroup>
|
||||||
|
<tbody valign="top">
|
||||||
|
<tr><td class="label"><a class="fn-backref" href="#id9" name="user-config-jam">[6]</a></td><td>Create the <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> file if you don't
|
||||||
|
already have one.</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<table class="docutils footnote" frame="void" id="home-dir" rules="none">
|
||||||
|
<colgroup><col class="label" /><col /></colgroup>
|
||||||
|
<tbody valign="top">
|
||||||
|
<tr><td class="label"><a name="home-dir">[7]</a></td><td><em>(<a class="fn-backref" href="#id5">1</a>, <a class="fn-backref" href="#id8">2</a>)</em> <p>Windows users, your home directory can be
|
||||||
|
found by typing:</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
ECHO %HOMEDRIVE%%HOMEPATH%
|
||||||
|
</pre>
|
||||||
|
<p class="last">into a <a class="reference" href="../../../more/getting_started/windows.html#or-build-from-the-command-prompt">Windows command prompt</a></p>
|
||||||
|
</td></tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="footer">
|
||||||
|
<hr class="footer" />
|
||||||
|
<a class="reference" href="./building.rst">View document source</a>.
|
||||||
|
Generated on: 2007-04-05 20:04 UTC.
|
||||||
|
Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
|
||||||
|
|
||||||
<h2><a name="variants">Build Variants</a></h2>
|
</div>
|
||||||
Three <a href=
|
</body>
|
||||||
"../../../tools/build/v1/build_system.htm#variants">variant</a>
|
|
||||||
configurations of all python-related targets are supported, and can be
|
|
||||||
selected by setting the <code><a href=
|
|
||||||
"../../../tools/build/v1/build_system.htm#user_globals">BUILD</a></code>
|
|
||||||
variable:
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li><code>release</code> (optimization, <tt>-DNDEBUG</tt>)</li>
|
|
||||||
|
|
||||||
<li><code>debug</code> (no optimization <tt>-D_DEBUG</tt>)</li>
|
|
||||||
|
|
||||||
<li><code>debug-python</code> (no optimization, <tt>-D_DEBUG
|
|
||||||
-DBOOST_DEBUG_PYTHON</tt>)</li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<p>The first two variants of the <code>boost_python</code> library are
|
|
||||||
built by default, and are compatible with the default Python
|
|
||||||
distribution. The <code>debug-python</code> variant corresponds to a
|
|
||||||
specially-built debugging version of Python. On Unix platforms, this
|
|
||||||
python is built by adding <code>--with-pydebug</code> when configuring
|
|
||||||
the Python build. On Windows, the debugging version of Python is
|
|
||||||
generated by the "Win32 Debug" target of the <code>PCBuild.dsw</code>
|
|
||||||
Visual C++ 6.0 project in the <code>PCBuild</code> subdirectory of your
|
|
||||||
Python distribution. Extension modules built with Python debugging
|
|
||||||
enabled are <b>not link-compatible</b> with a non-debug build of Python.
|
|
||||||
Since few people actually have a debug build of Python (it doesn't come
|
|
||||||
with the standard distribution), the normal <code>debug</code> variant
|
|
||||||
builds modules which are compatible with ordinary Python.</p>
|
|
||||||
|
|
||||||
<p>On many windows compilers, when extension modules are built with
|
|
||||||
<tt>-D_DEBUG</tt>, Python defaults to <i>force</i> linking with a special
|
|
||||||
debugging version of the Python DLL. Since this debug DLL isn't supplied
|
|
||||||
with the default Python installation for Windows, Boost.Python uses
|
|
||||||
<tt><a href=
|
|
||||||
"../../../boost/python/detail/wrap_python.hpp">boost/python/detail/wrap_python.hpp</a></tt>
|
|
||||||
to temporarily undefine <tt>_DEBUG</tt> when <tt>Python.h</tt> is
|
|
||||||
<tt>#include</tt>d - unless <code>BOOST_DEBUG_PYTHON</code> is
|
|
||||||
defined.</p>
|
|
||||||
|
|
||||||
<p>If you want the extra runtime checks available with the debugging
|
|
||||||
version of the library, <tt>#define BOOST_DEBUG_PYTHON</tt> to re-enable
|
|
||||||
python debuggin, and link with the <code>debug-python</code> variant of
|
|
||||||
<tt>boost_python</tt>.</p>
|
|
||||||
|
|
||||||
<p>If you do not <tt>#define BOOST_DEBUG_PYTHON</tt>, be sure that any
|
|
||||||
source files in your extension module <tt>#include <<a href=
|
|
||||||
"../../../boost/python/detail/wrap_python.hpp">boost/python/detail/wrap_python.hpp</a>></tt>
|
|
||||||
instead of the usual <tt>Python.h</tt>, or you will have link
|
|
||||||
incompatibilities.<br>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<h2><a name="VisualStudio">Building Using the Microsoft Visual Studio
|
|
||||||
IDE</a></h2>
|
|
||||||
|
|
||||||
<p>For the those of you who feel more comfortable in the IDE world, a
|
|
||||||
workspace and project file have been included in the <a href=
|
|
||||||
"../build/VisualStudio">libs/python/build/VisualStudio</a> subdirectory.
|
|
||||||
It builds release and debug versions of the Boost.Python libraries and
|
|
||||||
places them and the same directory as Jamfile build does, though the
|
|
||||||
intermediate object files are placed in a different directory. The files
|
|
||||||
have been created using Microsoft Visual C++ version 6, but they should
|
|
||||||
work for later versions as well. You will need to tell the IDE where to
|
|
||||||
find the Python <code>Include/</code> and <code>Libs/</code> directories.
|
|
||||||
Under <b>Tools>Options>Directories</b>, add an entry for the Python
|
|
||||||
include dir (i.e. <code>c:/Python22/Include</code>), and one for the Lib
|
|
||||||
(i.e. <code>c:/Python/Libs</code>. Make sure it is <code>Libs</code> with
|
|
||||||
an "<code>s</code>" and not just <code>Lib</code>).</p>
|
|
||||||
|
|
||||||
<h3>Using the IDE for your own projects</h3>
|
|
||||||
|
|
||||||
<p>Building your own projects using the IDE is slightly more complicated.
|
|
||||||
Firstly, you need to make sure that the project you create as the right
|
|
||||||
kind. It should be a "Win32 Dynamic-Link Library". The default one that
|
|
||||||
Visual Studio 6 creates needs some modifications: turn on RTTI, and
|
|
||||||
change the debug and release builds to use the respective debug and
|
|
||||||
release Multithreaded DLL versions. You should probably turn off
|
|
||||||
incremental linking too -- I believe it a bit flaky. If you do this, then
|
|
||||||
change the "Debug Info" to "Program Database" to get rid of the Edit and
|
|
||||||
Continue warning.</p>
|
|
||||||
|
|
||||||
<p>You'll need to add the Boost root directory under
|
|
||||||
<b>Tools>Options>Directories</b> to get your code compiling. To
|
|
||||||
make it link, add the above <code>boost_python.dsp</code> file to your
|
|
||||||
workspace, and make your project depend upon it (under
|
|
||||||
<b>Project>Dependencies</b>). You should be able to build now.</p>
|
|
||||||
|
|
||||||
<p>Lastly, go to the <b>Project Settings>Debug</b> Page and add the
|
|
||||||
<code>Python.exe</code> as the executable for the project. Set a startup
|
|
||||||
directory, and make sure that your current project's output dll, the
|
|
||||||
<code>boost_python.dll</code> and the <code>python22.dll</code> are on
|
|
||||||
the current <code>PATH</code>. If you have a python script that tests
|
|
||||||
your dll, then add it in the "Program Arguments". Now, if all went well,
|
|
||||||
you should be able to hit the Run (F5) button, and debug your code.</p>
|
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<em>The Visual Studio project files are graciously contributed and
|
|
||||||
maintained by <a href="mailto:brett.calcott@gmail.com">Brett
|
|
||||||
Calcott</a></em>.
|
|
||||||
</blockquote>
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
<p>© Copyright David Abrahams 2002-2004. Permission to copy,
|
|
||||||
use, modify, sell and distribute this document is granted provided
|
|
||||||
this copyright notice appears in all copies. This document is
|
|
||||||
provided ``as is'' without express or implied warranty, and with
|
|
||||||
no claim as to its suitability for any purpose.</p>
|
|
||||||
|
|
||||||
<p>Updated: 13 April 2004 (David Abrahams)</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
404
doc/building.rst
Normal file
404
doc/building.rst
Normal file
@@ -0,0 +1,404 @@
|
|||||||
|
.. 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)
|
||||||
|
|
||||||
|
==============================================
|
||||||
|
|(logo)|__ Boost.Python Build and Test HOWTO
|
||||||
|
==============================================
|
||||||
|
|
||||||
|
.. |(logo)| image:: ../boost.png
|
||||||
|
:alt: Boost C++ Libraries:
|
||||||
|
:class: boost-logo
|
||||||
|
|
||||||
|
__ ../index.htm
|
||||||
|
|
||||||
|
|
||||||
|
.. section-numbering::
|
||||||
|
:depth: 2
|
||||||
|
|
||||||
|
.. contents:: Contents
|
||||||
|
:depth: 2
|
||||||
|
:class: sidebar small
|
||||||
|
|
||||||
|
.. |newer| replace:: *newer*
|
||||||
|
|
||||||
|
Requirements
|
||||||
|
============
|
||||||
|
|
||||||
|
Boost.Python requires `Python 2.2`_ [#2.2]_ *or* |newer|__.
|
||||||
|
|
||||||
|
.. _Python 2.2: http://www.python.org/2.2
|
||||||
|
__ http://www.python.org
|
||||||
|
|
||||||
|
No-Install Quickstart
|
||||||
|
=====================
|
||||||
|
|
||||||
|
There is no need to install Boost in order to get started using
|
||||||
|
Boost.Python. These instructions use Boost.Build_ projects,
|
||||||
|
which will build those binaries as soon as they're needed. Your
|
||||||
|
first tests may take a little longer while you wait for
|
||||||
|
Boost.Python to build, but doing things this way will save you from
|
||||||
|
worrying about build intricacies like which library binaries to use
|
||||||
|
for a specific compiler configuration.
|
||||||
|
|
||||||
|
.. Note:: Of course it's possible to use other build systems to
|
||||||
|
build Boost.Python and its extensions, but they are not
|
||||||
|
officially supported by Boost. Moreover **99% of all “I can't
|
||||||
|
build Boost.Python” problems come from trying to use another
|
||||||
|
build system**.
|
||||||
|
|
||||||
|
If you want to use another system anyway, we suggest that you
|
||||||
|
follow these instructions, and then invoke ``bjam`` with the
|
||||||
|
``-a -o``\ *filename* option to dump the build commands it executes
|
||||||
|
to a file, so you can see what your build system needs to do.
|
||||||
|
|
||||||
|
1. Get Boost; see sections 1 and 2 of the Boost `Getting Started Guide`_.
|
||||||
|
2. Get the ``bjam`` build driver. See sections 5.2.1-5.2.3 of the
|
||||||
|
Boost `Getting Started Guide`_.
|
||||||
|
3. cd into the ``libs/python/test/example`` directory.
|
||||||
|
|
||||||
|
.. _Getting Started Guide: ../../../more/getting_started/index.html
|
||||||
|
|
||||||
|
|
||||||
|
Background
|
||||||
|
==========
|
||||||
|
|
||||||
|
There are two basic models for combining C++ and Python:
|
||||||
|
|
||||||
|
- extending_, in which the end-user launches the Python interpreter
|
||||||
|
executable and imports Python “extension modules” written in C++.
|
||||||
|
Think of taking a library written in C++ and giving it a Python
|
||||||
|
interface so Python programmers can use it. From Python, these
|
||||||
|
modules look just like regular Python modules.
|
||||||
|
|
||||||
|
- embedding_, in which the end-user launches a program written
|
||||||
|
in C++ that in turn invokes the Python interpreter as a library
|
||||||
|
subroutine. Think of adding scriptability to an existing
|
||||||
|
application.
|
||||||
|
|
||||||
|
.. _extending: http://www.python.org/doc/current/ext/intro.html
|
||||||
|
.. _embedding: http://www.python.org/doc/current/ext/embedding.html
|
||||||
|
|
||||||
|
The key distinction between extending and embedding is the location
|
||||||
|
of C++' ``main()`` function: in the Python interpreter executable,
|
||||||
|
or in some other program, respectively. Note that even when
|
||||||
|
embedding Python in another program, `extension modules are often
|
||||||
|
the best way to make C/C++ functionality accessible to Python
|
||||||
|
code`__, so the use of extension modules is really at the heart of
|
||||||
|
both models.
|
||||||
|
|
||||||
|
__ http://www.python.org/doc/current/ext/extending-with-embedding.html
|
||||||
|
|
||||||
|
Except in rare cases, extension modules are built as
|
||||||
|
dynamically-loaded libraries with a single entry point, which means
|
||||||
|
you can change them without rebuilding either the other extension
|
||||||
|
modules or the executable containing ``main()``.
|
||||||
|
|
||||||
|
Getting Boost.Python Binaries
|
||||||
|
=============================
|
||||||
|
|
||||||
|
Since Boost.Python is a separately-compiled (as opposed to
|
||||||
|
`header-only`_) library, its user relies on the services of a
|
||||||
|
Boost.Python library binary.
|
||||||
|
|
||||||
|
.. _header-only: ../../../more/getting_started/windows.html#header-only-libraries
|
||||||
|
|
||||||
|
Installing Boost.Python on your System
|
||||||
|
--------------------------------------
|
||||||
|
|
||||||
|
If you need a regular, installation of the Boost.Python library
|
||||||
|
binaries on your system, the Boost `Getting Started Guide`_ will
|
||||||
|
walk you through the steps of installing one. If building binaries
|
||||||
|
from source, you might want to supply the ``--with-python``
|
||||||
|
argument to ``bjam`` (or the ``--with-libraries=python`` argument
|
||||||
|
to ``configure``), so only the Boost.Python binary will be built,
|
||||||
|
rather than all the Boost binaries.
|
||||||
|
|
||||||
|
Configuring Boost.Build
|
||||||
|
=======================
|
||||||
|
|
||||||
|
As described in the `Boost.Build reference manual`__, a file called
|
||||||
|
``user-config.jam`` in your home
|
||||||
|
directory [#home-dir]_ is used to
|
||||||
|
describe the build resources available to the build system. You'll
|
||||||
|
need to tell it about your Python installation.
|
||||||
|
|
||||||
|
__ http://www.boost.orgdoc/html/bbv2/advanced.html#bbv2.advanced.configuration
|
||||||
|
|
||||||
|
.. Admonition:: Users of Unix-Variant OSes
|
||||||
|
|
||||||
|
If you are using a unix-variant OS and you ran Boost's
|
||||||
|
``configure`` script, it may have generated a
|
||||||
|
``user-config.jam`` for you. [#overwrite]_ If your ``configure``\
|
||||||
|
/\ ``make`` sequence was successful and Boost.Python binaries
|
||||||
|
were built, your ``user-config.jam`` file is probably already
|
||||||
|
correct.
|
||||||
|
|
||||||
|
If you have a fairly “standard” python installation for your
|
||||||
|
platform, there's very little you need to do to describe it.
|
||||||
|
Simply having ::
|
||||||
|
|
||||||
|
|
||||||
|
import toolset : using ;
|
||||||
|
using python ;
|
||||||
|
|
||||||
|
in a ``user-config.jam`` file in your home directory [#home-dir]_
|
||||||
|
should be enough. [#user-config.jam]_ For more complicated setups,
|
||||||
|
see `Advanced Configuration`_.
|
||||||
|
|
||||||
|
.. Note:: You might want to pass the ``--debug-configuration``
|
||||||
|
option to ``bjam`` the first few times you invoke it, to make
|
||||||
|
sure that Boost.Build is correctly locating all the parts of
|
||||||
|
your Python installation. If it isn't, consider passing some of
|
||||||
|
the optional `Python configuration parameters`_ detailed below.
|
||||||
|
|
||||||
|
Building an Extension Module
|
||||||
|
============================
|
||||||
|
|
||||||
|
|
||||||
|
Testing
|
||||||
|
=======
|
||||||
|
|
||||||
|
|
||||||
|
Advanced Configuration
|
||||||
|
======================
|
||||||
|
|
||||||
|
If you have several versions of Python installed, or Python is
|
||||||
|
installed in an unusual way, you may want to supply any or all of
|
||||||
|
the following optional parameters to ``using python``.
|
||||||
|
|
||||||
|
Python Configuration Parameters
|
||||||
|
-------------------------------
|
||||||
|
|
||||||
|
version
|
||||||
|
the version of Python to use. Should be in Major.Minor
|
||||||
|
format, for example, ``2.3``. Do not include the subminor
|
||||||
|
version (i.e. *not* ``2.5.1``). If you have multiple Python
|
||||||
|
versions installed, the version will usually be the only
|
||||||
|
additional argument required.
|
||||||
|
|
||||||
|
cmd-or-prefix
|
||||||
|
preferably, a command that invokes a Python
|
||||||
|
interpreter. Alternatively, the installation prefix for Python
|
||||||
|
libraries and header files. Use the alternative formulation if
|
||||||
|
there is no appropriate Python executable available.
|
||||||
|
|
||||||
|
includes
|
||||||
|
the ``#include`` path for Python headers.
|
||||||
|
|
||||||
|
libraries
|
||||||
|
the path to Python library binaries. On MacOS/Darwin,
|
||||||
|
you can also pass the path of the Python framework.
|
||||||
|
|
||||||
|
condition
|
||||||
|
if specified, should be a set of Boost.Build
|
||||||
|
properties that are matched against the build configuration when
|
||||||
|
Boost.Build selects a Python configuration to use.
|
||||||
|
|
||||||
|
extension-suffix
|
||||||
|
A string to append to the name of extension
|
||||||
|
modules before the true filename extension. You almost certainly
|
||||||
|
don't need to use this. Usually this suffix is only used when
|
||||||
|
targeting a Windows debug build of Python, and will be set
|
||||||
|
automatically for you based on the value of the
|
||||||
|
``<python-debugging>`` feature. However, at least one Linux
|
||||||
|
distribution (Ubuntu Feisty Fawn) has a specially configured
|
||||||
|
`python-dbg`__ package that claims to use such a suffix.
|
||||||
|
|
||||||
|
__ https://wiki.ubuntu.com/PyDbgBuilds
|
||||||
|
|
||||||
|
|
||||||
|
Examples
|
||||||
|
--------
|
||||||
|
|
||||||
|
Note that in the examples below, case and *especially whitespace* are
|
||||||
|
significant.
|
||||||
|
|
||||||
|
- If you have both python 2.5 and python 2.4 installed,
|
||||||
|
``user-config.jam`` might contain::
|
||||||
|
|
||||||
|
using python : 2.5 ; # Make both versions of Python available
|
||||||
|
|
||||||
|
using python : 2.4 ; # To build with python 2.4, add python=2.4
|
||||||
|
# to your command line.
|
||||||
|
|
||||||
|
The first version configured (2.5) becomes the default. To build
|
||||||
|
against python 2.4, add ``python=2.4`` to the ``bjam`` command line.
|
||||||
|
|
||||||
|
- If you have python installed in an unusual location, you might
|
||||||
|
supply the path to the interpreter in the ``cmd-or-prefix``
|
||||||
|
parameter::
|
||||||
|
|
||||||
|
using python : : /usr/local/python-2.6-beta/bin/python ;
|
||||||
|
|
||||||
|
- If you have a separate build of Python for use with a particular
|
||||||
|
toolset, you might supply that toolset in the ``condition``
|
||||||
|
parameter::
|
||||||
|
|
||||||
|
using python ; # use for most toolsets
|
||||||
|
|
||||||
|
# Use with Intel C++ toolset
|
||||||
|
using python
|
||||||
|
: # version
|
||||||
|
: c:\\Devel\\Python-2.5-IntelBuild\\PCBuild\\python # cmd-or-prefix
|
||||||
|
: # includes
|
||||||
|
: # libraries
|
||||||
|
: <toolset>intel # condition
|
||||||
|
;
|
||||||
|
|
||||||
|
- You can set up your user-config.jam so a bjam built under Windows
|
||||||
|
can build/test both Windows and Cygwin_ python extensions. Just pass
|
||||||
|
``<target-os>cygwin`` in the ``condition`` parameter
|
||||||
|
for the cygwin python installation::
|
||||||
|
|
||||||
|
# windows installation
|
||||||
|
using python ;
|
||||||
|
|
||||||
|
# cygwin installation
|
||||||
|
using python : : c:\\cygwin\\bin\\python2.5 : : : <target-os>cygwin ;
|
||||||
|
|
||||||
|
when you put target-os=cygwin in your build request, it should build
|
||||||
|
with the cygwin version of python: [#flavor]_
|
||||||
|
|
||||||
|
bjam target-os=cygwin toolset=gcc
|
||||||
|
|
||||||
|
This is supposed to work the other way, too (targeting windows
|
||||||
|
python with a Cygwin_ bjam) but it seems as though the support in
|
||||||
|
Boost.Build's toolsets for building that way is broken at the
|
||||||
|
time of this writing.
|
||||||
|
|
||||||
|
- Note that because of `the way Boost.Build currently selects target
|
||||||
|
alternatives`__, you might have be very explicit in your build
|
||||||
|
requests. For example, given::
|
||||||
|
|
||||||
|
using python : 2.5 ; # a regular windows build
|
||||||
|
using python : 2.4 : : : : <target-os>cygwin ;
|
||||||
|
|
||||||
|
building with ::
|
||||||
|
|
||||||
|
bjam target-os=cygwin
|
||||||
|
|
||||||
|
will yield an error. Instead, you'll need to write::
|
||||||
|
|
||||||
|
bjam target-os=cygwin/python=2.4
|
||||||
|
|
||||||
|
.. _Cygwin: http://cygwin.com
|
||||||
|
|
||||||
|
__ http://zigzag.cs.msu.su/boost.build/wiki/AlternativeSelection
|
||||||
|
|
||||||
|
Choosing a Boost.Python Library Binary
|
||||||
|
======================================
|
||||||
|
|
||||||
|
If—instead of letting Boost.Build construct and link withthe right
|
||||||
|
libraries automatically—you choose to use a pre-built Boost.Python
|
||||||
|
library, you'll need to think about which one to link with. The
|
||||||
|
Boost.Python binary comes in both static and dynamic flavors. Take
|
||||||
|
care to choose the right flavor for your application. [#naming]_
|
||||||
|
|
||||||
|
The Dynamic Binary
|
||||||
|
------------------
|
||||||
|
|
||||||
|
The dynamic library is the safest and most-versatile choice:
|
||||||
|
|
||||||
|
- A single copy of the library code is used by all extension
|
||||||
|
modules built with a given toolset. [#toolset-specific]_
|
||||||
|
|
||||||
|
- The library contains a type conversion registry. Because one
|
||||||
|
registry is shared among all extension modules, instances of a
|
||||||
|
class exposed to Python in one dynamically-loaded extension
|
||||||
|
module can be passed to functions exposed in another such module.
|
||||||
|
|
||||||
|
The Static Binary
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
It might be appropriate to use the static Boost.Python library in
|
||||||
|
any of the following cases:
|
||||||
|
|
||||||
|
- You are extending_ python and the types exposed in your
|
||||||
|
dynamically-loaded extension module don't need to be used by any
|
||||||
|
other Boost.Python extension modules, and you don't care if the
|
||||||
|
core library code is duplicated among them.
|
||||||
|
|
||||||
|
- You are embedding_ python in your application and either:
|
||||||
|
|
||||||
|
- You are targeting a Unix variant OS other than MacOS or AIX,
|
||||||
|
where the dynamically-loaded extension modules can “see” the
|
||||||
|
Boost.Python library symbols that are part of the executable.
|
||||||
|
|
||||||
|
- Or, you have statically linked some Boost.Python extension
|
||||||
|
modules into your application and you don't care if any
|
||||||
|
dynamically-loaded Boost.Python extension modules are able to
|
||||||
|
use the types exposed by your statically-linked extension
|
||||||
|
modules (and vice-versa).
|
||||||
|
|
||||||
|
Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users
|
||||||
|
=======================================================
|
||||||
|
|
||||||
|
If you are using a version of Python prior to 2.4.1 with a MinGW
|
||||||
|
prior to 3.0.0 (with binutils-2.13.90-20030111-1), you will need to
|
||||||
|
create a MinGW-compatible version of the Python library; the one
|
||||||
|
shipped with Python will only work with a Microsoft-compatible
|
||||||
|
linker. Follow the instructions in the “Non-Microsoft” section of
|
||||||
|
the “Building Extensions: Tips And Tricks” chapter in `Installing
|
||||||
|
Python Modules`__ to create ``libpythonXX.a``, where ``XX``
|
||||||
|
corresponds to the major and minor version numbers of your Python
|
||||||
|
installation.
|
||||||
|
|
||||||
|
__ http://www.python.org/doc/current/inst/index.html
|
||||||
|
|
||||||
|
-----------------------------
|
||||||
|
|
||||||
|
.. [#2.2] Note that although we tested earlier versions of
|
||||||
|
Boost.Python with Python 2.2, and we don't *think* we've done
|
||||||
|
anything to break compatibility, this release of Boost.Python
|
||||||
|
may not have been tested with versions of Python earlier than
|
||||||
|
2.4, so we're not 100% sure that python 2.2 and 2.3 are
|
||||||
|
supported.
|
||||||
|
|
||||||
|
.. [#naming] Information about how to identify the
|
||||||
|
static and dynamic builds of Boost.Python:
|
||||||
|
|
||||||
|
* `on Windows`__
|
||||||
|
* `on Unix variants`__
|
||||||
|
|
||||||
|
__ ../../../more/getting_started/windows.html#library-naming
|
||||||
|
__ ../../../more/getting_started/unix-variants.html#library-naming
|
||||||
|
|
||||||
|
Be sure to read this section even if your compiler supports
|
||||||
|
auto-linking, as Boost.Python does not yet take advantage of
|
||||||
|
that feature.
|
||||||
|
|
||||||
|
.. [#toolset-specific] Because of the way most \*nix platforms
|
||||||
|
share symbols among dynamically-loaded objects, I'm not
|
||||||
|
certainextension modules built with different compiler toolsets
|
||||||
|
will always use different copies of the Boost.Python library
|
||||||
|
when loaded into the same Python instance. Not using different
|
||||||
|
libraries could be a good thing if the compilers have compatible
|
||||||
|
ABIs, because extension modules built with the two libraries
|
||||||
|
would be interoperable. Otherwise, it could spell disaster,
|
||||||
|
since an extension module and the Boost.Python library would
|
||||||
|
have different ideas of such things as class layout. I would
|
||||||
|
appreciate someone doing the experiment to find out what
|
||||||
|
happens.
|
||||||
|
|
||||||
|
.. [#overwrite] ``configure`` overwrites the existing
|
||||||
|
``user-config.jam`` in your home directory
|
||||||
|
(if any) after making a backup of the old version.
|
||||||
|
|
||||||
|
.. [#flavor] Note that the ``<target-os>cygwin`` feature is
|
||||||
|
different from the ``<flavor>cygwin`` subfeature of the ``gcc``
|
||||||
|
toolset, and you might need handle both explicitly if you also
|
||||||
|
have a MinGW GCC installed.
|
||||||
|
|
||||||
|
.. [#user-config.jam] Create the ``user-config.jam`` file if you don't
|
||||||
|
already have one.
|
||||||
|
|
||||||
|
.. [#home-dir] Windows users, your home directory can be
|
||||||
|
found by typing::
|
||||||
|
|
||||||
|
ECHO %HOMEDRIVE%%HOMEPATH%
|
||||||
|
|
||||||
|
into a `Windows command prompt`__
|
||||||
|
|
||||||
|
__ ../../../more/getting_started/windows.html#or-build-from-the-command-prompt
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
181
doc/news.html
181
doc/news.html
@@ -1,16 +1,19 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
"HTML Tidy for Cygwin (vers 1st April 2002), see www.w3.org">
|
"HTML Tidy for Cygwin (vers 1st September 2004), see www.w3.org">
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
|
||||||
<link rel="stylesheet" type="text/css" href="boost.css">
|
<link rel="stylesheet" type="text/css" href="boost.css">
|
||||||
|
|
||||||
<title>Boost.Python - News/Change Log</title>
|
<title>Boost.Python - News/Change Log</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body link="#0000ff" vlink="#800080">
|
<body link="#0000FF" vlink="#800080">
|
||||||
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
||||||
"header">
|
"header">
|
||||||
<tr>
|
<tr>
|
||||||
@@ -29,32 +32,117 @@
|
|||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<dl class="page-index">
|
<dl class="page-index">
|
||||||
|
<dt>Current CVS</dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<ul>
|
||||||
|
<li>C++ signatures are now automatically appended to the
|
||||||
|
docstrings.
|
||||||
|
|
||||||
|
<li>New <a href="v2/docstring_options.html"
|
||||||
|
><code>docstring_options.hpp</code></a> header to
|
||||||
|
control the content of docstrings.
|
||||||
|
|
||||||
|
<li>Support for converting <code>void*</code> to/from python,
|
||||||
|
with <code><a
|
||||||
|
href="v2/opaque.html">opaque_pointer_converter</a></code>
|
||||||
|
as the return value policy. Thanks to Niall Douglas for the
|
||||||
|
initial patch.
|
||||||
|
</ul>
|
||||||
|
</dd>
|
||||||
|
|
||||||
|
<dt>19 October 2005 - 1.33.1 release</dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<ul>
|
||||||
|
<li><code>wrapper<T></code> can now be used as expected with a
|
||||||
|
held type of <i>some-smart-pointer</i><code><T></code></li>
|
||||||
|
|
||||||
|
<li>The build now assumes Python 2.4 by default, rather than 2.2</li>
|
||||||
|
|
||||||
|
<li>Support Python that's built without Unicode support</li>
|
||||||
|
|
||||||
|
<li>Support for wrapping classes with overloaded address-of
|
||||||
|
(<code>&</code>) operators</li>
|
||||||
|
</ul>
|
||||||
|
</dd>
|
||||||
|
|
||||||
|
<dt>14 August 2005 - 1.33 release</dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<ul>
|
||||||
|
<li>Support for docstrings on nonstatic properties.</li>
|
||||||
|
|
||||||
|
<li>We now export the client-provided docstrings for
|
||||||
|
<code>init<optional<> ></code> and
|
||||||
|
<i>XXX</i><code>_FUNCTION_OVERLOADS()</code> for only the last
|
||||||
|
overload.</li>
|
||||||
|
|
||||||
|
<li>Fixed some support for Embedded VC++ 4</li>
|
||||||
|
|
||||||
|
<li>Better support for rvalue from-python conversions of shared_ptr:
|
||||||
|
always return a pointer that holds the owning python object *unless*
|
||||||
|
the python object contains a NULL shared_ptr holder of the right
|
||||||
|
type.</li>
|
||||||
|
|
||||||
|
<li>Support for exposing <code>vector<T*></code> with the
|
||||||
|
indexing suite.</li>
|
||||||
|
|
||||||
|
<li>Support for GCC-3.3 on MacOS.</li>
|
||||||
|
|
||||||
|
<li>updated visual studio project build file to include two new files
|
||||||
|
(slice.cpp and wrapper.cpp)</li>
|
||||||
|
|
||||||
|
<li>Added search feature to the index page.</li>
|
||||||
|
|
||||||
|
<li>Numerous fixes to the tutorial</li>
|
||||||
|
|
||||||
|
<li>Numerous workarounds for MSVC 6 and 7, GCC 2.96, and EDG
|
||||||
|
2.45</li>
|
||||||
|
</ul>
|
||||||
|
</dd>
|
||||||
|
|
||||||
<dt>11 March 2005</dt>
|
<dt>11 March 2005</dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<ul>
|
<ul>
|
||||||
<li>Added a hack that will fool PyDoc into working with Boost.Python, thanks to Nick Rasmussen</li>
|
<li>Added a hack that will fool PyDoc into working with Boost.Python,
|
||||||
|
thanks to Nick Rasmussen</li>
|
||||||
</ul>
|
</ul>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt>19 November 2004 - 1.32 release</dt>
|
<dt>19 November 2004 - 1.32 release</dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<ul>
|
<ul>
|
||||||
<li>Updated to use the Boost Software License.</li>
|
<li>Updated to use the Boost Software License.</li>
|
||||||
<li>A new, <a href="tutorial/doc/html/python/exposing.html#python.class_virtual_functions">better method of wrapping classes with virtual functions</a> has been implemented.</li>
|
|
||||||
<li>Support for upcoming GCC symbol export control features have been folded in, thanks to Niall Douglas.</li>
|
<li>A new, <a href=
|
||||||
|
"tutorial/doc/html/python/exposing.html#python.class_virtual_functions">
|
||||||
|
better method of wrapping classes with virtual functions</a> has been
|
||||||
|
implemented.</li>
|
||||||
|
|
||||||
|
<li>Support for upcoming GCC symbol export control features have been
|
||||||
|
folded in, thanks to Niall Douglas.</li>
|
||||||
|
|
||||||
<li>Improved support for <code>std::auto_ptr</code>-like types.</li>
|
<li>Improved support for <code>std::auto_ptr</code>-like types.</li>
|
||||||
<li>The Visual C++ bug that makes top-level <i>cv-qualification</i> of function parameter types part of the function type has been worked around.</li>
|
|
||||||
<li>Components used by other libraries have been moved out of <code>python/detail</code> and into <code> boost/detail</code> to improve dependency relationships.</li>
|
<li>The Visual C++ bug that makes top-level <i>cv-qualification</i>
|
||||||
|
of function parameter types part of the function type has been worked
|
||||||
|
around.</li>
|
||||||
|
|
||||||
|
<li>Components used by other libraries have been moved out of
|
||||||
|
<code>python/detail</code> and into <code>boost/detail</code> to
|
||||||
|
improve dependency relationships.</li>
|
||||||
|
|
||||||
<li>Miscellaneous bug fixes and compiler workarounds.</li>
|
<li>Miscellaneous bug fixes and compiler workarounds.</li>
|
||||||
</ul>
|
</ul>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt>8 Sept 2004</dt>
|
<dt>8 Sept 2004</dt>
|
||||||
|
|
||||||
<dd>
|
<dd>Support for Python's Bool type, thanks to <a href=
|
||||||
Support for Python's Bool type, thanks to <a
|
"mailto:dholth-at-fastmail.fm">Daniel Holth</a>.</dd>
|
||||||
mailto="dholth-at-fastmail.fm">Daniel Holth</a>.
|
|
||||||
</dd>
|
|
||||||
|
|
||||||
<dt>11 Sept 2003</dt>
|
<dt>11 Sept 2003</dt>
|
||||||
|
|
||||||
@@ -88,18 +176,18 @@
|
|||||||
|
|
||||||
<dt>10 August 2003</dt>
|
<dt>10 August 2003</dt>
|
||||||
|
|
||||||
<dd>Added the new <code>properties</code> unit tests contributed by <a
|
<dd>Added the new <code>properties</code> unit tests contributed by
|
||||||
href="mailto:romany-at-actimize.com">Roman Yakovenko</a> and documented
|
<a href="mailto:romany-at-actimize.com">Roman Yakovenko</a> and
|
||||||
<code>add_static_property</code> at his urging.</dd>
|
documented <code>add_static_property</code> at his urging.</dd>
|
||||||
|
|
||||||
<dt>1 August 2003</dt>
|
<dt>1 August 2003</dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
Added the new <code>arg</code> class contributed by <a href=
|
Added the new <code>arg</code> class contributed by <a href=
|
||||||
"mailto:nickm-at-sitius.com">Nikolay Mladenov</a> which supplies the
|
"mailto:nickm-at-sitius.com">Nikolay Mladenov</a> which supplies the
|
||||||
ability to wrap functions that can be called with ommitted arguments
|
ability to wrap functions that can be called with ommitted arguments in
|
||||||
in the middle:
|
the middle:
|
||||||
<pre>
|
<pre>
|
||||||
void f(int x = 0, double y = 3.14, std::string z = std::string("foo"));
|
void f(int x = 0, double y = 3.14, std::string z = std::string("foo"));
|
||||||
|
|
||||||
BOOST_PYTHON_MODULE(test)
|
BOOST_PYTHON_MODULE(test)
|
||||||
@@ -108,14 +196,12 @@ BOOST_PYTHON_MODULE(test)
|
|||||||
, (arg("x", 0), arg("y", 3.14), arg("z", "foo")));
|
, (arg("x", 0), arg("y", 3.14), arg("z", "foo")));
|
||||||
}
|
}
|
||||||
|
|
||||||
</pre>
|
</pre>And in Python:
|
||||||
And in Python:
|
<pre>
|
||||||
<pre>
|
|
||||||
>>> import test
|
>>> import test
|
||||||
>>> f(0, z = "bar")
|
>>> f(0, z = "bar")
|
||||||
>>> f(z = "bar", y = 0.0)
|
>>> f(z = "bar", y = 0.0)
|
||||||
</pre>
|
</pre>Thanks, Nikolay!
|
||||||
Thanks, Nikolay!
|
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt>22 July 2003</dt>
|
<dt>22 July 2003</dt>
|
||||||
@@ -127,39 +213,36 @@ BOOST_PYTHON_MODULE(test)
|
|||||||
|
|
||||||
<dd>Added the new <code><a href=
|
<dd>Added the new <code><a href=
|
||||||
"v2/return_arg.html">return_arg</a></code> policy from <a href=
|
"v2/return_arg.html">return_arg</a></code> policy from <a href=
|
||||||
"mailto:nickm-at-sitius.com">Nikolay Mladenov</a>. Thanks,
|
"mailto:nickm-at-sitius.com">Nikolay Mladenov</a>. Thanks, Nikolay!</dd>
|
||||||
Nikolay!</dd>
|
|
||||||
|
|
||||||
<dt>18 March, 2003</dt>
|
<dt>18 March, 2003</dt>
|
||||||
|
|
||||||
<dd><a href="mailto:Gottfried.Ganssauge-at-haufe.de">Gottfried
|
<dd><a href="mailto:Gottfried.Ganssauge-at-haufe.de">Gottfried
|
||||||
Ganßauge</a> has contributed <a href=
|
Ganßauge</a> has contributed <a href=
|
||||||
"v2/opaque_pointer_converter.html">opaque pointer support</a>.<br>
|
"v2/opaque.html">opaque pointer support</a>.<br>
|
||||||
<a href="mailto:nicodemus-at-globalite.com.br">Bruno da Silva de Oliveira</a>
|
<a href="mailto:nicodemus-at-globalite.com.br">Bruno da Silva de
|
||||||
has contributed the exciting <a href="../pyste/index.html">Pyste</a>
|
Oliveira</a> has contributed the exciting <a href=
|
||||||
("Pie-steh") package.</dd>
|
"../pyste/index.html">Pyste</a> ("Pie-steh") package.</dd>
|
||||||
|
|
||||||
<dt>24 February 2003</dt>
|
<dt>24 February 2003</dt>
|
||||||
|
|
||||||
<dd>Finished improved support for <code>boost::shared_ptr</code>. Now
|
<dd>Finished improved support for <code>boost::shared_ptr</code>. Now any
|
||||||
any wrapped object of C++ class <code>X</code> can be converted
|
wrapped object of C++ class <code>X</code> can be converted automatically
|
||||||
automatically to <code>shared_ptr<X></code>, regardless of how it
|
to <code>shared_ptr<X></code>, regardless of how it was wrapped.
|
||||||
was wrapped. The <code>shared_ptr</code> will manage the lifetime of
|
The <code>shared_ptr</code> will manage the lifetime of the Python object
|
||||||
the Python object which supplied the <code>X</code>, rather than just
|
which supplied the <code>X</code>, rather than just the <code>X</code>
|
||||||
the <code>X</code> object itself, and when such a
|
object itself, and when such a <code>shared_ptr</code> is converted back
|
||||||
<code>shared_ptr</code> is converted back to Python, the original
|
to Python, the original Python object will be returned.</dd>
|
||||||
Python object will be returned.</dd>
|
|
||||||
|
|
||||||
<dt>19 January 2003</dt>
|
<dt>19 January 2003</dt>
|
||||||
|
|
||||||
<dd>Integrated <code>staticmethod</code> support from <a href=
|
<dd>Integrated <code>staticmethod</code> support from <a href=
|
||||||
"mailto:nickm-at-sitius.com">Nikolay Mladenov</a>. Thanks,
|
"mailto:nickm-at-sitius.com">Nikolay Mladenov</a>. Thanks, Nikolay!</dd>
|
||||||
Nikolay!</dd>
|
|
||||||
|
|
||||||
<dt>29 December 2002</dt>
|
<dt>29 December 2002</dt>
|
||||||
|
|
||||||
<dd>Added Visual Studio project file and instructions from Brett
|
<dd>Added Visual Studio project file and instructions from Brett Calcott.
|
||||||
Calcott. Thanks, Brett!</dd>
|
Thanks, Brett!</dd>
|
||||||
|
|
||||||
<dt>20 December 2002</dt>
|
<dt>20 December 2002</dt>
|
||||||
|
|
||||||
@@ -168,9 +251,9 @@ BOOST_PYTHON_MODULE(test)
|
|||||||
|
|
||||||
<dt>18 December 2002</dt>
|
<dt>18 December 2002</dt>
|
||||||
|
|
||||||
<dd>Optimized from_python conversions for wrapped classes by putting
|
<dd>Optimized from_python conversions for wrapped classes by putting the
|
||||||
the conversion logic in the shared library instead of registering
|
conversion logic in the shared library instead of registering separate
|
||||||
separate converters for each class in each extension module</dd>
|
converters for each class in each extension module</dd>
|
||||||
|
|
||||||
<dt>19 November 2002</dt>
|
<dt>19 November 2002</dt>
|
||||||
|
|
||||||
@@ -208,11 +291,9 @@ BOOST_PYTHON_MODULE(test)
|
|||||||
<p>Revised
|
<p>Revised
|
||||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
||||||
19 November 2004
|
19 November 2004
|
||||||
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||||
</p>
|
|
||||||
|
|
||||||
<p><i>© Copyright <a href="../../../people/dave_abrahams.htm">Dave
|
<p><i>© Copyright <a href="../../../people/dave_abrahams.htm">Dave
|
||||||
Abrahams</a> 2002-2003.</i></p>
|
Abrahams</a> 2002-2003.</i></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
.. 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)
|
||||||
|
|
||||||
How Runtime Polymorphism is expressed in Boost.Python:
|
How Runtime Polymorphism is expressed in Boost.Python:
|
||||||
-----------------------------------------------------
|
-----------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
"HTML Tidy for Cygwin (vers 1st April 2002), see www.w3.org">
|
"HTML Tidy for Cygwin (vers 1st September 2004), see www.w3.org">
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset="utf-8">
|
<meta http-equiv="Content-Type" content="text/html">
|
||||||
<link rel="stylesheet" type="text/css" href="boost.css">
|
<link rel="stylesheet" type="text/css" href="boost.css">
|
||||||
|
|
||||||
<title>Boost.Python - Projects using Boost.Python</title>
|
<title>Boost.Python - Projects using Boost.Python</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body link="#0000ff" vlink="#800080">
|
<body link="#0000FF" vlink="#800080">
|
||||||
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
||||||
"header">
|
"header">
|
||||||
<tr>
|
<tr>
|
||||||
@@ -30,9 +33,9 @@
|
|||||||
|
|
||||||
<h2>Introduction</h2>
|
<h2>Introduction</h2>
|
||||||
|
|
||||||
<p>This is a partial list of projects using Boost.Python. If you are
|
<p>This is a partial list of projects using Boost.Python. If you are using
|
||||||
using Boost.Python as your Python/C++ binding solution, we'd be proud to
|
Boost.Python as your Python/C++ binding solution, we'd be proud to list
|
||||||
list your project on this page. Just <a href=
|
your project on this page. Just <a href=
|
||||||
"mailto:c++-sig@python.org">post</a> a short description of your project
|
"mailto:c++-sig@python.org">post</a> a short description of your project
|
||||||
and how Boost.Python helps you get the job done, and we'll add it to this
|
and how Boost.Python helps you get the job done, and we'll add it to this
|
||||||
page .</p>
|
page .</p>
|
||||||
@@ -41,8 +44,7 @@
|
|||||||
<h3>Data Analysis</h3>
|
<h3>Data Analysis</h3>
|
||||||
|
|
||||||
<dl class="page-index">
|
<dl class="page-index">
|
||||||
<dt><b><a href=
|
<dt><b><a href="http://www.neuralynx.com">NeuraLab</a></b></dt>
|
||||||
"http://www.neuralynx.com/neuralab/index.htm">NeuraLab</a></b></dt>
|
|
||||||
|
|
||||||
<dd>Neuralab is a data analysis environment specifically tailored for
|
<dd>Neuralab is a data analysis environment specifically tailored for
|
||||||
neural data from <a href="http://www.neuralynx.com">Neuralynx</a>
|
neural data from <a href="http://www.neuralynx.com">Neuralynx</a>
|
||||||
@@ -50,9 +52,9 @@
|
|||||||
numerical analysis library, and the <a href=
|
numerical analysis library, and the <a href=
|
||||||
"http://www.python.org">Python</a> scripting engine in a single
|
"http://www.python.org">Python</a> scripting engine in a single
|
||||||
application. With Neuralab, Neuralynx users can perform common analysis
|
application. With Neuralab, Neuralynx users can perform common analysis
|
||||||
tasks with just a few mouse clicks. More advanced users can create
|
tasks with just a few mouse clicks. More advanced users can create custom
|
||||||
custom Python scripts, which can optionally be assigned to menus and
|
Python scripts, which can optionally be assigned to menus and mouse
|
||||||
mouse clicks.</dd>
|
clicks.</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
<dl class="page-index">
|
<dl class="page-index">
|
||||||
@@ -61,23 +63,23 @@
|
|||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
Fortress Investment Group has contracted <a href=
|
Fortress Investment Group has contracted <a href=
|
||||||
"http://www.boost-consulting.com">Boost Consulting</a> to develop
|
"http://www.boost-consulting.com">Boost Consulting</a> to develop core
|
||||||
core internal financial analysis tools in C++ and to prepare Python
|
internal financial analysis tools in C++ and to prepare Python bindings
|
||||||
bindings for them using Boost.Python.
|
for them using Boost.Python.
|
||||||
|
|
||||||
<p>Tom Barket of Fortress writes:</p>
|
<p>Tom Barket of Fortress writes:</p>
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
We have a large C++ analytical library specialized for research in
|
We have a large C++ analytical library specialized for research in
|
||||||
finance and economics, built for speed and mission critical
|
finance and economics, built for speed and mission critical
|
||||||
stability. Yet Python offers us the flexibility to test out new
|
stability. Yet Python offers us the flexibility to test out new ideas
|
||||||
ideas quickly and increase the productivity of our time versus
|
quickly and increase the productivity of our time versus working in
|
||||||
working in C++. There are several key features which make Python
|
C++. There are several key features which make Python stand out. Its
|
||||||
stand out. Its elegance, stability, and breadth of resources on the
|
elegance, stability, and breadth of resources on the web are all
|
||||||
web are all valuable, but the most important is its extensibility,
|
valuable, but the most important is its extensibility, due to its
|
||||||
due to its open source transparency. Boost.Python makes Python
|
open source transparency. Boost.Python makes Python extensibility
|
||||||
extensibility extremely simple and straightforward, yet preserves a
|
extremely simple and straightforward, yet preserves a great deal of
|
||||||
great deal of power and control.
|
power and control.
|
||||||
</blockquote>
|
</blockquote>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
@@ -89,14 +91,14 @@
|
|||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<p>KDE Interactive Geometry is a high-school level educational tool,
|
<p>KDE Interactive Geometry is a high-school level educational tool,
|
||||||
built for the KDE desktop. It is a nice tool to let students work
|
built for the KDE desktop. It is a nice tool to let students work with
|
||||||
with geometrical constructions. It is meant to be the most intuitive,
|
geometrical constructions. It is meant to be the most intuitive, yet
|
||||||
yet featureful application of its kind.</p>
|
featureful application of its kind.</p>
|
||||||
|
|
||||||
<p>Versions after 0.6.x (will) support objects built by the user
|
<p>Versions after 0.6.x (will) support objects built by the user
|
||||||
himself in the Python language. The exporting of the relevant
|
himself in the Python language. The exporting of the relevant internal
|
||||||
internal API's were done using Boost.Python, which made the process
|
API's were done using Boost.Python, which made the process very
|
||||||
very easy.</p>
|
easy.</p>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
@@ -132,8 +134,8 @@
|
|||||||
<blockquote>
|
<blockquote>
|
||||||
Boost.Python is used in an automated process to generate python
|
Boost.Python is used in an automated process to generate python
|
||||||
bindings to our api which is exposed though multiple backends and
|
bindings to our api which is exposed though multiple backends and
|
||||||
frontends. This allows us to write quick tests and bespoke scripts
|
frontends. This allows us to write quick tests and bespoke scripts to
|
||||||
to perform one off tasks without having to go through the full
|
perform one off tasks without having to go through the full
|
||||||
compilation cycle.
|
compilation cycle.
|
||||||
</blockquote>
|
</blockquote>
|
||||||
</dd>
|
</dd>
|
||||||
@@ -146,17 +148,17 @@
|
|||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
“The fourth game in the PC strategy series that has
|
“The fourth game in the PC strategy series that has sold over five
|
||||||
sold over five million copies, Sid Meier's Civilization IV is a bold
|
million copies, Sid Meier's Civilization IV is a bold step forward for
|
||||||
step forward for the franchise, with spectacular new 3D graphics and
|
the franchise, with spectacular new 3D graphics and all-new single and
|
||||||
all-new single and multiplayer content. Civilization IV will also set a
|
multiplayer content. Civilization IV will also set a new standard for
|
||||||
new standard for user-modification, allowing gamers to create their own
|
user-modification, allowing gamers to create their own add-ons using
|
||||||
add-ons using Python and XML.
|
Python and XML.
|
||||||
|
|
||||||
<p>Sid Meier's Civilization IV will be released for PC in late 2005.
|
<p>Sid Meier's Civilization IV will be released for PC in late 2005. For
|
||||||
For more information please visit <a href=
|
more information please visit <a href=
|
||||||
"http://www.firaxis.com">http://www.firaxis.com</a> or write <a href=
|
"http://www.firaxis.com">http://www.firaxis.com</a> or write <a href=
|
||||||
"mailto:kgilmore@firaxis.com">kgilmore@firaxis.com</a>”</p>
|
"mailto:kgilmore@firaxis.com">kgilmore@firaxis.com</a>”</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
|
|
||||||
<p>Boost.Python is used as the interface layer between the C++ game code
|
<p>Boost.Python is used as the interface layer between the C++ game code
|
||||||
@@ -184,11 +186,11 @@
|
|||||||
The result is a very flexible scripting system that treats units as
|
The result is a very flexible scripting system that treats units as
|
||||||
native python classes when designing missions or writing AI's.</p>
|
native python classes when designing missions or writing AI's.</p>
|
||||||
|
|
||||||
<p>A large economic and planetary simulation is currently being run
|
<p>A large economic and planetary simulation is currently being run in
|
||||||
in the background in python and the results are returned back into
|
the background in python and the results are returned back into C++ in
|
||||||
C++ in the form of various factions' spaceships appearing near worlds
|
the form of various factions' spaceships appearing near worlds that
|
||||||
that they are simulated to be near in python if the player is in the
|
they are simulated to be near in python if the player is in the general
|
||||||
general neighborhood.</p>
|
neighborhood.</p>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
@@ -198,8 +200,8 @@
|
|||||||
<dt><b><a href="http://sourceforge.net/projects/pyosg">OpenSceneGraph
|
<dt><b><a href="http://sourceforge.net/projects/pyosg">OpenSceneGraph
|
||||||
Bindings</a></b></dt>
|
Bindings</a></b></dt>
|
||||||
|
|
||||||
<dd><a href="mailto:gideon@computer.org">Gideon May</a> has created a
|
<dd><a href="mailto:gideon@computer.org">Gideon May</a> has created a set
|
||||||
set of bindings for <a href=
|
of bindings for <a href=
|
||||||
"http://www.openscenegraph.org">OpenSceneGraph</a>, a cross-platform
|
"http://www.openscenegraph.org">OpenSceneGraph</a>, a cross-platform
|
||||||
C++/OpenGL library for the real-time visualization.<br>
|
C++/OpenGL library for the real-time visualization.<br>
|
||||||
</dd>
|
</dd>
|
||||||
@@ -209,22 +211,20 @@
|
|||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
HippoDraw is a data analysis environment consisting of a canvas upon
|
HippoDraw is a data analysis environment consisting of a canvas upon
|
||||||
which graphs such as histograms, scattter plots, etc, are prsented.
|
which graphs such as histograms, scattter plots, etc, are prsented. It
|
||||||
It has a highly interactive GUI interface, but some things you need
|
has a highly interactive GUI interface, but some things you need to do
|
||||||
to do with scripts. HippoDraw can be run as Python extension module
|
with scripts. HippoDraw can be run as Python extension module so that
|
||||||
so that all the manipulation can be done from either Python or the
|
all the manipulation can be done from either Python or the GUI.
|
||||||
GUI.
|
|
||||||
|
|
||||||
<p>Before the web page came online, <a href=
|
<p>Before the web page came online, <a href=
|
||||||
"mailto:Paul_Kunz@SLAC.Stanford.EDU">Paul F. Kunz</a> wrote:</p>
|
"mailto:Paul_Kunz@SLAC.Stanford.EDU">Paul F. Kunz</a> wrote:</p>
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
Don't have a web page for the project, but the organization's is <a
|
Don't have a web page for the project, but the organization's is
|
||||||
href=
|
<a href=
|
||||||
"http://www.slac.stanford.edu">http://www.slac.stanford.edu</a>
|
"http://www.slac.stanford.edu">http://www.slac.stanford.edu</a> (the
|
||||||
(the first web server site in America, I installed it).
|
first web server site in America, I installed it).
|
||||||
</blockquote>
|
</blockquote>Which was just too cool a piece of trivia to omit.<br>
|
||||||
Which was just too cool a piece of trivia to omit.<br>
|
|
||||||
|
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
@@ -238,14 +238,12 @@
|
|||||||
IPLT is an image processing library and toolbox for the structural
|
IPLT is an image processing library and toolbox for the structural
|
||||||
biology electron microscopy community. I would call it a
|
biology electron microscopy community. I would call it a
|
||||||
budding/evolving project, since it is currently not in production
|
budding/evolving project, since it is currently not in production
|
||||||
stage, but rather under heavy development. Python is used as the
|
stage, but rather under heavy development. Python is used as the main
|
||||||
main scripting/interaction level, but also for rapid prototyping,
|
scripting/interaction level, but also for rapid prototyping, since
|
||||||
since the underlying C++ class library is pretty much fully exposed
|
the underlying C++ class library is pretty much fully exposed via
|
||||||
via boost.python (at least the high-level interface). The combined
|
boost.python (at least the high-level interface). The combined power
|
||||||
power of C++ and Python for this project turned out to be just
|
of C++ and Python for this project turned out to be just awesome.
|
||||||
awesome.
|
</blockquote><br>
|
||||||
</blockquote>
|
|
||||||
<br>
|
|
||||||
|
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
@@ -256,6 +254,21 @@
|
|||||||
"http://www.graphicsmagick.org">GraphicsMagick</a> image manipulation
|
"http://www.graphicsmagick.org">GraphicsMagick</a> image manipulation
|
||||||
library to Python.<br>
|
library to Python.<br>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
|
<dt><a href="http://www.vpython.org"><b>VPython</b></a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<a href="mailto:Bruce_Sherwood-at-ncsu.edu">Bruce Sherwood</a> writes:
|
||||||
|
|
||||||
|
<blockquote>
|
||||||
|
VPython is an extension for Python that makes it easy to create
|
||||||
|
navigable 3D animations, which are generated as a side effect of
|
||||||
|
computational code. VPython is used in education for various
|
||||||
|
purposes, including teaching physics and programming, but it has also
|
||||||
|
been used by research scientists to visualize systems or data in 3D.
|
||||||
|
</blockquote><br>
|
||||||
|
|
||||||
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
<h3>Scientific Computing</h3>
|
<h3>Scientific Computing</h3>
|
||||||
@@ -283,21 +296,18 @@
|
|||||||
atomic models of crystal structures, given experimental X-ray
|
atomic models of crystal structures, given experimental X-ray
|
||||||
diffraction data. The cctbx is an open-source library of fundamental
|
diffraction data. The cctbx is an open-source library of fundamental
|
||||||
algorithms for crystallographic computations. The core algorithms are
|
algorithms for crystallographic computations. The core algorithms are
|
||||||
implemented in C++ and accessed through higher-level Python
|
implemented in C++ and accessed through higher-level Python interfaces.
|
||||||
interfaces.
|
|
||||||
|
|
||||||
<p>The cctbx grew together with Boost.Python and is designed from the
|
<p>The cctbx grew together with Boost.Python and is designed from the
|
||||||
ground up as a hybrid Python/C++ system. With one minor exception,
|
ground up as a hybrid Python/C++ system. With one minor exception,
|
||||||
run-time polymorphism is completely handled by Python. C++
|
run-time polymorphism is completely handled by Python. C++ compile-time
|
||||||
compile-time polymorphism is used to implement performance critical
|
polymorphism is used to implement performance critical algorithms. The
|
||||||
algorithms. The Python and C++ layers are seamlessly integrated using
|
Python and C++ layers are seamlessly integrated using Boost.Python.</p>
|
||||||
Boost.Python.</p>
|
|
||||||
|
|
||||||
<p>The SourceForge cctbx project is organized in modules to
|
<p>The SourceForge cctbx project is organized in modules to facilitate
|
||||||
facilitate use in non-crystallographic applications. The scitbx
|
use in non-crystallographic applications. The scitbx module implements
|
||||||
module implements a general purpose array family for scientific
|
a general purpose array family for scientific applications and pure C++
|
||||||
applications and pure C++ ports of FFTPACK and the LBFGS conjugate
|
ports of FFTPACK and the L-BFGS quasi-Newton minimizer.</p>
|
||||||
gradient minimizer.</p>
|
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><a href="http://www.llnl.gov/CASC/emsolve"><b>EMSolve</b></a></dt>
|
<dt><a href="http://www.llnl.gov/CASC/emsolve"><b>EMSolve</b></a></dt>
|
||||||
@@ -311,32 +321,32 @@
|
|||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
Gaudi is a framework for particle physics collision data processing
|
Gaudi is a framework for particle physics collision data processing
|
||||||
applications developed in the context of the LHCb and ATLAS
|
applications developed in the context of the LHCb and ATLAS experiments
|
||||||
experiments at CERN.
|
at CERN.
|
||||||
|
|
||||||
<p><a href="mailto:Pere.Mato@cern.ch">Pere Mato Vila</a> writes:</p>
|
<p><a href="mailto:Pere.Mato@cern.ch">Pere Mato Vila</a> writes:</p>
|
||||||
|
|
||||||
<blockquote>
|
<blockquote>
|
||||||
We are using Boost.Python to provide scripting/interactive
|
We are using Boost.Python to provide scripting/interactive capability
|
||||||
capability to our framework. We have a module called "GaudiPython"
|
to our framework. We have a module called "GaudiPython" implemented
|
||||||
implemented using Boost.Python that allows the interaction with any
|
using Boost.Python that allows the interaction with any framework
|
||||||
framework service or algorithm from python. RootPython also uses
|
service or algorithm from python. RootPython also uses Boost.Python
|
||||||
Boost.Python to provide a generic "gateway" between the <a href=
|
to provide a generic "gateway" between the <a href=
|
||||||
"http://root.cern.ch">ROOT</a> framework and python
|
"http://root.cern.ch">ROOT</a> framework and python
|
||||||
|
|
||||||
<p>Boost.Python is great. We managed very quickly to interface our
|
<p>Boost.Python is great. We managed very quickly to interface our
|
||||||
framework to python, which is great language. We are trying to
|
framework to python, which is great language. We are trying to
|
||||||
facilitate to our physicists (end-users) a rapid analysis
|
facilitate to our physicists (end-users) a rapid analysis application
|
||||||
application development environment based on python. For that,
|
development environment based on python. For that, Boost.Python plays
|
||||||
Boost.Python plays and essential role.</p>
|
and essential role.</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><b><a href="http://www.esss.com.br">ESSS</a></b></dt>
|
<dt><b><a href="http://www.esss.com.br">ESSS</a></b></dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
ESSS (Engineering Simulation and Scientific Software) is a company
|
ESSS (Engineering Simulation and Scientific Software) is a company that
|
||||||
that provides engineering solutions and acts in the brazilian and
|
provides engineering solutions and acts in the brazilian and
|
||||||
south-american market providing products and services related to
|
south-american market providing products and services related to
|
||||||
Computational Fluid Dynamics and Image Analysis.
|
Computational Fluid Dynamics and Image Analysis.
|
||||||
|
|
||||||
@@ -351,17 +361,18 @@
|
|||||||
|
|
||||||
<p>Two projects have been developed so far with this technology:</p>
|
<p>Two projects have been developed so far with this technology:</p>
|
||||||
|
|
||||||
<p><b><a href="http://www.esss.com.br/dev_simba.phtml">Simba</a></b>
|
<p><b><a href=
|
||||||
|
"http://www.esss.com.br/index.php?pg=dev_projetos">Simba</a></b>
|
||||||
provides 3D visualization of geological formations gattered from the
|
provides 3D visualization of geological formations gattered from the
|
||||||
simulation of the evolution of oil systems, allowing the user to
|
simulation of the evolution of oil systems, allowing the user to
|
||||||
analyse various aspects of the simulation, like deformation, pressure
|
analyse various aspects of the simulation, like deformation, pressure
|
||||||
and fluids, along the time of the simulation.</p>
|
and fluids, along the time of the simulation.</p>
|
||||||
|
|
||||||
<p><b><a href="http://www.esss.com.br/dev_aero.phtml">Aero</a></b>
|
<p><b><a href=
|
||||||
aims to construct a CFD with brazilian technology, which involves
|
"http://www.esss.com.br/index.php?pg=dev_projetos">Aero</a></b> aims to
|
||||||
various companies and universities. ESSS is responsible for various
|
construct a CFD with brazilian technology, which involves various
|
||||||
of the application modules, including GUI and post-processing of
|
companies and universities. ESSS is responsible for various of the
|
||||||
results.</p>
|
application modules, including GUI and post-processing of results.</p>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><b><a href="http://www.rationaldiscovery.com">Rational Discovery
|
<dt><b><a href="http://www.rationaldiscovery.com">Rational Discovery
|
||||||
@@ -372,19 +383,19 @@
|
|||||||
library design and custom software development services to the
|
library design and custom software development services to the
|
||||||
pharmaceutical, biotech and chemical industries. We do a substantial
|
pharmaceutical, biotech and chemical industries. We do a substantial
|
||||||
amount of internal research to develop new approaches for applying
|
amount of internal research to develop new approaches for applying
|
||||||
machine-learning techniques to solve chemical problems. Because we're
|
machine-learning techniques to solve chemical problems. Because we're a
|
||||||
a small organization and chemistry is a large and complex field, it
|
small organization and chemistry is a large and complex field, it is
|
||||||
is essential that we be able to quickly and easily prototype and test
|
essential that we be able to quickly and easily prototype and test new
|
||||||
new algorithms.
|
algorithms.
|
||||||
|
|
||||||
<p>For our internal software, we implement core data structures in C
|
<p>For our internal software, we implement core data structures in C
|
||||||
and expose them to Python using Boost.Python. Algorithm development
|
and expose them to Python using Boost.Python. Algorithm development is
|
||||||
is done in Python and then translated to C if required (often it's
|
done in Python and then translated to C if required (often it's not).
|
||||||
not). This hybrid development approach not only greatly increases our
|
This hybrid development approach not only greatly increases our
|
||||||
productivity, but it also allows "non-developers" (people without C
|
productivity, but it also allows "non-developers" (people without C
|
||||||
experience) to take part in method development. Learning C is a
|
experience) to take part in method development. Learning C is a
|
||||||
daunting task, but "Python fits your brain." (Thanks to Bruce Eckel
|
daunting task, but "Python fits your brain." (Thanks to Bruce Eckel for
|
||||||
for the quote.)</p>
|
the quote.)</p>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
@@ -394,14 +405,14 @@
|
|||||||
<dt><a href="http://itamarst.org/software"><b>Fusion</b></a></dt>
|
<dt><a href="http://itamarst.org/software"><b>Fusion</b></a></dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<p>Fusion is a library that supports implementing protocols in C++
|
<p>Fusion is a library that supports implementing protocols in C++ for
|
||||||
for use with Twisted, allowing control over memory allocation
|
use with Twisted, allowing control over memory allocation strategies,
|
||||||
strategies, fast method calls internally, etc.. Fusion supports TCP,
|
fast method calls internally, etc.. Fusion supports TCP, UDP and
|
||||||
UDP and multicast, and is implemented using the Boost.Python python
|
multicast, and is implemented using the Boost.Python python
|
||||||
bindings.</p>
|
bindings.</p>
|
||||||
|
|
||||||
<p>Fusion is licensed under the MIT license, and available for
|
<p>Fusion is licensed under the MIT license, and available for download
|
||||||
download from <a href=
|
from <a href=
|
||||||
"http://itamarst.org/software">http://itamarst.org/software</a>.</p>
|
"http://itamarst.org/software">http://itamarst.org/software</a>.</p>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
@@ -413,14 +424,13 @@
|
|||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
Jayacard aims at developing a secure portable open source operating
|
Jayacard aims at developing a secure portable open source operating
|
||||||
system for contactless smart cards and a complete suite of high
|
system for contactless smart cards and a complete suite of high quality
|
||||||
quality development tools to ease smart card OS and application
|
development tools to ease smart card OS and application development.
|
||||||
development.
|
|
||||||
|
|
||||||
<p>The core of the smart card reader management is written in C++ but
|
<p>The core of the smart card reader management is written in C++ but
|
||||||
all the development tools are written in the friendly Python
|
all the development tools are written in the friendly Python language.
|
||||||
language. Boost plays the fundamental role of binding the tools to
|
Boost plays the fundamental role of binding the tools to our core smart
|
||||||
our core smart card reader library.</p>
|
card reader library.</p>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
<hr>
|
<hr>
|
||||||
@@ -431,6 +441,5 @@
|
|||||||
|
|
||||||
<p><i>© Copyright <a href="../../../people/dave_abrahams.htm">Dave
|
<p><i>© Copyright <a href="../../../people/dave_abrahams.htm">Dave
|
||||||
Abrahams</a> 2002-2003.</i></p>
|
Abrahams</a> 2002-2003.</i></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
# Copyright Joel de Guzman 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)
|
||||||
project boost/libs/python/doc/tutorial/doc ;
|
project boost/libs/python/doc/tutorial/doc ;
|
||||||
|
|
||||||
import boostbook : boostbook ;
|
import boostbook : boostbook ;
|
||||||
|
|||||||
@@ -23,23 +23,24 @@
|
|||||||
|
|
||||||
p
|
p
|
||||||
{
|
{
|
||||||
text-align: justify;
|
text-align: left;
|
||||||
font-size: 11pt;
|
font-size: 10pt;
|
||||||
line-height: 1.2;
|
line-height: 1.15;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*=============================================================================
|
/*=============================================================================
|
||||||
Program listings
|
Program listings
|
||||||
=============================================================================*/
|
=============================================================================*/
|
||||||
|
|
||||||
tt.computeroutput
|
/* Code on paragraphs */
|
||||||
|
p tt.computeroutput
|
||||||
{
|
{
|
||||||
font-size: 10pt;
|
font-size: 9pt;
|
||||||
}
|
}
|
||||||
|
|
||||||
pre.synopsis
|
pre.synopsis
|
||||||
{
|
{
|
||||||
font-size: 10pt;
|
font-size: 90%;
|
||||||
margin: 1pc 4% 0pc 4%;
|
margin: 1pc 4% 0pc 4%;
|
||||||
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
|
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
|
||||||
}
|
}
|
||||||
@@ -47,32 +48,36 @@
|
|||||||
.programlisting,
|
.programlisting,
|
||||||
.screen
|
.screen
|
||||||
{
|
{
|
||||||
font-size: 10pt;
|
font-size: 9pt;
|
||||||
display: block;
|
display: block;
|
||||||
margin: 1pc 4% 0pc 4%;
|
margin: 1pc 4% 0pc 4%;
|
||||||
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
|
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Program listings in tables don't get borders */
|
||||||
|
td .programlisting,
|
||||||
|
td .screen
|
||||||
|
{
|
||||||
|
margin: 0pc 0pc 0pc 0pc;
|
||||||
|
padding: 0pc 0pc 0pc 0pc;
|
||||||
|
}
|
||||||
|
|
||||||
/*=============================================================================
|
/*=============================================================================
|
||||||
Headings
|
Headings
|
||||||
=============================================================================*/
|
=============================================================================*/
|
||||||
|
|
||||||
h1,
|
h1, h2, h3, h4, h5, h6
|
||||||
h2,
|
|
||||||
h3,
|
|
||||||
h4,
|
|
||||||
h5,
|
|
||||||
h6
|
|
||||||
{
|
{
|
||||||
text-align: left;
|
text-align: left;
|
||||||
margin-top: 2pc;
|
margin: 1em 0em 0.5em 0em;
|
||||||
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
h1 { font: 170% }
|
h1 { font: 140% }
|
||||||
h2 { font: bold 140% }
|
h2 { font: bold 140% }
|
||||||
h3 { font: bold 120% }
|
h3 { font: bold 130% }
|
||||||
h4 { font: bold 100% }
|
h4 { font: bold 120% }
|
||||||
h5 { font: italic 100% }
|
h5 { font: italic 110% }
|
||||||
h6 { font: italic 100% }
|
h6 { font: italic 100% }
|
||||||
|
|
||||||
/* Top page titles */
|
/* Top page titles */
|
||||||
@@ -89,12 +94,41 @@
|
|||||||
margin-bottom: 1pc;
|
margin-bottom: 1pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
h1.title { font-size: 220% }
|
h1.title { font-size: 140% }
|
||||||
h2.title { font-size: 220% }
|
h2.title { font-size: 140% }
|
||||||
h3.title { font-size: 170% }
|
h3.title { font-size: 130% }
|
||||||
h4.title { font-size: 140% }
|
h4.title { font-size: 120% }
|
||||||
h5.title { font-size: 120% }
|
h5.title { font-size: 110% }
|
||||||
h6.title { font-size: 120% }
|
h6.title { font-size: 100% }
|
||||||
|
|
||||||
|
.section h1
|
||||||
|
{
|
||||||
|
margin: 0em 0em 0.5em 0em;
|
||||||
|
font-size: 140%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section h2 { font-size: 140% }
|
||||||
|
.section h3 { font-size: 130% }
|
||||||
|
.section h4 { font-size: 120% }
|
||||||
|
.section h5 { font-size: 110% }
|
||||||
|
.section h6 { font-size: 100% }
|
||||||
|
|
||||||
|
/* Code on titles */
|
||||||
|
h1 tt.computeroutput { font-size: 140% }
|
||||||
|
h2 tt.computeroutput { font-size: 140% }
|
||||||
|
h3 tt.computeroutput { font-size: 130% }
|
||||||
|
h4 tt.computeroutput { font-size: 120% }
|
||||||
|
h5 tt.computeroutput { font-size: 110% }
|
||||||
|
h6 tt.computeroutput { font-size: 100% }
|
||||||
|
|
||||||
|
/*=============================================================================
|
||||||
|
Author
|
||||||
|
=============================================================================*/
|
||||||
|
|
||||||
|
h3.author
|
||||||
|
{
|
||||||
|
font-size: 100%
|
||||||
|
}
|
||||||
|
|
||||||
/*=============================================================================
|
/*=============================================================================
|
||||||
Lists
|
Lists
|
||||||
@@ -102,20 +136,20 @@
|
|||||||
|
|
||||||
li
|
li
|
||||||
{
|
{
|
||||||
font-size: 11pt;
|
font-size: 10pt;
|
||||||
line-height: 1.3;
|
line-height: 1.3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unordered lists */
|
/* Unordered lists */
|
||||||
ul
|
ul
|
||||||
{
|
{
|
||||||
text-align: justify;
|
text-align: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Ordered lists */
|
/* Ordered lists */
|
||||||
ol
|
ol
|
||||||
{
|
{
|
||||||
text-align: justify;
|
text-align: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*=============================================================================
|
/*=============================================================================
|
||||||
@@ -159,9 +193,9 @@
|
|||||||
.toc
|
.toc
|
||||||
{
|
{
|
||||||
margin: 1pc 4% 0pc 4%;
|
margin: 1pc 4% 0pc 4%;
|
||||||
padding: 0.5pc;
|
padding: 0.1pc 1pc 0.1pc 1pc;
|
||||||
font-size: 11pt;
|
font-size: 80%;
|
||||||
line-height: 1.3;
|
line-height: 1.15;
|
||||||
}
|
}
|
||||||
|
|
||||||
.boost-toc
|
.boost-toc
|
||||||
@@ -180,7 +214,6 @@
|
|||||||
margin-left: 4%;
|
margin-left: 4%;
|
||||||
padding-right: 0.5em;
|
padding-right: 0.5em;
|
||||||
padding-left: 0.5em;
|
padding-left: 0.5em;
|
||||||
font-size: 120%;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.informaltable table,
|
.informaltable table,
|
||||||
@@ -202,8 +235,8 @@
|
|||||||
div.table table tr td
|
div.table table tr td
|
||||||
{
|
{
|
||||||
padding: 0.5em;
|
padding: 0.5em;
|
||||||
text-align: justify;
|
text-align: left;
|
||||||
font-size: 11pt;
|
font-size: 9pt;
|
||||||
}
|
}
|
||||||
|
|
||||||
div.informaltable table tr th,
|
div.informaltable table tr th,
|
||||||
@@ -211,51 +244,76 @@
|
|||||||
{
|
{
|
||||||
padding: 0.5em 0.5em 0.5em 0.5em;
|
padding: 0.5em 0.5em 0.5em 0.5em;
|
||||||
border: 1pt solid white;
|
border: 1pt solid white;
|
||||||
|
font-size: 80%;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*=============================================================================
|
/*=============================================================================
|
||||||
Blurbs
|
Blurbs
|
||||||
=============================================================================*/
|
=============================================================================*/
|
||||||
|
|
||||||
div.informaltable table tr td.blurb
|
div.note,
|
||||||
|
div.tip,
|
||||||
|
div.important,
|
||||||
|
div.caution,
|
||||||
|
div.warning,
|
||||||
|
p.blurb
|
||||||
{
|
{
|
||||||
font-size: 10pt; /* A little bit smaller than the main text */
|
font-size: 9pt; /* A little bit smaller than the main text */
|
||||||
line-height: 1.2;
|
line-height: 1.2;
|
||||||
|
display: block;
|
||||||
|
margin: 1pc 4% 0pc 4%;
|
||||||
|
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
td.blurb img
|
p.blurb img
|
||||||
{
|
{
|
||||||
padding: 1pt;
|
padding: 1pt;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*=============================================================================
|
/*=============================================================================
|
||||||
Misc
|
Variable Lists
|
||||||
=============================================================================*/
|
=============================================================================*/
|
||||||
|
|
||||||
/* Tone down the title of Parameter lists */
|
/* Make the terms in definition lists bold */
|
||||||
div.variablelist p.title
|
div.variablelist dl dt,
|
||||||
|
span.term
|
||||||
{
|
{
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
font-size: 100%;
|
font-size: 10pt;
|
||||||
text-align: left;
|
}
|
||||||
|
|
||||||
|
div.variablelist table tbody tr td
|
||||||
|
{
|
||||||
|
text-align: left;
|
||||||
|
vertical-align: top;
|
||||||
|
padding: 0em 2em 0em 0em;
|
||||||
|
font-size: 10pt;
|
||||||
|
margin: 0em 0em 0.5em 0em;
|
||||||
|
line-height: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Tabularize parameter lists */
|
|
||||||
div.variablelist dl dt
|
div.variablelist dl dt
|
||||||
{
|
{
|
||||||
float: left;
|
margin-bottom: 0.2em;
|
||||||
clear: left;
|
|
||||||
display: block;
|
|
||||||
font-style: italic;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
div.variablelist dl dd
|
div.variablelist dl dd
|
||||||
{
|
{
|
||||||
display: block;
|
margin: 0em 0em 0.5em 2em;
|
||||||
clear: right;
|
font-size: 10pt;
|
||||||
padding-left: 8pc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.variablelist table tbody tr td p,
|
||||||
|
div.variablelist dl dd p
|
||||||
|
{
|
||||||
|
margin: 0em 0em 0.5em 0em;
|
||||||
|
line-height: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*=============================================================================
|
||||||
|
Misc
|
||||||
|
=============================================================================*/
|
||||||
|
|
||||||
/* Title of books and articles in bibliographies */
|
/* Title of books and articles in bibliographies */
|
||||||
span.title
|
span.title
|
||||||
{
|
{
|
||||||
@@ -295,6 +353,14 @@
|
|||||||
color: #9c5a9c;
|
color: #9c5a9c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h1 a, h2 a, h3 a, h4 a, h5 a, h6 a,
|
||||||
|
h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover, h5 a:hover, h6 a:hover,
|
||||||
|
h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited
|
||||||
|
{
|
||||||
|
text-decoration: none; /* no underline */
|
||||||
|
color: #000000;
|
||||||
|
}
|
||||||
|
|
||||||
/* Syntax Highlighting */
|
/* Syntax Highlighting */
|
||||||
.keyword { color: #0000AA; }
|
.keyword { color: #0000AA; }
|
||||||
.identifier { color: #000000; }
|
.identifier { color: #000000; }
|
||||||
@@ -322,41 +388,50 @@
|
|||||||
/* Program listing */
|
/* Program listing */
|
||||||
pre.synopsis
|
pre.synopsis
|
||||||
{
|
{
|
||||||
background-color: #F3F3F3;
|
border: 1px solid #DCDCDC;
|
||||||
border: 1pt solid #C0C0C0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.programlisting,
|
.programlisting,
|
||||||
.screen
|
.screen
|
||||||
{
|
{
|
||||||
background-color: #F3F3F3;
|
border: 1px solid #DCDCDC;
|
||||||
border: 1pt solid #C0C0C0;
|
}
|
||||||
|
|
||||||
|
td .programlisting,
|
||||||
|
td .screen
|
||||||
|
{
|
||||||
|
border: 0px solid #DCDCDC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Blurbs */
|
/* Blurbs */
|
||||||
div.informaltable table tr td.blurb
|
div.note,
|
||||||
|
div.tip,
|
||||||
|
div.important,
|
||||||
|
div.caution,
|
||||||
|
div.warning,
|
||||||
|
p.blurb
|
||||||
{
|
{
|
||||||
background-color: #FFFFF0;
|
border: 1px solid #DCDCDC;
|
||||||
border: 1pt solid #707070;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Table of contents */
|
/* Table of contents */
|
||||||
.toc
|
.toc
|
||||||
{
|
{
|
||||||
background-color: #F3F3F3;
|
border: 1px solid #DCDCDC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Tables */
|
/* Tables */
|
||||||
div.informaltable table tr td,
|
div.informaltable table tr td,
|
||||||
div.table table tr td
|
div.table table tr td
|
||||||
{
|
{
|
||||||
background-color: #F0F0F0;
|
border: 1px solid #DCDCDC;
|
||||||
}
|
}
|
||||||
|
|
||||||
div.informaltable table tr th,
|
div.informaltable table tr th,
|
||||||
div.table table tr th
|
div.table table tr th
|
||||||
{
|
{
|
||||||
background-color: #E4E4E4;
|
background-color: #F0F0F0;
|
||||||
|
border: 1px solid #DCDCDC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Misc */
|
/* Misc */
|
||||||
@@ -396,6 +471,12 @@
|
|||||||
border: 1px solid gray;
|
border: 1px solid gray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
td .programlisting,
|
||||||
|
td .screen
|
||||||
|
{
|
||||||
|
border: 0px solid #DCDCDC;
|
||||||
|
}
|
||||||
|
|
||||||
/* Table of contents */
|
/* Table of contents */
|
||||||
.toc
|
.toc
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<title>Chapter 1. python 1.0</title>
|
<title>Chapter 1. python 1.0</title>
|
||||||
<link rel="stylesheet" href="boostbook.css" type="text/css">
|
<link rel="stylesheet" href="boostbook.css" type="text/css">
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
|
||||||
<link rel="start" href="index.html" title="Chapter 1. python 1.0">
|
<link rel="start" href="index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="next" href="python/hello.html" title=" Building Hello World">
|
<link rel="next" href="python/hello.html" title="Building Hello World">
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||||
<table cellpadding="2" width="100%">
|
<table cellpadding="2" width="100%">
|
||||||
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../../../../boost.png"></td>
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
|
||||||
<td align="center"><a href="../../../../../../index.htm">Home</a></td>
|
<td align="center"><a href="../../../../../../index.htm">Home</a></td>
|
||||||
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
|
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
|
||||||
<td align="center"><a href="../../../../../../people/people.htm">People</a></td>
|
<td align="center"><a href="../../../../../../people/people.htm">People</a></td>
|
||||||
@@ -28,15 +28,13 @@
|
|||||||
<div><div class="author"><h3 class="author">
|
<div><div class="author"><h3 class="author">
|
||||||
<span class="firstname">David</span> <span class="surname">Abrahams</span>
|
<span class="firstname">David</span> <span class="surname">Abrahams</span>
|
||||||
</h3></div></div>
|
</h3></div></div>
|
||||||
<div><p class="copyright">Copyright © 2002-2005 Joel de Guzman, David Abrahams</p></div>
|
<div><p class="copyright">Copyright © 2002-2005 Joel
|
||||||
|
de Guzman, David Abrahams</p></div>
|
||||||
<div><div class="legalnotice">
|
<div><div class="legalnotice">
|
||||||
<a name="id442427"></a><p>
|
<a name="id2632684"></a><p>
|
||||||
Distributed under the Boost Software License, Version 1.0.
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
(See accompanying file LICENSE_1_0.txt or copy at
|
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
|
||||||
<a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
|
http://www.boost.org/LICENSE_1_0.txt </a>)
|
||||||
http://www.boost.org/LICENSE_1_0.txt
|
|
||||||
</a>)
|
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
</div></div>
|
</div></div>
|
||||||
</div></div>
|
</div></div>
|
||||||
@@ -85,47 +83,69 @@
|
|||||||
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||||||
<a name="python.quickstart"></a>QuickStart</h2></div></div></div>
|
<a name="python.quickstart"></a>QuickStart</h2></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
The Boost Python Library is a framework for interfacing Python and
|
The Boost Python Library is a framework for interfacing Python and C++. It
|
||||||
C++. It allows you to quickly and seamlessly expose C++ classes
|
allows you to quickly and seamlessly expose C++ classes functions and objects
|
||||||
functions and objects to Python, and vice-versa, using no special
|
to Python, and vice-versa, using no special tools -- just your C++ compiler.
|
||||||
tools -- just your C++ compiler. It is designed to wrap C++ interfaces
|
It is designed to wrap C++ interfaces non-intrusively, so that you should not
|
||||||
non-intrusively, so that you should not have to change the C++ code at
|
have to change the C++ code at all in order to wrap it, making Boost.Python
|
||||||
all in order to wrap it, making Boost.Python ideal for exposing
|
ideal for exposing 3rd-party libraries to Python. The library's use of advanced
|
||||||
3rd-party libraries to Python. The library's use of advanced
|
metaprogramming techniques simplifies its syntax for users, so that wrapping
|
||||||
metaprogramming techniques simplifies its syntax for users, so that
|
code takes on the look of a kind of declarative interface definition language
|
||||||
wrapping code takes on the look of a kind of declarative interface
|
(IDL).
|
||||||
definition language (IDL).</p>
|
</p>
|
||||||
<a name="quickstart.hello_world"></a><h2>
|
<a name="quickstart.hello_world"></a><h3>
|
||||||
<a name="id372086"></a>Hello World</h2>
|
<a name="id2595112"></a>
|
||||||
|
Hello World
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
Following C/C++ tradition, let's start with the "hello, world". A C++
|
Following C/C++ tradition, let's start with the "hello, world". A
|
||||||
Function:</p>
|
C++ Function:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">char</span><span class="keyword"> const</span><span class="special">*</span><span class="identifier"> greet</span><span class="special">()</span><span class="special">
|
</p>
|
||||||
{</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
return</span><span class="string"> "hello, world"</span><span class="special">;</span><span class="special">
|
<span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">greet</span><span class="special">()</span>
|
||||||
}</span></tt></pre>
|
<span class="special">{</span>
|
||||||
|
<span class="keyword">return</span> <span class="string">"hello, world"</span><span class="special">;</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
can be exposed to Python by writing a Boost.Python wrapper:</p>
|
can be exposed to Python by writing a Boost.Python wrapper:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="preprocessor">#include</span><span class="special"> <</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span><span class="keyword">
|
</p>
|
||||||
using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span><span class="identifier">
|
<pre class="programlisting">
|
||||||
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||||||
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
|
||||||
|
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span><span class="special">
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span>
|
||||||
{</span><span class="identifier">
|
<span class="special">{</span>
|
||||||
def</span><span class="special">(</span><span class="string">"greet"</span><span class="special">,</span><span class="identifier"> greet</span><span class="special">);</span><span class="special">
|
<span class="identifier">def</span><span class="special">(</span><span class="string">"greet"</span><span class="special">,</span> <span class="identifier">greet</span><span class="special">);</span>
|
||||||
}</span></tt></pre>
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
That's it. We're done. We can now build this as a shared library. The
|
That's it. We're done. We can now build this as a shared library. The resulting
|
||||||
resulting DLL is now visible to Python. Here's a sample Python session:</p>
|
DLL is now visible to Python. Here's a sample Python session:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="keyword"> import</span><span class="identifier"> hello</span><span class="special">
|
<p>
|
||||||
>>></span><span class="keyword"> print</span><span class="identifier"> hello</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span><span class="identifier">
|
</p>
|
||||||
hello</span><span class="special">,</span><span class="identifier"> world</span></tt></pre>
|
<pre class="programlisting">
|
||||||
<p></p>
|
<span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">hello</span>
|
||||||
<div class="blockquote"><blockquote class="blockquote"><p><span class="emphasis"><em><span class="bold"><b>Next stop... Building your Hello World module from start to finish...</b></span></em></span></p></blockquote></div>
|
<span class="special">>>></span> <span class="keyword">print</span> <span class="identifier">hello</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span>
|
||||||
|
<span class="identifier">hello</span><span class="special">,</span> <span class="identifier">world</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<div class="blockquote"><blockquote class="blockquote">
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<span class="emphasis"><em><span class="bold"><strong>Next stop... Building your Hello World
|
||||||
|
module from start to finish...</strong></span></em></span>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
</blockquote></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||||
<td align="left"><small><p>Last revised: July 12, 2005 at 07:50:43 GMT</p></small></td>
|
<td align="left"><small><p>Last revised: May 18, 2007 at 15:46:01 GMT</p></small></td>
|
||||||
<td align="right"><small></small></td>
|
<td align="right"><small></small></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
|
|||||||
@@ -3,15 +3,15 @@
|
|||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<title>Embedding</title>
|
<title>Embedding</title>
|
||||||
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
|
||||||
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="prev" href="object.html" title=" Object Interface">
|
<link rel="prev" href="object.html" title="Object Interface">
|
||||||
<link rel="next" href="iterators.html" title="Iterators">
|
<link rel="next" href="iterators.html" title="Iterators">
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||||
<table cellpadding="2" width="100%">
|
<table cellpadding="2" width="100%">
|
||||||
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../../../../../boost.png"></td>
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
|
||||||
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
||||||
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
||||||
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
||||||
@@ -27,39 +27,46 @@
|
|||||||
<a name="python.embedding"></a>Embedding</h2></div></div></div>
|
<a name="python.embedding"></a>Embedding</h2></div></div></div>
|
||||||
<div class="toc"><dl><dt><span class="section"><a href="embedding.html#python.using_the_interpreter">Using the interpreter</a></span></dt></dl></div>
|
<div class="toc"><dl><dt><span class="section"><a href="embedding.html#python.using_the_interpreter">Using the interpreter</a></span></dt></dl></div>
|
||||||
<p>
|
<p>
|
||||||
By now you should know how to use Boost.Python to call your C++ code from
|
By now you should know how to use Boost.Python to call your C++ code from Python.
|
||||||
Python. However, sometimes you may need to do the reverse: call Python code
|
However, sometimes you may need to do the reverse: call Python code from the
|
||||||
from the C++-side. This requires you to <span class="emphasis"><em>embed</em></span> the Python interpreter
|
C++-side. This requires you to <span class="emphasis"><em>embed</em></span> the Python interpreter
|
||||||
into your C++ program.</p>
|
into your C++ program.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Currently, Boost.Python does not directly support everything you'll need
|
Currently, Boost.Python does not directly support everything you'll need when
|
||||||
when embedding. Therefore you'll need to use the
|
embedding. Therefore you'll need to use the <a href="http://www.python.org/doc/current/api/api.html" target="_top">Python/C
|
||||||
<a href="http://www.python.org/doc/current/api/api.html" target="_top">Python/C API</a> to fill in
|
API</a> to fill in the gaps. However, Boost.Python already makes embedding
|
||||||
the gaps. However, Boost.Python already makes embedding a lot easier and,
|
a lot easier and, in a future version, it may become unnecessary to touch the
|
||||||
in a future version, it may become unnecessary to touch the Python/C API at
|
Python/C API at all. So stay tuned... <span class="inlinemediaobject"><img src="../images/smiley.png" alt="smiley"></span>
|
||||||
all. So stay tuned... <span class="inlinemediaobject"><img src="../images/smiley.png"></span></p>
|
</p>
|
||||||
<a name="embedding.building_embedded_programs"></a><h2>
|
<a name="embedding.building_embedded_programs"></a><h3>
|
||||||
<a name="id456196"></a>Building embedded programs</h2>
|
<a name="id2654982"></a>
|
||||||
|
Building embedded programs
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
To be able to use embedding in your programs, they have to be linked to
|
To be able to embed python into your programs, you have to link to both Boost.Python's
|
||||||
both Boost.Python's and Python's static link library.</p>
|
as well as Python's own runtime library.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Boost.Python's static link library comes in two variants. Both are located
|
Boost.Python's library comes in two variants. Both are located in Boost's
|
||||||
in Boost's <tt class="literal">/libs/python/build/bin-stage</tt> subdirectory. On Windows, the
|
<code class="literal">/libs/python/build/bin-stage</code> subdirectory. On Windows, the
|
||||||
variants are called <tt class="literal">boost_python.lib</tt> (for release builds) and
|
variants are called <code class="literal">boost_python.lib</code> (for release builds)
|
||||||
<tt class="literal">boost_python_debug.lib</tt> (for debugging). If you can't find the libraries,
|
and <code class="literal">boost_python_debug.lib</code> (for debugging). If you can't
|
||||||
you probably haven't built Boost.Python yet. See
|
find the libraries, you probably haven't built Boost.Python yet. See <a href="../../../../building.html" target="_top">Building and Testing</a> on how to do this.
|
||||||
<a href="../../../../building.html" target="_top">Building and Testing</a> on how to do this.</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Python's static link library can be found in the <tt class="literal">/libs</tt> subdirectory of
|
Python's library can be found in the <code class="literal">/libs</code> subdirectory
|
||||||
your Python directory. On Windows it is called pythonXY.lib where X.Y is
|
of your Python directory. On Windows it is called pythonXY.lib where X.Y is
|
||||||
your major Python version number.</p>
|
your major Python version number.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Additionally, Python's <tt class="literal">/include</tt> subdirectory has to be added to your
|
Additionally, Python's <code class="literal">/include</code> subdirectory has to be added
|
||||||
include path.</p>
|
to your include path.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
In a Jamfile, all the above boils down to:</p>
|
In a Jamfile, all the above boils down to:
|
||||||
<pre class="programlisting"><tt class="literal">projectroot c:\projects\embedded_program ; # location of the program
|
</p>
|
||||||
|
<pre class="programlisting">projectroot c:\projects\embedded_program ; # location of the program
|
||||||
|
|
||||||
# bring in the rules for python
|
# bring in the rules for python
|
||||||
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
|
SEARCH on python.jam = $(BOOST_BUILD_PATH) ;
|
||||||
@@ -73,270 +80,194 @@ exe embedded_program # name of the executable
|
|||||||
$(PYTHON_PROPERTIES)
|
$(PYTHON_PROPERTIES)
|
||||||
<library-path>$(PYTHON_LIB_PATH)
|
<library-path>$(PYTHON_LIB_PATH)
|
||||||
<find-library>$(PYTHON_EMBEDDED_LIBRARY) ;
|
<find-library>$(PYTHON_EMBEDDED_LIBRARY) ;
|
||||||
</tt></pre>
|
</pre>
|
||||||
<a name="embedding.getting_started"></a><h2>
|
<a name="embedding.getting_started"></a><h3>
|
||||||
<a name="id456277"></a>Getting started</h2>
|
<a name="id2655076"></a>
|
||||||
|
Getting started
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
Being able to build is nice, but there is nothing to build yet. Embedding
|
Being able to build is nice, but there is nothing to build yet. Embedding the
|
||||||
the Python interpreter into one of your C++ programs requires these 4
|
Python interpreter into one of your C++ programs requires these 4 steps:
|
||||||
steps:</p>
|
</p>
|
||||||
<div class="orderedlist"><ol type="1">
|
<div class="orderedlist"><ol type="1">
|
||||||
<li>
|
<li>
|
||||||
#include <tt class="literal"><boost/python.hpp></tt><br><br>
|
#include <code class="literal"><boost/python.hpp></code><br><br>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-652" target="_top">Py_Initialize</a>() to start the interpreter and create the <tt class="literal"><span class="underline">_main</span>_</tt> module.<br><br>
|
Call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-652" target="_top">Py_Initialize</a>()
|
||||||
|
to start the interpreter and create the <code class="literal"><span class="underline">_main</span>_</code>
|
||||||
|
module.<br><br>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Call other Python C API routines to use the interpreter.<br><br>
|
Call other Python C API routines to use the interpreter.<br><br>
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
Call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-656" target="_top">Py_Finalize</a>() to stop the interpreter and release its resources.
|
|
||||||
</li>
|
</li>
|
||||||
</ol></div>
|
</ol></div>
|
||||||
|
<div class="sidebar">
|
||||||
|
<p class="title"><b></b></p>
|
||||||
<p>
|
<p>
|
||||||
(Of course, there can be other C++ code between all of these steps.)</p>
|
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> <span class="bold"><strong>Note that at this time you must
|
||||||
<div class="blockquote"><blockquote class="blockquote"><p><span class="emphasis"><em><span class="bold"><b>Now that we can embed the interpreter in our programs, lets see how to put it to use...</b></span></em></span></p></blockquote></div>
|
not call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-656" target="_top">Py_Finalize</a>()
|
||||||
|
to stop the interpreter. This may be fixed in a future version of boost.python.</strong></span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
(Of course, there can be other C++ code between all of these steps.)
|
||||||
|
</p>
|
||||||
|
<div class="blockquote"><blockquote class="blockquote">
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<span class="emphasis"><em><span class="bold"><strong>Now that we can embed the interpreter in
|
||||||
|
our programs, lets see how to put it to use...</strong></span></em></span>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
</blockquote></div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.using_the_interpreter"></a>Using the interpreter</h3></div></div></div>
|
<a name="python.using_the_interpreter"></a>Using the interpreter</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
As you probably already know, objects in Python are reference-counted.
|
As you probably already know, objects in Python are reference-counted. Naturally,
|
||||||
Naturally, the <tt class="literal">PyObject</tt>s of the Python/C API are also reference-counted.
|
the <code class="literal">PyObject</code>s of the Python/C API are also reference-counted.
|
||||||
There is a difference however. While the reference-counting is fully
|
There is a difference however. While the reference-counting is fully automatic
|
||||||
automatic in Python, the Python/C API requires you to do it
|
in Python, the Python<span class="emphasis"><em>C API requires you to do it [@http:</em></span>/www.python.org/doc/current/api/refcounts.html
|
||||||
<a href="http://www.python.org/doc/current/api/refcounts.html" target="_top">by hand</a>. This is
|
by hand]. This is messy and especially hard to get right in the presence
|
||||||
messy and especially hard to get right in the presence of C++ exceptions.
|
of C++ exceptions. Fortunately Boost.Python provides the <a href="../../../../v2/handle.html" target="_top">handle</a>
|
||||||
Fortunately Boost.Python provides the <a href="../../../../v2/handle.html" target="_top">handle</a> and
|
and <a href="../../../../v2/object.html" target="_top">object</a> class templates to
|
||||||
<a href="../../../../v2/object.html" target="_top">object</a> class templates to automate the process.</p>
|
automate the process.
|
||||||
<a name="using_the_interpreter.reference_counting_handles_and_objects"></a><h2>
|
</p>
|
||||||
<a name="id456409"></a>Reference-counting handles and objects</h2>
|
<a name="using_the_interpreter.running_python_code"></a><h3>
|
||||||
|
<a name="id2655255"></a>
|
||||||
|
Running Python code
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
There are two ways in which a function in the Python/C API can return a
|
Boost.python provides three related functions to run Python code from C++.
|
||||||
<tt class="literal">PyObject*</tt>: as a <span class="emphasis"><em>borrowed reference</em></span> or as a <span class="emphasis"><em>new reference</em></span>. Which of
|
</p>
|
||||||
these a function uses, is listed in that function's documentation. The two
|
<pre class="programlisting">
|
||||||
require slightely different approaches to reference-counting but both can
|
<span class="identifier">object</span> <span class="identifier">eval</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">expression</span><span class="special">,</span> <span class="identifier">object</span> <span class="identifier">globals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">(),</span> <span class="identifier">object</span> <span class="identifier">locals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">())</span>
|
||||||
be 'handled' by Boost.Python.</p>
|
<span class="identifier">object</span> <span class="identifier">exec</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">code</span><span class="special">,</span> <span class="identifier">object</span> <span class="identifier">globals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">(),</span> <span class="identifier">object</span> <span class="identifier">locals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">())</span>
|
||||||
|
<span class="identifier">object</span> <span class="identifier">exec_file</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">filename</span><span class="special">,</span> <span class="identifier">object</span> <span class="identifier">globals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">(),</span> <span class="identifier">object</span> <span class="identifier">locals</span> <span class="special">=</span> <span class="identifier">object</span><span class="special">())</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
For a function returning a <span class="emphasis"><em>borrowed reference</em></span> we'll have to tell the
|
eval evaluates the given expression and returns the resulting value. exec
|
||||||
<tt class="literal">handle</tt> that the <tt class="literal">PyObject*</tt> is borrowed with the aptly named
|
executes the given code (typically a set of statements) returning the result,
|
||||||
<a href="../../../../v2/handle.html#borrowed-spec" target="_top">borrowed</a> function. Two functions
|
and exec_file executes the code contained in the given file.
|
||||||
returning borrowed references are <a href="http://www.python.org/doc/current/api/importing.html#l2h-125" target="_top">PyImport_AddModule</a> and <a href="http://www.python.org/doc/current/api/moduleObjects.html#l2h-594" target="_top">PyModule_GetDict</a>.
|
</p>
|
||||||
The former returns a reference to an already imported module, the latter
|
<p>
|
||||||
retrieves a module's namespace dictionary. Let's use them to retrieve the
|
The <code class="literal">globals</code> and <code class="literal">locals</code> parameters are
|
||||||
namespace of the <tt class="literal"><span class="underline">_main</span>_</tt> module:</p>
|
Python dictionaries containing the globals and locals of the context in which
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">object</span><span class="identifier"> main_module</span><span class="special">((</span><span class="identifier">
|
to run the code. For most intents and purposes you can use the namespace
|
||||||
handle</span><span class="special"><>(</span><span class="identifier">borrowed</span><span class="special">(</span><a href="http://www.python.org/doc/current/api/importing.html#l2h-125" target="_top">PyImport_AddModule</a><span class="special">(</span><span class="string">"__main__"</span><span class="special">)))));</span><span class="identifier">
|
dictionary of the <code class="literal"><span class="underline">_main</span>_</code>
|
||||||
|
module for both parameters.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Boost.python provides a function to import a module:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">object</span> <span class="identifier">import</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">name</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
import imports a python module (potentially loading it into the running process
|
||||||
|
first), and returns it.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Let's import the <code class="literal"><span class="underline">_main</span>_</code>
|
||||||
|
module and run some Python code in its namespace:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">object</span> <span class="identifier">main_module</span> <span class="special">=</span> <span class="identifier">import</span><span class="special">(</span><span class="string">"__main__"</span><span class="special">);</span>
|
||||||
|
<span class="identifier">object</span> <span class="identifier">main_namespace</span> <span class="special">=</span> <span class="identifier">main_module</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">);</span>
|
||||||
|
|
||||||
object</span><span class="identifier"> main_namespace</span><span class="special"> =</span><span class="identifier"> main_module</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">);</span></tt></pre>
|
<span class="identifier">object</span> <span class="identifier">ignored</span> <span class="special">=</span> <span class="identifier">exec</span><span class="special">(</span><span class="string">"hello = file('hello.txt', 'w')\n"</span>
|
||||||
|
<span class="string">"hello.write('Hello world!')\n"</span>
|
||||||
|
<span class="string">"hello.close()"</span><span class="special">,</span>
|
||||||
|
<span class="identifier">main_namespace</span><span class="special">);</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
For a function returning a <span class="emphasis"><em>new reference</em></span> we can just create a <tt class="literal">handle</tt>
|
This should create a file called 'hello.txt' in the current directory containing
|
||||||
out of the raw <tt class="literal">PyObject*</tt> without wrapping it in a call to borrowed. One
|
a phrase that is well-known in programming circles.
|
||||||
such function that returns a new reference is <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a> which we'll
|
</p>
|
||||||
discuss in the next section.</p>
|
<a name="using_the_interpreter.manipulating_python_objects"></a><h3>
|
||||||
<div class="informaltable"><table class="table">
|
<a name="id2655783"></a>
|
||||||
<colgroup><col></colgroup>
|
Manipulating Python objects
|
||||||
<tbody><tr><td class="blurb">
|
</h3>
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span><span class="bold"><b>Handle is a class <span class="emphasis"><em>template</em></span>, so why haven't we been using any template parameters?</b></span><br><br><tt class="literal">handle</tt> has a single template parameter specifying the type of the managed object. This type is <tt class="literal">PyObject</tt> 99% of the time, so the parameter was defaulted to <tt class="literal">PyObject</tt> for convenience. Therefore we can use the shorthand <tt class="literal">handle<></tt> instead of the longer, but equivalent, <tt class="literal">handle<PyObject></tt>.
|
|
||||||
</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
<a name="using_the_interpreter.running_python_code"></a><h2>
|
|
||||||
<a name="id456714"></a>Running Python code</h2>
|
|
||||||
<p>
|
<p>
|
||||||
To run Python code from C++ there is a family of functions in the API
|
Often we'd like to have a class to manipulate Python objects. But we have
|
||||||
starting with the PyRun prefix. You can find the full list of these
|
already seen such a class above, and in the <a href="object.html" target="_top">previous
|
||||||
functions <a href="http://www.python.org/doc/current/api/veryhigh.html" target="_top">here</a>. They
|
section</a>: the aptly named <code class="literal">object</code> class and its
|
||||||
all work similarly so we will look at only one of them, namely:</p>
|
derivatives. We've already seen that they can be constructed from a <code class="literal">handle</code>.
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">PyObject</span><span class="special">*</span> <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a><span class="special">(</span><span class="keyword">char</span><span class="special"> *</span><span class="identifier">str</span><span class="special">,</span><span class="keyword"> int</span><span class="identifier"> start</span><span class="special">,</span><span class="identifier"> PyObject</span><span class="special"> *</span><span class="identifier">globals</span><span class="special">,</span><span class="identifier"> PyObject</span><span class="special"> *</span><span class="identifier">locals</span><span class="special">)</span></tt></pre>
|
The following examples should further illustrate this fact:
|
||||||
<p><a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a> takes the code to execute as a null-terminated (C-style)
|
</p>
|
||||||
string in its <tt class="literal">str</tt> parameter. The function returns a new reference to a
|
<pre class="programlisting">
|
||||||
Python object. Which object is returned depends on the <tt class="literal">start</tt> paramater.</p>
|
<span class="identifier">object</span> <span class="identifier">main_module</span> <span class="special">=</span> <span class="identifier">import</span><span class="special">(</span><span class="string">"__main__"</span><span class="special">);</span>
|
||||||
|
<span class="identifier">object</span> <span class="identifier">main_namespace</span> <span class="special">=</span> <span class="identifier">main_module</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">);</span>
|
||||||
|
<span class="identifier">object</span> <span class="identifier">ignored</span> <span class="special">=</span> <span class="identifier">exec</span><span class="special">(</span><span class="string">"result = 5 ** 2"</span><span class="special">,</span> <span class="identifier">main_namespace</span><span class="special">);</span>
|
||||||
|
<span class="keyword">int</span> <span class="identifier">five_squared</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">main_namespace</span><span class="special">[</span><span class="string">"result"</span><span class="special">]);</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
The <tt class="literal">start</tt> parameter is the start symbol from the Python grammar to use
|
Here we create a dictionary object for the <code class="literal"><span class="underline">_main</span>_</code>
|
||||||
for interpreting the code. The possible values are:</p>
|
module's namespace. Then we assign 5 squared to the result variable and read
|
||||||
<div class="informaltable">
|
this variable from the dictionary. Another way to achieve the same result
|
||||||
<h4>
|
is to use eval instead, which returns the result directly:
|
||||||
<a name="id456876"></a><span class="table-title">Start symbols</span>
|
</p>
|
||||||
</h4>
|
<pre class="programlisting">
|
||||||
<table class="table">
|
<span class="identifier">object</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">eval</span><span class="special">(</span><span class="string">"5 ** 2"</span><span class="special">);</span>
|
||||||
<colgroup>
|
<span class="keyword">int</span> <span class="identifier">five_squared</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">result</span><span class="special">);</span>
|
||||||
<col>
|
</pre>
|
||||||
<col>
|
<a name="using_the_interpreter.exception_handling"></a><h3>
|
||||||
</colgroup>
|
<a name="id2656116"></a>
|
||||||
<thead><tr>
|
Exception handling
|
||||||
<th><a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-58" target="_top">Py_eval_input</a></th>
|
</h3>
|
||||||
<th>for interpreting isolated expressions</th>
|
|
||||||
</tr></thead>
|
|
||||||
<tbody>
|
|
||||||
<tr>
|
|
||||||
<td><a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-59" target="_top">Py_file_input</a></td>
|
|
||||||
<td>for interpreting sequences of statements</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-60" target="_top">Py_single_input</a></td>
|
|
||||||
<td>for interpreting a single statement</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
<p>
|
<p>
|
||||||
When using <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-58" target="_top">Py_eval_input</a>, the input string must contain a single expression
|
If an exception occurs in the evaluation of the python expression, <a href="../../../../v2/errors.html#error_already_set-spec" target="_top">error_already_set</a>
|
||||||
and its result is returned. When using <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-59" target="_top">Py_file_input</a>, the string can
|
is thrown:
|
||||||
contain an abitrary number of statements and None is returned.
|
</p>
|
||||||
<a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-60" target="_top">Py_single_input</a> works in the same way as <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-59" target="_top">Py_file_input</a> but only accepts a
|
<pre class="programlisting">
|
||||||
single statement.</p>
|
<span class="keyword">try</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="identifier">object</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">eval</span><span class="special">(</span><span class="string">"5/0"</span><span class="special">);</span>
|
||||||
|
<span class="comment">// execution will never get here:
|
||||||
|
</span> <span class="keyword">int</span> <span class="identifier">five_divided_by_zero</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">result</span><span class="special">);</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
<span class="keyword">catch</span><span class="special">(</span><span class="identifier">error_already_set</span> <span class="keyword">const</span> <span class="special">&)</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="comment">// handle the exception in some way
|
||||||
|
</span><span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Lastly, the <tt class="literal">globals</tt> and <tt class="literal">locals</tt> parameters are Python dictionaries
|
The <code class="literal">error_already_set</code> exception class doesn't carry any
|
||||||
containing the globals and locals of the context in which to run the code.
|
information in itself. To find out more about the Python exception that occurred,
|
||||||
For most intents and purposes you can use the namespace dictionary of the
|
you need to use the <a href="http://www.python.org/doc/api/exceptionHandling.html" target="_top">exception
|
||||||
<tt class="literal"><span class="underline">_main</span>_</tt> module for both parameters.</p>
|
handling functions</a> of the Python<span class="emphasis"><em>C API in your catch-statement.
|
||||||
|
This can be as simple as calling [@http:</em></span>/www.python.org/doc/api/exceptionHandling.html#l2h-70
|
||||||
|
PyErr_Print()] to print the exception's traceback to the console, or comparing
|
||||||
|
the type of the exception with those of the <a href="http://www.python.org/doc/api/standardExceptions.html" target="_top">standard
|
||||||
|
exceptions</a>:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">catch</span><span class="special">(</span><span class="identifier">error_already_set</span> <span class="keyword">const</span> <span class="special">&)</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">PyErr_ExceptionMatches</span><span class="special">(</span><span class="identifier">PyExc_ZeroDivisionError</span><span class="special">))</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="comment">// handle ZeroDivisionError specially
|
||||||
|
</span> <span class="special">}</span>
|
||||||
|
<span class="keyword">else</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="comment">// print all other errors to stderr
|
||||||
|
</span> <span class="identifier">PyErr_Print</span><span class="special">();</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
We have already seen how to get the <tt class="literal"><span class="underline">_main</span>_</tt> module's namespace so let's
|
(To retrieve even more information from the exception you can use some of
|
||||||
run some Python code in it:</p>
|
the other exception handling functions listed <a href="http://www.python.org/doc/api/exceptionHandling.html" target="_top">here</a>.)
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">object</span><span class="identifier"> main_module</span><span class="special">((</span><span class="identifier">
|
</p>
|
||||||
handle</span><span class="special"><>(</span><span class="identifier">borrowed</span><span class="special">(</span><a href="http://www.python.org/doc/current/api/importing.html#l2h-125" target="_top">PyImport_AddModule</a><span class="special">(</span><span class="string">"__main__"</span><span class="special">)))));</span><span class="identifier">
|
|
||||||
|
|
||||||
object</span><span class="identifier"> main_namespace</span><span class="special"> =</span><span class="identifier"> main_module</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">);</span><span class="identifier">
|
|
||||||
|
|
||||||
handle</span><span class="special"><></span><span class="identifier"> ignored</span><span class="special">((</span><a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a><span class="special">(</span><span class="string">
|
|
||||||
|
|
||||||
"hello = file('hello.txt', 'w')\n"</span><span class="string">
|
|
||||||
"hello.write('Hello world!')\n"</span><span class="string">
|
|
||||||
"hello.close()"</span><span class="special">
|
|
||||||
|
|
||||||
,</span> <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-59" target="_top">Py_file_input</a><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">()</span><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">())</span><span class="special">
|
|
||||||
));</span></tt></pre>
|
|
||||||
<p>
|
|
||||||
Because the Python/C API doesn't know anything about <tt class="literal">object</tt>s, we used
|
|
||||||
the object's <tt class="literal">ptr</tt> member function to retrieve the <tt class="literal">PyObject*</tt>.</p>
|
|
||||||
<p>
|
|
||||||
This should create a file called 'hello.txt' in the current directory
|
|
||||||
containing a phrase that is well-known in programming circles.</p>
|
|
||||||
<div class="informaltable"><table class="table">
|
|
||||||
<colgroup><col></colgroup>
|
|
||||||
<tbody><tr><td class="blurb">
|
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span><span class="bold"><b>Note</b></span> that we wrap the return value of <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a> in a
|
|
||||||
(nameless) <tt class="literal">handle</tt> even though we are not interested in it. If we didn't
|
|
||||||
do this, the the returned object would be kept alive unnecessarily. Unless
|
|
||||||
you want to be a Dr. Frankenstein, always wrap <tt class="literal">PyObject*</tt>s in <tt class="literal">handle</tt>s.
|
|
||||||
</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
<a name="using_the_interpreter.beyond_handles"></a><h2>
|
|
||||||
<a name="id457324"></a>Beyond handles</h2>
|
|
||||||
<p>
|
|
||||||
It's nice that <tt class="literal">handle</tt> manages the reference counting details for us, but
|
|
||||||
other than that it doesn't do much. Often we'd like to have a more useful
|
|
||||||
class to manipulate Python objects. But we have already seen such a class
|
|
||||||
above, and in the <a href="object.html" target="_top">previous section</a>: the aptly
|
|
||||||
named <tt class="literal">object</tt> class and it's derivatives. We've already seen that they
|
|
||||||
can be constructed from a <tt class="literal">handle</tt>. The following examples should further
|
|
||||||
illustrate this fact:</p>
|
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">object</span><span class="identifier"> main_module</span><span class="special">((</span><span class="identifier">
|
|
||||||
handle</span><span class="special"><>(</span><span class="identifier">borrowed</span><span class="special">(</span><a href="http://www.python.org/doc/current/api/importing.html#l2h-125" target="_top">PyImport_AddModule</a><span class="special">(</span><span class="string">"__main__"</span><span class="special">)))));</span><span class="identifier">
|
|
||||||
|
|
||||||
object</span><span class="identifier"> main_namespace</span><span class="special"> =</span><span class="identifier"> main_module</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">);</span><span class="identifier">
|
|
||||||
|
|
||||||
handle</span><span class="special"><></span><span class="identifier"> ignored</span><span class="special">((</span><a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a><span class="special">(</span><span class="string">
|
|
||||||
|
|
||||||
"result = 5 ** 2"</span><span class="special">
|
|
||||||
|
|
||||||
,</span> <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-59" target="_top">Py_file_input</a><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">()</span><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">())</span><span class="special">
|
|
||||||
));</span><span class="keyword">
|
|
||||||
|
|
||||||
int</span><span class="identifier"> five_squared</span><span class="special"> =</span><span class="identifier"> extract</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">main_namespace</span><span class="special">[</span><span class="string">"result"</span><span class="special">]);</span></tt></pre>
|
|
||||||
<p>
|
|
||||||
Here we create a dictionary object for the <tt class="literal"><span class="underline">_main</span>_</tt> module's namespace.
|
|
||||||
Then we assign 5 squared to the result variable and read this variable from
|
|
||||||
the dictionary. Another way to achieve the same result is to let
|
|
||||||
<a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a> return the result directly with <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-58" target="_top">Py_eval_input</a>:</p>
|
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">object</span><span class="identifier"> result</span><span class="special">((</span><span class="identifier">handle</span><span class="special"><>(</span>
|
|
||||||
<a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a><span class="special">(</span><span class="string">"5 ** 2"</span><span class="special">
|
|
||||||
,</span> <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-58" target="_top">Py_eval_input</a><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">()</span><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">()))</span><span class="special">
|
|
||||||
));</span><span class="keyword">
|
|
||||||
|
|
||||||
int</span><span class="identifier"> five_squared</span><span class="special"> =</span><span class="identifier"> extract</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">result</span><span class="special">);</span></tt></pre>
|
|
||||||
<div class="informaltable"><table class="table">
|
|
||||||
<colgroup><col></colgroup>
|
|
||||||
<tbody><tr><td class="blurb">
|
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span><span class="bold"><b>Note</b></span> that <tt class="literal">object</tt>'s member function to return the wrapped
|
|
||||||
<tt class="literal">PyObject*</tt> is called <tt class="literal">ptr</tt> instead of <tt class="literal">get</tt>. This makes sense if you
|
|
||||||
take into account the different functions that <tt class="literal">object</tt> and <tt class="literal">handle</tt>
|
|
||||||
perform.
|
|
||||||
</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
<a name="using_the_interpreter.exception_handling"></a><h2>
|
|
||||||
<a name="id457906"></a>Exception handling</h2>
|
|
||||||
<p>
|
|
||||||
If an exception occurs in the execution of some Python code, the <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a>
|
|
||||||
function returns a null pointer. Constructing a <tt class="literal">handle</tt> out of this null
|
|
||||||
pointer throws <a href="../../../../v2/errors.html#error_already_set-spec" target="_top">error_already_set</a>,
|
|
||||||
so basically, the Python exception is automatically translated into a
|
|
||||||
C++ exception when using <tt class="literal">handle</tt>:</p>
|
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">try</span><span class="special">
|
|
||||||
{</span><span class="identifier">
|
|
||||||
object</span><span class="identifier"> result</span><span class="special">((</span><span class="identifier">handle</span><span class="special"><>(</span><a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a><span class="special">(</span><span class="string">
|
|
||||||
"5/0"</span><span class="special">
|
|
||||||
,</span> <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-58" target="_top">Py_eval_input</a><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">()</span><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">()))</span><span class="special">
|
|
||||||
));</span><span class="comment">
|
|
||||||
|
|
||||||
// execution will never get here:
|
|
||||||
</span><span class="keyword"> int</span><span class="identifier"> five_divided_by_zero</span><span class="special"> =</span><span class="identifier"> extract</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="identifier">result</span><span class="special">);</span><span class="special">
|
|
||||||
}</span><span class="keyword">
|
|
||||||
catch</span><span class="special">(</span><span class="identifier">error_already_set</span><span class="special">)</span><span class="special">
|
|
||||||
{</span><span class="comment">
|
|
||||||
// handle the exception in some way
|
|
||||||
</span><span class="special">}</span></tt></pre>
|
|
||||||
<p>
|
|
||||||
The <tt class="literal">error_already_set</tt> exception class doesn't carry any information in itself.
|
|
||||||
To find out more about the Python exception that occurred, you need to use the
|
|
||||||
<a href="http://www.python.org/doc/api/exceptionHandling.html" target="_top">exception handling functions</a>
|
|
||||||
of the Python/C API in your catch-statement. This can be as simple as calling
|
|
||||||
<a href="http://www.python.org/doc/api/exceptionHandling.html#l2h-70" target="_top">PyErr_Print()</a> to
|
|
||||||
print the exception's traceback to the console, or comparing the type of the
|
|
||||||
exception with those of the <a href="http://www.python.org/doc/api/standardExceptions.html" target="_top">standard exceptions</a>:</p>
|
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">catch</span><span class="special">(</span><span class="identifier">error_already_set</span><span class="special">)</span><span class="special">
|
|
||||||
{</span><span class="keyword">
|
|
||||||
if</span><span class="special"> (</span><span class="identifier">PyErr_ExceptionMatches</span><span class="special">(</span><span class="identifier">PyExc_ZeroDivisionError</span><span class="special">))</span><span class="special">
|
|
||||||
{</span><span class="comment">
|
|
||||||
// handle ZeroDivisionError specially
|
|
||||||
</span><span class="special"> }</span><span class="keyword">
|
|
||||||
else</span><span class="special">
|
|
||||||
{</span><span class="comment">
|
|
||||||
// print all other errors to stderr
|
|
||||||
</span><span class="identifier"> PyErr_Print</span><span class="special">();</span><span class="special">
|
|
||||||
}</span><span class="special">
|
|
||||||
}</span></tt></pre>
|
|
||||||
<p>
|
|
||||||
(To retrieve even more information from the exception you can use some of the other
|
|
||||||
exception handling functions listed <a href="http://www.python.org/doc/api/exceptionHandling.html" target="_top">here</a>.)</p>
|
|
||||||
<p>
|
|
||||||
If you'd rather not have <tt class="literal">handle</tt> throw a C++ exception when it is constructed, you
|
|
||||||
can use the <a href="../../../../v2/handle.html#allow_null-spec" target="_top">allow_null</a> function in the same
|
|
||||||
way you'd use borrowed:</p>
|
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">handle</span><span class="special"><></span><span class="identifier"> result</span><span class="special">((</span><span class="identifier">allow_null</span><span class="special">(</span><a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-55" target="_top">PyRun_String</a><span class="special">(</span><span class="string">
|
|
||||||
"5/0"</span><span class="special">
|
|
||||||
,</span> <a href="http://www.python.org/doc/current/api/veryhigh.html#l2h-58" target="_top">Py_eval_input</a><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">()</span><span class="special">
|
|
||||||
,</span><span class="identifier"> main_namespace</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">()))));</span><span class="keyword">
|
|
||||||
|
|
||||||
if</span><span class="special"> (!</span><span class="identifier">result</span><span class="special">)</span><span class="comment">
|
|
||||||
// Python exception occurred
|
|
||||||
</span><span class="keyword">else</span><span class="comment">
|
|
||||||
// everything went okay, it's safe to use the result
|
|
||||||
</span></tt></pre>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||||
<td align="left"></td>
|
<td align="left"></td>
|
||||||
<td align="right"><small>Copyright © 2002-2005 Joel de Guzman, David Abrahams</small></td>
|
<td align="right"><small>Copyright © 2002-2005 Joel
|
||||||
|
de Guzman, David Abrahams</small></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="spirit-nav">
|
<div class="spirit-nav">
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<title> Exception Translation</title>
|
<title>Exception Translation</title>
|
||||||
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
|
||||||
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="prev" href="iterators.html" title="Iterators">
|
<link rel="prev" href="iterators.html" title="Iterators">
|
||||||
<link rel="next" href="techniques.html" title=" General Techniques">
|
<link rel="next" href="techniques.html" title="General Techniques">
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||||
<table cellpadding="2" width="100%">
|
<table cellpadding="2" width="100%">
|
||||||
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../../../../../boost.png"></td>
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
|
||||||
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
||||||
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
||||||
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
||||||
@@ -26,25 +26,31 @@
|
|||||||
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||||||
<a name="python.exception"></a> Exception Translation</h2></div></div></div>
|
<a name="python.exception"></a> Exception Translation</h2></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
All C++ exceptions must be caught at the boundary with Python code. This
|
All C++ exceptions must be caught at the boundary with Python code. This boundary
|
||||||
boundary is the point where C++ meets Python. Boost.Python provides a
|
is the point where C++ meets Python. Boost.Python provides a default exception
|
||||||
default exception handler that translates selected standard exceptions,
|
handler that translates selected standard exceptions, then gives up:
|
||||||
then gives up:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">raise</span><span class="identifier"> RuntimeError</span><span class="special">,</span><span class="char"> 'unidentifiable C++ Exception'</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">raise</span> <span class="identifier">RuntimeError</span><span class="special">,</span> <span class="string">'unidentifiable C++ Exception'</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Users may provide custom translation. Here's an example:</p>
|
Users may provide custom translation. Here's an example:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> PodBayDoorException</span><span class="special">;</span><span class="keyword">
|
</p>
|
||||||
void</span><span class="identifier"> translator</span><span class="special">(</span><span class="identifier">PodBayDoorException</span><span class="keyword"> const</span><span class="special">&</span><span class="identifier"> x</span><span class="special">)</span><span class="special"> {</span><span class="identifier">
|
<pre class="programlisting">
|
||||||
PyErr_SetString</span><span class="special">(</span><span class="identifier">PyExc_UserWarning</span><span class="special">,</span><span class="string"> "I'm sorry Dave..."</span><span class="special">);</span><span class="special">
|
<span class="identifier">struct</span> <span class="identifier">PodBayDoorException</span><span class="special">;</span>
|
||||||
}</span><span class="identifier">
|
<span class="identifier">void</span> <span class="identifier">translator</span><span class="special">(</span><span class="identifier">PodBayDoorException</span> <span class="identifier">const</span><span class="special">&</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">kubrick</span><span class="special">)</span><span class="special"> {</span><span class="identifier">
|
<span class="identifier">PyErr_SetString</span><span class="special">(</span><span class="identifier">PyExc_UserWarning</span><span class="special">,</span> <span class="string">"I'm sorry Dave..."</span><span class="special">);</span>
|
||||||
register_exception_translator</span><span class="special"><</span><span class="identifier">
|
<span class="special">}</span>
|
||||||
PodBayDoorException</span><span class="special">>(</span><span class="identifier">translator</span><span class="special">);</span><span class="special">
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">kubrick</span><span class="special">)</span> <span class="special">{</span>
|
||||||
...</span></tt></pre>
|
<span class="identifier">register_exception_translator</span><span class="special"><</span>
|
||||||
|
<span class="identifier">PodBayDoorException</span><span class="special">>(</span><span class="identifier">translator</span><span class="special">);</span>
|
||||||
|
<span class="special">...</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||||
<td align="left"></td>
|
<td align="left"></td>
|
||||||
<td align="right"><small>Copyright © 2002-2005 Joel de Guzman, David Abrahams</small></td>
|
<td align="right"><small>Copyright © 2002-2005 Joel
|
||||||
|
de Guzman, David Abrahams</small></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="spirit-nav">
|
<div class="spirit-nav">
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<title> Exposing Classes</title>
|
<title>Exposing Classes</title>
|
||||||
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
|
||||||
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="prev" href="hello.html" title=" Building Hello World">
|
<link rel="prev" href="hello.html" title="Building Hello World">
|
||||||
<link rel="next" href="functions.html" title="Functions">
|
<link rel="next" href="functions.html" title="Functions">
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||||
<table cellpadding="2" width="100%">
|
<table cellpadding="2" width="100%">
|
||||||
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../../../../../boost.png"></td>
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
|
||||||
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
||||||
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
||||||
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
||||||
@@ -35,438 +35,582 @@
|
|||||||
<dt><span class="section"><a href="exposing.html#python.class_operators_special_functions">Class Operators/Special Functions</a></span></dt>
|
<dt><span class="section"><a href="exposing.html#python.class_operators_special_functions">Class Operators/Special Functions</a></span></dt>
|
||||||
</dl></div>
|
</dl></div>
|
||||||
<p>
|
<p>
|
||||||
Now let's expose a C++ class to Python.</p>
|
Now let's expose a C++ class to Python.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Consider a C++ class/struct that we want to expose to Python:</p>
|
Consider a C++ class/struct that we want to expose to Python:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> World</span><span class="special">
|
</p>
|
||||||
{</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
void</span><span class="identifier"> set</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> msg</span><span class="special">)</span><span class="special"> {</span><span class="keyword"> this</span><span class="special">-></span><span class="identifier">msg</span><span class="special"> =</span><span class="identifier"> msg</span><span class="special">;</span><span class="special"> }</span><span class="identifier">
|
<span class="keyword">struct</span> <span class="identifier">World</span>
|
||||||
std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> greet</span><span class="special">()</span><span class="special"> {</span><span class="keyword"> return</span><span class="identifier"> msg</span><span class="special">;</span><span class="special"> }</span><span class="identifier">
|
<span class="special">{</span>
|
||||||
std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> msg</span><span class="special">;</span><span class="special">
|
<span class="keyword">void</span> <span class="identifier">set</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">msg</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">this</span><span class="special">-></span><span class="identifier">msg</span> <span class="special">=</span> <span class="identifier">msg</span><span class="special">;</span> <span class="special">}</span>
|
||||||
};</span></tt></pre>
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">greet</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">msg</span><span class="special">;</span> <span class="special">}</span>
|
||||||
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">msg</span><span class="special">;</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
We can expose this to Python by writing a corresponding Boost.Python
|
We can expose this to Python by writing a corresponding Boost.Python C++ Wrapper:
|
||||||
C++ Wrapper:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="preprocessor">#include</span><span class="special"> <</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span><span class="keyword">
|
<pre class="programlisting">
|
||||||
using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span><span class="identifier">
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||||||
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
|
||||||
|
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span><span class="special">
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span>
|
||||||
{</span><span class="identifier">
|
<span class="special">{</span>
|
||||||
class_</span><span class="special"><</span><span class="identifier">World</span><span class="special">>(</span><span class="string">"World"</span><span class="special">)</span><span class="special">
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">World</span><span class="special">>(</span><span class="string">"World"</span><span class="special">)</span>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"greet"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">greet</span><span class="special">)</span><span class="special">
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"greet"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">greet</span><span class="special">)</span>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"set"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">set</span><span class="special">)</span><span class="special">
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"set"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">set</span><span class="special">)</span>
|
||||||
;</span><span class="special">
|
<span class="special">;</span>
|
||||||
}</span></tt></pre>
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Here, we wrote a C++ class wrapper that exposes the member functions
|
Here, we wrote a C++ class wrapper that exposes the member functions <code class="literal">greet</code>
|
||||||
<tt class="literal">greet</tt> and <tt class="literal">set</tt>. Now, after building our module as a shared library, we
|
and <code class="literal">set</code>. Now, after building our module as a shared library,
|
||||||
may use our class <tt class="literal">World</tt> in Python. Here's a sample Python session:</p>
|
we may use our class <code class="literal">World</code> in Python. Here's a sample Python
|
||||||
<p></p>
|
session:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="keyword"> import</span><span class="identifier"> hello</span><span class="special">
|
</p>
|
||||||
>>></span><span class="identifier"> planet</span><span class="special"> =</span><span class="identifier"> hello</span><span class="special">.</span><span class="identifier">World</span><span class="special">()</span><span class="special">
|
<p>
|
||||||
>>></span><span class="identifier"> planet</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="string">'howdy'</span><span class="special">)</span><span class="special">
|
</p>
|
||||||
>>></span><span class="identifier"> planet</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span><span class="string">
|
<pre class="programlisting">
|
||||||
'howdy'</span></tt></pre>
|
<span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">hello</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">planet</span> <span class="special">=</span> <span class="identifier">hello</span><span class="special">.</span><span class="identifier">World</span><span class="special">()</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">planet</span><span class="special">.</span><span class="identifier">set</span><span class="special">(</span><span class="string">'howdy'</span><span class="special">)</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">planet</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span>
|
||||||
|
<span class="string">'howdy'</span>
|
||||||
|
</pre>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.constructors"></a>Constructors</h3></div></div></div>
|
<a name="python.constructors"></a>Constructors</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
Our previous example didn't have any explicit constructors.
|
Our previous example didn't have any explicit constructors. Since <code class="literal">World</code>
|
||||||
Since <tt class="literal">World</tt> is declared as a plain struct, it has an implicit default
|
is declared as a plain struct, it has an implicit default constructor. Boost.Python
|
||||||
constructor. Boost.Python exposes the default constructor by default,
|
exposes the default constructor by default, which is why we were able to
|
||||||
which is why we were able to write</p>
|
write
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> planet</span><span class="special"> =</span><span class="identifier"> hello</span><span class="special">.</span><span class="identifier">World</span><span class="special">()</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="identifier">planet</span> <span class="special">=</span> <span class="identifier">hello</span><span class="special">.</span><span class="identifier">World</span><span class="special">()</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
We may wish to wrap a class with a non-default constructor. Let us
|
We may wish to wrap a class with a non-default constructor. Let us build
|
||||||
build on our previous example:</p>
|
on our previous example:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> World</span><span class="special">
|
|
||||||
{</span><span class="identifier">
|
|
||||||
World</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> msg</span><span class="special">):</span><span class="identifier"> msg</span><span class="special">(</span><span class="identifier">msg</span><span class="special">)</span><span class="special"> {}</span><span class="comment"> // added constructor
|
|
||||||
</span><span class="keyword"> void</span><span class="identifier"> set</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> msg</span><span class="special">)</span><span class="special"> {</span><span class="keyword"> this</span><span class="special">-></span><span class="identifier">msg</span><span class="special"> =</span><span class="identifier"> msg</span><span class="special">;</span><span class="special"> }</span><span class="identifier">
|
|
||||||
std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> greet</span><span class="special">()</span><span class="special"> {</span><span class="keyword"> return</span><span class="identifier"> msg</span><span class="special">;</span><span class="special"> }</span><span class="identifier">
|
|
||||||
std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> msg</span><span class="special">;</span><span class="special">
|
|
||||||
};</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
This time <tt class="literal">World</tt> has no default constructor; our previous
|
</p>
|
||||||
wrapping code would fail to compile when the library tried to expose
|
<pre class="programlisting">
|
||||||
it. We have to tell <tt class="literal">class_<World></tt> about the constructor we want to
|
<span class="keyword">struct</span> <span class="identifier">World</span>
|
||||||
expose instead.</p>
|
<span class="special">{</span>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="preprocessor">#include</span><span class="special"> <</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span><span class="keyword">
|
<span class="identifier">World</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">msg</span><span class="special">):</span> <span class="identifier">msg</span><span class="special">(</span><span class="identifier">msg</span><span class="special">)</span> <span class="special">{}</span> <span class="comment">// added constructor
|
||||||
using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span><span class="identifier">
|
</span> <span class="keyword">void</span> <span class="identifier">set</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">msg</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">this</span><span class="special">-></span><span class="identifier">msg</span> <span class="special">=</span> <span class="identifier">msg</span><span class="special">;</span> <span class="special">}</span>
|
||||||
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">greet</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">msg</span><span class="special">;</span> <span class="special">}</span>
|
||||||
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">msg</span><span class="special">;</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
This time <code class="literal">World</code> has no default constructor; our previous
|
||||||
|
wrapping code would fail to compile when the library tried to expose it.
|
||||||
|
We have to tell <code class="literal">class_<World></code> about the constructor
|
||||||
|
we want to expose instead.
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||||||
|
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
|
||||||
|
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span><span class="special">
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span>
|
||||||
{</span><span class="identifier">
|
<span class="special">{</span>
|
||||||
class_</span><span class="special"><</span><span class="identifier">World</span><span class="special">>(</span><span class="string">"World"</span><span class="special">,</span><span class="identifier"> init</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>())</span><span class="special">
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">World</span><span class="special">>(</span><span class="string">"World"</span><span class="special">,</span> <span class="identifier">init</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>())</span>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"greet"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">greet</span><span class="special">)</span><span class="special">
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"greet"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">greet</span><span class="special">)</span>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"set"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">set</span><span class="special">)</span><span class="special">
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"set"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">set</span><span class="special">)</span>
|
||||||
;</span><span class="special">
|
<span class="special">;</span>
|
||||||
}</span></tt></pre>
|
<span class="special">}</span>
|
||||||
<p><tt class="literal">init<std::string>()</tt> exposes the constructor taking in a
|
</pre>
|
||||||
<tt class="literal">std::string</tt> (in Python, constructors are spelled
|
|
||||||
"<tt class="literal">"<span class="underline">_init</span>_"</tt>").</p>
|
|
||||||
<p>
|
<p>
|
||||||
We can expose additional constructors by passing more <tt class="literal">init<...></tt>s to
|
<code class="literal">init<std::string>()</code> exposes the constructor taking
|
||||||
the <tt class="literal">def()</tt> member function. Say for example we have another World
|
in a <code class="literal">std::string</code> (in Python, constructors are spelled
|
||||||
constructor taking in two doubles:</p>
|
"<code class="literal">"<span class="underline">_init</span>_"</code>").
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">World</span><span class="special">>(</span><span class="string">"World"</span><span class="special">,</span><span class="identifier"> init</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>())</span><span class="special">
|
</p>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">init</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span><span class="keyword"> double</span><span class="special">>())</span><span class="special">
|
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"greet"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">greet</span><span class="special">)</span><span class="special">
|
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"set"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">set</span><span class="special">)</span><span class="special">
|
|
||||||
;</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
On the other hand, if we do not wish to expose any constructors at
|
We can expose additional constructors by passing more <code class="literal">init<...></code>s
|
||||||
all, we may use <tt class="literal">no_init</tt> instead:</p>
|
to the <code class="literal">def()</code> member function. Say for example we have
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">Abstract</span><span class="special">>(</span><span class="string">"Abstract"</span><span class="special">,</span><span class="identifier"> no_init</span><span class="special">)</span></tt></pre>
|
another World constructor taking in two doubles:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">World</span><span class="special">>(</span><span class="string">"World"</span><span class="special">,</span> <span class="identifier">init</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>())</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">init</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">>())</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"greet"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">greet</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"set"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">set</span><span class="special">)</span>
|
||||||
|
<span class="special">;</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
This actually adds an <tt class="literal"><span class="underline">_init</span>_</tt> method which always raises a
|
On the other hand, if we do not wish to expose any constructors at all, we
|
||||||
Python RuntimeError exception.</p>
|
may use <code class="literal">no_init</code> instead:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">Abstract</span><span class="special">>(</span><span class="string">"Abstract"</span><span class="special">,</span> <span class="identifier">no_init</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
This actually adds an <code class="literal"><span class="underline">_init</span>_</code>
|
||||||
|
method which always raises a Python RuntimeError exception.
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.class_data_members"></a>Class Data Members</h3></div></div></div>
|
<a name="python.class_data_members"></a>Class Data Members</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
Data members may also be exposed to Python so that they can be
|
Data members may also be exposed to Python so that they can be accessed as
|
||||||
accessed as attributes of the corresponding Python class. Each data
|
attributes of the corresponding Python class. Each data member that we wish
|
||||||
member that we wish to be exposed may be regarded as <span class="bold"><b>read-only</b></span> or
|
to be exposed may be regarded as <span class="bold"><strong>read-only</strong></span>
|
||||||
<span class="bold"><b>read-write</b></span>. Consider this class <tt class="literal">Var</tt>:</p>
|
or <span class="bold"><strong>read-write</strong></span>. Consider this class <code class="literal">Var</code>:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> Var</span><span class="special">
|
</p>
|
||||||
{</span><span class="identifier">
|
<pre class="programlisting">
|
||||||
Var</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> name</span><span class="special">)</span><span class="special"> :</span><span class="identifier"> name</span><span class="special">(</span><span class="identifier">name</span><span class="special">),</span><span class="identifier"> value</span><span class="special">()</span><span class="special"> {}</span><span class="identifier">
|
<span class="keyword">struct</span> <span class="identifier">Var</span>
|
||||||
std</span><span class="special">::</span><span class="identifier">string</span><span class="keyword"> const</span><span class="identifier"> name</span><span class="special">;</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
float</span><span class="identifier"> value</span><span class="special">;</span><span class="special">
|
<span class="identifier">Var</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">name</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">name</span><span class="special">(</span><span class="identifier">name</span><span class="special">),</span> <span class="identifier">value</span><span class="special">()</span> <span class="special">{}</span>
|
||||||
};</span></tt></pre>
|
<span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span> <span class="identifier">name</span><span class="special">;</span>
|
||||||
|
<span class="keyword">float</span> <span class="identifier">value</span><span class="special">;</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Our C++ <tt class="literal">Var</tt> class and its data members can be exposed to Python:</p>
|
Our C++ <code class="literal">Var</code> class and its data members can be exposed
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">Var</span><span class="special">>(</span><span class="string">"Var"</span><span class="special">,</span><span class="identifier"> init</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>())</span><span class="special">
|
to Python:
|
||||||
.</span><span class="identifier">def_readonly</span><span class="special">(</span><span class="string">"name"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">Var</span><span class="special">::</span><span class="identifier">name</span><span class="special">)</span><span class="special">
|
</p>
|
||||||
.</span><span class="identifier">def_readwrite</span><span class="special">(</span><span class="string">"value"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">Var</span><span class="special">::</span><span class="identifier">value</span><span class="special">);</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">Var</span><span class="special">>(</span><span class="string">"Var"</span><span class="special">,</span> <span class="identifier">init</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">>())</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def_readonly</span><span class="special">(</span><span class="string">"name"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Var</span><span class="special">::</span><span class="identifier">name</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def_readwrite</span><span class="special">(</span><span class="string">"value"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Var</span><span class="special">::</span><span class="identifier">value</span><span class="special">);</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Then, in Python, assuming we have placed our Var class inside the namespace
|
Then, in Python, assuming we have placed our Var class inside the namespace
|
||||||
hello as we did before:</p>
|
hello as we did before:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> x</span><span class="special"> =</span><span class="identifier"> hello</span><span class="special">.</span><span class="identifier">Var</span><span class="special">(</span><span class="string">'pi'</span><span class="special">)</span><span class="special">
|
|
||||||
>>></span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">value</span><span class="special"> =</span><span class="number"> 3.14</span><span class="special">
|
|
||||||
>>></span><span class="keyword"> print</span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">name</span><span class="special">,</span><span class="string"> 'is around'</span><span class="special">,</span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">value</span><span class="identifier">
|
|
||||||
pi</span><span class="keyword"> is</span><span class="identifier"> around</span><span class="number"> 3.14</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Note that <tt class="literal">name</tt> is exposed as <span class="bold"><b>read-only</b></span> while <tt class="literal">value</tt> is exposed
|
</p>
|
||||||
as <span class="bold"><b>read-write</b></span>.</p>
|
<pre class="programlisting">
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">name</span><span class="special"> =</span><span class="string"> 'e'</span><span class="comment"> # can't change name
|
<span class="special">>>></span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">hello</span><span class="special">.</span><span class="identifier">Var</span><span class="special">(</span><span class="string">'pi'</span><span class="special">)</span>
|
||||||
</span><span class="identifier">Traceback</span><span class="special"> (</span><span class="identifier">most</span><span class="identifier"> recent</span><span class="identifier"> call</span><span class="identifier"> last</span><span class="special">):</span><span class="identifier">
|
<span class="special">>>></span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">3.14</span>
|
||||||
File</span><span class="string"> "<stdin>"</span><span class="special">,</span><span class="identifier"> line</span><span class="number"> 1</span><span class="special">,</span><span class="keyword"> in</span>#<span class="identifier">
|
<span class="special">>>></span> <span class="keyword">print</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">name</span><span class="special">,</span> <span class="string">'is around'</span><span class="special">,</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">value</span>
|
||||||
AttributeError</span><span class="special">:</span><span class="identifier"> can</span>#<span class="identifier">t</span><span class="identifier"> set</span><span class="identifier"> attribute</span></tt></pre>
|
<span class="identifier">pi</span> <span class="keyword">is</span> <span class="identifier">around</span> <span class="number">3.14</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Note that <code class="literal">name</code> is exposed as <span class="bold"><strong>read-only</strong></span>
|
||||||
|
while <code class="literal">value</code> is exposed as <span class="bold"><strong>read-write</strong></span>.
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">name</span> <span class="special">=</span> <span class="string">'e'</span> <span class="comment"># can't change name
|
||||||
|
</span><span class="identifier">Traceback</span> <span class="special">(</span><span class="identifier">most</span> <span class="identifier">recent</span> <span class="identifier">call</span> <span class="identifier">last</span><span class="special">):</span>
|
||||||
|
<span class="identifier">File</span> <span class="string">"<stdin>"</span><span class="special">,</span> <span class="identifier">line</span> <span class="number">1</span><span class="special">,</span> <span class="keyword">in</span> #
|
||||||
|
<span class="identifier">AttributeError</span><span class="special">:</span> <span class="identifier">can</span>#<span class="identifier">t</span> <span class="identifier">set</span> <span class="identifier">attribute</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.class_properties"></a>Class Properties</h3></div></div></div>
|
<a name="python.class_properties"></a>Class Properties</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
In C++, classes with public data members are usually frowned
|
In C++, classes with public data members are usually frowned upon. Well designed
|
||||||
upon. Well designed classes that take advantage of encapsulation hide
|
classes that take advantage of encapsulation hide the class' data members.
|
||||||
the class' data members. The only way to access the class' data is
|
The only way to access the class' data is through access (getter/setter)
|
||||||
through access (getter/setter) functions. Access functions expose class
|
functions. Access functions expose class properties. Here's an example:
|
||||||
properties. Here's an example:</p>
|
</p>
|
||||||
<p></p>
|
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> Num</span><span class="special">
|
|
||||||
{</span><span class="identifier">
|
|
||||||
Num</span><span class="special">();</span><span class="keyword">
|
|
||||||
float</span><span class="identifier"> get</span><span class="special">()</span><span class="keyword"> const</span><span class="special">;</span><span class="keyword">
|
|
||||||
void</span><span class="identifier"> set</span><span class="special">(</span><span class="keyword">float</span><span class="identifier"> value</span><span class="special">);</span><span class="special">
|
|
||||||
...</span><span class="special">
|
|
||||||
};</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
However, in Python attribute access is fine; it doesn't neccessarily break
|
</p>
|
||||||
encapsulation to let users handle attributes directly, because the
|
<pre class="programlisting">
|
||||||
attributes can just be a different syntax for a method call. Wrapping our
|
<span class="keyword">struct</span> <span class="identifier">Num</span>
|
||||||
<tt class="literal">Num</tt> class using Boost.Python:</p>
|
<span class="special">{</span>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">Num</span><span class="special">>(</span><span class="string">"Num"</span><span class="special">)</span><span class="special">
|
<span class="identifier">Num</span><span class="special">();</span>
|
||||||
.</span><span class="identifier">add_property</span><span class="special">(</span><span class="string">"rovalue"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">)</span><span class="special">
|
<span class="keyword">float</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
|
||||||
.</span><span class="identifier">add_property</span><span class="special">(</span><span class="string">"value"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">,</span><span class="special"> &</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">set</span><span class="special">);</span></tt></pre>
|
<span class="keyword">void</span> <span class="identifier">set</span><span class="special">(</span><span class="keyword">float</span> <span class="identifier">value</span><span class="special">);</span>
|
||||||
|
<span class="special">...</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
And at last, in Python:</p>
|
However, in Python attribute access is fine; it doesn't neccessarily break
|
||||||
<p></p>
|
encapsulation to let users handle attributes directly, because the attributes
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> x</span><span class="special"> =</span><span class="identifier"> Num</span><span class="special">()</span><span class="special">
|
can just be a different syntax for a method call. Wrapping our <code class="literal">Num</code>
|
||||||
>>></span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">value</span><span class="special"> =</span><span class="number"> 3.14</span><span class="special">
|
class using Boost.Python:
|
||||||
>>></span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">value</span><span class="special">,</span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">rovalue</span><span class="special">
|
</p>
|
||||||
(</span><span class="number">3.14</span><span class="special">,</span><span class="number"> 3.14</span><span class="special">)</span><span class="special">
|
<pre class="programlisting">
|
||||||
>>></span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">rovalue</span><span class="special"> =</span><span class="number"> 2.17</span><span class="comment"> # error!
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">Num</span><span class="special">>(</span><span class="string">"Num"</span><span class="special">)</span>
|
||||||
</span></tt></pre>
|
<span class="special">.</span><span class="identifier">add_property</span><span class="special">(</span><span class="string">"rovalue"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">add_property</span><span class="special">(</span><span class="string">"value"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">set</span><span class="special">);</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Take note that the class property <tt class="literal">rovalue</tt> is exposed as <span class="bold"><b>read-only</b></span>
|
And at last, in Python:
|
||||||
since the <tt class="literal">rovalue</tt> setter member function is not passed in:</p>
|
</p>
|
||||||
<p></p>
|
<p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">.</span><span class="identifier">add_property</span><span class="special">(</span><span class="string">"rovalue"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">)</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">Num</span><span class="special">()</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">3.14</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">value</span><span class="special">,</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">rovalue</span>
|
||||||
|
<span class="special">(</span><span class="number">3.14</span><span class="special">,</span> <span class="number">3.14</span><span class="special">)</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">rovalue</span> <span class="special">=</span> <span class="number">2.17</span> <span class="comment"># error!
|
||||||
|
</span></pre>
|
||||||
|
<p>
|
||||||
|
Take note that the class property <code class="literal">rovalue</code> is exposed as
|
||||||
|
<span class="bold"><strong>read-only</strong></span> since the <code class="literal">rovalue</code>
|
||||||
|
setter member function is not passed in:
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">.</span><span class="identifier">add_property</span><span class="special">(</span><span class="string">"rovalue"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.inheritance"></a>Inheritance</h3></div></div></div>
|
<a name="python.inheritance"></a>Inheritance</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
In the previous examples, we dealt with classes that are not polymorphic.
|
In the previous examples, we dealt with classes that are not polymorphic.
|
||||||
This is not often the case. Much of the time, we will be wrapping
|
This is not often the case. Much of the time, we will be wrapping polymorphic
|
||||||
polymorphic classes and class hierarchies related by inheritance. We will
|
classes and class hierarchies related by inheritance. We will often have
|
||||||
often have to write Boost.Python wrappers for classes that are derived from
|
to write Boost.Python wrappers for classes that are derived from abstract
|
||||||
abstract base classes.</p>
|
base classes.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Consider this trivial inheritance structure:</p>
|
Consider this trivial inheritance structure:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> Base</span><span class="special"> {</span><span class="keyword"> virtual</span><span class="special"> ~</span><span class="identifier">Base</span><span class="special">();</span><span class="special"> };</span><span class="keyword">
|
</p>
|
||||||
struct</span><span class="identifier"> Derived</span><span class="special"> :</span><span class="identifier"> Base</span><span class="special"> {};</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">struct</span> <span class="identifier">Base</span> <span class="special">{</span> <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">Base</span><span class="special">();</span> <span class="special">};</span>
|
||||||
|
<span class="keyword">struct</span> <span class="identifier">Derived</span> <span class="special">:</span> <span class="identifier">Base</span> <span class="special">{};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
And a set of C++ functions operating on <tt class="literal">Base</tt> and <tt class="literal">Derived</tt> object
|
And a set of C++ functions operating on <code class="literal">Base</code> and <code class="literal">Derived</code>
|
||||||
instances:</p>
|
object instances:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">void</span><span class="identifier"> b</span><span class="special">(</span><span class="identifier">Base</span><span class="special">*);</span><span class="keyword">
|
</p>
|
||||||
void</span><span class="identifier"> d</span><span class="special">(</span><span class="identifier">Derived</span><span class="special">*);</span><span class="identifier">
|
<pre class="programlisting">
|
||||||
Base</span><span class="special">*</span><span class="identifier"> factory</span><span class="special">()</span><span class="special"> {</span><span class="keyword"> return</span><span class="keyword"> new</span><span class="identifier"> Derived</span><span class="special">;</span><span class="special"> }</span></tt></pre>
|
<span class="keyword">void</span> <span class="identifier">b</span><span class="special">(</span><span class="identifier">Base</span><span class="special">*);</span>
|
||||||
|
<span class="keyword">void</span> <span class="identifier">d</span><span class="special">(</span><span class="identifier">Derived</span><span class="special">*);</span>
|
||||||
|
<span class="identifier">Base</span><span class="special">*</span> <span class="identifier">factory</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">new</span> <span class="identifier">Derived</span><span class="special">;</span> <span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
We've seen how we can wrap the base class <tt class="literal">Base</tt>:</p>
|
We've seen how we can wrap the base class <code class="literal">Base</code>:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">Base</span><span class="special">>(</span><span class="string">"Base"</span><span class="special">)</span><span class="comment">
|
</p>
|
||||||
/*...*/</span><span class="special">
|
<pre class="programlisting">
|
||||||
;</span></tt></pre>
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">Base</span><span class="special">>(</span><span class="string">"Base"</span><span class="special">)</span>
|
||||||
|
<span class="comment">/*...*/</span>
|
||||||
|
<span class="special">;</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Now we can inform Boost.Python of the inheritance relationship between
|
Now we can inform Boost.Python of the inheritance relationship between <code class="literal">Derived</code>
|
||||||
<tt class="literal">Derived</tt> and its base class <tt class="literal">Base</tt>. Thus:</p>
|
and its base class <code class="literal">Base</code>. Thus:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">Derived</span><span class="special">,</span><span class="identifier"> bases</span><span class="special"><</span><span class="identifier">Base</span><span class="special">></span><span class="special"> >(</span><span class="string">"Derived"</span><span class="special">)</span><span class="comment">
|
</p>
|
||||||
/*...*/</span><span class="special">
|
<pre class="programlisting">
|
||||||
;</span></tt></pre>
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">Derived</span><span class="special">,</span> <span class="identifier">bases</span><span class="special"><</span><span class="identifier">Base</span><span class="special">></span> <span class="special">>(</span><span class="string">"Derived"</span><span class="special">)</span>
|
||||||
|
<span class="comment">/*...*/</span>
|
||||||
|
<span class="special">;</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Doing so, we get some things for free:</p>
|
Doing so, we get some things for free:
|
||||||
|
</p>
|
||||||
<div class="orderedlist"><ol type="1">
|
<div class="orderedlist"><ol type="1">
|
||||||
<li>
|
<li>
|
||||||
Derived automatically inherits all of Base's Python methods
|
Derived automatically inherits all of Base's Python methods (wrapped C++
|
||||||
(wrapped C++ member functions)
|
member functions)
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<span class="bold"><b>If</b></span> Base is polymorphic, <tt class="literal">Derived</tt> objects which have been passed to
|
<span class="bold"><strong>If</strong></span> Base is polymorphic, <code class="literal">Derived</code>
|
||||||
Python via a pointer or reference to <tt class="literal">Base</tt> can be passed where a pointer
|
objects which have been passed to Python via a pointer or reference to
|
||||||
or reference to <tt class="literal">Derived</tt> is expected.
|
<code class="literal">Base</code> can be passed where a pointer or reference to
|
||||||
</li>
|
<code class="literal">Derived</code> is expected.
|
||||||
|
</li>
|
||||||
</ol></div>
|
</ol></div>
|
||||||
<p>
|
<p>
|
||||||
Now, we shall expose the C++ free functions <tt class="literal">b</tt> and <tt class="literal">d</tt> and <tt class="literal">factory</tt>:</p>
|
Now, we shall expose the C++ free functions <code class="literal">b</code> and <code class="literal">d</code>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">def</span><span class="special">(</span><span class="string">"b"</span><span class="special">,</span><span class="identifier"> b</span><span class="special">);</span><span class="identifier">
|
and <code class="literal">factory</code>:
|
||||||
def</span><span class="special">(</span><span class="string">"d"</span><span class="special">,</span><span class="identifier"> d</span><span class="special">);</span><span class="identifier">
|
</p>
|
||||||
def</span><span class="special">(</span><span class="string">"factory"</span><span class="special">,</span><span class="identifier"> factory</span><span class="special">);</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">def</span><span class="special">(</span><span class="string">"b"</span><span class="special">,</span> <span class="identifier">b</span><span class="special">);</span>
|
||||||
|
<span class="identifier">def</span><span class="special">(</span><span class="string">"d"</span><span class="special">,</span> <span class="identifier">d</span><span class="special">);</span>
|
||||||
|
<span class="identifier">def</span><span class="special">(</span><span class="string">"factory"</span><span class="special">,</span> <span class="identifier">factory</span><span class="special">);</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Note that free function <tt class="literal">factory</tt> is being used to generate new
|
Note that free function <code class="literal">factory</code> is being used to generate
|
||||||
instances of class <tt class="literal">Derived</tt>. In such cases, we use
|
new instances of class <code class="literal">Derived</code>. In such cases, we use
|
||||||
<tt class="literal">return_value_policy<manage_new_object></tt> to instruct Python to adopt
|
<code class="literal">return_value_policy<manage_new_object></code> to instruct
|
||||||
the pointer to <tt class="literal">Base</tt> and hold the instance in a new Python <tt class="literal">Base</tt>
|
Python to adopt the pointer to <code class="literal">Base</code> and hold the instance
|
||||||
object until the the Python object is destroyed. We shall see more of
|
in a new Python <code class="literal">Base</code> object until the the Python object
|
||||||
Boost.Python <a href="functions.html#python.call_policies" title="Call Policies">call policies</a> later.</p>
|
is destroyed. We shall see more of Boost.Python <a href="functions.html#python.call_policies" title="Call Policies">call
|
||||||
<pre class="programlisting"><tt class="literal"><span class="comment">// Tell Python to take ownership of factory's result
|
policies</a> later.
|
||||||
</span><span class="identifier">def</span><span class="special">(</span><span class="string">"factory"</span><span class="special">,</span><span class="identifier"> factory</span><span class="special">,</span><span class="identifier">
|
</p>
|
||||||
return_value_policy</span><span class="special"><</span><span class="identifier">manage_new_object</span><span class="special">>());</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="comment">// Tell Python to take ownership of factory's result
|
||||||
|
</span><span class="identifier">def</span><span class="special">(</span><span class="string">"factory"</span><span class="special">,</span> <span class="identifier">factory</span><span class="special">,</span>
|
||||||
|
<span class="identifier">return_value_policy</span><span class="special"><</span><span class="identifier">manage_new_object</span><span class="special">>());</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.class_virtual_functions"></a>Class Virtual Functions</h3></div></div></div>
|
<a name="python.class_virtual_functions"></a>Class Virtual Functions</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
In this section, we shall learn how to make functions behave polymorphically
|
In this section, we shall learn how to make functions behave polymorphically
|
||||||
through virtual functions. Continuing our example, let us add a virtual function
|
through virtual functions. Continuing our example, let us add a virtual function
|
||||||
to our <tt class="literal">Base</tt> class:</p>
|
to our <code class="literal">Base</code> class:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> Base</span><span class="special">
|
</p>
|
||||||
{</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
virtual</span><span class="special"> ~</span><span class="identifier">Base</span><span class="special">()</span><span class="special"> {}</span><span class="keyword">
|
<span class="keyword">struct</span> <span class="identifier">Base</span>
|
||||||
virtual</span><span class="keyword"> int</span><span class="identifier"> f</span><span class="special">()</span><span class="special"> =</span><span class="number"> 0</span><span class="special">;</span><span class="special">
|
<span class="special">{</span>
|
||||||
};</span></tt></pre>
|
<span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">Base</span><span class="special">()</span> <span class="special">{}</span>
|
||||||
|
<span class="keyword">virtual</span> <span class="keyword">int</span> <span class="identifier">f</span><span class="special">()</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
One of the goals of Boost.Python is to be minimally intrusive on an existing C++
|
One of the goals of Boost.Python is to be minimally intrusive on an existing
|
||||||
design. In principle, it should be possible to expose the interface for a 3rd
|
C++ design. In principle, it should be possible to expose the interface for
|
||||||
party library without changing it. It is not ideal to add anything to our class
|
a 3rd party library without changing it. It is not ideal to add anything
|
||||||
<tt class="computeroutput"><span class="identifier">Base</span></tt>. Yet, when you have a virtual function that's going to be overridden in
|
to our class <code class="computeroutput"><span class="identifier">Base</span></code>. Yet, when
|
||||||
Python and called polymorphically <span class="bold"><b>from C++</b></span>, we'll need to add some
|
you have a virtual function that's going to be overridden in Python and called
|
||||||
scaffoldings to make things work properly. What we'll do is write a class
|
polymorphically <span class="bold"><strong>from C++</strong></span>, we'll need to
|
||||||
wrapper that derives from <tt class="computeroutput"><span class="identifier">Base</span></tt> that will unintrusively hook into the virtual
|
add some scaffoldings to make things work properly. What we'll do is write
|
||||||
functions so that a Python override may be called:</p>
|
a class wrapper that derives from <code class="computeroutput"><span class="identifier">Base</span></code>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> BaseWrap</span><span class="special"> :</span><span class="identifier"> Base</span><span class="special">,</span><span class="identifier"> wrapper</span><span class="special"><</span><span class="identifier">Base</span><span class="special">></span><span class="special">
|
that will unintrusively hook into the virtual functions so that a Python
|
||||||
{</span><span class="keyword">
|
override may be called:
|
||||||
int</span><span class="identifier"> f</span><span class="special">()</span><span class="special">
|
</p>
|
||||||
{</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
return</span><span class="keyword"> this</span><span class="special">-></span><span class="identifier">get_override</span><span class="special">(</span><span class="string">"f"</span><span class="special">)();</span><span class="special">
|
<span class="keyword">struct</span> <span class="identifier">BaseWrap</span> <span class="special">:</span> <span class="identifier">Base</span><span class="special">,</span> <span class="identifier">wrapper</span><span class="special"><</span><span class="identifier">Base</span><span class="special">></span>
|
||||||
}</span><span class="special">
|
<span class="special">{</span>
|
||||||
};</span></tt></pre>
|
<span class="keyword">int</span> <span class="identifier">f</span><span class="special">()</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="keyword">return</span> <span class="keyword">this</span><span class="special">-></span><span class="identifier">get_override</span><span class="special">(</span><span class="string">"f"</span><span class="special">)();</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Notice too that in addition to inheriting from <tt class="computeroutput"><span class="identifier">Base</span></tt>, we also multiply-
|
Notice too that in addition to inheriting from <code class="computeroutput"><span class="identifier">Base</span></code>,
|
||||||
inherited <tt class="computeroutput"><span class="identifier">wrapper</span><span class="special"><</span><span class="identifier">Base</span><span class="special">></span></tt> (See <a href="../../../../v2/wrapper.html" target="_top">Wrapper</a>). The
|
we also multiply- inherited <code class="computeroutput"><span class="identifier">wrapper</span><span class="special"><</span><span class="identifier">Base</span><span class="special">></span></code> (See <a href="../../../../v2/wrapper.html" target="_top">Wrapper</a>).
|
||||||
<tt class="computeroutput"><span class="identifier">wrapper</span></tt> template makes the job of wrapping classes that are meant to
|
The <code class="computeroutput"><span class="identifier">wrapper</span></code> template makes
|
||||||
overridden in Python, easier.</p>
|
the job of wrapping classes that are meant to overridden in Python, easier.
|
||||||
<div class="informaltable"><table class="table">
|
</p>
|
||||||
<colgroup><col></colgroup>
|
<div class="sidebar">
|
||||||
<tbody><tr><td class="blurb">
|
<p class="title"><b></b></p>
|
||||||
<span class="inlinemediaobject"><img src="../images/alert.png"></span><span class="bold"><b>MSVC6/7 Workaround</b></span><br><br>
|
|
||||||
|
|
||||||
If you are using Microsoft Visual C++ 6 or 7, you have to write <tt class="computeroutput"><span class="identifier">f</span></tt> as:<br><br><tt class="computeroutput"><span class="keyword">return</span><span class="identifier"> call</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="keyword">this</span><span class="special">-></span><span class="identifier">get_override</span><span class="special">(</span><span class="string">"f"</span><span class="special">).</span><span class="identifier">ptr</span><span class="special">());</span></tt>.</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
<p>
|
<p>
|
||||||
BaseWrap's overridden virtual member function <tt class="computeroutput"><span class="identifier">f</span></tt> in effect calls the
|
<span class="inlinemediaobject"><img src="../images/alert.png" alt="alert"></span> <span class="bold"><strong>MSVC6/7 Workaround</strong></span><br>
|
||||||
corresponding method of the Python object through <tt class="computeroutput"><span class="identifier">get_override</span></tt>.</p>
|
<br> If you are using Microsoft Visual C++ 6 or 7, you have to write <code class="computeroutput"><span class="identifier">f</span></code> as:<br> <br> <code class="computeroutput"><span class="keyword">return</span>
|
||||||
|
<span class="identifier">call</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(</span><span class="keyword">this</span><span class="special">-></span><span class="identifier">get_override</span><span class="special">(</span><span class="string">"f"</span><span class="special">).</span><span class="identifier">ptr</span><span class="special">());</span></code>.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
<p>
|
<p>
|
||||||
Finally, exposing <tt class="computeroutput"><span class="identifier">Base</span></tt>:</p>
|
BaseWrap's overridden virtual member function <code class="computeroutput"><span class="identifier">f</span></code>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">BaseWrap</span><span class="special">,</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">noncopyable</span><span class="special">>(</span><span class="string">"Base"</span><span class="special">)</span><span class="special">
|
in effect calls the corresponding method of the Python object through <code class="computeroutput"><span class="identifier">get_override</span></code>.
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> pure_virtual</span><span class="special">(&</span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">))</span><span class="special">
|
</p>
|
||||||
;</span></tt></pre>
|
<p>
|
||||||
<p><tt class="computeroutput"><span class="identifier">pure_virtual</span></tt> signals Boost.Python that the function <tt class="computeroutput"><span class="identifier">f</span></tt> is a pure virtual
|
Finally, exposing <code class="computeroutput"><span class="identifier">Base</span></code>:
|
||||||
function.</p>
|
</p>
|
||||||
<div class="informaltable"><table class="table">
|
<pre class="programlisting">
|
||||||
<colgroup><col></colgroup>
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">BaseWrap</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">noncopyable</span><span class="special">>(</span><span class="string">"Base"</span><span class="special">)</span>
|
||||||
<tbody><tr><td class="blurb">
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">pure_virtual</span><span class="special">(&</span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">))</span>
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span><span class="bold"><b>member function and methods</b></span><br><br>
|
<span class="special">;</span>
|
||||||
Python, like
|
</pre>
|
||||||
many object oriented languages uses the term <span class="bold"><b>methods</b></span>. Methods
|
<p>
|
||||||
correspond roughly to C++'s <span class="bold"><b>member functions</b></span>
|
<code class="computeroutput"><span class="identifier">pure_virtual</span></code> signals Boost.Python
|
||||||
</td></tr></tbody>
|
that the function <code class="computeroutput"><span class="identifier">f</span></code> is a
|
||||||
</table></div>
|
pure virtual function.
|
||||||
|
</p>
|
||||||
|
<div class="sidebar">
|
||||||
|
<p class="title"><b></b></p>
|
||||||
|
<p>
|
||||||
|
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> <span class="bold"><strong>member function and methods</strong></span><br>
|
||||||
|
<br> Python, like many object oriented languages uses the term <span class="bold"><strong>methods</strong></span>. Methods correspond roughly to C++'s <span class="bold"><strong>member functions</strong></span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.virtual_functions_with_default_implementations"></a>Virtual Functions with Default Implementations</h3></div></div></div>
|
<a name="python.virtual_functions_with_default_implementations"></a>Virtual Functions with Default Implementations</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
We've seen in the previous section how classes with pure virtual functions are
|
We've seen in the previous section how classes with pure virtual functions
|
||||||
wrapped using Boost.Python's <a href="../../../../v2/wrapper.html" target="_top">class wrapper</a>
|
are wrapped using Boost.Python's <a href="../../../../v2/wrapper.html" target="_top">class
|
||||||
facilities. If we wish to wrap <span class="bold"><b>non</b></span>-pure-virtual functions instead, the
|
wrapper</a> facilities. If we wish to wrap <span class="bold"><strong>non</strong></span>-pure-virtual
|
||||||
mechanism is a bit different.</p>
|
functions instead, the mechanism is a bit different.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Recall that in the <a href="exposing.html#python.class_virtual_functions" title="Class Virtual Functions">previous section</a>, we
|
Recall that in the <a href="exposing.html#python.class_virtual_functions" title="Class Virtual Functions">previous
|
||||||
wrapped a class with a pure virtual function that we then implemented in C++, or
|
section</a>, we wrapped a class with a pure virtual function that we then
|
||||||
Python classes derived from it. Our base class:</p>
|
implemented in C++, or Python classes derived from it. Our base class:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> Base</span><span class="special">
|
</p>
|
||||||
{</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
virtual</span><span class="keyword"> int</span><span class="identifier"> f</span><span class="special">()</span><span class="special"> =</span><span class="number"> 0</span><span class="special">;</span><span class="special">
|
<span class="keyword">struct</span> <span class="identifier">Base</span>
|
||||||
};</span></tt></pre>
|
<span class="special">{</span>
|
||||||
|
<span class="keyword">virtual</span> <span class="keyword">int</span> <span class="identifier">f</span><span class="special">()</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
had a pure virtual function <tt class="literal">f</tt>. If, however, its member function <tt class="literal">f</tt> was
|
had a pure virtual function <code class="literal">f</code>. If, however, its member
|
||||||
not declared as pure virtual:</p>
|
function <code class="literal">f</code> was not declared as pure virtual:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> Base</span><span class="special">
|
</p>
|
||||||
{</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
virtual</span><span class="special"> ~</span><span class="identifier">Base</span><span class="special">()</span><span class="special"> {}</span><span class="keyword">
|
<span class="keyword">struct</span> <span class="identifier">Base</span>
|
||||||
virtual</span><span class="keyword"> int</span><span class="identifier"> f</span><span class="special">()</span><span class="special"> {</span><span class="keyword"> return</span><span class="number"> 0</span><span class="special">;</span><span class="special"> }</span><span class="special">
|
<span class="special">{</span>
|
||||||
};</span></tt></pre>
|
<span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">Base</span><span class="special">()</span> <span class="special">{}</span>
|
||||||
|
<span class="keyword">virtual</span> <span class="keyword">int</span> <span class="identifier">f</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> <span class="special">}</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
We wrap it this way:</p>
|
We wrap it this way:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> BaseWrap</span><span class="special"> :</span><span class="identifier"> Base</span><span class="special">,</span><span class="identifier"> wrapper</span><span class="special"><</span><span class="identifier">Base</span><span class="special">></span><span class="special">
|
</p>
|
||||||
{</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
int</span><span class="identifier"> f</span><span class="special">()</span><span class="special">
|
<span class="keyword">struct</span> <span class="identifier">BaseWrap</span> <span class="special">:</span> <span class="identifier">Base</span><span class="special">,</span> <span class="identifier">wrapper</span><span class="special"><</span><span class="identifier">Base</span><span class="special">></span>
|
||||||
{</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
if</span><span class="special"> (</span><span class="identifier">override</span><span class="identifier"> f</span><span class="special"> =</span><span class="keyword"> this</span><span class="special">-></span><span class="identifier">get_override</span><span class="special">(</span><span class="string">"f"</span><span class="special">))</span><span class="keyword">
|
<span class="keyword">int</span> <span class="identifier">f</span><span class="special">()</span>
|
||||||
return</span><span class="identifier"> f</span><span class="special">();</span><span class="comment"> // *note*
|
<span class="special">{</span>
|
||||||
</span><span class="keyword"> return</span><span class="identifier"> Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">();</span><span class="special">
|
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">override</span> <span class="identifier">f</span> <span class="special">=</span> <span class="keyword">this</span><span class="special">-></span><span class="identifier">get_override</span><span class="special">(</span><span class="string">"f"</span><span class="special">))</span>
|
||||||
}</span><span class="keyword">
|
<span class="keyword">return</span> <span class="identifier">f</span><span class="special">();</span> <span class="comment">// *note*
|
||||||
|
</span> <span class="keyword">return</span> <span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">();</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
|
||||||
int</span><span class="identifier"> default_f</span><span class="special">()</span><span class="special"> {</span><span class="keyword"> return</span><span class="keyword"> this</span><span class="special">-></span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">();</span><span class="special"> }</span><span class="special">
|
<span class="keyword">int</span> <span class="identifier">default_f</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">this</span><span class="special">-></span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">();</span> <span class="special">}</span>
|
||||||
};</span></tt></pre>
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Notice how we implemented <tt class="computeroutput"><span class="identifier">BaseWrap</span><span class="special">::</span><span class="identifier">f</span></tt>. Now, we have to check if there is an
|
Notice how we implemented <code class="computeroutput"><span class="identifier">BaseWrap</span><span class="special">::</span><span class="identifier">f</span></code>. Now,
|
||||||
override for <tt class="computeroutput"><span class="identifier">f</span></tt>. If none, then we call <tt class="computeroutput"><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">()</span></tt>.</p>
|
we have to check if there is an override for <code class="computeroutput"><span class="identifier">f</span></code>.
|
||||||
<div class="informaltable"><table class="table">
|
If none, then we call <code class="computeroutput"><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">()</span></code>.
|
||||||
<colgroup><col></colgroup>
|
</p>
|
||||||
<tbody><tr><td class="blurb">
|
<div class="sidebar">
|
||||||
<span class="inlinemediaobject"><img src="../images/alert.png"></span><span class="bold"><b>MSVC6/7 Workaround</b></span><br><br>
|
<p class="title"><b></b></p>
|
||||||
|
|
||||||
If you are using Microsoft Visual C++ 6 or 7, you have to rewrite the line
|
|
||||||
with the <tt class="computeroutput"><span class="special">*</span><span class="identifier">note</span><span class="special">*</span></tt> as:<br><br><tt class="computeroutput"><span class="keyword">return</span><span class="identifier"> call</span><span class="special"><</span><span class="keyword">char</span><span class="keyword"> const</span><span class="special">*>(</span><span class="identifier">f</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">());</span></tt>.</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
<p>
|
<p>
|
||||||
Finally, exposing:</p>
|
<span class="inlinemediaobject"><img src="../images/alert.png" alt="alert"></span> <span class="bold"><strong>MSVC6/7 Workaround</strong></span><br>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">BaseWrap</span><span class="special">,</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">noncopyable</span><span class="special">>(</span><span class="string">"Base"</span><span class="special">)</span><span class="special">
|
<br> If you are using Microsoft Visual C++ 6 or 7, you have to rewrite
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">,</span><span class="special"> &</span><span class="identifier">BaseWrap</span><span class="special">::</span><span class="identifier">default_f</span><span class="special">)</span><span class="special">
|
the line with the <code class="computeroutput"><span class="special">*</span><span class="identifier">note</span><span class="special">*</span></code> as:<br> <br> <code class="computeroutput"><span class="keyword">return</span>
|
||||||
;</span></tt></pre>
|
<span class="identifier">call</span><span class="special"><</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*>(</span><span class="identifier">f</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">());</span></code>.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
<p>
|
<p>
|
||||||
Take note that we expose both <tt class="computeroutput"><span class="special">&</span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span></tt> and <tt class="computeroutput"><span class="special">&</span><span class="identifier">BaseWrap</span><span class="special">::</span><span class="identifier">default_f</span></tt>.
|
Finally, exposing:
|
||||||
Boost.Python needs to keep track of 1) the dispatch function <tt class="literal">f</tt> and 2) the
|
</p>
|
||||||
forwarding function to its default implementation <tt class="literal">default_f</tt>. There's a
|
<pre class="programlisting">
|
||||||
special <tt class="literal">def</tt> function for this purpose.</p>
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">BaseWrap</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">noncopyable</span><span class="special">>(</span><span class="string">"Base"</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">,</span> <span class="special">&</span><span class="identifier">BaseWrap</span><span class="special">::</span><span class="identifier">default_f</span><span class="special">)</span>
|
||||||
|
<span class="special">;</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
In Python, the results would be as expected:</p>
|
Take note that we expose both <code class="computeroutput"><span class="special">&</span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span></code> and <code class="computeroutput"><span class="special">&</span><span class="identifier">BaseWrap</span><span class="special">::</span><span class="identifier">default_f</span></code>. Boost.Python needs to keep track
|
||||||
<p></p>
|
of 1) the dispatch function <code class="literal">f</code> and 2) the forwarding function
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> base</span><span class="special"> =</span><span class="identifier"> Base</span><span class="special">()</span><span class="special">
|
to its default implementation <code class="literal">default_f</code>. There's a special
|
||||||
>>></span><span class="keyword"> class</span><span class="identifier"> Derived</span><span class="special">(</span><span class="identifier">Base</span><span class="special">):</span><span class="special">
|
<code class="literal">def</code> function for this purpose.
|
||||||
...</span><span class="keyword"> def</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span><span class="special">
|
</p>
|
||||||
...</span><span class="keyword"> return</span><span class="number"> 42</span><span class="special">
|
|
||||||
...</span><span class="special">
|
|
||||||
>>></span><span class="identifier"> derived</span><span class="special"> =</span><span class="identifier"> Derived</span><span class="special">()</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Calling <tt class="literal">base.f()</tt>:</p>
|
In Python, the results would be as expected:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> base</span><span class="special">.</span><span class="identifier">f</span><span class="special">()</span><span class="number">
|
</p>
|
||||||
0</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Calling <tt class="literal">derived.f()</tt>:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> derived</span><span class="special">.</span><span class="identifier">f</span><span class="special">()</span><span class="number">
|
<pre class="programlisting">
|
||||||
42</span></tt></pre>
|
<span class="special">>>></span> <span class="identifier">base</span> <span class="special">=</span> <span class="identifier">Base</span><span class="special">()</span>
|
||||||
|
<span class="special">>>></span> <span class="keyword">class</span> <span class="identifier">Derived</span><span class="special">(</span><span class="identifier">Base</span><span class="special">):</span>
|
||||||
|
<span class="special">...</span> <span class="keyword">def</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
|
||||||
|
<span class="special">...</span> <span class="keyword">return</span> <span class="number">42</span>
|
||||||
|
<span class="special">...</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">derived</span> <span class="special">=</span> <span class="identifier">Derived</span><span class="special">()</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Calling <code class="literal">base.f()</code>:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="identifier">base</span><span class="special">.</span><span class="identifier">f</span><span class="special">()</span>
|
||||||
|
<span class="number">0</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Calling <code class="literal">derived.f()</code>:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="identifier">derived</span><span class="special">.</span><span class="identifier">f</span><span class="special">()</span>
|
||||||
|
<span class="number">42</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.class_operators_special_functions"></a>Class Operators/Special Functions</h3></div></div></div>
|
<a name="python.class_operators_special_functions"></a>Class Operators/Special Functions</h3></div></div></div>
|
||||||
<a name="class_operators_special_functions.python_operators"></a><h2>
|
<a name="class_operators_special_functions.python_operators"></a><h3>
|
||||||
<a name="id447543"></a>Python Operators</h2>
|
<a name="id2646169"></a>
|
||||||
|
Python Operators
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
C is well known for the abundance of operators. C++ extends this to the
|
C is well known for the abundance of operators. C++ extends this to the extremes
|
||||||
extremes by allowing operator overloading. Boost.Python takes advantage of
|
by allowing operator overloading. Boost.Python takes advantage of this and
|
||||||
this and makes it easy to wrap C++ operator-powered classes.</p>
|
makes it easy to wrap C++ operator-powered classes.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Consider a file position class <tt class="literal">FilePos</tt> and a set of operators that take
|
Consider a file position class <code class="literal">FilePos</code> and a set of operators
|
||||||
on FilePos instances:</p>
|
that take on FilePos instances:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">class</span><span class="identifier"> FilePos</span><span class="special"> {</span><span class="comment"> /*...*/</span><span class="special"> };</span><span class="identifier">
|
<p>
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">class</span> <span class="identifier">FilePos</span> <span class="special">{</span> <span class="comment">/*...*/</span> <span class="special">};</span>
|
||||||
|
|
||||||
FilePos</span><span class="keyword"> operator</span><span class="special">+(</span><span class="identifier">FilePos</span><span class="special">,</span><span class="keyword"> int</span><span class="special">);</span><span class="identifier">
|
<span class="identifier">FilePos</span> <span class="keyword">operator</span><span class="special">+(</span><span class="identifier">FilePos</span><span class="special">,</span> <span class="keyword">int</span><span class="special">);</span>
|
||||||
FilePos</span><span class="keyword"> operator</span><span class="special">+(</span><span class="keyword">int</span><span class="special">,</span><span class="identifier"> FilePos</span><span class="special">);</span><span class="keyword">
|
<span class="identifier">FilePos</span> <span class="keyword">operator</span><span class="special">+(</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">FilePos</span><span class="special">);</span>
|
||||||
int</span><span class="keyword"> operator</span><span class="special">-(</span><span class="identifier">FilePos</span><span class="special">,</span><span class="identifier"> FilePos</span><span class="special">);</span><span class="identifier">
|
<span class="keyword">int</span> <span class="keyword">operator</span><span class="special">-(</span><span class="identifier">FilePos</span><span class="special">,</span> <span class="identifier">FilePos</span><span class="special">);</span>
|
||||||
FilePos</span><span class="keyword"> operator</span><span class="special">-(</span><span class="identifier">FilePos</span><span class="special">,</span><span class="keyword"> int</span><span class="special">);</span><span class="identifier">
|
<span class="identifier">FilePos</span> <span class="keyword">operator</span><span class="special">-(</span><span class="identifier">FilePos</span><span class="special">,</span> <span class="keyword">int</span><span class="special">);</span>
|
||||||
FilePos</span><span class="special">&</span><span class="keyword"> operator</span><span class="special">+=(</span><span class="identifier">FilePos</span><span class="special">&,</span><span class="keyword"> int</span><span class="special">);</span><span class="identifier">
|
<span class="identifier">FilePos</span><span class="special">&</span> <span class="keyword">operator</span><span class="special">+=(</span><span class="identifier">FilePos</span><span class="special">&,</span> <span class="keyword">int</span><span class="special">);</span>
|
||||||
FilePos</span><span class="special">&</span><span class="keyword"> operator</span><span class="special">-=(</span><span class="identifier">FilePos</span><span class="special">&,</span><span class="keyword"> int</span><span class="special">);</span><span class="keyword">
|
<span class="identifier">FilePos</span><span class="special">&</span> <span class="keyword">operator</span><span class="special">-=(</span><span class="identifier">FilePos</span><span class="special">&,</span> <span class="keyword">int</span><span class="special">);</span>
|
||||||
bool</span><span class="keyword"> operator</span><span class="special"><(</span><span class="identifier">FilePos</span><span class="special">,</span><span class="identifier"> FilePos</span><span class="special">);</span></tt></pre>
|
<span class="keyword">bool</span> <span class="keyword">operator</span><span class="special"><(</span><span class="identifier">FilePos</span><span class="special">,</span> <span class="identifier">FilePos</span><span class="special">);</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
The class and the various operators can be mapped to Python rather easily
|
The class and the various operators can be mapped to Python rather easily
|
||||||
and intuitively:</p>
|
and intuitively:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">FilePos</span><span class="special">>(</span><span class="string">"FilePos"</span><span class="special">)</span><span class="special">
|
</p>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span><span class="special"> +</span><span class="keyword"> int</span><span class="special">())</span><span class="comment"> // __add__
|
<pre class="programlisting">
|
||||||
</span><span class="special"> .</span><span class="identifier">def</span><span class="special">(</span><span class="keyword">int</span><span class="special">()</span><span class="special"> +</span><span class="identifier"> self</span><span class="special">)</span><span class="comment"> // __radd__
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">FilePos</span><span class="special">>(</span><span class="string">"FilePos"</span><span class="special">)</span>
|
||||||
</span><span class="special"> .</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span><span class="special"> -</span><span class="identifier"> self</span><span class="special">)</span><span class="comment"> // __sub__
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span> <span class="special">+</span> <span class="keyword">int</span><span class="special">())</span> <span class="comment">// __add__
|
||||||
</span><span class="special"> .</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span><span class="special"> -</span><span class="keyword"> int</span><span class="special">())</span><span class="comment"> // __sub__
|
</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="keyword">int</span><span class="special">()</span> <span class="special">+</span> <span class="identifier">self</span><span class="special">)</span> <span class="comment">// __radd__
|
||||||
</span><span class="special"> .</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span><span class="special"> +=</span><span class="keyword"> int</span><span class="special">())</span><span class="comment"> // __iadd__
|
</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span> <span class="special">-</span> <span class="identifier">self</span><span class="special">)</span> <span class="comment">// __sub__
|
||||||
</span><span class="special"> .</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span><span class="special"> -=</span><span class="identifier"> other</span><span class="special"><</span><span class="keyword">int</span><span class="special">>())</span><span class="special">
|
</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span> <span class="special">-</span> <span class="keyword">int</span><span class="special">())</span> <span class="comment">// __sub__
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span><span class="special"> <</span><span class="identifier"> self</span><span class="special">);</span><span class="comment"> // __lt__
|
</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span> <span class="special">+=</span> <span class="keyword">int</span><span class="special">())</span> <span class="comment">// __iadd__
|
||||||
</span></tt></pre>
|
</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span> <span class="special">-=</span> <span class="identifier">other</span><span class="special"><</span><span class="keyword">int</span><span class="special">>())</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span> <span class="special"><</span> <span class="identifier">self</span><span class="special">);</span> <span class="comment">// __lt__
|
||||||
|
</span></pre>
|
||||||
<p>
|
<p>
|
||||||
The code snippet above is very clear and needs almost no explanation at
|
The code snippet above is very clear and needs almost no explanation at all.
|
||||||
all. It is virtually the same as the operators' signatures. Just take
|
It is virtually the same as the operators' signatures. Just take note that
|
||||||
note that <tt class="literal">self</tt> refers to FilePos object. Also, not every class <tt class="literal">T</tt> that
|
<code class="literal">self</code> refers to FilePos object. Also, not every class
|
||||||
you might need to interact with in an operator expression is (cheaply)
|
<code class="literal">T</code> that you might need to interact with in an operator
|
||||||
default-constructible. You can use <tt class="literal">other<T>()</tt> in place of an actual
|
expression is (cheaply) default-constructible. You can use <code class="literal">other<T>()</code>
|
||||||
<tt class="literal">T</tt> instance when writing "self expressions".</p>
|
in place of an actual <code class="literal">T</code> instance when writing "self
|
||||||
<a name="class_operators_special_functions.special_methods"></a><h2>
|
expressions".
|
||||||
<a name="id448230"></a>Special Methods</h2>
|
</p>
|
||||||
|
<a name="class_operators_special_functions.special_methods"></a><h3>
|
||||||
|
<a name="id2646853"></a>
|
||||||
|
Special Methods
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
Python has a few more <span class="emphasis"><em>Special Methods</em></span>. Boost.Python supports all of the
|
Python has a few more <span class="emphasis"><em>Special Methods</em></span>. Boost.Python
|
||||||
standard special method names supported by real Python class instances. A
|
supports all of the standard special method names supported by real Python
|
||||||
similar set of intuitive interfaces can also be used to wrap C++ functions
|
class instances. A similar set of intuitive interfaces can also be used to
|
||||||
that correspond to these Python <span class="emphasis"><em>special functions</em></span>. Example:</p>
|
wrap C++ functions that correspond to these Python <span class="emphasis"><em>special functions</em></span>.
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">class</span><span class="identifier"> Rational</span><span class="special">
|
Example:
|
||||||
{</span><span class="keyword"> public</span><span class="special">:</span><span class="keyword"> operator</span><span class="keyword"> double</span><span class="special">()</span><span class="keyword"> const</span><span class="special">;</span><span class="special"> };</span><span class="identifier">
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">class</span> <span class="identifier">Rational</span>
|
||||||
|
<span class="special">{</span> <span class="keyword">public</span><span class="special">:</span> <span class="keyword">operator</span> <span class="keyword">double</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span> <span class="special">};</span>
|
||||||
|
|
||||||
Rational</span><span class="identifier"> pow</span><span class="special">(</span><span class="identifier">Rational</span><span class="special">,</span><span class="identifier"> Rational</span><span class="special">);</span><span class="identifier">
|
<span class="identifier">Rational</span> <span class="identifier">pow</span><span class="special">(</span><span class="identifier">Rational</span><span class="special">,</span> <span class="identifier">Rational</span><span class="special">);</span>
|
||||||
Rational</span><span class="identifier"> abs</span><span class="special">(</span><span class="identifier">Rational</span><span class="special">);</span><span class="identifier">
|
<span class="identifier">Rational</span> <span class="identifier">abs</span><span class="special">(</span><span class="identifier">Rational</span><span class="special">);</span>
|
||||||
ostream</span><span class="special">&</span><span class="keyword"> operator</span><span class="special"><<(</span><span class="identifier">ostream</span><span class="special">&,</span><span class="identifier">Rational</span><span class="special">);</span><span class="identifier">
|
<span class="identifier">ostream</span><span class="special">&</span> <span class="keyword">operator</span><span class="special"><<(</span><span class="identifier">ostream</span><span class="special">&,</span><span class="identifier">Rational</span><span class="special">);</span>
|
||||||
|
|
||||||
class_</span><span class="special"><</span><span class="identifier">Rational</span><span class="special">>(</span><span class="string">"Rational"</span><span class="special">)</span><span class="special">
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">Rational</span><span class="special">>(</span><span class="string">"Rational"</span><span class="special">)</span>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">float_</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span><span class="comment"> // __float__
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">float_</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span> <span class="comment">// __float__
|
||||||
</span><span class="special"> .</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">pow</span><span class="special">(</span><span class="identifier">self</span><span class="special">,</span><span class="identifier"> other</span><span class="special"><</span><span class="identifier">Rational</span><span class="special">>))</span><span class="comment"> // __pow__
|
</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">pow</span><span class="special">(</span><span class="identifier">self</span><span class="special">,</span> <span class="identifier">other</span><span class="special"><</span><span class="identifier">Rational</span><span class="special">>))</span> <span class="comment">// __pow__
|
||||||
</span><span class="special"> .</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">abs</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span><span class="comment"> // __abs__
|
</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">abs</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span> <span class="comment">// __abs__
|
||||||
</span><span class="special"> .</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">str</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span><span class="comment"> // __str__
|
</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">str</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span> <span class="comment">// __str__
|
||||||
</span><span class="special"> ;</span></tt></pre>
|
</span> <span class="special">;</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Need we say more?</p>
|
Need we say more?
|
||||||
<div class="informaltable"><table class="table">
|
</p>
|
||||||
<colgroup><col></colgroup>
|
<div class="sidebar">
|
||||||
<tbody><tr><td class="blurb">
|
<p class="title"><b></b></p>
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span> What is the business of <tt class="computeroutput"><span class="keyword">operator</span><span class="special"><<</span></tt>?
|
<p>
|
||||||
Well, the method <tt class="computeroutput"><span class="identifier">str</span></tt> requires the <tt class="computeroutput"><span class="keyword">operator</span><span class="special"><<</span></tt> to do its work (i.e.
|
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> What is the business of <code class="computeroutput"><span class="keyword">operator</span><span class="special"><<</span></code>? Well, the method <code class="computeroutput"><span class="identifier">str</span></code>
|
||||||
<tt class="computeroutput"><span class="keyword">operator</span><span class="special"><<</span></tt> is used by the method defined by <tt class="computeroutput"><span class="identifier">def</span><span class="special">(</span><span class="identifier">str</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span></tt>.</td></tr></tbody>
|
requires the <code class="computeroutput"><span class="keyword">operator</span><span class="special"><<</span></code>
|
||||||
</table></div>
|
to do its work (i.e. <code class="computeroutput"><span class="keyword">operator</span><span class="special"><<</span></code> is used by the method defined by
|
||||||
|
<code class="computeroutput"><span class="identifier">def</span><span class="special">(</span><span class="identifier">str</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span></code>.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||||
<td align="left"></td>
|
<td align="left"></td>
|
||||||
<td align="right"><small>Copyright © 2002-2005 Joel de Guzman, David Abrahams</small></td>
|
<td align="right"><small>Copyright © 2002-2005 Joel
|
||||||
|
de Guzman, David Abrahams</small></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="spirit-nav">
|
<div class="spirit-nav">
|
||||||
|
|||||||
@@ -3,15 +3,15 @@
|
|||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<title>Functions</title>
|
<title>Functions</title>
|
||||||
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
|
||||||
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="prev" href="exposing.html" title=" Exposing Classes">
|
<link rel="prev" href="exposing.html" title="Exposing Classes">
|
||||||
<link rel="next" href="object.html" title=" Object Interface">
|
<link rel="next" href="object.html" title="Object Interface">
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||||
<table cellpadding="2" width="100%">
|
<table cellpadding="2" width="100%">
|
||||||
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../../../../../boost.png"></td>
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
|
||||||
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
||||||
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
||||||
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
||||||
@@ -32,17 +32,26 @@
|
|||||||
<dt><span class="section"><a href="functions.html#python.auto_overloading">Auto-Overloading</a></span></dt>
|
<dt><span class="section"><a href="functions.html#python.auto_overloading">Auto-Overloading</a></span></dt>
|
||||||
</dl></div>
|
</dl></div>
|
||||||
<p>
|
<p>
|
||||||
In this chapter, we'll look at Boost.Python powered functions in closer
|
In this chapter, we'll look at Boost.Python powered functions in closer detail.
|
||||||
detail. We shall see some facilities to make exposing C++ functions to
|
We shall see some facilities to make exposing C++ functions to Python safe
|
||||||
Python safe from potential pifalls such as dangling pointers and
|
from potential pifalls such as dangling pointers and references. We shall also
|
||||||
references. We shall also see facilities that will make it even easier for
|
see facilities that will make it even easier for us to expose C++ functions
|
||||||
us to expose C++ functions that take advantage of C++ features such as
|
that take advantage of C++ features such as overloading and default arguments.
|
||||||
overloading and default arguments.</p>
|
</p>
|
||||||
<div class="blockquote"><blockquote class="blockquote"><p><span class="emphasis"><em>Read on...</em></span></p></blockquote></div>
|
<div class="blockquote"><blockquote class="blockquote">
|
||||||
<p>
|
<p>
|
||||||
But before you do, you might want to fire up Python 2.2 or later and type
|
</p>
|
||||||
<tt class="literal">>>> import this</tt>.</p>
|
<p>
|
||||||
<pre class="programlisting"><tt class="literal">>>> import this
|
<span class="emphasis"><em>Read on...</em></span>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
</blockquote></div>
|
||||||
|
<p>
|
||||||
|
But before you do, you might want to fire up Python 2.2 or later and type
|
||||||
|
<code class="literal">>>> import this</code>.
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">>>> import this
|
||||||
The Zen of Python, by Tim Peters
|
The Zen of Python, by Tim Peters
|
||||||
Beautiful is better than ugly.
|
Beautiful is better than ugly.
|
||||||
Explicit is better than implicit.
|
Explicit is better than implicit.
|
||||||
@@ -59,418 +68,541 @@ In the face of ambiguity, refuse the temptation to guess.
|
|||||||
There should be one-- and preferably only one --obvious way to do it
|
There should be one-- and preferably only one --obvious way to do it
|
||||||
Although that way may not be obvious at first unless you're Dutch.
|
Although that way may not be obvious at first unless you're Dutch.
|
||||||
Now is better than never.
|
Now is better than never.
|
||||||
Although never is often better than <span class="bold"><b>right</b></span> now.
|
Although never is often better than <span class="bold"><strong>right</strong></span> now.
|
||||||
If the implementation is hard to explain, it's a bad idea.
|
If the implementation is hard to explain, it's a bad idea.
|
||||||
If the implementation is easy to explain, it may be a good idea.
|
If the implementation is easy to explain, it may be a good idea.
|
||||||
Namespaces are one honking great idea -- let's do more of those!
|
Namespaces are one honking great idea -- let's do more of those!
|
||||||
</tt></pre>
|
</pre>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.call_policies"></a>Call Policies</h3></div></div></div>
|
<a name="python.call_policies"></a>Call Policies</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
In C++, we often deal with arguments and return types such as pointers
|
In C++, we often deal with arguments and return types such as pointers and
|
||||||
and references. Such primitive types are rather, ummmm, low level and
|
references. Such primitive types are rather, ummmm, low level and they really
|
||||||
they really don't tell us much. At the very least, we don't know the
|
don't tell us much. At the very least, we don't know the owner of the pointer
|
||||||
owner of the pointer or the referenced object. No wonder languages
|
or the referenced object. No wonder languages such as Java and Python never
|
||||||
such as Java and Python never deal with such low level entities. In
|
deal with such low level entities. In C++, it's usually considered a good
|
||||||
C++, it's usually considered a good practice to use smart pointers
|
practice to use smart pointers which exactly describe ownership semantics.
|
||||||
which exactly describe ownership semantics. Still, even good C++
|
Still, even good C++ interfaces use raw references and pointers sometimes,
|
||||||
interfaces use raw references and pointers sometimes, so Boost.Python
|
so Boost.Python must deal with them. To do this, it may need your help. Consider
|
||||||
must deal with them. To do this, it may need your help. Consider the
|
the following C++ function:
|
||||||
following C++ function:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">X</span><span class="special">&</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&</span><span class="identifier"> y</span><span class="special">,</span><span class="identifier"> Z</span><span class="special">*</span><span class="identifier"> z</span><span class="special">);</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">X</span><span class="special">&</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&</span> <span class="identifier">y</span><span class="special">,</span> <span class="identifier">Z</span><span class="special">*</span> <span class="identifier">z</span><span class="special">);</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
How should the library wrap this function? A naive approach builds a
|
How should the library wrap this function? A naive approach builds a Python
|
||||||
Python X object around result reference. This strategy might or might
|
X object around result reference. This strategy might or might not work out.
|
||||||
not work out. Here's an example where it didn't</p>
|
Here's an example where it didn't
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> x</span><span class="special"> =</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">y</span><span class="special">,</span><span class="identifier"> z</span><span class="special">)</span> #<span class="identifier"> x</span><span class="identifier"> refers</span><span class="identifier"> to</span><span class="identifier"> some</span><span class="identifier"> C</span><span class="special">++</span><span class="identifier"> X</span><span class="special">
|
</p>
|
||||||
>>></span><span class="identifier"> del</span><span class="identifier"> y</span><span class="special">
|
<pre class="programlisting">
|
||||||
>>></span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">some_method</span><span class="special">()</span> #<span class="identifier"> CRASH</span><span class="special">!</span></tt></pre>
|
<span class="special">>>></span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">y</span><span class="special">,</span> <span class="identifier">z</span><span class="special">)</span> <span class="preprocessor"># x</span> <span class="identifier">refers</span> <span class="identifier">to</span> <span class="identifier">some</span> <span class="identifier">C</span><span class="special">++</span> <span class="identifier">X</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">del</span> <span class="identifier">y</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">some_method</span><span class="special">()</span> <span class="preprocessor"># CRASH</span><span class="special">!</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
What's the problem?</p>
|
What's the problem?
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Well, what if f() was implemented as shown below:</p>
|
Well, what if f() was implemented as shown below:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">X</span><span class="special">&</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&</span><span class="identifier"> y</span><span class="special">,</span><span class="identifier"> Z</span><span class="special">*</span><span class="identifier"> z</span><span class="special">)</span><span class="special">
|
</p>
|
||||||
{</span><span class="identifier">
|
<pre class="programlisting">
|
||||||
y</span><span class="special">.</span><span class="identifier">z</span><span class="special"> =</span><span class="identifier"> z</span><span class="special">;</span><span class="keyword">
|
<span class="identifier">X</span><span class="special">&</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&</span> <span class="identifier">y</span><span class="special">,</span> <span class="identifier">Z</span><span class="special">*</span> <span class="identifier">z</span><span class="special">)</span>
|
||||||
return</span><span class="identifier"> y</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span><span class="special">
|
<span class="special">{</span>
|
||||||
}</span></tt></pre>
|
<span class="identifier">y</span><span class="special">.</span><span class="identifier">z</span> <span class="special">=</span> <span class="identifier">z</span><span class="special">;</span>
|
||||||
|
<span class="keyword">return</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
The problem is that the lifetime of result X& is tied to the lifetime
|
The problem is that the lifetime of result X& is tied to the lifetime
|
||||||
of y, because the f() returns a reference to a member of the y
|
of y, because the f() returns a reference to a member of the y object. This
|
||||||
object. This idiom is is not uncommon and perfectly acceptable in the
|
idiom is is not uncommon and perfectly acceptable in the context of C++.
|
||||||
context of C++. However, Python users should not be able to crash the
|
However, Python users should not be able to crash the system just by using
|
||||||
system just by using our C++ interface. In this case deleting y will
|
our C++ interface. In this case deleting y will invalidate the reference
|
||||||
invalidate the reference to X. We have a dangling reference.</p>
|
to X. We have a dangling reference.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Here's what's happening:</p>
|
Here's what's happening:
|
||||||
|
</p>
|
||||||
<div class="orderedlist"><ol type="1">
|
<div class="orderedlist"><ol type="1">
|
||||||
<li>
|
<li>
|
||||||
<tt class="literal">f</tt> is called passing in a reference to <tt class="literal">y</tt> and a pointer to <tt class="literal">z</tt>
|
<code class="literal">f</code> is called passing in a reference to <code class="literal">y</code>
|
||||||
|
and a pointer to <code class="literal">z</code>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
A reference to <tt class="literal">y.x</tt> is returned
|
A reference to <code class="literal">y.x</code> is returned
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<tt class="literal">y</tt> is deleted. <tt class="literal">x</tt> is a dangling reference
|
<code class="literal">y</code> is deleted. <code class="literal">x</code> is a dangling reference
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<tt class="literal">x.some_method()</tt> is called
|
<code class="literal">x.some_method()</code> is called
|
||||||
</li>
|
</li>
|
||||||
<li><span class="bold"><b>BOOM!</b></span></li>
|
<li><span class="bold"><strong>BOOM!</strong></span></li>
|
||||||
</ol></div>
|
</ol></div>
|
||||||
<p>
|
<p>
|
||||||
We could copy result into a new object:</p>
|
We could copy result into a new object:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">y</span><span class="special">,</span><span class="identifier"> z</span><span class="special">).</span><span class="identifier">set</span><span class="special">(</span><span class="number">42</span><span class="special">)</span><span class="comment"> # Result disappears
|
|
||||||
</span><span class="special">>>></span><span class="identifier"> y</span><span class="special">.</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span><span class="comment"> # No crash, but still bad
|
|
||||||
</span><span class="number">3.14</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
This is not really our intent of our C++ interface. We've broken our
|
</p>
|
||||||
promise that the Python interface should reflect the C++ interface as
|
<pre class="programlisting">
|
||||||
closely as possible.</p>
|
<span class="special">>>></span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">y</span><span class="special">,</span> <span class="identifier">z</span><span class="special">).</span><span class="identifier">set</span><span class="special">(</span><span class="number">42</span><span class="special">)</span> <span class="comment"># Result disappears
|
||||||
|
</span><span class="special">>>></span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="comment"># No crash, but still bad
|
||||||
|
</span><span class="number">3.14</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Our problems do not end there. Suppose Y is implemented as follows:</p>
|
This is not really our intent of our C++ interface. We've broken our promise
|
||||||
<p></p>
|
that the Python interface should reflect the C++ interface as closely as
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> Y</span><span class="special">
|
possible.
|
||||||
{</span><span class="identifier">
|
</p>
|
||||||
X</span><span class="identifier"> x</span><span class="special">;</span><span class="identifier"> Z</span><span class="special">*</span><span class="identifier"> z</span><span class="special">;</span><span class="keyword">
|
|
||||||
int</span><span class="identifier"> z_value</span><span class="special">()</span><span class="special"> {</span><span class="keyword"> return</span><span class="identifier"> z</span><span class="special">-></span><span class="identifier">value</span><span class="special">();</span><span class="special"> }</span><span class="special">
|
|
||||||
};</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Notice that the data member <tt class="literal">z</tt> is held by class Y using a raw
|
Our problems do not end there. Suppose Y is implemented as follows:
|
||||||
pointer. Now we have a potential dangling pointer problem inside Y:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> x</span><span class="special"> =</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">y</span><span class="special">,</span><span class="identifier"> z</span><span class="special">)</span> #<span class="identifier"> y</span><span class="identifier"> refers</span><span class="identifier"> to</span><span class="identifier"> z</span><span class="special">
|
|
||||||
>>></span><span class="identifier"> del</span><span class="identifier"> z</span> #<span class="identifier"> Kill</span><span class="identifier"> the</span><span class="identifier"> z</span><span class="identifier"> object</span><span class="special">
|
|
||||||
>>></span><span class="identifier"> y</span><span class="special">.</span><span class="identifier">z_value</span><span class="special">()</span> #<span class="identifier"> CRASH</span><span class="special">!</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
For reference, here's the implementation of <tt class="literal">f</tt> again:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">X</span><span class="special">&</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&</span><span class="identifier"> y</span><span class="special">,</span><span class="identifier"> Z</span><span class="special">*</span><span class="identifier"> z</span><span class="special">)</span><span class="special">
|
<pre class="programlisting">
|
||||||
{</span><span class="identifier">
|
<span class="keyword">struct</span> <span class="identifier">Y</span>
|
||||||
y</span><span class="special">.</span><span class="identifier">z</span><span class="special"> =</span><span class="identifier"> z</span><span class="special">;</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
return</span><span class="identifier"> y</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span><span class="special">
|
<span class="identifier">X</span> <span class="identifier">x</span><span class="special">;</span> <span class="identifier">Z</span><span class="special">*</span> <span class="identifier">z</span><span class="special">;</span>
|
||||||
}</span></tt></pre>
|
<span class="keyword">int</span> <span class="identifier">z_value</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">z</span><span class="special">-></span><span class="identifier">value</span><span class="special">();</span> <span class="special">}</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Here's what's happening:</p>
|
Notice that the data member <code class="literal">z</code> is held by class Y using
|
||||||
|
a raw pointer. Now we have a potential dangling pointer problem inside Y:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">y</span><span class="special">,</span> <span class="identifier">z</span><span class="special">)</span> <span class="preprocessor"># y</span> <span class="identifier">refers</span> <span class="identifier">to</span> <span class="identifier">z</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">del</span> <span class="identifier">z</span> <span class="preprocessor"># Kill</span> <span class="identifier">the</span> <span class="identifier">z</span> <span class="identifier">object</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">z_value</span><span class="special">()</span> <span class="preprocessor"># CRASH</span><span class="special">!</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
For reference, here's the implementation of <code class="literal">f</code> again:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">X</span><span class="special">&</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&</span> <span class="identifier">y</span><span class="special">,</span> <span class="identifier">Z</span><span class="special">*</span> <span class="identifier">z</span><span class="special">)</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="identifier">y</span><span class="special">.</span><span class="identifier">z</span> <span class="special">=</span> <span class="identifier">z</span><span class="special">;</span>
|
||||||
|
<span class="keyword">return</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Here's what's happening:
|
||||||
|
</p>
|
||||||
<div class="orderedlist"><ol type="1">
|
<div class="orderedlist"><ol type="1">
|
||||||
<li>
|
<li>
|
||||||
<tt class="literal">f</tt> is called passing in a reference to <tt class="literal">y</tt> and a pointer to <tt class="literal">z</tt>
|
<code class="literal">f</code> is called passing in a reference to <code class="literal">y</code>
|
||||||
|
and a pointer to <code class="literal">z</code>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
A pointer to <tt class="literal">z</tt> is held by <tt class="literal">y</tt>
|
A pointer to <code class="literal">z</code> is held by <code class="literal">y</code>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
A reference to <tt class="literal">y.x</tt> is returned
|
A reference to <code class="literal">y.x</code> is returned
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<tt class="literal">z</tt> is deleted. <tt class="literal">y.z</tt> is a dangling pointer
|
<code class="literal">z</code> is deleted. <code class="literal">y.z</code> is a dangling pointer
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<tt class="literal">y.z_value()</tt> is called
|
<code class="literal">y.z_value()</code> is called
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<tt class="literal">z->value()</tt> is called
|
<code class="literal">z->value()</code> is called
|
||||||
</li>
|
</li>
|
||||||
<li><span class="bold"><b>BOOM!</b></span></li>
|
<li><span class="bold"><strong>BOOM!</strong></span></li>
|
||||||
</ol></div>
|
</ol></div>
|
||||||
<a name="call_policies.call_policies"></a><h2>
|
<a name="call_policies.call_policies"></a><h3>
|
||||||
<a name="id449896"></a>Call Policies</h2>
|
<a name="id2648560"></a>
|
||||||
|
Call Policies
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
Call Policies may be used in situations such as the example detailed above.
|
Call Policies may be used in situations such as the example detailed above.
|
||||||
In our example, <tt class="literal">return_internal_reference</tt> and <tt class="literal">with_custodian_and_ward</tt>
|
In our example, <code class="literal">return_internal_reference</code> and <code class="literal">with_custodian_and_ward</code>
|
||||||
are our friends:</p>
|
are our friends:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> f</span><span class="special">,</span><span class="identifier">
|
</p>
|
||||||
return_internal_reference</span><span class="special"><</span><span class="number">1</span><span class="special">,</span><span class="identifier">
|
<pre class="programlisting">
|
||||||
with_custodian_and_ward</span><span class="special"><</span><span class="number">1</span><span class="special">,</span><span class="number"> 2</span><span class="special">></span><span class="special"> >());</span></tt></pre>
|
<span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">f</span><span class="special">,</span>
|
||||||
|
<span class="identifier">return_internal_reference</span><span class="special"><</span><span class="number">1</span><span class="special">,</span>
|
||||||
|
<span class="identifier">with_custodian_and_ward</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">></span> <span class="special">>());</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
What are the <tt class="literal">1</tt> and <tt class="literal">2</tt> parameters, you ask?</p>
|
What are the <code class="literal">1</code> and <code class="literal">2</code> parameters, you
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">return_internal_reference</span><span class="special"><</span><span class="number">1</span></tt></pre>
|
ask?
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">return_internal_reference</span><span class="special"><</span><span class="number">1</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Informs Boost.Python that the first argument, in our case <tt class="literal">Y& y</tt>, is the
|
Informs Boost.Python that the first argument, in our case <code class="literal">Y&
|
||||||
owner of the returned reference: <tt class="literal">X&</tt>. The "<tt class="literal">1</tt>" simply specifies the
|
y</code>, is the owner of the returned reference: <code class="literal">X&</code>.
|
||||||
first argument. In short: "return an internal reference <tt class="literal">X&</tt> owned by the
|
The "<code class="literal">1</code>" simply specifies the first argument.
|
||||||
1st argument <tt class="literal">Y& y</tt>".</p>
|
In short: "return an internal reference <code class="literal">X&</code> owned
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">with_custodian_and_ward</span><span class="special"><</span><span class="number">1</span><span class="special">,</span><span class="number"> 2</span><span class="special">></span></tt></pre>
|
by the 1st argument <code class="literal">Y& y</code>".
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">with_custodian_and_ward</span><span class="special"><</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">></span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Informs Boost.Python that the lifetime of the argument indicated by ward
|
Informs Boost.Python that the lifetime of the argument indicated by ward
|
||||||
(i.e. the 2nd argument: <tt class="literal">Z* z</tt>) is dependent on the lifetime of the
|
(i.e. the 2nd argument: <code class="literal">Z* z</code>) is dependent on the lifetime
|
||||||
argument indicated by custodian (i.e. the 1st argument: <tt class="literal">Y& y</tt>).</p>
|
of the argument indicated by custodian (i.e. the 1st argument: <code class="literal">Y&
|
||||||
|
y</code>).
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
It is also important to note that we have defined two policies above. Two
|
It is also important to note that we have defined two policies above. Two
|
||||||
or more policies can be composed by chaining. Here's the general syntax:</p>
|
or more policies can be composed by chaining. Here's the general syntax:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">policy1</span><span class="special"><</span><span class="identifier">args</span><span class="special">...,</span><span class="identifier">
|
</p>
|
||||||
policy2</span><span class="special"><</span><span class="identifier">args</span><span class="special">...,</span><span class="identifier">
|
<pre class="programlisting">
|
||||||
policy3</span><span class="special"><</span><span class="identifier">args</span><span class="special">...></span><span class="special"> ></span><span class="special"> ></span></tt></pre>
|
<span class="identifier">policy1</span><span class="special"><</span><span class="identifier">args</span><span class="special">...,</span>
|
||||||
|
<span class="identifier">policy2</span><span class="special"><</span><span class="identifier">args</span><span class="special">...,</span>
|
||||||
|
<span class="identifier">policy3</span><span class="special"><</span><span class="identifier">args</span><span class="special">...></span> <span class="special">></span> <span class="special">></span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Here is the list of predefined call policies. A complete reference detailing
|
Here is the list of predefined call policies. A complete reference detailing
|
||||||
these can be found <a href="../../../../v2/reference.html#models_of_call_policies" target="_top">here</a>.</p>
|
these can be found <a href="../../../../v2/reference.html#models_of_call_policies" target="_top">here</a>.
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
<span class="bold"><b>with_custodian_and_ward</b></span><br>
|
<span class="bold"><strong>with_custodian_and_ward</strong></span><br> Ties lifetimes
|
||||||
Ties lifetimes of the arguments
|
of the arguments
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<span class="bold"><b>with_custodian_and_ward_postcall</b></span><br>
|
<span class="bold"><strong>with_custodian_and_ward_postcall</strong></span><br>
|
||||||
Ties lifetimes of the arguments and results
|
Ties lifetimes of the arguments and results
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<span class="bold"><strong>return_internal_reference</strong></span><br> Ties lifetime
|
||||||
|
of one argument to that of result
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<span class="bold"><strong>return_value_policy<T> with T one of:</strong></span><br>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<span class="bold"><b>return_internal_reference</b></span><br>
|
<span class="bold"><strong>reference_existing_object</strong></span><br> naive
|
||||||
Ties lifetime of one argument to that of result
|
(dangerous) approach
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<span class="bold"><strong>copy_const_reference</strong></span><br> Boost.Python
|
||||||
|
v1 approach
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<span class="bold"><strong>copy_non_const_reference</strong></span><br>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<span class="bold"><b>return_value_policy<T> with T one of:</b></span><br>
|
<span class="bold"><strong>manage_new_object</strong></span><br> Adopt a pointer
|
||||||
</li>
|
and hold the instance
|
||||||
<li>
|
</li>
|
||||||
<span class="bold"><b>reference_existing_object</b></span><br>
|
|
||||||
naive (dangerous) approach
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<span class="bold"><b>copy_const_reference</b></span><br>
|
|
||||||
Boost.Python v1 approach
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<span class="bold"><b>copy_non_const_reference</b></span><br>
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<span class="bold"><b>manage_new_object</b></span><br>
|
|
||||||
Adopt a pointer and hold the instance
|
|
||||||
</li>
|
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<div class="informaltable"><table class="table">
|
<div class="sidebar">
|
||||||
<colgroup><col></colgroup>
|
<p class="title"><b></b></p>
|
||||||
<tbody><tr><td class="blurb">
|
<p>
|
||||||
<span class="inlinemediaobject"><img src="../images/smiley.png"></span><span class="bold"><b>Remember the Zen, Luke:</b></span><br><br>
|
<span class="inlinemediaobject"><img src="../images/smiley.png" alt="smiley"></span> <span class="bold"><strong>Remember the Zen, Luke:</strong></span><br>
|
||||||
|
<br> "Explicit is better than implicit"<br> "In the face
|
||||||
"Explicit is better than implicit"<br>
|
of ambiguity, refuse the temptation to guess"<br>
|
||||||
|
</p>
|
||||||
"In the face of ambiguity, refuse the temptation to guess"<br>
|
</div>
|
||||||
</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.overloading"></a>Overloading</h3></div></div></div>
|
<a name="python.overloading"></a>Overloading</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
The following illustrates a scheme for manually wrapping an overloaded
|
The following illustrates a scheme for manually wrapping an overloaded member
|
||||||
member functions. Of course, the same technique can be applied to wrapping
|
functions. Of course, the same technique can be applied to wrapping overloaded
|
||||||
overloaded non-member functions.</p>
|
non-member functions.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
We have here our C++ class:</p>
|
We have here our C++ class:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> X</span><span class="special">
|
</p>
|
||||||
{</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
bool</span><span class="identifier"> f</span><span class="special">(</span><span class="keyword">int</span><span class="identifier"> a</span><span class="special">)</span><span class="special">
|
<span class="keyword">struct</span> <span class="identifier">X</span>
|
||||||
{</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
return</span><span class="keyword"> true</span><span class="special">;</span><span class="special">
|
<span class="keyword">bool</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a</span><span class="special">)</span>
|
||||||
}</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
|
<span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
|
||||||
bool</span><span class="identifier"> f</span><span class="special">(</span><span class="keyword">int</span><span class="identifier"> a</span><span class="special">,</span><span class="keyword"> double</span><span class="identifier"> b</span><span class="special">)</span><span class="special">
|
<span class="keyword">bool</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">b</span><span class="special">)</span>
|
||||||
{</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
return</span><span class="keyword"> true</span><span class="special">;</span><span class="special">
|
<span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
|
||||||
}</span><span class="keyword">
|
<span class="special">}</span>
|
||||||
|
|
||||||
bool</span><span class="identifier"> f</span><span class="special">(</span><span class="keyword">int</span><span class="identifier"> a</span><span class="special">,</span><span class="keyword"> double</span><span class="identifier"> b</span><span class="special">,</span><span class="keyword"> char</span><span class="identifier"> c</span><span class="special">)</span><span class="special">
|
<span class="keyword">bool</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">b</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">c</span><span class="special">)</span>
|
||||||
{</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
return</span><span class="keyword"> true</span><span class="special">;</span><span class="special">
|
<span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
|
||||||
}</span><span class="keyword">
|
<span class="special">}</span>
|
||||||
|
|
||||||
int</span><span class="identifier"> f</span><span class="special">(</span><span class="keyword">int</span><span class="identifier"> a</span><span class="special">,</span><span class="keyword"> int</span><span class="identifier"> b</span><span class="special">,</span><span class="keyword"> int</span><span class="identifier"> c</span><span class="special">)</span><span class="special">
|
<span class="keyword">int</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">b</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">c</span><span class="special">)</span>
|
||||||
{</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
return</span><span class="identifier"> a</span><span class="special"> +</span><span class="identifier"> b</span><span class="special"> +</span><span class="identifier"> c</span><span class="special">;</span><span class="special">
|
<span class="keyword">return</span> <span class="identifier">a</span> <span class="special">+</span> <span class="identifier">b</span> <span class="special">+</span> <span class="identifier">c</span><span class="special">;</span>
|
||||||
};</span><span class="special">
|
<span class="special">};</span>
|
||||||
};</span></tt></pre>
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Class X has 4 overloaded functions. We shall start by introducing some
|
Class X has 4 overloaded functions. We shall start by introducing some member
|
||||||
member function pointer variables:</p>
|
function pointer variables:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">bool</span><span class="special"> (</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx1</span><span class="special">)(</span><span class="keyword">int</span><span class="special">)</span><span class="special"> =</span><span class="special"> &</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span><span class="keyword">
|
</p>
|
||||||
bool</span><span class="special"> (</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx2</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword"> double</span><span class="special">)</span><span class="special"> =</span><span class="special"> &</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
bool</span><span class="special"> (</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx3</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword"> double</span><span class="special">,</span><span class="keyword"> char</span><span class="special">)=</span><span class="special"> &</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span><span class="keyword">
|
<span class="keyword">bool</span> <span class="special">(</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx1</span><span class="special">)(</span><span class="keyword">int</span><span class="special">)</span> <span class="special">=</span> <span class="special">&</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span>
|
||||||
int</span><span class="special"> (</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx4</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword"> int</span><span class="special">,</span><span class="keyword"> int</span><span class="special">)</span><span class="special"> =</span><span class="special"> &</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span></tt></pre>
|
<span class="keyword">bool</span> <span class="special">(</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx2</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">)</span> <span class="special">=</span> <span class="special">&</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span>
|
||||||
|
<span class="keyword">bool</span> <span class="special">(</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx3</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">,</span> <span class="keyword">char</span><span class="special">)=</span> <span class="special">&</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span>
|
||||||
|
<span class="keyword">int</span> <span class="special">(</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx4</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">=</span> <span class="special">&</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
With these in hand, we can proceed to define and wrap this for Python:</p>
|
With these in hand, we can proceed to define and wrap this for Python:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> fx1</span><span class="special">)</span><span class="special">
|
</p>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> fx2</span><span class="special">)</span><span class="special">
|
<pre class="programlisting">
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> fx3</span><span class="special">)</span><span class="special">
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">fx1</span><span class="special">)</span>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> fx4</span><span class="special">)</span></tt></pre>
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">fx2</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">fx3</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">fx4</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.default_arguments"></a>Default Arguments</h3></div></div></div>
|
<a name="python.default_arguments"></a>Default Arguments</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
Boost.Python wraps (member) function pointers. Unfortunately, C++ function
|
Boost.Python wraps (member) function pointers. Unfortunately, C++ function
|
||||||
pointers carry no default argument info. Take a function <tt class="literal">f</tt> with default
|
pointers carry no default argument info. Take a function <code class="literal">f</code>
|
||||||
arguments:</p>
|
with default arguments:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">int</span><span class="identifier"> f</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword"> double</span><span class="special"> =</span><span class="number"> 3.14</span><span class="special">,</span><span class="keyword"> char</span><span class="keyword"> const</span><span class="special">*</span><span class="special"> =</span><span class="string"> "hello"</span><span class="special">);</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">int</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span> <span class="special">=</span> <span class="number">3.14</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="special">=</span> <span class="string">"hello"</span><span class="special">);</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
But the type of a pointer to the function <tt class="literal">f</tt> has no information
|
But the type of a pointer to the function <code class="literal">f</code> has no information
|
||||||
about its default arguments:</p>
|
about its default arguments:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">int</span><span class="special">(*</span><span class="identifier">g</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">double</span><span class="special">,</span><span class="keyword">char</span><span class="keyword"> const</span><span class="special">*)</span><span class="special"> =</span><span class="identifier"> f</span><span class="special">;</span><span class="comment"> // defaults lost!
|
</p>
|
||||||
</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">int</span><span class="special">(*</span><span class="identifier">g</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">double</span><span class="special">,</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*)</span> <span class="special">=</span> <span class="identifier">f</span><span class="special">;</span> <span class="comment">// defaults lost!
|
||||||
|
</span></pre>
|
||||||
<p>
|
<p>
|
||||||
When we pass this function pointer to the <tt class="literal">def</tt> function, there is no way
|
When we pass this function pointer to the <code class="literal">def</code> function,
|
||||||
to retrieve the default arguments:</p>
|
there is no way to retrieve the default arguments:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> f</span><span class="special">);</span><span class="comment"> // defaults lost!
|
</p>
|
||||||
</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">f</span><span class="special">);</span> <span class="comment">// defaults lost!
|
||||||
|
</span></pre>
|
||||||
<p>
|
<p>
|
||||||
Because of this, when wrapping C++ code, we had to resort to manual
|
Because of this, when wrapping C++ code, we had to resort to manual wrapping
|
||||||
wrapping as outlined in the <a href="functions.html#python.overloading" title="Overloading">previous section</a>, or
|
as outlined in the <a href="functions.html#python.overloading" title="Overloading">previous section</a>,
|
||||||
writing thin wrappers:</p>
|
or writing thin wrappers:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="comment">// write "thin wrappers"
|
</p>
|
||||||
</span><span class="keyword">int</span><span class="identifier"> f1</span><span class="special">(</span><span class="keyword">int</span><span class="identifier"> x</span><span class="special">)</span><span class="special"> {</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span><span class="special"> }</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
int</span><span class="identifier"> f2</span><span class="special">(</span><span class="keyword">int</span><span class="identifier"> x</span><span class="special">,</span><span class="keyword"> double</span><span class="identifier"> y</span><span class="special">)</span><span class="special"> {</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span><span class="identifier">y</span><span class="special">);</span><span class="special"> }</span><span class="comment">
|
<span class="comment">// write "thin wrappers"
|
||||||
|
</span><span class="keyword">int</span> <span class="identifier">f1</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span> <span class="special">}</span>
|
||||||
|
<span class="keyword">int</span> <span class="identifier">f2</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span><span class="identifier">y</span><span class="special">);</span> <span class="special">}</span>
|
||||||
|
|
||||||
/*...*/
|
<span class="comment">/*...*/</span>
|
||||||
|
|
||||||
// in module init
|
<span class="comment">// in module init
|
||||||
</span><span class="identifier"> def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> f</span><span class="special">);</span><span class="comment"> // all arguments
|
</span> <span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">f</span><span class="special">);</span> <span class="comment">// all arguments
|
||||||
</span><span class="identifier"> def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> f2</span><span class="special">);</span><span class="comment"> // two arguments
|
</span> <span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">f2</span><span class="special">);</span> <span class="comment">// two arguments
|
||||||
</span><span class="identifier"> def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> f1</span><span class="special">);</span><span class="comment"> // one argument
|
</span> <span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">f1</span><span class="special">);</span> <span class="comment">// one argument
|
||||||
</span></tt></pre>
|
</span></pre>
|
||||||
<p>
|
<p>
|
||||||
When you want to wrap functions (or member functions) that either:</p>
|
When you want to wrap functions (or member functions) that either:
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
have default arguments, or
|
have default arguments, or
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
are overloaded with a common sequence of initial arguments
|
are overloaded with a common sequence of initial arguments
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<a name="default_arguments.boost_python_function_overloads"></a><h2>
|
<a name="default_arguments.boost_python_function_overloads"></a><h3>
|
||||||
<a name="id451716"></a>BOOST_PYTHON_FUNCTION_OVERLOADS</h2>
|
<a name="id2650414"></a>
|
||||||
|
BOOST_PYTHON_FUNCTION_OVERLOADS
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
Boost.Python now has a way to make it easier. For instance, given a function:</p>
|
Boost.Python now has a way to make it easier. For instance, given a function:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">int</span><span class="identifier"> foo</span><span class="special">(</span><span class="keyword">int</span><span class="identifier"> a</span><span class="special">,</span><span class="keyword"> char</span><span class="identifier"> b</span><span class="special"> =</span><span class="number"> 1</span><span class="special">,</span><span class="keyword"> unsigned</span><span class="identifier"> c</span><span class="special"> =</span><span class="number"> 2</span><span class="special">,</span><span class="keyword"> double</span><span class="identifier"> d</span><span class="special"> =</span><span class="number"> 3</span><span class="special">)</span><span class="special">
|
</p>
|
||||||
{</span><span class="comment">
|
<pre class="programlisting">
|
||||||
/*...*/</span><span class="special">
|
<span class="keyword">int</span> <span class="identifier">foo</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">b</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">c</span> <span class="special">=</span> <span class="number">2</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">d</span> <span class="special">=</span> <span class="number">3</span><span class="special">)</span>
|
||||||
}</span></tt></pre>
|
<span class="special">{</span>
|
||||||
|
<span class="comment">/*...*/</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
The macro invocation:</p>
|
The macro invocation:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">BOOST_PYTHON_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">foo_overloads</span><span class="special">,</span><span class="identifier"> foo</span><span class="special">,</span><span class="number"> 1</span><span class="special">,</span><span class="number"> 4</span><span class="special">)</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">BOOST_PYTHON_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">foo_overloads</span><span class="special">,</span> <span class="identifier">foo</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">4</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
will automatically create the thin wrappers for us. This macro will create
|
will automatically create the thin wrappers for us. This macro will create
|
||||||
a class <tt class="literal">foo_overloads</tt> that can be passed on to <tt class="literal">def(...)</tt>. The third
|
a class <code class="literal">foo_overloads</code> that can be passed on to <code class="literal">def(...)</code>.
|
||||||
and fourth macro argument are the minimum arguments and maximum arguments,
|
The third and fourth macro argument are the minimum arguments and maximum
|
||||||
respectively. In our <tt class="literal">foo</tt> function the minimum number of arguments is 1
|
arguments, respectively. In our <code class="literal">foo</code> function the minimum
|
||||||
and the maximum number of arguments is 4. The <tt class="literal">def(...)</tt> function will
|
number of arguments is 1 and the maximum number of arguments is 4. The <code class="literal">def(...)</code>
|
||||||
automatically add all the foo variants for us:</p>
|
function will automatically add all the foo variants for us:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">def</span><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span><span class="identifier"> foo</span><span class="special">,</span><span class="identifier"> foo_overloads</span><span class="special">());</span></tt></pre>
|
</p>
|
||||||
<a name="default_arguments.boost_python_member_function_overloads"></a><h2>
|
<pre class="programlisting">
|
||||||
<a name="id451995"></a>BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</h2>
|
<span class="identifier">def</span><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span> <span class="identifier">foo</span><span class="special">,</span> <span class="identifier">foo_overloads</span><span class="special">());</span>
|
||||||
|
</pre>
|
||||||
|
<a name="default_arguments.boost_python_member_function_overloads"></a><h3>
|
||||||
|
<a name="id2650701"></a>
|
||||||
|
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
Objects here, objects there, objects here there everywhere. More frequently
|
Objects here, objects there, objects here there everywhere. More frequently
|
||||||
than anything else, we need to expose member functions of our classes to
|
than anything else, we need to expose member functions of our classes to
|
||||||
Python. Then again, we have the same inconveniences as before when default
|
Python. Then again, we have the same inconveniences as before when default
|
||||||
arguments or overloads with a common sequence of initial arguments come
|
arguments or overloads with a common sequence of initial arguments come into
|
||||||
into play. Another macro is provided to make this a breeze.</p>
|
play. Another macro is provided to make this a breeze.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Like <tt class="literal">BOOST_PYTHON_FUNCTION_OVERLOADS</tt>,
|
Like <code class="literal">BOOST_PYTHON_FUNCTION_OVERLOADS</code>, <code class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</code>
|
||||||
<tt class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</tt> may be used to automatically create
|
may be used to automatically create the thin wrappers for wrapping member
|
||||||
the thin wrappers for wrapping member functions. Let's have an example:</p>
|
functions. Let's have an example:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> george</span><span class="special">
|
</p>
|
||||||
{</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
void</span><span class="identifier">
|
<span class="keyword">struct</span> <span class="identifier">george</span>
|
||||||
wack_em</span><span class="special">(</span><span class="keyword">int</span><span class="identifier"> a</span><span class="special">,</span><span class="keyword"> int</span><span class="identifier"> b</span><span class="special"> =</span><span class="number"> 0</span><span class="special">,</span><span class="keyword"> char</span><span class="identifier"> c</span><span class="special"> =</span><span class="char"> 'x'</span><span class="special">)</span><span class="special">
|
<span class="special">{</span>
|
||||||
{</span><span class="comment">
|
<span class="keyword">void</span>
|
||||||
/*...*/</span><span class="special">
|
<span class="identifier">wack_em</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">b</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">c</span> <span class="special">=</span> <span class="char">'x'</span><span class="special">)</span>
|
||||||
}</span><span class="special">
|
<span class="special">{</span>
|
||||||
};</span></tt></pre>
|
<span class="comment">/*...*/</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
<span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
The macro invocation:</p>
|
The macro invocation:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">george_overloads</span><span class="special">,</span><span class="identifier"> wack_em</span><span class="special">,</span><span class="number"> 1</span><span class="special">,</span><span class="number"> 3</span><span class="special">)</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">george_overloads</span><span class="special">,</span> <span class="identifier">wack_em</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">3</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
will generate a set of thin wrappers for george's <tt class="literal">wack_em</tt> member function
|
will generate a set of thin wrappers for george's <code class="literal">wack_em</code>
|
||||||
accepting a minimum of 1 and a maximum of 3 arguments (i.e. the third and
|
member function accepting a minimum of 1 and a maximum of 3 arguments (i.e.
|
||||||
fourth macro argument). The thin wrappers are all enclosed in a class named
|
the third and fourth macro argument). The thin wrappers are all enclosed
|
||||||
<tt class="literal">george_overloads</tt> that can then be used as an argument to <tt class="literal">def(...)</tt>:</p>
|
in a class named <code class="literal">george_overloads</code> that can then be used
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"wack_em"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">george</span><span class="special">::</span><span class="identifier">wack_em</span><span class="special">,</span><span class="identifier"> george_overloads</span><span class="special">());</span></tt></pre>
|
as an argument to <code class="literal">def(...)</code>:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"wack_em"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">george</span><span class="special">::</span><span class="identifier">wack_em</span><span class="special">,</span> <span class="identifier">george_overloads</span><span class="special">());</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
See the <a href="../../../../v2/overloads.html#BOOST_PYTHON_FUNCTION_OVERLOADS-spec" target="_top">overloads reference</a>
|
See the <a href="../../../../v2/overloads.html#BOOST_PYTHON_FUNCTION_OVERLOADS-spec" target="_top">overloads
|
||||||
for details.</p>
|
reference</a> for details.
|
||||||
<a name="default_arguments.init_and_optional"></a><h2>
|
</p>
|
||||||
<a name="id452323"></a>init and optional</h2>
|
<a name="default_arguments.init_and_optional"></a><h3>
|
||||||
|
<a name="id2651031"></a>
|
||||||
|
init and optional
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
A similar facility is provided for class constructors, again, with
|
A similar facility is provided for class constructors, again, with default
|
||||||
default arguments or a sequence of overloads. Remember <tt class="literal">init<...></tt>? For example,
|
arguments or a sequence of overloads. Remember <code class="literal">init<...></code>?
|
||||||
given a class X with a constructor:</p>
|
For example, given a class X with a constructor:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">struct</span><span class="identifier"> X</span><span class="special">
|
</p>
|
||||||
{</span><span class="identifier">
|
<pre class="programlisting">
|
||||||
X</span><span class="special">(</span><span class="keyword">int</span><span class="identifier"> a</span><span class="special">,</span><span class="keyword"> char</span><span class="identifier"> b</span><span class="special"> =</span><span class="char"> 'D'</span><span class="special">,</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> c</span><span class="special"> =</span><span class="string"> "constructor"</span><span class="special">,</span><span class="keyword"> double</span><span class="identifier"> d</span><span class="special"> =</span><span class="number"> 0.0</span><span class="special">);</span><span class="comment">
|
<span class="keyword">struct</span> <span class="identifier">X</span>
|
||||||
/*...*/</span><span class="special">
|
<span class="special">{</span>
|
||||||
}</span></tt></pre>
|
<span class="identifier">X</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">b</span> <span class="special">=</span> <span class="char">'D'</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">c</span> <span class="special">=</span> <span class="string">"constructor"</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">d</span> <span class="special">=</span> <span class="number">0.0</span><span class="special">);</span>
|
||||||
|
<span class="comment">/*...*/</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
You can easily add this constructor to Boost.Python in one shot:</p>
|
You can easily add this constructor to Boost.Python in one shot:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">init</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span><span class="identifier"> optional</span><span class="special"><</span><span class="keyword">char</span><span class="special">,</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span><span class="keyword"> double</span><span class="special">></span><span class="special"> >())</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">init</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">optional</span><span class="special"><</span><span class="keyword">char</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">double</span><span class="special">></span> <span class="special">>())</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Notice the use of <tt class="literal">init<...></tt> and <tt class="literal">optional<...></tt> to signify the default
|
Notice the use of <code class="literal">init<...></code> and <code class="literal">optional<...></code>
|
||||||
(optional arguments).</p>
|
to signify the default (optional arguments).
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.auto_overloading"></a>Auto-Overloading</h3></div></div></div>
|
<a name="python.auto_overloading"></a>Auto-Overloading</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
It was mentioned in passing in the previous section that
|
It was mentioned in passing in the previous section that <code class="literal">BOOST_PYTHON_FUNCTION_OVERLOADS</code>
|
||||||
<tt class="literal">BOOST_PYTHON_FUNCTION_OVERLOADS</tt> and <tt class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</tt>
|
and <code class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</code> can also be
|
||||||
can also be used for overloaded functions and member functions with a
|
used for overloaded functions and member functions with a common sequence
|
||||||
common sequence of initial arguments. Here is an example:</p>
|
of initial arguments. Here is an example:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">void</span><span class="identifier"> foo</span><span class="special">()</span><span class="special">
|
</p>
|
||||||
{</span><span class="comment">
|
<pre class="programlisting">
|
||||||
/*...*/</span><span class="special">
|
<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span>
|
||||||
}</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
|
<span class="comment">/*...*/</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
|
||||||
void</span><span class="identifier"> foo</span><span class="special">(</span><span class="keyword">bool</span><span class="identifier"> a</span><span class="special">)</span><span class="special">
|
<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span><span class="keyword">bool</span> <span class="identifier">a</span><span class="special">)</span>
|
||||||
{</span><span class="comment">
|
<span class="special">{</span>
|
||||||
/*...*/</span><span class="special">
|
<span class="comment">/*...*/</span>
|
||||||
}</span><span class="keyword">
|
<span class="special">}</span>
|
||||||
|
|
||||||
void</span><span class="identifier"> foo</span><span class="special">(</span><span class="keyword">bool</span><span class="identifier"> a</span><span class="special">,</span><span class="keyword"> int</span><span class="identifier"> b</span><span class="special">)</span><span class="special">
|
<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span><span class="keyword">bool</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">b</span><span class="special">)</span>
|
||||||
{</span><span class="comment">
|
<span class="special">{</span>
|
||||||
/*...*/</span><span class="special">
|
<span class="comment">/*...*/</span>
|
||||||
}</span><span class="keyword">
|
<span class="special">}</span>
|
||||||
|
|
||||||
void</span><span class="identifier"> foo</span><span class="special">(</span><span class="keyword">bool</span><span class="identifier"> a</span><span class="special">,</span><span class="keyword"> int</span><span class="identifier"> b</span><span class="special">,</span><span class="keyword"> char</span><span class="identifier"> c</span><span class="special">)</span><span class="special">
|
<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span><span class="keyword">bool</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">b</span><span class="special">,</span> <span class="keyword">char</span> <span class="identifier">c</span><span class="special">)</span>
|
||||||
{</span><span class="comment">
|
<span class="special">{</span>
|
||||||
/*...*/</span><span class="special">
|
<span class="comment">/*...*/</span>
|
||||||
}</span></tt></pre>
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Like in the previous section, we can generate thin wrappers for these
|
Like in the previous section, we can generate thin wrappers for these overloaded
|
||||||
overloaded functions in one-shot:</p>
|
functions in one-shot:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">BOOST_PYTHON_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">foo_overloads</span><span class="special">,</span><span class="identifier"> foo</span><span class="special">,</span><span class="number"> 0</span><span class="special">,</span><span class="number"> 3</span><span class="special">)</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">BOOST_PYTHON_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">foo_overloads</span><span class="special">,</span> <span class="identifier">foo</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">3</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Then...</p>
|
Then...
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span><span class="identifier"> foo</span><span class="special">,</span><span class="identifier"> foo_overloads</span><span class="special">());</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span> <span class="special">(</span><span class="keyword">void</span><span class="special">(*)(</span><span class="keyword">bool</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">))</span><span class="number">0</span><span class="special">,</span> <span class="identifier">foo_overloads</span><span class="special">());</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Notice though that we have a situation now where we have a minimum of zero
|
Notice though that we have a situation now where we have a minimum of zero
|
||||||
(0) arguments and a maximum of 3 arguments.</p>
|
(0) arguments and a maximum of 3 arguments.
|
||||||
<a name="auto_overloading.manual_wrapping"></a><h2>
|
</p>
|
||||||
<a name="id452969"></a>Manual Wrapping</h2>
|
<a name="auto_overloading.manual_wrapping"></a><h3>
|
||||||
|
<a name="id2651734"></a>
|
||||||
|
Manual Wrapping
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
It is important to emphasize however that <span class="bold"><b>the overloaded functions must
|
It is important to emphasize however that <span class="bold"><strong>the overloaded
|
||||||
have a common sequence of initial arguments</b></span>. Otherwise, our scheme above
|
functions must have a common sequence of initial arguments</strong></span>. Otherwise,
|
||||||
will not work. If this is not the case, we have to wrap our functions
|
our scheme above will not work. If this is not the case, we have to wrap
|
||||||
<a href="functions.html#python.overloading" title="Overloading">manually</a>.</p>
|
our functions <a href="functions.html#python.overloading" title="Overloading">manually</a>.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Actually, we can mix and match manual wrapping of overloaded functions and
|
Actually, we can mix and match manual wrapping of overloaded functions and
|
||||||
automatic wrapping through <tt class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</tt> and
|
automatic wrapping through <code class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</code>
|
||||||
its sister, <tt class="literal">BOOST_PYTHON_FUNCTION_OVERLOADS</tt>. Following up on our example
|
and its sister, <code class="literal">BOOST_PYTHON_FUNCTION_OVERLOADS</code>. Following
|
||||||
presented in the section <a href="functions.html#python.overloading" title="Overloading">on overloading</a>, since the
|
up on our example presented in the section <a href="functions.html#python.overloading" title="Overloading">on
|
||||||
first 4 overload functins have a common sequence of initial arguments, we
|
overloading</a>, since the first 4 overload functins have a common sequence
|
||||||
can use <tt class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</tt> to automatically wrap the
|
of initial arguments, we can use <code class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</code>
|
||||||
first three of the <tt class="literal">def</tt>s and manually wrap just the last. Here's
|
to automatically wrap the first three of the <code class="literal">def</code>s and
|
||||||
how we'll do this:</p>
|
manually wrap just the last. Here's how we'll do this:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">xf_overloads</span><span class="special">,</span><span class="identifier"> f</span><span class="special">,</span><span class="number"> 1</span><span class="special">,</span><span class="number"> 4</span><span class="special">)</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">xf_overloads</span><span class="special">,</span> <span class="identifier">f</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">4</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Create a member function pointers as above for both X::f overloads:</p>
|
Create a member function pointers as above for both X::f overloads:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">bool</span><span class="special"> (</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx1</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword"> double</span><span class="special">,</span><span class="keyword"> char</span><span class="special">)</span><span class="special"> =</span><span class="special"> &</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span><span class="keyword">
|
</p>
|
||||||
int</span><span class="special"> (</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx2</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span><span class="keyword"> int</span><span class="special">,</span><span class="keyword"> int</span><span class="special">)</span><span class="special"> =</span><span class="special"> &</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">bool</span> <span class="special">(</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx1</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">,</span> <span class="keyword">char</span><span class="special">)</span> <span class="special">=</span> <span class="special">&</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span>
|
||||||
|
<span class="keyword">int</span> <span class="special">(</span><span class="identifier">X</span><span class="special">::*</span><span class="identifier">fx2</span><span class="special">)(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">=</span> <span class="special">&</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Then...</p>
|
Then...
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> fx1</span><span class="special">,</span><span class="identifier"> xf_overloads</span><span class="special">());</span><span class="special">
|
</p>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span><span class="identifier"> fx2</span><span class="special">)</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">fx1</span><span class="special">,</span> <span class="identifier">xf_overloads</span><span class="special">());</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">fx2</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||||
<td align="left"></td>
|
<td align="left"></td>
|
||||||
<td align="right"><small>Copyright © 2002-2005 Joel de Guzman, David Abrahams</small></td>
|
<td align="right"><small>Copyright © 2002-2005 Joel
|
||||||
|
de Guzman, David Abrahams</small></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="spirit-nav">
|
<div class="spirit-nav">
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<title> Building Hello World</title>
|
<title>Building Hello World</title>
|
||||||
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
|
||||||
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="prev" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="prev" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="next" href="exposing.html" title=" Exposing Classes">
|
<link rel="next" href="exposing.html" title="Exposing Classes">
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||||
<table cellpadding="2" width="100%">
|
<table cellpadding="2" width="100%">
|
||||||
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../../../../../boost.png"></td>
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
|
||||||
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
||||||
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
||||||
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
||||||
@@ -25,79 +25,92 @@
|
|||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||||||
<a name="python.hello"></a> Building Hello World</h2></div></div></div>
|
<a name="python.hello"></a> Building Hello World</h2></div></div></div>
|
||||||
<a name="hello.from_start_to_finish"></a><h2>
|
<a name="hello.from_start_to_finish"></a><h3>
|
||||||
<a name="id374047"></a>From Start To Finish</h2>
|
<a name="id2595436"></a>
|
||||||
|
From Start To Finish
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
Now the first thing you'd want to do is to build the Hello World module and
|
Now the first thing you'd want to do is to build the Hello World module and
|
||||||
try it for yourself in Python. In this section, we shall outline the steps
|
try it for yourself in Python. In this section, we shall outline the steps
|
||||||
necessary to achieve that. We shall use the build tool that comes bundled
|
necessary to achieve that. We shall use the build tool that comes bundled with
|
||||||
with every boost distribution: <span class="bold"><b>bjam</b></span>.</p>
|
every boost distribution: <span class="bold"><strong>bjam</strong></span>.
|
||||||
<div class="informaltable"><table class="table">
|
</p>
|
||||||
<colgroup><col></colgroup>
|
<div class="sidebar">
|
||||||
<tbody><tr><td class="blurb">
|
<p class="title"><b></b></p>
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span><span class="bold"><b>Building without bjam</b></span><br><br>
|
<p>
|
||||||
|
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> <span class="bold"><strong>Building without bjam</strong></span><br>
|
||||||
Besides bjam, there are of course other ways to get your module built.
|
<br> Besides bjam, there are of course other ways to get your module built.
|
||||||
What's written here should not be taken as "the one and only way".
|
What's written here should not be taken as "the one and only way".
|
||||||
There are of course other build tools apart from <tt class="literal">bjam</tt>.<br><br>
|
There are of course other build tools apart from <code class="literal">bjam</code>.<br>
|
||||||
|
<br> Take note however that the preferred build tool for Boost.Python is
|
||||||
Take note however that the preferred build tool for Boost.Python is bjam.
|
bjam. There are so many ways to set up the build incorrectly. Experience shows
|
||||||
There are so many ways to set up the build incorrectly. Experience shows
|
|
||||||
that 90% of the "I can't build Boost.Python" problems come from people
|
that 90% of the "I can't build Boost.Python" problems come from people
|
||||||
who had to use a different tool.
|
who had to use a different tool.
|
||||||
</td></tr></tbody>
|
</p>
|
||||||
</table></div>
|
</div>
|
||||||
<p>
|
<p>
|
||||||
We shall skip over the details. Our objective will be to simply create the
|
We shall skip over the details. Our objective will be to simply create the
|
||||||
hello world module and run it in Python. For a complete reference to
|
hello world module and run it in Python. For a complete reference to building
|
||||||
building Boost.Python, check out: <a href="../../../../building.html" target="_top">building.html</a>.
|
Boost.Python, check out: <a href="../../../../building.html" target="_top">building.html</a>.
|
||||||
After this brief <span class="emphasis"><em>bjam</em></span> tutorial, we should have built two DLLs:</p>
|
After this brief <span class="emphasis"><em>bjam</em></span> tutorial, we should have built two
|
||||||
|
DLLs:
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
boost_python.dll
|
boost_python.dll
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
hello.pyd
|
hello.pyd
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p>
|
<p>
|
||||||
if you are on Windows, and</p>
|
if you are on Windows, and
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
libboost_python.so
|
libboost_python.so
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
hello.so
|
hello.so
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p>
|
<p>
|
||||||
if you are on Unix.</p>
|
if you are on Unix.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
The tutorial example can be found in the directory:
|
The tutorial example can be found in the directory: <code class="literal">libs/python/example/tutorial</code>.
|
||||||
<tt class="literal">libs/python/example/tutorial</tt>. There, you can find:</p>
|
There, you can find:
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
hello.cpp
|
hello.cpp
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Jamfile
|
Jamfile
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p>
|
<p>
|
||||||
The <tt class="literal">hello.cpp</tt> file is our C++ hello world example. The <tt class="literal">Jamfile</tt> is a
|
The <code class="literal">hello.cpp</code> file is our C++ hello world example. The
|
||||||
minimalist <span class="emphasis"><em>bjam</em></span> script that builds the DLLs for us.</p>
|
<code class="literal">Jamfile</code> is a minimalist <span class="emphasis"><em>bjam</em></span> script
|
||||||
|
that builds the DLLs for us.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Before anything else, you should have the bjam executable in your boost
|
Before anything else, you should have the bjam executable in your boost directory
|
||||||
directory or somewhere in your path such that <tt class="literal">bjam</tt> can be executed in
|
or somewhere in your path such that <code class="literal">bjam</code> can be executed
|
||||||
the command line. Pre-built Boost.Jam executables are available for most
|
in the command line. Pre-built Boost.Jam executables are available for most
|
||||||
platforms. The complete list of Bjam executables can be found
|
platforms. The complete list of Bjam executables can be found <a href="http://sourceforge.net/project/showfiles.php?group_id=7586" target="_top">here</a>.
|
||||||
<a href="http://sourceforge.net/project/showfiles.php?group_id=7586" target="_top">here</a>.</p>
|
</p>
|
||||||
<a name="hello.let_s_jam_"></a><h2>
|
<a name="hello.let_s_jam_"></a><h3>
|
||||||
<a name="id372653"></a>Let's Jam!</h2>
|
<a name="id2595623"></a>
|
||||||
<p><span class="inlinemediaobject"><img src="../images/jam.png"></span></p>
|
Let's Jam!
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
Here is our minimalist Jamfile:</p>
|
<span class="inlinemediaobject"><img src="../images/jam.png" alt="jam"></span>
|
||||||
<pre class="programlisting"><tt class="literal"># This is the top of our own project tree
|
</p>
|
||||||
|
<p>
|
||||||
|
Here is our minimalist Jamfile:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting"># This is the top of our own project tree
|
||||||
project-root ;
|
project-root ;
|
||||||
|
|
||||||
import python ;
|
import python ;
|
||||||
@@ -107,85 +120,113 @@ extension hello # Declare a Python extension called hello
|
|||||||
# requirements and dependencies for Boost.Python extensions
|
# requirements and dependencies for Boost.Python extensions
|
||||||
<template>@boost/libs/python/build/extension
|
<template>@boost/libs/python/build/extension
|
||||||
;
|
;
|
||||||
</tt></pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
First, we need to specify our location. You may place your project anywhere.
|
First, we need to specify our location. You may place your project anywhere.
|
||||||
<tt class="literal">project-root</tt> allows you to do that.</p>
|
<code class="literal">project-root</code> allows you to do that.
|
||||||
<pre class="programlisting"><tt class="literal">project-root ;
|
</p>
|
||||||
</tt></pre>
|
<pre class="programlisting">project-root ;
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
By doing so, you'll need a Jamrules file. Simply copy the one in the
|
By doing so, you'll need a Jamrules file. Simply copy the one in the <a href="../../../../../example/tutorial/Jamrules" target="_top">example/tutorial directory</a>
|
||||||
<a href="../../../../../example/tutorial/Jamrules" target="_top">example/tutorial directory</a> and tweak
|
and tweak the <code class="literal">path-global BOOST_ROOT</code> to where your boost
|
||||||
the <tt class="literal">path-global BOOST_ROOT</tt> to where your boost root directory is. The file
|
root directory is. The file has <a href="../../../../../example/tutorial/Jamrules" target="_top">detailed
|
||||||
has <a href="../../../../../example/tutorial/Jamrules" target="_top">detailed instructions</a> you can follow.</p>
|
instructions</a> you can follow.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Then we will import the definitions needed by Python modules:</p>
|
Then we will import the definitions needed by Python modules:
|
||||||
<pre class="programlisting"><tt class="literal">import python ;
|
</p>
|
||||||
</tt></pre>
|
<pre class="programlisting">import python ;
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Finally we declare our <tt class="literal">hello</tt> extension:</p>
|
Finally we declare our <code class="literal">hello</code> extension:
|
||||||
<pre class="programlisting"><tt class="literal">extension hello # Declare a Python extension called hello
|
</p>
|
||||||
|
<pre class="programlisting">extension hello # Declare a Python extension called hello
|
||||||
: hello.cpp # source
|
: hello.cpp # source
|
||||||
|
|
||||||
# requirements and dependencies for Boost.Python extensions
|
# requirements and dependencies for Boost.Python extensions
|
||||||
<template>@boost/libs/python/build/extension
|
<template>@boost/libs/python/build/extension
|
||||||
;
|
;
|
||||||
</tt></pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
The last part tells BJam that we are depending on the Boost Python Library.</p>
|
The last part tells BJam that we are depending on the Boost Python Library.
|
||||||
<a name="hello.running_bjam"></a><h2>
|
</p>
|
||||||
<a name="id372775"></a>Running bjam</h2>
|
<a name="hello.running_bjam"></a><h3>
|
||||||
<p><span class="emphasis"><em>bjam</em></span> is run using your operating system's command line interpreter.</p>
|
<a name="id2595752"></a>
|
||||||
<div class="blockquote"><blockquote class="blockquote"><p>Start it up.</p></blockquote></div>
|
Running bjam
|
||||||
|
</h3>
|
||||||
<p>
|
<p>
|
||||||
Make sure that the environment is set so that we can invoke the C++
|
<span class="emphasis"><em>bjam</em></span> is run using your operating system's command line
|
||||||
compiler. With MSVC, that would mean running the <tt class="literal">Vcvars32.bat</tt> batch
|
interpreter.
|
||||||
file. For instance:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal">C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat
|
<div class="blockquote"><blockquote class="blockquote">
|
||||||
</tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Some environment variables will have to be setup for proper building of our
|
</p>
|
||||||
Python modules. Example:</p>
|
<p>
|
||||||
<pre class="programlisting"><tt class="literal">set PYTHON_ROOT=c:/dev/tools/python
|
Start it up.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
</blockquote></div>
|
||||||
|
<p>
|
||||||
|
Make sure that the environment is set so that we can invoke the C++ compiler.
|
||||||
|
With MSVC, that would mean running the <code class="literal">Vcvars32.bat</code> batch
|
||||||
|
file. For instance:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Some environment variables will have to be setup for proper building of our
|
||||||
|
Python modules. Example:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">set PYTHON_ROOT=c:/dev/tools/python
|
||||||
set PYTHON_VERSION=2.2
|
set PYTHON_VERSION=2.2
|
||||||
</tt></pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
The above assumes that the Python installation is in <tt class="literal">c:/dev/tools/python</tt>
|
The above assumes that the Python installation is in <code class="literal">c:/dev/tools/python</code>
|
||||||
and that we are using Python version 2.2. You'll have to tweak these
|
and that we are using Python version 2.2. You'll have to tweak these appropriately.
|
||||||
appropriately.</p>
|
</p>
|
||||||
<div class="informaltable"><table class="table">
|
<div class="sidebar">
|
||||||
<colgroup><col></colgroup>
|
<p class="title"><b></b></p>
|
||||||
<tbody><tr><td class="blurb">
|
|
||||||
<span class="inlinemediaobject"><img src="../images/tip.png"></span> Be sure not to include a third number, e.g. <span class="bold"><b>not</b></span> "2.2.1",
|
|
||||||
even if that's the version you have.</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
<p>
|
<p>
|
||||||
Take note that you may also do that through the Jamrules file we put in
|
<span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> Be sure not to include a third number, e.g. <span class="bold"><strong>not</strong></span> "2.2.1", even if that's the version you
|
||||||
our project as detailed above. The file
|
have.
|
||||||
has <a href="../../../../../example/tutorial/Jamrules" target="_top">detailed instructions</a> you
|
</p>
|
||||||
can follow.</p>
|
</div>
|
||||||
<p>
|
<p>
|
||||||
Now we are ready... Be sure to <tt class="literal">cd</tt> to <tt class="literal">libs/python/example/tutorial</tt>
|
Take note that you may also do that through the Jamrules file we put in our
|
||||||
where the tutorial <tt class="literal">"hello.cpp"</tt> and the <tt class="literal">"Jamfile"</tt> is situated.</p>
|
project as detailed above. The file has <a href="../../../../../example/tutorial/Jamrules" target="_top">detailed
|
||||||
|
instructions</a> you can follow.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Finally:</p>
|
Now we are ready... Be sure to <code class="literal">cd</code> to <code class="literal">libs/python/example/tutorial</code>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">bjam</span><span class="special"> -</span><span class="identifier">sTOOLS</span><span class="special">=</span><span class="identifier">vc</span><span class="special">-</span><span class="number">7</span><span class="identifier">_1</span></tt></pre>
|
where the tutorial <code class="literal">"hello.cpp"</code> and the <code class="literal">"Jamfile"</code>
|
||||||
|
is situated.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
We are again assuming that we are using Microsoft Visual C++ version 7.1. If
|
Finally:
|
||||||
not, then you will have to specify the appropriate tool. See
|
</p>
|
||||||
<a href="../../../../../../../tools/build/index.html" target="_top">Building Boost Libraries</a> for
|
<pre class="programlisting">
|
||||||
further details.</p>
|
<span class="identifier">bjam</span> <span class="special">-</span><span class="identifier">sTOOLS</span><span class="special">=</span><span class="identifier">vc</span><span class="special">-</span><span class="number">7</span><span class="identifier">_1</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
It should be building now:</p>
|
We are again assuming that we are using Microsoft Visual C++ version 7.1. If
|
||||||
<pre class="programlisting"><tt class="literal">cd C:\dev\boost\libs\python\example\tutorial
|
not, then you will have to specify the appropriate tool. See <a href="../../../../../../../tools/build/index.html" target="_top">Building
|
||||||
|
Boost Libraries</a> for further details.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
It should be building now:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">cd C:\dev\boost\libs\python\example\tutorial
|
||||||
bjam -sTOOLS=msvc
|
bjam -sTOOLS=msvc
|
||||||
...patience...
|
...patience...
|
||||||
...found 1703 targets...
|
...found 1703 targets...
|
||||||
...updating 40 targets...
|
...updating 40 targets...
|
||||||
</tt></pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
And so on... Finally:</p>
|
And so on... Finally:
|
||||||
<pre class="programlisting"><tt class="literal">Creating library bin\boost\libs\python\build\boost_python.dll\vc-7_1\debug\th
|
</p>
|
||||||
|
<pre class="programlisting">Creating library bin\boost\libs\python\build\boost_python.dll\vc-7_1\debug\th
|
||||||
reading-multi\boost_python.lib and object bin\boost\libs\python\build\boost_pyth
|
reading-multi\boost_python.lib and object bin\boost\libs\python\build\boost_pyth
|
||||||
on.dll\vc-7_1\debug\threading-multi\boost_python.exp
|
on.dll\vc-7_1\debug\threading-multi\boost_python.exp
|
||||||
vc-C++ bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.obj
|
vc-C++ bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.obj
|
||||||
@@ -195,45 +236,66 @@ al\hello.pyd\vc-7_1\debug\threading-multi\hello.lib
|
|||||||
Creating library bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.li
|
Creating library bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.li
|
||||||
b and object bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.exp
|
b and object bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.exp
|
||||||
...updated 31 targets...
|
...updated 31 targets...
|
||||||
</tt></pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
If all is well, you should now have:</p>
|
If all is well, you should now have:
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
boost_python.dll
|
boost_python.dll
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
hello.pyd
|
hello.pyd
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p>
|
<p>
|
||||||
if you are on Windows, and</p>
|
if you are on Windows, and
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
libboost_python.so
|
libboost_python.so
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
hello.so
|
hello.so
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p>
|
<p>
|
||||||
if you are on Unix.</p>
|
if you are on Unix.
|
||||||
<p><tt class="literal">boost_python.dll</tt> and <tt class="literal">hello.pyd</tt> can be found somewhere in your project's
|
</p>
|
||||||
<tt class="literal">bin</tt> directory. After a successful build, you can just link in these DLLs with
|
|
||||||
the Python interpreter. In Windows for example, you can simply put these libraries
|
|
||||||
inside the directory where the Python executable is.</p>
|
|
||||||
<p>
|
<p>
|
||||||
You may now fire up Python and run our hello module:</p>
|
<code class="literal">boost_python.dll</code> and <code class="literal">hello.pyd</code> can be
|
||||||
<p></p>
|
found somewhere in your project's <code class="literal">bin</code> directory. After a
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="keyword"> import</span><span class="identifier"> hello</span><span class="special">
|
successful build, you make it possible for the system to find boost_python.dll
|
||||||
>>></span><span class="keyword"> print</span><span class="identifier"> hello</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span><span class="identifier">
|
or libboost_python.so (usually done with LD_LIBRARY_PATH, DYLD_LIBRARY_PATH,
|
||||||
hello</span><span class="special">,</span><span class="identifier"> world</span></tt></pre>
|
or some other variable on *nix and with PATH on Windows) and for Python to
|
||||||
<p></p>
|
find the hello module (Done with PYTHONPATH on all systems.)
|
||||||
<div class="blockquote"><blockquote class="blockquote"><p><span class="bold"><b>There you go... Have fun!</b></span></p></blockquote></div>
|
</p>
|
||||||
|
<p>
|
||||||
|
You may now fire up Python and run our hello module:
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">hello</span>
|
||||||
|
<span class="special">>>></span> <span class="keyword">print</span> <span class="identifier">hello</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span>
|
||||||
|
<span class="identifier">hello</span><span class="special">,</span> <span class="identifier">world</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<div class="blockquote"><blockquote class="blockquote">
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<span class="bold"><strong>There you go... Have fun!</strong></span>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
</blockquote></div>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||||
<td align="left"></td>
|
<td align="left"></td>
|
||||||
<td align="right"><small>Copyright © 2002-2005 Joel de Guzman, David Abrahams</small></td>
|
<td align="right"><small>Copyright © 2002-2005 Joel
|
||||||
|
de Guzman, David Abrahams</small></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="spirit-nav">
|
<div class="spirit-nav">
|
||||||
|
|||||||
@@ -3,15 +3,15 @@
|
|||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<title>Iterators</title>
|
<title>Iterators</title>
|
||||||
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
|
||||||
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="prev" href="embedding.html" title="Embedding">
|
<link rel="prev" href="embedding.html" title="Embedding">
|
||||||
<link rel="next" href="exception.html" title=" Exception Translation">
|
<link rel="next" href="exception.html" title="Exception Translation">
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||||
<table cellpadding="2" width="100%">
|
<table cellpadding="2" width="100%">
|
||||||
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../../../../../boost.png"></td>
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
|
||||||
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
||||||
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
||||||
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
||||||
@@ -26,104 +26,172 @@
|
|||||||
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||||||
<a name="python.iterators"></a>Iterators</h2></div></div></div>
|
<a name="python.iterators"></a>Iterators</h2></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
In C++, and STL in particular, we see iterators everywhere. Python also has
|
In C++, and STL in particular, we see iterators everywhere. Python also has
|
||||||
iterators, but these are two very different beasts.</p>
|
iterators, but these are two very different beasts.
|
||||||
<p><span class="bold"><b>C++ iterators:</b></span></p>
|
</p>
|
||||||
|
<p>
|
||||||
|
<span class="bold"><strong>C++ iterators:</strong></span>
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
C++ has 5 type categories (random-access, bidirectional, forward, input, output)
|
C++ has 5 type categories (random-access, bidirectional, forward, input,
|
||||||
</li>
|
output)
|
||||||
|
</li>
|
||||||
<li>
|
<li>
|
||||||
There are 2 Operation categories: reposition, access
|
There are 2 Operation categories: reposition, access
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
A pair of iterators is needed to represent a (first/last) range.
|
A pair of iterators is needed to represent a (first/last) range.
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p><span class="bold"><b>Python Iterators:</b></span></p>
|
<p>
|
||||||
|
<span class="bold"><strong>Python Iterators:</strong></span>
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
1 category (forward)
|
1 category (forward)
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
1 operation category (next())
|
1 operation category (next())
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Raises StopIteration exception at end
|
Raises StopIteration exception at end
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p>
|
<p>
|
||||||
The typical Python iteration protocol: <tt class="literal"><span class="bold"><b>for y in x...</b></span></tt> is as follows:</p>
|
The typical Python iteration protocol: <code class="literal"><span class="bold"><strong>for y
|
||||||
<p></p>
|
in x...</strong></span></code> is as follows:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">iter</span><span class="special"> =</span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">__iter__</span><span class="special">()</span><span class="comment"> # get iterator
|
</p>
|
||||||
</span><span class="keyword">try</span><span class="special">:</span><span class="keyword">
|
|
||||||
while</span><span class="number"> 1</span><span class="special">:</span><span class="identifier">
|
|
||||||
y</span><span class="special"> =</span><span class="identifier"> iter</span><span class="special">.</span><span class="identifier">next</span><span class="special">()</span><span class="comment"> # get each item
|
|
||||||
</span><span class="special"> ...</span><span class="comment"> # process y
|
|
||||||
</span><span class="keyword">except</span><span class="identifier"> StopIteration</span><span class="special">:</span><span class="keyword"> pass</span><span class="comment"> # iterator exhausted
|
|
||||||
</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Boost.Python provides some mechanisms to make C++ iterators play along
|
</p>
|
||||||
nicely as Python iterators. What we need to do is to produce
|
<pre class="programlisting">
|
||||||
appropriate <tt class="computeroutput"><span class="identifier">__iter__</span></tt> function from C++ iterators that is compatible
|
<span class="identifier">iter</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">__iter__</span><span class="special">()</span> <span class="comment"># get iterator
|
||||||
with the Python iteration protocol. For example:</p>
|
</span><span class="keyword">try</span><span class="special">:</span>
|
||||||
<p></p>
|
<span class="keyword">while</span> <span class="number">1</span><span class="special">:</span>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">object</span><span class="identifier"> get_iterator</span><span class="special"> =</span><span class="identifier"> iterator</span><span class="special"><</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span><span class="special"> >();</span><span class="identifier">
|
<span class="identifier">y</span> <span class="special">=</span> <span class="identifier">iter</span><span class="special">.</span><span class="identifier">next</span><span class="special">()</span> <span class="comment"># get each item
|
||||||
object</span><span class="identifier"> iter</span><span class="special"> =</span><span class="identifier"> get_iterator</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span><span class="identifier">
|
</span> <span class="special">...</span> <span class="comment"># process y
|
||||||
object</span><span class="identifier"> first</span><span class="special"> =</span><span class="identifier"> iter</span><span class="special">.</span><span class="identifier">next</span><span class="special">();</span></tt></pre>
|
</span><span class="keyword">except</span> <span class="identifier">StopIteration</span><span class="special">:</span> <span class="keyword">pass</span> <span class="comment"># iterator exhausted
|
||||||
|
</span></pre>
|
||||||
<p>
|
<p>
|
||||||
Or for use in class_<>:</p>
|
Boost.Python provides some mechanisms to make C++ iterators play along nicely
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"__iter__"</span><span class="special">,</span><span class="identifier"> iterator</span><span class="special"><</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span><span class="special"> >())</span></tt></pre>
|
as Python iterators. What we need to do is to produce appropriate <code class="computeroutput"><span class="identifier">__iter__</span></code> function from C++ iterators that
|
||||||
<p><span class="bold"><b>range</b></span></p>
|
is compatible with the Python iteration protocol. For example:
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
We can create a Python savvy iterator using the range function:</p>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">object</span> <span class="identifier">get_iterator</span> <span class="special">=</span> <span class="identifier">iterator</span><span class="special"><</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">>();</span>
|
||||||
|
<span class="identifier">object</span> <span class="identifier">iter</span> <span class="special">=</span> <span class="identifier">get_iterator</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span>
|
||||||
|
<span class="identifier">object</span> <span class="identifier">first</span> <span class="special">=</span> <span class="identifier">iter</span><span class="special">.</span><span class="identifier">next</span><span class="special">();</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Or for use in class_<>:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"__iter__"</span><span class="special">,</span> <span class="identifier">iterator</span><span class="special"><</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">>())</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
<span class="bold"><strong>range</strong></span>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
We can create a Python savvy iterator using the range function:
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
range(start, finish)
|
range(start, finish)
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
range<Policies,Target>(start, finish)
|
range<Policies,Target>(start, finish)
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p>
|
<p>
|
||||||
Here, start/finish may be one of:</p>
|
Here, start/finish may be one of:
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
member data pointers
|
member data pointers
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
member function pointers
|
member function pointers
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
adaptable function object (use Target parameter)
|
adaptable function object (use Target parameter)
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p><span class="bold"><b>iterator</b></span></p>
|
<p>
|
||||||
|
<span class="bold"><strong>iterator</strong></span>
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc"><li>
|
<div class="itemizedlist"><ul type="disc"><li>
|
||||||
iterator<T, Policies>()
|
iterator<T, Policies>()
|
||||||
</li></ul></div>
|
</li></ul></div>
|
||||||
<p>
|
<p>
|
||||||
Given a container <tt class="literal">T</tt>, iterator is a shortcut that simply calls <tt class="literal">range</tt>
|
Given a container <code class="literal">T</code>, iterator is a shortcut that simply
|
||||||
with &T::begin, &T::end.</p>
|
calls <code class="literal">range</code> with &T::begin, &T::end.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Let's put this into action... Here's an example from some hypothetical
|
Let's put this into action... Here's an example from some hypothetical bogon
|
||||||
bogon Particle accelerator code:</p>
|
Particle accelerator code:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">f</span><span class="special"> =</span><span class="identifier"> Field</span><span class="special">()</span><span class="keyword">
|
|
||||||
for</span><span class="identifier"> x</span><span class="keyword"> in</span><span class="identifier"> f</span><span class="special">.</span><span class="identifier">pions</span><span class="special">:</span><span class="identifier">
|
|
||||||
smash</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span><span class="keyword">
|
|
||||||
for</span><span class="identifier"> y</span><span class="keyword"> in</span><span class="identifier"> f</span><span class="special">.</span><span class="identifier">bogons</span><span class="special">:</span><span class="identifier">
|
|
||||||
count</span><span class="special">(</span><span class="identifier">y</span><span class="special">)</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Now, our C++ Wrapper:</p>
|
</p>
|
||||||
<p></p>
|
<pre class="programlisting">
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">F</span><span class="special">>(</span><span class="string">"Field"</span><span class="special">)</span><span class="special">
|
<span class="identifier">f</span> <span class="special">=</span> <span class="identifier">Field</span><span class="special">()</span>
|
||||||
.</span><span class="identifier">property</span><span class="special">(</span><span class="string">"pions"</span><span class="special">,</span><span class="identifier"> range</span><span class="special">(&</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">p_begin</span><span class="special">,</span><span class="special"> &</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">p_end</span><span class="special">))</span><span class="special">
|
<span class="keyword">for</span> <span class="identifier">x</span> <span class="keyword">in</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">pions</span><span class="special">:</span>
|
||||||
.</span><span class="identifier">property</span><span class="special">(</span><span class="string">"bogons"</span><span class="special">,</span><span class="identifier"> range</span><span class="special">(&</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">b_begin</span><span class="special">,</span><span class="special"> &</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">b_end</span><span class="special">));</span></tt></pre>
|
<span class="identifier">smash</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span>
|
||||||
|
<span class="keyword">for</span> <span class="identifier">y</span> <span class="keyword">in</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">bogons</span><span class="special">:</span>
|
||||||
|
<span class="identifier">count</span><span class="special">(</span><span class="identifier">y</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Now, our C++ Wrapper:
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">F</span><span class="special">>(</span><span class="string">"Field"</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">property</span><span class="special">(</span><span class="string">"pions"</span><span class="special">,</span> <span class="identifier">range</span><span class="special">(&</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">p_begin</span><span class="special">,</span> <span class="special">&</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">p_end</span><span class="special">))</span>
|
||||||
|
<span class="special">.</span><span class="identifier">property</span><span class="special">(</span><span class="string">"bogons"</span><span class="special">,</span> <span class="identifier">range</span><span class="special">(&</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">b_begin</span><span class="special">,</span> <span class="special">&</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">b_end</span><span class="special">));</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
<span class="bold"><strong>stl_input_iterator</strong></span>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
So far, we have seen how to expose C++ iterators and ranges to Python. Sometimes
|
||||||
|
we wish to go the other way, though: we'd like to pass a Python sequence to
|
||||||
|
an STL algorithm or use it to initialize an STL container. We need to make
|
||||||
|
a Python iterator look like an STL iterator. For that, we use <code class="computeroutput"><span class="identifier">stl_input_iterator</span><span class="special"><></span></code>.
|
||||||
|
Consider how we might implement a function that exposes <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">assign</span><span class="special">()</span></code> to Python:
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">></span>
|
||||||
|
<span class="keyword">void</span> <span class="identifier">list_assign</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special"><</span><span class="identifier">T</span><span class="special">>&</span> <span class="identifier">l</span><span class="special">,</span> <span class="identifier">object</span> <span class="identifier">o</span><span class="special">)</span> <span class="special">{</span>
|
||||||
|
<span class="comment">// Turn a Python sequence into an STL input range
|
||||||
|
</span> <span class="identifier">stl_input_iterator</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">begin</span><span class="special">(</span><span class="identifier">o</span><span class="special">),</span> <span class="identifier">end</span><span class="special">;</span>
|
||||||
|
<span class="identifier">l</span><span class="special">.</span><span class="identifier">assign</span><span class="special">(</span><span class="identifier">begin</span><span class="special">,</span> <span class="identifier">end</span><span class="special">);</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
|
||||||
|
<span class="comment">// Part of the wrapper for list<int>
|
||||||
|
</span><span class="identifier">class_</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">>(</span><span class="string">"list_int"</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"assign"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">list_assign</span><span class="special"><</span><span class="keyword">int</span><span class="special">>)</span>
|
||||||
|
<span class="comment">// ...
|
||||||
|
</span> <span class="special">;</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Now in Python, we can assign any integer sequence to <code class="computeroutput"><span class="identifier">list_int</span></code>
|
||||||
|
objects:
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">x</span> <span class="special">=</span> <span class="identifier">list_int</span><span class="special">();</span>
|
||||||
|
<span class="identifier">x</span><span class="special">.</span><span class="identifier">assign</span><span class="special">([</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">])</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||||
<td align="left"></td>
|
<td align="left"></td>
|
||||||
<td align="right"><small>Copyright © 2002-2005 Joel de Guzman, David Abrahams</small></td>
|
<td align="right"><small>Copyright © 2002-2005 Joel
|
||||||
|
de Guzman, David Abrahams</small></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="spirit-nav">
|
<div class="spirit-nav">
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<title> Object Interface</title>
|
<title>Object Interface</title>
|
||||||
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
|
||||||
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="prev" href="functions.html" title="Functions">
|
<link rel="prev" href="functions.html" title="Functions">
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||||
<table cellpadding="2" width="100%">
|
<table cellpadding="2" width="100%">
|
||||||
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../../../../../boost.png"></td>
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
|
||||||
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
||||||
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
||||||
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
||||||
@@ -32,234 +32,313 @@
|
|||||||
<dt><span class="section"><a href="object.html#python.enums">Enums</a></span></dt>
|
<dt><span class="section"><a href="object.html#python.enums">Enums</a></span></dt>
|
||||||
</dl></div>
|
</dl></div>
|
||||||
<p>
|
<p>
|
||||||
Python is dynamically typed, unlike C++ which is statically typed. Python
|
Python is dynamically typed, unlike C++ which is statically typed. Python variables
|
||||||
variables may hold an integer, a float, list, dict, tuple, str, long etc.,
|
may hold an integer, a float, list, dict, tuple, str, long etc., among other
|
||||||
among other things. In the viewpoint of Boost.Python and C++, these
|
things. In the viewpoint of Boost.Python and C++, these Pythonic variables
|
||||||
Pythonic variables are just instances of class <tt class="literal">object</tt>. We shall see in
|
are just instances of class <code class="literal">object</code>. We shall see in this
|
||||||
this chapter how to deal with Python objects.</p>
|
chapter how to deal with Python objects.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
As mentioned, one of the goals of Boost.Python is to provide a
|
As mentioned, one of the goals of Boost.Python is to provide a bidirectional
|
||||||
bidirectional mapping between C++ and Python while maintaining the Python
|
mapping between C++ and Python while maintaining the Python feel. Boost.Python
|
||||||
feel. Boost.Python C++ <tt class="literal">object</tt>s are as close as possible to Python. This
|
C++ <code class="literal">object</code>s are as close as possible to Python. This should
|
||||||
should minimize the learning curve significantly.</p>
|
minimize the learning curve significantly.
|
||||||
<p><span class="inlinemediaobject"><img src="../images/python.png"></span></p>
|
</p>
|
||||||
|
<p>
|
||||||
|
<span class="inlinemediaobject"><img src="../images/python.png" alt="python"></span>
|
||||||
|
</p>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.basic_interface"></a>Basic Interface</h3></div></div></div>
|
<a name="python.basic_interface"></a>Basic Interface</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
Class <tt class="literal">object</tt> wraps <tt class="literal">PyObject*</tt>. All the intricacies of dealing with
|
Class <code class="literal">object</code> wraps <code class="literal">PyObject*</code>. All the
|
||||||
<tt class="literal">PyObject</tt>s such as managing reference counting are handled by the
|
intricacies of dealing with <code class="literal">PyObject</code>s such as managing
|
||||||
<tt class="literal">object</tt> class. C++ object interoperability is seamless. Boost.Python C++
|
reference counting are handled by the <code class="literal">object</code> class. C++
|
||||||
<tt class="literal">object</tt>s can in fact be explicitly constructed from any C++ object.</p>
|
object interoperability is seamless. Boost.Python C++ <code class="literal">object</code>s
|
||||||
|
can in fact be explicitly constructed from any C++ object.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
To illustrate, this Python code snippet:</p>
|
To illustrate, this Python code snippet:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">def</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span><span class="identifier"> y</span><span class="special">):</span><span class="keyword">
|
<p>
|
||||||
if</span><span class="special"> (</span><span class="identifier">y</span><span class="special"> ==</span><span class="string"> 'foo'</span><span class="special">):</span><span class="identifier">
|
</p>
|
||||||
x</span><span class="special">[</span><span class="number">3</span><span class="special">:</span><span class="number">7</span><span class="special">]</span><span class="special"> =</span><span class="string"> 'bar'</span><span class="keyword">
|
<pre class="programlisting">
|
||||||
else</span><span class="special">:</span><span class="identifier">
|
<span class="keyword">def</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">y</span><span class="special">):</span>
|
||||||
x</span><span class="special">.</span><span class="identifier">items</span><span class="special"> +=</span><span class="identifier"> y</span><span class="special">(</span><span class="number">3</span><span class="special">,</span><span class="identifier"> x</span><span class="special">)</span><span class="keyword">
|
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">y</span> <span class="special">==</span> <span class="string">'foo'</span><span class="special">):</span>
|
||||||
return</span><span class="identifier"> x</span><span class="keyword">
|
<span class="identifier">x</span><span class="special">[</span><span class="number">3</span><span class="special">:</span><span class="number">7</span><span class="special">]</span> <span class="special">=</span> <span class="string">'bar'</span>
|
||||||
|
<span class="keyword">else</span><span class="special">:</span>
|
||||||
|
<span class="identifier">x</span><span class="special">.</span><span class="identifier">items</span> <span class="special">+=</span> <span class="identifier">y</span><span class="special">(</span><span class="number">3</span><span class="special">,</span> <span class="identifier">x</span><span class="special">)</span>
|
||||||
|
<span class="keyword">return</span> <span class="identifier">x</span>
|
||||||
|
|
||||||
def</span><span class="identifier"> getfunc</span><span class="special">():</span><span class="keyword">
|
<span class="keyword">def</span> <span class="identifier">getfunc</span><span class="special">():</span>
|
||||||
return</span><span class="identifier"> f</span><span class="special">;</span></tt></pre>
|
<span class="keyword">return</span> <span class="identifier">f</span><span class="special">;</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Can be rewritten in C++ using Boost.Python facilities this way:</p>
|
Can be rewritten in C++ using Boost.Python facilities this way:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">object</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">object</span><span class="identifier"> x</span><span class="special">,</span><span class="identifier"> object</span><span class="identifier"> y</span><span class="special">)</span><span class="special"> {</span><span class="keyword">
|
|
||||||
if</span><span class="special"> (</span><span class="identifier">y</span><span class="special"> ==</span><span class="string"> "foo"</span><span class="special">)</span><span class="identifier">
|
|
||||||
x</span><span class="special">.</span><span class="identifier">slice</span><span class="special">(</span><span class="number">3</span><span class="special">,</span><span class="number">7</span><span class="special">)</span><span class="special"> =</span><span class="string"> "bar"</span><span class="special">;</span><span class="keyword">
|
|
||||||
else</span><span class="identifier">
|
|
||||||
x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"items"</span><span class="special">)</span><span class="special"> +=</span><span class="identifier"> y</span><span class="special">(</span><span class="number">3</span><span class="special">,</span><span class="identifier"> x</span><span class="special">);</span><span class="keyword">
|
|
||||||
return</span><span class="identifier"> x</span><span class="special">;</span><span class="special">
|
|
||||||
}</span><span class="identifier">
|
|
||||||
object</span><span class="identifier"> getfunc</span><span class="special">()</span><span class="special"> {</span><span class="keyword">
|
|
||||||
return</span><span class="identifier"> object</span><span class="special">(</span><span class="identifier">f</span><span class="special">);</span><span class="special">
|
|
||||||
}</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Apart from cosmetic differences due to the fact that we are writing the
|
</p>
|
||||||
code in C++, the look and feel should be immediately apparent to the Python
|
<pre class="programlisting">
|
||||||
coder.</p>
|
<span class="identifier">object</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">object</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">object</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">{</span>
|
||||||
|
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">y</span> <span class="special">==</span> <span class="string">"foo"</span><span class="special">)</span>
|
||||||
|
<span class="identifier">x</span><span class="special">.</span><span class="identifier">slice</span><span class="special">(</span><span class="number">3</span><span class="special">,</span><span class="number">7</span><span class="special">)</span> <span class="special">=</span> <span class="string">"bar"</span><span class="special">;</span>
|
||||||
|
<span class="keyword">else</span>
|
||||||
|
<span class="identifier">x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"items"</span><span class="special">)</span> <span class="special">+=</span> <span class="identifier">y</span><span class="special">(</span><span class="number">3</span><span class="special">,</span> <span class="identifier">x</span><span class="special">);</span>
|
||||||
|
<span class="keyword">return</span> <span class="identifier">x</span><span class="special">;</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
<span class="identifier">object</span> <span class="identifier">getfunc</span><span class="special">()</span> <span class="special">{</span>
|
||||||
|
<span class="keyword">return</span> <span class="identifier">object</span><span class="special">(</span><span class="identifier">f</span><span class="special">);</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Apart from cosmetic differences due to the fact that we are writing the code
|
||||||
|
in C++, the look and feel should be immediately apparent to the Python coder.
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.derived_object_types"></a>Derived Object types</h3></div></div></div>
|
<a name="python.derived_object_types"></a>Derived Object types</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
Boost.Python comes with a set of derived <tt class="literal">object</tt> types corresponding to
|
Boost.Python comes with a set of derived <code class="literal">object</code> types
|
||||||
that of Python's:</p>
|
corresponding to that of Python's:
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
list
|
list
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
dict
|
dict
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
tuple
|
tuple
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
str
|
str
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
long_
|
long_
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
enum
|
enum
|
||||||
</li>
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p>
|
<p>
|
||||||
These derived <tt class="literal">object</tt> types act like real Python types. For instance:</p>
|
These derived <code class="literal">object</code> types act like real Python types.
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">str</span><span class="special">(</span><span class="number">1</span><span class="special">)</span><span class="special"> ==></span><span class="string"> "1"</span></tt></pre>
|
For instance:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">str</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">==></span> <span class="string">"1"</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Wherever appropriate, a particular derived <tt class="literal">object</tt> has corresponding
|
Wherever appropriate, a particular derived <code class="literal">object</code> has
|
||||||
Python type's methods. For instance, <tt class="literal">dict</tt> has a <tt class="literal">keys()</tt> method:</p>
|
corresponding Python type's methods. For instance, <code class="literal">dict</code>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">d</span><span class="special">.</span><span class="identifier">keys</span><span class="special">()</span></tt></pre>
|
has a <code class="literal">keys()</code> method:
|
||||||
<p><tt class="literal">make_tuple</tt> is provided for declaring <span class="emphasis"><em>tuple literals</em></span>. Example:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">make_tuple</span><span class="special">(</span><span class="number">123</span><span class="special">,</span><span class="char"> 'D'</span><span class="special">,</span><span class="string"> "Hello, World"</span><span class="special">,</span><span class="number"> 0.0</span><span class="special">);</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">d</span><span class="special">.</span><span class="identifier">keys</span><span class="special">()</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
In C++, when Boost.Python <tt class="literal">object</tt>s are used as arguments to functions,
|
<code class="literal">make_tuple</code> is provided for declaring <span class="emphasis"><em>tuple literals</em></span>.
|
||||||
subtype matching is required. For example, when a function <tt class="literal">f</tt>, as
|
Example:
|
||||||
declared below, is wrapped, it will only accept instances of Python's
|
</p>
|
||||||
<tt class="literal">str</tt> type and subtypes.</p>
|
<pre class="programlisting">
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">void</span><span class="identifier"> f</span><span class="special">(</span><span class="identifier">str</span><span class="identifier"> name</span><span class="special">)</span><span class="special">
|
<span class="identifier">make_tuple</span><span class="special">(</span><span class="number">123</span><span class="special">,</span> <span class="char">'D'</span><span class="special">,</span> <span class="string">"Hello, World"</span><span class="special">,</span> <span class="number">0.0</span><span class="special">);</span>
|
||||||
{</span><span class="identifier">
|
</pre>
|
||||||
object</span><span class="identifier"> n2</span><span class="special"> =</span><span class="identifier"> name</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"upper"</span><span class="special">)();</span><span class="comment"> // NAME = name.upper()
|
|
||||||
</span><span class="identifier"> str</span><span class="identifier"> NAME</span><span class="special"> =</span><span class="identifier"> name</span><span class="special">.</span><span class="identifier">upper</span><span class="special">();</span><span class="comment"> // better
|
|
||||||
</span><span class="identifier"> object</span><span class="identifier"> msg</span><span class="special"> =</span><span class="string"> "%s is bigger than %s"</span><span class="special"> %</span><span class="identifier"> make_tuple</span><span class="special">(</span><span class="identifier">NAME</span><span class="special">,</span><span class="identifier">name</span><span class="special">);</span><span class="special">
|
|
||||||
}</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
In finer detail:</p>
|
In C++, when Boost.Python <code class="literal">object</code>s are used as arguments
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">str</span><span class="identifier"> NAME</span><span class="special"> =</span><span class="identifier"> name</span><span class="special">.</span><span class="identifier">upper</span><span class="special">();</span></tt></pre>
|
to functions, subtype matching is required. For example, when a function
|
||||||
|
<code class="literal">f</code>, as declared below, is wrapped, it will only accept
|
||||||
|
instances of Python's <code class="literal">str</code> type and subtypes.
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">void</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">name</span><span class="special">)</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="identifier">object</span> <span class="identifier">n2</span> <span class="special">=</span> <span class="identifier">name</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"upper"</span><span class="special">)();</span> <span class="comment">// NAME = name.upper()
|
||||||
|
</span> <span class="identifier">str</span> <span class="identifier">NAME</span> <span class="special">=</span> <span class="identifier">name</span><span class="special">.</span><span class="identifier">upper</span><span class="special">();</span> <span class="comment">// better
|
||||||
|
</span> <span class="identifier">object</span> <span class="identifier">msg</span> <span class="special">=</span> <span class="string">"%s is bigger than %s"</span> <span class="special">%</span> <span class="identifier">make_tuple</span><span class="special">(</span><span class="identifier">NAME</span><span class="special">,</span><span class="identifier">name</span><span class="special">);</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Illustrates that we provide versions of the str type's methods as C++
|
In finer detail:
|
||||||
member functions.</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">object</span><span class="identifier"> msg</span><span class="special"> =</span><span class="string"> "%s is bigger than %s"</span><span class="special"> %</span><span class="identifier"> make_tuple</span><span class="special">(</span><span class="identifier">NAME</span><span class="special">,</span><span class="identifier">name</span><span class="special">);</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">str</span> <span class="identifier">NAME</span> <span class="special">=</span> <span class="identifier">name</span><span class="special">.</span><span class="identifier">upper</span><span class="special">();</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Demonstrates that you can write the C++ equivalent of <tt class="literal">"format" % x,y,z</tt>
|
Illustrates that we provide versions of the str type's methods as C++ member
|
||||||
in Python, which is useful since there's no easy way to do that in std C++.</p>
|
functions.
|
||||||
<div class="informaltable"><table class="table">
|
</p>
|
||||||
<colgroup><col></colgroup>
|
<pre class="programlisting">
|
||||||
<tbody><tr><td class="blurb">
|
<span class="identifier">object</span> <span class="identifier">msg</span> <span class="special">=</span> <span class="string">"%s is bigger than %s"</span> <span class="special">%</span> <span class="identifier">make_tuple</span><span class="special">(</span><span class="identifier">NAME</span><span class="special">,</span><span class="identifier">name</span><span class="special">);</span>
|
||||||
<span class="inlinemediaobject"><img src="../images/alert.png"></span><span class="bold"><b>Beware</b></span> the common pitfall of forgetting that the constructors
|
</pre>
|
||||||
of most of Python's mutable types make copies, just as in Python.
|
|
||||||
</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
<p>
|
<p>
|
||||||
Python:
|
Demonstrates that you can write the C++ equivalent of <code class="literal">"format"
|
||||||
</p>
|
% x,y,z</code> in Python, which is useful since there's no easy way to
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> d</span><span class="special"> =</span><span class="identifier"> dict</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">__dict__</span><span class="special">)</span><span class="comment"> # copies x.__dict__
|
do that in std C++.
|
||||||
</span><span class="special">>>></span><span class="identifier"> d</span><span class="special">[</span><span class="string">'whatever'</span><span class="special">]</span><span class="comment"> # modifies the copy
|
</p>
|
||||||
</span></tt></pre>
|
<div class="sidebar">
|
||||||
|
<p class="title"><b></b></p>
|
||||||
<p>
|
<p>
|
||||||
C++:
|
<span class="inlinemediaobject"><img src="../images/alert.png" alt="alert"></span> <span class="bold"><strong>Beware</strong></span> the common pitfall
|
||||||
</p>
|
of forgetting that the constructors of most of Python's mutable types make
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">dict</span><span class="identifier"> d</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">));</span><span class="comment"> // copies x.__dict__
|
copies, just as in Python.
|
||||||
</span><span class="identifier">d</span><span class="special">[</span><span class="char">'whatever'</span><span class="special">]</span><span class="special"> =</span><span class="number"> 3</span><span class="special">;</span><span class="comment"> // modifies the copy
|
</p>
|
||||||
</span></tt></pre>
|
</div>
|
||||||
<a name="derived_object_types.class__lt_t_gt__as_objects"></a><h2>
|
|
||||||
<a name="id454735"></a>class_<T> as objects</h2>
|
|
||||||
<p>
|
<p>
|
||||||
Due to the dynamic nature of Boost.Python objects, any <tt class="literal">class_<T></tt> may
|
Python:
|
||||||
also be one of these types! The following code snippet wraps the class
|
</p>
|
||||||
(type) object.</p>
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">dict</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">__dict__</span><span class="special">)</span> <span class="comment"># copies x.__dict__
|
||||||
|
</span><span class="special">>>></span> <span class="identifier">d</span><span class="special">[</span><span class="string">'whatever'</span><span class="special">]</span> <span class="special">=</span> <span class="number">3</span> <span class="comment"># modifies the copy
|
||||||
|
</span></pre>
|
||||||
<p>
|
<p>
|
||||||
We can use this to create wrapped instances. Example:</p>
|
C++:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">object</span><span class="identifier"> vec345</span><span class="special"> =</span><span class="special"> (</span><span class="identifier">
|
</p>
|
||||||
class_</span><span class="special"><</span><span class="identifier">Vec2</span><span class="special">>(</span><span class="string">"Vec2"</span><span class="special">,</span><span class="identifier"> init</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span><span class="keyword"> double</span><span class="special">>())</span><span class="special">
|
<pre class="programlisting">
|
||||||
.</span><span class="identifier">def_readonly</span><span class="special">(</span><span class="string">"length"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">Point</span><span class="special">::</span><span class="identifier">length</span><span class="special">)</span><span class="special">
|
<span class="identifier">dict</span> <span class="identifier">d</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">));</span> <span class="comment">// copies x.__dict__
|
||||||
.</span><span class="identifier">def_readonly</span><span class="special">(</span><span class="string">"angle"</span><span class="special">,</span><span class="special"> &</span><span class="identifier">Point</span><span class="special">::</span><span class="identifier">angle</span><span class="special">)</span><span class="special">
|
</span><span class="identifier">d</span><span class="special">[</span><span class="char">'whatever'</span><span class="special">]</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span> <span class="comment">// modifies the copy
|
||||||
)(</span><span class="number">3.0</span><span class="special">,</span><span class="number"> 4.0</span><span class="special">);</span><span class="identifier">
|
</span></pre>
|
||||||
|
<a name="derived_object_types.class__lt_t_gt__as_objects"></a><h3>
|
||||||
|
<a name="id2653534"></a>
|
||||||
|
class_<T> as objects
|
||||||
|
</h3>
|
||||||
|
<p>
|
||||||
|
Due to the dynamic nature of Boost.Python objects, any <code class="literal">class_<T></code>
|
||||||
|
may also be one of these types! The following code snippet wraps the class
|
||||||
|
(type) object.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
We can use this to create wrapped instances. Example:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">object</span> <span class="identifier">vec345</span> <span class="special">=</span> <span class="special">(</span>
|
||||||
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">Vec2</span><span class="special">>(</span><span class="string">"Vec2"</span><span class="special">,</span> <span class="identifier">init</span><span class="special"><</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">>())</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def_readonly</span><span class="special">(</span><span class="string">"length"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Point</span><span class="special">::</span><span class="identifier">length</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def_readonly</span><span class="special">(</span><span class="string">"angle"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">Point</span><span class="special">::</span><span class="identifier">angle</span><span class="special">)</span>
|
||||||
|
<span class="special">)(</span><span class="number">3.0</span><span class="special">,</span> <span class="number">4.0</span><span class="special">);</span>
|
||||||
|
|
||||||
assert</span><span class="special">(</span><span class="identifier">vec345</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"length"</span><span class="special">)</span><span class="special"> ==</span><span class="number"> 5.0</span><span class="special">);</span></tt></pre>
|
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">vec345</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"length"</span><span class="special">)</span> <span class="special">==</span> <span class="number">5.0</span><span class="special">);</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.extracting_c___objects"></a>Extracting C++ objects</h3></div></div></div>
|
<a name="python.extracting_c___objects"></a>Extracting C++ objects</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
At some point, we will need to get C++ values out of object instances. This
|
At some point, we will need to get C++ values out of object instances. This
|
||||||
can be achieved with the <tt class="literal">extract<T></tt> function. Consider the following:</p>
|
can be achieved with the <code class="literal">extract<T></code> function. Consider
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">double</span><span class="identifier"> x</span><span class="special"> =</span><span class="identifier"> o</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"length"</span><span class="special">);</span><span class="comment"> // compile error
|
the following:
|
||||||
</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">double</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">o</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"length"</span><span class="special">);</span> <span class="comment">// compile error
|
||||||
|
</span></pre>
|
||||||
<p>
|
<p>
|
||||||
In the code above, we got a compiler error because Boost.Python
|
In the code above, we got a compiler error because Boost.Python <code class="literal">object</code>
|
||||||
<tt class="literal">object</tt> can't be implicitly converted to <tt class="literal">double</tt>s. Instead, what
|
can't be implicitly converted to <code class="literal">double</code>s. Instead, what
|
||||||
we wanted to do above can be achieved by writing:</p>
|
we wanted to do above can be achieved by writing:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">double</span><span class="identifier"> l</span><span class="special"> =</span><span class="identifier"> extract</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="identifier">o</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"length"</span><span class="special">));</span><span class="identifier">
|
</p>
|
||||||
Vec2</span><span class="special">&</span><span class="identifier"> v</span><span class="special"> =</span><span class="identifier"> extract</span><span class="special"><</span><span class="identifier">Vec2</span><span class="special">&>(</span><span class="identifier">o</span><span class="special">);</span><span class="identifier">
|
<pre class="programlisting">
|
||||||
assert</span><span class="special">(</span><span class="identifier">l</span><span class="special"> ==</span><span class="identifier"> v</span><span class="special">.</span><span class="identifier">length</span><span class="special">());</span></tt></pre>
|
<span class="keyword">double</span> <span class="identifier">l</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="keyword">double</span><span class="special">>(</span><span class="identifier">o</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"length"</span><span class="special">));</span>
|
||||||
|
<span class="identifier">Vec2</span><span class="special">&</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="identifier">Vec2</span><span class="special">&>(</span><span class="identifier">o</span><span class="special">);</span>
|
||||||
|
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">l</span> <span class="special">==</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">length</span><span class="special">());</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
The first line attempts to extract the "length" attribute of the Boost.Python
|
The first line attempts to extract the "length" attribute of the
|
||||||
<tt class="literal">object</tt>. The second line attempts to <span class="emphasis"><em>extract</em></span> the <tt class="literal">Vec2</tt> object from held
|
Boost.Python <code class="literal">object</code>. The second line attempts to <span class="emphasis"><em>extract</em></span>
|
||||||
by the Boost.Python <tt class="literal">object</tt>.</p>
|
the <code class="literal">Vec2</code> object from held by the Boost.Python <code class="literal">object</code>.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Take note that we said "attempt to" above. What if the Boost.Python <tt class="literal">object</tt>
|
Take note that we said "attempt to" above. What if the Boost.Python
|
||||||
does not really hold a <tt class="literal">Vec2</tt> type? This is certainly a possibility considering
|
<code class="literal">object</code> does not really hold a <code class="literal">Vec2</code>
|
||||||
the dynamic nature of Python <tt class="literal">object</tt>s. To be on the safe side, if the C++ type
|
type? This is certainly a possibility considering the dynamic nature of Python
|
||||||
can't be extracted, an appropriate exception is thrown. To avoid an exception,
|
<code class="literal">object</code>s. To be on the safe side, if the C++ type can't
|
||||||
we need to test for extractibility:</p>
|
be extracted, an appropriate exception is thrown. To avoid an exception,
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">extract</span><span class="special"><</span><span class="identifier">Vec2</span><span class="special">&></span><span class="identifier"> x</span><span class="special">(</span><span class="identifier">o</span><span class="special">);</span><span class="keyword">
|
we need to test for extractibility:
|
||||||
if</span><span class="special"> (</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">check</span><span class="special">())</span><span class="special"> {</span><span class="identifier">
|
</p>
|
||||||
Vec2</span><span class="special">&</span><span class="identifier"> v</span><span class="special"> =</span><span class="identifier"> x</span><span class="special">();</span><span class="special"> ...</span></tt></pre>
|
<pre class="programlisting">
|
||||||
<p><span class="inlinemediaobject"><img src="../images/tip.png"></span> The astute reader might have noticed that the <tt class="literal">extract<T></tt>
|
<span class="identifier">extract</span><span class="special"><</span><span class="identifier">Vec2</span><span class="special">&></span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">o</span><span class="special">);</span>
|
||||||
facility in fact solves the mutable copying problem:</p>
|
<span class="keyword">if</span> <span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">check</span><span class="special">())</span> <span class="special">{</span>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">dict</span><span class="identifier"> d</span><span class="special"> =</span><span class="identifier"> extract</span><span class="special"><</span><span class="identifier">dict</span><span class="special">>(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">));</span><span class="identifier">
|
<span class="identifier">Vec2</span><span class="special">&</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">();</span> <span class="special">...</span>
|
||||||
d</span><span class="special">[</span><span class="char">'whatever'</span><span class="special">]</span><span class="special"> =</span><span class="number"> 3</span><span class="special">;</span> #<span class="identifier"> modifies</span><span class="identifier"> x</span><span class="special">.</span><span class="identifier">__dict__</span><span class="special"> !</span></tt></pre>
|
</pre>
|
||||||
|
<p>
|
||||||
|
<span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> The astute reader might have noticed that the <code class="literal">extract<T></code>
|
||||||
|
facility in fact solves the mutable copying problem:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">dict</span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special"><</span><span class="identifier">dict</span><span class="special">>(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__dict__"</span><span class="special">));</span>
|
||||||
|
<span class="identifier">d</span><span class="special">[</span><span class="string">"whatever"</span><span class="special">]</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span> <span class="comment">// modifies x.__dict__ !
|
||||||
|
</span></pre>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.enums"></a>Enums</h3></div></div></div>
|
<a name="python.enums"></a>Enums</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
Boost.Python has a nifty facility to capture and wrap C++ enums. While
|
Boost.Python has a nifty facility to capture and wrap C++ enums. While Python
|
||||||
Python has no <tt class="literal">enum</tt> type, we'll often want to expose our C++ enums to
|
has no <code class="literal">enum</code> type, we'll often want to expose our C++ enums
|
||||||
Python as an <tt class="literal">int</tt>. Boost.Python's enum facility makes this easy while
|
to Python as an <code class="literal">int</code>. Boost.Python's enum facility makes
|
||||||
taking care of the proper conversions from Python's dynamic typing to C++'s
|
this easy while taking care of the proper conversions from Python's dynamic
|
||||||
strong static typing (in C++, ints cannot be implicitly converted to
|
typing to C++'s strong static typing (in C++, ints cannot be implicitly converted
|
||||||
enums). To illustrate, given a C++ enum:</p>
|
to enums). To illustrate, given a C++ enum:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">enum</span><span class="identifier"> choice</span><span class="special"> {</span><span class="identifier"> red</span><span class="special">,</span><span class="identifier"> blue</span><span class="special"> };</span></tt></pre>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">enum</span> <span class="identifier">choice</span> <span class="special">{</span> <span class="identifier">red</span><span class="special">,</span> <span class="identifier">blue</span> <span class="special">};</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
the construct:</p>
|
the construct:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">enum_</span><span class="special"><</span><span class="identifier">choice</span><span class="special">>(</span><span class="string">"choice"</span><span class="special">)</span><span class="special">
|
</p>
|
||||||
.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"red"</span><span class="special">,</span><span class="identifier"> red</span><span class="special">)</span><span class="special">
|
<pre class="programlisting">
|
||||||
.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"blue"</span><span class="special">,</span><span class="identifier"> blue</span><span class="special">)</span><span class="special">
|
<span class="identifier">enum_</span><span class="special"><</span><span class="identifier">choice</span><span class="special">>(</span><span class="string">"choice"</span><span class="special">)</span>
|
||||||
;</span></tt></pre>
|
<span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"red"</span><span class="special">,</span> <span class="identifier">red</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"blue"</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">)</span>
|
||||||
|
<span class="special">;</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
can be used to expose to Python. The new enum type is created in the
|
can be used to expose to Python. The new enum type is created in the current
|
||||||
current <tt class="literal">scope()</tt>, which is usually the current module. The snippet above
|
<code class="literal">scope()</code>, which is usually the current module. The snippet
|
||||||
creates a Python class derived from Python's <tt class="literal">int</tt> type which is
|
above creates a Python class derived from Python's <code class="literal">int</code>
|
||||||
associated with the C++ type passed as its first parameter.</p>
|
type which is associated with the C++ type passed as its first parameter.
|
||||||
<div class="informaltable"><table class="table">
|
</p>
|
||||||
<colgroup><col></colgroup>
|
<div class="sidebar">
|
||||||
<tbody><tr><td class="blurb">
|
<p class="title"><b></b></p>
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span><span class="bold"><b>what is a scope?</b></span><br><br>
|
|
||||||
The scope is a class that has an
|
|
||||||
associated global Python object which controls the Python namespace in
|
|
||||||
which new extension classes and wrapped functions will be defined as
|
|
||||||
attributes. Details can be found <a href="../../../../v2/scope.html" target="_top">here</a>.</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
<p>
|
<p>
|
||||||
You can access those values in Python as</p>
|
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> <span class="bold"><strong>what is a scope?</strong></span><br>
|
||||||
<p></p>
|
<br> The scope is a class that has an associated global Python object which
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="identifier"> my_module</span><span class="special">.</span><span class="identifier">choice</span><span class="special">.</span><span class="identifier">red</span><span class="identifier">
|
controls the Python namespace in which new extension classes and wrapped
|
||||||
my_module</span><span class="special">.</span><span class="identifier">choice</span><span class="special">.</span><span class="identifier">red</span></tt></pre>
|
functions will be defined as attributes. Details can be found <a href="../../../../v2/scope.html" target="_top">here</a>.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
<p>
|
<p>
|
||||||
where my_module is the module where the enum is declared. You can also
|
You can access those values in Python as
|
||||||
create a new scope around a class:</p>
|
</p>
|
||||||
<p></p>
|
<p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">scope</span><span class="identifier"> in_X</span><span class="special"> =</span><span class="identifier"> class_</span><span class="special"><</span><span class="identifier">X</span><span class="special">>(</span><span class="string">"X"</span><span class="special">)</span><span class="special">
|
</p>
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="special"> ...</span><span class="special"> )</span><span class="special">
|
<pre class="programlisting">
|
||||||
.</span><span class="identifier">def</span><span class="special">(</span><span class="special"> ...</span><span class="special"> )</span><span class="special">
|
<span class="special">>>></span> <span class="identifier">my_module</span><span class="special">.</span><span class="identifier">choice</span><span class="special">.</span><span class="identifier">red</span>
|
||||||
;</span><span class="comment">
|
<span class="identifier">my_module</span><span class="special">.</span><span class="identifier">choice</span><span class="special">.</span><span class="identifier">red</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
where my_module is the module where the enum is declared. You can also create
|
||||||
|
a new scope around a class:
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">scope</span> <span class="identifier">in_X</span> <span class="special">=</span> <span class="identifier">class_</span><span class="special"><</span><span class="identifier">X</span><span class="special">>(</span><span class="string">"X"</span><span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span> <span class="special">...</span> <span class="special">)</span>
|
||||||
|
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span> <span class="special">...</span> <span class="special">)</span>
|
||||||
|
<span class="special">;</span>
|
||||||
|
|
||||||
// Expose X::nested as X.nested
|
<span class="comment">// Expose X::nested as X.nested
|
||||||
</span><span class="identifier">enum_</span><span class="special"><</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">nested</span><span class="special">>(</span><span class="string">"nested"</span><span class="special">)</span><span class="special">
|
</span><span class="identifier">enum_</span><span class="special"><</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">nested</span><span class="special">>(</span><span class="string">"nested"</span><span class="special">)</span>
|
||||||
.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"red"</span><span class="special">,</span><span class="identifier"> red</span><span class="special">)</span><span class="special">
|
<span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"red"</span><span class="special">,</span> <span class="identifier">red</span><span class="special">)</span>
|
||||||
.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"blue"</span><span class="special">,</span><span class="identifier"> blue</span><span class="special">)</span><span class="special">
|
<span class="special">.</span><span class="identifier">value</span><span class="special">(</span><span class="string">"blue"</span><span class="special">,</span> <span class="identifier">blue</span><span class="special">)</span>
|
||||||
;</span></tt></pre>
|
<span class="special">;</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||||
<td align="left"></td>
|
<td align="left"></td>
|
||||||
<td align="right"><small>Copyright © 2002-2005 Joel de Guzman, David Abrahams</small></td>
|
<td align="right"><small>Copyright © 2002-2005 Joel
|
||||||
|
de Guzman, David Abrahams</small></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="spirit-nav">
|
<div class="spirit-nav">
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||||
<title> General Techniques</title>
|
<title>General Techniques</title>
|
||||||
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
|
||||||
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="start" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
<link rel="up" href="../index.html" title="Chapter 1. python 1.0">
|
||||||
<link rel="prev" href="exception.html" title=" Exception Translation">
|
<link rel="prev" href="exception.html" title="Exception Translation">
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||||
<table cellpadding="2" width="100%">
|
<table cellpadding="2" width="100%">
|
||||||
<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../../../../../boost.png"></td>
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
|
||||||
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
<td align="center"><a href="../../../../../../../index.htm">Home</a></td>
|
||||||
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
|
||||||
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
<td align="center"><a href="../../../../../../../people/people.htm">People</a></td>
|
||||||
@@ -30,333 +30,426 @@
|
|||||||
<dt><span class="section"><a href="techniques.html#python.reducing_compiling_time">Reducing Compiling Time</a></span></dt>
|
<dt><span class="section"><a href="techniques.html#python.reducing_compiling_time">Reducing Compiling Time</a></span></dt>
|
||||||
</dl></div>
|
</dl></div>
|
||||||
<p>
|
<p>
|
||||||
Here are presented some useful techniques that you can use while wrapping code with Boost.Python.</p>
|
Here are presented some useful techniques that you can use while wrapping code
|
||||||
|
with Boost.Python.
|
||||||
|
</p>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.creating_packages"></a>Creating Packages</h3></div></div></div>
|
<a name="python.creating_packages"></a>Creating Packages</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
A Python package is a collection of modules that provide to the user a certain
|
A Python package is a collection of modules that provide to the user a certain
|
||||||
functionality. If you're not familiar on how to create packages, a good
|
functionality. If you're not familiar on how to create packages, a good introduction
|
||||||
introduction to them is provided in the
|
to them is provided in the <a href="http://www.python.org/doc/current/tut/node8.html" target="_top">Python
|
||||||
<a href="http://www.python.org/doc/current/tut/node8.html" target="_top">Python Tutorial</a>.</p>
|
Tutorial</a>.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
But we are wrapping C++ code, using Boost.Python. How can we provide a nice
|
But we are wrapping C++ code, using Boost.Python. How can we provide a nice
|
||||||
package interface to our users? To better explain some concepts, let's work
|
package interface to our users? To better explain some concepts, let's work
|
||||||
with an example.</p>
|
with an example.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
We have a C++ library that works with sounds: reading and writing various
|
We have a C++ library that works with sounds: reading and writing various
|
||||||
formats, applying filters to the sound data, etc. It is named (conveniently)
|
formats, applying filters to the sound data, etc. It is named (conveniently)
|
||||||
<tt class="literal">sounds</tt>. Our library already has a neat C++ namespace hierarchy, like so:</p>
|
<code class="literal">sounds</code>. Our library already has a neat C++ namespace hierarchy,
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">sounds</span><span class="special">::</span><span class="identifier">core</span><span class="identifier">
|
like so:
|
||||||
sounds</span><span class="special">::</span><span class="identifier">io</span><span class="identifier">
|
</p>
|
||||||
sounds</span><span class="special">::</span><span class="identifier">filters</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">sounds</span><span class="special">::</span><span class="identifier">core</span>
|
||||||
|
<span class="identifier">sounds</span><span class="special">::</span><span class="identifier">io</span>
|
||||||
|
<span class="identifier">sounds</span><span class="special">::</span><span class="identifier">filters</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
We would like to present this same hierarchy to the Python user, allowing him
|
We would like to present this same hierarchy to the Python user, allowing
|
||||||
to write code like this:</p>
|
him to write code like this:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">import</span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="identifier">
|
</p>
|
||||||
sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">.</span><span class="identifier">echo</span><span class="special">(...)</span> #<span class="identifier"> echo</span><span class="identifier"> is</span><span class="identifier"> a</span><span class="identifier"> C</span><span class="special">++</span><span class="identifier"> function</span></tt></pre>
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
|
||||||
|
<span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">.</span><span class="identifier">echo</span><span class="special">(...)</span> <span class="comment"># echo is a C++ function
|
||||||
|
</span></pre>
|
||||||
<p>
|
<p>
|
||||||
The first step is to write the wrapping code. We have to export each module
|
The first step is to write the wrapping code. We have to export each module
|
||||||
separately with Boost.Python, like this:</p>
|
separately with Boost.Python, like this:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="comment">/* file core.cpp */</span><span class="identifier">
|
</p>
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">core</span><span class="special">)</span><span class="special">
|
<pre class="programlisting">
|
||||||
{</span><span class="comment">
|
<span class="special">/*</span> <span class="identifier">file</span> <span class="identifier">core</span><span class="special">.</span><span class="identifier">cpp</span> <span class="special">*/</span>
|
||||||
/* export everything in the sounds::core namespace */</span><span class="special">
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">core</span><span class="special">)</span>
|
||||||
...</span><span class="special">
|
<span class="special">{</span>
|
||||||
}</span><span class="comment">
|
<span class="special">/*</span> <span class="identifier">export</span> <span class="identifier">everything</span> <span class="keyword">in</span> <span class="identifier">the</span> <span class="identifier">sounds</span><span class="special">::</span><span class="identifier">core</span> <span class="identifier">namespace</span> <span class="special">*/</span>
|
||||||
|
<span class="special">...</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
|
||||||
/* file io.cpp */</span><span class="identifier">
|
<span class="special">/*</span> <span class="identifier">file</span> <span class="identifier">io</span><span class="special">.</span><span class="identifier">cpp</span> <span class="special">*/</span>
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">io</span><span class="special">)</span><span class="special">
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">io</span><span class="special">)</span>
|
||||||
{</span><span class="comment">
|
<span class="special">{</span>
|
||||||
/* export everything in the sounds::io namespace */</span><span class="special">
|
<span class="special">/*</span> <span class="identifier">export</span> <span class="identifier">everything</span> <span class="keyword">in</span> <span class="identifier">the</span> <span class="identifier">sounds</span><span class="special">::</span><span class="identifier">io</span> <span class="identifier">namespace</span> <span class="special">*/</span>
|
||||||
...</span><span class="special">
|
<span class="special">...</span>
|
||||||
}</span><span class="comment">
|
<span class="special">}</span>
|
||||||
|
|
||||||
/* file filters.cpp */</span><span class="identifier">
|
<span class="special">/*</span> <span class="identifier">file</span> <span class="identifier">filters</span><span class="special">.</span><span class="identifier">cpp</span> <span class="special">*/</span>
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">filters</span><span class="special">)</span><span class="special">
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">filters</span><span class="special">)</span>
|
||||||
{</span><span class="comment">
|
<span class="special">{</span>
|
||||||
/* export everything in the sounds::filters namespace */</span><span class="special">
|
<span class="special">/*</span> <span class="identifier">export</span> <span class="identifier">everything</span> <span class="keyword">in</span> <span class="identifier">the</span> <span class="identifier">sounds</span><span class="special">::</span><span class="identifier">filters</span> <span class="identifier">namespace</span> <span class="special">*/</span>
|
||||||
...</span><span class="special">
|
<span class="special">...</span>
|
||||||
}</span></tt></pre>
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Compiling these files will generate the following Python extensions:
|
Compiling these files will generate the following Python extensions: <code class="literal">core.pyd</code>,
|
||||||
<tt class="literal">core.pyd</tt>, <tt class="literal">io.pyd</tt> and <tt class="literal">filters.pyd</tt>.</p>
|
<code class="literal">io.pyd</code> and <code class="literal">filters.pyd</code>.
|
||||||
<div class="informaltable"><table class="table">
|
</p>
|
||||||
<colgroup><col></colgroup>
|
<div class="sidebar">
|
||||||
<tbody><tr><td class="blurb">
|
<p class="title"><b></b></p>
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span> The extension <tt class="literal">.pyd</tt> is used for python extension modules, which
|
|
||||||
are just shared libraries. Using the default for your system, like <tt class="literal">.so</tt> for
|
|
||||||
Unix and <tt class="literal">.dll</tt> for Windows, works just as well.</td></tr></tbody>
|
|
||||||
</table></div>
|
|
||||||
<p>
|
<p>
|
||||||
Now, we create this directory structure for our Python package:</p>
|
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> The extension <code class="literal">.pyd</code> is used for python
|
||||||
<pre class="programlisting"><tt class="literal">sounds/
|
extension modules, which are just shared libraries. Using the default for
|
||||||
|
your system, like <code class="literal">.so</code> for Unix and <code class="literal">.dll</code>
|
||||||
|
for Windows, works just as well.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
Now, we create this directory structure for our Python package:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">sounds/
|
||||||
__init__.py
|
__init__.py
|
||||||
core.pyd
|
core.pyd
|
||||||
filters.pyd
|
filters.pyd
|
||||||
io.pyd
|
io.pyd
|
||||||
</tt></pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
The file <tt class="literal">__init__.py</tt> is what tells Python that the directory <tt class="literal">sounds/</tt> is
|
The file <code class="literal">__init__.py</code> is what tells Python that the directory
|
||||||
actually a Python package. It can be a empty file, but can also perform some
|
<code class="literal">sounds/</code> is actually a Python package. It can be a empty
|
||||||
magic, that will be shown later.</p>
|
file, but can also perform some magic, that will be shown later.
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Now our package is ready. All the user has to do is put <tt class="literal">sounds</tt> into his
|
Now our package is ready. All the user has to do is put <code class="literal">sounds</code>
|
||||||
<a href="http://www.python.org/doc/current/tut/node8.html#SECTION008110000000000000000" target="_top">PYTHONPATH</a>
|
into his <a href="http://www.python.org/doc/current/tut/node8.html#SECTION008110000000000000000" target="_top">PYTHONPATH</a>
|
||||||
and fire up the interpreter:</p>
|
and fire up the interpreter:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="keyword"> import</span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">io</span><span class="special">
|
|
||||||
>>></span><span class="keyword"> import</span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">
|
|
||||||
>>></span><span class="identifier"> sound</span><span class="special"> =</span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">io</span><span class="special">.</span><span class="identifier">open</span><span class="special">(</span><span class="string">'file.mp3'</span><span class="special">)</span><span class="special">
|
|
||||||
>>></span><span class="identifier"> new_sound</span><span class="special"> =</span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">.</span><span class="identifier">echo</span><span class="special">(</span><span class="identifier">sound</span><span class="special">,</span><span class="number"> 1.0</span><span class="special">)</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Nice heh?</p>
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">io</span>
|
||||||
|
<span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">sound</span> <span class="special">=</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">io</span><span class="special">.</span><span class="identifier">open</span><span class="special">(</span><span class="string">'file.mp3'</span><span class="special">)</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">new_sound</span> <span class="special">=</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">.</span><span class="identifier">echo</span><span class="special">(</span><span class="identifier">sound</span><span class="special">,</span> <span class="number">1.0</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
This is the simplest way to create hierarchies of packages, but it is not very
|
Nice heh?
|
||||||
flexible. What if we want to add a <span class="emphasis"><em>pure</em></span> Python function to the filters
|
</p>
|
||||||
package, for instance, one that applies 3 filters in a sound object at once?
|
|
||||||
Sure, you can do this in C++ and export it, but why not do so in Python? You
|
|
||||||
don't have to recompile the extension modules, plus it will be easier to write
|
|
||||||
it.</p>
|
|
||||||
<p>
|
<p>
|
||||||
If we want this flexibility, we will have to complicate our package hierarchy a
|
This is the simplest way to create hierarchies of packages, but it is not
|
||||||
little. First, we will have to change the name of the extension modules:</p>
|
very flexible. What if we want to add a <span class="emphasis"><em>pure</em></span> Python
|
||||||
<p></p>
|
function to the filters package, for instance, one that applies 3 filters
|
||||||
<pre class="programlisting"><tt class="literal"><span class="comment">/* file core.cpp */</span><span class="identifier">
|
in a sound object at once? Sure, you can do this in C++ and export it, but
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_core</span><span class="special">)</span><span class="special">
|
why not do so in Python? You don't have to recompile the extension modules,
|
||||||
{</span><span class="special">
|
plus it will be easier to write it.
|
||||||
...</span><span class="comment">
|
</p>
|
||||||
/* export everything in the sounds::core namespace */</span><span class="special">
|
|
||||||
}</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Note that we added an underscore to the module name. The filename will have to
|
If we want this flexibility, we will have to complicate our package hierarchy
|
||||||
be changed to <tt class="literal">_core.pyd</tt> as well, and we do the same to the other extension modules.
|
a little. First, we will have to change the name of the extension modules:
|
||||||
Now, we change our package hierarchy like so:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal">sounds/
|
<p>
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="comment">/* file core.cpp */</span>
|
||||||
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_core</span><span class="special">)</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="special">...</span>
|
||||||
|
<span class="comment">/* export everything in the sounds::core namespace */</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
Note that we added an underscore to the module name. The filename will have
|
||||||
|
to be changed to <code class="literal">_core.pyd</code> as well, and we do the same
|
||||||
|
to the other extension modules. Now, we change our package hierarchy like
|
||||||
|
so:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">sounds/
|
||||||
__init__.py
|
__init__.py
|
||||||
core/
|
core/
|
||||||
__init__.py
|
__init__.py
|
||||||
_core.pyd
|
<span class="underline">core.pyd
|
||||||
filters/
|
filters/
|
||||||
__init__.py
|
\</span>_init__.py
|
||||||
_filters.pyd
|
<span class="underline">filters.pyd
|
||||||
io/
|
io/
|
||||||
__init__.py
|
\</span>_init__.py
|
||||||
_io.pyd
|
_io.pyd
|
||||||
</tt></pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Note that we created a directory for each extension module, and added a
|
Note that we created a directory for each extension module, and added a __init__.py
|
||||||
__init__.py to each one. But if we leave it that way, the user will have to
|
to each one. But if we leave it that way, the user will have to access the
|
||||||
access the functions in the core module with this syntax:</p>
|
functions in the core module with this syntax:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="keyword"> import</span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">core</span><span class="special">.</span><span class="identifier">_core</span><span class="special">
|
|
||||||
>>></span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">core</span><span class="special">.</span><span class="identifier">_core</span><span class="special">.</span><span class="identifier">foo</span><span class="special">(...)</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
which is not what we want. But here enters the <tt class="literal">__init__.py</tt> magic: everything
|
</p>
|
||||||
that is brought to the <tt class="literal">__init__.py</tt> namespace can be accessed directly by the
|
<pre class="programlisting">
|
||||||
user. So, all we have to do is bring the entire namespace from <tt class="literal">_core.pyd</tt>
|
<span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">core</span><span class="special">.</span><span class="identifier">_core</span>
|
||||||
to <tt class="literal">core/__init__.py</tt>. So add this line of code to <tt class="literal">sounds<span class="emphasis"><em>core</em></span>__init__.py</tt>:</p>
|
<span class="special">>>></span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">core</span><span class="special">.</span><span class="identifier">_core</span><span class="special">.</span><span class="identifier">foo</span><span class="special">(...)</span>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">from</span><span class="identifier"> _core</span><span class="keyword"> import</span><span class="special"> *</span></tt></pre>
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
We do the same for the other packages. Now the user accesses the functions and
|
which is not what we want. But here enters the <code class="literal">__init__.py</code>
|
||||||
classes in the extension modules like before:</p>
|
magic: everything that is brought to the <code class="literal">__init__.py</code> namespace
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="keyword"> import</span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">
|
can be accessed directly by the user. So, all we have to do is bring the
|
||||||
>>></span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">.</span><span class="identifier">echo</span><span class="special">(...)</span></tt></pre>
|
entire namespace from <code class="literal">_core.pyd</code> to <code class="literal">core/__init__.py</code>.
|
||||||
|
So add this line of code to <code class="literal">sounds<span class="emphasis"><em>core</em></span>__init__.py</code>:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">from</span> <span class="identifier">_core</span> <span class="keyword">import</span> <span class="special">*</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
with the additional benefit that we can easily add pure Python functions to
|
We do the same for the other packages. Now the user accesses the functions
|
||||||
any module, in a way that the user can't tell the difference between a C++
|
and classes in the extension modules like before:
|
||||||
function and a Python function. Let's add a <span class="emphasis"><em>pure</em></span> Python function,
|
</p>
|
||||||
<tt class="literal">echo_noise</tt>, to the <tt class="literal">filters</tt> package. This function applies both the
|
<pre class="programlisting">
|
||||||
<tt class="literal">echo</tt> and <tt class="literal">noise</tt> filters in sequence in the given <tt class="literal">sound</tt> object. We
|
<span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
|
||||||
create a file named <tt class="literal">sounds/filters/echo_noise.py</tt> and code our function:</p>
|
<span class="special">>>></span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">.</span><span class="identifier">echo</span><span class="special">(...)</span>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">import</span><span class="identifier"> _filters</span><span class="keyword">
|
</pre>
|
||||||
def</span><span class="identifier"> echo_noise</span><span class="special">(</span><span class="identifier">sound</span><span class="special">):</span><span class="identifier">
|
|
||||||
s</span><span class="special"> =</span><span class="identifier"> _filters</span><span class="special">.</span><span class="identifier">echo</span><span class="special">(</span><span class="identifier">sound</span><span class="special">)</span><span class="identifier">
|
|
||||||
s</span><span class="special"> =</span><span class="identifier"> _filters</span><span class="special">.</span><span class="identifier">noise</span><span class="special">(</span><span class="identifier">sound</span><span class="special">)</span><span class="keyword">
|
|
||||||
return</span><span class="identifier"> s</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Next, we add this line to <tt class="literal">sounds<span class="emphasis"><em>filters</em></span>__init__.py</tt>:</p>
|
with the additional benefit that we can easily add pure Python functions
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">from</span><span class="identifier"> echo_noise</span><span class="keyword"> import</span><span class="identifier"> echo_noise</span></tt></pre>
|
to any module, in a way that the user can't tell the difference between a
|
||||||
|
C++ function and a Python function. Let's add a <span class="emphasis"><em>pure</em></span>
|
||||||
|
Python function, <code class="literal">echo_noise</code>, to the <code class="literal">filters</code>
|
||||||
|
package. This function applies both the <code class="literal">echo</code> and <code class="literal">noise</code>
|
||||||
|
filters in sequence in the given <code class="literal">sound</code> object. We create
|
||||||
|
a file named <code class="literal">sounds/filters/echo_noise.py</code> and code our
|
||||||
|
function:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">import</span> <span class="identifier">_filters</span>
|
||||||
|
<span class="keyword">def</span> <span class="identifier">echo_noise</span><span class="special">(</span><span class="identifier">sound</span><span class="special">):</span>
|
||||||
|
<span class="identifier">s</span> <span class="special">=</span> <span class="identifier">_filters</span><span class="special">.</span><span class="identifier">echo</span><span class="special">(</span><span class="identifier">sound</span><span class="special">)</span>
|
||||||
|
<span class="identifier">s</span> <span class="special">=</span> <span class="identifier">_filters</span><span class="special">.</span><span class="identifier">noise</span><span class="special">(</span><span class="identifier">sound</span><span class="special">)</span>
|
||||||
|
<span class="keyword">return</span> <span class="identifier">s</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
And that's it. The user now accesses this function like any other function
|
Next, we add this line to <code class="literal">sounds<span class="emphasis"><em>filters</em></span>__init__.py</code>:
|
||||||
from the <tt class="literal">filters</tt> package:</p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="keyword"> import</span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">
|
<pre class="programlisting">
|
||||||
>>></span><span class="identifier"> sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">.</span><span class="identifier">echo_noise</span><span class="special">(...)</span></tt></pre>
|
<span class="keyword">from</span> <span class="identifier">echo_noise</span> <span class="keyword">import</span> <span class="identifier">echo_noise</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
And that's it. The user now accesses this function like any other function
|
||||||
|
from the <code class="literal">filters</code> package:
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="special">>>></span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span><span class="special">.</span><span class="identifier">echo_noise</span><span class="special">(...)</span>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.extending_wrapped_objects_in_python"></a>Extending Wrapped Objects in Python</h3></div></div></div>
|
<a name="python.extending_wrapped_objects_in_python"></a>Extending Wrapped Objects in Python</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
Thanks to Python's flexibility, you can easily add new methods to a class,
|
Thanks to Python's flexibility, you can easily add new methods to a class,
|
||||||
even after it was already created:</p>
|
even after it was already created:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="keyword"> class</span><span class="identifier"> C</span><span class="special">(</span><span class="identifier">object</span><span class="special">):</span><span class="keyword"> pass</span><span class="special">
|
</p>
|
||||||
>>></span><span class="special">
|
<pre class="programlisting">
|
||||||
>>></span><span class="comment"> # a regular function
|
<span class="special">>>></span> <span class="keyword">class</span> <span class="identifier">C</span><span class="special">(</span><span class="identifier">object</span><span class="special">):</span> <span class="keyword">pass</span>
|
||||||
</span><span class="special">>>></span><span class="keyword"> def</span><span class="identifier"> C_str</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span><span class="keyword"> return</span><span class="string"> 'A C instance!'</span><span class="special">
|
<span class="special">>>></span>
|
||||||
>>></span><span class="special">
|
<span class="special">>>></span> <span class="comment"># a regular function
|
||||||
>>></span><span class="comment"> # now we turn it in a member function
|
</span><span class="special">>>></span> <span class="keyword">def</span> <span class="identifier">C_str</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span> <span class="keyword">return</span> <span class="string">'A C instance!'</span>
|
||||||
</span><span class="special">>>></span><span class="identifier"> C</span><span class="special">.</span><span class="identifier">__str__</span><span class="special"> =</span><span class="identifier"> C_str</span><span class="special">
|
<span class="special">>>></span>
|
||||||
>>></span><span class="special">
|
<span class="special">>>></span> <span class="comment"># now we turn it in a member function
|
||||||
>>></span><span class="identifier"> c</span><span class="special"> =</span><span class="identifier"> C</span><span class="special">()</span><span class="special">
|
</span><span class="special">>>></span> <span class="identifier">C</span><span class="special">.</span><span class="identifier">__str__</span> <span class="special">=</span> <span class="identifier">C_str</span>
|
||||||
>>></span><span class="keyword"> print</span><span class="identifier"> c</span><span class="identifier">
|
<span class="special">>>></span>
|
||||||
A</span><span class="identifier"> C</span><span class="identifier"> instance</span><span class="special">!</span><span class="special">
|
<span class="special">>>></span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">C</span><span class="special">()</span>
|
||||||
>>></span><span class="identifier"> C_str</span><span class="special">(</span><span class="identifier">c</span><span class="special">)</span><span class="identifier">
|
<span class="special">>>></span> <span class="keyword">print</span> <span class="identifier">c</span>
|
||||||
A</span><span class="identifier"> C</span><span class="identifier"> instance</span><span class="special">!</span></tt></pre>
|
<span class="identifier">A</span> <span class="identifier">C</span> <span class="identifier">instance</span><span class="special">!</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">C_str</span><span class="special">(</span><span class="identifier">c</span><span class="special">)</span>
|
||||||
|
<span class="identifier">A</span> <span class="identifier">C</span> <span class="identifier">instance</span><span class="special">!</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Yes, Python rox. <span class="inlinemediaobject"><img src="../images/smiley.png"></span></p>
|
Yes, Python rox. <span class="inlinemediaobject"><img src="../images/smiley.png" alt="smiley"></span>
|
||||||
|
</p>
|
||||||
<p>
|
<p>
|
||||||
We can do the same with classes that were wrapped with Boost.Python. Suppose
|
We can do the same with classes that were wrapped with Boost.Python. Suppose
|
||||||
we have a class <tt class="literal">point</tt> in C++:</p>
|
we have a class <code class="literal">point</code> in C++:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">class</span><span class="identifier"> point</span><span class="special"> {...};</span><span class="identifier">
|
|
||||||
|
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_geom</span><span class="special">)</span><span class="special">
|
|
||||||
{</span><span class="identifier">
|
|
||||||
class_</span><span class="special"><</span><span class="identifier">point</span><span class="special">>(</span><span class="string">"point"</span><span class="special">)...;</span><span class="special">
|
|
||||||
}</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
If we are using the technique from the previous session,
|
</p>
|
||||||
<a href="techniques.html#python.creating_packages" title="Creating Packages">Creating Packages</a>, we can code directly
|
<pre class="programlisting">
|
||||||
into <tt class="literal">geom/__init__.py</tt>:</p>
|
<span class="keyword">class</span> <span class="identifier">point</span> <span class="special">{...};</span>
|
||||||
<p></p>
|
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">from</span><span class="identifier"> _geom</span><span class="keyword"> import</span><span class="special"> *</span><span class="comment">
|
|
||||||
|
|
||||||
# a regular function
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_geom</span><span class="special">)</span>
|
||||||
</span><span class="keyword">def</span><span class="identifier"> point_str</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span><span class="keyword">
|
<span class="special">{</span>
|
||||||
return</span><span class="identifier"> str</span><span class="special">((</span><span class="identifier">self</span><span class="special">.</span><span class="identifier">x</span><span class="special">,</span><span class="identifier"> self</span><span class="special">.</span><span class="identifier">y</span><span class="special">))</span><span class="comment">
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">point</span><span class="special">>(</span><span class="string">"point"</span><span class="special">)...;</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
If we are using the technique from the previous session, <a href="techniques.html#python.creating_packages" title="Creating Packages">Creating
|
||||||
|
Packages</a>, we can code directly into <code class="literal">geom/__init__.py</code>:
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">from</span> <span class="identifier">_geom</span> <span class="keyword">import</span> <span class="special">*</span>
|
||||||
|
|
||||||
# now we turn it into a member function
|
<span class="comment"># a regular function
|
||||||
</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">__str__</span><span class="special"> =</span><span class="identifier"> point_str</span></tt></pre>
|
</span><span class="keyword">def</span> <span class="identifier">point_str</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
|
||||||
<p><span class="bold"><b>All</b></span> point instances created from C++ will also have this member function!
|
<span class="keyword">return</span> <span class="identifier">str</span><span class="special">((</span><span class="identifier">self</span><span class="special">.</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">self</span><span class="special">.</span><span class="identifier">y</span><span class="special">))</span>
|
||||||
This technique has several advantages:</p>
|
|
||||||
|
<span class="comment"># now we turn it into a member function
|
||||||
|
</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">__str__</span> <span class="special">=</span> <span class="identifier">point_str</span>
|
||||||
|
</pre>
|
||||||
|
<p>
|
||||||
|
<span class="bold"><strong>All</strong></span> point instances created from C++ will
|
||||||
|
also have this member function! This technique has several advantages:
|
||||||
|
</p>
|
||||||
<div class="itemizedlist"><ul type="disc">
|
<div class="itemizedlist"><ul type="disc">
|
||||||
<li>
|
<li>
|
||||||
Cut down compile times to zero for these additional functions
|
Cut down compile times to zero for these additional functions
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Reduce the memory footprint to virtually zero
|
Reduce the memory footprint to virtually zero
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Minimize the need to recompile
|
Minimize the need to recompile
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
Rapid prototyping (you can move the code to C++ if required without changing the interface)
|
Rapid prototyping (you can move the code to C++ if required without changing
|
||||||
</li>
|
the interface)
|
||||||
|
</li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
<p>
|
<p>
|
||||||
You can even add a little syntactic sugar with the use of metaclasses. Let's
|
You can even add a little syntactic sugar with the use of metaclasses. Let's
|
||||||
create a special metaclass that "injects" methods in other classes.</p>
|
create a special metaclass that "injects" methods in other classes.
|
||||||
<pre class="programlisting"><tt class="literal"><span class="comment"># The one Boost.Python uses for all wrapped classes.
|
</p>
|
||||||
# You can use here any class exported by Boost instead of "point"
|
<pre class="programlisting">
|
||||||
</span><span class="identifier">BoostPythonMetaclass</span><span class="special"> =</span><span class="identifier"> point</span><span class="special">.</span><span class="identifier">__class__</span><span class="keyword">
|
<span class="comment"># The one Boost.Python uses for all wrapped classes.
|
||||||
|
</span><span class="comment"># You can use here any class exported by Boost instead of "point"
|
||||||
|
</span><span class="identifier">BoostPythonMetaclass</span> <span class="special">=</span> <span class="identifier">point</span><span class="special">.</span><span class="identifier">__class__</span>
|
||||||
|
|
||||||
class</span><span class="identifier"> injector</span><span class="special">(</span><span class="identifier">object</span><span class="special">):</span><span class="keyword">
|
<span class="keyword">class</span> <span class="identifier">injector</span><span class="special">(</span><span class="identifier">object</span><span class="special">):</span>
|
||||||
class</span><span class="identifier"> __metaclass__</span><span class="special">(</span><span class="identifier">BoostPythonMetaclass</span><span class="special">):</span><span class="keyword">
|
<span class="keyword">class</span> <span class="identifier">__metaclass__</span><span class="special">(</span><span class="identifier">BoostPythonMetaclass</span><span class="special">):</span>
|
||||||
def</span><span class="identifier"> __init__</span><span class="special">(</span><span class="identifier">self</span><span class="special">,</span><span class="identifier"> name</span><span class="special">,</span><span class="identifier"> bases</span><span class="special">,</span><span class="identifier"> dict</span><span class="special">):</span><span class="keyword">
|
<span class="keyword">def</span> <span class="identifier">__init__</span><span class="special">(</span><span class="identifier">self</span><span class="special">,</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">bases</span><span class="special">,</span> <span class="identifier">dict</span><span class="special">):</span>
|
||||||
for</span><span class="identifier"> b</span><span class="keyword"> in</span><span class="identifier"> bases</span><span class="special">:</span><span class="keyword">
|
<span class="keyword">for</span> <span class="identifier">b</span> <span class="keyword">in</span> <span class="identifier">bases</span><span class="special">:</span>
|
||||||
if</span><span class="identifier"> type</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</span><span class="keyword"> not</span><span class="keyword"> in</span><span class="special"> (</span><span class="identifier">self</span><span class="special">,</span><span class="identifier"> type</span><span class="special">):</span><span class="keyword">
|
<span class="keyword">if</span> <span class="identifier">type</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</span> <span class="keyword">not</span> <span class="keyword">in</span> <span class="special">(</span><span class="identifier">self</span><span class="special">,</span> <span class="identifier">type</span><span class="special">):</span>
|
||||||
for</span><span class="identifier"> k</span><span class="special">,</span><span class="identifier">v</span><span class="keyword"> in</span><span class="identifier"> dict</span><span class="special">.</span><span class="identifier">items</span><span class="special">():</span><span class="identifier">
|
<span class="keyword">for</span> <span class="identifier">k</span><span class="special">,</span><span class="identifier">v</span> <span class="keyword">in</span> <span class="identifier">dict</span><span class="special">.</span><span class="identifier">items</span><span class="special">():</span>
|
||||||
setattr</span><span class="special">(</span><span class="identifier">b</span><span class="special">,</span><span class="identifier">k</span><span class="special">,</span><span class="identifier">v</span><span class="special">)</span><span class="keyword">
|
<span class="identifier">setattr</span><span class="special">(</span><span class="identifier">b</span><span class="special">,</span><span class="identifier">k</span><span class="special">,</span><span class="identifier">v</span><span class="special">)</span>
|
||||||
return</span><span class="identifier"> type</span><span class="special">.</span><span class="identifier">__init__</span><span class="special">(</span><span class="identifier">self</span><span class="special">,</span><span class="identifier"> name</span><span class="special">,</span><span class="identifier"> bases</span><span class="special">,</span><span class="identifier"> dict</span><span class="special">)</span><span class="comment">
|
<span class="keyword">return</span> <span class="identifier">type</span><span class="special">.</span><span class="identifier">__init__</span><span class="special">(</span><span class="identifier">self</span><span class="special">,</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">bases</span><span class="special">,</span> <span class="identifier">dict</span><span class="special">)</span>
|
||||||
|
|
||||||
# inject some methods in the point foo
|
<span class="comment"># inject some methods in the point foo
|
||||||
</span><span class="keyword">class</span><span class="identifier"> more_point</span><span class="special">(</span><span class="identifier">injector</span><span class="special">,</span><span class="identifier"> point</span><span class="special">):</span><span class="keyword">
|
</span><span class="keyword">class</span> <span class="identifier">more_point</span><span class="special">(</span><span class="identifier">injector</span><span class="special">,</span> <span class="identifier">point</span><span class="special">):</span>
|
||||||
def</span><span class="identifier"> __repr__</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span><span class="keyword">
|
<span class="keyword">def</span> <span class="identifier">__repr__</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
|
||||||
return</span><span class="string"> 'Point(x=%s, y=%s)'</span><span class="special"> %</span><span class="special"> (</span><span class="identifier">self</span><span class="special">.</span><span class="identifier">x</span><span class="special">,</span><span class="identifier"> self</span><span class="special">.</span><span class="identifier">y</span><span class="special">)</span><span class="keyword">
|
<span class="keyword">return</span> <span class="string">'Point(x=%s, y=%s)'</span> <span class="special">%</span> <span class="special">(</span><span class="identifier">self</span><span class="special">.</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">self</span><span class="special">.</span><span class="identifier">y</span><span class="special">)</span>
|
||||||
def</span><span class="identifier"> foo</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span><span class="keyword">
|
<span class="keyword">def</span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
|
||||||
print</span><span class="string"> 'foo!'</span></tt></pre>
|
<span class="keyword">print</span> <span class="string">'foo!'</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Now let's see how it got:</p>
|
Now let's see how it got:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="special">>>></span><span class="keyword"> print</span><span class="identifier"> point</span><span class="special">()</span><span class="identifier">
|
</p>
|
||||||
Point</span><span class="special">(</span><span class="identifier">x</span><span class="special">=</span><span class="number">10</span><span class="special">,</span><span class="identifier"> y</span><span class="special">=</span><span class="number">10</span><span class="special">)</span><span class="special">
|
<pre class="programlisting">
|
||||||
>>></span><span class="identifier"> point</span><span class="special">().</span><span class="identifier">foo</span><span class="special">()</span><span class="identifier">
|
<span class="special">>>></span> <span class="keyword">print</span> <span class="identifier">point</span><span class="special">()</span>
|
||||||
foo</span><span class="special">!</span></tt></pre>
|
<span class="identifier">Point</span><span class="special">(</span><span class="identifier">x</span><span class="special">=</span><span class="number">10</span><span class="special">,</span> <span class="identifier">y</span><span class="special">=</span><span class="number">10</span><span class="special">)</span>
|
||||||
|
<span class="special">>>></span> <span class="identifier">point</span><span class="special">().</span><span class="identifier">foo</span><span class="special">()</span>
|
||||||
|
<span class="identifier">foo</span><span class="special">!</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
Another useful idea is to replace constructors with factory functions:</p>
|
Another useful idea is to replace constructors with factory functions:
|
||||||
<pre class="programlisting"><tt class="literal"><span class="identifier">_point</span><span class="special"> =</span><span class="identifier"> point</span><span class="keyword">
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="identifier">_point</span> <span class="special">=</span> <span class="identifier">point</span>
|
||||||
|
|
||||||
def</span><span class="identifier"> point</span><span class="special">(</span><span class="identifier">x</span><span class="special">=</span><span class="number">0</span><span class="special">,</span><span class="identifier"> y</span><span class="special">=</span><span class="number">0</span><span class="special">):</span><span class="keyword">
|
<span class="keyword">def</span> <span class="identifier">point</span><span class="special">(</span><span class="identifier">x</span><span class="special">=</span><span class="number">0</span><span class="special">,</span> <span class="identifier">y</span><span class="special">=</span><span class="number">0</span><span class="special">):</span>
|
||||||
return</span><span class="identifier"> _point</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span><span class="identifier"> y</span><span class="special">)</span></tt></pre>
|
<span class="keyword">return</span> <span class="identifier">_point</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">y</span><span class="special">)</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
In this simple case there is not much gained, but for constructurs with
|
In this simple case there is not much gained, but for constructurs with many
|
||||||
many overloads and/or arguments this is often a great simplification, again
|
overloads and/or arguments this is often a great simplification, again with
|
||||||
with virtually zero memory footprint and zero compile-time overhead for
|
virtually zero memory footprint and zero compile-time overhead for the keyword
|
||||||
the keyword support.</p>
|
support.
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" lang="en">
|
<div class="section" lang="en">
|
||||||
<div class="titlepage"><div><div><h3 class="title">
|
<div class="titlepage"><div><div><h3 class="title">
|
||||||
<a name="python.reducing_compiling_time"></a>Reducing Compiling Time</h3></div></div></div>
|
<a name="python.reducing_compiling_time"></a>Reducing Compiling Time</h3></div></div></div>
|
||||||
<p>
|
<p>
|
||||||
If you have ever exported a lot of classes, you know that it takes quite a good
|
If you have ever exported a lot of classes, you know that it takes quite
|
||||||
time to compile the Boost.Python wrappers. Plus the memory consumption can
|
a good time to compile the Boost.Python wrappers. Plus the memory consumption
|
||||||
easily become too high. If this is causing you problems, you can split the
|
can easily become too high. If this is causing you problems, you can split
|
||||||
class_ definitions in multiple files:</p>
|
the class_ definitions in multiple files:
|
||||||
<p></p>
|
</p>
|
||||||
<pre class="programlisting"><tt class="literal"><span class="comment">/* file point.cpp */</span><span class="preprocessor">
|
|
||||||
#include</span><span class="special"> <</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">h</span><span class="special">></span><span class="preprocessor">
|
|
||||||
#include</span><span class="special"> <</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span><span class="keyword">
|
|
||||||
|
|
||||||
void</span><span class="identifier"> export_point</span><span class="special">()</span><span class="special">
|
|
||||||
{</span><span class="identifier">
|
|
||||||
class_</span><span class="special"><</span><span class="identifier">point</span><span class="special">>(</span><span class="string">"point"</span><span class="special">)...;</span><span class="special">
|
|
||||||
}</span><span class="comment">
|
|
||||||
|
|
||||||
/* file triangle.cpp */</span><span class="preprocessor">
|
|
||||||
#include</span><span class="special"> <</span><span class="identifier">triangle</span><span class="special">.</span><span class="identifier">h</span><span class="special">></span><span class="preprocessor">
|
|
||||||
#include</span><span class="special"> <</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span><span class="keyword">
|
|
||||||
|
|
||||||
void</span><span class="identifier"> export_triangle</span><span class="special">()</span><span class="special">
|
|
||||||
{</span><span class="identifier">
|
|
||||||
class_</span><span class="special"><</span><span class="identifier">triangle</span><span class="special">>(</span><span class="string">"triangle"</span><span class="special">)...;</span><span class="special">
|
|
||||||
}</span></tt></pre>
|
|
||||||
<p>
|
<p>
|
||||||
Now you create a file <tt class="literal">main.cpp</tt>, which contains the <tt class="literal">BOOST_PYTHON_MODULE</tt>
|
</p>
|
||||||
macro, and call the various export functions inside it.</p>
|
<pre class="programlisting">
|
||||||
<pre class="programlisting"><tt class="literal"><span class="keyword">void</span><span class="identifier"> export_point</span><span class="special">();</span><span class="keyword">
|
<span class="comment">/* file point.cpp */</span>
|
||||||
void</span><span class="identifier"> export_triangle</span><span class="special">();</span><span class="identifier">
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">h</span><span class="special">></span>
|
||||||
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||||||
|
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_geom</span><span class="special">)</span><span class="special">
|
<span class="keyword">void</span> <span class="identifier">export_point</span><span class="special">()</span>
|
||||||
{</span><span class="identifier">
|
<span class="special">{</span>
|
||||||
export_point</span><span class="special">();</span><span class="identifier">
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">point</span><span class="special">>(</span><span class="string">"point"</span><span class="special">)...;</span>
|
||||||
export_triangle</span><span class="special">();</span><span class="special">
|
<span class="special">}</span>
|
||||||
}</span></tt></pre>
|
|
||||||
<p>
|
|
||||||
Compiling and linking together all this files produces the same result as the
|
|
||||||
usual approach:</p>
|
|
||||||
<pre class="programlisting"><tt class="literal"><span class="preprocessor">#include</span><span class="special"> <</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span><span class="preprocessor">
|
|
||||||
#include</span><span class="special"> <</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">h</span><span class="special">></span><span class="preprocessor">
|
|
||||||
#include</span><span class="special"> <</span><span class="identifier">triangle</span><span class="special">.</span><span class="identifier">h</span><span class="special">></span><span class="identifier">
|
|
||||||
|
|
||||||
BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_geom</span><span class="special">)</span><span class="special">
|
<span class="comment">/* file triangle.cpp */</span>
|
||||||
{</span><span class="identifier">
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">triangle</span><span class="special">.</span><span class="identifier">h</span><span class="special">></span>
|
||||||
class_</span><span class="special"><</span><span class="identifier">point</span><span class="special">>(</span><span class="string">"point"</span><span class="special">)...;</span><span class="identifier">
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||||||
class_</span><span class="special"><</span><span class="identifier">triangle</span><span class="special">>(</span><span class="string">"triangle"</span><span class="special">)...;</span><span class="special">
|
|
||||||
}</span></tt></pre>
|
<span class="keyword">void</span> <span class="identifier">export_triangle</span><span class="special">()</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">triangle</span><span class="special">>(</span><span class="string">"triangle"</span><span class="special">)...;</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
but the memory is kept under control.</p>
|
Now you create a file <code class="literal">main.cpp</code>, which contains the <code class="literal">BOOST_PYTHON_MODULE</code>
|
||||||
|
macro, and call the various export functions inside it.
|
||||||
|
</p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
<span class="keyword">void</span> <span class="identifier">export_point</span><span class="special">();</span>
|
||||||
|
<span class="keyword">void</span> <span class="identifier">export_triangle</span><span class="special">();</span>
|
||||||
|
|
||||||
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_geom</span><span class="special">)</span>
|
||||||
|
<span class="special">{</span>
|
||||||
|
<span class="identifier">export_point</span><span class="special">();</span>
|
||||||
|
<span class="identifier">export_triangle</span><span class="special">();</span>
|
||||||
|
<span class="special">}</span>
|
||||||
|
</pre>
|
||||||
<p>
|
<p>
|
||||||
This method is recommended too if you are developing the C++ library and
|
Compiling and linking together all this files produces the same result as
|
||||||
exporting it to Python at the same time: changes in a class will only demand
|
the usual approach:
|
||||||
the compilation of a single cpp, instead of the entire wrapper code.</p>
|
</p>
|
||||||
<div class="informaltable"><table class="table">
|
<pre class="programlisting">
|
||||||
<colgroup><col></colgroup>
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||||||
<tbody><tr><td class="blurb">
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">h</span><span class="special">></span>
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span> If you're exporting your classes with <a href="../../../../../pyste/index.html" target="_top">Pyste</a>,
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">triangle</span><span class="special">.</span><span class="identifier">h</span><span class="special">></span>
|
||||||
take a look at the <tt class="literal">--multiple</tt> option, that generates the wrappers in
|
|
||||||
various files as demonstrated here.</td></tr></tbody>
|
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_geom</span><span class="special">)</span>
|
||||||
</table></div>
|
<span class="special">{</span>
|
||||||
<div class="informaltable"><table class="table">
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">point</span><span class="special">>(</span><span class="string">"point"</span><span class="special">)...;</span>
|
||||||
<colgroup><col></colgroup>
|
<span class="identifier">class_</span><span class="special"><</span><span class="identifier">triangle</span><span class="special">>(</span><span class="string">"triangle"</span><span class="special">)...;</span>
|
||||||
<tbody><tr><td class="blurb">
|
<span class="special">}</span>
|
||||||
<span class="inlinemediaobject"><img src="../images/note.png"></span> This method is useful too if you are getting the error message
|
</pre>
|
||||||
<span class="emphasis"><em>"fatal error C1204:Compiler limit:internal structure overflow"</em></span> when compiling
|
<p>
|
||||||
a large source file, as explained in the <a href="../../../../v2/faq.html#c1204" target="_top">FAQ</a>.</td></tr></tbody>
|
but the memory is kept under control.
|
||||||
</table></div>
|
</p>
|
||||||
|
<p>
|
||||||
|
This method is recommended too if you are developing the C++ library and
|
||||||
|
exporting it to Python at the same time: changes in a class will only demand
|
||||||
|
the compilation of a single cpp, instead of the entire wrapper code.
|
||||||
|
</p>
|
||||||
|
<div class="sidebar">
|
||||||
|
<p class="title"><b></b></p>
|
||||||
|
<p>
|
||||||
|
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> If you're exporting your classes with <a href="../../../../../pyste/index.html" target="_top">Pyste</a>,
|
||||||
|
take a look at the <code class="literal">--multiple</code> option, that generates the
|
||||||
|
wrappers in various files as demonstrated here.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="sidebar">
|
||||||
|
<p class="title"><b></b></p>
|
||||||
|
<p>
|
||||||
|
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> This method is useful too if you are getting the error
|
||||||
|
message <span class="emphasis"><em>"fatal error C1204:Compiler limit:internal structure
|
||||||
|
overflow"</em></span> when compiling a large source file, as explained
|
||||||
|
in the <a href="../../../../v2/faq.html#c1204" target="_top">FAQ</a>.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||||
<td align="left"></td>
|
<td align="left"></td>
|
||||||
<td align="right"><small>Copyright © 2002-2005 Joel de Guzman, David Abrahams</small></td>
|
<td align="right"><small>Copyright © 2002-2005 Joel
|
||||||
|
de Guzman, David Abrahams</small></td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
<hr>
|
<hr>
|
||||||
<div class="spirit-nav">
|
<div class="spirit-nav">
|
||||||
|
|||||||
@@ -252,9 +252,10 @@ if you are on Windows, and
|
|||||||
if you are on Unix.
|
if you are on Unix.
|
||||||
|
|
||||||
[^boost_python.dll] and [^hello.pyd] can be found somewhere in your project's
|
[^boost_python.dll] and [^hello.pyd] can be found somewhere in your project's
|
||||||
[^bin] directory. After a successful build, you can just link in these DLLs with
|
[^bin] directory. After a successful build, you make it possible for the system
|
||||||
the Python interpreter. In Windows for example, you can simply put these libraries
|
to find boost_python.dll or libboost_python.so (usually done with LD_LIBRARY_PATH,
|
||||||
inside the directory where the Python executable is.
|
DYLD_LIBRARY_PATH, or some other variable on *nix and with PATH on Windows) and
|
||||||
|
for Python to find the hello module (Done with PYTHONPATH on all systems.)
|
||||||
|
|
||||||
You may now fire up Python and run our hello module:
|
You may now fire up Python and run our hello module:
|
||||||
|
|
||||||
@@ -1099,7 +1100,7 @@ overloaded functions in one-shot:
|
|||||||
|
|
||||||
Then...
|
Then...
|
||||||
|
|
||||||
.def("foo", foo, foo_overloads());
|
.def("foo", (void(*)(bool, int, char))0, foo_overloads());
|
||||||
|
|
||||||
Notice though that we have a situation now where we have a minimum of zero
|
Notice though that we have a situation now where we have a minimum of zero
|
||||||
(0) arguments and a maximum of 3 arguments.
|
(0) arguments and a maximum of 3 arguments.
|
||||||
@@ -1249,7 +1250,7 @@ Python:
|
|||||||
[python]
|
[python]
|
||||||
|
|
||||||
>>> d = dict(x.__dict__) # copies x.__dict__
|
>>> d = dict(x.__dict__) # copies x.__dict__
|
||||||
>>> d['whatever'] # modifies the copy
|
>>> d['whatever'] = 3 # modifies the copy
|
||||||
|
|
||||||
C++:
|
C++:
|
||||||
[c++]
|
[c++]
|
||||||
@@ -1307,7 +1308,7 @@ __tip__ The astute reader might have noticed that the [^extract<T>]
|
|||||||
facility in fact solves the mutable copying problem:
|
facility in fact solves the mutable copying problem:
|
||||||
|
|
||||||
dict d = extract<dict>(x.attr("__dict__"));
|
dict d = extract<dict>(x.attr("__dict__"));
|
||||||
d['whatever'] = 3; # modifies x.__dict__ !
|
d["whatever"] = 3; // modifies x.__dict__ !
|
||||||
|
|
||||||
|
|
||||||
[endsect]
|
[endsect]
|
||||||
@@ -1364,11 +1365,6 @@ create a new scope around a class:
|
|||||||
|
|
||||||
[def Py_Initialize [@http://www.python.org/doc/current/api/initialization.html#l2h-652 Py_Initialize]]
|
[def Py_Initialize [@http://www.python.org/doc/current/api/initialization.html#l2h-652 Py_Initialize]]
|
||||||
[def Py_Finalize [@http://www.python.org/doc/current/api/initialization.html#l2h-656 Py_Finalize]]
|
[def Py_Finalize [@http://www.python.org/doc/current/api/initialization.html#l2h-656 Py_Finalize]]
|
||||||
[def PyRun_String [@http://www.python.org/doc/current/api/veryhigh.html#l2h-55 PyRun_String]]
|
|
||||||
[def PyRun_File [@http://www.python.org/doc/current/api/veryhigh.html#l2h-56 PyRun_File]]
|
|
||||||
[def Py_eval_input [@http://www.python.org/doc/current/api/veryhigh.html#l2h-58 Py_eval_input]]
|
|
||||||
[def Py_file_input [@http://www.python.org/doc/current/api/veryhigh.html#l2h-59 Py_file_input]]
|
|
||||||
[def Py_single_input [@http://www.python.org/doc/current/api/veryhigh.html#l2h-60 Py_single_input]]
|
|
||||||
[def Py_XINCREF [@http://www.python.org/doc/current/api/countingRefs.html#l2h-65 Py_XINCREF]]
|
[def Py_XINCREF [@http://www.python.org/doc/current/api/countingRefs.html#l2h-65 Py_XINCREF]]
|
||||||
[def Py_XDECREF [@http://www.python.org/doc/current/api/countingRefs.html#l2h-67 Py_XDECREF]]
|
[def Py_XDECREF [@http://www.python.org/doc/current/api/countingRefs.html#l2h-67 Py_XDECREF]]
|
||||||
[def PyImport_AppendInittab [@http://www.python.org/doc/current/api/importing.html#l2h-137 PyImport_AppendInittab]]
|
[def PyImport_AppendInittab [@http://www.python.org/doc/current/api/importing.html#l2h-137 PyImport_AppendInittab]]
|
||||||
@@ -1395,17 +1391,17 @@ all. So stay tuned... :-)
|
|||||||
|
|
||||||
[h2 Building embedded programs]
|
[h2 Building embedded programs]
|
||||||
|
|
||||||
To be able to use embedding in your programs, they have to be linked to
|
To be able to embed python into your programs, you have to link to
|
||||||
both Boost.Python's and Python's static link library.
|
both Boost.Python's as well as Python's own runtime library.
|
||||||
|
|
||||||
Boost.Python's static link library comes in two variants. Both are located
|
Boost.Python's library comes in two variants. Both are located
|
||||||
in Boost's [^/libs/python/build/bin-stage] subdirectory. On Windows, the
|
in Boost's [^/libs/python/build/bin-stage] subdirectory. On Windows, the
|
||||||
variants are called [^boost_python.lib] (for release builds) and
|
variants are called [^boost_python.lib] (for release builds) and
|
||||||
[^boost_python_debug.lib] (for debugging). If you can't find the libraries,
|
[^boost_python_debug.lib] (for debugging). If you can't find the libraries,
|
||||||
you probably haven't built Boost.Python yet. See
|
you probably haven't built Boost.Python yet. See
|
||||||
[@../../../building.html Building and Testing] on how to do this.
|
[@../../../building.html Building and Testing] on how to do this.
|
||||||
|
|
||||||
Python's static link library can be found in the [^/libs] subdirectory of
|
Python's library can be found in the [^/libs] subdirectory of
|
||||||
your Python directory. On Windows it is called pythonXY.lib where X.Y is
|
your Python directory. On Windows it is called pythonXY.lib where X.Y is
|
||||||
your major Python version number.
|
your major Python version number.
|
||||||
|
|
||||||
@@ -1443,7 +1439,11 @@ steps:
|
|||||||
|
|
||||||
# Call other Python C API routines to use the interpreter.\n\n
|
# Call other Python C API routines to use the interpreter.\n\n
|
||||||
|
|
||||||
# Call Py_Finalize() to stop the interpreter and release its resources.
|
[/ # Call Py_Finalize() to stop the interpreter and release its resources.]
|
||||||
|
|
||||||
|
[blurb __note__ [*Note that at this time you must not call Py_Finalize() to stop the
|
||||||
|
interpreter. This may be fixed in a future version of boost.python.]
|
||||||
|
]
|
||||||
|
|
||||||
(Of course, there can be other C++ code between all of these steps.)
|
(Of course, there can be other C++ code between all of these steps.)
|
||||||
|
|
||||||
@@ -1460,171 +1460,76 @@ messy and especially hard to get right in the presence of C++ exceptions.
|
|||||||
Fortunately Boost.Python provides the [@../../../v2/handle.html handle] and
|
Fortunately Boost.Python provides the [@../../../v2/handle.html handle] and
|
||||||
[@../../../v2/object.html object] class templates to automate the process.
|
[@../../../v2/object.html object] class templates to automate the process.
|
||||||
|
|
||||||
[h2 Reference-counting handles and objects]
|
|
||||||
|
|
||||||
There are two ways in which a function in the Python/C API can return a
|
|
||||||
[^PyObject*]: as a ['borrowed reference] or as a ['new reference]. Which of
|
|
||||||
these a function uses, is listed in that function's documentation. The two
|
|
||||||
require slightely different approaches to reference-counting but both can
|
|
||||||
be 'handled' by Boost.Python.
|
|
||||||
|
|
||||||
For a function returning a ['borrowed reference] we'll have to tell the
|
|
||||||
[^handle] that the [^PyObject*] is borrowed with the aptly named
|
|
||||||
[@../../../v2/handle.html#borrowed-spec borrowed] function. Two functions
|
|
||||||
returning borrowed references are PyImport_AddModule and PyModule_GetDict.
|
|
||||||
The former returns a reference to an already imported module, the latter
|
|
||||||
retrieves a module's namespace dictionary. Let's use them to retrieve the
|
|
||||||
namespace of the [^__main__] module:
|
|
||||||
|
|
||||||
object main_module((
|
|
||||||
handle<>(borrowed(PyImport_AddModule("__main__")))));
|
|
||||||
|
|
||||||
object main_namespace = main_module.attr("__dict__");
|
|
||||||
|
|
||||||
For a function returning a ['new reference] we can just create a [^handle]
|
|
||||||
out of the raw [^PyObject*] without wrapping it in a call to borrowed. One
|
|
||||||
such function that returns a new reference is PyRun_String which we'll
|
|
||||||
discuss in the next section.
|
|
||||||
|
|
||||||
[blurb __note__ [*Handle is a class ['template], so why haven't we been using any template parameters?]\n
|
|
||||||
\n
|
|
||||||
[^handle] has a single template parameter specifying the type of the managed object. This type is [^PyObject] 99% of the time, so the parameter was defaulted to [^PyObject] for convenience. Therefore we can use the shorthand [^handle<>] instead of the longer, but equivalent, [^handle<PyObject>].
|
|
||||||
]
|
|
||||||
|
|
||||||
[h2 Running Python code]
|
[h2 Running Python code]
|
||||||
|
|
||||||
To run Python code from C++ there is a family of functions in the API
|
Boost.python provides three related functions to run Python code from C++.
|
||||||
starting with the PyRun prefix. You can find the full list of these
|
|
||||||
functions [@http://www.python.org/doc/current/api/veryhigh.html here]. They
|
|
||||||
all work similarly so we will look at only one of them, namely:
|
|
||||||
|
|
||||||
PyObject* PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
|
object eval(str expression, object globals = object(), object locals = object())
|
||||||
|
object exec(str code, object globals = object(), object locals = object())
|
||||||
|
object exec_file(str filename, object globals = object(), object locals = object())
|
||||||
|
|
||||||
PyRun_String takes the code to execute as a null-terminated (C-style)
|
eval evaluates the given expression and returns the resulting value.
|
||||||
string in its [^str] parameter. The function returns a new reference to a
|
exec executes the given code (typically a set of statements) returning the result,
|
||||||
Python object. Which object is returned depends on the [^start] paramater.
|
and exec_file executes the code contained in the given file.
|
||||||
|
|
||||||
The [^start] parameter is the start symbol from the Python grammar to use
|
The [^globals] and [^locals] parameters are Python dictionaries
|
||||||
for interpreting the code. The possible values are:
|
|
||||||
|
|
||||||
[table Start symbols
|
|
||||||
|
|
||||||
[[Py_eval_input] [for interpreting isolated expressions]]
|
|
||||||
[[Py_file_input] [for interpreting sequences of statements]]
|
|
||||||
[[Py_single_input] [for interpreting a single statement]]
|
|
||||||
]
|
|
||||||
|
|
||||||
When using Py_eval_input, the input string must contain a single expression
|
|
||||||
and its result is returned. When using Py_file_input, the string can
|
|
||||||
contain an abitrary number of statements and None is returned.
|
|
||||||
Py_single_input works in the same way as Py_file_input but only accepts a
|
|
||||||
single statement.
|
|
||||||
|
|
||||||
Lastly, the [^globals] and [^locals] parameters are Python dictionaries
|
|
||||||
containing the globals and locals of the context in which to run the code.
|
containing the globals and locals of the context in which to run the code.
|
||||||
For most intents and purposes you can use the namespace dictionary of the
|
For most intents and purposes you can use the namespace dictionary of the
|
||||||
[^__main__] module for both parameters.
|
[^__main__] module for both parameters.
|
||||||
|
|
||||||
We have already seen how to get the [^__main__] module's namespace so let's
|
Boost.python provides a function to import a module:
|
||||||
run some Python code in it:
|
|
||||||
|
|
||||||
object main_module((
|
object import(str name)
|
||||||
handle<>(borrowed(PyImport_AddModule("__main__")))));
|
|
||||||
|
|
||||||
|
import imports a python module (potentially loading it into the running process
|
||||||
|
first), and returns it.
|
||||||
|
|
||||||
|
Let's import the [^__main__] module and run some Python code in its namespace:
|
||||||
|
|
||||||
|
object main_module = import("__main__");
|
||||||
object main_namespace = main_module.attr("__dict__");
|
object main_namespace = main_module.attr("__dict__");
|
||||||
|
|
||||||
handle<> ignored((PyRun_String(
|
object ignored = exec("hello = file('hello.txt', 'w')\n"
|
||||||
|
|
||||||
"hello = file('hello.txt', 'w')\n"
|
|
||||||
"hello.write('Hello world!')\n"
|
"hello.write('Hello world!')\n"
|
||||||
"hello.close()"
|
"hello.close()",
|
||||||
|
main_namespace);
|
||||||
, Py_file_input
|
|
||||||
, main_namespace.ptr()
|
|
||||||
, main_namespace.ptr())
|
|
||||||
));
|
|
||||||
|
|
||||||
Because the Python/C API doesn't know anything about [^object]s, we used
|
|
||||||
the object's [^ptr] member function to retrieve the [^PyObject*].
|
|
||||||
|
|
||||||
This should create a file called 'hello.txt' in the current directory
|
This should create a file called 'hello.txt' in the current directory
|
||||||
containing a phrase that is well-known in programming circles.
|
containing a phrase that is well-known in programming circles.
|
||||||
|
|
||||||
[blurb
|
[h2 Manipulating Python objects]
|
||||||
__note__ [*Note] that we wrap the return value of PyRun_String in a
|
|
||||||
(nameless) [^handle] even though we are not interested in it. If we didn't
|
|
||||||
do this, the the returned object would be kept alive unnecessarily. Unless
|
|
||||||
you want to be a Dr. Frankenstein, always wrap [^PyObject*]s in [^handle]s.
|
|
||||||
]
|
|
||||||
|
|
||||||
[h2 Beyond handles]
|
Often we'd like to have a class to manipulate Python objects.
|
||||||
|
But we have already seen such a class above, and in the
|
||||||
It's nice that [^handle] manages the reference counting details for us, but
|
[@python/object.html previous section]: the aptly named [^object] class
|
||||||
other than that it doesn't do much. Often we'd like to have a more useful
|
and its derivatives. We've already seen that they can be constructed from
|
||||||
class to manipulate Python objects. But we have already seen such a class
|
a [^handle]. The following examples should further illustrate this fact:
|
||||||
above, and in the [@python/object.html previous section]: the aptly
|
|
||||||
named [^object] class and it's derivatives. We've already seen that they
|
|
||||||
can be constructed from a [^handle]. The following examples should further
|
|
||||||
illustrate this fact:
|
|
||||||
|
|
||||||
object main_module((
|
|
||||||
handle<>(borrowed(PyImport_AddModule("__main__")))));
|
|
||||||
|
|
||||||
|
object main_module = import("__main__");
|
||||||
object main_namespace = main_module.attr("__dict__");
|
object main_namespace = main_module.attr("__dict__");
|
||||||
|
object ignored = exec("result = 5 ** 2", main_namespace);
|
||||||
handle<> ignored((PyRun_String(
|
|
||||||
|
|
||||||
"result = 5 ** 2"
|
|
||||||
|
|
||||||
, Py_file_input
|
|
||||||
, main_namespace.ptr()
|
|
||||||
, main_namespace.ptr())
|
|
||||||
));
|
|
||||||
|
|
||||||
int five_squared = extract<int>(main_namespace["result"]);
|
int five_squared = extract<int>(main_namespace["result"]);
|
||||||
|
|
||||||
Here we create a dictionary object for the [^__main__] module's namespace.
|
Here we create a dictionary object for the [^__main__] module's namespace.
|
||||||
Then we assign 5 squared to the result variable and read this variable from
|
Then we assign 5 squared to the result variable and read this variable from
|
||||||
the dictionary. Another way to achieve the same result is to let
|
the dictionary. Another way to achieve the same result is to use eval instead,
|
||||||
PyRun_String return the result directly with Py_eval_input:
|
which returns the result directly:
|
||||||
|
|
||||||
object result((handle<>(
|
|
||||||
PyRun_String("5 ** 2"
|
|
||||||
, Py_eval_input
|
|
||||||
, main_namespace.ptr()
|
|
||||||
, main_namespace.ptr()))
|
|
||||||
));
|
|
||||||
|
|
||||||
|
object result = eval("5 ** 2");
|
||||||
int five_squared = extract<int>(result);
|
int five_squared = extract<int>(result);
|
||||||
|
|
||||||
[blurb
|
|
||||||
__note__ [*Note] that [^object]'s member function to return the wrapped
|
|
||||||
[^PyObject*] is called [^ptr] instead of [^get]. This makes sense if you
|
|
||||||
take into account the different functions that [^object] and [^handle]
|
|
||||||
perform.
|
|
||||||
]
|
|
||||||
|
|
||||||
[h2 Exception handling]
|
[h2 Exception handling]
|
||||||
|
|
||||||
If an exception occurs in the execution of some Python code, the PyRun_String
|
If an exception occurs in the evaluation of the python expression,
|
||||||
function returns a null pointer. Constructing a [^handle] out of this null
|
[@../../../v2/errors.html#error_already_set-spec error_already_set] is thrown:
|
||||||
pointer throws [@../../../v2/errors.html#error_already_set-spec error_already_set],
|
|
||||||
so basically, the Python exception is automatically translated into a
|
|
||||||
C++ exception when using [^handle]:
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
object result((handle<>(PyRun_String(
|
object result = eval("5/0");
|
||||||
"5/0"
|
|
||||||
, Py_eval_input
|
|
||||||
, main_namespace.ptr()
|
|
||||||
, main_namespace.ptr()))
|
|
||||||
));
|
|
||||||
|
|
||||||
// execution will never get here:
|
// execution will never get here:
|
||||||
int five_divided_by_zero = extract<int>(result);
|
int five_divided_by_zero = extract<int>(result);
|
||||||
}
|
}
|
||||||
catch(error_already_set)
|
catch(error_already_set const &)
|
||||||
{
|
{
|
||||||
// handle the exception in some way
|
// handle the exception in some way
|
||||||
}
|
}
|
||||||
@@ -1638,7 +1543,7 @@ print the exception's traceback to the console, or comparing the type of the
|
|||||||
exception with those of the [@http://www.python.org/doc/api/standardExceptions.html
|
exception with those of the [@http://www.python.org/doc/api/standardExceptions.html
|
||||||
standard exceptions]:
|
standard exceptions]:
|
||||||
|
|
||||||
catch(error_already_set)
|
catch(error_already_set const &)
|
||||||
{
|
{
|
||||||
if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError))
|
if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError))
|
||||||
{
|
{
|
||||||
@@ -1654,21 +1559,6 @@ standard exceptions]:
|
|||||||
(To retrieve even more information from the exception you can use some of the other
|
(To retrieve even more information from the exception you can use some of the other
|
||||||
exception handling functions listed [@http://www.python.org/doc/api/exceptionHandling.html here].)
|
exception handling functions listed [@http://www.python.org/doc/api/exceptionHandling.html here].)
|
||||||
|
|
||||||
If you'd rather not have [^handle] throw a C++ exception when it is constructed, you
|
|
||||||
can use the [@../../../v2/handle.html#allow_null-spec allow_null] function in the same
|
|
||||||
way you'd use borrowed:
|
|
||||||
|
|
||||||
handle<> result((allow_null(PyRun_String(
|
|
||||||
"5/0"
|
|
||||||
, Py_eval_input
|
|
||||||
, main_namespace.ptr()
|
|
||||||
, main_namespace.ptr()))));
|
|
||||||
|
|
||||||
if (!result)
|
|
||||||
// Python exception occurred
|
|
||||||
else
|
|
||||||
// everything went okay, it's safe to use the result
|
|
||||||
|
|
||||||
[endsect]
|
[endsect]
|
||||||
[endsect] [/ Embedding]
|
[endsect] [/ Embedding]
|
||||||
|
|
||||||
@@ -1754,6 +1644,38 @@ Now, our C++ Wrapper:
|
|||||||
.property("pions", range(&F::p_begin, &F::p_end))
|
.property("pions", range(&F::p_begin, &F::p_end))
|
||||||
.property("bogons", range(&F::b_begin, &F::b_end));
|
.property("bogons", range(&F::b_begin, &F::b_end));
|
||||||
|
|
||||||
|
[*stl_input_iterator]
|
||||||
|
|
||||||
|
So far, we have seen how to expose C++ iterators and ranges to Python.
|
||||||
|
Sometimes we wish to go the other way, though: we'd like to pass a
|
||||||
|
Python sequence to an STL algorithm or use it to initialize an STL
|
||||||
|
container. We need to make a Python iterator look like an STL iterator.
|
||||||
|
For that, we use `stl_input_iterator<>`. Consider how we might
|
||||||
|
implement a function that exposes `std::list<int>::assign()` to
|
||||||
|
Python:
|
||||||
|
|
||||||
|
[c++]
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void list_assign(std::list<T>& l, object o) {
|
||||||
|
// Turn a Python sequence into an STL input range
|
||||||
|
stl_input_iterator<T> begin(o), end;
|
||||||
|
l.assign(begin, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part of the wrapper for list<int>
|
||||||
|
class_<std::list<int> >("list_int")
|
||||||
|
.def("assign", &list_assign<int>)
|
||||||
|
// ...
|
||||||
|
;
|
||||||
|
|
||||||
|
Now in Python, we can assign any integer sequence to `list_int` objects:
|
||||||
|
|
||||||
|
[python]
|
||||||
|
|
||||||
|
x = list_int();
|
||||||
|
x.assign([1,2,3,4,5])
|
||||||
|
|
||||||
[endsect]
|
[endsect]
|
||||||
[section:exception Exception Translation]
|
[section:exception Exception Translation]
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,18 @@
|
|||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="refresh" content="0; URL=doc/html/index.html">
|
<meta http-equiv="refresh" content="0; URL=doc/html/index.html">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
Automatic redirection failed, click this
|
Automatic redirection failed, click this
|
||||||
<a href="doc/html/index.html">link</a>
|
<a href="doc/html/index.html">link</a> <hr>
|
||||||
|
<p>© Copyright Beman Dawes, 2001</p>
|
||||||
|
<p>Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
accompanying file <a href="../../../LICENSE_1_0.txt">
|
||||||
|
LICENSE_1_0.txt</a> or copy at
|
||||||
|
<a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -79,7 +82,7 @@ call_method<ResultType>(self_object, "<i>method-name</i>", a1, a2... a<i>N
|
|||||||
the arguments <code>a1</code>...<code>a<i>N</i></code> are copied into
|
the arguments <code>a1</code>...<code>a<i>N</i></code> are copied into
|
||||||
new Python objects, but this behavior can be overridden by the use of
|
new Python objects, but this behavior can be overridden by the use of
|
||||||
<code><a href="ptr.html#ptr-spec">ptr()</a></code> and <a href=
|
<code><a href="ptr.html#ptr-spec">ptr()</a></code> and <a href=
|
||||||
"../../../bind/ref.html#reference_wrapper">ref()</a>:</p>
|
"../../../bind/ref.html">ref()</a>:</p>
|
||||||
<pre>
|
<pre>
|
||||||
class X : boost::noncopyable
|
class X : boost::noncopyable
|
||||||
{
|
{
|
||||||
@@ -128,7 +131,7 @@ void apply(PyObject* callable, X& x)
|
|||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td><code><a href=
|
<td><code><a href=
|
||||||
"../../../bind/ref.html#reference_wrapper">boost::reference_wrapper</a><T></code></td>
|
"../../../bind/ref.html">boost::reference_wrapper</a><T></code></td>
|
||||||
|
|
||||||
<td>The Python argument contains a pointer to, rather than a copy of,
|
<td>The Python argument contains a pointer to, rather than a copy of,
|
||||||
<code>x.get()</code>. Note: failure to ensure that no Python code
|
<code>x.get()</code>. Note: failure to ensure that no Python code
|
||||||
|
|||||||
@@ -1,3 +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)
|
||||||
|
|
||||||
Here's the plan:
|
Here's the plan:
|
||||||
|
|
||||||
I aim to provide an interface similar to that of Boost.Python v1's
|
I aim to provide an interface similar to that of Boost.Python v1's
|
||||||
|
|||||||
@@ -1,23 +1,26 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
"HTML Tidy for Cygwin (vers 1st April 2002), see www.w3.org">
|
"HTML Tidy for Cygwin (vers 1st September 2004), see www.w3.org">
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-latin-1-dos">
|
||||||
<link rel="stylesheet" type="text/css" href="../boost.css">
|
<link rel="stylesheet" type="text/css" href="../boost.css">
|
||||||
|
|
||||||
<title>Boost.Python - <boost/python/class.hpp>,
|
<title>Boost.Python - <boost/python/class.hpp>,
|
||||||
<boost/python/class_fwd.hpp></title>
|
<boost/python/class_fwd.hpp></title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body link="#0000ff" vlink="#800080">
|
<body link="#0000FF" vlink="#800080">
|
||||||
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
||||||
"header">
|
"header">
|
||||||
<tr>
|
<tr>
|
||||||
<td valign="top" width="300">
|
<td valign="top" width="300">
|
||||||
<h3><a href="../../../../index.htm"><img height="86" width="277"
|
<h3><a href="../../../../index.htm"><img height="86" width="277" alt=
|
||||||
alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3>
|
"C++ Boost" src="../../../../boost.png" border="0"></a></h3>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
<td valign="top">
|
<td valign="top">
|
||||||
@@ -55,8 +58,7 @@
|
|||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><a href="#bases-spec">Class template
|
<dt><a href="#bases-spec">Class template <code>bases</code></a></dt>
|
||||||
<code>bases</code></a></dt>
|
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<dl class="page-index">
|
<dl class="page-index">
|
||||||
@@ -71,31 +73,30 @@
|
|||||||
</dl>
|
</dl>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<h2><a name="introduction"></a>Introduction</h2>
|
<h2><a name="introduction" id="introduction"></a>Introduction</h2>
|
||||||
|
|
||||||
<p><code><boost/python/class.hpp></code> defines the interface
|
<p><code><boost/python/class.hpp></code> defines the interface
|
||||||
through which users expose their C++ classes to Python. It declares the
|
through which users expose their C++ classes to Python. It declares the
|
||||||
<code>class_</code> class template, which is parameterized on the class
|
<code>class_</code> class template, which is parameterized on the class
|
||||||
type being exposed. It also exposes the <code>init</code>,
|
type being exposed. It also exposes the <code>init</code>,
|
||||||
<code>optional</code> and <code>bases</code> utility class templates,
|
<code>optional</code> and <code>bases</code> utility class templates, which
|
||||||
which are used in conjunction with <code>class_</code>.</p>
|
are used in conjunction with <code>class_</code>.</p>
|
||||||
|
|
||||||
<p><code><boost/python/class_fwd.hpp></code> contains a forward
|
<p><code><boost/python/class_fwd.hpp></code> contains a forward
|
||||||
declaration of the <code>class_</code> class template.</p>
|
declaration of the <code>class_</code> class template.</p>
|
||||||
|
|
||||||
<h2><a name="classes"></a>Classes</h2>
|
<h2><a name="classes" id="classes"></a>Classes</h2>
|
||||||
|
|
||||||
<h3><a name="class_-spec"></a>Class template
|
<h3><a name="class_-spec" id="class_-spec"></a>Class template
|
||||||
<code>class_<T, <font color="#007F00">Bases, HeldType,
|
<code>class_<T, <font color="#007F00">Bases, HeldType,
|
||||||
NonCopyable</font>></code></h3>
|
NonCopyable</font>></code></h3>
|
||||||
|
|
||||||
<p>Creates a Python class associated with the C++ type passed as its
|
<p>Creates a Python class associated with the C++ type passed as its first
|
||||||
first parameter. Although it has four template parameters, only the first
|
parameter. Although it has four template parameters, only the first one is
|
||||||
one is required. The three optional arguments can actually be supplied
|
required. The three optional arguments can actually be supplied
|
||||||
<font color="#007F00"><b>in any order</b></font>; Boost.Python determines
|
<font color="#007F00"><b>in any order</b></font>; Boost.Python determines
|
||||||
the role of the argument from its type.<br>
|
the role of the argument from its type.<br>
|
||||||
<br>
|
<br></p>
|
||||||
</p>
|
|
||||||
|
|
||||||
<table border="1" summary="class_ template parameters">
|
<table border="1" summary="class_ template parameters">
|
||||||
<tr>
|
<tr>
|
||||||
@@ -121,16 +122,16 @@
|
|||||||
|
|
||||||
<td>A specialization of <a href=
|
<td>A specialization of <a href=
|
||||||
"#bases-spec"><code>bases<</code>...<code>></code></a> which
|
"#bases-spec"><code>bases<</code>...<code>></code></a> which
|
||||||
specifies previously-exposed C++ base classes of <code>T</code><a
|
specifies previously-exposed C++ base classes of <code>T</code><a href=
|
||||||
href="#footnote_1">[1]</a>.</td>
|
"#footnote_1">[1]</a>.</td>
|
||||||
|
|
||||||
<td>Registers <code>from_python</code> conversions from wrapped
|
<td>Registers <code>from_python</code> conversions from wrapped
|
||||||
<code>T</code> instances to each of its exposed direct and indirect
|
<code>T</code> instances to each of its exposed direct and indirect
|
||||||
bases. For each polymorphic base <code>B</code>, registers
|
bases. For each polymorphic base <code>B</code>, registers conversions
|
||||||
conversions from indirectly-held wrapped <code>B</code> instances to
|
from indirectly-held wrapped <code>B</code> instances to
|
||||||
<code>T</code>.</td>
|
<code>T</code>.</td>
|
||||||
|
|
||||||
<td><code><a href="#bases">bases<></a></code></td>
|
<td><code><a href="#bases-spec">bases<></a></code></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
@@ -139,12 +140,20 @@
|
|||||||
<td>Must be <code>T</code>, a class derived from <code>T</code>, or a
|
<td>Must be <code>T</code>, a class derived from <code>T</code>, or a
|
||||||
<a href="Dereferenceable.html">Dereferenceable</a> type for which
|
<a href="Dereferenceable.html">Dereferenceable</a> type for which
|
||||||
<code><a href=
|
<code><a href=
|
||||||
"pointee.html#pointee-spec">pointee</a><HeldType>::type</code>
|
"pointee.html#pointee-spec">pointee</a><HeldType>::type</code> is
|
||||||
is <code>T</code> or a class derived from <code>T</code>.</td>
|
<code>T</code> or a class derived from <code>T</code>.</td>
|
||||||
|
|
||||||
<td>Specifies the type which is actually embedded in a Python object
|
<td>Specifies the type that is actually embedded in a Python object
|
||||||
wrapping a <code>T</code> instance. More details <a href=
|
wrapping a <code>T</code> instance when <code>T</code>'s constructor is
|
||||||
"#HeldType">below</a>.</td>
|
called or when a <code>T</code> or <code>T*</code> is converted to
|
||||||
|
Python without the use of <a href=
|
||||||
|
"http://www.boost.org/libs/python/doc/v2/callbacks.html#argument_handling">
|
||||||
|
<code>ptr</code></a>, <a href=
|
||||||
|
"http://www.boost.org/libs/python/doc/v2/callbacks.html#argument_handling">
|
||||||
|
<code>ref</code></a>, or <a href="CallPolicies.html">Call Policies</a>
|
||||||
|
such as <code><a href=
|
||||||
|
"return_internal_reference.html">return_internal_reference</a></code>.
|
||||||
|
More details <a href="#HeldType">below</a>.</td>
|
||||||
|
|
||||||
<td><code>T</code></td>
|
<td><code>T</code></td>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -153,7 +162,7 @@
|
|||||||
<td><code><font color="#007F00">NonCopyable</font></code></td>
|
<td><code><font color="#007F00">NonCopyable</font></code></td>
|
||||||
|
|
||||||
<td>If supplied, must be <a href=
|
<td>If supplied, must be <a href=
|
||||||
"../../../utility/utility.htm#Class%20noncopyable">boost::noncopyable</a>.</td>
|
"../../../utility/utility.htm#Class_noncopyable">boost::noncopyable</a>.</td>
|
||||||
|
|
||||||
<td>Suppresses automatic registration of <code>to_python</code>
|
<td>Suppresses automatic registration of <code>to_python</code>
|
||||||
conversions which copy <code>T</code> instances. Required when
|
conversions which copy <code>T</code> instances. Required when
|
||||||
@@ -164,41 +173,40 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<h4><a name="#HeldType">HeldType Semantics</a></h4>
|
<h4><a name="HeldType" id="HeldType">HeldType Semantics</a></h4>
|
||||||
|
|
||||||
<ol>
|
<ol>
|
||||||
<li>If <code>HeldType</code> is derived from T, its exposed
|
<li>If <code>HeldType</code> is derived from T, its exposed
|
||||||
constructor(s) must accept an initial <code>PyObject*</code> argument
|
constructor(s) must accept an initial <code>PyObject*</code> argument
|
||||||
which refers back to the Python object that contains the
|
which refers back to the Python object that contains the
|
||||||
<code>HeldType</code> instance, as shown in <a href=
|
<code>HeldType</code> instance, as shown in <a href=
|
||||||
"call_method.html#example">this example</a>. This argument is not
|
"call_method.html#examples">this example</a>. This argument is not
|
||||||
included in the <em><a href=
|
included in the <em><a href=
|
||||||
"init.html#init-expressions">init-expression</a></em> passed to <a
|
"init.html#init-expressions">init-expression</a></em> passed to <a href=
|
||||||
href="#class-spec-modifiers"><code>def(init_expr)</code></a>, below,
|
"#class_-spec-modifiers"><code>def(init_expr)</code></a>, below, nor is
|
||||||
nor is it passed explicitly by users when Python instances of
|
it passed explicitly by users when Python instances of <code>T</code> are
|
||||||
<code>T</code> are created. This idiom allows C++ virtual functions
|
created. This idiom allows C++ virtual functions which will be overridden
|
||||||
which will be overridden in Python to access the Python object so the
|
in Python to access the Python object so the Python method can be
|
||||||
Python method can be invoked. Boost.Python automatically registers
|
invoked. Boost.Python automatically registers additional converters which
|
||||||
additional converters which allow wrapped instances of <code>T</code>
|
allow wrapped instances of <code>T</code> to be passed to wrapped C++
|
||||||
to be passed to wrapped C++ functions expecting <code>HeldType</code>
|
functions expecting <code>HeldType</code> arguments.</li>
|
||||||
arguments.</li>
|
|
||||||
|
|
||||||
<li>Because Boost.Python will always allow wrapped instances of
|
<li>Because Boost.Python will always allow wrapped instances of
|
||||||
<code>T</code> to be passed in place of <code>HeldType</code>
|
<code>T</code> to be passed in place of <code>HeldType</code> arguments,
|
||||||
arguments, specifying a smart pointer for <code>HeldType</code> allows
|
specifying a smart pointer for <code>HeldType</code> allows users to pass
|
||||||
users to pass Python <code>T</code> instances where a smart
|
Python <code>T</code> instances where a smart pointer-to-<code>T</code>
|
||||||
pointer-to-<code>T</code> is expected. Smart pointers such as
|
is expected. Smart pointers such as <code>std::auto_ptr<></code> or
|
||||||
<code>std::auto_ptr<></code> or <code><a href=
|
<code><a href=
|
||||||
"../../../smart_ptr/shared_ptr.htm">boost::shared_ptr<></a></code>
|
"../../../smart_ptr/shared_ptr.htm">boost::shared_ptr<></a></code>
|
||||||
which contain a nested type <code>element_type</code> designating the
|
which contain a nested type <code>element_type</code> designating the
|
||||||
referent type are automatically supported; additional smart pointer
|
referent type are automatically supported; additional smart pointer types
|
||||||
types can be supported by specializing <a href=
|
can be supported by specializing <a href=
|
||||||
"pointee.html#pointee-spec">pointee<HeldType></a>.</li>
|
"pointee.html#pointee-spec">pointee<HeldType></a>.</li>
|
||||||
|
|
||||||
<li>As in case 1 above, when <code>HeldType</code> is a smart pointer
|
<li>As in case 1 above, when <code>HeldType</code> is a smart pointer to
|
||||||
to a class derived from <code>T</code>, the initial
|
a class derived from <code>T</code>, the initial <code>PyObject*</code>
|
||||||
<code>PyObject*</code> argument must be supplied by all of
|
argument must be supplied by all of <code>HeldType</code>'s exposed
|
||||||
<code>HeldType</code>'s exposed constructors.</li>
|
constructors.</li>
|
||||||
|
|
||||||
<li>Except in cases 1 and 3, users may optionally specify that T itself
|
<li>Except in cases 1 and 3, users may optionally specify that T itself
|
||||||
gets initialized with a similar initial <code>PyObject*</code> argument
|
gets initialized with a similar initial <code>PyObject*</code> argument
|
||||||
@@ -206,9 +214,9 @@
|
|||||||
"has_back_reference.html#has_back_reference-spec">has_back_reference<T></a>.</li>
|
"has_back_reference.html#has_back_reference-spec">has_back_reference<T></a>.</li>
|
||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
<h4><a name="class_-spec-synopsis"></a>Class template <code>class_</code>
|
<h4><a name="class_-spec-synopsis" id="class_-spec-synopsis"></a>Class
|
||||||
synopsis</h4>
|
template <code>class_</code> synopsis</h4>
|
||||||
<pre>
|
<pre>
|
||||||
namespace boost { namespace python
|
namespace boost { namespace python
|
||||||
{
|
{
|
||||||
template <class T
|
template <class T
|
||||||
@@ -287,9 +295,9 @@ namespace boost { namespace python
|
|||||||
}}
|
}}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h4><a name="class_-spec-ctors"></a>Class template <code>class_</code>
|
<h4><a name="class_-spec-ctors" id="class_-spec-ctors"></a>Class template
|
||||||
constructors</h4>
|
<code>class_</code> constructors</h4>
|
||||||
<pre>
|
<pre>
|
||||||
class_(char const* name);
|
class_(char const* name);
|
||||||
class_(char const* name, char const* docstring);
|
class_(char const* name, char const* docstring);
|
||||||
template <class Init>
|
template <class Init>
|
||||||
@@ -306,24 +314,24 @@ class_(char const* name, char const* docstring, Init init_spec);
|
|||||||
<a href="definitions.html#ntbs">ntbs</a>. If <code>init_spec</code> is
|
<a href="definitions.html#ntbs">ntbs</a>. If <code>init_spec</code> is
|
||||||
supplied, it must be either the special enumeration constant
|
supplied, it must be either the special enumeration constant
|
||||||
<code>no_init</code> or an <a href=
|
<code>no_init</code> or an <a href=
|
||||||
"init.html#init-expression">init-expression</a> compatible with
|
"init.html#init-expressions">init-expression</a> compatible with
|
||||||
<code>T</code>.</dt>
|
<code>T</code>.</dt>
|
||||||
|
|
||||||
<dt><b>Effects:</b> Constructs a <code>class_</code> object holding a
|
<dt><b>Effects:</b> Constructs a <code>class_</code> object holding a
|
||||||
Boost.Python extension class named <code>name</code>. The
|
Boost.Python extension class named <code>name</code>. The
|
||||||
<code>name</code>d attribute of the <a href=
|
<code>name</code>d attribute of the <a href=
|
||||||
"scope.html#introduction">current scope</a> is bound to the new
|
"scope.html#introduction">current scope</a> is bound to the new extension
|
||||||
extension class.</dt>
|
class.</dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<ul>
|
<ul>
|
||||||
<li>If supplied, the value of <code>docstring</code> is bound to
|
<li>If supplied, the value of <code>docstring</code> is bound to the
|
||||||
the <code>__doc__</code> attribute of the extension class.</li>
|
<code>__doc__</code> attribute of the extension class.</li>
|
||||||
|
|
||||||
<li>If <code>init_spec</code> is <code>no_init</code>, a special
|
<li>If <code>init_spec</code> is <code>no_init</code>, a special
|
||||||
<code>__init__</code> function is generated which always raises a
|
<code>__init__</code> function is generated which always raises a
|
||||||
Python exception. Otherwise, <code>this->def(init_spec)</code>
|
Python exception. Otherwise, <code>this->def(init_spec)</code> is
|
||||||
is called.</li>
|
called.</li>
|
||||||
|
|
||||||
<li>If <code>init_spec</code> is not supplied,
|
<li>If <code>init_spec</code> is not supplied,
|
||||||
<code>this->def(init<>())</code> is called.</li>
|
<code>this->def(init<>())</code> is called.</li>
|
||||||
@@ -333,47 +341,45 @@ class_(char const* name, char const* docstring, Init init_spec);
|
|||||||
<dt><b>Rationale:</b>Allowing the user to specify constructor arguments
|
<dt><b>Rationale:</b>Allowing the user to specify constructor arguments
|
||||||
in the <code>class_<></code> constructor helps her to avoid the
|
in the <code>class_<></code> constructor helps her to avoid the
|
||||||
common run-time errors which result from invoking wrapped member
|
common run-time errors which result from invoking wrapped member
|
||||||
functions without having exposed an <code>__init__</code> function
|
functions without having exposed an <code>__init__</code> function which
|
||||||
which creates the requisite <code>T</code> instance. Types which are
|
creates the requisite <code>T</code> instance. Types which are not
|
||||||
not default-constructible will cause a compile-time error unless
|
default-constructible will cause a compile-time error unless
|
||||||
<code>Init</code> is supplied. The user must always supply
|
<code>Init</code> is supplied. The user must always supply
|
||||||
<code>name</code> as there is currently no portable method to derive
|
<code>name</code> as there is currently no portable method to derive the
|
||||||
the text of the class name from its type.</dt>
|
text of the class name from its type.</dt>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
<h4><a name="class_-spec-modifiers"></a>Class template
|
<h4><a name="class_-spec-modifiers" id="class_-spec-modifiers"></a>Class
|
||||||
<code>class_</code> modifier functions</h4>
|
template <code>class_</code> modifier functions</h4>
|
||||||
<pre>
|
<pre>
|
||||||
template <class Init>
|
template <class Init>
|
||||||
class_& def(Init init_expr);
|
class_& def(Init init_expr);
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<dl class="function-semantics">
|
<dl class="function-semantics">
|
||||||
<dt><b>Requires:</b> <code>init_expr</code> is the result of an <a
|
<dt><b>Requires:</b> <code>init_expr</code> is the result of an <a href=
|
||||||
href="init.html#init-expression">init-expression</a> compatible with
|
"init.html#init-expressions">init-expression</a> compatible with
|
||||||
<code>T</code>.</dt>
|
<code>T</code>.</dt>
|
||||||
|
|
||||||
<dt><b>Effects:</b> For each <a href="init.html#init-expressions">valid
|
<dt><b>Effects:</b> For each <a href="init.html#init-expressions">valid
|
||||||
prefix</a> <em>P</em> of <code>Init</code>, adds an
|
prefix</a> <em>P</em> of <code>Init</code>, adds an
|
||||||
<code>__init__(</code>...<code>)</code> function overload to the
|
<code>__init__(</code>...<code>)</code> function overload to the
|
||||||
extension class accepting <em>P</em> as arguments. Each overload
|
extension class accepting <em>P</em> as arguments. Each overload
|
||||||
generated constructs an object of <code>HeldType</code> according to
|
generated constructs an object of <code>HeldType</code> according to the
|
||||||
the semantics described <a href="#HeldType">above</a>, using a copy of
|
semantics described <a href="#HeldType">above</a>, using a copy of
|
||||||
<code>init_expr</code>'s <a href="CallPolicies.html">call policies</a>.
|
<code>init_expr</code>'s <a href="CallPolicies.html">call policies</a>.
|
||||||
If the longest <a href="init.html#init-expressions">valid prefix</a> of
|
If the longest <a href="init.html#init-expressions">valid prefix</a> of
|
||||||
<code>Init</code> contains <em>N</em> types and <code>init_expr</code>
|
<code>Init</code> contains <em>N</em> types and <code>init_expr</code>
|
||||||
holds <em>M</em> keywords, an initial sequence of the keywords are used
|
holds <em>M</em> keywords, an initial sequence of the keywords are used
|
||||||
for all but the first <em>N</em> - <em>M</em> arguments of
|
for all but the first <em>N</em> - <em>M</em> arguments of each
|
||||||
each overload.</dt>
|
overload.</dt>
|
||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
|
|
||||||
<dt><b>Rationale:</b> Allows users to easily expose a class'
|
<dt><b>Rationale:</b> Allows users to easily expose a class' constructor
|
||||||
constructor to Python.</dt>
|
to Python.</dt>
|
||||||
</dl>
|
</dl><br>
|
||||||
<br>
|
<pre>
|
||||||
|
|
||||||
<pre>
|
|
||||||
template <class F>
|
template <class F>
|
||||||
class_& def(char const* name, Fn fn);
|
class_& def(char const* name, Fn fn);
|
||||||
template <class Fn, class A1>
|
template <class Fn, class A1>
|
||||||
@@ -392,19 +398,18 @@ class_& def(char const* name, Fn fn, A1 const& a1, A2 const& a2, A3
|
|||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>If <code>a1</code> is the result of an <a href=
|
||||||
If <code>a1</code> is the result of an <a href=
|
|
||||||
"overloads.html#overload-dispatch-expression"><em>overload-dispatch-expression</em></a>,
|
"overloads.html#overload-dispatch-expression"><em>overload-dispatch-expression</em></a>,
|
||||||
only the second form is allowed and fn must be a pointer to
|
only the second form is allowed and fn must be a pointer to function
|
||||||
function or pointer to member function whose <a href=
|
or pointer to member function whose <a href="definitions.html#arity">
|
||||||
"definitions.html#arity">arity</a> is the same as A1's <a href=
|
arity</a> is the same as A1's <a href=
|
||||||
"overloads.html#overload-dispatch-expression"><em>maximum
|
"overloads.html#overload-dispatch-expression"><em>maximum
|
||||||
arity</em></a>.
|
arity</em></a>.
|
||||||
|
|
||||||
<dl>
|
<dl>
|
||||||
<dt><b>Effects:</b> For each prefix <em>P</em> of
|
<dt><b>Effects:</b> For each prefix <em>P</em> of
|
||||||
<code>Fn</code>'s sequence of argument types, beginning with
|
<code>Fn</code>'s sequence of argument types, beginning with the
|
||||||
the one whose length is <code>A1</code>'s <a href=
|
one whose length is <code>A1</code>'s <a href=
|
||||||
"overloads.html#overload-dispatch-expression"><em>minimum
|
"overloads.html#overload-dispatch-expression"><em>minimum
|
||||||
arity</em></a>, adds a
|
arity</em></a>, adds a
|
||||||
<code><em>name</em>(</code>...<code>)</code> method overload to
|
<code><em>name</em>(</code>...<code>)</code> method overload to
|
||||||
@@ -414,34 +419,31 @@ class_& def(char const* name, Fn fn, A1 const& a1, A2 const& a2, A3
|
|||||||
policies</a>. If the longest valid prefix of <code>A1</code>
|
policies</a>. If the longest valid prefix of <code>A1</code>
|
||||||
contains <em>N</em> types and <code>a1</code> holds <em>M</em>
|
contains <em>N</em> types and <code>a1</code> holds <em>M</em>
|
||||||
keywords, an initial sequence of the keywords are used for all
|
keywords, an initial sequence of the keywords are used for all
|
||||||
but the first <em>N</em> - <em>M</em> arguments of
|
but the first <em>N</em> - <em>M</em> arguments of each
|
||||||
each overload.<br>
|
overload.<br></dt>
|
||||||
</dt>
|
|
||||||
</dl>
|
</dl>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<li>
|
<li>Otherwise, a single method overload is built around fn, which
|
||||||
Otherwise, a single method overload is built around fn, which
|
|
||||||
must not be null:
|
must not be null:
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>If fn is a function pointer, its first argument must be of
|
<li>If fn is a function pointer, its first argument must be of
|
||||||
the form <code>U</code>, <code>U <em>cv</em>&</code>,
|
the form <code>U</code>, <code>U <em>cv</em>&</code>, <code>U
|
||||||
<code>U <em>cv</em>*</code>, or
|
<em>cv</em>*</code>, or <code>U <em>cv</em>* const&</code>,
|
||||||
<code>U <em>cv</em>* const&</code>, where
|
where <code>T*</code> is convertible to <code>U*</code>, and
|
||||||
<code>T*</code> is convertible to <code>U*</code>, and
|
<code>a1</code>-<code>a3</code>, if supplied, may be selected in
|
||||||
<code>a1</code>-<code>a3</code>, if supplied, may be selected
|
any order from the table below.</li>
|
||||||
in any order from the table below.</li>
|
|
||||||
|
|
||||||
<li>Otherwise, if fn is a member function pointer, its target
|
<li>Otherwise, if fn is a member function pointer, its target
|
||||||
must be <code>T</code> or one of its public base classes, and
|
must be <code>T</code> or one of its public base classes, and
|
||||||
<code>a1</code>-<code>a3</code>, if supplied, may be selected
|
<code>a1</code>-<code>a3</code>, if supplied, may be selected in
|
||||||
in any order from the table below.</li>
|
any order from the table below.</li>
|
||||||
|
|
||||||
<li>Otherwise, <code>Fn</code> must be [derived from] <code><a
|
<li>Otherwise, <code>Fn</code> must be [derived from]
|
||||||
href="object.html#object-spec">object</a></code>, and
|
<code><a href="object.html#object-spec">object</a></code>, and
|
||||||
<code>a1-a2</code>, if supplied, may be selcted in any order
|
<code>a1-a2</code>, if supplied, may be selcted in any order from
|
||||||
from the first two rows of the table below. To be useful,
|
the first two rows of the table below. To be useful,
|
||||||
<code>fn</code> should be <a href=
|
<code>fn</code> should be <a href=
|
||||||
"http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-6">
|
"http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-6">
|
||||||
callable</a>.</li>
|
callable</a>.</li>
|
||||||
@@ -495,7 +497,7 @@ class_& def(char const* name, Fn fn, A1 const& a1, A2 const& a2, A3
|
|||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
</dl>
|
</dl>
|
||||||
<pre>
|
<pre>
|
||||||
class_& staticmethod(char const* name);
|
class_& staticmethod(char const* name);
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@@ -509,11 +511,11 @@ class_& staticmethod(char const* name);
|
|||||||
<dt><b>Effects:</b> Replaces the existing named attribute <i>x</i> with
|
<dt><b>Effects:</b> Replaces the existing named attribute <i>x</i> with
|
||||||
the result of invoking <code>staticmethod(</code><i>x</i><code>)</code>
|
the result of invoking <code>staticmethod(</code><i>x</i><code>)</code>
|
||||||
in Python. Specifies that the corresponding method is static and
|
in Python. Specifies that the corresponding method is static and
|
||||||
therefore no object instance will be passed to it. This is equivalent
|
therefore no object instance will be passed to it. This is equivalent to
|
||||||
to the Python statement:</dt>
|
the Python statement:</dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<pre>
|
<pre>
|
||||||
setattr(self, name, staticmethod(getattr(self, name)))
|
setattr(self, name, staticmethod(getattr(self, name)))
|
||||||
</pre>
|
</pre>
|
||||||
</dd>
|
</dd>
|
||||||
@@ -523,10 +525,8 @@ setattr(self, name, staticmethod(getattr(self, name)))
|
|||||||
"definitions.html#raise">raise</a> a RuntimeError.</dt>
|
"definitions.html#raise">raise</a> a RuntimeError.</dt>
|
||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
</dl>
|
</dl><br>
|
||||||
<br>
|
<pre>
|
||||||
|
|
||||||
<pre>
|
|
||||||
template <<i>unspecified</i>>
|
template <<i>unspecified</i>>
|
||||||
class_& def(<a href=
|
class_& def(<a href=
|
||||||
"operators.html#operator_-spec">detail::operator_</a><unspecified>);
|
"operators.html#operator_-spec">detail::operator_</a><unspecified>);
|
||||||
@@ -539,7 +539,7 @@ class_& def(<a href=
|
|||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
</dl>
|
</dl>
|
||||||
<pre>
|
<pre>
|
||||||
template <class U>
|
template <class U>
|
||||||
class_& setattr(char const* name, U const& u);
|
class_& setattr(char const* name, U const& u);
|
||||||
</pre>
|
</pre>
|
||||||
@@ -556,16 +556,15 @@ class_& setattr(char const* name, U const& u);
|
|||||||
<dd>
|
<dd>
|
||||||
<blockquote>
|
<blockquote>
|
||||||
<code><a href=
|
<code><a href=
|
||||||
"http://www.python.org/doc/current/api/object.html#l2h-166">PyObject_SetAttrString</a>(this->ptr(), name, <a
|
"http://www.python.org/doc/current/api/object.html#l2h-166">PyObject_SetAttrString</a>(this->ptr(),
|
||||||
href="object.html#object-spec-ctors">object</a>(u).ptr());</code>
|
name, <a href=
|
||||||
|
"object.html#object-spec-ctors">object</a>(u).ptr());</code>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
</dl>
|
</dl><br>
|
||||||
<br>
|
<pre>
|
||||||
|
|
||||||
<pre>
|
|
||||||
template <class Get>
|
template <class Get>
|
||||||
void add_property(char const* name, Get const& fget, char const* doc=0);
|
void add_property(char const* name, Get const& fget, char const* doc=0);
|
||||||
template <class Get, class Set>
|
template <class Get, class Set>
|
||||||
@@ -582,19 +581,18 @@ void add_property(
|
|||||||
<dt><b>Effects:</b> Creates a new Python <a href=
|
<dt><b>Effects:</b> Creates a new Python <a href=
|
||||||
"http://www.python.org/2.2.2/descrintro.html#property"><code>property</code></a>
|
"http://www.python.org/2.2.2/descrintro.html#property"><code>property</code></a>
|
||||||
class instance, passing <code><a href=
|
class instance, passing <code><a href=
|
||||||
"object.html#object-spec-ctors">object</a>(fget)</code> (and <code><a
|
"object.html#object-spec-ctors">object</a>(fget)</code> (and
|
||||||
href="object.html#object-spec-ctors">object</a>(fset)</code> in the
|
<code><a href="object.html#object-spec-ctors">object</a>(fset)</code> in
|
||||||
second form) with an (optional) docstring <code>doc</code> to its constructor,
|
the second form) with an (optional) docstring <code>doc</code> to its
|
||||||
then adds that property to the Python class object under construction
|
constructor, then adds that property to the Python class object under
|
||||||
with the given attribute <code>name</code>.</dt>
|
construction with the given attribute <code>name</code>.</dt>
|
||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
|
|
||||||
<dt><b>Rationale:</b> Allows users to easily expose functions that can
|
<dt><b>Rationale:</b> Allows users to easily expose functions that can be
|
||||||
be invoked from Python with attribute access syntax.</dt>
|
invoked from Python with attribute access syntax.</dt>
|
||||||
</dl>
|
</dl><br>
|
||||||
<br>
|
<pre>
|
||||||
<pre>
|
|
||||||
template <class Get>
|
template <class Get>
|
||||||
void add_static_property(char const* name, Get const& fget);
|
void add_static_property(char const* name, Get const& fget);
|
||||||
template <class Get, class Set>
|
template <class Get, class Set>
|
||||||
@@ -607,12 +605,12 @@ void add_static_property(char const* name, Get const& fget, Set const& f
|
|||||||
"http://www.python.org/doc/current/ref/identifiers.html">identifier
|
"http://www.python.org/doc/current/ref/identifiers.html">identifier
|
||||||
naming rules</a>.</dt>
|
naming rules</a>.</dt>
|
||||||
|
|
||||||
<dt><b>Effects:</b> Creates a Boost.Python.StaticProperty object,
|
<dt><b>Effects:</b> Creates a Boost.Python.StaticProperty object, passing
|
||||||
passing <code><a href=
|
<code><a href="object.html#object-spec-ctors">object</a>(fget)</code>
|
||||||
"object.html#object-spec-ctors">object</a>(fget)</code> (and <code><a
|
(and <code><a href=
|
||||||
href="object.html#object-spec-ctors">object</a>(fset)</code> in the
|
"object.html#object-spec-ctors">object</a>(fset)</code> in the second
|
||||||
second form) to its constructor, then adds that property to the Python
|
form) to its constructor, then adds that property to the Python class
|
||||||
class under construction with the given attribute <code>name</code>.
|
under construction with the given attribute <code>name</code>.
|
||||||
StaticProperty is a special subclass of Python's <a href=
|
StaticProperty is a special subclass of Python's <a href=
|
||||||
"http://www.python.org/2.2.2/descrintro.html#property"><code>property</code></a>
|
"http://www.python.org/2.2.2/descrintro.html#property"><code>property</code></a>
|
||||||
class which can be called without an initial <code>self</code>
|
class which can be called without an initial <code>self</code>
|
||||||
@@ -620,11 +618,10 @@ void add_static_property(char const* name, Get const& fget, Set const& f
|
|||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
|
|
||||||
<dt><b>Rationale:</b> Allows users to easily expose functions that can
|
<dt><b>Rationale:</b> Allows users to easily expose functions that can be
|
||||||
be invoked from Python with static attribute access syntax.</dt>
|
invoked from Python with static attribute access syntax.</dt>
|
||||||
</dl>
|
</dl><br>
|
||||||
<br>
|
<pre>
|
||||||
<pre>
|
|
||||||
template <class D>
|
template <class D>
|
||||||
class_& def_readonly(char const* name, D T::*pm, char const* doc=0);
|
class_& def_readonly(char const* name, D T::*pm, char const* doc=0);
|
||||||
template <class D>
|
template <class D>
|
||||||
@@ -640,26 +637,24 @@ class_& def_readonly(char const* name, D const& d);
|
|||||||
<dt><b>Effects:</b></dt>
|
<dt><b>Effects:</b></dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<pre>
|
<pre>
|
||||||
this->add_property(name, <a href=
|
this->add_property(name, <a href=
|
||||||
"data_members.html#make_getter-spec">make_getter</a>(pm), doc);
|
"data_members.html#make_getter-spec">make_getter</a>(pm), doc);
|
||||||
</pre>
|
</pre>and
|
||||||
and
|
<pre>
|
||||||
<pre>
|
|
||||||
this->add_static_property(name, <a href=
|
this->add_static_property(name, <a href=
|
||||||
"data_members.html#make_getter-spec">make_getter</a>(d));
|
"data_members.html#make_getter-spec">make_getter</a>(d));
|
||||||
</pre>
|
</pre>respectively.<br>
|
||||||
respectively.<br>
|
|
||||||
<br>
|
<br>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
|
|
||||||
<dt><b>Rationale:</b> Allows users to easily expose a class' data
|
<dt><b>Rationale:</b> Allows users to easily expose a class' data member
|
||||||
member or free variable such that it can be inspected from Python with
|
or free variable such that it can be inspected from Python with a natural
|
||||||
a natural syntax.</dt>
|
syntax.</dt>
|
||||||
</dl>
|
</dl>
|
||||||
<pre>
|
<pre>
|
||||||
template <class D>
|
template <class D>
|
||||||
class_& def_readwrite(char const* name, D T::*pm, char const* doc=0);
|
class_& def_readwrite(char const* name, D T::*pm, char const* doc=0);
|
||||||
template <class D>
|
template <class D>
|
||||||
@@ -670,77 +665,72 @@ class_& def_readwrite(char const* name, D& d);
|
|||||||
<dt><b>Effects:</b></dt>
|
<dt><b>Effects:</b></dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<pre>
|
<pre>
|
||||||
this->add_property(name, <a href=
|
this->add_property(name, <a href=
|
||||||
"data_members.html#make_getter-spec">make_getter</a>(pm), <a href=
|
"data_members.html#make_getter-spec">make_getter</a>(pm), <a href=
|
||||||
"data_members.html#make_setter-spec">make_setter</a>(pm), doc);
|
"data_members.html#make_setter-spec">make_setter</a>(pm), doc);
|
||||||
</pre>
|
</pre>and
|
||||||
and
|
<pre>
|
||||||
<pre>
|
|
||||||
this->add_static_property(name, <a href=
|
this->add_static_property(name, <a href=
|
||||||
"data_members.html#make_getter-spec">make_getter</a>(d), <a href=
|
"data_members.html#make_getter-spec">make_getter</a>(d), <a href=
|
||||||
"data_members.html#make_setter-spec">make_setter</a>(d));
|
"data_members.html#make_setter-spec">make_setter</a>(d));
|
||||||
</pre>
|
</pre>respectively.<br>
|
||||||
respectively.<br>
|
|
||||||
<br>
|
<br>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
|
|
||||||
<dt><b>Rationale:</b> Allows users to easily expose a class' data or
|
<dt><b>Rationale:</b> Allows users to easily expose a class' data or free
|
||||||
free variable member such that it can be inspected and set from Python
|
variable member such that it can be inspected and set from Python with a
|
||||||
with a natural syntax.</dt>
|
natural syntax.</dt>
|
||||||
</dl>
|
</dl>
|
||||||
<pre>
|
<pre>
|
||||||
template <typename PickleSuite>
|
template <typename PickleSuite>
|
||||||
class_& def_pickle(PickleSuite const&);
|
class_& def_pickle(PickleSuite const&);
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<dl class="function-semantics">
|
<dl class="function-semantics">
|
||||||
<dt><b>Requires:</b> PickleSuite must be publically derived from <a
|
<dt><b>Requires:</b> PickleSuite must be publically derived from <a href=
|
||||||
href="pickle.html"><code>pickle_suite</code></a>.</dt>
|
"pickle.html"><code>pickle_suite</code></a>.</dt>
|
||||||
|
|
||||||
<dt><b>Effects:</b> Defines a legal combination of the special
|
<dt><b>Effects:</b> Defines a legal combination of the special attributes
|
||||||
attributes and methods: <code>__getinitargs__</code>,
|
and methods: <code>__getinitargs__</code>, <code>__getstate__</code>,
|
||||||
<code>__getstate__</code>, <code>__setstate__</code>,
|
<code>__setstate__</code>, <code>__getstate_manages_dict__</code>,
|
||||||
<code>__getstate_manages_dict__</code>,
|
|
||||||
<code>__safe_for_unpickling__</code>, <code>__reduce__</code></dt>
|
<code>__safe_for_unpickling__</code>, <code>__reduce__</code></dt>
|
||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
|
|
||||||
<dt><b>Rationale:</b> Provides an <a href="pickle.html">easy to use
|
<dt><b>Rationale:</b> Provides an <a href="pickle.html">easy to use
|
||||||
high-level interface</a> for establishing complete pickle support for
|
high-level interface</a> for establishing complete pickle support for the
|
||||||
the wrapped class. The user is protected by compile-time consistency
|
wrapped class. The user is protected by compile-time consistency
|
||||||
checks.</dt>
|
checks.</dt>
|
||||||
</dl>
|
</dl><br>
|
||||||
<br>
|
<pre>
|
||||||
<pre>
|
|
||||||
class_& enable_pickling();
|
class_& enable_pickling();
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<dl class="function-semantics">
|
<dl class="function-semantics">
|
||||||
<dt><b>Effects:</b> Defines the <code>__reduce__</code> method and
|
<dt><b>Effects:</b> Defines the <code>__reduce__</code> method and the
|
||||||
the <code>__safe_for_unpickling__</code> attribute.
|
<code>__safe_for_unpickling__</code> attribute.</dt>
|
||||||
|
|
||||||
<dt><b>Returns:</b> <code>*this</code></dt>
|
<dt><b>Returns:</b> <code>*this</code></dt>
|
||||||
|
|
||||||
<dt><b>Rationale:</b> Light-weight alternative to
|
<dt><b>Rationale:</b> Light-weight alternative to
|
||||||
<code>def_pickle()</code>. Enables implementation of
|
<code>def_pickle()</code>. Enables implementation of <a href=
|
||||||
<a href="pickle.html">pickle support</a> from Python.</dt>
|
"pickle.html">pickle support</a> from Python.</dt>
|
||||||
</dl>
|
</dl><br>
|
||||||
<br>
|
|
||||||
|
|
||||||
|
<h3><a name="bases-spec" id="bases-spec"></a>Class template
|
||||||
|
<code>bases<T1, T2,</code>...<code>TN></code></h3>
|
||||||
|
|
||||||
<h3><a name="bases-spec"></a>Class template
|
<p>An <a href="../../../mpl/doc/refmanual/forward-sequence.html">MPL
|
||||||
<code>bases<T1, T2,</code>...<code>TN></code></h3>
|
sequence</a> which can be used in
|
||||||
|
<code>class_<</code>...<code>></code> instantiations indicate a list
|
||||||
|
of base classes.</p>
|
||||||
|
|
||||||
<p>An <a href="../../../mpl/doc/refmanual/forward-sequence.html">MPL sequence</a>
|
<h4><a name="bases-spec-synopsis" id="bases-spec-synopsis"></a>Class
|
||||||
which can be used in <code>class_<</code>...<code>></code>
|
template <code>bases</code> synopsis</h4>
|
||||||
instantiations indicate a list of base classes.</p>
|
<pre>
|
||||||
|
|
||||||
<h4><a name="bases-spec-synopsis"></a>Class template <code>bases</code>
|
|
||||||
synopsis</h4>
|
|
||||||
<pre>
|
|
||||||
namespace boost { namespace python
|
namespace boost { namespace python
|
||||||
{
|
{
|
||||||
template <T1 = <i>unspecified</i>,...T<i>n</i> = <i>unspecified</i>>
|
template <T1 = <i>unspecified</i>,...T<i>n</i> = <i>unspecified</i>>
|
||||||
@@ -749,10 +739,10 @@ namespace boost { namespace python
|
|||||||
}}
|
}}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h2><a name="examples"></a>Example(s)</h2>
|
<h2><a name="examples" id="examples"></a>Example(s)</h2>
|
||||||
|
|
||||||
<p>Given a C++ class declaration:</p>
|
<p>Given a C++ class declaration:</p>
|
||||||
<pre>
|
<pre>
|
||||||
class Foo : public Bar, public Baz
|
class Foo : public Bar, public Baz
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -766,9 +756,8 @@ class Foo : public Bar, public Baz
|
|||||||
private:
|
private:
|
||||||
...
|
...
|
||||||
};
|
};
|
||||||
</pre>
|
</pre>A corresponding Boost.Python extension class can be created with:
|
||||||
A corresponding Boost.Python extension class can be created with:
|
<pre>
|
||||||
<pre>
|
|
||||||
using namespace boost::python;
|
using namespace boost::python;
|
||||||
|
|
||||||
class_<Foo,bases<Bar,Baz> >("Foo",
|
class_<Foo,bases<Bar,Baz> >("Foo",
|
||||||
@@ -784,21 +773,18 @@ class_<Foo,bases<Bar,Baz> >("Foo",
|
|||||||
;
|
;
|
||||||
</pre>
|
</pre>
|
||||||
<hr>
|
<hr>
|
||||||
<a name="footnote_1">[1]</a> By "previously-exposed" we mean that the for
|
<a name="footnote_1" id="footnote_1">[1]</a> By "previously-exposed" we
|
||||||
each <code>B</code> in <code>bases</code>, an instance of
|
mean that the for each <code>B</code> in <code>bases</code>, an instance of
|
||||||
<code>class_<B<font color="#007F00">, ...</font>></code> must have
|
<code>class_<B<font color="#007F00">, ...</font>></code> must have
|
||||||
already been constructed.
|
already been constructed.
|
||||||
<pre>
|
<pre>
|
||||||
class_<Base>("Base");
|
class_<Base>("Base");
|
||||||
class_<Derived, bases<Base> >("Derived");
|
class_<Derived, bases<Base> >("Derived");
|
||||||
</pre>
|
</pre>Revised
|
||||||
Revised
|
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
||||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
1 November, 2005 <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
||||||
5 August, 2002 <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
|
||||||
|
|
||||||
|
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
||||||
<p><i>© Copyright <a href=
|
Abrahams</a> 2002.</i></p>
|
||||||
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
|
</body>
|
||||||
</body>
|
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -132,4 +132,6 @@ BOOST_PYTHON_MODULE(my_ext)
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
<p><i>© Copyright Joel de Guzman 2003. </i>
|
<p><i>© Copyright Joel de Guzman 2003. </i> 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)
|
||||||
|
|||||||
@@ -166,5 +166,7 @@ struct return_value_policy : Base
|
|||||||
|
|
||||||
|
|
||||||
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
||||||
Abrahams</a> 2002. </i>
|
Abrahams</a> 2002.</i> 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)</p>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
339
doc/v2/docstring_options.html
Normal file
339
doc/v2/docstring_options.html
Normal file
@@ -0,0 +1,339 @@
|
|||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta name="generator" content=
|
||||||
|
"HTML Tidy for Linux/x86 (vers 1st September 2004), see www.w3.org">
|
||||||
|
<meta http-equiv="Content-Type" content=
|
||||||
|
"text/html; charset=us-ascii">
|
||||||
|
<link rel="stylesheet" type="text/css" href="../boost.css">
|
||||||
|
|
||||||
|
<title>Boost.Python -
|
||||||
|
<boost/python/docstring_options.hpp></title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<table border="0" cellpadding="7" cellspacing="0" width="100%"
|
||||||
|
summary="header">
|
||||||
|
<tr>
|
||||||
|
<td valign="top" width="300">
|
||||||
|
<h3><a href="../../../../index.htm"><img height="86" width=
|
||||||
|
"277" alt="C++ Boost" src="../../../../boost.png" border=
|
||||||
|
"0"></a></h3>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td valign="top">
|
||||||
|
<h1 align="center"><a href=
|
||||||
|
"../index.html">Boost.Python</a></h1>
|
||||||
|
|
||||||
|
<h2 align="center">Header
|
||||||
|
<boost/python/docstring_options.hpp></h2>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h2>Contents</h2>
|
||||||
|
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#introduction">Introduction</a></dt>
|
||||||
|
|
||||||
|
<dt><a href="#classes">Classes</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#docstring_options-spec">Class
|
||||||
|
<code>docstring_options</code></a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#docstring_options-spec-synopsis">Class
|
||||||
|
<code>docstring_options</code> synopsis</a></dt>
|
||||||
|
|
||||||
|
<dt><a href="#docstring_options-spec-ctors">Class
|
||||||
|
<code>docstring_options</code> constructors</a></dt>
|
||||||
|
|
||||||
|
<dt><a href="#docstring_options-spec-dtors">Class
|
||||||
|
<code>docstring_options</code> destructors</a></dt>
|
||||||
|
|
||||||
|
<dt><a href="#docstring_options-spec-modifiers">Class
|
||||||
|
<code>docstring_options</code> modifiers</a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
|
||||||
|
<dt><a href="#examples">Examples</a></dt>
|
||||||
|
</dl>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h2><a name="introduction" id=
|
||||||
|
"introduction"></a>Introduction</h2>
|
||||||
|
|
||||||
|
<p>Boost.Python supports user-defined docstrings with automatic
|
||||||
|
appending of C++ signatures. These features are enabled by
|
||||||
|
default. The <code>class docstring_options</code> is available to
|
||||||
|
selectively suppress the user-defined docstrings, signatures, or
|
||||||
|
both.</p>
|
||||||
|
|
||||||
|
<h2><a name="classes" id="classes"></a>Classes</h2>
|
||||||
|
|
||||||
|
<h3><a name="docstring_options-spec" id=
|
||||||
|
"docstring_options-spec"></a>Class
|
||||||
|
<code>docstring_options</code></h3>
|
||||||
|
|
||||||
|
<p>Controls the appearance of docstrings of wrapped functions and
|
||||||
|
member functions for the life-time of the instance. The instances
|
||||||
|
are noncopyable to eliminate the possibility of surprising side
|
||||||
|
effects.</p>
|
||||||
|
|
||||||
|
<h4><a name="docstring_options-spec-synopsis" id=
|
||||||
|
"docstring_options-spec-synopsis"></a>Class
|
||||||
|
<code>docstring_options</code> synopsis</h4>
|
||||||
|
<pre>
|
||||||
|
namespace boost { namespace python {
|
||||||
|
|
||||||
|
class docstring_options : boost::noncopyable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
docstring_options(bool show_all=true);
|
||||||
|
|
||||||
|
docstring_options(bool show_user_defined, bool show_signatures);
|
||||||
|
|
||||||
|
~docstring_options();
|
||||||
|
|
||||||
|
void
|
||||||
|
disable_user_defined();
|
||||||
|
|
||||||
|
void
|
||||||
|
enable_user_defined();
|
||||||
|
|
||||||
|
void
|
||||||
|
disable_signatures();
|
||||||
|
|
||||||
|
void
|
||||||
|
enable_signatures();
|
||||||
|
|
||||||
|
void
|
||||||
|
disable_all();
|
||||||
|
|
||||||
|
void
|
||||||
|
enable_all();
|
||||||
|
};
|
||||||
|
|
||||||
|
}}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<h4><a name="docstring_options-spec-ctors" id=
|
||||||
|
"docstring_options-spec-ctors"></a>Class
|
||||||
|
<code>docstring_options</code> constructors</h4>
|
||||||
|
<pre>
|
||||||
|
docstring_options(bool show_all=true);
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt><b>Effects:</b> Constructs a <code>docstring_options</code>
|
||||||
|
object which controls the appearance of function and
|
||||||
|
member-function docstrings defined in the code that follows. If
|
||||||
|
<code>show_all</code> is <code>true</code>, both the
|
||||||
|
user-defined docstrings and the automatically generated C++
|
||||||
|
signatures are shown. If <code>show_all</code> is
|
||||||
|
<code>false</code> the <code>__doc__</code> attributes are
|
||||||
|
<code>None</code>.</dt>
|
||||||
|
</dl>
|
||||||
|
<pre>
|
||||||
|
docstring_options(bool show_user_defined, bool show_signatures);
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt><b>Effects:</b> Constructs a <code>docstring_options</code>
|
||||||
|
object which controls the appearance of function and
|
||||||
|
member-function docstrings defined in the code that follows.
|
||||||
|
Iff <code>show_user_defined</code> is <code>true</code>, the
|
||||||
|
user-defined docstrings are shown. Iff
|
||||||
|
<code>show_signatures</code> is <code>true</code>, C++
|
||||||
|
signatures are automatically added. If both
|
||||||
|
<code>show_user_defined</code> and <code>show_signatures</code>
|
||||||
|
are <code>false</code>, the <code>__doc__</code> attributes are
|
||||||
|
<code>None</code>.</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h4><a name="docstring_options-spec-dtors" id=
|
||||||
|
"docstring_options-spec-dtors"></a>Class
|
||||||
|
<code>docstring_options</code> destructors</h4>
|
||||||
|
<pre>
|
||||||
|
~docstring_options();
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt><b>Effects:</b> Restores the previous state of the
|
||||||
|
docstring options. In particular, if
|
||||||
|
<code>docstring_options</code> instances are in nested C++
|
||||||
|
scopes the settings effective in the enclosing scope are
|
||||||
|
restored. If the last <code>docstring_options</code> instance
|
||||||
|
goes out of scope the default "all on" settings are
|
||||||
|
restored.</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h4><a name="docstring_options-spec-modifiers" id=
|
||||||
|
"docstring_options-spec-modifiers"></a>Class
|
||||||
|
<code>docstring_options</code> modifier functions</h4>
|
||||||
|
<pre>
|
||||||
|
void disable_user_defined();
|
||||||
|
void enable_user_defined();
|
||||||
|
void disable_signatures();
|
||||||
|
void enable_signatures();
|
||||||
|
void disable_all();
|
||||||
|
void enable_all();
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt>These member functions dynamically change the appearance of
|
||||||
|
docstrings in the code that follows. The
|
||||||
|
<code>*_user_defined()</code> and <code>*_signatures()</code>
|
||||||
|
member functions are provided for fine-grained control. The
|
||||||
|
<code>*_all()</code> member functions are convenient shortcuts
|
||||||
|
to manipulate both settings simultaneously.</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h2><a name="examples" id="examples"></a>Examples</h2>
|
||||||
|
|
||||||
|
<h4>Docstring options defined at compile time</h4>
|
||||||
|
<pre>
|
||||||
|
#include <boost/python/module.hpp>
|
||||||
|
#include <boost/python/def.hpp>
|
||||||
|
#include <boost/python/docstring_options.hpp>
|
||||||
|
|
||||||
|
void foo() {}
|
||||||
|
|
||||||
|
BOOST_PYTHON_MODULE(demo)
|
||||||
|
{
|
||||||
|
using namespace boost::python;
|
||||||
|
docstring_options doc_options(DEMO_DOCSTRING_SHOW_ALL);
|
||||||
|
def("foo", foo, "foo doc");
|
||||||
|
}
|
||||||
|
</pre>If compiled with <code>-DDEMO_DOCSTRING_SHOW_ALL=true</code>:
|
||||||
|
<pre>
|
||||||
|
>>> import demo
|
||||||
|
>>> print demo.foo.__doc__
|
||||||
|
foo doc
|
||||||
|
C++ signature:
|
||||||
|
foo(void) -> void
|
||||||
|
</pre>If compiled with
|
||||||
|
<code>-DDEMO_DOCSTRING_SHOW_ALL=false</code>:
|
||||||
|
<pre>
|
||||||
|
>>> import demo
|
||||||
|
>>> print demo.foo.__doc__
|
||||||
|
None
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<h4>Selective suppressions</h4>
|
||||||
|
<pre>
|
||||||
|
#include <boost/python/module.hpp>
|
||||||
|
#include <boost/python/def.hpp>
|
||||||
|
#include <boost/python/args.hpp>
|
||||||
|
#include <boost/python/docstring_options.hpp>
|
||||||
|
|
||||||
|
int foo1(int i) { return i; }
|
||||||
|
int foo2(long l) { return static_cast<int>(l); }
|
||||||
|
int foo3(float f) { return static_cast<int>(f); }
|
||||||
|
int foo4(double d) { return static_cast<int>(d); }
|
||||||
|
|
||||||
|
BOOST_PYTHON_MODULE(demo)
|
||||||
|
{
|
||||||
|
using namespace boost::python;
|
||||||
|
docstring_options doc_options;
|
||||||
|
def("foo1", foo1, arg("i"), "foo1 doc");
|
||||||
|
doc_options.disable_user_defined();
|
||||||
|
def("foo2", foo2, arg("l"), "foo2 doc");
|
||||||
|
doc_options.disable_signatures();
|
||||||
|
def("foo3", foo3, arg("f"), "foo3 doc");
|
||||||
|
doc_options.enable_user_defined();
|
||||||
|
def("foo4", foo4, arg("d"), "foo4 doc");
|
||||||
|
}
|
||||||
|
</pre>Python code:
|
||||||
|
<pre>
|
||||||
|
>>> import demo
|
||||||
|
>>> print demo.foo1.__doc__
|
||||||
|
foo1 doc
|
||||||
|
C++ signature:
|
||||||
|
foo1(int i) -> int
|
||||||
|
>>> print demo.foo2.__doc__
|
||||||
|
C++ signature:
|
||||||
|
foo2(long l) -> int
|
||||||
|
>>> print demo.foo3.__doc__
|
||||||
|
None
|
||||||
|
>>> print demo.foo4.__doc__
|
||||||
|
foo4 doc
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<h4>Wrapping from multiple C++ scopes</h4>
|
||||||
|
<pre>
|
||||||
|
#include <boost/python/module.hpp>
|
||||||
|
#include <boost/python/def.hpp>
|
||||||
|
#include <boost/python/args.hpp>
|
||||||
|
#include <boost/python/docstring_options.hpp>
|
||||||
|
|
||||||
|
int foo1(int i) { return i; }
|
||||||
|
int foo2(long l) { return static_cast<int>(l); }
|
||||||
|
|
||||||
|
int bar1(int i) { return i; }
|
||||||
|
int bar2(long l) { return static_cast<int>(l); }
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void wrap_foos()
|
||||||
|
{
|
||||||
|
using namespace boost::python;
|
||||||
|
// no docstring_options here
|
||||||
|
// -> settings from outer C++ scope are in effect
|
||||||
|
def("foo1", foo1, arg("i"), "foo1 doc");
|
||||||
|
def("foo2", foo2, arg("l"), "foo2 doc");
|
||||||
|
}
|
||||||
|
|
||||||
|
void wrap_bars()
|
||||||
|
{
|
||||||
|
using namespace boost::python;
|
||||||
|
bool show_user_defined = true;
|
||||||
|
bool show_signatures = false;
|
||||||
|
docstring_options doc_options(show_user_defined, show_signatures);
|
||||||
|
def("bar1", bar1, arg("i"), "bar1 doc");
|
||||||
|
def("bar2", bar2, arg("l"), "bar2 doc");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_PYTHON_MODULE(demo)
|
||||||
|
{
|
||||||
|
boost::python::docstring_options doc_options(false);
|
||||||
|
wrap_foos();
|
||||||
|
wrap_bars();
|
||||||
|
}
|
||||||
|
</pre>Python code:
|
||||||
|
<pre>
|
||||||
|
>>> import demo
|
||||||
|
>>> print demo.foo1.__doc__
|
||||||
|
None
|
||||||
|
>>> print demo.foo2.__doc__
|
||||||
|
None
|
||||||
|
>>> print demo.bar1.__doc__
|
||||||
|
bar1 doc
|
||||||
|
>>> print demo.bar2.__doc__
|
||||||
|
bar2 doc
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<h4>See also: <code>boost/libs/python/test/docstring.cpp</code>
|
||||||
|
and <code>docstring.py</code></h4>
|
||||||
|
|
||||||
|
<p>Revised
|
||||||
|
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
||||||
|
16 January, 2006
|
||||||
|
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||||
|
|
||||||
|
<p><i>© Copyright <a href=
|
||||||
|
"../../../../people/ralf_w_grosse_kunstleve.htm">Ralf W.
|
||||||
|
Grosse-Kunstleve</a> 2006.</i></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -134,7 +137,7 @@ void handle_exception() throw();
|
|||||||
"make_function.html#make_function-spec">make_function</a>()</code>,
|
"make_function.html#make_function-spec">make_function</a>()</code>,
|
||||||
<code><a href=
|
<code><a href=
|
||||||
"make_function.html#make_constructor-spec">make_constructor</a>()</code>,
|
"make_function.html#make_constructor-spec">make_constructor</a>()</code>,
|
||||||
<code><a href="def.html#def-spec">def</a>()</code> and <code><a href=
|
<code><a href="def.html#class_-spec-modifiers">def</a>()</code> and <code><a href=
|
||||||
"class.html#def-spec">class_::def</a>()</code>. The second form can be
|
"class.html#def-spec">class_::def</a>()</code>. The second form can be
|
||||||
more convenient to use (see the <a href="#examples">example</a> below),
|
more convenient to use (see the <a href="#examples">example</a> below),
|
||||||
but various compilers have problems when exceptions are rethrown from
|
but various compilers have problems when exceptions are rethrown from
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
163
doc/v2/exec.html
Normal file
163
doc/v2/exec.html
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
<link rel="stylesheet" type="text/css" href="../boost.css">
|
||||||
|
|
||||||
|
<title>Boost.Python - <boost/python/exec.hpp></title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
||||||
|
"header">
|
||||||
|
<tr>
|
||||||
|
<td valign="top" width="300">
|
||||||
|
<h3><a href="../../../../index.htm"><img height="86" width="277"
|
||||||
|
alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td valign="top">
|
||||||
|
<h1 align="center"><a href="../index.html">Boost.Python</a></h1>
|
||||||
|
|
||||||
|
<h2 align="center">Header <boost/python/exec.hpp></h2>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h2>Contents</h2>
|
||||||
|
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#introduction">Introduction</a></dt>
|
||||||
|
|
||||||
|
<dt><a href="#functions">Functions</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#eval-spec"><code>eval</code></a></dt>
|
||||||
|
<dt><a href="#exec-spec"><code>exec</code></a></dt>
|
||||||
|
<dt><a href="#exec_file-spec"><code>exec_file</code></a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
<dt><a href="#examples">Examples</a></dt>
|
||||||
|
</dl>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h2><a name="introduction"></a>Introduction</h2>
|
||||||
|
|
||||||
|
<p>Exposes a mechanism for embedding the python interpreter into C++ code.</p>
|
||||||
|
|
||||||
|
<h2><a name="functions"></a>Functions</h2>
|
||||||
|
|
||||||
|
<h3><a name="eval-spec"></a><code>eval</code></h3>
|
||||||
|
<pre>
|
||||||
|
object eval(str expression,
|
||||||
|
object globals = object(),
|
||||||
|
object locals = object());
|
||||||
|
</pre>
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt><b>Effects:</b>
|
||||||
|
Evaluate Python expression from <code>expression</code> in the context
|
||||||
|
specified by the dictionaries <code>globals</code> and <code>locals</code>.
|
||||||
|
</dt>
|
||||||
|
<dt><b>Returns:</b>
|
||||||
|
An instance of <a href="object.html#object-spec">object</a>
|
||||||
|
which holds the value of the expression.
|
||||||
|
</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h3><a name="exec-spec"></a><code>exec</code></h3>
|
||||||
|
<pre>
|
||||||
|
object exec(str code,
|
||||||
|
object globals = object(),
|
||||||
|
object locals = object());
|
||||||
|
</pre>
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt><b>Effects:</b>
|
||||||
|
Execute Python source code from <code>code</code> in the context
|
||||||
|
specified by the dictionaries <code>globals</code> and <code>locals</code>.
|
||||||
|
</dt>
|
||||||
|
<dt><b>Returns:</b>
|
||||||
|
An instance of <a href="object.html#object-spec">object</a>
|
||||||
|
which holds the result of executing the code.
|
||||||
|
</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h3><a name="exec_file-spec"></a><code>exec_file</code></h3>
|
||||||
|
<pre>
|
||||||
|
object exec_file(str filename,
|
||||||
|
object globals = object(),
|
||||||
|
object locals = object());
|
||||||
|
</pre>
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt><b>Effects:</b>
|
||||||
|
Execute Python source code from the file named by <code>filename</code>
|
||||||
|
in the context specified by the dictionaries <code>globals</code> and
|
||||||
|
<code>locals</code>.
|
||||||
|
</dt>
|
||||||
|
<dt><b>Returns:</b>
|
||||||
|
An instance of <a href="object.html#object-spec">object</a>
|
||||||
|
which holds the result of executing the code.
|
||||||
|
</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h2><a name="examples"></a>Examples</h2>
|
||||||
|
|
||||||
|
<para>The following example demonstrates the use of <function>import</function>
|
||||||
|
and <function>exec</function> to define a function in python, and later call
|
||||||
|
it from within C++.</para>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace boost::python;
|
||||||
|
|
||||||
|
void greet()
|
||||||
|
{
|
||||||
|
// Retrieve the main module.
|
||||||
|
object main = import("__main__");
|
||||||
|
|
||||||
|
// Retrieve the main module's namespace
|
||||||
|
object global(main.attr("__dict__"));
|
||||||
|
|
||||||
|
// Define greet function in Python.
|
||||||
|
object result = exec(
|
||||||
|
"def greet(): \n"
|
||||||
|
" return 'Hello from Python!' \n",
|
||||||
|
global, global);
|
||||||
|
|
||||||
|
// Create a reference to it.
|
||||||
|
object greet = global["greet"];
|
||||||
|
|
||||||
|
// Call it.
|
||||||
|
std::string message = extract<std::string>(greet());
|
||||||
|
std::cout << message << std::endl;
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<para>Instead of embedding the python script into a string,
|
||||||
|
we could also store it in an a file...</para>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
def greet():
|
||||||
|
return 'Hello from Python!'
|
||||||
|
</pre>
|
||||||
|
<para>... and execute that instead.</para>
|
||||||
|
<pre>
|
||||||
|
// ...
|
||||||
|
// Load the greet function from a file.
|
||||||
|
object result = exec_file(script, global, global);
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
<p>Revised 01 November, 2005</p>
|
||||||
|
|
||||||
|
<p><i>© Copyright Stefan Seefeld 2005.</i></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -67,8 +70,6 @@
|
|||||||
>error C2064: term does not evaluate to a function taking 2 arguments</a>
|
>error C2064: term does not evaluate to a function taking 2 arguments</a>
|
||||||
</dt>
|
</dt>
|
||||||
|
|
||||||
<dt><a href="#voidptr">How do I handle <tt>void *</tt> conversion?</a></dt>
|
|
||||||
|
|
||||||
<dt><a href="#custom_string"
|
<dt><a href="#custom_string"
|
||||||
>How can I automatically convert my custom string type to
|
>How can I automatically convert my custom string type to
|
||||||
and from a Python string?</a></dt>
|
and from a Python string?</a></dt>
|
||||||
@@ -568,7 +569,7 @@ handle<> f_wrap()
|
|||||||
...
|
...
|
||||||
|
|
||||||
def("f", f_wrap());
|
def("f", f_wrap());
|
||||||
class_<X,X_wrap>("X", init<int>())
|
class_<X,X_wrap,boost::noncopyable>("X", init<int>())
|
||||||
...
|
...
|
||||||
;
|
;
|
||||||
</pre>
|
</pre>
|
||||||
@@ -693,29 +694,6 @@ void Export_FXThread()
|
|||||||
.def("setAutoDelete", (bool (FXThread::*)(bool)) &FXThread::setAutoDelete)</pre>
|
.def("setAutoDelete", (bool (FXThread::*)(bool)) &FXThread::setAutoDelete)</pre>
|
||||||
<p>(The bug has been reported to Microsoft.)</p>
|
<p>(The bug has been reported to Microsoft.)</p>
|
||||||
|
|
||||||
<hr>
|
|
||||||
<h2><a name="voidptr"></a>How do I handle <tt>void *</tt> conversion?</h2>
|
|
||||||
<font size="-1"><i>Niall Douglas provides these notes:</i></font><p>
|
|
||||||
For several reasons Boost.Python does not support <tt>void *</tt> as
|
|
||||||
an argument or as a return value. However, it is possible to wrap
|
|
||||||
functions with <tt>void *</tt> arguments or return values using
|
|
||||||
thin wrappers and the <i>opaque pointer</i> facility. E.g.:
|
|
||||||
<pre>// Declare the following in each translation unit
|
|
||||||
struct void_ {};
|
|
||||||
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(void_);
|
|
||||||
|
|
||||||
void *foo(int par1, void *par2);
|
|
||||||
|
|
||||||
void_ *foo_wrapper(int par1, void_ *par2)
|
|
||||||
{
|
|
||||||
return (void_ *) foo(par1, par2);
|
|
||||||
}
|
|
||||||
...
|
|
||||||
BOOST_PYTHON_MODULE(bar)
|
|
||||||
{
|
|
||||||
def("foo", &foo_wrapper);
|
|
||||||
}</pre>
|
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
<h2><a name="custom_string"></a>How can I automatically
|
<h2><a name="custom_string"></a>How can I automatically
|
||||||
convert my custom string type to and from a Python string?</h2>
|
convert my custom string type to and from a Python string?</h2>
|
||||||
@@ -873,11 +851,11 @@ BOOST_PYTHON_MODULE(custom_string)
|
|||||||
|
|
||||||
<p>Revised
|
<p>Revised
|
||||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
||||||
28 January, 2004
|
12 March, 2006
|
||||||
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p><i>© Copyright <a href=
|
<p><i>© Copyright <a href=
|
||||||
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002-2003.</i></p>
|
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002-2006.</i></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -361,6 +361,7 @@
|
|||||||
|
|
||||||
|
|
||||||
<p class="c3">© Copyright <a href=
|
<p class="c3">© Copyright <a href=
|
||||||
"../../../../people/dave_abrahams.htm">Dave Abrahams</a>
|
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002. Distributed
|
||||||
2002.
|
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)</p>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -110,7 +113,7 @@ using namespace boost::python;
|
|||||||
struct X
|
struct X
|
||||||
{
|
{
|
||||||
X(int x) : v(x) {}
|
X(int x) : v(x) {}
|
||||||
operator int() { return v; }
|
operator int() const { return v; }
|
||||||
int v;
|
int v;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
90
doc/v2/import.html
Normal file
90
doc/v2/import.html
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
<link rel="stylesheet" type="text/css" href="../boost.css">
|
||||||
|
|
||||||
|
<title>Boost.Python - <boost/python/import.hpp></title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
||||||
|
"header">
|
||||||
|
<tr>
|
||||||
|
<td valign="top" width="300">
|
||||||
|
<h3><a href="../../../../index.htm"><img height="86" width="277"
|
||||||
|
alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td valign="top">
|
||||||
|
<h1 align="center"><a href="../index.html">Boost.Python</a></h1>
|
||||||
|
|
||||||
|
<h2 align="center">Header <boost/python/import.hpp></h2>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h2>Contents</h2>
|
||||||
|
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#introduction">Introduction</a></dt>
|
||||||
|
|
||||||
|
<dt><a href="#functions">Functions</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#import-spec"><code>import</code></a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
<dt><a href="#examples">Examples</a></dt>
|
||||||
|
</dl>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h2><a name="introduction"></a>Introduction</h2>
|
||||||
|
|
||||||
|
<p>Exposes a mechanism for importing python modules.</p>
|
||||||
|
|
||||||
|
<h2><a name="functions"></a>Functions</h2>
|
||||||
|
|
||||||
|
<h3><a name="import-spec"></a><code>import</code></h3>
|
||||||
|
<pre>
|
||||||
|
object import(str name);
|
||||||
|
</pre>
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt><b>Effects:</b> Imports the module named by <code>name</code>.</dt>
|
||||||
|
<dt><b>Returns:</b> An instance of <a href="object.html#object-spec">object</a>
|
||||||
|
which holds a reference to the imported module.</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h2><a name="examples"></a>Examples</h2>
|
||||||
|
|
||||||
|
<para>The following example demonstrates the use of <function>import</function>
|
||||||
|
to access a function in python, and later call it from within C++.</para>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace boost::python;
|
||||||
|
|
||||||
|
void print_python_version()
|
||||||
|
{
|
||||||
|
// Load the sys module.
|
||||||
|
object sys = import("sys");
|
||||||
|
|
||||||
|
// Extract the python version.
|
||||||
|
std::string version = extract<std::string>(sys.attr("version"));
|
||||||
|
std::cout << version << std::endl;
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
<p>Revised 01 November, 2005</p>
|
||||||
|
|
||||||
|
<p><i>© Copyright Stefan Seefeld 2005.</i></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -66,8 +69,8 @@
|
|||||||
</dt>
|
</dt>
|
||||||
<dt>
|
<dt>
|
||||||
<a href="#vector_indexing_suite_class">vector_indexing_suite
|
<a href="#vector_indexing_suite_class">vector_indexing_suite
|
||||||
class</a>
|
class<br>
|
||||||
</dt>
|
</a><a href="#map_indexing_suite_class">map_indexing_suite class</a> </dt>
|
||||||
</dl>
|
</dl>
|
||||||
<hr>
|
<hr>
|
||||||
<h2>
|
<h2>
|
||||||
@@ -141,7 +144,7 @@
|
|||||||
<h3> <a name="indexing_suite"></a>indexing_suite [ Header <boost/python/indexing/indexing_suite.hpp>
|
<h3> <a name="indexing_suite"></a>indexing_suite [ Header <boost/python/indexing/indexing_suite.hpp>
|
||||||
]</h3>
|
]</h3>
|
||||||
<p>
|
<p>
|
||||||
The <tt>indexing_suite</tt> class is the base protocol class for the
|
The <tt>indexing_suite</tt> class is the base class for the
|
||||||
management of C++ containers intended to be integrated to Python. The
|
management of C++ containers intended to be integrated to Python. The
|
||||||
objective is make a C++ container look and feel and behave exactly as
|
objective is make a C++ container look and feel and behave exactly as
|
||||||
we'd expect a Python container. The class automatically wraps these
|
we'd expect a Python container. The class automatically wraps these
|
||||||
@@ -275,8 +278,7 @@
|
|||||||
The <tt>vector_indexing_suite</tt> class is a predefined
|
The <tt>vector_indexing_suite</tt> class is a predefined
|
||||||
<tt>indexing_suite</tt> derived class designed to wrap
|
<tt>indexing_suite</tt> derived class designed to wrap
|
||||||
<tt>std::vector</tt> (and <tt>std::vector</tt> like [i.e. a class with
|
<tt>std::vector</tt> (and <tt>std::vector</tt> like [i.e. a class with
|
||||||
std::vector interface]) classes (currently, this is the only predefined
|
std::vector interface]) classes. It provides all the policies required by the
|
||||||
suite available). It provides all the policies required by the
|
|
||||||
<tt>indexing_suite</tt>.
|
<tt>indexing_suite</tt>.
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
@@ -295,22 +297,32 @@
|
|||||||
<a href="../../test/vector_indexing_suite.cpp">example in full</a>,
|
<a href="../../test/vector_indexing_suite.cpp">example in full</a>,
|
||||||
along with its <a href="../../test/vector_indexing_suite.py">python
|
along with its <a href="../../test/vector_indexing_suite.py">python
|
||||||
test</a>).
|
test</a>).
|
||||||
</p>
|
</p>
|
||||||
|
<h3><a name="map_indexing_suite" id="map_indexing_suite"></a>map_indexing_suite [ Header <boost/python/indexing/map_indexing_suite.hpp> ] </h3>
|
||||||
|
<p> The <tt>map_indexing_suite</tt> class is a predefined <tt>indexing_suite</tt> derived class designed to wrap <tt>std::map</tt> (and <tt>std::map</tt> like [i.e. a class with std::map interface]) classes. It provides all the policies required by the <tt>indexing_suite</tt>. </p>
|
||||||
|
<p> Example usage: </p>
|
||||||
|
<pre>
|
||||||
|
class X {...};
|
||||||
|
...
|
||||||
|
|
||||||
|
class_<std::map<X> >("XMap")
|
||||||
|
.def(map_indexing_suite<std::map<X> >())
|
||||||
|
;
|
||||||
|
</pre>
|
||||||
|
<p> By default indexed elements are returned by proxy. This can be disabled by supplying <tt>true</tt> in the NoProxy template parameter. <tt>XMap</tt> is now a full-fledged Python container (see the <a href="../../test/map_indexing_suite.cpp">example in full</a>, along with its <a href="../../test/map_indexing_suite.py">python test</a>).</p>
|
||||||
<hr>
|
<hr>
|
||||||
<h2>
|
<h2>
|
||||||
<a name="indexing_suite_class"></a>indexing_suite class
|
<a name="indexing_suite_class"></a>indexing_suite class </h2>
|
||||||
</h2>
|
<h2> <tt>indexing_suite<<br>
|
||||||
<h3>
|
</tt><tt>class Container<br>
|
||||||
<br>
|
|
||||||
<tt>indexing_suite<<br>
|
|
||||||
class Container<br>
|
|
||||||
, class DerivedPolicies<font color="#007F00"><br>
|
, class DerivedPolicies<font color="#007F00"><br>
|
||||||
</font></tt> <tt>,
|
</font></tt> <tt>,
|
||||||
bool NoProxy<br>
|
bool NoProxy<br>
|
||||||
, class Element<br>
|
,
|
||||||
, class Key<br>
|
bool NoSlice<br>
|
||||||
, class Index</tt>
|
</tt><tt>, class Data<br>
|
||||||
</h3>
|
, class Index<br>
|
||||||
|
</tt><tt>, class Key</tt></h2>
|
||||||
<table width="100%" border="1">
|
<table width="100%" border="1">
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
@@ -355,36 +367,45 @@
|
|||||||
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td> <font color="#007F00"><tt>NoProxy</tt></font> </td>
|
||||||
|
<td> A boolean </td>
|
||||||
|
<td> By default indexed elements have Python reference semantics and are returned by proxy. This can be disabled by supplying <strong>true</strong> in the <tt>NoProxy</tt> template parameter. </td>
|
||||||
|
<td> false </td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<font color="#007F00"><tt>NoProxy</tt></font>
|
<font color="#007F00"><tt>NoSlice</tt></font>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
A boolean
|
A boolean
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
By default indexed elements have Python reference semantics and are
|
Do not allow slicing. </td>
|
||||||
returned by proxy. This can be disabled by supplying
|
|
||||||
<strong>true</strong> in the <tt>NoProxy</tt> template parameter.
|
|
||||||
</td>
|
|
||||||
<td>
|
<td>
|
||||||
false
|
false
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<font color="#007F00"><tt>Element</tt></font>
|
<font color="#007F00"><tt>Data</tt></font>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
|
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
The container's element type.
|
The container's data type.
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<tt>Container::value_type</tt>
|
<tt>Container::value_type</tt>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td> <font color="#007F00"><tt>Index</tt></font> </td>
|
||||||
|
<td> </td>
|
||||||
|
<td> The container's index type. </td>
|
||||||
|
<td> <tt>Container::size_type</tt> </td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<font color="#007F00"><tt>Key</tt></font>
|
<font color="#007F00"><tt>Key</tt></font>
|
||||||
@@ -399,28 +420,14 @@
|
|||||||
<tt>Container::value_type</tt>
|
<tt>Container::value_type</tt>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
|
||||||
<td>
|
|
||||||
<font color="#007F00"><tt>Index</tt></font>
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
The container's index type.
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<tt>Container::size_type</tt>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
</table>
|
||||||
<pre>
|
<pre>
|
||||||
template <<br> class Container
|
template <<br> class Container
|
||||||
, class DerivedPolicies
|
, class DerivedPolicies
|
||||||
, bool NoProxy = false
|
, bool NoProxy = false<br> , bool NoSlice = false
|
||||||
, class Element = typename Container::value_type
|
, class Data = typename Container::value_type
|
||||||
, class Key = typename Container::value_type
|
|
||||||
, class Index = typename Container::size_type
|
, class Index = typename Container::size_type
|
||||||
|
, class Key = typename Container::value_type
|
||||||
><br> class indexing_suite
|
><br> class indexing_suite
|
||||||
: unspecified
|
: unspecified
|
||||||
{
|
{
|
||||||
@@ -435,23 +442,22 @@
|
|||||||
<dl>
|
<dl>
|
||||||
<dd>
|
<dd>
|
||||||
Derived classes provide the hooks needed by
|
Derived classes provide the hooks needed by
|
||||||
the<tt>indexing_suite:</tt>
|
the <tt>indexing_suite:</tt>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
<pre>
|
<pre> data_type&
|
||||||
static element_type&
|
|
||||||
get_item(Container& container, index_type i);
|
get_item(Container& container, index_type i);
|
||||||
|
|
||||||
static object
|
static object
|
||||||
get_slice(Container& container, index_type from, index_type to);
|
get_slice(Container& container, index_type from, index_type to);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
set_item(Container& container, index_type i, element_type const& v);
|
set_item(Container& container, index_type i, data_type const& v);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
set_slice(
|
set_slice(
|
||||||
Container& container, index_type from,
|
Container& container, index_type from,
|
||||||
index_type to, element_type const& v
|
index_type to, data_type const& v
|
||||||
);
|
);
|
||||||
|
|
||||||
template <class Iter>
|
template <class Iter>
|
||||||
@@ -593,16 +599,16 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
<pre>
|
<pre>
|
||||||
template <<br> class Container,<br> bool NoProxy = false,<br> class DerivedPolicies = unspecified_default<br> class vector_indexing_suite<br> : public indexing_suite<Container, DerivedPolicies, NoProxy><br> {<br> public:<br><br> typedef typename Container::value_type element_type;<br> typedef typename Container::value_type key_type;<br> typedef typename Container::size_type index_type;<br> typedef typename Container::size_type size_type;<br> typedef typename Container::difference_type difference_type;<br> <br> static element_type&<br> get_item(Container& container, index_type i);
|
template <<br> class Container,<br> bool NoProxy = false,<br> class DerivedPolicies = unspecified_default<br> class vector_indexing_suite : unspecified_base<br> {<br> public:<br><br> typedef typename Container::value_type data_type;<br> typedef typename Container::value_type key_type;<br> typedef typename Container::size_type index_type;<br> typedef typename Container::size_type size_type;<br> typedef typename Container::difference_type difference_type;<br> <br> data_type&<br> get_item(Container& container, index_type i);
|
||||||
|
|
||||||
static object
|
static object
|
||||||
get_slice(Container& container, index_type from, index_type to);
|
get_slice(Container& container, index_type from, index_type to);
|
||||||
|
|
||||||
static void<br> set_item(Container& container, index_type i, element_type const& v);
|
static void<br> set_item(Container& container, index_type i, data_type const& v);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
set_slice(Container& container, index_type from,
|
set_slice(Container& container, index_type from,
|
||||||
index_type to, element_type const& v);
|
index_type to, data_type const& v);
|
||||||
|
|
||||||
template <class Iter><br> static void<br> set_slice(Container& container, index_type from,<br> index_type to, Iter first, Iter last);
|
template <class Iter><br> static void<br> set_slice(Container& container, index_type from,<br> index_type to, Iter first, Iter last);
|
||||||
|
|
||||||
@@ -624,7 +630,60 @@
|
|||||||
adjust_index(index_type current, index_type from,
|
adjust_index(index_type current, index_type from,
|
||||||
index_type to, size_type len);
|
index_type to, size_type len);
|
||||||
};
|
};
|
||||||
</pre>
|
|
||||||
|
</pre>
|
||||||
|
<h2><a name="vector_indexing_suite_class"></a>map_indexing_suite class </h2>
|
||||||
|
<h3> Class template <tt><br>
|
||||||
|
map_indexing_suite<<br>
|
||||||
|
class <font color="#007F00">Container</font><br>
|
||||||
|
, bool <font color="#007F00">NoProxy</font><br>
|
||||||
|
, class <font color="#007F00">DerivedPolicies</font>></tt> </h3>
|
||||||
|
<table width="100%" border="1">
|
||||||
|
<tr>
|
||||||
|
<td> <strong>Template Parameter</strong><br>
|
||||||
|
</td>
|
||||||
|
<td> <strong>Requirements</strong> </td>
|
||||||
|
<td> <strong>Semantics</strong> </td>
|
||||||
|
<td> <strong>Default</strong> </td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td> <font color="#007F00"><tt>Container</tt></font> </td>
|
||||||
|
<td> A class type </td>
|
||||||
|
<td> The container type to be wrapped to Python. </td>
|
||||||
|
<td> </td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td> <font color="#007F00"><tt>NoProxy</tt></font> </td>
|
||||||
|
<td> A boolean </td>
|
||||||
|
<td> By default indexed elements have Python reference semantics and are returned by proxy. This can be disabled by supplying <strong>true</strong> in the <tt>NoProxy</tt> template parameter. </td>
|
||||||
|
<td> false </td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td> <font color="#007F00"><tt>DerivedPolicies</tt></font> </td>
|
||||||
|
<td> A subclass of indexing_suite </td>
|
||||||
|
<td> The <tt>vector_indexing_suite</tt> may still be derived to further tweak any of the predefined policies. Static polymorphism through CRTP (James Coplien. "Curiously Recurring Template Pattern". C++ Report, Feb. 1995) enables the base <tt>indexing_suite</tt> class to call policy function of the most derived class </td>
|
||||||
|
<td> </td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<pre>
|
||||||
|
template <<br> class Container,<br> bool NoProxy = false,<br> class DerivedPolicies = unspecified_default<br> class map_indexing_suite : unspecified_base<br> {<br> public:<br><br> typedef typename Container::value_type value_type;<br> typedef typename Container::value_type::second_type data_type;<br> typedef typename Container::key_type key_type;<br> typedef typename Container::key_type index_type;<br> typedef typename Container::size_type size_type;<br> typedef typename Container::difference_type difference_type;<br><br> static data_type&<br> get_item(Container& container, index_type i);
|
||||||
|
|
||||||
|
static void<br> set_item(Container& container, index_type i, data_type const& v);
|
||||||
|
|
||||||
|
static void
|
||||||
|
delete_item(Container& container, index_type i);<br>
|
||||||
|
static size_t
|
||||||
|
size(Container& container);
|
||||||
|
|
||||||
|
static bool
|
||||||
|
contains(Container& container, key_type const& key);
|
||||||
|
|
||||||
|
static bool<br> compare_index(Container& container, index_type a, index_type b);
|
||||||
|
<br> static index_type
|
||||||
|
convert_index(Container& container, PyObject* i);
|
||||||
|
};
|
||||||
|
|
||||||
|
</pre>
|
||||||
<hr>
|
<hr>
|
||||||
© Copyright Joel de Guzman 2003. Permission to copy, use, modify,
|
© Copyright Joel de Guzman 2003. Permission to copy, use, modify,
|
||||||
sell and distribute this document is granted provided this copyright
|
sell and distribute this document is granted provided this copyright
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -45,7 +48,7 @@
|
|||||||
<dt><a href="#iterator-spec-synopsis">Class
|
<dt><a href="#iterator-spec-synopsis">Class
|
||||||
<code>iterator</code> synopsis</a></dt>
|
<code>iterator</code> synopsis</a></dt>
|
||||||
|
|
||||||
<dt><a href="#iterator-spec-ctors">Class template
|
<dt><a href="#iterator-spec-constructors">Class template
|
||||||
<code>iterator</code> constructor</a></dt>
|
<code>iterator</code> constructor</a></dt>
|
||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ This macro generates two functions in the scope where it is used:
|
|||||||
and <code>void init_module_<i>name</i>()</code>, whose body must
|
and <code>void init_module_<i>name</i>()</code>, whose body must
|
||||||
follow the macro invocation. <code>init_<i>name</i></code> passes
|
follow the macro invocation. <code>init_<i>name</i></code> passes
|
||||||
<code>init_module_<i>name</i></code> to <code><a
|
<code>init_module_<i>name</i></code> to <code><a
|
||||||
href="errors.html#handle_exception">handle_exception</a>()</code> so
|
href="errors.html#handle_exception-spec">handle_exception</a>()</code> so
|
||||||
that any C++ exceptions generated are safely processeed. During the
|
that any C++ exceptions generated are safely processeed. During the
|
||||||
body of <code>init_<i>name</i></code>, the current <code><a
|
body of <code>init_<i>name</i></code>, the current <code><a
|
||||||
href="scope.html#scope-spec">scope</a></code> refers to the module
|
href="scope.html#scope-spec">scope</a></code> refers to the module
|
||||||
@@ -104,5 +104,7 @@ RuntimeError: Unidentifiable C++ Exception
|
|||||||
|
|
||||||
|
|
||||||
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
||||||
Abrahams</a> 2002. </i>
|
Abrahams</a> 2002. </i> 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)</p>
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,25 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
<!-- 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) -->
|
||||||
|
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
"HTML Tidy for Windows (vers 1st August 2002), see www.w3.org">
|
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
|
||||||
<link rel="stylesheet" type="text/css" href="../boost.css">
|
<link rel="stylesheet" type="text/css" href="../boost.css">
|
||||||
|
|
||||||
<title>Boost.Python - <boost/python/numeric.hpp></title>
|
<title>Boost.Python - <boost/python/numeric.hpp></title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
||||||
"header">
|
"header">
|
||||||
<tr>
|
<tr>
|
||||||
<td valign="top" width="300">
|
<td valign="top" width="300">
|
||||||
<h3><a href="../../../../index.htm"><img height="86" width="277"
|
<h3><a href="../../../../index.htm"><img height="86" width="277" alt=
|
||||||
alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3>
|
"C++ Boost" src="../../../../boost.png" border="0"></a></h3>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
<td valign="top">
|
<td valign="top">
|
||||||
@@ -47,8 +50,8 @@
|
|||||||
<dt><a href="#array-spec-observers">Class <code>array</code>
|
<dt><a href="#array-spec-observers">Class <code>array</code>
|
||||||
observer functions</a></dt>
|
observer functions</a></dt>
|
||||||
|
|
||||||
<dt><a href="#array-spec-statics">Class <code>array</code>
|
<dt><a href="#array-spec-statics">Class <code>array</code> static
|
||||||
static functions</a></dt>
|
functions</a></dt>
|
||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
@@ -58,7 +61,7 @@
|
|||||||
</dl>
|
</dl>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<h2><a name="introduction"></a>Introduction</h2>
|
<h2><a name="introduction" id="introduction"></a>Introduction</h2>
|
||||||
|
|
||||||
<p>Exposes a <a href=
|
<p>Exposes a <a href=
|
||||||
"ObjectWrapper.html#TypeWrapper-concept">TypeWrapper</a> for the Python
|
"ObjectWrapper.html#TypeWrapper-concept">TypeWrapper</a> for the Python
|
||||||
@@ -66,9 +69,9 @@
|
|||||||
"http://www.python.org/dev/doc/devel/lib/typesmapping.html">array</a>
|
"http://www.python.org/dev/doc/devel/lib/typesmapping.html">array</a>
|
||||||
type.</p>
|
type.</p>
|
||||||
|
|
||||||
<h2><a name="classes"></a>Classes</h2>
|
<h2><a name="classes" id="classes"></a>Classes</h2>
|
||||||
|
|
||||||
<h3><a name="array-spec"></a>Class <code>array</code></h3>
|
<h3><a name="array-spec" id="array-spec"></a>Class <code>array</code></h3>
|
||||||
|
|
||||||
<p>Provides access to the array types of <a href=
|
<p>Provides access to the array types of <a href=
|
||||||
"http://www.pfdubois.com/numpy/">Numerical Python</a>'s <a href=
|
"http://www.pfdubois.com/numpy/">Numerical Python</a>'s <a href=
|
||||||
@@ -76,27 +79,27 @@
|
|||||||
"http://stsdas.stsci.edu/numarray/index.html">NumArray</a> modules. With
|
"http://stsdas.stsci.edu/numarray/index.html">NumArray</a> modules. With
|
||||||
the exception of the functions documented <a href=
|
the exception of the functions documented <a href=
|
||||||
"#array-spec-observers">below</a>, the semantics of the constructors and
|
"#array-spec-observers">below</a>, the semantics of the constructors and
|
||||||
member functions defined below can be fully understood by reading the <a
|
member functions defined below can be fully understood by reading the
|
||||||
href="ObjectWrapper.html#TypeWrapper-concept">TypeWrapper</a> concept
|
<a href="ObjectWrapper.html#TypeWrapper-concept">TypeWrapper</a> concept
|
||||||
definition. Since <code>array</code> is publicly derived from <code><a
|
definition. Since <code>array</code> is publicly derived from
|
||||||
href="object.html#object-spec">object</a></code>, the public object
|
<code><a href="object.html#object-spec">object</a></code>, the public
|
||||||
interface applies to <code>array</code> instances as well.</p>
|
object interface applies to <code>array</code> instances as well.</p>
|
||||||
|
|
||||||
<p><a name="default_search"></a>The default behavior is to use
|
<p><a name="default_search" id="default_search"></a>The default behavior is
|
||||||
<code>numarray.NDArray</code> as the associated Python type if the
|
to use <code>numarray.NDArray</code> as the associated Python type if the
|
||||||
<code>numarray</code> module is installed in the default location.
|
<code>numarray</code> module is installed in the default location.
|
||||||
Otherwise it falls back to use <code>Numeric.ArrayType</code>. If neither
|
Otherwise it falls back to use <code>Numeric.ArrayType</code>. If neither
|
||||||
extension module is installed, conversions to arguments of type
|
extension module is installed, overloads of wrapped C++ functions with
|
||||||
<code>numeric::array</code> will cause overload resolution to reject the
|
<code>numeric::array</code> parameters will never be matched, and other
|
||||||
overload, and other attempted uses of <code>numeric::array</code> will <a
|
attempted uses of <code>numeric::array</code> will <a href=
|
||||||
href="definitions.html#raise">raise</a> an appropriate Python exception.
|
"definitions.html#raise">raise</a> an appropriate Python exception. The
|
||||||
The associated Python type can be set manually using the <code><a href=
|
associated Python type can be set manually using the <code><a href=
|
||||||
"#array-spec-statics">set_module_and_type</a>(...)</code> static
|
"#array-spec-statics">set_module_and_type</a>(...)</code> static
|
||||||
function.</p>
|
function.</p>
|
||||||
|
|
||||||
<h4><a name="array-spec-synopsis"></a>Class <code>array</code>
|
<h4><a name="array-spec-synopsis" id="array-spec-synopsis"></a>Class
|
||||||
synopsis</h4>
|
<code>array</code> synopsis</h4>
|
||||||
<pre>
|
<pre>
|
||||||
namespace boost { namespace python { namespace numeric
|
namespace boost { namespace python { namespace numeric
|
||||||
{
|
{
|
||||||
class array : public object
|
class array : public object
|
||||||
@@ -107,7 +110,7 @@ namespace boost { namespace python { namespace numeric
|
|||||||
object astype(Type const& type_);
|
object astype(Type const& type_);
|
||||||
|
|
||||||
template <class Type>
|
template <class Type>
|
||||||
object new_(Type const& type_) const;
|
array new_(Type const& type_) const;
|
||||||
|
|
||||||
template <class Sequence>
|
template <class Sequence>
|
||||||
void resize(Sequence const& x);
|
void resize(Sequence const& x);
|
||||||
@@ -133,14 +136,14 @@ namespace boost { namespace python { namespace numeric
|
|||||||
void tofile(File const& f) const;
|
void tofile(File const& f) const;
|
||||||
|
|
||||||
object factory();
|
object factory();
|
||||||
template <class Buffer>
|
template <class Sequence>
|
||||||
object factory(Buffer const&);
|
object factory(Sequence const&);
|
||||||
template <class Buffer, class Type>
|
template <class Sequence, class Typecode>
|
||||||
object factory(Buffer const&, Type const&);
|
object factory(Sequence const&, Typecode const&, bool copy = true, bool savespace = false);
|
||||||
template <class Buffer, class Type, class Shape>
|
template <class Sequence, class Typecode, class Type>
|
||||||
object factory(Buffer const&, Type const&, Shape const&, bool copy = true, bool savespace = false);
|
object factory(Sequence const&, Typecode const&, bool copy, bool savespace, Type const&);
|
||||||
template <class Buffer, class Type, class Shape>
|
template <class Sequence, class Typecode, class Type, class Shape>
|
||||||
object factory(Buffer const&, Type const&, Shape const&, bool copy, bool savespace, char typecode);
|
object factory(Sequence const&, Typecode const&, bool copy, bool savespace, Type const&, Shape const&);
|
||||||
|
|
||||||
template <class T1>
|
template <class T1>
|
||||||
explicit array(T1 const& x1);
|
explicit array(T1 const& x1);
|
||||||
@@ -152,6 +155,7 @@ namespace boost { namespace python { namespace numeric
|
|||||||
|
|
||||||
static void set_module_and_type();
|
static void set_module_and_type();
|
||||||
static void set_module_and_type(char const* package_path = 0, char const* type_name = 0);
|
static void set_module_and_type(char const* package_path = 0, char const* type_name = 0);
|
||||||
|
static void get_module_name();
|
||||||
|
|
||||||
object argmax(long axis=-1);
|
object argmax(long axis=-1);
|
||||||
|
|
||||||
@@ -200,34 +204,32 @@ namespace boost { namespace python { namespace numeric
|
|||||||
}}}
|
}}}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h4><a name="array-spec-observers"></a>Class <code>array</code> observer
|
<h4><a name="array-spec-observers" id="array-spec-observers"></a>Class
|
||||||
functions</h4>
|
<code>array</code> observer functions</h4>
|
||||||
<pre>
|
<pre>
|
||||||
object factory();
|
object factory();
|
||||||
template <class Buffer>
|
template <class Sequence>
|
||||||
object factory(Buffer const&);
|
object factory(Sequence const&);
|
||||||
template <class Buffer, class Type>
|
template <class Sequence, class Typecode>
|
||||||
object factory(Buffer const&, Type const&);
|
object factory(Sequence const&, Typecode const&, bool copy = true, bool savespace = false);
|
||||||
template <class Buffer, class Type, class Shape>
|
template <class Sequence, class Typecode, class Type>
|
||||||
object factory(Buffer const&, Type const&, Shape const&, bool copy = true, bool savespace = false);
|
object factory(Sequence const&, Typecode const&, bool copy, bool savespace, Type const&);
|
||||||
template <class Buffer, class Type, class Shape>
|
template <class Sequence, class Typecode, class Type, class Shape>
|
||||||
object factory(Buffer const&, Type const&, Shape const&, bool copy, bool savespace, char typecode);
|
object factory(Sequence const&, Typecode const&, bool copy, bool savespace, Type const&, Shape const&);
|
||||||
</pre>
|
</pre>These functions map to the underlying array type's <code>array()</code>
|
||||||
These functions map to the underlying array type's <code>array()</code>
|
function family. They are not called "<code>array</code>" because of the C++
|
||||||
function family. They are not called "<code>array</code>" because of the
|
limitation that you can't define a member function with the same name as its
|
||||||
C++ limitation that you can't define a member function with the same name
|
enclosing class.
|
||||||
as its enclosing class.
|
<pre>
|
||||||
<pre>
|
|
||||||
template <class Type>
|
template <class Type>
|
||||||
object new_(Type const&) const;
|
array new_(Type const&) const;
|
||||||
</pre>
|
</pre>This function maps to the underlying array type's <code>new()</code>
|
||||||
This function maps to the underlying array type's <code>new()</code>
|
function. It is not called "<code>new</code>" because that is a keyword in
|
||||||
function. It is not called "<code>new</code>" because that is a keyword
|
C++.
|
||||||
in C++.
|
|
||||||
|
|
||||||
<h4><a name="array-spec-statics"></a>Class <code>array</code> static
|
<h4><a name="array-spec-statics" id="array-spec-statics"></a>Class
|
||||||
functions</h4>
|
<code>array</code> static functions</h4>
|
||||||
<pre>
|
<pre>
|
||||||
static void set_module_and_type(char const* package_path, char const* type_name);
|
static void set_module_and_type(char const* package_path, char const* type_name);
|
||||||
static void set_module_and_type();
|
static void set_module_and_type();
|
||||||
</pre>
|
</pre>
|
||||||
@@ -238,16 +240,24 @@ static void set_module_and_type();
|
|||||||
"definitions.html#ntbs">ntbs</a>.</dt>
|
"definitions.html#ntbs">ntbs</a>.</dt>
|
||||||
|
|
||||||
<dt><b>Effects:</b> The first form sets the package path of the module
|
<dt><b>Effects:</b> The first form sets the package path of the module
|
||||||
which supplies the type named by <code>type_name</code> to
|
that supplies the type named by <code>type_name</code> to
|
||||||
<code>package_path</code>. The second form restores the <a href=
|
<code>package_path</code>. The second form restores the <a href=
|
||||||
"#default_search">default search behavior</a>. The associated Python
|
"#default_search">default search behavior</a>. The associated Python type
|
||||||
type will be searched for only the first time it is needed, and
|
will be searched for only the first time it is needed, and thereafter the
|
||||||
thereafter the first time it is needed after an invocation of
|
first time it is needed after an invocation of
|
||||||
<code>set_module_and_type</code>.</dt>
|
<code>set_module_and_type</code>.</dt>
|
||||||
</dl>
|
</dl>
|
||||||
|
<pre>
|
||||||
|
static std::string get_module_name()
|
||||||
|
</pre>
|
||||||
|
|
||||||
<h2><a name="examples"></a>Example</h2>
|
<dl class="function-semantics">
|
||||||
<pre>
|
<dt><b>Effects:</b> Returns the name of the module containing the class
|
||||||
|
that will be held by new <code>numeric::array</code> instances.</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h2><a name="examples" id="examples"></a>Example</h2>
|
||||||
|
<pre>
|
||||||
#include <boost/python/numeric.hpp>
|
#include <boost/python/numeric.hpp>
|
||||||
#include <boost/python/tuple.hpp>
|
#include <boost/python/tuple.hpp>
|
||||||
|
|
||||||
@@ -258,10 +268,9 @@ void set_first_element(numeric::array& y, double value)
|
|||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>Revised 03 October, 2002</p>
|
<p>Revised 07 October, 2006</p>
|
||||||
|
|
||||||
<p><i>© Copyright <a href=
|
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
||||||
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
|
Abrahams</a> 2002-2006.</i></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -183,10 +186,15 @@
|
|||||||
<dt><a href="#binary-spec">binary operations</a></dt>
|
<dt><a href="#binary-spec">binary operations</a></dt>
|
||||||
|
|
||||||
<dt><a href="#assignment-spec">assignment operations</a></dt>
|
<dt><a href="#assignment-spec">assignment operations</a></dt>
|
||||||
|
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
<dl class="page-index">
|
<dl class="page-index">
|
||||||
<dt><a href="#operators-spec">operators</a></dt>
|
<dt><a href="#object_operators-spec">operators</a></dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#len-spec">len()</a></dt>
|
||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
@@ -232,7 +240,7 @@ x[slice(_,_,-1)]
|
|||||||
<p>The policies which are used for proxies representing an attribute
|
<p>The policies which are used for proxies representing an attribute
|
||||||
access to a <code>const object</code>.</p>
|
access to a <code>const object</code>.</p>
|
||||||
|
|
||||||
<h4><a name="class-spec-synopsis"></a>Class
|
<h4><a name="const_attribute_policies-spec-synopsis"></a>Class
|
||||||
<code>const_attribute_policies</code> synopsis</h4>
|
<code>const_attribute_policies</code> synopsis</h4>
|
||||||
<pre>
|
<pre>
|
||||||
namespace boost { namespace python { namespace api
|
namespace boost { namespace python { namespace api
|
||||||
@@ -846,12 +854,12 @@ void del(proxy<T> const& x);
|
|||||||
</dl>
|
</dl>
|
||||||
<pre>
|
<pre>
|
||||||
<a name="comparisons-spec"></a>
|
<a name="comparisons-spec"></a>
|
||||||
template<class L,class R> bool operator>(L const&l,R const&r);
|
template<class L,class R> object operator>(L const&l,R const&r);
|
||||||
template<class L,class R> bool operator>=(L const&l,R const&r);
|
template<class L,class R> object operator>=(L const&l,R const&r);
|
||||||
template<class L,class R> bool operator<(L const&l,R const&r);
|
template<class L,class R> object operator<(L const&l,R const&r);
|
||||||
template<class L,class R> bool operator<=(L const&l,R const&r);
|
template<class L,class R> object operator<=(L const&l,R const&r);
|
||||||
template<class L,class R> bool operator==(L const&l,R const&r);
|
template<class L,class R> object operator==(L const&l,R const&r);
|
||||||
template<class L,class R> bool operator!=(L const&l,R const&r);
|
template<class L,class R> object operator!=(L const&l,R const&r);
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<dl class="function-semantics">
|
<dl class="function-semantics">
|
||||||
@@ -900,7 +908,16 @@ template<class R> object& operator|=(object&l,R const&r);
|
|||||||
<dt><b>Returns:</b> <code>l</code>.</dt>
|
<dt><b>Returns:</b> <code>l</code>.</dt>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
<h2><a name="examples"></a>Example</h2>
|
<pre>
|
||||||
|
inline long len(object const& obj);
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt><b>Effects:</b> PyObject_Length(obj.ptr()) </dt>
|
||||||
|
<dt><b>Returns:</b> len() of object.</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h2><a name="examples"></a>Example</h2>
|
||||||
Python code:
|
Python code:
|
||||||
<pre>
|
<pre>
|
||||||
def sum_items(seq):
|
def sum_items(seq):
|
||||||
@@ -914,20 +931,18 @@ def sum_items(seq):
|
|||||||
object sum_items(object seq)
|
object sum_items(object seq)
|
||||||
{
|
{
|
||||||
object result = object(0);
|
object result = object(0);
|
||||||
for (int i = 0; i < seq.attr("__len__")(); ++i)
|
for (int i = 0; i < len(seq); ++i)
|
||||||
result += seq[i];
|
result += seq[i];
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>Revised
|
<p>Revised
|
||||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
||||||
13 November, 2002
|
13 January, 2006
|
||||||
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p><i>© Copyright <a href=
|
<p><i>© Copyright <a href=
|
||||||
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
|
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2006.</i></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
138
doc/v2/opaque.html
Normal file
138
doc/v2/opaque.html
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- Copyright 2003..2006 Haufe Mediengruppe. 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) -->
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
<link rel="stylesheet" type="text/css" href="../boost.css">
|
||||||
|
|
||||||
|
<title>Boost.Python - <boost/python/opaque_pointer_converter.hpp></title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
||||||
|
"header">
|
||||||
|
<tr>
|
||||||
|
<td valign="top" width="300">
|
||||||
|
<h3><a href="../../../../index.htm"><img height="86" width="277"
|
||||||
|
alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td valign="top">
|
||||||
|
<h1 align="center"><a href="../index.html">Boost.Python</a></h1>
|
||||||
|
|
||||||
|
<h2 align="center">Header
|
||||||
|
<boost/python/opaque_pointer_converter.hpp></h2>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h2>Contents</h2>
|
||||||
|
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#classes">Classes</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#opaque-spec">Class template
|
||||||
|
<code>opaque<Pointee></code></a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#opaque-spec-synopsis">Class template
|
||||||
|
<code>opaque</code> synopsis</a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
|
||||||
|
<dt><a href="#macros">Macros</a></dt>
|
||||||
|
<dd>
|
||||||
|
<dl class="page-index">
|
||||||
|
<dt><a href="#BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID-spec">Macro
|
||||||
|
<code>BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID</code></a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
|
||||||
|
<dt><a href="#see-also">See Also</a></dt>
|
||||||
|
</dl>
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<h2><a name="classes"></a>Classes</h2>
|
||||||
|
|
||||||
|
<h3><a name="opaque-spec"></a>Class template
|
||||||
|
<code>opaque<P></code></h3>
|
||||||
|
|
||||||
|
<p><code>opaque<></code> registers itself as a converter from
|
||||||
|
Python objects to pointers to undefined types and vice versa.</p>
|
||||||
|
|
||||||
|
<h4><a name="opaque-spec-synopsis"></a>Class template
|
||||||
|
<code>opaque</code> synopsis</h4>
|
||||||
|
<pre>
|
||||||
|
namespace boost { namespace python
|
||||||
|
{
|
||||||
|
template<class Pointee>
|
||||||
|
struct opaque
|
||||||
|
{
|
||||||
|
opaque();
|
||||||
|
};
|
||||||
|
}}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<h4><a name="opaque-spec-constructor"></a>Class template
|
||||||
|
<code>opaque</code> constructor</h4>
|
||||||
|
<pre>
|
||||||
|
opaque();
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<dl class="function-semantics">
|
||||||
|
<dt><b>Effects:</b>
|
||||||
|
<ul>
|
||||||
|
<li>Registers the instance as a
|
||||||
|
<a href="lvalue_from_pytype.html#lvalue_from_pytype-spec"> <code>lvalue_from_pytype</code></a>
|
||||||
|
converter from Python objects into opaque pointers.</p>
|
||||||
|
<p>The Python Objects created are named after the type pointed to
|
||||||
|
by the opaque pointer being wrapped.</p></li>
|
||||||
|
<li>Registers the instance as a
|
||||||
|
<a href="to_python_converter.html#to_python_converter-spec"> <code>to_python_converter</code></a>
|
||||||
|
from opaque pointers to Python objects.</p></li>
|
||||||
|
</ul>
|
||||||
|
<p>If there is already an instance registered by another module, this
|
||||||
|
instance doesn't try to register again in order to avoid warnings
|
||||||
|
about multiple registrations.</p>
|
||||||
|
|
||||||
|
<h4>Note</h4>
|
||||||
|
<p>Normally only a single instance of this class is created for every
|
||||||
|
Pointee.</p>
|
||||||
|
</dt>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
<h2><a name="macros"></a>Macros</h2>
|
||||||
|
|
||||||
|
<h3><a name="BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID-spec"></a>
|
||||||
|
Macro BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(Pointee)</h3>
|
||||||
|
<p>This macro must be used to define specializations of the
|
||||||
|
<a href="type_id.html#type_id-spec">type_id</a> function
|
||||||
|
which can't be instantiated for incomplete types.</p>
|
||||||
|
<h4>Note</h4>
|
||||||
|
<p>The macro must be invoked in every translation unit which uses the
|
||||||
|
opaque converter.</p>
|
||||||
|
|
||||||
|
<h2><a name="see-also"></a>See Also</h2>
|
||||||
|
<p>
|
||||||
|
<a href="return_opaque_pointer.html">return_opaque_pointer</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>Revised
|
||||||
|
10 September, 2006
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p><i>© Copyright 2003..2006 Haufe Mediengruppe. All Rights
|
||||||
|
Reserved.</i></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
@@ -1,142 +0,0 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
||||||
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
||||||
<link rel="stylesheet" type="text/css" href="../boost.css">
|
|
||||||
|
|
||||||
<title>Boost.Python - <boost/python/opaque_pointer_converter.hpp></title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
|
|
||||||
"header">
|
|
||||||
<tr>
|
|
||||||
<td valign="top" width="300">
|
|
||||||
<h3><a href="../../../../index.htm"><img height="86" width="277"
|
|
||||||
alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3>
|
|
||||||
</td>
|
|
||||||
|
|
||||||
<td valign="top">
|
|
||||||
<h1 align="center"><a href="../index.html">Boost.Python</a></h1>
|
|
||||||
|
|
||||||
<h2 align="center">Header
|
|
||||||
<boost/python/opaque_pointer_converter.hpp></h2>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
<h2>Contents</h2>
|
|
||||||
|
|
||||||
<dl class="page-index">
|
|
||||||
<dt><a href="#classes">Classes</a></dt>
|
|
||||||
|
|
||||||
<dd>
|
|
||||||
<dl class="page-index">
|
|
||||||
<dt><a href="#opaque_pointer_converter-spec">Class template
|
|
||||||
<code>opaque_pointer_converter<P></code></a></dt>
|
|
||||||
|
|
||||||
<dd>
|
|
||||||
<dl class="page-index">
|
|
||||||
<dt><a href="#opaque_pointer_converter-spec-synopsis">Class template
|
|
||||||
<code>opaque_pointer_converter</code> synopsis</a></dt>
|
|
||||||
</dl>
|
|
||||||
</dd>
|
|
||||||
</dl>
|
|
||||||
</dd>
|
|
||||||
|
|
||||||
<dt><a href="#macros">Macros</a></dt>
|
|
||||||
<dd>
|
|
||||||
<dl class="page-index">
|
|
||||||
<dt><a href="#BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID-spec">Macro
|
|
||||||
<code>BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID</code></a></dt>
|
|
||||||
</dl>
|
|
||||||
</dd>
|
|
||||||
|
|
||||||
<dt><a href="#examples">Example</a></dt>
|
|
||||||
|
|
||||||
<dt><a href="#see-also">See Also</a></dt>
|
|
||||||
</dl>
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
<h2><a name="classes"></a>Classes</h2>
|
|
||||||
|
|
||||||
<h3><a name="opaque_pointer_converter-spec"></a>Class template
|
|
||||||
<code>opaque_pointer_converter<P></code></h3>
|
|
||||||
|
|
||||||
<p><code>opaque_pointer_converter<></code> is derived from
|
|
||||||
<a href="to_python_converter.html#to_python_converter-spec">
|
|
||||||
<code>to_python_converter</code></a>
|
|
||||||
and registers itself as an
|
|
||||||
<a href="lvalue_from_pytype.html#lvalue_from_pytype-spec">
|
|
||||||
<code>lvalue_from_pytype</code></a> converter from Python objects
|
|
||||||
into pointers to undefined types.
|
|
||||||
Thus it may be used as a converter from opaque pointers into
|
|
||||||
Python objects and vice versa.</p>
|
|
||||||
|
|
||||||
<h4><a name="opaque_pointer_converter-spec-synopsis"></a>Class template
|
|
||||||
<code>opaque_pointer_converter</code> synopsis</h4>
|
|
||||||
<pre>
|
|
||||||
namespace boost { namespace python
|
|
||||||
{
|
|
||||||
template<class Pointer>
|
|
||||||
struct opaque_pointer_converter
|
|
||||||
: to_python_converter<
|
|
||||||
Pointer, opaque_pointer_converter<Pointer> >
|
|
||||||
{
|
|
||||||
explicit opaque_pointer_converter(char const* name);
|
|
||||||
};
|
|
||||||
}}
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<h4><a name="opaque_pointer_converter-spec-constructor"></a>Class template
|
|
||||||
<code>opaque_pointer_converter</code> constructor</h4>
|
|
||||||
<pre>
|
|
||||||
explicit opaque_pointer_converter(char const* name);
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<dl class="function-semantics">
|
|
||||||
<dt><b>Effects:</b>
|
|
||||||
<p>Registers the instance as a
|
|
||||||
<a href="lvalue_from_pytype.html#lvalue_from_pytype-spec">
|
|
||||||
<code>lvalue_from_pytype</code></a> converter from Python objects
|
|
||||||
into opaque pointers.</p>
|
|
||||||
<p>The name is used for the type of the Python Objects created;
|
|
||||||
it should be printable but needn't be an
|
|
||||||
<a href="definitions.html#ntbs">ntbs</a> because the object type is
|
|
||||||
not supposed to be user constructible within python scripts.</p>
|
|
||||||
</dt>
|
|
||||||
</dl>
|
|
||||||
|
|
||||||
<h2><a name="macros"></a>Macros</h2>
|
|
||||||
|
|
||||||
<h3><a name="BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID-spec"></a>
|
|
||||||
Macro BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(Pointee)</h3>
|
|
||||||
<p>This macro must be used to define specializations of the
|
|
||||||
<a href="type_id.html#type_id-spec">type_id</a> function
|
|
||||||
which can't be instantiated for incomplete types.</p>
|
|
||||||
<h4>Note</h4>
|
|
||||||
<p>In order for this to work in a cross-module environment the macro must
|
|
||||||
be invoked in every translation unit which uses the
|
|
||||||
opaque_pointer_converter.</p>
|
|
||||||
|
|
||||||
<h2><a name="examples"></a>Example</h2>
|
|
||||||
|
|
||||||
please see example for <a href="return_opaque_pointer.html#example">
|
|
||||||
return_opaque_pointer</a>.
|
|
||||||
|
|
||||||
<h2><a name="see-also"></a>See Also</h2>
|
|
||||||
<p>
|
|
||||||
<a href="return_opaque_pointer.html">return_opaque_pointer</a>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p>Revised
|
|
||||||
10 March, 2003
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p><i>© Copyright 2003 Haufe Mediengruppe. All Rights
|
|
||||||
Reserved.</i></p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -57,7 +60,7 @@
|
|||||||
<dt><a href="#self_t-spec-value-unary-ops">Class
|
<dt><a href="#self_t-spec-value-unary-ops">Class
|
||||||
<code>self_t</code> unary operations</a></dt>
|
<code>self_t</code> unary operations</a></dt>
|
||||||
|
|
||||||
<dt><a href="#self_t-spec-value-value-ops">Class
|
<dt><a href="#self_t-spec-value-ops">Class
|
||||||
<code>self_t</code> value operations</a></dt>
|
<code>self_t</code> value operations</a></dt>
|
||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
@@ -211,6 +214,9 @@ namespace boost { namespace python { namespace self_ns {
|
|||||||
<a href=
|
<a href=
|
||||||
"#operator_-spec">operator_</a><<i>unspecified</i>> str(self_t);
|
"#operator_-spec">operator_</a><<i>unspecified</i>> str(self_t);
|
||||||
|
|
||||||
|
<a href=
|
||||||
|
"#operator_-spec">operator_</a><<i>unspecified</i>> repr(self_t);
|
||||||
|
|
||||||
}}};
|
}}};
|
||||||
</pre>
|
</pre>
|
||||||
The tables below describe the methods generated when the results of the
|
The tables below describe the methods generated when the results of the
|
||||||
@@ -762,6 +768,15 @@ namespace boost { namespace python { namespace self_ns {
|
|||||||
<td><code><a href=
|
<td><code><a href=
|
||||||
"../../../conversion/lexical_cast.htm#lexical_cast">lexical_cast</a><std::string>(x)</code></td>
|
"../../../conversion/lexical_cast.htm#lexical_cast">lexical_cast</a><std::string>(x)</code></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td><code>repr</code></td>
|
||||||
|
|
||||||
|
<td><code>__repr__</code></td>
|
||||||
|
|
||||||
|
<td><code><a href=
|
||||||
|
"../../../conversion/lexical_cast.htm#lexical_cast">lexical_cast</a><std::string>(x)</code></td>
|
||||||
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<h3><a name="other-spec"></a>Class Template <code>other</code></h3>
|
<h3><a name="other-spec"></a>Class Template <code>other</code></h3>
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -92,8 +92,8 @@ following examples.
|
|||||||
<hr>
|
<hr>
|
||||||
<h2>Examples</h2>
|
<h2>Examples</h2>
|
||||||
|
|
||||||
There are three files in <a href="../../test/"
|
There are three files in
|
||||||
><tt>boost/libs/python/test</tt></a> that show how to
|
<tt>boost/libs/python/test</tt> that show how to
|
||||||
provide pickle support.
|
provide pickle support.
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
@@ -320,11 +320,9 @@ See also the
|
|||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
© Copyright Ralf W. Grosse-Kunstleve 2001-2004. Permission to copy,
|
© Copyright Ralf W. Grosse-Kunstleve 2001-2004. Distributed under
|
||||||
use, modify, sell and distribute this document is granted provided this
|
the Boost Software License, Version 1.0. (See accompanying file
|
||||||
copyright notice appears in all copies. This document is provided "as
|
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
is" without express or implied warranty, and with no claim as to its
|
|
||||||
suitability for any purpose.
|
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Updated: Feb 2004.
|
Updated: Feb 2004.
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -51,13 +54,11 @@
|
|||||||
3.1, and 3.2 on <a href="http://www.redhat.com">RedHat Linux 7.3</a>
|
3.1, and 3.2 on <a href="http://www.redhat.com">RedHat Linux 7.3</a>
|
||||||
for Intel x86</dt>
|
for Intel x86</dt>
|
||||||
|
|
||||||
<dt><a href=
|
<dt>Tru64 CXX 6.5.1 on OSF v. 5.1 for Dec/Compaq
|
||||||
"http://www.tru64unix.compaq.com/cplus/index.html">Tru64 CXX
|
Alpha</dt>
|
||||||
6.5.1</a> on OSF v. 5.1 for Dec/Compaq Alpha</dt>
|
|
||||||
|
|
||||||
<dt><a href=
|
<dt>
|
||||||
"http://www.sgi.com/developers/devtools/languages/mipspro.html">
|
MIPSPro 7.3.1.2m on <a href=
|
||||||
MIPSPro 7.3.1.2m</a> on <a href=
|
|
||||||
"http://www.sgi.com/software/irix6.5/">IRIX 6.5</a> for SGI
|
"http://www.sgi.com/software/irix6.5/">IRIX 6.5</a> for SGI
|
||||||
mips</dt>
|
mips</dt>
|
||||||
|
|
||||||
@@ -70,21 +71,16 @@
|
|||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<dl>
|
<dl>
|
||||||
<dt><a href=
|
<dt>KCC 3.4d on OSF v. 5.1 for Dec/Compaq Alpha</dt>
|
||||||
"http://developer.intel.com/software/products/kcc/">KCC
|
|
||||||
3.4d</a> on OSF v. 5.1 for Dec/Compaq Alpha</dt>
|
|
||||||
|
|
||||||
<dt><a href=
|
<dt>KCC 3.4d</a> on AIX</dt>
|
||||||
"http://developer.intel.com/software/products/kcc/">KCC
|
|
||||||
3.4d</a> on AIX</dt>
|
|
||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
<br>
|
<br>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><a href="http://www.microsoft.com/windowsxp/default.asp">Microsoft
|
<dt>Microsoft Windows XP Professional with Python <a href=
|
||||||
Windows XP Professional</a> with Python <a href=
|
|
||||||
"http://www.python.org/2.2">2.2</a>, <a href=
|
"http://www.python.org/2.2">2.2</a>, <a href=
|
||||||
"http://www.python.org/2.2.1">2.2.1</a>, and <a href=
|
"http://www.python.org/2.2.1">2.2.1</a>, and <a href=
|
||||||
"http://www.python.org/2.2.2">2.2.2b1</a>:</dt>
|
"http://www.python.org/2.2.2">2.2.2b1</a>:</dt>
|
||||||
|
|||||||
@@ -45,8 +45,8 @@
|
|||||||
|
|
||||||
<p><code><boost/python/pointee.hpp></code> introduces a
|
<p><code><boost/python/pointee.hpp></code> introduces a
|
||||||
traits <a
|
traits <a
|
||||||
href="../../../mpl/doc/index.html#metafunctions">metafunction</a>
|
href="../../../mpl/doc/refmanual/metafunction.html">metafunction</a>
|
||||||
template <code>pointee<T></code> which can be used to extract the "pointed-to" type from the type of a pointer or smart pointer.
|
template <code>pointee<T></code> that can be used to extract the "pointed-to" type from the type of a pointer or smart pointer.
|
||||||
|
|
||||||
<h2><a name="classes"></a>Classes</h2>
|
<h2><a name="classes"></a>Classes</h2>
|
||||||
|
|
||||||
@@ -112,5 +112,8 @@ BOOST_PYTHON_MODULE(pointee_demo)
|
|||||||
|
|
||||||
|
|
||||||
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
||||||
Abrahams</a> 2002. </i>
|
Abrahams</a> 2002. </i> 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)</p>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -259,5 +259,7 @@ void pass_as_arg(expensive_to_copy* x, PyObject* f)
|
|||||||
|
|
||||||
|
|
||||||
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
|
||||||
Abrahams</a> 2002. </i>
|
Abrahams</a> 2002. </i> 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)</p>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -60,6 +63,8 @@
|
|||||||
|
|
||||||
<dt><a href="#type_conversion">To/From Python Type Conversion</a></dt>
|
<dt><a href="#type_conversion">To/From Python Type Conversion</a></dt>
|
||||||
|
|
||||||
|
<dt><a href="#embedding">Embedding</a></dt>
|
||||||
|
|
||||||
<dt><a href="#utility">Utility and Infrastructure</a></dt>
|
<dt><a href="#utility">Utility and Infrastructure</a></dt>
|
||||||
|
|
||||||
<dt><a href="#topics">Topics</a></dt>
|
<dt><a href="#topics">Topics</a></dt>
|
||||||
@@ -76,7 +81,7 @@
|
|||||||
<dt><a href=
|
<dt><a href=
|
||||||
"Dereferenceable.html#Dereferenceable-concept">Dereferenceable</a></dt>
|
"Dereferenceable.html#Dereferenceable-concept">Dereferenceable</a></dt>
|
||||||
|
|
||||||
<dt><a href="Dereferenceable.html#Extractor-concept">Extractor</a></dt>
|
<dt><a href="Extractor.html#Extractor-concept">Extractor</a></dt>
|
||||||
|
|
||||||
<dt><a href=
|
<dt><a href=
|
||||||
"HolderGenerator.html#HolderGenerator-concept">HolderGenerator</a></dt>
|
"HolderGenerator.html#HolderGenerator-concept">HolderGenerator</a></dt>
|
||||||
@@ -135,6 +140,14 @@
|
|||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
|
<dt><a href="docstring_options.html">docstring_options.hpp</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="index">
|
||||||
|
<dt><a href="docstring_options.html#classes">Classes</a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
|
||||||
<dt><a href="enum.html">enum.hpp</a></dt>
|
<dt><a href="enum.html">enum.hpp</a></dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
@@ -291,6 +304,20 @@
|
|||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
|
<dt><a href="stl_iterator.html">stl_iterator.hpp</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="index">
|
||||||
|
<dt><a href="stl_iterator.html#classes">Classes</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="index">
|
||||||
|
<dt><a href="stl_iterator.html#stl_input_iterator-spec">stl_input_iterator</a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
|
||||||
<dt><a href="wrapper.html">wrapper.hpp</a></dt>
|
<dt><a href="wrapper.html">wrapper.hpp</a></dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
@@ -880,26 +907,26 @@
|
|||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><a href=
|
<dt><a href=
|
||||||
"opaque_pointer_converter.html">opaque_pointer_converter.hpp</a></dt>
|
"opaque.html">opaque_pointer_converter.hpp</a></dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<dl class="index">
|
<dl class="index">
|
||||||
<dt><a href=
|
<dt><a href=
|
||||||
"opaque_pointer_converter.html#classes">Classes</a></dt>
|
"opaque.html#classes">Classes</a></dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<dl class="index">
|
<dl class="index">
|
||||||
<dt><a href=
|
<dt><a href=
|
||||||
"opaque_pointer_converter.html#opaque_pointer_converter-spec">opaque_pointer_converter</a></dt>
|
"opaque.html#opaque-spec">opaque</a></dt>
|
||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
<dt><a href="opaque_pointer_converter.html#macros">Macros</a></dt>
|
<dt><a href="opaque.html#macros">Macros</a></dt>
|
||||||
|
|
||||||
<dd>
|
<dd>
|
||||||
<dl class="index">
|
<dl class="index">
|
||||||
<dt><a href=
|
<dt><a href=
|
||||||
"opaque_pointer_converter.html#BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID-spec">
|
"opaque.html#BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID-spec">
|
||||||
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID</a></dt>
|
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID</a></dt>
|
||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
@@ -939,6 +966,40 @@
|
|||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
|
<h2><a name="embedding">Embedding</a></h2>
|
||||||
|
|
||||||
|
<dl class="index">
|
||||||
|
<dt><a href="exec.html">exec.hpp</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="index">
|
||||||
|
<dt><a href="exec.html#functions">Functions</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="index">
|
||||||
|
<dt><a href="exec.html#eval-spec">eval</a></dt>
|
||||||
|
<dt><a href="exec.html#exec-spec">exec</a></dt>
|
||||||
|
<dt><a href="exec.html#exec_file-spec">exec_file</a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
|
||||||
|
<dt><a href="import.html">import.hpp</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="index">
|
||||||
|
<dt><a href="import.html#functions">Functions</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="index">
|
||||||
|
<dt><a href="import.html#import-spec">import</a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
|
||||||
<h2><a name="utility">Utility and Infrastructure</a></h2>
|
<h2><a name="utility">Utility and Infrastructure</a></h2>
|
||||||
|
|
||||||
<dl>
|
<dl>
|
||||||
@@ -981,7 +1042,7 @@
|
|||||||
<dd>
|
<dd>
|
||||||
<dl class="index">
|
<dl class="index">
|
||||||
<dt>class template <a href=
|
<dt>class template <a href=
|
||||||
"pointee.html#pointee">pointee</a></dt>
|
"pointee.html#pointee-spec">pointee</a></dt>
|
||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
@@ -1034,6 +1095,16 @@
|
|||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
|
<dt><a href="ssize_t.html">ssize_t.hpp</a></dt>
|
||||||
|
|
||||||
|
<dd>
|
||||||
|
<dl class="index">
|
||||||
|
<dt><a href="ssize_t.html#typedefs">Typedefs</a></dt>
|
||||||
|
|
||||||
|
<dt><a href="ssize_t.html#constants">Constants</a></dt>
|
||||||
|
</dl>
|
||||||
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
||||||
<h2><a name="topics">Topics</a></h2>
|
<h2><a name="topics">Topics</a></h2>
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -102,9 +105,9 @@ template <class T> struct apply
|
|||||||
|
|
||||||
<dt><b>Returns:</b> <code>typedef <a href=
|
<dt><b>Returns:</b> <code>typedef <a href=
|
||||||
"to_python_indirect.html#to_python_indirect-spec">to_python_indirect</a><T,V>
|
"to_python_indirect.html#to_python_indirect-spec">to_python_indirect</a><T,V>
|
||||||
type</code>, where <code>V</code> is a <a href=
|
type</code>, where <code>V</code> is a class whose
|
||||||
"to_python_indirect.html#HolderObjectGenerator">HolderObjectGenerator</a>
|
static <code>execute</code> function constructs an instance
|
||||||
which constructs an instance holder containing an <i>unowned</i>
|
holder containing an <i>unowned</i>
|
||||||
<code>U*</code> pointing to the referent of the wrapped function's
|
<code>U*</code> pointing to the referent of the wrapped function's
|
||||||
return value.</dt>
|
return value.</dt>
|
||||||
</dl>
|
</dl>
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
@@ -137,8 +140,8 @@ namespace boost { namespace python
|
|||||||
}}
|
}}
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h4><a name="default_call_policies-spec-statics"></a>Class
|
<h4><a name="return_internal_reference-spec-statics"></a>Class
|
||||||
<code>default_call_policies</code> static functions</h4>
|
<code>return_internal_reference</code> static functions</h4>
|
||||||
<pre>
|
<pre>
|
||||||
PyObject* postcall(PyObject* args, PyObject* result);
|
PyObject* postcall(PyObject* args, PyObject* result);
|
||||||
</pre>
|
</pre>
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||||
@@ -67,7 +70,7 @@
|
|||||||
undefined types such that the return value is copied into a
|
undefined types such that the return value is copied into a
|
||||||
new Python object.</p>
|
new Python object.</p>
|
||||||
<p>In addition to specifying the <code>return_opaque_pointer</code>
|
<p>In addition to specifying the <code>return_opaque_pointer</code>
|
||||||
policy the <a href="opaque_pointer_converter.html#BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID-spec">
|
policy the <a href="opaque.html#BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID-spec">
|
||||||
<code>BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID</code></a> macro must be
|
<code>BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID</code></a> macro must be
|
||||||
used to define specializations for the
|
used to define specializations for the
|
||||||
<a href="type_id.html#type_id-spec">type_id</a> function
|
<a href="type_id.html#type_id-spec">type_id</a> function
|
||||||
@@ -174,8 +177,8 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
<h2><a name="see-also"></a>See Also</h2>
|
<h2><a name="see-also"></a>See Also</h2>
|
||||||
<p>
|
<p>
|
||||||
<a href="opaque_pointer_converter.html">
|
<a href="opaque.html">
|
||||||
opaque_pointer_converter</a>
|
opaque</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>Revised
|
<p>Revised
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||||
|
|
||||||
|
<!-- 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) -->
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta name="generator" content=
|
<meta name="generator" content=
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user