2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-22 17:32:55 +00:00

Compare commits

...

353 Commits

Author SHA1 Message Date
Stefan Seefeld
99022d2eb8 Merge branch 'develop' 2015-06-30 18:09:40 -04:00
Stefan Seefeld
37f8f37cda Merge pull request #16 from teeks99/main_crt_error
Fixes empty example
2015-06-24 18:01:24 -04:00
Stefan Seefeld
156054371b Merge pull request #24 from agnat/fix/cv_tag_volatile_constant
Fix is_volatile constant in detail::cv_tag<>
2015-05-27 11:42:39 -04:00
David Siegel
d06f4aeaca Fix is_volatile constant in detail::cv_tag<> 2015-05-27 17:30:09 +02:00
Stefan Seefeld
e8d7bb5027 Merge pull request #23 from jzmaddock/patch-1
Stop Using type_traits details.
2015-05-27 11:07:30 -04:00
Stefan Seefeld
b207fc1a04 Merge pull request #20 from Lastique/patch-2
Use _Py_fopen instead of fopen
2015-05-27 10:59:50 -04:00
jzmaddock
9742c30f47 Stop Using type_traits details.
The type_traits internal details used here are being moved to make it clear they are details and subject to change.  Old code will continue to work, but will warn about use of a deprecated header.
2015-05-21 19:20:31 +01:00
Stefan Seefeld
97f894bc1a Merge pull request #7 from PeterMartini/develop
Boost.Build now uses src/ instead of v2/
2015-05-08 13:18:33 -04:00
Andrey Semashev
eefc434bae Use _Py_fopen instead of fopen
A followup fix for the previous commit 36f8f69411.
2015-05-06 15:29:42 +03:00
Stefan Seefeld
226d1ac961 Merge pull request #19 from Lastique/patch-1
Fix compilation with python 3.0-3.3
2015-05-06 08:10:30 -04:00
Andrey Semashev
36f8f69411 Fix compilation with python 3.0-3.3
Backport commit 3e405b6fd5 to develop branch: Fix exec_file for Python 3 < 3.4. Also fix version check to actually fix compilation with python 3.4.
2015-05-06 15:06:29 +03:00
Tom Kent
97b0d9737c Fixes empty example
Uses of visual studio were getting a 'unresolved external symbol _mainCRTStartup' because there was no main() being linked into the test_embed.exe. I'm not a bjam expert, but I think that is because the embedding target is an executable (instead of an obj/lib?). I also have no idea if this was a problem on unix or if this fix will affect that platform.
2015-04-10 19:47:44 -05:00
Stefan Seefeld
3e405b6fd5 Fix exec_file for Python 3 < 3.4. 2015-04-09 08:57:08 -04:00
Stefan Seefeld
e1e9eb303a Merge pull request #13 from ax3l/fix-missingPlaceholderInclude
Fix Trac #10932 Missing Include: Bind Placeholders
2015-03-26 09:16:15 -04:00
Stefan Seefeld
9eee9ef461 Merge pull request #15 from mmatrosov/master
Fix #11100 and #8058: binary compatibility and leaked file handle in exec_file()
2015-03-26 08:23:15 -04:00
Mikhail Matrosov
fe24ab9dd5 [#8058] Close file handle before exec_file() routine returns. 2015-03-11 01:11:54 +03:00
Mikhail Matrosov
a911c17fd6 [#11100] Fix binary incompatibilities with fopen() in exec_file() routine. 2015-03-11 01:11:54 +03:00
Daniel James
8d5d777ebb Add metadata file. 2015-01-25 21:25:23 +02:00
Peter Dimov
ea87bfee8c Merge pull request #9 from danieljames/metadata
Create metadata file.
2015-01-25 20:31:16 +02:00
Axel Huebl
17a7655c74 Fix Missing Include: Bind Placeholders
boost/python/exception_translator.hpp(22): error: identifier "_1" is undefined
boost/python/exception_translator.hpp(22): error: identifier "_2" is undefined
2015-01-14 16:02:23 +01:00
Daniel James
0a47c583b1 Add metadata file. 2014-08-18 15:09:41 +01:00
Peter Martini
e445e193fe Boost.Build now uses src/ instead of v2/
Update examples to reflect this
2014-04-21 23:07:11 -04:00
Beman Dawes
832a1edd79 Merge 86392 from trunk. The spirit change was not applied because the file is not present in branches/release.
[SVN r86489]
2013-10-27 21:10:04 +00:00
John Maddock
6b7e0cc71f Remove all references to now defunct (and removed) header.
[SVN r86438]
2013-10-26 10:13:38 +00:00
Beman Dawes
74b9bcaaa5 Add Boost.Build support for the Git modular layout, using links to to new header locations, thus preventing breakage to vast amounts of user code and build setups. Being added to SVN so that changes will automatically propagate to Git and modular Boost. Thanks to Steven Watanabe, Jürgen Hunold, Vladimir Prus, and Bjørn Roald for developing this patch. Steven in particular conceived the approach and did the initial implementation, which was further refined by the other contributors.
[SVN r86392]
2013-10-22 15:23:42 +00:00
Stephen Kelly
371914a0e0 Remove use of obsolete BOOST_TT_BROKEN_COMPILER_SPEC
[SVN r86250]
2013-10-11 23:23:26 +00:00
Stephen Kelly
8021a21abb Remove remaining occurances of BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
These evaded scripting.

[SVN r86249]
2013-10-11 23:22:36 +00:00
Stephen Kelly
9ceb9c02f5 Remove BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Process #ifdef...#else...#endif blocks.

[SVN r86246]
2013-10-11 23:19:17 +00:00
Stephen Kelly
97f9a10b9c Remove BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Process #ifndef...#else...#endif blocks.

[SVN r86245]
2013-10-11 23:17:48 +00:00
Stephen Kelly
87f776b178 Remove obsolete files.
[SVN r86242]
2013-10-11 23:11:35 +00:00
Stephen Kelly
590b7887f4 Python: Fix build.
[SVN r86099]
2013-10-01 08:40:58 +00:00
Stephen Kelly
badedbebfc Python: Remove obsolete GCC version checks.
[SVN r86066]
2013-09-30 15:58:19 +00:00
Stephen Kelly
cc80f46a6d Python: Remove obsolete MSVC version check.
[SVN r86029]
2013-09-30 00:19:23 +00:00
Stephen Kelly
42e7d7bbb3 Python: Clean up some old MSVC related code.
[SVN r85922]
2013-09-25 21:17:17 +00:00
Stephen Kelly
5476f97d59 Python: Simplify object_operators implementation.
The old-MSVC-specific version is no longer needed.

[SVN r85898]
2013-09-25 11:20:43 +00:00
Stephen Kelly
da01e9b660 Remove use of obsolete BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE{,_SPEC} macro.
[SVN r85896]
2013-09-25 10:29:44 +00:00
Stephen Kelly
a89bf7e9e8 Remove use of obsolete BOOST_EXPLICIT_TEMPLATE_TYPE{,_SPEC} macros.
[SVN r85894]
2013-09-25 10:28:56 +00:00
Vladimir Prus
52201f0cba Undo previous commit, it was meant for another branch
[SVN r85888]
2013-09-25 08:32:03 +00:00
Vladimir Prus
3480aee9e7 Initial modular patch, from Bjorn.
[SVN r85884]
2013-09-25 08:13:57 +00:00
Stephen Kelly
ead8d0aba5 Python: Simplify object and BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS macro.
The macro no longer needs an implementation for old MSVC.

[SVN r85881]
2013-09-25 07:55:31 +00:00
Stephen Kelly
7a4b240838 Python: Remove use of obsolete BOOST_PYTHON_EXPLICIT_TT_DEF macro.
[SVN r85880]
2013-09-25 07:55:17 +00:00
Stephen Kelly
f500202dab Python: Trivially define the BOOST_PYTHON_EXPLICIT_TT_DEF macro
As BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS is never defined, this
macro is obsolete.

[SVN r85879]
2013-09-25 07:55:01 +00:00
Ralf W. Grosse-Kunstleve
ca18dc9daa merging current boost/python and libs/python from trunk into release branch
[SVN r82295]
2012-12-31 04:14:35 +00:00
Ralf W. Grosse-Kunstleve
fc3f1bb531 libs/python/doc/projects.html: adding Pyrap
[SVN r82294]
2012-12-31 03:44:48 +00:00
Ralf W. Grosse-Kunstleve
8cc149f4f4 merging current boost/python and libs/python from trunk into release branch
[SVN r81965]
2012-12-15 01:25:41 +00:00
Ralf W. Grosse-Kunstleve
5f8f5b4c3e libs/python/doc/projects.html: update pointed out by Greg Landrum
[SVN r81964]
2012-12-15 01:15:24 +00:00
Ralf W. Grosse-Kunstleve
a3f478e9af boost.python: merging trunk to release
[SVN r81773]
2012-12-07 19:51:06 +00:00
Ralf W. Grosse-Kunstleve
303885fefa applying patch from trac ticket 4609
[SVN r79774]
2012-07-27 22:43:32 +00:00
Ralf W. Grosse-Kunstleve
46796f3413 merging current boost/python and libs/python from trunk into release branch
[SVN r79096]
2012-06-25 22:09:52 +00:00
Ralf W. Grosse-Kunstleve
2c1a276671 python: minor doc bug fix (ticket #7014)
[SVN r79095]
2012-06-25 21:56:38 +00:00
Ralf W. Grosse-Kunstleve
0b8b88abc7 merging current boost/python and libs/python from trunk into release branch
[SVN r79011]
2012-06-19 20:01:25 +00:00
Dave Abrahams
c3d20eb07f Replace all uses of boost/utility.hpp with more-granular includes. Solves modularization dependency nightmare.
[SVN r78502]
2012-05-18 04:44:04 +00:00
Ralf W. Grosse-Kunstleve
e32979fe0a boost/python/object_core.hpp: trac #6890
[SVN r78427]
2012-05-11 20:39:21 +00:00
Ralf W. Grosse-Kunstleve
76db3311ed merging current boost/python and libs/python from trunk into release branch
[SVN r77125]
2012-02-27 02:49:42 +00:00
Ralf W. Grosse-Kunstleve
af8efb72bd libs/python/doc/v2/make_function.html: fixing obvious doc bug pointed out by Johan Rade
[SVN r77124]
2012-02-27 02:20:10 +00:00
Ralf W. Grosse-Kunstleve
5792bffaa2 Python: applying #6409: __visibility__ (with underscores) to avoid name clashes
[SVN r76562]
2012-01-17 19:36:56 +00:00
Ralf W. Grosse-Kunstleve
7acb544b47 merging current boost/python and libs/python from trunk into release branch
[SVN r76422]
2012-01-11 23:48:18 +00:00
Daniel James
e3130fe75b Document 'Creating boost::python::object from PyObject*'.
From Pedro Larroy. Fixes #6393.

[SVN r76421]
2012-01-11 23:07:01 +00:00
Daniel James
f054b64315 Merge python doc rebuild to release.
[SVN r76359]
2012-01-08 10:07:09 +00:00
Daniel James
ef6194f1f8 Revert accidental python commit.
I forgot to revert this when merging from quickbook-dev.


[SVN r76357]
2012-01-08 09:22:28 +00:00
Daniel James
b262615aa8 Quickbook: Merge escape change from quickbook-dev branch.
Only changes 'raw_escape' which is only used in 1.6


[SVN r76260]
2012-01-01 14:51:32 +00:00
Daniel James
842cf85d4b Rebuild python tutorial with recent versions of tools.
This will make further changes less noisy and also happens to fix the misparse
of `__main__`.


[SVN r76196]
2011-12-26 21:56:24 +00:00
Ralf W. Grosse-Kunstleve
9064a063b2 merging current boost/python and libs/python from trunk into release branch
[SVN r75919]
2011-12-12 17:46:33 +00:00
Ralf W. Grosse-Kunstleve
5daa36d04d boost/python/slice.hpp: bug fix (track #6256)
[SVN r75918]
2011-12-12 17:28:13 +00:00
Ralf W. Grosse-Kunstleve
fa24dbb88f merging current boost/python and libs/python from trunk into release branch
[SVN r75414]
2011-11-08 21:27:05 +00:00
Ralf W. Grosse-Kunstleve
ba5503cf89 libs/python/pyste/src/Pyste/GCCXMLParser.py: applying python2.5-elementtree.patch from Trac Ticket #2208
[SVN r75413]
2011-11-08 21:16:29 +00:00
Ralf W. Grosse-Kunstleve
49e8699b9f merging current boost/python and libs/python from trunk into release branch
[SVN r74931]
2011-10-12 21:38:53 +00:00
Ralf W. Grosse-Kunstleve
8449c34948 libs/python/src/object/function.cpp: apply patch by Matthew Bradbury (trac #4259)
[SVN r74321]
2011-09-08 21:27:10 +00:00
Ralf W. Grosse-Kunstleve
838f44c050 boost/python: fixing long-standing mis-spelling, resolves trac #5805
[SVN r74021]
2011-08-23 16:42:15 +00:00
Ralf W. Grosse-Kunstleve
6699e8a4a2 boost/python/class.hpp: commenting out assertion, resolves trac #5803
[SVN r74020]
2011-08-23 16:24:48 +00:00
Ralf W. Grosse-Kunstleve
302fee14b0 libs/python/doc/v2/with_custodian_and_ward.html: correcting minor typo
[SVN r72748]
2011-06-25 01:27:17 +00:00
Ralf W. Grosse-Kunstleve
211c90ae0f boost/python.hpp: adding two more missing includes
[SVN r72746]
2011-06-24 22:16:06 +00:00
Ralf W. Grosse-Kunstleve
f1efb481c3 boost/python.hpp: include slice.hpp (trac issue 5639)
[SVN r72745]
2011-06-24 21:27:40 +00:00
Ralf W. Grosse-Kunstleve
3e409f9a87 boost/python/slice.hpp: correct long-standing spelling error; affects interface; keeping old interface for backward compatibility
[SVN r72602]
2011-06-14 23:23:33 +00:00
Ralf W. Grosse-Kunstleve
cfa2baf8c9 merging current boost/python and libs/python from trunk into release branch
[SVN r72464]
2011-06-07 04:15:33 +00:00
Ralf W. Grosse-Kunstleve
1212a14739 boost/python/override.hpp: trac issue #4101
[SVN r72384]
2011-06-04 09:50:27 +00:00
Ralf W. Grosse-Kunstleve
8e703e9569 libs/python/src: patches by Bogdan Opanchuk (trac #5590)
[SVN r72349]
2011-06-02 17:04:43 +00:00
Ralf W. Grosse-Kunstleve
faf3cd0b78 merging current boost/python and libs/python from trunk into release branch
[SVN r72277]
2011-05-30 05:51:49 +00:00
Ralf W. Grosse-Kunstleve
4df7f1c247 libs/python/doc/tutorial/doc/tutorial.qbk: fixing small oversight (issue #5574)
[SVN r72220]
2011-05-27 17:11:44 +00:00
Ralf W. Grosse-Kunstleve
201c100422 libs/python/test/Jamfile.v2: correction, by Steven Watanabe
[SVN r72175]
2011-05-26 04:09:40 +00:00
Ralf W. Grosse-Kunstleve
f441ec7dfd libs/python/test/Jamfile.v2: implementing suggestions by Steven Watanabe (thanks!)
[SVN r72174]
2011-05-26 01:50:32 +00:00
Ralf W. Grosse-Kunstleve
09dc86f5f2 libs/python/test/Jamfile.v2: patch provided by Niklas Angare (Ticket #5565)
[SVN r72173]
2011-05-26 00:21:33 +00:00
Ralf W. Grosse-Kunstleve
a5e564be83 merging current boost/python and libs/python from trunk into release branch
[SVN r71226]
2011-04-13 17:34:35 +00:00
Dave Abrahams
dcc3590907 verifying commit privs
[SVN r71150]
2011-04-09 16:55:43 +00:00
Ralf W. Grosse-Kunstleve
a11a3f9f76 libs/python/src/converter/builtin_converters.cpp: Python 3.2 compatibility, based on patch by Matthew Bradbury, fixes issue #4994
[SVN r71050]
2011-04-06 21:32:59 +00:00
Ralf W. Grosse-Kunstleve
74daea3d88 boost/libs/python/doc/PyConDC_2003: removing $Date$ and $Revision$ so they do not create noise on each merge to the release branch
[SVN r71004]
2011-04-05 16:49:31 +00:00
Ralf W. Grosse-Kunstleve
4c2927ca46 boost/libs/python/doc/PyConDC_2003: removing $Date$ and $Revision$ so they do not create noise on each merge to the release branch
[SVN r71003]
2011-04-05 16:39:36 +00:00
Ralf W. Grosse-Kunstleve
e28f0509d8 merging current boost/python and libs/python from trunk into release branch
[SVN r71002]
2011-04-05 16:31:51 +00:00
Ralf W. Grosse-Kunstleve
a8fb9f7746 libs/python/src/module.cpp: bug fix: adding missing BOOST_PYTHON_DECL that should have been in rev. 67462, fixes trac issue #5283
[SVN r70493]
2011-03-24 04:10:59 +00:00
Ralf W. Grosse-Kunstleve
0a211a746d merging current boost/python and libs/python from trunk into release branch
[SVN r70448]
2011-03-23 00:14:55 +00:00
Ralf W. Grosse-Kunstleve
2a20884e78 libs/python/src/object/class.cpp: metaclass fixes by James Emerton: james at emdata dot net
[SVN r69551]
2011-03-04 17:32:41 +00:00
Ralf W. Grosse-Kunstleve
ba213663b6 merging current boost/python and libs/python from trunk into release branch
[SVN r67483]
2010-12-28 20:38:29 +00:00
Ralf W. Grosse-Kunstleve
b5336f36d6 Boost.Python: Python 3 module initialization fixes (using PyModuleDef), based on patches provided by Austin Bingham
[SVN r67462]
2010-12-26 22:42:32 +00:00
Ralf W. Grosse-Kunstleve
736ba48c2b merging current boost/python and libs/python from trunk into release branch
[SVN r67446]
2010-12-24 17:31:05 +00:00
Ralf W. Grosse-Kunstleve
fa32b8dfab boost/python/converter/builtin_converters.hpp: fix for Python 3 (by Austin Bingham)
[SVN r67432]
2010-12-23 16:36:53 +00:00
Eric Niebler
6066ffef9a Merged revisions 66606 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r66606 | rwgk | 2010-11-15 18:17:00 -0500 (Mon, 15 Nov 2010) | 1 line
  
  boost/python/object/make_instance.hpp: adding missing includes
........


[SVN r66614]
2010-11-16 18:30:39 +00:00
Ralf W. Grosse-Kunstleve
c501874bc2 boost/python/object/make_instance.hpp: adding missing includes
[SVN r66606]
2010-11-15 23:17:00 +00:00
Ralf W. Grosse-Kunstleve
f759e9eb0e merging current boost/python and libs/python from trunk into release branch
[SVN r66066]
2010-10-18 04:04:52 +00:00
Vladimir Prus
cae31b5380 Make extension copied to current directory, for convenient testing
[SVN r65638]
2010-09-27 16:27:16 +00:00
Ralf W. Grosse-Kunstleve
76af2cfc6b libs/python/src/object/function.cpp: support __module__ attribute (to help certain doc generation systems)
[SVN r65555]
2010-09-23 19:22:12 +00:00
Douglas Gregor
846c5d9914 Add missing includes
[SVN r65142]
2010-08-31 04:50:56 +00:00
Ralf W. Grosse-Kunstleve
bd8a9eb1fd Boost.Python: Python 3 compatibility patch by Trigve Siver (trac ticket 4497)
[SVN r65055]
2010-08-27 17:28:26 +00:00
Ralf W. Grosse-Kunstleve
65b3aadc63 merging current boost/python and libs/python from trunk into release branch
[SVN r63937]
2010-07-12 22:29:41 +00:00
Ralf W. Grosse-Kunstleve
38e92833f6 boost/python/module_init.hpp: patch https://svn.boost.org/trac/boost/ticket/3843
[SVN r63777]
2010-07-09 14:50:09 +00:00
Ralf W. Grosse-Kunstleve
14ea71e201 boost/python/converter/builtin_converters.hpp: 64-bit Windows special case to avoid getting a Python long for each std::size_t
[SVN r63696]
2010-07-06 14:29:25 +00:00
Hartmut Kaiser
e13ebcd0e8 Spirit: merging from trunk upto rev. 61489
[SVN r63640]
2010-07-04 22:38:38 +00:00
Marshall Clow
63a3f188b8 Merged changes for #2981, #4166, and #4377 to release branch
[SVN r63523]
2010-07-02 15:37:14 +00:00
Daniel James
34b162738f Merge documentation update.
[SVN r63517]
2010-07-02 08:27:42 +00:00
Daniel James
e481c136e5 Rebuild python documentation.
[SVN r63506]
2010-07-01 22:13:45 +00:00
Daniel James
569b395cdd Merge documentation fixes.
* Use `doc/src/*.css` instead of `doc/html/*.css`.
* Remove wiki and people directories.
* Some documentation fixes.
* Left out `minimal.css` changes and boostbook changes because of clashes.


[SVN r63347]
2010-06-26 12:30:09 +00:00
Daniel James
ea3c254370 Update various libraries' documentation build.
Mostly to use the images and css files under doc/src instead of
doc/html, usually be deleting the settings in order to use the defaults.
Also add 'boost.root' to some builds in order to fix links which rely on
it.

[SVN r63146]
2010-06-20 18:00:48 +00:00
Marshall Clow
ed6e9d6726 Applied patch; fixes #4166
[SVN r63081]
2010-06-18 15:50:25 +00:00
Marshall Clow
1f04129832 Typo; fixes #2891
[SVN r62925]
2010-06-14 01:33:59 +00:00
Steven Watanabe
b63d44e781 Delay the warning that the Boost.Python Jamfile emits, until someone actually tries to use the library. Fixes #2417.
[SVN r62895]
2010-06-13 04:00:10 +00:00
Steven Watanabe
52d7dfffdf Use the convertible_function typedef instead of the raw function pointer type. Fixes #903
[SVN r62808]
2010-06-11 18:06:25 +00:00
Steven Watanabe
a3e76d59c3 Explicitly qualify bind with boost::. Fixes #4223.
[SVN r62198]
2010-05-25 14:22:55 +00:00
Douglas Gregor
4b9cb5337d Include all of the headers needed for the BOOST_MPL_ASSERT that is failing for several compilers
[SVN r62015]
2010-05-16 01:27:58 +00:00
Jeremiah Willcock
5b36b84444 Fixed tab and no-newline-at-end-of-file issues from inspection report
[SVN r61435]
2010-04-20 17:54:16 +00:00
Ralf W. Grosse-Kunstleve
78ec0d12db libs/python/test/Jamfile.v2: moving "rule require-windows" block up to avoid bjam syntax error
[SVN r61086]
2010-04-05 21:28:30 +00:00
Ralf W. Grosse-Kunstleve
a7c16bf695 libs/python/test/Jamfile.v2: compile calling_conventions tests only under Windows, following Valdimir Prus' instructions (boost mailing list)
[SVN r61053]
2010-04-04 21:03:33 +00:00
Ralf W. Grosse-Kunstleve
86db60255a libs/python/test/Jamfile.v2: disabling calling_conventions tests
[SVN r61034]
2010-04-04 05:23:15 +00:00
Ralf W. Grosse-Kunstleve
37b45d2baf merging current boost/python and libs/python from trunk into release branch
[SVN r61033]
2010-04-04 05:19:57 +00:00
Steven Watanabe
6d4be7ab3a Handle the destructor call in a way that keeps msvc happy. Fixes #4003
[SVN r60868]
2010-03-26 19:04:40 +00:00
Ralf W. Grosse-Kunstleve
65e74ccf1e boost/python/object_core.hpp: new .is_none() member function
[SVN r60625]
2010-03-15 22:00:30 +00:00
Vladimir Prus
9398a63250 Implement --python-buildid.
Fixes #3544.
Patch from Gaudenz Steinlin.


[SVN r59987]
2010-02-28 08:22:46 +00:00
Dave Abrahams
1660730320 Remove unintentional reference to "union" test that's not checked in.
[SVN r59331]
2010-01-28 14:39:30 +00:00
Dave Abrahams
5418a663cb Support different MS calling conventions, thanks to Nicolas Lelong.
Closes #3833.


[SVN r59265]
2010-01-25 14:48:49 +00:00
Ralf W. Grosse-Kunstleve
33408d2dcc boost/python/object/make_instance.hpp: missing include added to resolve g++ 4.4 compilation errors
[SVN r59256]
2010-01-24 19:04:59 +00:00
Dave Abrahams
3ad52bce72 Support different MS calling conventions, thanks to Nicolas Lelong.
Closes #3833.


[SVN r59247]
2010-01-24 02:08:46 +00:00
Ralf W. Grosse-Kunstleve
471be524f4 boost.python: merging from trunk to release (gcc 4.4 -std=c++0x compatibility)
[SVN r57837]
2009-11-21 14:41:51 +00:00
Ralf W. Grosse-Kunstleve
4f6a37f80d boost.python: some make_tuple changed to boost::python::make_tuple for gcc 4.4 -std=c++0x compatibility; https://svn.boost.org/trac/boost/ticket/3584
[SVN r57641]
2009-11-13 20:48:25 +00:00
Ralf W. Grosse-Kunstleve
e485244886 boost.python: merging from trunk to release (Troy Straszheim's fix for a long-standing bug)
[SVN r57621]
2009-11-13 00:40:01 +00:00
Troy D. Straszheim
338732793a Don't use type_is_gc... type_is_gc expects a PyTypeObject, which is not what will get passed to it if we pass it to the tp_is_gc fields of static_data_object
[SVN r57590]
2009-11-12 00:46:46 +00:00
Troy D. Straszheim
b9cd3ff109 merge from trunk very minor fixes for ticket #2582
[SVN r57017]
2009-10-20 15:18:26 +00:00
Troy D. Straszheim
8b0655ce94 Fix for #2582, thanks for the patch.
[SVN r57016]
2009-10-20 15:06:21 +00:00
Troy D. Straszheim
36d24b9f8b rm cmake from trunk. I'm not entirely sure this is necessary to satisfy the inspect script, but I'm not taking any chances, and it is easy to put back
[SVN r56942]
2009-10-17 02:07:38 +00:00
Troy D. Straszheim
d804f1250e rm cmake from the release branch before it goes out broken. Policy dictates that you never commit to release, you commit to trunk and merge to release.
[SVN r56941]
2009-10-17 01:10:45 +00:00
Ralf W. Grosse-Kunstleve
89100353db boost/python, libs/python: all changes from trunk merged into branches/release
[SVN r56806]
2009-10-13 22:37:59 +00:00
Ralf W. Grosse-Kunstleve
35ff0adf2b boost/python: some Py_ssize_t replaced with boost::python::ssize_t to restore compatibility with Python 2.3 and 2.4
[SVN r56491]
2009-09-30 12:35:54 +00:00
Stefan Seefeld
115cde9c7f Revert accidental change.
[SVN r56310]
2009-09-19 16:59:05 +00:00
Stefan Seefeld
ef2a02c396 Merged 2009 GSoC work from sandbox-branches/bhy/py3k branch back into trunk.
[SVN r56305]
2009-09-19 02:32:41 +00:00
Ralf W. Grosse-Kunstleve
46be73387c boost/python/object_operators.hpp: merging trunk rev. 55708 into release branch
[SVN r55742]
2009-08-23 21:22:52 +00:00
Ralf W. Grosse-Kunstleve
e3f6f01588 boost/python/object_operators.hpp: added missing error checks in operator bool_type(), operator!(); this resolves https://svn.boost.org/trac/boost/ticket/3356 posted by Stefan Seefeld
[SVN r55708]
2009-08-22 00:18:28 +00:00
Ralf W. Grosse-Kunstleve
d685a5e8c5 python library: sync with trunk to adopt recent exec.cpp fixes
[SVN r55642]
2009-08-18 01:08:46 +00:00
Ralf W. Grosse-Kunstleve
d7389277d3 libs/python/test/exec.cpp: new exercise_embedding_html(), in reaction to https://svn.boost.org/trac/boost/ticket/1890
[SVN r55640]
2009-08-18 00:49:05 +00:00
Ralf W. Grosse-Kunstleve
8d2ca93e98 libs/python/src/exec.cpp: bug fixes
Remark:
  operator!() for boost::python::object invokes PyObject_IsTrue()
  and is therefore not equivalent to "is None".
  In this particular case !global or !local returns true for an
  empty dict.
(Changes to libs/python/test/exec.cpp just helped in debugging.)


[SVN r55639]
2009-08-18 00:24:54 +00:00
Ralf W. Grosse-Kunstleve
e80224b1ad boost/python, libs/python: all changes from trunk merged into branches/release (without any manual modifications)
Commands used:
  svn merge https://svn.boost.org/svn/boost/branches/release/boost/python https://svn.boost.org/svn/boost/trunk/boost/python /net/chevy/raid1/rwgk/boost_release/merge_attempt/boost/boost/python

  svn merge https://svn.boost.org/svn/boost/branches/release/libs/python https://svn.boost.org/svn/boost/trunk/libs/python /net/chevy/raid1/rwgk/boost_release/merge_attempt/boost/libs/python

  svn, version 1.6.4 (r38063)
     compiled Aug 17 2009, 13:31:03


[SVN r55629]
2009-08-17 21:01:18 +00:00
Troy D. Straszheim
d47e3b2a15 Copyrights on CMakeLists.txt to keep them from clogging up the inspect
reports.  This is essentially the same commit as r55095 on the release
branch.



[SVN r55159]
2009-07-26 00:49:56 +00:00
Troy D. Straszheim
03fdf5b992 Add basic copyright/license to keep cmake out of the inspection report
[SVN r55095]
2009-07-22 21:51:01 +00:00
Ralf W. Grosse-Kunstleve
27aa9382fc boost_python_unsigned_converter_fix_no_ctypes.patch by Anderson Lizardo (https://svn.boost.org/trac/boost/ticket/3189)
[SVN r54923]
2009-07-13 15:00:07 +00:00
Ralf W. Grosse-Kunstleve
1a204046c7 boost::python: unsigned converter fix by Anderson Lizardo (https://svn.boost.org/trac/boost/ticket/3189)
[SVN r54919]
2009-07-13 05:16:34 +00:00
Stefan Seefeld
3c98a72153 Use appropriate default values for global and local dicts.
[SVN r53936]
2009-06-15 14:53:48 +00:00
Dave Abrahams
a4f028246a Allow duplicate enum values. Fixes #2744
Thanks to hugo.lima@openbossa.org


[SVN r53660]
2009-06-05 21:18:14 +00:00
Dave Abrahams
6ffeca641c GCC Warning Suppression
[SVN r53659]
2009-06-05 20:15:01 +00:00
Dave Abrahams
22b65a1485 Fix up a lot of missing dependency info. Closes #3136.
[SVN r53657]
2009-06-05 20:12:47 +00:00
Dave Abrahams
d45b9ea66b Add missing dependencies
[SVN r53645]
2009-06-04 23:27:40 +00:00
Vladimir Prus
0373302165 If no python is configured, default-configure it.
Fixes #2846.


[SVN r53380]
2009-05-29 08:18:09 +00:00
Vladimir Prus
ef53bedd0a Don't mention --preserve-test-targets.
Targets are no longer removed by default, so this reference
is unnecessary.

Addresses #2001.


[SVN r53379]
2009-05-29 08:05:49 +00:00
Jeremiah Willcock
73b4cd3325 Fixed almost all tab and min/max issues found by inspect tool
[SVN r53142]
2009-05-20 19:41:20 +00:00
Jeremiah Willcock
3ecb3301a2 Fixed most tab and min/max issues from trunk inspection report
[SVN r53141]
2009-05-20 19:19:00 +00:00
Troy D. Straszheim
e16b3f8ab8 Merge cmake files release -> trunk.
[SVN r52866]
2009-05-09 22:57:30 +00:00
Douglas Gregor
f7d31f6ead Fix Boost.Python tests
[SVN r52856]
2009-05-08 20:42:11 +00:00
Douglas Gregor
47bb3f55a7 Add the pyrun.py script used to run Boost.Python tests via CMake
[SVN r52818]
2009-05-07 04:44:23 +00:00
Joel de Guzman
6296bd5bc4 bumped wrong version
[SVN r52656]
2009-04-29 08:16:02 +00:00
Joel de Guzman
7a7b32661e fixed wrong version
[SVN r52604]
2009-04-26 14:46:18 +00:00
Ralf W. Grosse-Kunstleve
e312047405 libs/python/src/converter/builtin_converters.cpp: Troy Straszheim's bool/int patch with additional BOOST_PYTHON_BOOL_INT_STRICT condition; see also: http://mail.python.org/pipermail/cplusplus-sig/2009-March/014354.html
[SVN r52299]
2009-04-09 17:30:13 +00:00
Troy D. Straszheim
50acffa593 Tune up python tests for removal of our ctest hacks
[SVN r52255]
2009-04-08 15:46:14 +00:00
Troy D. Straszheim
cf5fcc0a1e Test for overload resolution bug.
See this mail for details:

http://mail.python.org/pipermail/cplusplus-sig/2009-March/014362.html



[SVN r52250]
2009-04-08 12:08:35 +00:00
Troy D. Straszheim
c9e694bed8 Build python modules without leading 'lib' and not single threaded
[SVN r52249]
2009-04-08 11:54:20 +00:00
Jeremiah Willcock
0b7333f854 Moved property map library into property_map/ directory; made old files into stubs with #warnings; converted uses and docs of property map library to use new names
[SVN r52226]
2009-04-07 01:28:38 +00:00
John Maddock
217250f078 Merge PDF build changes from Trunk.
[SVN r51417]
2009-02-23 18:39:32 +00:00
John Maddock
98a9fa445b Add PDF generation options to fix external links to point to the web site.
Added a few more Boostbook based libs that were missed first time around.
Fixed PDF naming issues.

[SVN r51284]
2009-02-17 10:05:58 +00:00
John Maddock
354fbb4d24 Add Jamfile to build PDF versions of all the docs.
Tweaked some existing Jamfiles so that PDF build finds all the necessary image files etc.
Tweaked fo.xsl to provide more options by default, and improve formatting.

[SVN r51104]
2009-02-08 16:59:14 +00:00
Troy D. Straszheim
29152af56c merge of cmake build files from trunk per beman
[SVN r50756]
2009-01-24 18:57:20 +00:00
Ralf W. Grosse-Kunstleve
472b18881b Boost.Python enable_shared_from_this patches by Nicolas Lelong and Chad Austin:
http://mail.python.org/pipermail/cplusplus-sig/2008-December/014103.html
  http://mail.python.org/pipermail/cplusplus-sig/2008-February/013003.html


[SVN r50368]
2008-12-23 07:55:33 +00:00
Joel de Guzman
96798a3a38 Cosmetic fixes (improper links)
[SVN r49867]
2008-11-22 03:25:41 +00:00
Michael A. Jackson
7d2f44b21e Updating CMake files to latest trunk. Added dependency information for regression tests and a few new macros for internal use.
[SVN r49627]
2008-11-07 17:02:56 +00:00
Michael A. Jackson
5cda75ebe7 Continuing merge of CMake build system files into trunk with the encouragement of Doug Gregor
[SVN r49510]
2008-11-01 13:15:41 +00:00
Daniel James
19846f5d79 Merge quickbook workaround and fix some links.
Merged revisions 48987,49230-49231 via svnmerge from 
https://svn.boost.org/svn/boost/trunk

........
  r48987 | danieljames | 2008-09-28 13:21:39 +0100 (Sun, 28 Sep 2008) | 1 line
  
  Clean up some link errors.
........
  r49230 | danieljames | 2008-10-09 23:13:48 +0100 (Thu, 09 Oct 2008) | 1 line
  
  position_iterator is meant to be a forward iterator, so avoid using operator+ with it.
........
  r49231 | danieljames | 2008-10-09 23:14:14 +0100 (Thu, 09 Oct 2008) | 4 lines
  
  Work around the problems with window newlines in position_iterator. (I'm
  about to fix them, but this will get quickbook working immediately).
  
  Fixes #2155
........


[SVN r49242]
2008-10-10 09:29:21 +00:00
Daniel James
6347f2e86c Clean up some link errors.
[SVN r48987]
2008-09-28 12:21:39 +00:00
Ralf W. Grosse-Kunstleve
97f3d849df boost/python, boost/mpl: gcc 4.4 compatibility (see http://svn.boost.org/trac/boost/ticket/2069)
[SVN r48960]
2008-09-25 04:49:24 +00:00
Ralf W. Grosse-Kunstleve
61fc9cf054 python/object_core.hpp: "inline" added to declarations, to match definitions (resolves MIPSpro 7.41 warnings)
[SVN r48659]
2008-09-08 02:15:06 +00:00
Ralf W. Grosse-Kunstleve
b3e91f845e boost/python/object_core.hpp: work around Tru64 cxx 6.5 name lookup problems (with fully-qualified names)
[SVN r48629]
2008-09-06 05:30:02 +00:00
Stefan Seefeld
d67cd6717d Add generic call operator support.
[SVN r47846]
2008-07-27 19:41:41 +00:00
Dave Abrahams
a1924a2a72 Merging all trunk changes in Boost.Python to release. The logs for
these changes follow.  r44807 is a merge from another branch; many of
those changes probably don't apply.

  ------------------------------------------------------------------------
  r43423 | danieljames | 2008-02-27 14:22:01 -0500 (Wed, 27 Feb 2008) | 1 line

  Fix incorrect links to copyright of the form 'http:#www.boost.org
  ------------------------------------------------------------------------
  r43546 | rwgk | 2008-03-08 23:30:13 -0500 (Sat, 08 Mar 2008) | 1 line

  map std::invalid_argument -> Python ValueError
  ------------------------------------------------------------------------
  r43799 | grafik | 2008-03-22 18:53:35 -0400 (Sat, 22 Mar 2008) | 1 line

  Remove svn exec property from files that should not be executable to fix permission problems in archives.
  ------------------------------------------------------------------------
  r43845 | dave | 2008-03-24 14:27:22 -0400 (Mon, 24 Mar 2008) | 1 line

  Work around vc9 bugs
  ------------------------------------------------------------------------
  r43901 | bemandawes | 2008-03-27 22:11:13 -0400 (Thu, 27 Mar 2008) | 1 line

  Remove per email from Dave
  ------------------------------------------------------------------------
  r44041 | rwgk | 2008-04-04 14:57:27 -0400 (Fri, 04 Apr 2008) | 1 line

  bogus VC8 warning C4180 disabled
  ------------------------------------------------------------------------
  r44449 | djowel | 2008-04-15 23:07:06 -0400 (Tue, 15 Apr 2008) | 1 line

  Andreas patch
  ------------------------------------------------------------------------
  r44450 | djowel | 2008-04-15 23:07:11 -0400 (Tue, 15 Apr 2008) | 1 line

  Andreas indexing suite patch
  ------------------------------------------------------------------------
  r44807 | danieljames | 2008-04-27 03:39:49 -0400 (Sun, 27 Apr 2008) | 78 lines

  Merge in documentation fixes.  Apart from the change to optional's documenation
  Jamfile, which I included by mistake.

  Fixes #1659, #1661, #1684, #1685, 1687, #1690, #1801

  I wrote about this at:

  http://lists.boost.org/Archives/boost/2008/04/136405.php

  Merged revisions 44585-44806 via svnmerge from 
  https://svn.boost.org/svn/boost/branches/doc

  ........
    r44585 | danieljames | 2008-04-19 16:25:27 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix broken link to vacpp in bjam docs. Refs #1512
  ........
    r44586 | danieljames | 2008-04-19 16:27:36 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix broken link to bcpp in bjam docs. Refs #1513
  ........
    r44587 | danieljames | 2008-04-19 16:33:58 +0100 (Sat, 19 Apr 2008) | 2 lines

    DateTime documentation - Fix a link to the serialization library. Refs #1659
  ........
    r44588 | danieljames | 2008-04-19 16:35:36 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix some links in interprocess & intrusive. Refs #1661
  ........
    r44589 | danieljames | 2008-04-19 16:37:39 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix some links in the python docs. Refs #1684.
  ........
    r44590 | danieljames | 2008-04-19 16:38:29 +0100 (Sat, 19 Apr 2008) | 2 lines

    Work around a quickbook bug which is affecting the python docs. Refs #1684.
  ........
    r44591 | danieljames | 2008-04-19 16:39:34 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix a broken link in the numeric conversion docs. Refs #1685
  ........
    r44592 | danieljames | 2008-04-19 16:40:45 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix some links in the optional docs. Refs #1687
  ........
    r44593 | danieljames | 2008-04-19 16:42:09 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix link to the hash documentation from bimap. Refs #1690
  ........
    r44599 | danieljames | 2008-04-19 18:07:33 +0100 (Sat, 19 Apr 2008) | 2 lines

    Fix a typo in the format library. Refs #1801
  ........
    r44600 | danieljames | 2008-04-19 19:20:59 +0100 (Sat, 19 Apr 2008) | 1 line

    Initialise svnmerge.
  ........
    r44641 | danieljames | 2008-04-20 18:59:47 +0100 (Sun, 20 Apr 2008) | 2 lines

    Fix the lincense url in shared container iterator documentation.
  ........
    r44642 | danieljames | 2008-04-20 19:00:00 +0100 (Sun, 20 Apr 2008) | 2 lines

    Fix image link in the mpi documentation.
  ........
    r44643 | danieljames | 2008-04-20 19:00:11 +0100 (Sun, 20 Apr 2008) | 2 lines

    Fix a typo in the spirit docs.
  ........
    r44644 | danieljames | 2008-04-20 19:00:23 +0100 (Sun, 20 Apr 2008) | 2 lines

    Escape the slash so that quickbook doesn't think it the start of an italic section, and mess up the link. Refs #1844
  ........
    r44647 | danieljames | 2008-04-20 19:39:47 +0100 (Sun, 20 Apr 2008) | 2 lines

    Fix another typo in spirit docs.
  ........

  ------------------------------------------------------------------------
  r45283 | danieljames | 2008-05-11 09:49:20 -0400 (Sun, 11 May 2008) | 1 line

  Quote href values - our tools don't support unquoted values.
  ------------------------------------------------------------------------
  r45359 | rwgk | 2008-05-14 15:38:08 -0400 (Wed, 14 May 2008) | 1 line

  braces added to resolve g++ 4.3.0 warning
  ------------------------------------------------------------------------
  r45918 | rwgk | 2008-05-29 15:48:55 -0400 (Thu, 29 May 2008) | 66 lines

  See Python C++-SIG thread: "object.attr(object& attrname) proposal"
  Started 2008-05-25 by hohehohe2@gmail.com.

  Excerpts:

  If char const* is passed to objecjt.attr(), it uses
  PyObject_GetAttrStrng() or PyObject_SetAttrStrng().  If object is
  passed to objecjt.attr(), it takes the object as a Python string
  object and uses PyObject_GetAttr() or PyObject_SetAttr().

  If attr() behaves like this, it can be useful when there are lots
  of objects which you know have the same attribute name. You can save
  time by first making a boost::python::object and passing it to every
  object's attr() inside a loop.

  I just made a bit of modification to boost:python locally and did a
  quick test, like

  test 1:
    for(int i = 0; i < n; ++i)
    {
      omain.attr(attrname) = 444; //attrname is a char const*
    }

  test 2:
    for(int i = 0; i < n; ++i)
    {
      object o = omain.attr(attrname); //attrname is a char const*
    }

  test 3:
    for(int i = 0; i < n; ++i)
    {
      omain.attr(oaaaa) = 444; //oaaaa is boost::python::object that represents a string
    }

  test 4:
    for(int i = 0; i < n; ++i)
    {
      object o = omain.attr(oaaaa); //oaaaa is boost::python::object that represents a string
    }

  and it reasonably reflected the difference between PyObject_*Attr() and PyObject_*AttrString.

  test 1 :2783ms
  test 2 :2357ms
  test 3 :1882ms
  test 4 :1267ms

  test5: PyObject_SetAttrString(po_main, "aaaa", po_num444);
  test6: Py_DECREF(PyObject_GetAttrString(po_main, "aaaa"));
  test7: PyObject_SetAttr(po_main, po_aaaa, po_num444);
  test8: Py_DECREF(PyObject_GetAttr(po_main, po_aaaa));
  (po_ prefixed variables are PyObject*),

  all inside each for loop, and the results were

  test 5 :2410ms
  test 6 :2277ms
  test 7 :1629ms
  test 8 :1094ms

  It's boost 1.35.0, Python 2.5 on linux(gcc4.1.2).
  I also did the same test on windows(vs8) and the tendency was not
  so different.

  ------------------------------------------------------------------------
  r45920 | rwgk | 2008-05-29 16:04:19 -0400 (Thu, 29 May 2008) | 1 line

  Projects using Boost.Python: PolyBoRi (text provided by Michael Brickenstein)
  ------------------------------------------------------------------------
  r46115 | schoepflin | 2008-06-04 11:11:05 -0400 (Wed, 04 Jun 2008) | 1 line

  Added missing ostream header file.
  ------------------------------------------------------------------------
  r46445 | dgregor | 2008-06-17 09:54:46 -0400 (Tue, 17 Jun 2008) | 1 line

  Add mime-type and eol-style properties as needed
  ------------------------------------------------------------------------
  r46721 | dave | 2008-06-26 12:41:34 -0400 (Thu, 26 Jun 2008) | 2 lines

  Compatibility with Apache STDCXX library.  Don't assume eh.h comes along with the other headers automatically.

  ------------------------------------------------------------------------
  r46808 | bemandawes | 2008-06-28 09:45:21 -0400 (Sat, 28 Jun 2008) | 1 line

  With his kind permission, change Jaakko "J<LATIN SMALL LETTER A WITH DIARESIS>rvi" to "Jarvi"
  ------------------------------------------------------------------------
  r46817 | djowel | 2008-06-28 14:24:17 -0400 (Sat, 28 Jun 2008) | 1 line

  added note on removing targets
  ------------------------------------------------------------------------
  r46844 | dave | 2008-06-29 08:26:17 -0400 (Sun, 29 Jun 2008) | 2 lines

  Make valid HTML

  ------------------------------------------------------------------------




[SVN r47459]
2008-07-15 21:05:24 +00:00
Joel de Guzman
c205cd86c6 added note on removing targets
[SVN r46900]
2008-06-30 12:14:28 +00:00
Dave Abrahams
8d86dc199c merged valid HTML from trunk
[SVN r46845]
2008-06-29 12:55:50 +00:00
Dave Abrahams
c44100afda Make valid HTML
[SVN r46844]
2008-06-29 12:26:17 +00:00
Joel de Guzman
1dee81dc71 added note on removing targets
[SVN r46817]
2008-06-28 18:24:17 +00:00
Beman Dawes
55e9ff14a1 With his kind permission, change Jaakko "Järvi" to "Jarvi"
[SVN r46809]
2008-06-28 13:57:20 +00:00
Beman Dawes
801326275f With his kind permission, change Jaakko "Järvi" to "Jarvi"
[SVN r46808]
2008-06-28 13:45:21 +00:00
Dave Abrahams
98f20f30d6 Compatibility with Apache STDCXX library. Don't assume eh.h comes along with the other headers automatically.
[SVN r46721]
2008-06-26 16:41:34 +00:00
Markus Schöpflin
b01e0e6b9c Added missing ostream header file.
[SVN r46115]
2008-06-04 15:11:05 +00:00
Ralf W. Grosse-Kunstleve
b0a9b11c9c Projects using Boost.Python: PolyBoRi (text provided by Michael Brickenstein)
[SVN r45920]
2008-05-29 20:04:19 +00:00
Ralf W. Grosse-Kunstleve
304277b806 See Python C++-SIG thread: "object.attr(object& attrname) proposal"
Started 2008-05-25 by hohehohe2@gmail.com.

Excerpts:

If char const* is passed to objecjt.attr(), it uses
PyObject_GetAttrStrng() or PyObject_SetAttrStrng().  If object is
passed to objecjt.attr(), it takes the object as a Python string
object and uses PyObject_GetAttr() or PyObject_SetAttr().

If attr() behaves like this, it can be useful when there are lots
of objects which you know have the same attribute name. You can save
time by first making a boost::python::object and passing it to every
object's attr() inside a loop.

I just made a bit of modification to boost:python locally and did a
quick test, like

test 1:
  for(int i = 0; i < n; ++i)
  {
    omain.attr(attrname) = 444; //attrname is a char const*
  }

test 2:
  for(int i = 0; i < n; ++i)
  {
    object o = omain.attr(attrname); //attrname is a char const*
  }

test 3:
  for(int i = 0; i < n; ++i)
  {
    omain.attr(oaaaa) = 444; //oaaaa is boost::python::object that represents a string
  }

test 4:
  for(int i = 0; i < n; ++i)
  {
    object o = omain.attr(oaaaa); //oaaaa is boost::python::object that represents a string
  }

and it reasonably reflected the difference between PyObject_*Attr() and PyObject_*AttrString.

test 1 :2783ms
test 2 :2357ms
test 3 :1882ms
test 4 :1267ms

test5: PyObject_SetAttrString(po_main, "aaaa", po_num444);
test6: Py_DECREF(PyObject_GetAttrString(po_main, "aaaa"));
test7: PyObject_SetAttr(po_main, po_aaaa, po_num444);
test8: Py_DECREF(PyObject_GetAttr(po_main, po_aaaa));
(po_ prefixed variables are PyObject*),

all inside each for loop, and the results were

test 5 :2410ms
test 6 :2277ms
test 7 :1629ms
test 8 :1094ms

It's boost 1.35.0, Python 2.5 on linux(gcc4.1.2).
I also did the same test on windows(vs8) and the tendency was not
so different.


[SVN r45918]
2008-05-29 19:48:55 +00:00
Daniel James
4fea58f634 Add minor documentation fixes to the release branch.
(I left out the changes to the hash library).

Merged via svnmerge from 
https://svn.boost.org/svn/boost/trunk

................
  r44807 | danieljames | 2008-04-27 08:39:49 +0100 (Sun, 27 Apr 2008) | 78 lines
  
  Merge in documentation fixes.  Apart from the change to optional's documenation
  Jamfile, which I included by mistake.
  
  I wrote about this at:
  
  http://lists.boost.org/Archives/boost/2008/04/136405.php
  
  Merged revisions 44585-44806 via svnmerge from 
  https://svn.boost.org/svn/boost/branches/doc
  
  ........
    r44585 | danieljames | 2008-04-19 16:25:27 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    Fix broken link to vacpp in bjam docs.
  ........
    r44586 | danieljames | 2008-04-19 16:27:36 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    Fix broken link to bcpp in bjam docs.
  ........
    r44587 | danieljames | 2008-04-19 16:33:58 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    DateTime documentation - Fix a link to the serialization library.
  ........
    r44588 | danieljames | 2008-04-19 16:35:36 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    Fix some links in interprocess & intrusive.
  ........
    r44589 | danieljames | 2008-04-19 16:37:39 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    Fix some links in the python docs.
  ........
    r44590 | danieljames | 2008-04-19 16:38:29 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    Work around a quickbook bug which is affecting the python docs.
  ........
    r44591 | danieljames | 2008-04-19 16:39:34 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    Fix a broken link in the numeric conversion docs.
  ........
    r44592 | danieljames | 2008-04-19 16:40:45 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    Fix some links in the optional docs.
  ........
    r44593 | danieljames | 2008-04-19 16:42:09 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    Fix link to the hash documentation from bimap.
  ........
    r44599 | danieljames | 2008-04-19 18:07:33 +0100 (Sat, 19 Apr 2008) | 2 lines
    
    Fix a typo in the format library.
  ........
    r44600 | danieljames | 2008-04-19 19:20:59 +0100 (Sat, 19 Apr 2008) | 1 line
    
    Initialise svnmerge.
  ........
    r44641 | danieljames | 2008-04-20 18:59:47 +0100 (Sun, 20 Apr 2008) | 2 lines
    
    Fix the lincense url in shared container iterator documentation.
  ........
    r44642 | danieljames | 2008-04-20 19:00:00 +0100 (Sun, 20 Apr 2008) | 2 lines
    
    Fix image link in the mpi documentation.
  ........
    r44643 | danieljames | 2008-04-20 19:00:11 +0100 (Sun, 20 Apr 2008) | 2 lines
    
    Fix a typo in the spirit docs.
  ........
    r44644 | danieljames | 2008-04-20 19:00:23 +0100 (Sun, 20 Apr 2008) | 2 lines
    
    Escape the slash so that quickbook doesn't think it the start of an italic section, and mess up the link. Refs #1844
  ........
    r44647 | danieljames | 2008-04-20 19:39:47 +0100 (Sun, 20 Apr 2008) | 2 lines
    
    Fix another typo in spirit docs.
  ........
................
  r45232 | danieljames | 2008-05-08 22:50:19 +0100 (Thu, 08 May 2008) | 1 line
  
  Fix some invalid xml by replacing ampersands with character entities.
................
  r45576 | danieljames | 2008-05-20 16:39:25 +0100 (Tue, 20 May 2008) | 20 lines
  
  Merge some small documentation fixes from the doc branch, and mark some
  previously merged changes as merged.
  
  Merged revisions 44811,45129,45142,45154,45281-45282,45365 via svnmerge from 
  https://svn.boost.org/svn/boost/branches/doc
  
  ........
    r45129 | danieljames | 2008-05-05 12:36:50 +0100 (Mon, 05 May 2008) | 2 lines
    
    Update navbar links in boostbook.
  ........
    r45282 | danieljames | 2008-05-11 14:15:31 +0100 (Sun, 11 May 2008) | 2 lines
    
    Group functions in the hash header - requires Frank's free-function-group fix.
    (not included in release branch).
  ........
    r45365 | danieljames | 2008-05-14 21:39:00 +0100 (Wed, 14 May 2008) | 2 lines
    
    Add boost.root to standalone hash documentation.
    (not included in release branch).
  ........
................


[SVN r45622]
2008-05-21 20:57:05 +00:00
Ralf W. Grosse-Kunstleve
a334649b0c braces added to resolve g++ 4.3.0 warning
[SVN r45359]
2008-05-14 19:38:08 +00:00
Daniel James
79b7f88df6 Quote href values - our tools don't support unquoted values.
[SVN r45283]
2008-05-11 13:49:20 +00:00
Daniel James
a33ed032c6 Merge in documentation fixes. Apart from the change to optional's documenation
Jamfile, which I included by mistake.

Fixes #1659, #1661, #1684, #1685, 1687, #1690, #1801

I wrote about this at:

http://lists.boost.org/Archives/boost/2008/04/136405.php

Merged revisions 44585-44806 via svnmerge from 
https://svn.boost.org/svn/boost/branches/doc

........
  r44585 | danieljames | 2008-04-19 16:25:27 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  Fix broken link to vacpp in bjam docs. Refs #1512
........
  r44586 | danieljames | 2008-04-19 16:27:36 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  Fix broken link to bcpp in bjam docs. Refs #1513
........
  r44587 | danieljames | 2008-04-19 16:33:58 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  DateTime documentation - Fix a link to the serialization library. Refs #1659
........
  r44588 | danieljames | 2008-04-19 16:35:36 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  Fix some links in interprocess & intrusive. Refs #1661
........
  r44589 | danieljames | 2008-04-19 16:37:39 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  Fix some links in the python docs. Refs #1684.
........
  r44590 | danieljames | 2008-04-19 16:38:29 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  Work around a quickbook bug which is affecting the python docs. Refs #1684.
........
  r44591 | danieljames | 2008-04-19 16:39:34 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  Fix a broken link in the numeric conversion docs. Refs #1685
........
  r44592 | danieljames | 2008-04-19 16:40:45 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  Fix some links in the optional docs. Refs #1687
........
  r44593 | danieljames | 2008-04-19 16:42:09 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  Fix link to the hash documentation from bimap. Refs #1690
........
  r44599 | danieljames | 2008-04-19 18:07:33 +0100 (Sat, 19 Apr 2008) | 2 lines
  
  Fix a typo in the format library. Refs #1801
........
  r44600 | danieljames | 2008-04-19 19:20:59 +0100 (Sat, 19 Apr 2008) | 1 line
  
  Initialise svnmerge.
........
  r44641 | danieljames | 2008-04-20 18:59:47 +0100 (Sun, 20 Apr 2008) | 2 lines
  
  Fix the lincense url in shared container iterator documentation.
........
  r44642 | danieljames | 2008-04-20 19:00:00 +0100 (Sun, 20 Apr 2008) | 2 lines
  
  Fix image link in the mpi documentation.
........
  r44643 | danieljames | 2008-04-20 19:00:11 +0100 (Sun, 20 Apr 2008) | 2 lines
  
  Fix a typo in the spirit docs.
........
  r44644 | danieljames | 2008-04-20 19:00:23 +0100 (Sun, 20 Apr 2008) | 2 lines
  
  Escape the slash so that quickbook doesn't think it the start of an italic section, and mess up the link. Refs #1844
........
  r44647 | danieljames | 2008-04-20 19:39:47 +0100 (Sun, 20 Apr 2008) | 2 lines
  
  Fix another typo in spirit docs.
........


[SVN r44807]
2008-04-27 07:39:49 +00:00
Joel de Guzman
b316819925 Andreas indexing suite patch
[SVN r44450]
2008-04-16 03:07:11 +00:00
Joel de Guzman
52245de2e5 Andreas patch
[SVN r44449]
2008-04-16 03:07:06 +00:00
Ralf W. Grosse-Kunstleve
2f1f79ce87 bogus VC8 warning C4180 disabled
[SVN r44041]
2008-04-04 18:57:27 +00:00
Beman Dawes
694ae13063 Merge from trunk 43901 at Dave's request
[SVN r43902]
2008-03-28 02:16:12 +00:00
Beman Dawes
863bff9072 Remove per email from Dave
[SVN r43901]
2008-03-28 02:11:13 +00:00
Joel de Guzman
5168895803 Added note about --preserve-test-targets
[SVN r43849]
2008-03-24 23:04:36 +00:00
Joel de Guzman
c8bf94663c Added note about --preserve-test-targets
[SVN r43848]
2008-03-24 23:02:57 +00:00
Dave Abrahams
2213cf98c6 Work around vc9 bugs
[SVN r43845]
2008-03-24 18:27:22 +00:00
Daniel James
928a9389ce Rebuild a lot of documentation.
[SVN r43650]
2008-03-16 11:38:32 +00:00
Ralf W. Grosse-Kunstleve
2dba3148ce map std::invalid_argument -> Python ValueError
[SVN r43546]
2008-03-09 04:30:13 +00:00
Daniel James
7d22435994 Merged revisions 43211,43214-43219,43222-43225,43227-43238,43242,43244-43245,43249-43250,43257-43259,43261,43263,43265,43267-43268,43270-43271,43273,43275-43279,43284-43289,43291,43295,43297-43298,43304-43305,43307,43313,43315,43324,43326-43327,43331,43333,43339-43343,43345,43348,43350,43352-43353,43355-43356,43358,43360,43366-43367,43369-43370,43372-43376,43378-43389,43394,43396-43398,43400-43401,43403-43404,43406-43408,43413-43415,43417-43418,43420,43422-43423 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r43417 | danieljames | 2008-02-26 22:04:55 +0000 (Tue, 26 Feb 2008) | 2 lines
  
  Fix a link to Boost.Bimap.
........
  r43418 | danieljames | 2008-02-26 22:07:25 +0000 (Tue, 26 Feb 2008) | 2 lines
  
  Change another link that's no longer in the repository to link to the website.
........
  r43422 | danieljames | 2008-02-27 18:51:14 +0000 (Wed, 27 Feb 2008) | 1 line
  
  Fix broken copyright urls. Fixes #1573.
........
  r43423 | danieljames | 2008-02-27 19:22:01 +0000 (Wed, 27 Feb 2008) | 1 line
  
  Fix incorrect links to copyright of the form 'http:#www.boost.org
........


[SVN r43425]
2008-02-27 20:00:24 +00:00
Daniel James
48aa6ab9a9 Fix incorrect links to copyright of the form 'http:#www.boost.org
[SVN r43423]
2008-02-27 19:22:01 +00:00
Daniel James
96dd880146 Merged revisions 43206,43208-43213 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r43206 | danieljames | 2008-02-10 09:55:03 +0000 (Sun, 10 Feb 2008) | 1 line
  
  Fix some broken links.
........
  r43209 | danieljames | 2008-02-10 14:56:22 +0000 (Sun, 10 Feb 2008) | 1 line
  
  Link to people pages on the website, as they've been removed from the download.
........
  r43210 | danieljames | 2008-02-10 15:02:17 +0000 (Sun, 10 Feb 2008) | 1 line
  
  Point links to the pages that used to be in 'more' to the site.
........
  r43212 | danieljames | 2008-02-10 16:10:16 +0000 (Sun, 10 Feb 2008) | 1 line
  
  Fix links on the home page as well.
........
  r43213 | danieljames | 2008-02-10 16:21:22 +0000 (Sun, 10 Feb 2008) | 1 line
  
  Generated documentation which is no longer generated.
........


[SVN r43214]
2008-02-10 16:39:38 +00:00
Daniel James
6ba5067e0b Point links to the pages that used to be in 'more' to the site.
[SVN r43210]
2008-02-10 15:02:17 +00:00
Daniel James
60f4f5e54c Link to people pages on the website, as they've been removed from the download.
[SVN r43209]
2008-02-10 14:56:22 +00:00
Eric Niebler
7ee9cf679b stl_iterator does better error handling
[SVN r42836]
2008-01-17 22:47:54 +00:00
Jürgen Hunold
63f8e9f3d7 Merged revisions 41544-41546,41549-41551,41558-41561,41564,41567,41570,41573,41576-41579,41581,41583-41586,41589,41591,41594-41612,41614-41620,41622-41624,41628,41630-41635,41637,41640-41641,41643,41649-41650 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r41544 | jhunold | 2007-12-01 20:27:06 +0100 (Sa, 01 Dez 2007) | 2 lines
  
  Silence compiler by adding cosmetic virtual destructors.
........
  r41549 | jhunold | 2007-12-01 21:24:51 +0100 (Sa, 01 Dez 2007) | 2 lines
  
  Silence compiler by adding cosmetic virtual destructors.
........
  r41550 | jhunold | 2007-12-01 21:26:37 +0100 (Sa, 01 Dez 2007) | 3 lines
  
  Remove unused paramters.
  Add -Wextra to gcc flags to enable more warnings.
........
  r41577 | jhunold | 2007-12-02 12:51:08 +0100 (So, 02 Dez 2007) | 3 lines
  
  Revert revisions 41544 and 41549.
  See http://lists.boost.org/Archives/boost/2007/12/131116.php for details.
........
  r41649 | jhunold | 2007-12-03 19:47:17 +0100 (Mo, 03 Dez 2007) | 2 lines
  
  Silence unused paramter warning in release mode.
........
  r41650 | jhunold | 2007-12-03 19:51:26 +0100 (Mo, 03 Dez 2007) | 2 lines
  
  Add cosmetic virtual detructors to silence compile warnings.
........


[SVN r42094]
2007-12-16 10:12:07 +00:00
Ralf W. Grosse-Kunstleve
03a72363a4 undo revision 41404; see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34238
[SVN r41986]
2007-12-12 05:39:45 +00:00
Jürgen Hunold
38cc1a0c15 Add cosmetic virtual detructors to silence compile warnings.
[SVN r41650]
2007-12-03 18:51:26 +00:00
Jürgen Hunold
ff44521920 Silence unused paramter warning in release mode.
[SVN r41649]
2007-12-03 18:47:17 +00:00
Jürgen Hunold
0ac7e3f858 Revert revisions 41544 and 41549.
See http://lists.boost.org/Archives/boost/2007/12/131116.php for details.


[SVN r41577]
2007-12-02 11:51:08 +00:00
Jürgen Hunold
5cbb539ec5 Remove unused paramters.
Add -Wextra to gcc flags to enable more warnings.


[SVN r41550]
2007-12-01 20:26:37 +00:00
Jürgen Hunold
40e4940877 Silence compiler by adding cosmetic virtual destructors.
[SVN r41549]
2007-12-01 20:24:51 +00:00
Jürgen Hunold
ab0911cf53 Silence compiler by adding cosmetic virtual destructors.
[SVN r41544]
2007-12-01 19:27:06 +00:00
Dave Abrahams
0d81eb6695 Boost.Python:
* Workarounds for many SunCC 5.9 bugs
* Suppression of many SunCC 5.9 warnings
* Improve the style of some test invocations in Jamfile


[SVN r41521]
2007-12-01 02:15:17 +00:00
Dave Abrahams
e0b535df1e Try to extend the workaround to SunPro 5.9, since we're marked as not working on 5.8
[SVN r41408]
2007-11-26 22:01:50 +00:00
Ralf W. Grosse-Kunstleve
d2517faa78 g++ 4.3.0 compatibility (4.3.0 20071125 (experimental))
[SVN r41404]
2007-11-26 20:46:28 +00:00
Beman Dawes
d9b4ada654 Full merge from trunk at revision 41356 of entire boost-root tree.
[SVN r41370]
2007-11-25 18:38:02 +00:00
Beman Dawes
f5df393360 Full merge from trunk at revision 41356 of entire boost-root tree.
[SVN r41369]
2007-11-25 18:07:19 +00:00
Beman Dawes
8cd4ff8950 Remove extra ) from prior commit
[SVN r41355]
2007-11-25 13:56:09 +00:00
Dave Abrahams
26f77691ee Attempt SunPro workaround
[SVN r41352]
2007-11-25 09:02:01 +00:00
Joel de Guzman
6e7f594027 fix for trac ticket #1450
[SVN r41164]
2007-11-17 01:51:04 +00:00
Beman Dawes
eada30f0cb Get rid of .cvsignore files
[SVN r41107]
2007-11-15 15:20:27 +00:00
Joel de Guzman
e919ffdac4 refresh docs
[SVN r41082]
2007-11-14 10:24:21 +00:00
Joel de Guzman
1cf41fd031 fix mismatch include guard
[SVN r41019]
2007-11-11 22:32:48 +00:00
Nikolay Mladenov
7b67118271 changed handle cast to downcast
[SVN r40935]
2007-11-08 16:28:44 +00:00
Nikolay Mladenov
e14c702a40 added forgotten array_object_manager_traits::get_pytype
[SVN r40889]
2007-11-07 16:06:55 +00:00
Joel de Guzman
d303ea16fc added copyright and license info for each page.
[SVN r40871]
2007-11-07 03:35:49 +00:00
Ralf W. Grosse-Kunstleve
65114d8637 reinterpret_cast -> const_cast; commented out dead code removed
[SVN r40749]
2007-11-04 18:08:28 +00:00
Joel de Guzman
597dfc586b tutorial update
[SVN r40734]
2007-11-04 00:12:29 +00:00
Dave Abrahams
6a3085ad5d Merging some of the more obvious changes from RC_1_34_0
[SVN r40714]
2007-11-03 03:25:13 +00:00
Joel de Guzman
190d0d7ea6 replacing reinterpret_cast with static_cast<PySliceObject*>(static_cast<void*>(i))
[SVN r40712]
2007-11-03 03:05:26 +00:00
Eric Niebler
8b915a15ff merge Changeset 37947
[SVN r40675]
2007-11-02 04:35:01 +00:00
Dave Abrahams
90c5c19220 Take out print statement I added for debugging purposes.
[SVN r40536]
2007-10-28 19:24:02 +00:00
Dave Abrahams
cfe6f96f69 Closes #1379, really this time. The old code would sandwich argv[1] between quotes and interpret it as a string, so backslashes in windows paths were interpreted as escape sequences.
[SVN r40535]
2007-10-28 19:22:21 +00:00
Vladimir Prus
77907c5369 Make sure every library can be installed by using
bjam stage|install

in libs/<library>/build.


[SVN r40475]
2007-10-26 09:04:25 +00:00
Rene Rivera
bf33b54638 Recreate release branch from fixed 1.34.1 tag.
[SVN r40341]
2007-10-23 06:42:50 +00:00
Douglas Gregor
87451007b9 Move Attic files back into their rightful places
[SVN r40311]
2007-10-23 01:47:48 +00:00
Rene Rivera
512b30c971 Do not refer to nonexistent target when python is not configured.
[SVN r40216]
2007-10-20 16:36:18 +00:00
Rene Rivera
f005518686 Fix build system error when Python is not configured, without preventing the BPL target from being declared. Instead the target is now unbuildable, and will be skipped when Python is not configured.
[SVN r40156]
2007-10-18 16:11:41 +00:00
Rene Rivera
274a219965 Remove BPL build conditional as it prevents normal build failures.
[SVN r39731]
2007-10-06 19:46:39 +00:00
Beman Dawes
2392a6a3e2 Starting point for releases
[SVN r39706]
2007-10-05 14:25:06 +00:00
Ralf W. Grosse-Kunstleve
abc4abf84a gcc 4.3.0 compatibility (resolves new "changes meaning" error)
[SVN r39434]
2007-09-20 23:20:45 +00:00
Nikolay Mladenov
94a3ced83a fixed cpp signature related test failure
[SVN r39372]
2007-09-18 17:51:47 +00:00
Nikolay Mladenov
7eb0c678ee epydoc friendlier formatting
[SVN r39371]
2007-09-18 17:32:06 +00:00
Nikolay Mladenov
92460adce6 tabs removes, code reformatting
[SVN r39370]
2007-09-18 17:28:23 +00:00
Nikolay Mladenov
8cfd3fb2ef epydoc friendlier formatting
[SVN r39368]
2007-09-18 17:16:31 +00:00
Nikolay Mladenov
62ef542eaf fixed problem reported by Neal Becker; added a test case
[SVN r39223]
2007-09-12 21:31:39 +00:00
Ralf W. Grosse-Kunstleve
5809078ba9 Patches by Nikolay Mladenov (nickm at sitius com): new pythonic signatures; docstring support for enums; fix unrelated Visual C++ 6 problem
[SVN r39191]
2007-09-11 16:53:50 +00:00
Stefan Seefeld
71f54cc920 Fix ticket #1115.
[SVN r38288]
2007-07-26 14:41:41 +00:00
nobody
6c1e7decfa This commit was manufactured by cvs2svn to create tag
'Version_1_34_1'.

[SVN r38286]
2007-07-24 19:28:14 +00:00
Thomas Witt
2851325748 Doc and build fixes by Dave Abrahams.
[SVN r38154]
2007-07-06 19:47:17 +00:00
Thomas Witt
bd606e5017 Fix #583.
[SVN r37947]
2007-06-08 18:30:46 +00:00
Thomas Witt
a5706ec3b0 Fixes for #583.
[SVN r37929]
2007-06-07 18:08:54 +00:00
Rene Rivera
a346c577cf Fix the abolute reference to the Boost sources inserted by Dave.
[SVN r37837]
2007-05-31 22:03:06 +00:00
Dave Abrahams
217e4ca8f8 Repair auto-configuration and allow this project to work with --build-dir
[SVN r37829]
2007-05-31 13:49:45 +00:00
Stefan Seefeld
f2f47f85c0 Enhance embedding python docs.
[SVN r37710]
2007-05-18 15:54:25 +00:00
Stefan Seefeld
b130c93af6 Backport new eval() function from HEAD.
[SVN r37693]
2007-05-15 13:43:52 +00:00
Dave Abrahams
13432b504f Jamfile had extra bogus tst executable target
Build/test instructions were outdated somehow; a checkin must've failed.


[SVN r37620]
2007-05-07 19:00:01 +00:00
Dave Abrahams
0739bb7df8 Added reference to Py++
[SVN r37586]
2007-05-04 01:08:54 +00:00
Dave Abrahams
c253c5cc9c Updated build-and-test howto
[SVN r37585]
2007-05-04 00:56:22 +00:00
Dave Abrahams
8f263e1fdb Progress on the build docs
[SVN r37551]
2007-05-01 16:14:39 +00:00
Ralf W. Grosse-Kunstleve
a6125a3632 merged from trunk
[SVN r37520]
2007-04-27 22:16:47 +00:00
nobody
a784bfc0f8 This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'.
[SVN r37419]
2007-04-11 23:35:09 +00:00
Dave Abrahams
b8937d0bae Relieve need to explicitly configure Python
[SVN r37382]
2007-04-06 18:17:43 +00:00
Dave Abrahams
4827ae73d5 Trivial change to force a rebuild on incremental testers' machines.
[SVN r37327]
2007-04-02 00:54:48 +00:00
Dave Abrahams
8eddc7aa37 Support for --with-pydebug builds.
python.jam:

  Support for the specification of "_d" extension suffix.

  In compute-default-paths, fixed the check for residence in
  a "PCBuild.*" directory so we can build against Windows Python built
  in a source distribution.

common.jam:

  Fixed generation of the "y" tag to look for <python-debugging>on
  rather than the whole debug-python build variant.

  Fixed some grammar and spelling.

virtual-target.jam:

  Added the ability to forego the prepending of "." to a
  generated-target-suffix by specifying the suffix enclosed in <...>

libs/python/build/Jamfile.v2:

  #define BOOST_DEBUG_PYTHON when <python-debugging>on


[SVN r37326]
2007-04-02 00:51:15 +00:00
Stefan Seefeld
98a468dadc Fix reference counting error.
[SVN r37312]
2007-03-28 18:12:08 +00:00
Dave Abrahams
ea4e6c0a4c Trivial change to force a rebuild on incremental testers' machines.
[SVN r37311]
2007-03-28 12:44:57 +00:00
Dave Abrahams
7cd7f6d8ee Trivial change to force a rebuild on incremental testers' machines.
[SVN r37287]
2007-03-26 15:47:05 +00:00
Dave Abrahams
4c39e8c990 Making a trivial change to trigger a test run from incremental testers
[SVN r37278]
2007-03-24 18:57:19 +00:00
Dave Abrahams
6c6f654fbe Add commented-out test for import_ so it's easy to reinstate
[SVN r37256]
2007-03-21 17:44:57 +00:00
Dave Abrahams
b7eaea096e Eliminate import_ test, as it doesn't work consistently on Windows and Linux.
[SVN r37250]
2007-03-21 05:09:21 +00:00
Dave Abrahams
904ae8604c fix builtin_converters test so it can work (BBv1 allowed the
duplication of main target names; BBv2 does not)


[SVN r37216]
2007-03-17 20:26:21 +00:00
Dave Abrahams
44d53c448b libs/python/build/Jamfile.v2: remove needless <define> property.
libs/python/test/Jamfile.v2:
  * add dynamically-linked embedding test

  * fix builtin_converters test so it can work (BBv1 allowed the
    duplication of main target names; BBv2 does not)


libs/python/test/import_.cpp: move some more of the Python code within
  a handle_exception callback so at least we can better diagnose
  failures.


[SVN r37214]
2007-03-17 20:17:56 +00:00
Stefan Seefeld
e61401d27e Fix import_ failure.
[SVN r37141]
2007-03-05 18:44:45 +00:00
Stefan Seefeld
be7ca7d269 Add copyright notice.
[SVN r37132]
2007-03-02 17:16:51 +00:00
Stefan Seefeld
47b4b4efbb Fix boost::python::import.
[SVN r37123]
2007-03-01 18:31:10 +00:00
nobody
4fa07f2b3d This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'.
[SVN r37121]
2007-03-01 15:17:30 +00:00
Dave Abrahams
c880e7d69d Correct testing bugs:
either changing assert(...) or BOOST_ASSERT(...) to BOOST_TEST
    (in my code only)

    or adding "return boost::report_errors();" where it was clearly
    missing (and a pure bug, in anyone's code).

    or changing BOOST_TEST to BOOST_CHECK where the integer library
    was clearly using Boost.Test and not returning report_errors().


[SVN r37063]
2007-02-25 15:28:02 +00:00
Dave Abrahams
c73ad50286 Roll back improved error message because it causes problems for vc6/7
[SVN r36435]
2006-12-16 22:00:35 +00:00
Dave Abrahams
0910710ac4 Correct class/function mismatches for MSVC and bring across
documentation tweaks from HEAD.


[SVN r36355]
2006-12-13 15:08:26 +00:00
Dave Abrahams
04c528138b Fully switch to BBv2
[SVN r36327]
2006-12-11 05:22:04 +00:00
nobody
1b66cd9643 This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'.
[SVN r36324]
2006-12-11 05:02:35 +00:00
Dave Abrahams
c2dd9fa833 Fix auto-link to look at the right variable.
[SVN r36319]
2006-12-11 03:00:26 +00:00
Dave Abrahams
b085121369 merged from trunk
[SVN r36292]
2006-12-07 17:45:17 +00:00
Beman Dawes
fa219bce9b Merged copyright and license addition
[SVN r35907]
2006-11-07 19:27:00 +00:00
Rene Rivera
10b85d67e7 Remove obsolete Boost.Build v1 files.
[SVN r35880]
2006-11-06 17:10:46 +00:00
Stefan Seefeld
ca91dc828e Fix symbol visibility.
[SVN r35754]
2006-10-27 21:19:47 +00:00
Dave Abrahams
3a53c1dec0 Add fixes somehow missed for darwin
[SVN r35612]
2006-10-14 19:10:30 +00:00
nobody
da8e309957 This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'.
[SVN r35598]
2006-10-13 21:34:27 +00:00
Dave Abrahams
2bd9141d4a make numpy tests portable to Darwin with older docutils
[SVN r35597]
2006-10-13 21:34:26 +00:00
Dave Abrahams
c3bda6a903 Fix some problems with testing on old docutils installations
[SVN r35594]
2006-10-13 19:35:28 +00:00
Dave Abrahams
d61909d3ea Make object comparison operators return object instead of bool, to
accomodate strange beasts like numarray arrays that return arrays that
can't be used as truth values from their comparison ops.

Fix numpy test for portability with old doctest (again!)


[SVN r35572]
2006-10-12 09:07:07 +00:00
Dave Abrahams
1755dad7e6 merged from trunk
[SVN r35569]
2006-10-12 06:42:20 +00:00
Dave Abrahams
3b392c99be Adjust tests to account for numarray behavior differences
[SVN r35539]
2006-10-10 22:44:09 +00:00
Dave Abrahams
24ba93607b Try for backward compatibility with older versions of doctest
[SVN r35535]
2006-10-10 18:12:43 +00:00
Dave Abrahams
49d4aac8ec Fix lots of bugs in the numeric interface and tests.
Tests:
* Coerce a result to bool to deal with Python's new Bool type
* Better reporting of mismatches in expected and received results
* Remove bogus nullary y.astype() call
* Fix all uses of trace and diagonal so they don't cause errors
* Use appropriate typecodes
* Use doctest detailed API to run just the relevant tests
* Factor out error handling from macro

API:
* Added get_module_name() function to get current numeric module
* new_(x) now returns an array instead of object
* Fixed the signatures of the factory() family of functions
* Updated docs accordingly.


[SVN r35528]
2006-10-09 04:05:25 +00:00
Dave Abrahams
545be29ad3 (hopefully) grab the last bit of logic from HEAD for a bugfix I've been trying to apply to the branch for days(!)
[SVN r35436]
2006-10-01 18:25:44 +00:00
Dave Abrahams
8553c109c7 merged from HEAD
[SVN r35429]
2006-09-29 22:27:57 +00:00
Dave Abrahams
315c3d50ee Apply missing elements of previously-"applied" bug fix.
[SVN r35421]
2006-09-29 15:45:29 +00:00
Dave Abrahams
d5219979a4 Fix bugs uncovered by Roman Yakovenko
[SVN r35410]
2006-09-29 02:09:13 +00:00
Dave Abrahams
d42054f3a0 Cleans up license/copyright warnings
[SVN r35409]
2006-09-29 02:08:32 +00:00
nobody
72b06e70ee This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'.
[SVN r35366]
2006-09-28 14:41:02 +00:00
Dave Abrahams
375cc3aa93 Fix broken links
[SVN r35330]
2006-09-26 04:25:49 +00:00
Ralf W. Grosse-Kunstleve
4eb286a034 ssize_t patches merged from HEAD
[SVN r35327]
2006-09-26 00:48:44 +00:00
nobody
a824230155 This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'.
[SVN r35326]
2006-09-26 00:25:08 +00:00
Gottfried Ganßauge
1bc3750ab3 repaired broken links
[SVN r35315]
2006-09-25 07:35:57 +00:00
Dave Abrahams
36abcee847 merged from HEAD
[SVN r35277]
2006-09-22 15:13:30 +00:00
Gottfried Ganßauge
b8b7768eb7 Test for cross module support of opaque
[SVN r35256]
2006-09-21 16:47:49 +00:00
Gottfried Ganßauge
7ad9dc6c64 Changed references to opaque_pointer_converter.html to point to opaque.html instead
[SVN r35255]
2006-09-21 16:47:31 +00:00
Gottfried Ganßauge
decc34551a Renamed to opaque.html because class name changed
[SVN r35254]
2006-09-21 16:47:00 +00:00
Gottfried Ganßauge
5acb44ede0 Renamed from opaque_pointer_converter.html because class name changed
[SVN r35253]
2006-09-21 16:46:42 +00:00
Gottfried Ganßauge
ef62f87963 type object gets initialized. Cross module support works
[SVN r35252]
2006-09-21 16:46:21 +00:00
Joel de Guzman
b5c893381f merge from head
[SVN r35246]
2006-09-21 10:52:01 +00:00
Vladimir Prus
815969bf8b Merge: Add tests
[SVN r35245]
2006-09-21 07:27:11 +00:00
nobody
0d57e9e808 This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'.
[SVN r35243]
2006-09-21 07:07:15 +00:00
Dave Abrahams
7ba6a00617 Apply Boost license, with permission from Prabhu Ramachandran.
[SVN r35240]
2006-09-21 03:43:59 +00:00
Dave Abrahams
e70bbe4791 Fix license/copyright.
Also port some "glaringly obvious" bugfixes from HEAD.  Hope it
doesn't cause problems.


[SVN r35237]
2006-09-20 22:49:18 +00:00
Stefan Seefeld
7ff0f62729 Fix copyright issues.
[SVN r35236]
2006-09-20 22:30:39 +00:00
Dave Abrahams
95f0b39c90 merged from HEAD
[SVN r35185]
2006-09-18 22:22:31 +00:00
nobody
23057688f9 This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'.
[SVN r35170]
2006-09-18 19:56:20 +00:00
Dave Abrahams
029618f719 merged from HEAD
[SVN r35165]
2006-09-18 18:25:57 +00:00
Dave Abrahams
38f7a4e9b6 merge from HEAD
[SVN r35141]
2006-09-17 02:42:51 +00:00
Dave Abrahams
326c871224 attempt unverified workaround for http://tinyurl.com/gvrgd
[SVN r35103]
2006-09-13 22:47:11 +00:00
Dave Abrahams
924eeccd53 merged from trunk
[SVN r35080]
2006-09-13 00:02:56 +00:00
Dave Abrahams
4a63cf4843 Move definition of BOOST_PYTHON_SUPPRESS_REGISTRY_INITIALIZATION back
where it belongs.


[SVN r35076]
2006-09-12 22:37:09 +00:00
Dave Abrahams
1d65b74273 Add missing license/copyright
[SVN r35070]
2006-09-11 22:27:29 +00:00
Dave Abrahams
3c1ae689a7 merged from trunk
[SVN r34943]
2006-08-24 19:04:23 +00:00
Dave Abrahams
a99bd50e52 merged from trunk
[SVN r34940]
2006-08-24 13:05:30 +00:00
Dave Abrahams
8b178594ff merged from trunk
[SVN r34915]
2006-08-22 11:51:18 +00:00
Dave Abrahams
786aeef998 merged from trunk
[SVN r34872]
2006-08-11 15:51:09 +00:00
Dave Abrahams
45e4cf506f merged from trunk
[SVN r34865]
2006-08-11 00:48:19 +00:00
Gennaro Prota
8fb6e1f48d (merge from head)
removed tabs (inspect tool)


[SVN r34723]
2006-07-24 22:28:41 +00:00
Gennaro Prota
ecf70b05f2 (merge from head)
removed tabs (inspect tool)


[SVN r34721]
2006-07-24 22:21:39 +00:00
Gennaro Prota
c54acdb9db (merge from head)
minor fix: violation of min/max guidelines


[SVN r34718]
2006-07-24 22:06:09 +00:00
Vladimir Prus
489dae2b58 Clarify comment
[SVN r34668]
2006-07-22 12:53:49 +00:00
Vladimir Prus
f852ce6f7b Windows
[SVN r34667]
2006-07-22 12:28:43 +00:00
Vladimir Prus
e500919d25 Merge: Don't link Boost.Python to python library
[SVN r34663]
2006-07-22 07:16:18 +00:00
Markus Schöpflin
7a645a6d8a Merged header inclusion order fix from trunk to release branch.
[SVN r34195]
2006-06-06 09:44:35 +00:00
Markus Schöpflin
fa2185e5ab Merged header inclusion order fix from trunk to release branch.
[SVN r34194]
2006-06-06 09:39:16 +00:00
Vladimir Prus
5cc33461dd Merge from trunk
[SVN r33626]
2006-04-10 09:02:57 +00:00
Markus Schöpflin
7b93f2fdc5 Merged fix from HEAD for Tru64/CXX compilation error.
[SVN r33455]
2006-03-23 09:40:38 +00:00
nobody
219743964d This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'.
[SVN r33417]
2006-03-21 02:26:31 +00:00
402 changed files with 8060 additions and 5723 deletions

View File

@@ -3,75 +3,149 @@
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
import os ; import os ;
import indirect ;
import modules ; import modules ;
import feature ;
import python ; import python ;
if [ python.configured ] { if ! [ python.configured ] && ! ( --without-python in [ modules.peek : ARGV ] )
{
# Attempt default configuration of python
import toolset : using ;
using python ;
}
project boost/python if [ python.configured ] || ( --without-python in [ modules.peek : ARGV ] )
: source-location ../src {
; alias config-warning ;
lib boost_python
: # 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/stl_iterator.cpp
object_protocol.cpp
object_operators.cpp
wrapper.cpp
import.cpp
exec.cpp
: # requirements
<link>static:<define>BOOST_PYTHON_STATIC_LIB
<define>BOOST_PYTHON_SOURCE
# 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
{ {
ECHO "warning: Python location is not configured" ; message config-warning
ECHO "warning: the Boost.Python library won't be built" ; : "warning: No python installation configured and autoconfiguration"
: "note: failed. See http://www.boost.org/libs/python/doc/building.html"
: "note: for configuration instructions or pass --without-python to"
: "note: suppress this message and silently skip all Boost.Python targets"
;
}
rule find-py3-version
{
local versions = [ feature.values python ] ;
local py3ver ;
for local v in $(versions)
{
if $(v) >= 3.0
{
py3ver = $(v) ;
}
}
return $(py3ver) ;
}
py3-version = [ find-py3-version ] ;
project boost/python
: source-location ../src
: requirements
-<tag>@$(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).tag
<tag>@$(__name__).tag
;
rule tag ( name : type ? : property-set )
{
local result = $(name) ;
if $(type) in STATIC_LIB SHARED_LIB IMPORT_LIB
{
if $(name) = boost_python && $(PYTHON_ID)
{
result = $(result)-$(PYTHON_ID) ;
}
}
# forward to the boost tagging rule
return [ indirect.call $(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).tag
$(result) : $(type) : $(property-set) ] ;
}
rule cond ( test ? : yes * : no * ) { if $(test) { return $(yes) ; } else { return $(no) ; } }
rule unless ( test ? : yes * : no * ) { if ! $(test) { return $(yes) ; } else { return $(no) ; } }
rule lib_boost_python ( is-py3 ? )
{
lib [ cond $(is-py3) : boost_python3 : boost_python ]
: # sources
numeric.cpp
list.cpp
long.cpp
dict.cpp
tuple.cpp
str.cpp
slice.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/stl_iterator.cpp
object_protocol.cpp
object_operators.cpp
wrapper.cpp
import.cpp
exec.cpp
object/function_doc_signature.cpp
: # requirements
<link>static:<define>BOOST_PYTHON_STATIC_LIB
<define>BOOST_PYTHON_SOURCE
# 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.
[ cond [ python.configured ] : <library>/python//python_for_extensions ]
# we prevent building when there is no python available
# as it's not possible anyway, and to cause dependents to
# fail to build
[ unless [ python.configured ] : <build>no ]
<dependency>config-warning
<python-debugging>on:<define>BOOST_DEBUG_PYTHON
[ cond $(is-py3) : <python>$(py3-version) ]
: # default build
<link>shared
: # usage requirements
<link>static:<define>BOOST_PYTHON_STATIC_LIB
<python-debugging>on:<define>BOOST_DEBUG_PYTHON
;
}
lib_boost_python ;
boost-install boost_python ;
if $(py3-version)
{
lib_boost_python yes ;
boost-install boost_python3 ;
} }

View File

@@ -1,910 +0,0 @@
# Microsoft Developer Studio Project File - Name="boost_python" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
CFG=BOOST_PYTHON - WIN32 RELEASE
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "boost_python.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "boost_python.mak" CFG="BOOST_PYTHON - WIN32 RELEASE"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "boost_python - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE "boost_python - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
MTL=midl.exe
RSC=rc.exe
!IF "$(CFG)" == "boost_python - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "../bin-stage"
# PROP Intermediate_Dir "release-obj"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
F90=df.exe
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BPL_EXPORTS" /YX /FD /Zm800 /Zm800 /Zm800 /c
# ADD CPP /nologo /MD /W3 /GR /GX /O2 /I "../../../../" /D "NDEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BOOST_PYTHON_DYNAMIC_LIB" /D "BOOST_PYTHON_SOURCE" /FD /Zm800 /Zm800 /Zm800 /Zm800 /c
# SUBTRACT CPP /YX
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x1409 /d "NDEBUG"
# ADD RSC /l 0x1409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
# ADD LINK32 kernel32.lib user32.lib /nologo /dll /machine:I386
!ELSEIF "$(CFG)" == "boost_python - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "../bin-stage"
# PROP Intermediate_Dir "debug-obj"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
F90=df.exe
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BPL_EXPORTS" /YX /FD /Zm800 /Zm800 /Zm800 /GZ /c
# ADD CPP /nologo /MDd /W3 /GR /GX /Zi /Od /I "../../../../" /D "_DEBUG" /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BOOST_PYTHON_DYNAMIC_LIB" /D "BOOST_PYTHON_SOURCE" /FD /Zm800 /Zm800 /Zm800 /Zm800 /Zm800 /GZ /c
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x1409 /d "_DEBUG"
# ADD RSC /l 0x1409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib /nologo /dll /incremental:no /debug /machine:I386 /out:"../bin-stage/boost_python_debug.dll" /pdbtype:sept
!ENDIF
# Begin Target
# Name "boost_python - Win32 Release"
# Name "boost_python - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\..\src\aix_init_module.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\converter\arg_to_python_base.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\converter\builtin_converters.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object\class.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\dict.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object\enum.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\errors.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\converter\from_python.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object\function.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object\inheritance.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object\iterator.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object\life_support.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\list.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\long.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\module.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\numeric.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object_operators.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object_protocol.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object\pickle_support.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\converter\registry.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\slice.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\object\stl_iterator.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\str.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\tuple.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\converter\type_id.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\wrapper.cpp
# 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
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Group "detail"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\aix_init_module.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\api_placeholder.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\arg_tuple_size.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\borrowed_ptr.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\call_object.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\caller.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\char_array.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\config.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\construct.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\convertible.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\cv_category.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\decorated_type_id.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\def_helper.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\defaults_def.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\defaults_gen.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\dependent.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\destroy.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\exception_handler.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\force_instantiate.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\if_else.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\indirect_traits.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\make_keyword_range_fn.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\make_tuple.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\map_entry.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\member_function_cast.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\module_base.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\module_init.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\msvc_typeinfo.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\none.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\not_specified.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\operator_id.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\overloads_fwd.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\pointee.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\preprocessor.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\python22_fixed.h
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\raw_pyobject.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\referent_storage.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\result.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\returning.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\scope.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\string_literal.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\target.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\translate_exception.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\type_list.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\type_list_impl.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\type_list_impl_no_pts.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\type_list_utils.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\unwind_type.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\void_ptr.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\void_return.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\detail\wrap_python.hpp
# End Source File
# End Group
# Begin Group "converter"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\arg_from_python.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\arg_to_python.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\arg_to_python_base.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\builtin_converters.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\constructor_function.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\convertible_function.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\from_python.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\implicit.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\obj_mgr_arg_from_python.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\object_manager.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\pointer_type_id.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\pyobject_traits.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\pyobject_type.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\pytype_arg_from_python.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\pytype_object_mgr_traits.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\registered.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\registered_pointee.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\registrations.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\registry.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\return_from_python.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\rvalue_from_python_data.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\converter\to_python_function_type.hpp
# End Source File
# End Group
# Begin Group "object"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\add_to_namespace.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\class.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\class_converters.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\class_detail.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\class_wrapper.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\construct.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\enum_base.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\find_instance.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\forward.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\function.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\function_handle.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\function_object.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\inheritance.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\instance.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\iterator.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\iterator_core.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\life_support.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\make_holder.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\make_instance.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\pickle_support.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\pointer_holder.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\py_function.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\select_holder.hpp
# End 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
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object\value_holder_fwd.hpp
# End Source File
# End Group
# Begin Source File
SOURCE=..\..\..\..\boost\python\arg_from_python.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\args.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\args_fwd.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\back_reference.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\base_type_traits.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\bases.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\borrowed.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\call.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\call_method.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\cast.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\class.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\class_fwd.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\copy_const_reference.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\copy_non_const_reference.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\data_members.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\def.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\default_call_policies.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\dict.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\enum.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\errors.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\exception_translator.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\extract.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\handle.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\handle_fwd.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\has_back_reference.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\implicit.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\init.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\instance_holder.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\iterator.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\list.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\long.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\lvalue_from_pytype.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\make_function.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\manage_new_object.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\module.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\module_init.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\numeric.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object_attributes.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object_call.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object_core.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object_fwd.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object_items.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object_operators.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object_protocol.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object_protocol_core.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\object_slices.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\operators.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\operators2.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\other.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\overloads.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\pointee.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\proxy.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\ptr.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\refcount.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\reference_existing_object.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\return_internal_reference.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\return_value_policy.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\scope.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\self.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\signature.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\slice_nil.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\stl_iterator.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\str.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\tag.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\to_python_converter.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\to_python_indirect.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\to_python_value.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\tuple.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\type_id.hpp
# End Source File
# Begin Source File
SOURCE=..\..\..\..\boost\python\with_custodian_and_ward.hpp
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project

View File

@@ -1,29 +0,0 @@
Microsoft Developer Studio Workspace File, Format Version 6.00
# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!
###############################################################################
Project: "boost_python"=".\boost_python.dsp" - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
}}}
###############################################################################
Global:
Package=<5>
{{{
}}}
Package=<3>
{{{
}}}
###############################################################################

28
class.cpp Normal file
View File

@@ -0,0 +1,28 @@
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/object.hpp>
#include <boost/python/class.hpp>
using namespace boost::python;
struct X
{
int x;
X(int n) : x(n) { }
};
int x_function(X& x)
{ return x.x;
}
BOOST_PYTHON_MODULE(class_ext)
{
class_<X>("X", init<int>());
def("x_function", x_function);
}
#include "module_tail.cpp"

View File

@@ -27,7 +27,6 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Somerville, MA 02143 Somerville, MA 02143
:Contact: dave@boost-consulting.com :Contact: dave@boost-consulting.com
:organization: `Boost Consulting`_ :organization: `Boost Consulting`_
:date: $Date$
:status: This is a "work in progress" :status: This is a "work in progress"
:version: 1 :version: 1
:copyright: Copyright David Abrahams 2002. All rights reserved :copyright: Copyright David Abrahams 2002. All rights reserved

View File

@@ -1,8 +1,6 @@
/* /*
:Author: David Goodger :Author: David Goodger
:Contact: goodger@users.sourceforge.net :Contact: goodger@users.sourceforge.net
:date: $Date$
:version: $Revision$
:copyright: This stylesheet has been placed in the public domain. :copyright: This stylesheet has been placed in the public domain.
boostinspect:nolicense boostinspect:nolicense

View File

@@ -9,60 +9,63 @@
</head> </head>
<body> <body>
<div class="document" id="logo-boost-python-build-and-test-howto"> <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> <h1 class="title"><a class="reference external" href="../index.html"><img alt="Boost C++ Libraries:" class="boost-logo" src="../../../boost.png" /></a> Boost.Python Build and Test HOWTO</h1>
<!-- Copyright David Abrahams 2006. Distributed under the Boost --> <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
<!-- Software License, Version 1.0. (See accompanying --> <!-- Software License, Version 1.0. (See accompanying -->
<!-- file LICENSE_1_0.txt or copy at --> <!-- file LICENSE_1_0.txt or copy at -->
<!-- http://www.boost.org/LICENSE_1_0.txt) --> <!-- http://www.boost.org/LICENSE_1_0.txt) -->
<div class="contents sidebar small topic"> <div class="contents sidebar small topic" id="contents">
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p> <p class="topic-title first">Contents</p>
<ul class="auto-toc simple"> <ul class="auto-toc simple">
<li><a class="reference" href="#requirements" id="id20" name="id20">1&nbsp;&nbsp;&nbsp;Requirements</a></li> <li><a class="reference internal" href="#requirements" id="id25">1&nbsp;&nbsp;&nbsp;Requirements</a></li>
<li><a class="reference" href="#background" id="id21" name="id21">2&nbsp;&nbsp;&nbsp;Background</a></li> <li><a class="reference internal" href="#background" id="id26">2&nbsp;&nbsp;&nbsp;Background</a></li>
<li><a class="reference" href="#getting-boost-python-binaries" id="id22" name="id22">3&nbsp;&nbsp;&nbsp;Getting Boost.Python Binaries</a><ul class="auto-toc"> <li><a class="reference internal" href="#no-install-quickstart" id="id27">3&nbsp;&nbsp;&nbsp;No-Install Quickstart</a><ul class="auto-toc">
<li><a class="reference" href="#no-install-quickstart" id="id23" name="id23">3.1&nbsp;&nbsp;&nbsp;No-Install Quickstart</a></li> <li><a class="reference internal" href="#basic-procedure" id="id28">3.1&nbsp;&nbsp;&nbsp;Basic Procedure</a></li>
<li><a class="reference" href="#installing-boost-python-on-your-system" id="id24" name="id24">3.2&nbsp;&nbsp;&nbsp;Installing Boost.Python on your System</a></li> <li><a class="reference internal" href="#in-case-of-trouble" id="id29">3.2&nbsp;&nbsp;&nbsp;In Case of Trouble</a></li>
<li><a class="reference internal" href="#in-case-everything-seemed-to-work" id="id30">3.3&nbsp;&nbsp;&nbsp;In Case Everything Seemed to Work</a></li>
<li><a class="reference internal" href="#modifying-the-example-project" id="id31">3.4&nbsp;&nbsp;&nbsp;Modifying the Example Project</a></li>
</ul> </ul>
</li> </li>
<li><a class="reference" href="#configuring-boost-build" id="id25" name="id25">4&nbsp;&nbsp;&nbsp;Configuring Boost.Build</a></li> <li><a class="reference internal" href="#installing-boost-python-on-your-system" id="id32">4&nbsp;&nbsp;&nbsp;Installing Boost.Python on your System</a></li>
<li><a class="reference" href="#building-an-extension-module" id="id26" name="id26">5&nbsp;&nbsp;&nbsp;Building an Extension Module</a></li> <li><a class="reference internal" href="#configuring-boost-build" id="id33">5&nbsp;&nbsp;&nbsp;Configuring Boost.Build</a><ul class="auto-toc">
<li><a class="reference" href="#testing" id="id27" name="id27">6&nbsp;&nbsp;&nbsp;Testing</a></li> <li><a class="reference internal" href="#python-configuration-parameters" id="id34">5.1&nbsp;&nbsp;&nbsp;Python Configuration Parameters</a></li>
<li><a class="reference" href="#advanced-configuration" id="id28" name="id28">7&nbsp;&nbsp;&nbsp;Advanced Configuration</a><ul class="auto-toc"> <li><a class="reference internal" href="#examples" id="id35">5.2&nbsp;&nbsp;&nbsp;Examples</a></li>
<li><a class="reference" href="#python-configuration-parameters" id="id29" name="id29">7.1&nbsp;&nbsp;&nbsp;Python Configuration Parameters</a></li>
<li><a class="reference" href="#examples" id="id30" name="id30">7.2&nbsp;&nbsp;&nbsp;Examples</a></li>
</ul> </ul>
</li> </li>
<li><a class="reference" href="#choosing-a-boost-python-library-binary" id="id31" name="id31">8&nbsp;&nbsp;&nbsp;Choosing a Boost.Python Library Binary</a><ul class="auto-toc"> <li><a class="reference internal" href="#choosing-a-boost-python-library-binary" id="id36">6&nbsp;&nbsp;&nbsp;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&nbsp;&nbsp;&nbsp;The Dynamic Binary</a></li> <li><a class="reference internal" href="#the-dynamic-binary" id="id37">6.1&nbsp;&nbsp;&nbsp;The Dynamic Binary</a></li>
<li><a class="reference" href="#the-static-binary" id="id33" name="id33">8.2&nbsp;&nbsp;&nbsp;The Static Binary</a></li> <li><a class="reference internal" href="#the-static-binary" id="id38">6.2&nbsp;&nbsp;&nbsp;The Static Binary</a></li>
</ul> </ul>
</li> </li>
<li><a class="reference" href="#notes-for-mingw-and-cygwin-with-mno-cygwin-gcc-users" id="id34" name="id34">9&nbsp;&nbsp;&nbsp;Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users</a></li> <li><a class="reference internal" href="#include-issues" id="id39">7&nbsp;&nbsp;&nbsp;<tt class="docutils literal"><span class="pre">#include</span></tt> Issues</a></li>
<li><a class="reference internal" href="#python-debugging-builds" id="id40">8&nbsp;&nbsp;&nbsp;Python Debugging Builds</a></li>
<li><a class="reference internal" href="#testing-boost-python" id="id41">9&nbsp;&nbsp;&nbsp;Testing Boost.Python</a></li>
<li><a class="reference internal" href="#notes-for-mingw-and-cygwin-with-mno-cygwin-gcc-users" id="id42">10&nbsp;&nbsp;&nbsp;Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users</a></li>
</ul> </ul>
</div> </div>
<div class="section"> <div class="section" id="requirements">
<h1><a class="toc-backref" href="#id20" id="requirements" name="requirements">1&nbsp;&nbsp;&nbsp;Requirements</a></h1> <h1><a class="toc-backref" href="#id25">1&nbsp;&nbsp;&nbsp;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> <p>Boost.Python requires <a class="reference external" href="http://www.python.org/2.2">Python 2.2</a><a class="footnote-reference" href="#id22" id="id2"><sup>1</sup></a> <em>or</em> <a class="reference external" href="http://www.python.org"><em>newer</em></a>.</p>
</div> </div>
<div class="section"> <div class="section" id="background">
<h1><a class="toc-backref" href="#id21" id="background" name="background">2&nbsp;&nbsp;&nbsp;Background</a></h1> <h1><a class="toc-backref" href="#id26">2&nbsp;&nbsp;&nbsp;Background</a></h1>
<p>There are two basic models for combining C++ and Python:</p> <p>There are two basic models for combining C++ and Python:</p>
<ul class="simple"> <ul class="simple">
<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 <li><a class="reference external" href="http://www.python.org/doc/current/ext/intro.html">extending</a>, in which the end-user launches the Python interpreter
executable and imports Python “extension modules” written in C++. executable and imports Python “extension modules” written in C++.
Think of taking a library written in C++ and giving it a Python Think of taking a library written in C++ and giving it a Python
interface so Python programmers can use it. From Python, these interface so Python programmers can use it. From Python, these
modules look just like regular Python modules.</li> 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 <li><a class="reference external" href="http://www.python.org/doc/current/ext/embedding.html">embedding</a>, in which the end-user launches a program written
in C++ that in turn invokes the Python interpreter as a library in C++ that in turn invokes the Python interpreter as a library
subroutine. Think of adding scriptability to an existing subroutine. Think of adding scriptability to an existing
application.</li> application.</li>
</ul> </ul>
<p>The key distinction between extending and embedding is the location <p>The key distinction between extending and embedding is the location
of C++' <tt class="docutils literal"><span class="pre">main()</span></tt> function: in the Python interpreter executable, of the 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 or in some other program, respectively. Note that even when
embedding Python in another program, <a class="reference" href="http://www.python.org/doc/current/ext/extending-with-embedding.html">extension modules are often embedding Python in another program, <a class="reference external" href="http://www.python.org/doc/current/ext/extending-with-embedding.html">extension modules are often
the best way to make C/C++ functionality accessible to Python the best way to make C/C++ functionality accessible to Python
code</a>, so the use of extension modules is really at the heart of code</a>, so the use of extension modules is really at the heart of
both models.</p> both models.</p>
@@ -71,127 +74,281 @@ dynamically-loaded libraries with a single entry point, which means
you can change them without rebuilding either the other extension you can change them without rebuilding either the other extension
modules or the executable containing <tt class="docutils literal"><span class="pre">main()</span></tt>.</p> modules or the executable containing <tt class="docutils literal"><span class="pre">main()</span></tt>.</p>
</div> </div>
<div class="section"> <div class="section" id="no-install-quickstart">
<h1><a class="toc-backref" href="#id22" id="getting-boost-python-binaries" name="getting-boost-python-binaries">3&nbsp;&nbsp;&nbsp;Getting Boost.Python Binaries</a></h1> <span id="quickstart"></span><h1><a class="toc-backref" href="#id27">3&nbsp;&nbsp;&nbsp;No-Install Quickstart</a></h1>
<p>Since Boost.Python is a separately-compiled (as opposed to <p>There is no need to “install Boost” in order to get started using
<a class="reference" href="../../../more/getting_started/windows.html#header-only-libraries">header-only</a>) library, its user relies on the services of a Boost.Python. These instructions use <a class="reference external" href="../../../tools/build/index.html">Boost.Build</a> projects,
Boost.Python library binary.</p>
<div class="section">
<h2><a class="toc-backref" href="#id23" id="no-install-quickstart" name="no-install-quickstart">3.1&nbsp;&nbsp;&nbsp;No-Install Quickstart</a></h2>
<p>If you just want to get started quickly building and testing
Boost.Python extension modules, or embedding Python in an
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,
which will build those binaries as soon as they're needed. Your which will build those binaries as soon as they're needed. Your
first tests may take a little longer while you wait for first tests may take a little longer while you wait for
Boost.Python to build, but doing things this way will save you from Boost.Python to build, but doing things this way will save you from
worrying about build intricacies like which library binaries to use worrying about build intricacies like which library binaries to use
for a specific compiler configuration.</p> for a specific compiler configuration and figuring out the right
compiler options to use yourself.</p>
<!-- .. raw:: html
<div style="width:50%"> -->
<div class="note"> <div class="note">
<p class="first admonition-title">Note</p> <p class="first admonition-title">Note</p>
<p>Of course it's possible to use other build systems to <p>Of course it's possible to use other build systems to
build Boost.Python and its extensions, but they are not build Boost.Python and its extensions, but they are not
officially supported by Boost and <strong>99% of all “I can't build officially supported by Boost. Moreover <strong>99% of all “I can't
Boost.Python” problems come from trying to use another build build Boost.Python” problems come from trying to use another
system</strong>.</p> build system</strong> without first following these instructions.</p>
<p class="last">If you want to use another system anyway, we suggest that you <p>If you want to use another system anyway, we suggest that you
follow these instructions, and then invoke <tt class="docutils literal"><span class="pre">bjam</span></tt> with the follow these instructions, and then invoke <tt class="docutils literal"><span class="pre">bjam</span></tt> with the</p>
<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 <pre class="literal-block">
to a file, so you can see what your build system needs to do.</p> <tt class="docutils literal"><span class="pre">-a</span> <span class="pre">-o</span></tt><em>filename</em>
</pre>
<p class="last">options to dump the build commands it executes to a file, so
you can see what your alternate build system needs to do.</p>
</div>
<!-- .. raw:: html
</div> -->
<div class="section" id="basic-procedure">
<h2><a class="toc-backref" href="#id28">3.1&nbsp;&nbsp;&nbsp;Basic Procedure</a></h2>
<ol class="arabic">
<li><p class="first">Get Boost; see sections 1 and 2 [<a class="reference external" href="../../../more/getting_started/unix-variants.html#get-boost">Unix/Linux</a>, <a class="reference external" href="../../../more/getting_started/windows.html#get-boost">Windows</a>] of the
Boost <a class="reference external" href="../../../more/getting_started/index.html">Getting Started Guide</a>.</p>
</li>
<li><p class="first">Get the <tt class="docutils literal"><span class="pre">bjam</span></tt> build driver. See section 5 [<a class="reference external" href="../../../more/getting_started/unix-variants.html#prepare-to-use-a-boost-library-binary">Unix/Linux</a>,
<a class="reference external" href="../../../more/getting_started/windows.html#prepare-to-use-a-boost-library-binary">Windows</a>] of the Boost <a class="reference external" href="../../../more/getting_started/index.html">Getting Started Guide</a>.</p>
</li>
<li><p class="first">cd into the <tt class="docutils literal"><span class="pre">libs/python/example/quickstart/</span></tt> directory of your
Boost installation, which contains a small example project.</p>
</li>
<li><p class="first">Invoke <tt class="docutils literal"><span class="pre">bjam</span></tt>. Replace the “<tt class="docutils literal"><span class="pre">stage</span></tt>“ argument from the
example invocation from section 5 of the <a class="reference external" href="../../../more/getting_started/index.html">Getting Started
Guide</a> with “<tt class="docutils literal"><span class="pre">test</span></tt>,“ to build all the test targets. Also add
the argument “<tt class="docutils literal"><span class="pre">--verbose-test</span></tt>” to see the output generated by
the tests when they are run.</p>
<p>On Windows, your <tt class="docutils literal"><span class="pre">bjam</span></tt> invocation might look something like:</p>
<pre class="literal-block">
C:\boost_1_34_0\…\quickstart&gt; <strong>bjam toolset=msvc --verbose-test test</strong>
</pre>
<p>and on Unix variants, perhaps,</p>
<pre class="literal-block">
~/boost_1_34_0/…/quickstart$ <strong>bjam toolset=gcc --verbose-test test</strong>
</pre>
</li>
</ol>
<div class="admonition-note-to-windows-users admonition">
<p class="first admonition-title">Note to Windows Users</p>
<p class="last">For the sake of concision, the rest of this guide will use
unix-style forward slashes in pathnames instead of the
backslashes with which you may be more familiar. The forward
slashes should work everywhere except in <a class="reference external" href="../../../more/getting_started/windows.html#command-prompt">Command Prompt</a>
windows, where you should use backslashes.</p>
</div>
<p>If you followed this procedure successfully, you will have built an
extension module called <tt class="docutils literal"><span class="pre">extending</span></tt> and tested it by running a
Python script called <tt class="docutils literal"><span class="pre">test_extending.py</span></tt>. You will also have
built and run a simple application called <tt class="docutils literal"><span class="pre">embedding</span></tt> that embeds
python.</p>
</div>
<div class="section" id="in-case-of-trouble">
<h2><a class="toc-backref" href="#id29">3.2&nbsp;&nbsp;&nbsp;In Case of Trouble</a></h2>
<p>If you're seeing lots of compiler and/or linker error messages,
it's probably because Boost.Build is having trouble finding your
Python installation. 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
<a class="reference internal" href="#configuring-boost-build">Configuring Boost.Build</a> as detailed below.</p>
<p>If you're still having trouble, Someone on one of the following
mailing lists may be able to help:</p>
<ul class="simple">
<li>The <a class="reference external" href="http://www.boost.org/more/mailing_lists.htm#jamboost">Boost.Build mailing list</a> for issues related to Boost.Build</li>
<li>The Python <a class="reference external" href="http://www.boost.org/more/mailing_lists.htm#cplussig">C++ Sig</a> for issues specifically related to Boost.Python</li>
</ul>
</div>
<div class="section" id="in-case-everything-seemed-to-work">
<h2><a class="toc-backref" href="#id30">3.3&nbsp;&nbsp;&nbsp;In Case Everything Seemed to Work</a></h2>
<p>Rejoice! If you're new to Boost.Python, at this point it might be
a good idea to ignore build issues for a while and concentrate on
learning the library by going through the <a class="reference external" href="tutorial/index.html">tutorial</a> and perhaps
some of the <a class="reference external" href="v2/reference.html">reference documentation</a>, trying out what you've
learned about the API by modifying the quickstart project.</p>
</div>
<div class="section" id="modifying-the-example-project">
<h2><a class="toc-backref" href="#id31">3.4&nbsp;&nbsp;&nbsp;Modifying the Example Project</a></h2>
<p>If you're content to keep your extension module forever in one
source file called <a class="reference external" href="../example/quickstart/extending.cpp"><tt class="docutils literal"><span class="pre">extending.cpp</span></tt></a>, inside your Boost
distribution, and import it forever as <tt class="docutils literal"><span class="pre">extending</span></tt>, then you can
stop here. However, it's likely that you will want to make a few
changes. There are a few things you can do without having to learn
<a class="reference external" href="../../../tools/build/index.html">Boost.Build</a> in depth.</p>
<p>The project you just built is specified in two files in the current
directory: <a class="reference external" href="../example/quickstart/boost-build.jam"><tt class="docutils literal"><span class="pre">boost-build.jam</span></tt></a>, which tells <tt class="docutils literal"><span class="pre">bjam</span></tt> where it can
find the interpreted code of the Boost build system, and
<a class="reference external" href="../example/quickstart/Jamroot"><tt class="docutils literal"><span class="pre">Jamroot</span></tt></a>, which describes the targets you just built. These
files are heavily commented, so they should be easy to modify.
Take care, however, to preserve whitespace. Punctuation such as
<tt class="docutils literal"><span class="pre">;</span></tt> will not be recognized as intended by <tt class="docutils literal"><span class="pre">bjam</span></tt> if it is not
surrounded by whitespace.</p>
<div class="section" id="relocate-the-project">
<h3>Relocate the Project</h3>
<p>You'll probably want to copy this project elsewhere so you can
change it without modifying your Boost distribution. To do that,
simply</p>
<ol class="loweralpha simple">
<li>copy the entire <tt class="docutils literal"><span class="pre">libs/python/example/quickstart/</span></tt> directory
into a new directory.</li>
<li>In the new copies of <a class="reference external" href="../example/quickstart/boost-build.jam"><tt class="docutils literal"><span class="pre">boost-build.jam</span></tt></a> and <a class="reference external" href="../example/quickstart/Jamroot"><tt class="docutils literal"><span class="pre">Jamroot</span></tt></a>, locate
the relative path near the top of the file that is clearly
marked by a comment, and edit that path so that it refers to the
same directory your Boost distribution as it referred to when
the file was in its original location in the
<tt class="docutils literal"><span class="pre">libs/python/example/quickstart/</span></tt> directory.</li>
</ol>
<p>For example, if you moved the project from
<tt class="docutils literal"><span class="pre">/home/dave/boost_1_34_0/libs/python/example/quickstart</span></tt> to
<tt class="docutils literal"><span class="pre">/home/dave/my-project</span></tt>, you could change the first path in
<a class="reference external" href="../example/quickstart/boost-build.jam"><tt class="docutils literal"><span class="pre">boost-build.jam</span></tt></a> from</p>
<pre class="literal-block">
<strong>../../../..</strong>/tools/build/v2
</pre>
<p>to</p>
<pre class="literal-block">
<strong>/home/dave/boost_1_34_0</strong>/tools/build/v2
</pre>
<p>and change the first path in <a class="reference external" href="../example/quickstart/Jamroot"><tt class="docutils literal"><span class="pre">Jamroot</span></tt></a> from</p>
<pre class="literal-block">
<strong>../../../..</strong>
</pre>
<p>to</p>
<pre class="literal-block">
<strong>/home/dave/boost_1_34_0</strong>
</pre>
</div>
<div class="section" id="add-new-or-change-names-of-existing-source-files">
<h3>Add New or Change Names of Existing Source Files</h3>
<p>The names of additional source files involved in building your
extension module or embedding application can be listed in
<a class="reference external" href="../example/quickstart/Jamroot"><tt class="docutils literal"><span class="pre">Jamroot</span></tt></a> right alongside <tt class="docutils literal"><span class="pre">extending.cpp</span></tt> or <tt class="docutils literal"><span class="pre">embedding.cpp</span></tt>
respectively. Just be sure to leave whitespace around each
filename:</p>
<pre class="literal-block">
… file1.cpp file2.cpp file3.cpp …
</pre>
<p>Naturally, if you want to change the name of a source file you can
tell Boost.Build about it by editing the name in <a class="reference external" href="../example/quickstart/Jamroot"><tt class="docutils literal"><span class="pre">Jamroot</span></tt></a>.</p>
</div>
<div class="section" id="change-the-name-of-your-extension-module">
<h3>Change the Name of your Extension Module</h3>
<p>The name of the extension module is determined by two things:</p>
<ol class="arabic simple">
<li>the name in <a class="reference external" href="../example/quickstart/Jamroot"><tt class="docutils literal"><span class="pre">Jamroot</span></tt></a> immediately following <tt class="docutils literal"><span class="pre">python-extension</span></tt>, and</li>
<li>the name passed to <tt class="docutils literal"><span class="pre">BOOST_PYTHON_MODULE</span></tt> in <a class="reference external" href="../example/quickstart/extending.cpp"><tt class="docutils literal"><span class="pre">extending.cpp</span></tt></a>.</li>
</ol>
<p>To change the name of the extension module from <tt class="docutils literal"><span class="pre">extending</span></tt> to
<tt class="docutils literal"><span class="pre">hello</span></tt>, you'd edit <a class="reference external" href="../example/quickstart/Jamroot"><tt class="docutils literal"><span class="pre">Jamroot</span></tt></a>, changing</p>
<pre class="literal-block">
python-extension <strong>extending</strong> : extending.cpp ;
</pre>
<p>to</p>
<pre class="literal-block">
python-extension <strong>hello</strong> : extending.cpp ;
</pre>
<p>and you'd edit extending.cpp, changing</p>
<pre class="literal-block">
BOOST_PYTHON_MODULE(<strong>extending</strong>)
</pre>
<p>to</p>
<pre class="literal-block">
BOOST_PYTHON_MODULE(<strong>hello</strong>)
</pre>
</div> </div>
</div> </div>
<div class="section"> </div>
<h2><a class="toc-backref" href="#id24" id="installing-boost-python-on-your-system" name="installing-boost-python-on-your-system">3.2&nbsp;&nbsp;&nbsp;Installing Boost.Python on your System</a></h2> <div class="section" id="installing-boost-python-on-your-system">
<p>If you need a regular, installation of the Boost.Python library <h1><a class="toc-backref" href="#id32">4&nbsp;&nbsp;&nbsp;Installing Boost.Python on your System</a></h1>
binaries on your system, the Boost <a class="reference" href="../../../more/getting_started/index.html">Getting Started Guide</a> will <p>Since Boost.Python is a separately-compiled (as opposed to
walk you through the steps of installing one. If building binaries <a class="reference external" href="../../../more/getting_started/windows.html#header-only-libraries">header-only</a>) library, its user relies on the services of a
Boost.Python library binary.</p>
<p>If you need a regular installation of the Boost.Python library
binaries on your system, the Boost <a class="reference external" href="../../../more/getting_started/index.html">Getting Started Guide</a> will
walk you through the steps of creating one. If building binaries
from source, you might want to supply the <tt class="docutils literal"><span class="pre">--with-python</span></tt> from source, you might want to supply the <tt class="docutils literal"><span class="pre">--with-python</span></tt>
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 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
to <tt class="docutils literal"><span class="pre">configure</span></tt>), so only the Boost.Python binary will be built, 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> rather than all the Boost binaries.</p>
</div> </div>
</div> <div class="section" id="configuring-boost-build">
<div class="section"> <h1><a class="toc-backref" href="#id33">5&nbsp;&nbsp;&nbsp;Configuring Boost.Build</a></h1>
<h1><a class="toc-backref" href="#id25" id="configuring-boost-build" name="configuring-boost-build">4&nbsp;&nbsp;&nbsp;Configuring Boost.Build</a></h1> <p>As described in the <a class="reference external" href="http://www.boost.orgdoc/html/bbv2/advanced.html#bbv2.advanced.configuration">Boost.Build reference manual</a>, a file called
<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 <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> in your home directory<a class="footnote-reference" href="#home-dir" id="id11"><sup>6</sup></a> is used to
<tt class="docutils literal"><span class="pre">user-config.jam</span></tt> in your home specify the tools and libraries available to the build system. You
directory<a class="footnote-reference" href="#home-dir" id="id5" name="id5"><sup>7</sup></a> is used to may need to create or edit <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> to tell Boost.Build
describe the build resources available to the build system. You'll how to invoke Python, <tt class="docutils literal"><span class="pre">#include</span></tt> its headers, and link with its
need to tell it about your Python installation.</p> libraries.</p>
<div class="admonition-users-of-unix-variant-oses admonition"> <div class="admonition-users-of-unix-variant-oses admonition">
<p class="first admonition-title">Users of Unix-Variant OSes</p> <p class="first admonition-title">Users of Unix-Variant OSes</p>
<p class="last">If you are using a unix-variant OS and you ran Boost's <p class="last">If you are using a unix-variant OS and you ran Boost's
<tt class="docutils literal"><span class="pre">configure</span></tt> script, it may have generated a <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 <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> for you.<a class="footnote-reference" href="#overwrite" id="id13"><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
were built, your <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> file is probably already were built, your <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> file is probably already
correct.</p> correct.</p>
</div> </div>
<p>If you have a fairly “standard” python installation for your <p>If you have one fairly “standard” python installation for your
platform, there's very little you need to do to describe it. platform, you might not need to do anything special to describe it. If
Simply having</p> you haven't configured python in <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> (and you don't
specify <tt class="docutils literal"><span class="pre">--without-python</span></tt> on the Boost.Build command line),
Boost.Build will automatically execute the equivalent of</p>
<pre class="literal-block"> <pre class="literal-block">
import toolset : using ; import toolset : using ;
using python ; using python ;
</pre> </pre>
<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> <p>which automatically looks for Python in the most likely places.
should be enough.<a class="footnote-reference" href="#user-config-jam" id="id9" name="id9"><sup>6</sup></a> For more complicated setups, However, that only happens when using the Boost.Python project file
see <a class="reference" href="#advanced-configuration">Advanced Configuration</a>.</p> (e.g. when referred to by another project as in the <a class="reference internal" href="#quickstart">quickstart</a>
<div class="note"> method). If instead you are linking against separately-compiled
<p class="first admonition-title">Note</p> Boost.Python binaries, you should set up a <tt class="docutils literal"><span class="pre">user-config.jam</span></tt> file
<p class="last">You might want to pass the <tt class="docutils literal"><span class="pre">--debug-configuration</span></tt> with at least the minimal incantation above.</p>
option to <tt class="docutils literal"><span class="pre">bjam</span></tt> the first few times you invoke it, to make <div class="section" id="python-configuration-parameters">
sure that Boost.Build is correctly locating all the parts of <h2><a class="toc-backref" href="#id34">5.1&nbsp;&nbsp;&nbsp;Python Configuration Parameters</a></h2>
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&nbsp;&nbsp;&nbsp;Building an Extension Module</a></h1>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id27" id="testing" name="testing">6&nbsp;&nbsp;&nbsp;Testing</a></h1>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id28" id="advanced-configuration" name="advanced-configuration">7&nbsp;&nbsp;&nbsp;Advanced Configuration</a></h1>
<p>If you have several versions of Python installed, or Python is <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 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> 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&nbsp;&nbsp;&nbsp;Python Configuration Parameters</a></h2>
<dl class="docutils"> <dl class="docutils">
<dt>version</dt> <dt>version</dt>
<dd>the version of Python to use. Should be in Major.Minor <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 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 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 versions installed, the version will usually be the only
additional argument required.</dd> configuration argument required.</dd>
<dt>cmd-or-prefix</dt> <dt>cmd-or-prefix</dt>
<dd>preferably, a command that invokes a Python <dd>preferably, a command that invokes a Python interpreter.
interpreter. Alternatively, the installation prefix for Python Alternatively, the installation prefix for Python libraries and
libraries and header files. Use the alternative formulation if header files. Only use the alternative formulation if there is
there is no appropriate Python executable available.</dd> no appropriate Python executable available.</dd>
<dt>includes</dt> <dt>includes</dt>
<dd>the <tt class="docutils literal"><span class="pre">#include</span></tt> path for Python headers.</dd> <dd>the <tt class="docutils literal"><span class="pre">#include</span></tt> paths for Python headers. Normally the correct
path(s) will be automatically deduced from <tt class="docutils literal"><span class="pre">version</span></tt> and/or
<tt class="docutils literal"><span class="pre">cmd-or-prefix</span></tt>.</dd>
<dt>libraries</dt> <dt>libraries</dt>
<dd>the path to Python library binaries. On MacOS/Darwin, <dd>the path to Python library binaries. On MacOS/Darwin,
you can also pass the path of the Python framework.</dd> you can also pass the path of the Python framework. Normally the
correct path(s) will be automatically deduced from <tt class="docutils literal"><span class="pre">version</span></tt>
and/or <tt class="docutils literal"><span class="pre">cmd-or-prefix</span></tt>.</dd>
<dt>condition</dt> <dt>condition</dt>
<dd>if specified, should be a set of Boost.Build <dd>if specified, should be a set of Boost.Build
properties that are matched against the build configuration when properties that are matched against the build configuration when
Boost.Build selects a Python configuration to use.</dd> Boost.Build selects a Python configuration to use. See examples
below for details.</dd>
<dt>extension-suffix</dt> <dt>extension-suffix</dt>
<dd>A string to append to the name of extension <dd>A string to append to the name of extension
modules before the true filename extension. You almost certainly modules before the true filename extension. You almost certainly
don't need to use this. Usually this suffix is only used when don't need to use this. Usually this suffix is only used when
targeting a Windows debug build of Python, and will be set targeting a Windows debug build of Python, and will be set
automatically for you based on the value of the automatically for you based on the value of the
<tt class="docutils literal"><span class="pre">&lt;python-debugging&gt;</span></tt> feature. However, at least one Linux <a class="reference internal" href="#python-debugging"><tt class="docutils literal"><span class="pre">&lt;python-debugging&gt;</span></tt></a> feature. However, at least one Linux
distribution (Ubuntu Feisty Fawn) has a specially configured 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> <a class="reference external" href="https://wiki.ubuntu.com/PyDbgBuilds">python-dbg</a> package that claims to use such a suffix.</dd>
</dl> </dl>
</div> </div>
<div class="section"> <div class="section" id="examples">
<h2><a class="toc-backref" href="#id30" id="examples" name="examples">7.2&nbsp;&nbsp;&nbsp;Examples</a></h2> <h2><a class="toc-backref" href="#id35">5.2&nbsp;&nbsp;&nbsp;Examples</a></h2>
<p>Note that in the examples below, case and <em>especially whitespace</em> are <p>Note that in the examples below, case and <em>especially whitespace</em> are
significant.</p> significant.</p>
<ul> <ul>
@@ -229,8 +386,19 @@ using python
; ;
</pre> </pre>
</li> </li>
<li><p class="first">If you have downloaded the Python sources and built both the
normal and the “<a class="reference internal" href="#id19">python debugging</a>” builds from source on
Windows, you might see:</p>
<pre class="literal-block">
using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python ;
using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python_d
: # includes
: # libs
: &lt;python-debugging&gt;on ;
</pre>
</li>
<li><p class="first">You can set up your user-config.jam so a bjam built under Windows <li><p class="first">You can set up your user-config.jam so a bjam built under Windows
can build/test both Windows and <a class="reference" href="http://cygwin.com">Cygwin</a> python extensions. Just pass can build/test both Windows and <a class="reference external" href="http://cygwin.com">Cygwin</a> python extensions. Just pass
<tt class="docutils literal"><span class="pre">&lt;target-os&gt;cygwin</span></tt> in the <tt class="docutils literal"><span class="pre">condition</span></tt> parameter <tt class="docutils literal"><span class="pre">&lt;target-os&gt;cygwin</span></tt> in the <tt class="docutils literal"><span class="pre">condition</span></tt> parameter
for the cygwin python installation:</p> for the cygwin python installation:</p>
<pre class="literal-block"> <pre class="literal-block">
@@ -241,16 +409,16 @@ using python ;
using python : : c:\\cygwin\\bin\\python2.5 : : : &lt;target-os&gt;cygwin ; using python : : c:\\cygwin\\bin\\python2.5 : : : &lt;target-os&gt;cygwin ;
</pre> </pre>
<p>when you put target-os=cygwin in your build request, it should build <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> with the cygwin version of python:<a class="footnote-reference" href="#flavor" id="id15"><sup>5</sup></a></p>
<blockquote> <blockquote>
<p>bjam target-os=cygwin toolset=gcc</p> <p>bjam target-os=cygwin toolset=gcc</p>
</blockquote> </blockquote>
<p>This is supposed to work the other way, too (targeting windows <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 python with a <a class="reference external" 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 Boost.Build's toolsets for building that way is broken at the
time of this writing.</p> time of this writing.</p>
</li> </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 <li><p class="first">Note that because of <a class="reference external" 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 alternatives</a>, you might have be very explicit in your build
requests. For example, given:</p> requests. For example, given:</p>
<pre class="literal-block"> <pre class="literal-block">
@@ -269,35 +437,35 @@ bjam target-os=cygwin/python=2.4
</ul> </ul>
</div> </div>
</div> </div>
<div class="section"> <div class="section" id="choosing-a-boost-python-library-binary">
<h1><a class="toc-backref" href="#id31" id="choosing-a-boost-python-library-binary" name="choosing-a-boost-python-library-binary">8&nbsp;&nbsp;&nbsp;Choosing a Boost.Python Library Binary</a></h1> <h1><a class="toc-backref" href="#id36">6&nbsp;&nbsp;&nbsp;Choosing a Boost.Python Library Binary</a></h1>
<p>If—instead of letting Boost.Build construct and link withthe right <p>If—instead of letting Boost.Build construct and link with the right
libraries automatically—you choose to use a pre-built Boost.Python libraries automatically—you choose to use a pre-built Boost.Python
library, you'll need to think about which one to link with. The library, you'll need to think about which one to link with. The
Boost.Python binary comes in both static and dynamic flavors. Take 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> care to choose the right flavor for your application.<a class="footnote-reference" href="#naming" id="id17"><sup>2</sup></a></p>
<div class="section"> <div class="section" id="the-dynamic-binary">
<h2><a class="toc-backref" href="#id32" id="the-dynamic-binary" name="the-dynamic-binary">8.1&nbsp;&nbsp;&nbsp;The Dynamic Binary</a></h2> <h2><a class="toc-backref" href="#id37">6.1&nbsp;&nbsp;&nbsp;The Dynamic Binary</a></h2>
<p>The dynamic library is the safest and most-versatile choice:</p> <p>The dynamic library is the safest and most-versatile choice:</p>
<ul class="simple"> <ul class="simple">
<li>A single copy of the library code is used by all extension <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> modules built with a given toolset.<a class="footnote-reference" href="#toolset-specific" id="id18"><sup>3</sup></a></li>
<li>The library contains a type conversion registry. Because one <li>The library contains a type conversion registry. Because one
registry is shared among all extension modules, instances of a registry is shared among all extension modules, instances of a
class exposed to Python in one dynamically-loaded extension class exposed to Python in one dynamically-loaded extension
module can be passed to functions exposed in another such module.</li> module can be passed to functions exposed in another such module.</li>
</ul> </ul>
</div> </div>
<div class="section"> <div class="section" id="the-static-binary">
<h2><a class="toc-backref" href="#id33" id="the-static-binary" name="the-static-binary">8.2&nbsp;&nbsp;&nbsp;The Static Binary</a></h2> <h2><a class="toc-backref" href="#id38">6.2&nbsp;&nbsp;&nbsp;The Static Binary</a></h2>
<p>It might be appropriate to use the static Boost.Python library in <p>It might be appropriate to use the static Boost.Python library in
any of the following cases:</p> any of the following cases:</p>
<ul class="simple"> <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 <li>You are <a class="reference external" 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 dynamically-loaded extension module don't need to be used by any
other Boost.Python extension modules, and you don't care if the other Boost.Python extension modules, and you don't care if the
core library code is duplicated among them.</li> 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 <a class="reference external" 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, <li>You are targeting a Unix variant OS other than MacOS or AIX,
where the dynamically-loaded extension modules can “see” the where the dynamically-loaded extension modules can “see” the
Boost.Python library symbols that are part of the executable.</li> Boost.Python library symbols that are part of the executable.</li>
@@ -311,22 +479,73 @@ modules (and vice-versa).</li>
</ul> </ul>
</div> </div>
</div> </div>
<div class="section"> <div class="section" id="include-issues">
<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&nbsp;&nbsp;&nbsp;Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users</a></h1> <h1><a class="toc-backref" href="#id39">7&nbsp;&nbsp;&nbsp;<tt class="docutils literal"><span class="pre">#include</span></tt> Issues</a></h1>
<ol class="arabic simple">
<li>If you should ever have occasion to <tt class="docutils literal"><span class="pre">#include</span> <span class="pre">&quot;python.h&quot;</span></tt>
directly in a translation unit of a program using Boost.Python,
use <tt class="docutils literal"><span class="pre">#include</span> <span class="pre">&quot;boost/python/detail/wrap_python.hpp&quot;</span></tt> instead.
It handles several issues necessary for use with Boost.Python,
one of which is mentioned in the next section.</li>
<li>Be sure not to <tt class="docutils literal"><span class="pre">#include</span></tt> any system headers before
<tt class="docutils literal"><span class="pre">wrap_python.hpp</span></tt>. This restriction is actually imposed by
Python, or more properly, by Python's interaction with your
operating system. See
<a class="reference external" href="http://docs.python.org/ext/simpleExample.html">http://docs.python.org/ext/simpleExample.html</a> for details.</li>
</ol>
</div>
<div class="section" id="python-debugging-builds">
<span id="id19"></span><span id="python-debugging"></span><h1><a class="toc-backref" href="#id40">8&nbsp;&nbsp;&nbsp;Python Debugging Builds</a></h1>
<p>Python can be built in a special “python debugging” configuration
that adds extra checks and instrumentation that can be very useful
for developers of extension modules. The data structures used by
the debugging configuration contain additional members, so <strong>a
Python executable built with python debugging enabled cannot be
used with an extension module or library compiled without it, and
vice-versa.</strong></p>
<p>Since pre-built “python debugging” versions of the Python
executable and libraries are not supplied with most distributions
of Python,<a class="footnote-reference" href="#get-debug-build" id="id20"><sup>7</sup></a> and we didn't want to force our users
to build them, Boost.Build does not automatically enable python
debugging in its <tt class="docutils literal"><span class="pre">debug</span></tt> build variant (which is the default).
Instead there is a special build property called
<tt class="docutils literal"><span class="pre">python-debugging</span></tt> that, when used as a build property, will
define the right preprocessor symbols and select the right
libraries to link with.</p>
<p>On unix-variant platforms, the debugging versions of Python's data
structures will only be used if the symbol <tt class="docutils literal"><span class="pre">Py_DEBUG</span></tt> is defined.
On many windows compilers, when extension modules are built with
the preprocessor symbol <tt class="docutils literal"><span class="pre">_DEBUG</span></tt>, Python defaults to force
linking with a special debugging version of the Python DLL. Since
that symbol is very commonly used even when Python is not present,
Boost.Python temporarily undefines _DEBUG when Python.h
is #included from <tt class="docutils literal"><span class="pre">boost/python/detail/wrap_python.hpp</span></tt> - unless
<tt class="docutils literal"><span class="pre">BOOST_DEBUG_PYTHON</span></tt> is defined. The upshot is that if you want
“python debugging”and you aren't using Boost.Build, you should make
sure <tt class="docutils literal"><span class="pre">BOOST_DEBUG_PYTHON</span></tt> is defined, or python debugging will be
suppressed.</p>
</div>
<div class="section" id="testing-boost-python">
<h1><a class="toc-backref" href="#id41">9&nbsp;&nbsp;&nbsp;Testing Boost.Python</a></h1>
<p>To run the full test suite for Boost.Python, invoke <tt class="docutils literal"><span class="pre">bjam</span></tt> in the
<tt class="docutils literal"><span class="pre">libs/python/test</span></tt> subdirectory of your Boost distribution.</p>
</div>
<div class="section" id="notes-for-mingw-and-cygwin-with-mno-cygwin-gcc-users">
<h1><a class="toc-backref" href="#id42">10&nbsp;&nbsp;&nbsp;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 <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 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 create a MinGW-compatible version of the Python library; the one
shipped with Python will only work with a Microsoft-compatible shipped with Python will only work with a Microsoft-compatible
linker. Follow the instructions in the “Non-Microsoft” section of 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 the “Building Extensions: Tips And Tricks” chapter in <a class="reference external" 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> 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 corresponds to the major and minor version numbers of your Python
installation.</p> installation.</p>
<hr class="docutils" /> <hr class="docutils" />
<table class="docutils footnote" frame="void" id="id16" rules="none"> <table class="docutils footnote" frame="void" id="id22" rules="none">
<colgroup><col class="label" /><col /></colgroup> <colgroup><col class="label" /><col /></colgroup>
<tbody valign="top"> <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 <tr><td class="label"><a class="fn-backref" href="#id2">[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 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 anything to break compatibility, this release of Boost.Python
may not have been tested with versions of Python earlier than may not have been tested with versions of Python earlier than
@@ -337,24 +556,21 @@ supported.</td></tr>
<table class="docutils footnote" frame="void" id="naming" rules="none"> <table class="docutils footnote" frame="void" id="naming" rules="none">
<colgroup><col class="label" /><col /></colgroup> <colgroup><col class="label" /><col /></colgroup>
<tbody valign="top"> <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 <tr><td class="label"><a class="fn-backref" href="#id17">[2]</a></td><td><p class="first">Information about how to identify the
static and dynamic builds of Boost.Python:</p> static and dynamic builds of Boost.Python:</p>
<ul class="simple"> <ul class="simple">
<li><a class="reference" href="../../../more/getting_started/windows.html#library-naming">on Windows</a></li> <li><a class="reference external" 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> <li><a class="reference external" href="../../../more/getting_started/unix-variants.html#library-naming">on Unix variants</a></li>
</ul> </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> </td></tr>
</tbody> </tbody>
</table> </table>
<table class="docutils footnote" frame="void" id="toolset-specific" rules="none"> <table class="docutils footnote" frame="void" id="toolset-specific" rules="none">
<colgroup><col class="label" /><col /></colgroup> <colgroup><col class="label" /><col /></colgroup>
<tbody valign="top"> <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 <tr><td class="label"><a class="fn-backref" href="#id18">[3]</a></td><td>Because of the way most *nix platforms
share symbols among dynamically-loaded objects, I'm not share symbols among dynamically-loaded objects, I'm not certain
certainextension modules built with different compiler toolsets that extension modules built with different compiler toolsets
will always use different copies of the Boost.Python library will always use different copies of the Boost.Python library
when loaded into the same Python instance. Not using different when loaded into the same Python instance. Not using different
libraries could be a good thing if the compilers have compatible libraries could be a good thing if the compilers have compatible
@@ -369,7 +585,7 @@ happens.</td></tr>
<table class="docutils footnote" frame="void" id="overwrite" rules="none"> <table class="docutils footnote" frame="void" id="overwrite" rules="none">
<colgroup><col class="label" /><col /></colgroup> <colgroup><col class="label" /><col /></colgroup>
<tbody valign="top"> <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 <tr><td class="label"><a class="fn-backref" href="#id13">[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 <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> (if any) after making a backup of the old version.</td></tr>
</tbody> </tbody>
@@ -377,28 +593,33 @@ happens.</td></tr>
<table class="docutils footnote" frame="void" id="flavor" rules="none"> <table class="docutils footnote" frame="void" id="flavor" rules="none">
<colgroup><col class="label" /><col /></colgroup> <colgroup><col class="label" /><col /></colgroup>
<tbody valign="top"> <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">&lt;target-os&gt;cygwin</span></tt> feature is <tr><td class="label"><a class="fn-backref" href="#id15">[5]</a></td><td>Note that the <tt class="docutils literal"><span class="pre">&lt;target-os&gt;cygwin</span></tt> feature is
different from the <tt class="docutils literal"><span class="pre">&lt;flavor&gt;cygwin</span></tt> subfeature of the <tt class="docutils literal"><span class="pre">gcc</span></tt> different from the <tt class="docutils literal"><span class="pre">&lt;flavor&gt;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 toolset, and you might need handle both explicitly if you also
have a MinGW GCC installed.</td></tr> have a MinGW GCC installed.</td></tr>
</tbody> </tbody>
</table> </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"> <table class="docutils footnote" frame="void" id="home-dir" rules="none">
<colgroup><col class="label" /><col /></colgroup> <colgroup><col class="label" /><col /></colgroup>
<tbody valign="top"> <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 <tr><td class="label"><a class="fn-backref" href="#id11">[6]</a></td><td><p class="first">Windows users, your home directory can be
found by typing:</p> found by typing:</p>
<pre class="literal-block"> <pre class="literal-block">
ECHO %HOMEDRIVE%%HOMEPATH% ECHO %HOMEDRIVE%%HOMEPATH%
</pre> </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> <p class="last">into a <a class="reference external" href="../../../more/getting_started/windows.html#command-prompt">command prompt</a> window.</p>
</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="get-debug-build" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id20">[7]</a></td><td>On Unix and similar platforms, a debugging
python and associated libraries are built by adding
<tt class="docutils literal"><span class="pre">--with-pydebug</span></tt> when configuring the Python build. On
Windows, the debugging version of Python is generated by
the &quot;Win32 Debug&quot; target of the Visual Studio project in the
PCBuild subdirectory of a full Python source code distribution.
</td></tr> </td></tr>
</tbody> </tbody>
</table> </table>
@@ -406,9 +627,9 @@ ECHO %HOMEDRIVE%%HOMEPATH%
</div> </div>
<div class="footer"> <div class="footer">
<hr class="footer" /> <hr class="footer" />
<a class="reference" href="./building.rst">View document source</a>. <a class="reference external" href="./building.rst">View document source</a>.
Generated on: 2007-04-05 20:04 UTC. Generated on: 2007-07-02 13:46 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. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
</div> </div>
</body> </body>

View File

@@ -7,11 +7,11 @@
|(logo)|__ Boost.Python Build and Test HOWTO |(logo)|__ Boost.Python Build and Test HOWTO
============================================== ==============================================
.. |(logo)| image:: ../boost.png .. |(logo)| image:: ../../../boost.png
:alt: Boost C++ Libraries: :alt: Boost C++ Libraries:
:class: boost-logo :class: boost-logo
__ ../index.htm __ ../index.html
.. section-numbering:: .. section-numbering::
@@ -31,36 +31,6 @@ Boost.Python requires `Python 2.2`_ [#2.2]_ *or* |newer|__.
.. _Python 2.2: http://www.python.org/2.2 .. _Python 2.2: http://www.python.org/2.2
__ http://www.python.org __ 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 Background
========== ==========
@@ -81,7 +51,7 @@ There are two basic models for combining C++ and Python:
.. _embedding: http://www.python.org/doc/current/ext/embedding.html .. _embedding: http://www.python.org/doc/current/ext/embedding.html
The key distinction between extending and embedding is the location The key distinction between extending and embedding is the location
of C++' ``main()`` function: in the Python interpreter executable, of the C++ ``main()`` function: in the Python interpreter executable,
or in some other program, respectively. Note that even when or in some other program, respectively. Note that even when
embedding Python in another program, `extension modules are often embedding Python in another program, `extension modules are often
the best way to make C/C++ functionality accessible to Python the best way to make C/C++ functionality accessible to Python
@@ -95,8 +65,254 @@ dynamically-loaded libraries with a single entry point, which means
you can change them without rebuilding either the other extension you can change them without rebuilding either the other extension
modules or the executable containing ``main()``. modules or the executable containing ``main()``.
Getting Boost.Python Binaries .. _quickstart:
=============================
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 and figuring out the right
compiler options to use yourself.
.. .. raw:: html
<div style="width:50%">
.. 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** without first following these instructions.
If you want to use another system anyway, we suggest that you
follow these instructions, and then invoke ``bjam`` with the
.. parsed-literal::
``-a -o``\ *filename*
options to dump the build commands it executes to a file, so
you can see what your alternate build system needs to do.
.. .. raw:: html
</div>
.. _Boost.Build: ../../../tools/build/index.html
Basic Procedure
---------------
1. Get Boost; see sections 1 and 2 [`Unix/Linux`__, `Windows`__\ ] of the
Boost `Getting Started Guide`_.
__ ../../../more/getting_started/unix-variants.html#get-boost
__ ../../../more/getting_started/windows.html#get-boost
2. Get the ``bjam`` build driver. See section 5 [`Unix/Linux`__,
`Windows`__\ ] of the Boost `Getting Started Guide`_.
__ ../../../more/getting_started/unix-variants.html#prepare-to-use-a-boost-library-binary
__ ../../../more/getting_started/windows.html#prepare-to-use-a-boost-library-binary
3. cd into the ``libs/python/example/quickstart/`` directory of your
Boost installation, which contains a small example project.
4. Invoke ``bjam``. Replace the “\ ``stage``\ “ argument from the
example invocation from section 5 of the `Getting Started
Guide`_ with “\ ``test``\ ,“ to build all the test targets. Also add
the argument “\ ``--verbose-test``\ ” to see the output generated by
the tests when they are run.
On Windows, your ``bjam`` invocation might look something like:
.. parsed-literal::
C:\\boost_1_34_0\\…\\quickstart> **bjam toolset=msvc --verbose-test test**
and on Unix variants, perhaps,
.. parsed-literal::
~/boost_1_34_0/…/quickstart$ **bjam toolset=gcc --verbose-test test**
.. Admonition:: Note to Windows Users
For the sake of concision, the rest of this guide will use
unix-style forward slashes in pathnames instead of the
backslashes with which you may be more familiar. The forward
slashes should work everywhere except in `Command Prompt`_
windows, where you should use backslashes.
.. _Command Prompt: ../../../more/getting_started/windows.html#command-prompt
If you followed this procedure successfully, you will have built an
extension module called ``extending`` and tested it by running a
Python script called ``test_extending.py``. You will also have
built and run a simple application called ``embedding`` that embeds
python.
.. _Getting Started Guide: ../../../more/getting_started/index.html
In Case of Trouble
------------------
If you're seeing lots of compiler and/or linker error messages,
it's probably because Boost.Build is having trouble finding your
Python installation. 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
`Configuring Boost.Build`_ as detailed below.
If you're still having trouble, Someone on one of the following
mailing lists may be able to help:
* The `Boost.Build mailing list`__ for issues related to Boost.Build
* The Python `C++ Sig`__ for issues specifically related to Boost.Python
__ http://www.boost.org/more/mailing_lists.htm#jamboost
__ http://www.boost.org/more/mailing_lists.htm#cplussig
In Case Everything Seemed to Work
---------------------------------
Rejoice! If you're new to Boost.Python, at this point it might be
a good idea to ignore build issues for a while and concentrate on
learning the library by going through the tutorial_ and perhaps
some of the `reference documentation`_, trying out what you've
learned about the API by modifying the quickstart project.
.. _reference documentation: v2/reference.html
.. _tutorial: tutorial/index.html
Modifying the Example Project
-----------------------------
If you're content to keep your extension module forever in one
source file called |extending.cpp|_, inside your Boost
distribution, and import it forever as ``extending``, then you can
stop here. However, it's likely that you will want to make a few
changes. There are a few things you can do without having to learn
Boost.Build_ in depth.
The project you just built is specified in two files in the current
directory: |boost-build.jam|_, which tells ``bjam`` where it can
find the interpreted code of the Boost build system, and
|Jamroot|_, which describes the targets you just built. These
files are heavily commented, so they should be easy to modify.
Take care, however, to preserve whitespace. Punctuation such as
``;`` will not be recognized as intended by ``bjam`` if it is not
surrounded by whitespace.
.. |boost-build.jam| replace:: ``boost-build.jam``
.. _boost-build.jam: ../example/quickstart/boost-build.jam
.. |Jamroot| replace:: ``Jamroot``
.. _Jamroot: ../example/quickstart/Jamroot
.. |extending.cpp| replace:: ``extending.cpp``
.. _extending.cpp: ../example/quickstart/extending.cpp
Relocate the Project
....................
You'll probably want to copy this project elsewhere so you can
change it without modifying your Boost distribution. To do that,
simply
a. copy the entire ``libs/python/example/quickstart/`` directory
into a new directory.
b. In the new copies of |boost-build.jam|_ and |Jamroot|_, locate
the relative path near the top of the file that is clearly
marked by a comment, and edit that path so that it refers to the
same directory your Boost distribution as it referred to when
the file was in its original location in the
``libs/python/example/quickstart/`` directory.
For example, if you moved the project from
``/home/dave/boost_1_34_0/libs/python/example/quickstart`` to
``/home/dave/my-project``, you could change the first path in
|boost-build.jam|_ from
.. parsed-literal::
**../../../..**\ /tools/build/v2
to
.. parsed-literal::
**/home/dave/boost_1_34_0**\ /tools/build/v2
and change the first path in |Jamroot|_ from
.. parsed-literal::
**../../../..**
to
.. parsed-literal::
**/home/dave/boost_1_34_0**
Add New or Change Names of Existing Source Files
................................................
The names of additional source files involved in building your
extension module or embedding application can be listed in
|Jamroot|_ right alongside ``extending.cpp`` or ``embedding.cpp``
respectively. Just be sure to leave whitespace around each
filename::
… file1.cpp file2.cpp file3.cpp …
Naturally, if you want to change the name of a source file you can
tell Boost.Build about it by editing the name in |Jamroot|_.
Change the Name of your Extension Module
........................................
The name of the extension module is determined by two things:
1. the name in |Jamroot|_ immediately following ``python-extension``, and
2. the name passed to ``BOOST_PYTHON_MODULE`` in |extending.cpp|_.
To change the name of the extension module from ``extending`` to
``hello``, you'd edit |Jamroot|_, changing
.. parsed-literal::
python-extension **extending** : extending.cpp ;
to
.. parsed-literal::
python-extension **hello** : extending.cpp ;
and you'd edit extending.cpp, changing
.. parsed-literal::
BOOST_PYTHON_MODULE(\ **extending**\ )
to
.. parsed-literal::
BOOST_PYTHON_MODULE(\ **hello**\ )
Installing Boost.Python on your System
======================================
Since Boost.Python is a separately-compiled (as opposed to Since Boost.Python is a separately-compiled (as opposed to
`header-only`_) library, its user relies on the services of a `header-only`_) library, its user relies on the services of a
@@ -104,25 +320,24 @@ Boost.Python library binary.
.. _header-only: ../../../more/getting_started/windows.html#header-only-libraries .. _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
--------------------------------------
If you need a regular, installation of the Boost.Python library
binaries on your system, the Boost `Getting Started Guide`_ will binaries on your system, the Boost `Getting Started Guide`_ will
walk you through the steps of installing one. If building binaries walk you through the steps of creating one. If building binaries
from source, you might want to supply the ``--with-python`` from source, you might want to supply the ``--with-python``
argument to ``bjam`` (or the ``--with-libraries=python`` argument argument to ``bjam`` (or the ``--with-libraries=python`` argument
to ``configure``), so only the Boost.Python binary will be built, to ``configure``), so only the Boost.Python binary will be built,
rather than all the Boost binaries. rather than all the Boost binaries.
Configuring Boost.Build Configuring Boost.Build
======================= =======================
As described in the `Boost.Build reference manual`__, a file called As described in the `Boost.Build reference manual`__, a file called
``user-config.jam`` in your home ``user-config.jam`` in your home directory [#home-dir]_ is used to
directory [#home-dir]_ is used to specify the tools and libraries available to the build system. You
describe the build resources available to the build system. You'll may need to create or edit ``user-config.jam`` to tell Boost.Build
need to tell it about your Python installation. how to invoke Python, ``#include`` its headers, and link with its
libraries.
__ http://www.boost.orgdoc/html/bbv2/advanced.html#bbv2.advanced.configuration __ http://www.boost.orgdoc/html/bbv2/advanced.html#bbv2.advanced.configuration
@@ -135,66 +350,58 @@ __ http://www.boost.orgdoc/html/bbv2/advanced.html#bbv2.advanced.configuration
were built, your ``user-config.jam`` file is probably already were built, your ``user-config.jam`` file is probably already
correct. correct.
If you have a fairly “standard” python installation for your If you have one fairly “standard” python installation for your
platform, there's very little you need to do to describe it. platform, you might not need to do anything special to describe it. If
Simply having :: you haven't configured python in ``user-config.jam`` (and you don't
specify ``--without-python`` on the Boost.Build command line),
Boost.Build will automatically execute the equivalent of ::
import toolset : using ; import toolset : using ;
using python ; using python ;
in a ``user-config.jam`` file in your home directory [#home-dir]_ which automatically looks for Python in the most likely places.
should be enough. [#user-config.jam]_ For more complicated setups, However, that only happens when using the Boost.Python project file
see `Advanced Configuration`_. (e.g. when referred to by another project as in the quickstart_
method). If instead you are linking against separately-compiled
Boost.Python binaries, you should set up a ``user-config.jam`` file
with at least the minimal incantation above.
.. Note:: You might want to pass the ``--debug-configuration`` Python Configuration Parameters
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 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 installed in an unusual way, you may want to supply any or all of
the following optional parameters to ``using python``. the following optional parameters to ``using python``.
Python Configuration Parameters
-------------------------------
version version
the version of Python to use. Should be in Major.Minor the version of Python to use. Should be in Major.Minor
format, for example, ``2.3``. Do not include the subminor format, for example, ``2.3``. Do not include the subminor
version (i.e. *not* ``2.5.1``). If you have multiple Python version (i.e. *not* ``2.5.1``). If you have multiple Python
versions installed, the version will usually be the only versions installed, the version will usually be the only
additional argument required. configuration argument required.
cmd-or-prefix cmd-or-prefix
preferably, a command that invokes a Python preferably, a command that invokes a Python interpreter.
interpreter. Alternatively, the installation prefix for Python Alternatively, the installation prefix for Python libraries and
libraries and header files. Use the alternative formulation if header files. Only use the alternative formulation if there is
there is no appropriate Python executable available. no appropriate Python executable available.
includes includes
the ``#include`` path for Python headers. the ``#include`` paths for Python headers. Normally the correct
path(s) will be automatically deduced from ``version`` and/or
``cmd-or-prefix``.
libraries libraries
the path to Python library binaries. On MacOS/Darwin, the path to Python library binaries. On MacOS/Darwin,
you can also pass the path of the Python framework. you can also pass the path of the Python framework. Normally the
correct path(s) will be automatically deduced from ``version``
and/or ``cmd-or-prefix``.
condition condition
if specified, should be a set of Boost.Build if specified, should be a set of Boost.Build
properties that are matched against the build configuration when properties that are matched against the build configuration when
Boost.Build selects a Python configuration to use. Boost.Build selects a Python configuration to use. See examples
below for details.
extension-suffix extension-suffix
A string to append to the name of extension A string to append to the name of extension
@@ -202,10 +409,12 @@ extension-suffix
don't need to use this. Usually this suffix is only used when don't need to use this. Usually this suffix is only used when
targeting a Windows debug build of Python, and will be set targeting a Windows debug build of Python, and will be set
automatically for you based on the value of the automatically for you based on the value of the
``<python-debugging>`` feature. However, at least one Linux |python-debugging|_ feature. However, at least one Linux
distribution (Ubuntu Feisty Fawn) has a specially configured distribution (Ubuntu Feisty Fawn) has a specially configured
`python-dbg`__ package that claims to use such a suffix. `python-dbg`__ package that claims to use such a suffix.
.. |python-debugging| replace:: ``<python-debugging>``
__ https://wiki.ubuntu.com/PyDbgBuilds __ https://wiki.ubuntu.com/PyDbgBuilds
@@ -247,6 +456,17 @@ significant.
: <toolset>intel # condition : <toolset>intel # condition
; ;
- If you have downloaded the Python sources and built both the
normal and the “\ `python debugging`_\ ” builds from source on
Windows, you might see::
using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python ;
using python : 2.5 : C:\\src\\Python-2.5\\PCBuild\\python_d
: # includes
: # libs
: <python-debugging>on ;
- You can set up your user-config.jam so a bjam built under Windows - 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 can build/test both Windows and Cygwin_ python extensions. Just pass
``<target-os>cygwin`` in the ``condition`` parameter ``<target-os>cygwin`` in the ``condition`` parameter
@@ -290,7 +510,7 @@ __ http://zigzag.cs.msu.su/boost.build/wiki/AlternativeSelection
Choosing a Boost.Python Library Binary Choosing a Boost.Python Library Binary
====================================== ======================================
If—instead of letting Boost.Build construct and link withthe right If—instead of letting Boost.Build construct and link with the right
libraries automatically—you choose to use a pre-built Boost.Python libraries automatically—you choose to use a pre-built Boost.Python
library, you'll need to think about which one to link with. The library, you'll need to think about which one to link with. The
Boost.Python binary comes in both static and dynamic flavors. Take Boost.Python binary comes in both static and dynamic flavors. Take
@@ -332,6 +552,64 @@ any of the following cases:
use the types exposed by your statically-linked extension use the types exposed by your statically-linked extension
modules (and vice-versa). modules (and vice-versa).
``#include`` Issues
===================
1. If you should ever have occasion to ``#include "python.h"``
directly in a translation unit of a program using Boost.Python,
use ``#include "boost/python/detail/wrap_python.hpp"`` instead.
It handles several issues necessary for use with Boost.Python,
one of which is mentioned in the next section.
2. Be sure not to ``#include`` any system headers before
``wrap_python.hpp``. This restriction is actually imposed by
Python, or more properly, by Python's interaction with your
operating system. See
http://docs.python.org/ext/simpleExample.html for details.
.. _python-debugging:
.. _python debugging:
Python Debugging Builds
=======================
Python can be built in a special “python debugging” configuration
that adds extra checks and instrumentation that can be very useful
for developers of extension modules. The data structures used by
the debugging configuration contain additional members, so **a
Python executable built with python debugging enabled cannot be
used with an extension module or library compiled without it, and
vice-versa.**
Since pre-built “python debugging” versions of the Python
executable and libraries are not supplied with most distributions
of Python, [#get-debug-build]_ and we didn't want to force our users
to build them, Boost.Build does not automatically enable python
debugging in its ``debug`` build variant (which is the default).
Instead there is a special build property called
``python-debugging`` that, when used as a build property, will
define the right preprocessor symbols and select the right
libraries to link with.
On unix-variant platforms, the debugging versions of Python's data
structures will only be used if the symbol ``Py_DEBUG`` is defined.
On many windows compilers, when extension modules are built with
the preprocessor symbol ``_DEBUG``, Python defaults to force
linking with a special debugging version of the Python DLL. Since
that symbol is very commonly used even when Python is not present,
Boost.Python temporarily undefines _DEBUG when Python.h
is #included from ``boost/python/detail/wrap_python.hpp`` - unless
``BOOST_DEBUG_PYTHON`` is defined. The upshot is that if you want
“python debugging”and you aren't using Boost.Build, you should make
sure ``BOOST_DEBUG_PYTHON`` is defined, or python debugging will be
suppressed.
Testing Boost.Python
====================
To run the full test suite for Boost.Python, invoke ``bjam`` in the
``libs/python/test`` subdirectory of your Boost distribution.
Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users Notes for MinGW (and Cygwin with -mno-cygwin) GCC Users
======================================================= =======================================================
@@ -365,13 +643,9 @@ __ http://www.python.org/doc/current/inst/index.html
__ ../../../more/getting_started/windows.html#library-naming __ ../../../more/getting_started/windows.html#library-naming
__ ../../../more/getting_started/unix-variants.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 .. [#toolset-specific] Because of the way most \*nix platforms
share symbols among dynamically-loaded objects, I'm not share symbols among dynamically-loaded objects, I'm not certain
certainextension modules built with different compiler toolsets that extension modules built with different compiler toolsets
will always use different copies of the Boost.Python library will always use different copies of the Boost.Python library
when loaded into the same Python instance. Not using different when loaded into the same Python instance. Not using different
libraries could be a good thing if the compilers have compatible libraries could be a good thing if the compilers have compatible
@@ -391,14 +665,16 @@ __ http://www.python.org/doc/current/inst/index.html
toolset, and you might need handle both explicitly if you also toolset, and you might need handle both explicitly if you also
have a MinGW GCC installed. 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 .. [#home-dir] Windows users, your home directory can be
found by typing:: found by typing::
ECHO %HOMEDRIVE%%HOMEPATH% ECHO %HOMEDRIVE%%HOMEPATH%
into a `Windows command prompt`__ into a `command prompt`_ window.
__ ../../../more/getting_started/windows.html#or-build-from-the-command-prompt .. [#get-debug-build] On Unix and similar platforms, a debugging
python and associated libraries are built by adding
``--with-pydebug`` when configuring the Python build. On
Windows, the debugging version of Python is generated by
the "Win32 Debug" target of the Visual Studio project in the
PCBuild subdirectory of a full Python source code distribution.

View File

@@ -147,7 +147,9 @@
<dt><a href="v2/faq.html">Frequently Asked Questions (FAQs)</a></dt> <dt><a href="v2/faq.html">Frequently Asked Questions (FAQs)</a></dt>
<dt><a href="../pyste/index.html">Pyste (Boost.Python code generator)</a></dt> <dt><a href="http://www.language-binding.net/pyplusplus/pyplusplus.html">Py++ Boost.Python code generator</a></dt>
<dt><a href="../pyste/index.html">Pyste Boost.Python code generator (no longer maintained)</a></dt>
<dt><a href="internals.html">Internals Documentation</a></dt> <dt><a href="internals.html">Internals Documentation</a></dt>
@@ -175,7 +177,7 @@
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002-2003.</i></p> Abrahams</a> 2002-2003.</i></p>
</body> </body>
</html> </html>

View File

@@ -32,7 +32,79 @@
<hr> <hr>
<dl class="page-index"> <dl class="page-index">
<dt>Current CVS</dt>
<dt>Current SVN</dt>
<dd>
<ul>
<li>Python 3 support:</li>
<ul>
<li>All the current Boost.Python test cases passed. Extension modules using
Boost.Python expected to support Python 3 smoothly.</li>
<li>Introduced <code>object.contains</code> where <code>x.contains(y)</code>
is equivalent to Python code <code>y in x</code>.
Now <code>dict.has_key</code> is just a wrapper of <code>object.contains</code>.
</li>
<li>When building against Python 3, <code>str.decode</code> will be removed.</li>
<li>When building against Python 3, the original signature of <code>list.sort</code>, which is:
<pre>void sort(object_cref cmpfunc);</pre>
will change to:
<pre>void sort(args_proxy const &args, kwds_proxy const &kwds);</pre>
This is because in Python 3 <code>list.sort</code> requires all its arguments be keyword arguments.
So you should call it like this:
<pre>x.sort(*tuple(), **dict(make_tuple(make_tuple("reverse", true))));</pre>
</li>
<li>According to <a href="http://www.python.org/dev/peps/pep-3123/">PEP 3123</a>,
when building Boost.Python against Python older than 2.6, the following macros will
be defined in Boost.Python header:
<pre>
# define Py_TYPE(o) (((PyObject*)(o))->ob_type)
# define Py_REFCNT(o) (((PyObject*)(o))->ob_refcnt)
# define Py_SIZE(o) (((PyVarObject*)(o))->ob_size)</pre>
So extension writers can use these macro directly, to make code clean and compatible with Python 3.
</li>
</ul>
</ul>
</dd>
<dt>1.39.0 Release</dt>
<dd>
<ul>
<li>Pythonic signatures are now automatically appended to the
docstrings.
<li>Use <a href="v2/docstring_options.html"
><code>docstring_options.hpp</code></a> header
control the content of docstrings.
<li>This new feature increases the size of the modules by about 14%.
If this is not acceptable it can be turned off by defining the macro
BOOST_PYTHON_NO_PY_SIGNATURES. Modules compiled with and without the macro
defined are compatible.
</li>
<li> If BOOST_PYTHON_NO_PY_SIGNATURES is undefined, this version defines the
macro BOOST_PYTHON_SUPPORTS_PY_SIGNATURES. This allows writing code that will compile
with older version of Boost.Python (see <a href="v2/pytype_function.html#examples">here</a>).
</li>
<li>By defining BOOST_PYTHON_PY_SIGNATURES_PROPER_INIT_SELF_TYPE, and at a cost
of another 14% size increase, proper pythonic type is generated for the "self"
parameter of the __init__ methods.
</li>
<li> To support this new feature changes were made to the
<a href="v2/to_python_converter.html"><code>to_python_converter.hpp</code></a>,
<a href="v2/default_call_policies.html"><code>default_call_policies</code></a>,
<a href="v2/ResultConverter.html"><code>ResultConverter</code></a>,
<a href="v2/CallPolicies.html"><code>CallPolicies</code></a> and some others.
Efforts were made not to have interface breaking changes.
</li>
</ul>
</dd>
<dt>12 May 2007 - 1.34.0 release</dt>
<dd> <dd>
<ul> <ul>
@@ -293,7 +365,7 @@ BOOST_PYTHON_MODULE(test)
19 November 2004 19 November 2004
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p> <!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
<p><i>&copy; Copyright <a href="../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002-2003.</i></p> Abrahams</a> 2002-2003.</i></p>
</body> </body>
</html> </html>

View File

@@ -375,27 +375,54 @@
application modules, including GUI and post-processing of 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://polybori.sourceforge.net/">PolyBoRi</a></b></dt>
LLC</a></b></dt>
<dd> <dd>
Rational Discovery provides computational modeling, combinatorial <p><a href="mailto:brickenstein@mfo.de"
library design and custom software development services to the >Michael Brickenstein</a> writes:</p>
pharmaceutical, biotech and chemical industries. We do a substantial
amount of internal research to develop new approaches for applying
machine-learning techniques to solve chemical problems. Because we're a
small organization and chemistry is a large and complex field, it is
essential that we be able to quickly and easily prototype and test new
algorithms.
<p>For our internal software, we implement core data structures in C <blockquote>
and expose them to Python using Boost.Python. Algorithm development is <p>The core of PolyBoRi is a C++ library, which provides
done in Python and then translated to C if required (often it's not). high-level data types for Boolean polynomials and monomials,
This hybrid development approach not only greatly increases our exponent vectors, as well as for the underlying polynomial
productivity, but it also allows "non-developers" (people without C rings and subsets of the powerset of the Boolean variables. As
experience) to take part in method development. Learning C is a a unique approach, binary decision diagrams are used as
daunting task, but "Python fits your brain." (Thanks to Bruce Eckel for internal storage type for polynomial structures. On top of
the quote.)</p> this C++-library we provide a Python interface. This allows
parsing of complex polynomial systems, as well as sophisticated
and extendable strategies for Gr&ouml;bner basis computation.
Boost.Python has helped us to create this interface in a
very clean way.</p>
</blockquote>
</dd>
<dt><b><a href="http://pyrap.googlecode.com/">Pyrap</a></b></dt>
<dd>
<p><a href="diepen@astron.nl"
>Ger van Diepen</a> writes:</p>
<blockquote>
<p>Pyrap is the python interface to the Radio-Astronomical Package
casacore (<a href="http://casacore.googlecode.com/"
>casacore.googlecode.com</a>). Astronomers love pyrap because
it makes it easily possible to get their data (observed with
radio-astronomical telescopes like LOFAR, ASKAP, and eVLA) in numpy
arrays and do basic data inspection and manipulation using the many
python packages that are available.</p>
<p>Boost.Python made it quite easily possible to create converters for
the various data types, also for numpy arrays and individual elements
of a numpy array. It's nice they work fully recursively. Mapping C++
functions to Python was straightforward.</p>
</blockquote>
</dd>
<dt><b><a href="http://www.rdkit.org/"
>RDKit: Cheminformatics and Machine Learning Software</a></b></dt>
<dd>
A collection of cheminformatics and machine-learning software
written in C++ and Python.
</dd> </dd>
</dl> </dl>
@@ -435,11 +462,11 @@
</dl> </dl>
<hr> <hr>
<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 -->
15 July, 2003</p> 29 May, 2008</p>
<p><i>&copy; Copyright <a href="../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002-2003.</i></p> Abrahams</a> 2002-2008.</i></p>
</body> </body>
</html> </html>

View File

@@ -67,7 +67,7 @@
12 Sept, 2003 <!--webbot bot="Timestamp" endspan i-checksum="39359" --> 12 Sept, 2003 <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2003.</i></p> Abrahams</a> 2003.</i></p>
</body> </body>
</html> </html>

View File

@@ -6,10 +6,13 @@ project boost/libs/python/doc/tutorial/doc ;
import boostbook : boostbook ; import boostbook : boostbook ;
using quickbook ; using quickbook ;
path-constant images : html ;
boostbook tutorial boostbook tutorial
: :
tutorial.qbk tutorial.qbk
: :
<xsl:param>boost.root=../../../../../.. <xsl:param>boost.root=../../../../../..
<xsl:param>boost.libraries=../../../../../../libs/libraries.htm <format>pdf:<xsl:param>img.src.path=$(images)/
<format>pdf:<xsl:param>boost.url.prefix=http://www.boost.org/doc/libs/release/libs/python/doc/tutorial/doc/html
; ;

View File

@@ -1,511 +0,0 @@
/*=============================================================================
Copyright (c) 2004 Joel de Guzman
http://spirit.sourceforge.net/
Use, modification and distribution is subject to 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)
=============================================================================*/
/*=============================================================================
Body defaults
=============================================================================*/
body
{
margin: 1em;
font-family: sans-serif;
}
/*=============================================================================
Paragraphs
=============================================================================*/
p
{
text-align: left;
font-size: 10pt;
line-height: 1.15;
}
/*=============================================================================
Program listings
=============================================================================*/
/* Code on paragraphs */
p tt.computeroutput
{
font-size: 9pt;
}
pre.synopsis
{
font-size: 90%;
margin: 1pc 4% 0pc 4%;
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
}
.programlisting,
.screen
{
font-size: 9pt;
display: block;
margin: 1pc 4% 0pc 4%;
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
=============================================================================*/
h1, h2, h3, h4, h5, h6
{
text-align: left;
margin: 1em 0em 0.5em 0em;
font-weight: bold;
}
h1 { font: 140% }
h2 { font: bold 140% }
h3 { font: bold 130% }
h4 { font: bold 120% }
h5 { font: italic 110% }
h6 { font: italic 100% }
/* Top page titles */
title,
h1.title,
h2.title
h3.title,
h4.title,
h5.title,
h6.title,
.refentrytitle
{
font-weight: bold;
margin-bottom: 1pc;
}
h1.title { font-size: 140% }
h2.title { font-size: 140% }
h3.title { font-size: 130% }
h4.title { font-size: 120% }
h5.title { font-size: 110% }
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
=============================================================================*/
li
{
font-size: 10pt;
line-height: 1.3;
}
/* Unordered lists */
ul
{
text-align: left;
}
/* Ordered lists */
ol
{
text-align: left;
}
/*=============================================================================
Links
=============================================================================*/
a
{
text-decoration: none; /* no underline */
}
a:hover
{
text-decoration: underline;
}
/*=============================================================================
Spirit style navigation
=============================================================================*/
.spirit-nav
{
text-align: right;
}
.spirit-nav a
{
color: white;
padding-left: 0.5em;
}
.spirit-nav img
{
border-width: 0px;
}
/*=============================================================================
Table of contents
=============================================================================*/
.toc
{
margin: 1pc 4% 0pc 4%;
padding: 0.1pc 1pc 0.1pc 1pc;
font-size: 80%;
line-height: 1.15;
}
.boost-toc
{
float: right;
padding: 0.5pc;
}
/*=============================================================================
Tables
=============================================================================*/
.table-title,
div.table p.title
{
margin-left: 4%;
padding-right: 0.5em;
padding-left: 0.5em;
}
.informaltable table,
.table table
{
width: 92%;
margin-left: 4%;
margin-right: 4%;
}
div.informaltable table,
div.table table
{
padding: 4px;
}
/* Table Cells */
div.informaltable table tr td,
div.table table tr td
{
padding: 0.5em;
text-align: left;
font-size: 9pt;
}
div.informaltable table tr th,
div.table table tr th
{
padding: 0.5em 0.5em 0.5em 0.5em;
border: 1pt solid white;
font-size: 80%;
}
/*=============================================================================
Blurbs
=============================================================================*/
div.note,
div.tip,
div.important,
div.caution,
div.warning,
p.blurb
{
font-size: 9pt; /* A little bit smaller than the main text */
line-height: 1.2;
display: block;
margin: 1pc 4% 0pc 4%;
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
}
p.blurb img
{
padding: 1pt;
}
/*=============================================================================
Variable Lists
=============================================================================*/
/* Make the terms in definition lists bold */
div.variablelist dl dt,
span.term
{
font-weight: bold;
font-size: 10pt;
}
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;
}
div.variablelist dl dt
{
margin-bottom: 0.2em;
}
div.variablelist dl dd
{
margin: 0em 0em 0.5em 2em;
font-size: 10pt;
}
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 */
span.title
{
font-style: italic;
}
span.underline
{
text-decoration: underline;
}
span.strikethrough
{
text-decoration: line-through;
}
/* Copyright, Legal Notice */
div div.legalnotice p
{
text-align: left
}
/*=============================================================================
Colors
=============================================================================*/
@media screen
{
/* Links */
a
{
color: #005a9c;
}
a:visited
{
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 */
.keyword { color: #0000AA; }
.identifier { color: #000000; }
.special { color: #707070; }
.preprocessor { color: #402080; }
.char { color: teal; }
.comment { color: #800000; }
.string { color: teal; }
.number { color: teal; }
.white_bkd { background-color: #FFFFFF; }
.dk_grey_bkd { background-color: #999999; }
/* Copyright, Legal Notice */
.copyright
{
color: #666666;
font-size: small;
}
div div.legalnotice p
{
color: #666666;
}
/* Program listing */
pre.synopsis
{
border: 1px solid #DCDCDC;
}
.programlisting,
.screen
{
border: 1px solid #DCDCDC;
}
td .programlisting,
td .screen
{
border: 0px solid #DCDCDC;
}
/* Blurbs */
div.note,
div.tip,
div.important,
div.caution,
div.warning,
p.blurb
{
border: 1px solid #DCDCDC;
}
/* Table of contents */
.toc
{
border: 1px solid #DCDCDC;
}
/* Tables */
div.informaltable table tr td,
div.table table tr td
{
border: 1px solid #DCDCDC;
}
div.informaltable table tr th,
div.table table tr th
{
background-color: #F0F0F0;
border: 1px solid #DCDCDC;
}
/* Misc */
span.highlight
{
color: #00A000;
}
}
@media print
{
/* Links */
a
{
color: black;
}
a:visited
{
color: black;
}
.spirit-nav
{
display: none;
}
/* Program listing */
pre.synopsis
{
border: 1px solid gray;
}
.programlisting,
.screen
{
border: 1px solid gray;
}
td .programlisting,
td .screen
{
border: 0px solid #DCDCDC;
}
/* Table of contents */
.toc
{
border: 1px solid gray;
}
.informaltable table,
.table table
{
border: 1px solid gray;
border-collapse: collapse;
}
/* Tables */
div.informaltable table tr td,
div.table table tr td
{
border: 1px solid gray;
}
div.informaltable table tr th,
div.table table tr th
{
border: 1px solid gray;
}
/* Misc */
span.highlight
{
font-weight: bold;
}
}

View File

@@ -1,37 +1,37 @@
<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=US-ASCII">
<title>Chapter 1. python 1.0</title> <title>Chapter&#160;1.&#160;python 2.0</title>
<link rel="stylesheet" href="boostbook.css" type="text/css"> <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="start" href="index.html" title="Chapter 1. python 1.0"> <link rel="home" href="index.html" title="Chapter&#160;1.&#160;python 2.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%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" 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.html">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="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="../../../../../../more/faq.htm">FAQ</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td> <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</table> </tr></table>
<hr> <hr>
<div class="spirit-nav"><a accesskey="n" href="python/hello.html"><img src="images/next.png" alt="Next"></a></div> <div class="spirit-nav"><a accesskey="n" href="python/hello.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a></div>
<div class="chapter" lang="en"> <div class="chapter">
<div class="titlepage"><div> <div class="titlepage"><div>
<div><h2 class="title"> <div><h2 class="title">
<a name="python"></a>Chapter 1. python 1.0</h2></div> <a name="python"></a>Chapter&#160;1.&#160;python 2.0</h2></div>
<div><div class="author"><h3 class="author"> <div><div class="author"><h3 class="author">
<span class="firstname">Joel</span> <span class="surname">de Guzman</span> <span class="firstname">Joel</span> <span class="surname">de Guzman</span>
</h3></div></div> </h3></div></div>
<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 <div><p class="copyright">Copyright &#169; 2002-2005 Joel
de Guzman, David Abrahams</p></div> de Guzman, David Abrahams</p></div>
<div><div class="legalnotice"> <div><div class="legalnotice">
<a name="id2632684"></a><p> <a name="python.legal"></a><p>
Distributed under the Boost Software License, Version 1.0. (See accompanying Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top"> file LICENSE_1_0.txt or copy at <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>)
@@ -42,8 +42,8 @@
<p><b>Table of Contents</b></p> <p><b>Table of Contents</b></p>
<dl> <dl>
<dt><span class="section"><a href="index.html#python.quickstart">QuickStart</a></span></dt> <dt><span class="section"><a href="index.html#python.quickstart">QuickStart</a></span></dt>
<dt><span class="section"><a href="python/hello.html"> Building Hello World</a></span></dt> <dt><span class="section"><a href="python/hello.html">Building Hello World</a></span></dt>
<dt><span class="section"><a href="python/exposing.html"> Exposing Classes</a></span></dt> <dt><span class="section"><a href="python/exposing.html">Exposing Classes</a></span></dt>
<dd><dl> <dd><dl>
<dt><span class="section"><a href="python/exposing.html#python.constructors">Constructors</a></span></dt> <dt><span class="section"><a href="python/exposing.html#python.constructors">Constructors</a></span></dt>
<dt><span class="section"><a href="python/exposing.html#python.class_data_members">Class Data Members</a></span></dt> <dt><span class="section"><a href="python/exposing.html#python.class_data_members">Class Data Members</a></span></dt>
@@ -60,18 +60,19 @@
<dt><span class="section"><a href="python/functions.html#python.default_arguments">Default Arguments</a></span></dt> <dt><span class="section"><a href="python/functions.html#python.default_arguments">Default Arguments</a></span></dt>
<dt><span class="section"><a href="python/functions.html#python.auto_overloading">Auto-Overloading</a></span></dt> <dt><span class="section"><a href="python/functions.html#python.auto_overloading">Auto-Overloading</a></span></dt>
</dl></dd> </dl></dd>
<dt><span class="section"><a href="python/object.html"> Object Interface</a></span></dt> <dt><span class="section"><a href="python/object.html">Object Interface</a></span></dt>
<dd><dl> <dd><dl>
<dt><span class="section"><a href="python/object.html#python.basic_interface">Basic Interface</a></span></dt> <dt><span class="section"><a href="python/object.html#python.basic_interface">Basic Interface</a></span></dt>
<dt><span class="section"><a href="python/object.html#python.derived_object_types">Derived Object types</a></span></dt> <dt><span class="section"><a href="python/object.html#python.derived_object_types">Derived Object types</a></span></dt>
<dt><span class="section"><a href="python/object.html#python.extracting_c___objects">Extracting C++ objects</a></span></dt> <dt><span class="section"><a href="python/object.html#python.extracting_c___objects">Extracting C++ objects</a></span></dt>
<dt><span class="section"><a href="python/object.html#python.enums">Enums</a></span></dt> <dt><span class="section"><a href="python/object.html#python.enums">Enums</a></span></dt>
<dt><span class="section"><a href="python/object.html#python.creating_python_object">Creating <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span></code> from <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span></code></a></span></dt>
</dl></dd> </dl></dd>
<dt><span class="section"><a href="python/embedding.html">Embedding</a></span></dt> <dt><span class="section"><a href="python/embedding.html">Embedding</a></span></dt>
<dd><dl><dt><span class="section"><a href="python/embedding.html#python.using_the_interpreter">Using the interpreter</a></span></dt></dl></dd> <dd><dl><dt><span class="section"><a href="python/embedding.html#python.using_the_interpreter">Using the interpreter</a></span></dt></dl></dd>
<dt><span class="section"><a href="python/iterators.html">Iterators</a></span></dt> <dt><span class="section"><a href="python/iterators.html">Iterators</a></span></dt>
<dt><span class="section"><a href="python/exception.html"> Exception Translation</a></span></dt> <dt><span class="section"><a href="python/exception.html">Exception Translation</a></span></dt>
<dt><span class="section"><a href="python/techniques.html"> General Techniques</a></span></dt> <dt><span class="section"><a href="python/techniques.html">General Techniques</a></span></dt>
<dd><dl> <dd><dl>
<dt><span class="section"><a href="python/techniques.html#python.creating_packages">Creating Packages</a></span></dt> <dt><span class="section"><a href="python/techniques.html#python.creating_packages">Creating Packages</a></span></dt>
<dt><span class="section"><a href="python/techniques.html#python.extending_wrapped_objects_in_python">Extending Wrapped Objects in Python</a></span></dt> <dt><span class="section"><a href="python/techniques.html#python.extending_wrapped_objects_in_python">Extending Wrapped Objects in Python</a></span></dt>
@@ -79,7 +80,7 @@
</dl></dd> </dl></dd>
</dl> </dl>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -93,16 +94,15 @@
code takes on the look of a kind of declarative interface definition language code takes on the look of a kind of declarative interface definition language
(IDL). (IDL).
</p> </p>
<a name="quickstart.hello_world"></a><h3> <h3>
<a name="id2595112"></a> <a name="quickstart.hello_world"></a>
Hello World Hello World
</h3> </h3>
<p> <p>
Following C/C++ tradition, let's start with the "hello, world". A Following C/C++ tradition, let's start with the "hello, world". A
C++ Function: C++ Function:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">greet</span><span class="special">()</span>
<span class="special">{</span> <span class="special">{</span>
<span class="keyword">return</span> <span class="string">"hello, world"</span><span class="special">;</span> <span class="keyword">return</span> <span class="string">"hello, world"</span><span class="special">;</span>
<span class="special">}</span> <span class="special">}</span>
@@ -110,12 +110,11 @@
<p> <p>
can be exposed to Python by writing a Boost.Python wrapper: can be exposed to Python by writing a Boost.Python wrapper:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</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>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span> <span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello_ext</span><span class="special">)</span>
<span class="special">{</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>
<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 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 class="special">}</span> <span class="special">}</span>
</pre> </pre>
@@ -123,32 +122,21 @@
That's it. We're done. We can now build this as a shared library. The resulting That's it. We're done. We can now build this as a shared library. The resulting
DLL is now visible to Python. Here's a sample Python session: DLL is now visible to Python. Here's a sample Python session:
</p> </p>
<p> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">hello_ext</span>
</p> <span class="special">&gt;&gt;&gt;</span> <span class="keyword">print</span> <span class="identifier">hello_ext</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span>
<pre class="programlisting">
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">hello</span>
<span class="special">&gt;&gt;&gt;</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> <span class="identifier">hello</span><span class="special">,</span> <span class="identifier">world</span>
</pre> </pre>
<p> <div class="blockquote"><blockquote class="blockquote"><p>
</p> <span class="emphasis"><em><span class="bold"><strong>Next stop... Building your Hello World module
<div class="blockquote"><blockquote class="blockquote"> from start to finish...</strong></span></em></span>
<p> </p></blockquote></div>
</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: May 18, 2007 at 15:46:01 GMT</p></small></td> <td align="left"><p><small>Last revised: December 26, 2011 at 21:58:39 GMT</small></p></td>
<td align="right"><small></small></td> <td align="right"><div class="copyright-footer"></div></td>
</tr></table> </tr></table>
<hr> <hr>
<div class="spirit-nav"><a accesskey="n" href="python/hello.html"><img src="images/next.png" alt="Next"></a></div> <div class="spirit-nav"><a accesskey="n" href="python/hello.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a></div>
</body> </body>
</html> </html>

View File

@@ -1,28 +1,28 @@
<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=US-ASCII">
<title>Embedding</title> <title>Embedding</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css"> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="start" href="../index.html" title="Chapter 1. python 1.0"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="up" href="../index.html" title="Chapter 1. python 1.0"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;python 2.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%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" 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.html">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="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="../../../../../../../more/faq.htm">FAQ</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="object.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="object.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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.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>
@@ -39,8 +39,8 @@
a lot easier and, in a future version, it may become unnecessary to touch the a lot easier and, in a future version, it may become unnecessary to touch the
Python/C API at all. So stay tuned... <span class="inlinemediaobject"><img src="../images/smiley.png" alt="smiley"></span> Python/C API at all. So stay tuned... <span class="inlinemediaobject"><img src="../images/smiley.png" alt="smiley"></span>
</p> </p>
<a name="embedding.building_embedded_programs"></a><h3> <h3>
<a name="id2654982"></a> <a name="embedding.building_embedded_programs"></a>
Building embedded programs Building embedded programs
</h3> </h3>
<p> <p>
@@ -81,70 +81,64 @@ exe embedded_program # name of the executable
&lt;library-path&gt;$(PYTHON_LIB_PATH) &lt;library-path&gt;$(PYTHON_LIB_PATH)
&lt;find-library&gt;$(PYTHON_EMBEDDED_LIBRARY) ; &lt;find-library&gt;$(PYTHON_EMBEDDED_LIBRARY) ;
</pre> </pre>
<a name="embedding.getting_started"></a><h3> <h3>
<a name="id2655076"></a> <a name="embedding.getting_started"></a>
Getting started Getting started
</h3> </h3>
<p> <p>
Being able to build is nice, but there is nothing to build yet. Embedding the Being able to build is nice, but there is nothing to build yet. Embedding the
Python interpreter into one of your C++ programs requires these 4 steps: Python interpreter into one of your C++ programs requires these 4 steps:
</p> </p>
<div class="orderedlist"><ol type="1"> <div class="orderedlist"><ol class="orderedlist" type="1">
<li> <li class="listitem">
#include <code class="literal">&lt;boost/python.hpp&gt;</code><br><br> #include <code class="literal">&lt;boost/python.hpp&gt;</code>
</li> </li>
<li> <li class="listitem">
Call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-652" target="_top">Py_Initialize</a>() 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> to start the interpreter and create the <code class="literal">__main__</code> module.
module.<br><br> </li>
</li> <li class="listitem">
<li> Call other Python C API routines to use the interpreter.
Call other Python C API routines to use the interpreter.<br><br> </li>
</li>
</ol></div> </ol></div>
<div class="sidebar"> <div class="note"><table border="0" summary="Note">
<p class="title"><b></b></p> <tr>
<p> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> <span class="bold"><strong>Note that at this time you must <th align="left">Note</th>
not call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-656" target="_top">Py_Finalize</a>() </tr>
to stop the interpreter. This may be fixed in a future version of boost.python.</strong></span> <tr><td align="left" valign="top"><p>
</p> <span class="bold"><strong>Note that at this time you must not call <a href="http://www.python.org/doc/current/api/initialization.html#l2h-656" target="_top">Py_Finalize</a>()
</div> to stop the interpreter. This may be fixed in a future version of boost.python.</strong></span>
</p></td></tr>
</table></div>
<p> <p>
(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.)
</p> </p>
<div class="blockquote"><blockquote class="blockquote"> <div class="blockquote"><blockquote class="blockquote"><p>
<p> <span class="emphasis"><em><span class="bold"><strong>Now that we can embed the interpreter in
</p> our programs, lets see how to put it to use...</strong></span></em></span>
<p> </p></blockquote></div>
<span class="emphasis"><em><span class="bold"><strong>Now that we can embed the interpreter in <div class="section">
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="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. Naturally, As you probably already know, objects in Python are reference-counted. Naturally,
the <code class="literal">PyObject</code>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 automatic There is a difference however. While the reference-counting is fully automatic
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 in Python, the Python C API requires you to do it <a href="http://www.python.org/doc/current/c-api/refcounting.html" target="_top">by
by hand]. This is messy and especially hard to get right in the presence hand</a>. This is messy and especially hard to get right in the presence
of C++ exceptions. Fortunately Boost.Python provides the <a href="../../../../v2/handle.html" target="_top">handle</a> of C++ exceptions. Fortunately Boost.Python provides the <a href="../../../../v2/handle.html" target="_top">handle</a>
and <a href="../../../../v2/object.html" target="_top">object</a> class templates to and <a href="../../../../v2/object.html" target="_top">object</a> class templates to
automate the process. automate the process.
</p> </p>
<a name="using_the_interpreter.running_python_code"></a><h3> <h3>
<a name="id2655255"></a> <a name="using_the_interpreter.running_python_code"></a>
Running Python code Running Python code
</h3> </h3>
<p> <p>
Boost.python provides three related functions to run Python code from C++. Boost.python provides three related functions to run Python code from C++.
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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>
<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>
<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</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> <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> </pre>
@@ -157,25 +151,22 @@ exe embedded_program # name of the executable
The <code class="literal">globals</code> and <code class="literal">locals</code> parameters are The <code class="literal">globals</code> and <code class="literal">locals</code> parameters are
Python dictionaries containing the globals and locals of the context in which Python dictionaries containing the globals and locals of the context in which
to run the code. For most intents and purposes you can use the namespace to run the code. For most intents and purposes you can use the namespace
dictionary of the <code class="literal"><span class="underline">_main</span>_</code> dictionary of the <code class="literal">__main__</code> module for both parameters.
module for both parameters.
</p> </p>
<p> <p>
Boost.python provides a function to import a module: Boost.python provides a function to import a module:
</p> </p>
<pre class="programlisting"> <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>
<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> </pre>
<p> <p>
import imports a python module (potentially loading it into the running process import imports a python module (potentially loading it into the running process
first), and returns it. first), and returns it.
</p> </p>
<p> <p>
Let's import the <code class="literal"><span class="underline">_main</span>_</code> Let's import the <code class="literal">__main__</code> module and run some Python code
module and run some Python code in its namespace: in its namespace:
</p> </p>
<pre class="programlisting"> <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_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">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">"hello = file('hello.txt', 'w')\n"</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">"hello = file('hello.txt', 'w')\n"</span>
@@ -187,8 +178,8 @@ exe embedded_program # name of the executable
This should create a file called 'hello.txt' in the current directory containing This should create a file called 'hello.txt' in the current directory containing
a phrase that is well-known in programming circles. a phrase that is well-known in programming circles.
</p> </p>
<a name="using_the_interpreter.manipulating_python_objects"></a><h3> <h3>
<a name="id2655783"></a> <a name="using_the_interpreter.manipulating_python_objects"></a>
Manipulating Python objects Manipulating Python objects
</h3> </h3>
<p> <p>
@@ -198,63 +189,59 @@ exe embedded_program # name of the executable
derivatives. We've already seen that they can be constructed from a <code class="literal">handle</code>. derivatives. We've already seen that they can be constructed from a <code class="literal">handle</code>.
The following examples should further illustrate this fact: The following examples should further illustrate this fact:
</p> </p>
<pre class="programlisting"> <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_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">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="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">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">main_namespace</span><span class="special">[</span><span class="string">"result"</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">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">main_namespace</span><span class="special">[</span><span class="string">"result"</span><span class="special">]);</span>
</pre> </pre>
<p> <p>
Here we create a dictionary object for the <code class="literal"><span class="underline">_main</span>_</code> Here we create a dictionary object for the <code class="literal">__main__</code> module's
module's namespace. Then we assign 5 squared to the result variable and read namespace. Then we assign 5 squared to the result variable and read this
this variable from the dictionary. Another way to achieve the same result variable from the dictionary. Another way to achieve the same result is to
is to use eval instead, which returns the result directly: use eval instead, which returns the result directly:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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>
<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>
<span class="keyword">int</span> <span class="identifier">five_squared</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">result</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">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">result</span><span class="special">);</span>
</pre> </pre>
<a name="using_the_interpreter.exception_handling"></a><h3> <h3>
<a name="id2656116"></a> <a name="using_the_interpreter.exception_handling"></a>
Exception handling Exception handling
</h3> </h3>
<p> <p>
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> 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>
is thrown: is thrown:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">try</span>
<span class="keyword">try</span>
<span class="special">{</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="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 class="comment">// execution will never get here:</span>
</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">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">result</span><span class="special">);</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">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">result</span><span class="special">);</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">&amp;)</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">&amp;)</span>
<span class="special">{</span> <span class="special">{</span>
<span class="comment">// handle the exception in some way <span class="comment">// handle the exception in some way</span>
</span><span class="special">}</span> <span class="special">}</span>
</pre> </pre>
<p> <p>
The <code class="literal">error_already_set</code> exception class doesn't carry any The <code class="literal">error_already_set</code> exception class doesn't carry any
information in itself. To find out more about the Python exception that occurred, 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 you need to use the <a href="http://www.python.org/doc/api/exceptionHandling.html" target="_top">exception
handling functions</a> of the Python<span class="emphasis"><em>C API in your catch-statement. handling functions</a> of the Python C API in your catch-statement. This
This can be as simple as calling [@http:</em></span>/www.python.org/doc/api/exceptionHandling.html#l2h-70 can be as simple as calling <a href="http://www.python.org/doc/api/exceptionHandling.html#l2h-70" target="_top">PyErr_Print()</a>
PyErr_Print()] to print the exception's traceback to the console, or comparing to print the exception's traceback to the console, or comparing the type
the type of the exception with those of the <a href="http://www.python.org/doc/api/standardExceptions.html" target="_top">standard of the exception with those of the <a href="http://www.python.org/doc/api/standardExceptions.html" target="_top">standard
exceptions</a>: exceptions</a>:
</p> </p>
<pre class="programlisting"> <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">&amp;)</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">&amp;)</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="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="special">{</span>
<span class="comment">// handle ZeroDivisionError specially <span class="comment">// handle ZeroDivisionError specially</span>
</span> <span class="special">}</span> <span class="special">}</span>
<span class="keyword">else</span> <span class="keyword">else</span>
<span class="special">{</span> <span class="special">{</span>
<span class="comment">// print all other errors to stderr <span class="comment">// print all other errors to stderr</span>
</span> <span class="identifier">PyErr_Print</span><span class="special">();</span> <span class="identifier">PyErr_Print</span><span class="special">();</span>
<span class="special">}</span> <span class="special">}</span>
<span class="special">}</span> <span class="special">}</span>
</pre> </pre>
@@ -266,12 +253,17 @@ exe embedded_program # name of the executable
</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 <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005 Joel
de Guzman, David Abrahams</small></td> de Guzman, David Abrahams<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
http://www.boost.org/LICENSE_1_0.txt </a>)
</p>
</div></td>
</tr></table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="object.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="object.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="iterators.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -1,43 +1,41 @@
<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=US-ASCII">
<title>Exception Translation</title> <title>Exception Translation</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css"> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="start" href="../index.html" title="Chapter 1. python 1.0"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="up" href="../index.html" title="Chapter 1. python 1.0"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;python 2.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%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" 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.html">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="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="../../../../../../../more/faq.htm">FAQ</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="iterators.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="techniques.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="iterators.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="techniques.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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 boundary All C++ exceptions must be caught at the boundary with Python code. This boundary
is the point where C++ meets Python. Boost.Python provides a default exception is the point where C++ meets Python. Boost.Python provides a default exception
handler that translates selected standard exceptions, then gives up: handler that translates selected standard exceptions, then gives up:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">raise</span> <span class="identifier">RuntimeError</span><span class="special">,</span> <span class="string">'unidentifiable C++ Exception'</span>
<span class="keyword">raise</span> <span class="identifier">RuntimeError</span><span class="special">,</span> <span class="string">'unidentifiable C++ Exception'</span>
</pre> </pre>
<p> <p>
Users may provide custom translation. Here's an example: Users may provide custom translation. Here's an example:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">struct</span> <span class="identifier">PodBayDoorException</span><span class="special">;</span>
<span class="identifier">struct</span> <span class="identifier">PodBayDoorException</span><span class="special">;</span>
<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">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span> <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">&amp;</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
<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> <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>
<span class="special">}</span> <span class="special">}</span>
@@ -49,12 +47,17 @@
</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 <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005 Joel
de Guzman, David Abrahams</small></td> de Guzman, David Abrahams<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
http://www.boost.org/LICENSE_1_0.txt </a>)
</p>
</div></td>
</tr></table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="iterators.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="techniques.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="iterators.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="techniques.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -1,30 +1,30 @@
<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=US-ASCII">
<title>Exposing Classes</title> <title>Exposing Classes</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css"> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="start" href="../index.html" title="Chapter 1. python 1.0"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="up" href="../index.html" title="Chapter 1. python 1.0"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;python 2.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%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" 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.html">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="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="../../../../../../../more/faq.htm">FAQ</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="hello.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="functions.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="hello.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="functions.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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.exposing"></a> Exposing Classes</h2></div></div></div> <a name="python.exposing"></a>Exposing Classes</h2></div></div></div>
<div class="toc"><dl> <div class="toc"><dl>
<dt><span class="section"><a href="exposing.html#python.constructors">Constructors</a></span></dt> <dt><span class="section"><a href="exposing.html#python.constructors">Constructors</a></span></dt>
<dt><span class="section"><a href="exposing.html#python.class_data_members">Class Data Members</a></span></dt> <dt><span class="section"><a href="exposing.html#python.class_data_members">Class Data Members</a></span></dt>
@@ -40,8 +40,7 @@
<p> <p>
Consider a C++ class/struct that we want to expose to Python: Consider a C++ class/struct that we want to expose to Python:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">World</span>
<span class="keyword">struct</span> <span class="identifier">World</span>
<span class="special">{</span> <span class="special">{</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">-&gt;</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="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">-&gt;</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">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>
@@ -51,8 +50,7 @@
<p> <p>
We can expose this to Python by writing a corresponding Boost.Python C++ Wrapper: We can expose this to Python by writing a corresponding Boost.Python C++ Wrapper:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</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> <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>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span> <span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span>
@@ -69,16 +67,13 @@
we may use our class <code class="literal">World</code> in Python. Here's a sample Python we may use our class <code class="literal">World</code> in Python. Here's a sample Python
session: session:
</p> </p>
<p> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">hello</span>
</p>
<pre class="programlisting">
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">hello</span>
<span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="identifier">planet</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span> <span class="special">&gt;&gt;&gt;</span> <span class="identifier">planet</span><span class="special">.</span><span class="identifier">greet</span><span class="special">()</span>
<span class="string">'howdy'</span> <span class="string">'howdy'</span>
</pre> </pre>
<div class="section" lang="en"> <div class="section">
<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>
@@ -87,20 +82,16 @@
exposes the default constructor by default, which is why we were able to exposes the default constructor by default, which is why we were able to
write write
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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> </pre>
<p> <p>
We may wish to wrap a class with a non-default constructor. Let us build We may wish to wrap a class with a non-default constructor. Let us build
on our previous example: on our previous example:
</p> </p>
<p> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">World</span>
</p>
<pre class="programlisting">
<span class="keyword">struct</span> <span class="identifier">World</span>
<span class="special">{</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 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> <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">-&gt;</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="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">-&gt;</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">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="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="special">};</span>
@@ -111,8 +102,7 @@
We have to tell <code class="literal">class_&lt;World&gt;</code> about the constructor We have to tell <code class="literal">class_&lt;World&gt;</code> about the constructor
we want to expose instead. we want to expose instead.
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</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> <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>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span> <span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span>
@@ -126,15 +116,14 @@
<p> <p>
<code class="literal">init&lt;std::string&gt;()</code> exposes the constructor taking <code class="literal">init&lt;std::string&gt;()</code> exposes the constructor taking
in a <code class="literal">std::string</code> (in Python, constructors are spelled in a <code class="literal">std::string</code> (in Python, constructors are spelled
"<code class="literal">"<span class="underline">_init</span>_"</code>"). "<code class="literal">"__init__"</code>").
</p> </p>
<p> <p>
We can expose additional constructors by passing more <code class="literal">init&lt;...&gt;</code>s We can expose additional constructors by passing more <code class="literal">init&lt;...&gt;</code>s
to the <code class="literal">def()</code> member function. Say for example we have to the <code class="literal">def()</code> member function. Say for example we have
another World constructor taking in two doubles: another World constructor taking in two doubles:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">World</span><span class="special">&gt;(</span><span class="string">"World"</span><span class="special">,</span> <span class="identifier">init</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;())</span>
<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">World</span><span class="special">&gt;(</span><span class="string">"World"</span><span class="special">,</span> <span class="identifier">init</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;())</span>
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;())</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;())</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">&amp;</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">"greet"</span><span class="special">,</span> <span class="special">&amp;</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">&amp;</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">set</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">&amp;</span><span class="identifier">World</span><span class="special">::</span><span class="identifier">set</span><span class="special">)</span>
@@ -144,15 +133,14 @@
On the other hand, if we do not wish to expose any constructors at all, we On the other hand, if we do not wish to expose any constructors at all, we
may use <code class="literal">no_init</code> instead: may use <code class="literal">no_init</code> instead:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Abstract</span><span class="special">&gt;(</span><span class="string">"Abstract"</span><span class="special">,</span> <span class="identifier">no_init</span><span class="special">)</span>
<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Abstract</span><span class="special">&gt;(</span><span class="string">"Abstract"</span><span class="special">,</span> <span class="identifier">no_init</span><span class="special">)</span>
</pre> </pre>
<p> <p>
This actually adds an <code class="literal"><span class="underline">_init</span>_</code> This actually adds an <code class="literal">__init__</code> method which always raises
method which always raises a Python RuntimeError exception. a Python RuntimeError exception.
</p> </p>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -161,8 +149,7 @@
to be exposed may be regarded as <span class="bold"><strong>read-only</strong></span> to be exposed may be regarded as <span class="bold"><strong>read-only</strong></span>
or <span class="bold"><strong>read-write</strong></span>. Consider this class <code class="literal">Var</code>: or <span class="bold"><strong>read-write</strong></span>. Consider this class <code class="literal">Var</code>:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">Var</span>
<span class="keyword">struct</span> <span class="identifier">Var</span>
<span class="special">{</span> <span class="special">{</span>
<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 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 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="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>
@@ -173,8 +160,7 @@
Our C++ <code class="literal">Var</code> class and its data members can be exposed Our C++ <code class="literal">Var</code> class and its data members can be exposed
to Python: to Python:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Var</span><span class="special">&gt;(</span><span class="string">"Var"</span><span class="special">,</span> <span class="identifier">init</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;())</span>
<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Var</span><span class="special">&gt;(</span><span class="string">"Var"</span><span class="special">,</span> <span class="identifier">init</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;())</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">&amp;</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_readonly</span><span class="special">(</span><span class="string">"name"</span><span class="special">,</span> <span class="special">&amp;</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">&amp;</span><span class="identifier">Var</span><span class="special">::</span><span class="identifier">value</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">&amp;</span><span class="identifier">Var</span><span class="special">::</span><span class="identifier">value</span><span class="special">);</span>
</pre> </pre>
@@ -182,10 +168,7 @@
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: hello as we did before:
</p> </p>
<p> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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>
</p>
<pre class="programlisting">
<span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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="special">&gt;&gt;&gt;</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> <span class="identifier">pi</span> <span class="keyword">is</span> <span class="identifier">around</span> <span class="number">3.14</span>
@@ -194,14 +177,13 @@
Note that <code class="literal">name</code> is exposed as <span class="bold"><strong>read-only</strong></span> 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>. while <code class="literal">value</code> is exposed as <span class="bold"><strong>read-write</strong></span>.
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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="special">&gt;&gt;&gt;</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="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><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">"&lt;stdin&gt;"</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="error">?</span>
<span class="identifier">File</span> <span class="string">"&lt;stdin&gt;"</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="error">'</span><span class="identifier">t</span> <span class="identifier">set</span> <span class="identifier">attribute</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> </pre>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -210,10 +192,7 @@
The only way to access the class' data is through access (getter/setter) The only way to access the class' data is through access (getter/setter)
functions. Access functions expose class properties. Here's an example: functions. Access functions expose class properties. Here's an example:
</p> </p>
<p> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">Num</span>
</p>
<pre class="programlisting">
<span class="keyword">struct</span> <span class="identifier">Num</span>
<span class="special">{</span> <span class="special">{</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">float</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
@@ -227,35 +206,28 @@
can just be a different syntax for a method call. Wrapping our <code class="literal">Num</code> can just be a different syntax for a method call. Wrapping our <code class="literal">Num</code>
class using Boost.Python: class using Boost.Python:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Num</span><span class="special">&gt;(</span><span class="string">"Num"</span><span class="special">)</span>
<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Num</span><span class="special">&gt;(</span><span class="string">"Num"</span><span class="special">)</span>
<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">&amp;</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">"rovalue"</span><span class="special">,</span> <span class="special">&amp;</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">&amp;</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">set</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">&amp;</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">set</span><span class="special">);</span>
</pre> </pre>
<p> <p>
And at last, in Python: And at last, in Python:
</p> </p>
<p> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">Num</span><span class="special">()</span>
</p>
<pre class="programlisting">
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">Num</span><span class="special">()</span>
<span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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="number">3.14</span><span class="special">,</span> <span class="number">3.14</span><span class="special">)</span>
<span class="special">&gt;&gt;&gt;</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="special">&gt;&gt;&gt;</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>
</span></pre> </pre>
<p> <p>
Take note that the class property <code class="literal">rovalue</code> is exposed as 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> <span class="bold"><strong>read-only</strong></span> since the <code class="literal">rovalue</code>
setter member function is not passed in: 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">&amp;</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">)</span>
</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">&amp;</span><span class="identifier">Num</span><span class="special">::</span><span class="identifier">get</span><span class="special">)</span>
</pre> </pre>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -268,24 +240,21 @@
<p> <p>
Consider this trivial inheritance structure: Consider this trivial inheritance structure:
</p> </p>
<pre class="programlisting"> <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">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> <span class="keyword">struct</span> <span class="identifier">Derived</span> <span class="special">:</span> <span class="identifier">Base</span> <span class="special">{};</span>
</pre> </pre>
<p> <p>
And a set of C++ functions operating on <code class="literal">Base</code> and <code class="literal">Derived</code> And a set of C++ functions operating on <code class="literal">Base</code> and <code class="literal">Derived</code>
object instances: object instances:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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">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="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> <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> </pre>
<p> <p>
We've seen how we can wrap the base class <code class="literal">Base</code>: We've seen how we can wrap the base class <code class="literal">Base</code>:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;(</span><span class="string">"Base"</span><span class="special">)</span>
<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;(</span><span class="string">"Base"</span><span class="special">)</span>
<span class="comment">/*...*/</span> <span class="comment">/*...*/</span>
<span class="special">;</span> <span class="special">;</span>
</pre> </pre>
@@ -293,32 +262,30 @@
Now we can inform Boost.Python of the inheritance relationship between <code class="literal">Derived</code> Now we can inform Boost.Python of the inheritance relationship between <code class="literal">Derived</code>
and its base class <code class="literal">Base</code>. Thus: and its base class <code class="literal">Base</code>. Thus:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">,</span> <span class="identifier">bases</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span> <span class="special">&gt;(</span><span class="string">"Derived"</span><span class="special">)</span>
<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Derived</span><span class="special">,</span> <span class="identifier">bases</span><span class="special">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span> <span class="special">&gt;(</span><span class="string">"Derived"</span><span class="special">)</span>
<span class="comment">/*...*/</span> <span class="comment">/*...*/</span>
<span class="special">;</span> <span class="special">;</span>
</pre> </pre>
<p> <p>
Doing so, we get some things for free: Doing so, we get some things for free:
</p> </p>
<div class="orderedlist"><ol type="1"> <div class="orderedlist"><ol class="orderedlist" type="1">
<li> <li class="listitem">
Derived automatically inherits all of Base's Python methods (wrapped C++ Derived automatically inherits all of Base's Python methods (wrapped
member functions) C++ member functions)
</li> </li>
<li> <li class="listitem">
<span class="bold"><strong>If</strong></span> Base is polymorphic, <code class="literal">Derived</code> <span class="bold"><strong>If</strong></span> Base is polymorphic, <code class="literal">Derived</code>
objects which have been passed to Python via a pointer or reference to objects which have been passed to Python via a pointer or reference to
<code class="literal">Base</code> can be passed where a pointer or reference to <code class="literal">Base</code> can be passed where a pointer or reference to
<code class="literal">Derived</code> is expected. <code class="literal">Derived</code> is expected.
</li> </li>
</ol></div> </ol></div>
<p> <p>
Now, we shall expose the C++ free functions <code class="literal">b</code> and <code class="literal">d</code> Now, we will expose the C++ free functions <code class="literal">b</code> and <code class="literal">d</code>
and <code class="literal">factory</code>: and <code class="literal">factory</code>:
</p> </p>
<pre class="programlisting"> <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">"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">"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> <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> </pre>
@@ -328,25 +295,23 @@
<code class="literal">return_value_policy&lt;manage_new_object&gt;</code> to instruct <code class="literal">return_value_policy&lt;manage_new_object&gt;</code> to instruct
Python to adopt the pointer to <code class="literal">Base</code> and hold the instance Python to adopt the pointer to <code class="literal">Base</code> and hold the instance
in a new Python <code class="literal">Base</code> object until the the Python object in a new Python <code class="literal">Base</code> object until the the Python object
is destroyed. We shall see more of Boost.Python <a href="functions.html#python.call_policies" title="Call Policies">call is destroyed. We will see more of Boost.Python <a class="link" href="functions.html#python.call_policies" title="Call Policies">call
policies</a> later. policies</a> later.
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="comment">// Tell Python to take ownership of factory's result</span>
<span class="comment">// Tell Python to take ownership of factory's result <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><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">&lt;</span><span class="identifier">manage_new_object</span><span class="special">&gt;());</span> <span class="identifier">return_value_policy</span><span class="special">&lt;</span><span class="identifier">manage_new_object</span><span class="special">&gt;());</span>
</pre> </pre>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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 will 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 <code class="literal">Base</code> class: to our <code class="literal">Base</code> class:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">Base</span>
<span class="keyword">struct</span> <span class="identifier">Base</span>
<span class="special">{</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">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="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>
@@ -364,8 +329,7 @@
that will unintrusively hook into the virtual functions so that a Python that will unintrusively hook into the virtual functions so that a Python
override may be called: override may be called:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span>
<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">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span>
<span class="special">{</span> <span class="special">{</span>
<span class="keyword">int</span> <span class="identifier">f</span><span class="special">()</span> <span class="keyword">int</span> <span class="identifier">f</span><span class="special">()</span>
<span class="special">{</span> <span class="special">{</span>
@@ -380,11 +344,15 @@
the job of wrapping classes that are meant to overridden in Python, easier. the job of wrapping classes that are meant to overridden in Python, easier.
</p> </p>
<div class="sidebar"> <div class="sidebar">
<p class="title"><b></b></p> <div class="titlepage"></div>
<p> <p>
<span class="inlinemediaobject"><img src="../images/alert.png" alt="alert"></span> <span class="bold"><strong>MSVC6/7 Workaround</strong></span><br> <span class="inlinemediaobject"><img src="../images/alert.png" alt="alert"></span> <span class="bold"><strong>MSVC6/7 Workaround</strong></span>
<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> </p>
<span class="identifier">call</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="keyword">this</span><span class="special">-&gt;</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>
If you are using Microsoft Visual C++ 6 or 7, you have to write <code class="computeroutput"><span class="identifier">f</span></code> as:
</p>
<p>
<code class="computeroutput"><span class="keyword">return</span> <span class="identifier">call</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="keyword">this</span><span class="special">-&gt;</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> </p>
</div> </div>
<p> <p>
@@ -394,8 +362,7 @@
<p> <p>
Finally, exposing <code class="computeroutput"><span class="identifier">Base</span></code>: Finally, exposing <code class="computeroutput"><span class="identifier">Base</span></code>:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</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">&gt;(</span><span class="string">"Base"</span><span class="special">)</span>
<span class="identifier">class_</span><span class="special">&lt;</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">&gt;(</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="identifier">pure_virtual</span><span class="special">(&amp;</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">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">pure_virtual</span><span class="special">(&amp;</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>
</pre> </pre>
@@ -404,15 +371,23 @@
that the function <code class="computeroutput"><span class="identifier">f</span></code> is a that the function <code class="computeroutput"><span class="identifier">f</span></code> is a
pure virtual function. pure virtual function.
</p> </p>
<div class="sidebar"> <div class="note"><table border="0" summary="Note">
<p class="title"><b></b></p> <tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top">
<p> <p>
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> <span class="bold"><strong>member function and methods</strong></span><br> <span class="bold"><strong>member function and methods</strong></span>
<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>
</p> <p>
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>
</td></tr>
</table></div>
</div> </div>
</div> <div class="section">
<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>
@@ -422,12 +397,11 @@
functions instead, the mechanism is a bit different. functions instead, the mechanism is a bit different.
</p> </p>
<p> <p>
Recall that in the <a href="exposing.html#python.class_virtual_functions" title="Class Virtual Functions">previous Recall that in the <a class="link" href="exposing.html#python.class_virtual_functions" title="Class Virtual Functions">previous
section</a>, we wrapped a class with a pure virtual function that we then section</a>, we wrapped a class with a pure virtual function that we then
implemented in C++, or Python classes derived from it. Our base class: implemented in C++, or Python classes derived from it. Our base class:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">Base</span>
<span class="keyword">struct</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="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> <span class="special">};</span>
@@ -436,8 +410,7 @@
had a pure virtual function <code class="literal">f</code>. If, however, its member had a pure virtual function <code class="literal">f</code>. If, however, its member
function <code class="literal">f</code> was not declared as pure virtual: function <code class="literal">f</code> was not declared as pure virtual:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">Base</span>
<span class="keyword">struct</span> <span class="identifier">Base</span>
<span class="special">{</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">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="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>
@@ -446,14 +419,13 @@
<p> <p>
We wrap it this way: We wrap it this way:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span>
<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">&lt;</span><span class="identifier">Base</span><span class="special">&gt;</span>
<span class="special">{</span> <span class="special">{</span>
<span class="keyword">int</span> <span class="identifier">f</span><span class="special">()</span> <span class="keyword">int</span> <span class="identifier">f</span><span class="special">()</span>
<span class="special">{</span> <span class="special">{</span>
<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">-&gt;</span><span class="identifier">get_override</span><span class="special">(</span><span class="string">"f"</span><span class="special">))</span> <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">-&gt;</span><span class="identifier">get_override</span><span class="special">(</span><span class="string">"f"</span><span class="special">))</span>
<span class="keyword">return</span> <span class="identifier">f</span><span class="special">();</span> <span class="comment">// *note* <span class="keyword">return</span> <span class="identifier">f</span><span class="special">();</span> <span class="comment">// *note*</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="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> <span class="special">}</span>
<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">-&gt;</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="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">-&gt;</span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">();</span> <span class="special">}</span>
@@ -465,19 +437,22 @@
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>. 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>.
</p> </p>
<div class="sidebar"> <div class="sidebar">
<p class="title"><b></b></p> <div class="titlepage"></div>
<p> <p>
<span class="inlinemediaobject"><img src="../images/alert.png" alt="alert"></span> <span class="bold"><strong>MSVC6/7 Workaround</strong></span><br> <span class="inlinemediaobject"><img src="../images/alert.png" alt="alert"></span> <span class="bold"><strong>MSVC6/7 Workaround</strong></span>
<br> If you are using Microsoft Visual C++ 6 or 7, you have to rewrite </p>
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> <p>
<span class="identifier">call</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*&gt;(</span><span class="identifier">f</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">());</span></code>. If you are using Microsoft Visual C++ 6 or 7, you have to rewrite the line
with the <code class="computeroutput"><span class="special">*</span><span class="identifier">note</span><span class="special">*</span></code> as:
</p>
<p>
<code class="computeroutput"><span class="keyword">return</span> <span class="identifier">call</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*&gt;(</span><span class="identifier">f</span><span class="special">.</span><span class="identifier">ptr</span><span class="special">());</span></code>.
</p> </p>
</div> </div>
<p> <p>
Finally, exposing: Finally, exposing:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</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">&gt;(</span><span class="string">"Base"</span><span class="special">)</span>
<span class="identifier">class_</span><span class="special">&lt;</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">&gt;(</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">&amp;</span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">BaseWrap</span><span class="special">::</span><span class="identifier">default_f</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">&amp;</span><span class="identifier">Base</span><span class="special">::</span><span class="identifier">f</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">BaseWrap</span><span class="special">::</span><span class="identifier">default_f</span><span class="special">)</span>
<span class="special">;</span> <span class="special">;</span>
</pre> </pre>
@@ -490,10 +465,7 @@
<p> <p>
In Python, the results would be as expected: In Python, the results would be as expected:
</p> </p>
<p> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">base</span> <span class="special">=</span> <span class="identifier">Base</span><span class="special">()</span>
</p>
<pre class="programlisting">
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">base</span> <span class="special">=</span> <span class="identifier">Base</span><span class="special">()</span>
<span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">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="keyword">return</span> <span class="number">42</span>
@@ -503,23 +475,21 @@
<p> <p>
Calling <code class="literal">base.f()</code>: Calling <code class="literal">base.f()</code>:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">base</span><span class="special">.</span><span class="identifier">f</span><span class="special">()</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">base</span><span class="special">.</span><span class="identifier">f</span><span class="special">()</span>
<span class="number">0</span> <span class="number">0</span>
</pre> </pre>
<p> <p>
Calling <code class="literal">derived.f()</code>: Calling <code class="literal">derived.f()</code>:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">derived</span><span class="special">.</span><span class="identifier">f</span><span class="special">()</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">derived</span><span class="special">.</span><span class="identifier">f</span><span class="special">()</span>
<span class="number">42</span> <span class="number">42</span>
</pre> </pre>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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><h3> <h3>
<a name="id2646169"></a> <a name="class_operators_special_functions.python_operators"></a>
Python Operators Python Operators
</h3> </h3>
<p> <p>
@@ -531,10 +501,7 @@
Consider a file position class <code class="literal">FilePos</code> and a set of operators Consider a file position class <code class="literal">FilePos</code> and a set of operators
that take on FilePos instances: that take on FilePos instances:
</p> </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>
</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>
<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> <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>
<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> <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>
@@ -548,16 +515,15 @@
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: and intuitively:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">FilePos</span><span class="special">&gt;(</span><span class="string">"FilePos"</span><span class="special">)</span>
<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">FilePos</span><span class="special">&gt;(</span><span class="string">"FilePos"</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">// __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">// __add__ <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> <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="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> <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">// __sub__</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="keyword">int</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">// __iadd__</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="keyword">int</span><span class="special">())</span> <span class="comment">// __iadd__ <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">&lt;</span><span class="keyword">int</span><span class="special">&gt;())</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">other</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;())</span> <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">self</span> <span class="special">&lt;</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">&lt;</span> <span class="identifier">self</span><span class="special">);</span> <span class="comment">// __lt__ </pre>
</span></pre>
<p> <p>
The code snippet above is very clear and needs almost no explanation at all. The code snippet above is very clear and needs almost no explanation at all.
It is virtually the same as the operators' signatures. Just take note that It is virtually the same as the operators' signatures. Just take note that
@@ -567,8 +533,8 @@
in place of an actual <code class="literal">T</code> instance when writing "self in place of an actual <code class="literal">T</code> instance when writing "self
expressions". expressions".
</p> </p>
<a name="class_operators_special_functions.special_methods"></a><h3> <h3>
<a name="id2646853"></a> <a name="class_operators_special_functions.special_methods"></a>
Special Methods Special Methods
</h3> </h3>
<p> <p>
@@ -578,8 +544,7 @@
wrap C++ functions that correspond to these Python <span class="emphasis"><em>special functions</em></span>. wrap C++ functions that correspond to these Python <span class="emphasis"><em>special functions</em></span>.
Example: Example:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">Rational</span>
<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> <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>
<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> <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>
@@ -587,34 +552,40 @@
<span class="identifier">ostream</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span><span class="identifier">ostream</span><span class="special">&amp;,</span><span class="identifier">Rational</span><span class="special">);</span> <span class="identifier">ostream</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span><span class="identifier">ostream</span><span class="special">&amp;,</span><span class="identifier">Rational</span><span class="special">);</span>
<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Rational</span><span class="special">&gt;(</span><span class="string">"Rational"</span><span class="special">)</span> <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Rational</span><span class="special">&gt;(</span><span class="string">"Rational"</span><span class="special">)</span>
<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 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> <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">&lt;</span><span class="identifier">Rational</span><span class="special">&gt;))</span> <span class="comment">// __pow__ <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">&lt;</span><span class="identifier">Rational</span><span class="special">&gt;))</span> <span class="comment">// __pow__</span>
</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 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> <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 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> <span class="special">;</span> <span class="special">;</span>
</pre> </pre>
<p> <p>
Need we say more? Need we say more?
</p> </p>
<div class="sidebar"> <div class="note"><table border="0" summary="Note">
<p class="title"><b></b></p> <tr>
<p> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<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">&lt;&lt;</span></code>? Well, the method <code class="computeroutput"><span class="identifier">str</span></code> <th align="left">Note</th>
requires the <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code> </tr>
to do its work (i.e. <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code> is used by the method defined by <tr><td align="left" valign="top"><p>
<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>. What is the business of <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code>? Well, the method <code class="computeroutput"><span class="identifier">str</span></code> requires the <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code> to do its work (i.e. <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code>
</p> 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>.
</div> </p></td></tr>
</table></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 <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005 Joel
de Guzman, David Abrahams</small></td> de Guzman, David Abrahams<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
http://www.boost.org/LICENSE_1_0.txt </a>)
</p>
</div></td>
</tr></table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="hello.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="functions.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="hello.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="functions.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -1,28 +1,28 @@
<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=US-ASCII">
<title>Functions</title> <title>Functions</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css"> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="start" href="../index.html" title="Chapter 1. python 1.0"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="up" href="../index.html" title="Chapter 1. python 1.0"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;python 2.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%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" 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.html">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="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="../../../../../../../more/faq.htm">FAQ</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="exposing.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="object.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="exposing.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="object.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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.functions"></a>Functions</h2></div></div></div> <a name="python.functions"></a>Functions</h2></div></div></div>
<div class="toc"><dl> <div class="toc"><dl>
@@ -33,20 +33,14 @@
</dl></div> </dl></div>
<p> <p>
In this chapter, we'll look at Boost.Python powered functions in closer detail. In this chapter, we'll look at Boost.Python powered functions in closer detail.
We shall see some facilities to make exposing C++ functions to Python safe We will see some facilities to make exposing C++ functions to Python safe from
from potential pifalls such as dangling pointers and references. We shall also potential pifalls such as dangling pointers and references. We will also see
see facilities that will make it even easier for us to expose C++ functions facilities that will make it even easier for us to expose C++ functions that
that take advantage of C++ features such as overloading and default arguments. take advantage of C++ features such as overloading and default arguments.
</p> </p>
<div class="blockquote"><blockquote class="blockquote"> <div class="blockquote"><blockquote class="blockquote"><p>
<p> <span class="emphasis"><em>Read on...</em></span>
</p> </p></blockquote></div>
<p>
<span class="emphasis"><em>Read on...</em></span>
</p>
<p>
</p>
</blockquote></div>
<p> <p>
But before you do, you might want to fire up Python 2.2 or later and type But before you do, you might want to fire up Python 2.2 or later and type
<code class="literal">&gt;&gt;&gt; import this</code>. <code class="literal">&gt;&gt;&gt; import this</code>.
@@ -73,7 +67,7 @@ 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!
</pre> </pre>
<div class="section" lang="en"> <div class="section">
<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>
@@ -87,16 +81,14 @@ Namespaces are one honking great idea -- let's do more of those!
so Boost.Python must deal with them. To do this, it may need your help. Consider so Boost.Python must deal with them. To do this, it may need your help. Consider
the following C++ function: the following C++ function:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">X</span><span class="special">&amp;</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&amp;</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="identifier">X</span><span class="special">&amp;</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&amp;</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> </pre>
<p> <p>
How should the library wrap this function? A naive approach builds a Python How should the library wrap this function? A naive approach builds a Python
X object around result reference. This strategy might or might not work out. X object around result reference. This strategy might or might not work out.
Here's an example where it didn't Here's an example where it didn't
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="identifier">del</span> <span class="identifier">y</span> <span class="special">&gt;&gt;&gt;</span> <span class="identifier">del</span> <span class="identifier">y</span>
<span class="special">&gt;&gt;&gt;</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> <span class="special">&gt;&gt;&gt;</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> </pre>
@@ -106,8 +98,7 @@ Namespaces are one honking great idea -- let's do more of those!
<p> <p>
Well, what if f() was implemented as shown below: Well, what if f() was implemented as shown below:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">X</span><span class="special">&amp;</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&amp;</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="identifier">X</span><span class="special">&amp;</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&amp;</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="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="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="keyword">return</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span>
@@ -124,31 +115,30 @@ Namespaces are one honking great idea -- let's do more of those!
<p> <p>
Here's what's happening: Here's what's happening:
</p> </p>
<div class="orderedlist"><ol type="1"> <div class="orderedlist"><ol class="orderedlist" type="1">
<li> <li class="listitem">
<code class="literal">f</code> is called passing in a reference to <code class="literal">y</code> <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> and a pointer to <code class="literal">z</code>
</li> </li>
<li> <li class="listitem">
A reference to <code class="literal">y.x</code> is returned A reference to <code class="literal">y.x</code> is returned
</li> </li>
<li> <li class="listitem">
<code class="literal">y</code> is deleted. <code class="literal">x</code> is a dangling reference <code class="literal">y</code> is deleted. <code class="literal">x</code> is a dangling reference
</li> </li>
<li> <li class="listitem">
<code class="literal">x.some_method()</code> is called <code class="literal">x.some_method()</code> is called
</li> </li>
<li><span class="bold"><strong>BOOM!</strong></span></li> <li class="listitem">
<span class="bold"><strong>BOOM!</strong></span>
</li>
</ol></div> </ol></div>
<p> <p>
We could copy result into a new object: We could copy result into a new object:
</p> </p>
<p> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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>
</p> <span class="special">&gt;&gt;&gt;</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>
<pre class="programlisting"> <span class="number">3.14</span>
<span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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> </pre>
<p> <p>
This is not really our intent of our C++ interface. We've broken our promise This is not really our intent of our C++ interface. We've broken our promise
@@ -158,10 +148,7 @@ Namespaces are one honking great idea -- let's do more of those!
<p> <p>
Our problems do not end there. Suppose Y is implemented as follows: Our problems do not end there. Suppose Y is implemented as follows:
</p> </p>
<p> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">Y</span>
</p>
<pre class="programlisting">
<span class="keyword">struct</span> <span class="identifier">Y</span>
<span class="special">{</span> <span class="special">{</span>
<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 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 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">-&gt;</span><span class="identifier">value</span><span class="special">();</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">-&gt;</span><span class="identifier">value</span><span class="special">();</span> <span class="special">}</span>
@@ -171,16 +158,14 @@ Namespaces are one honking great idea -- let's do more of those!
Notice that the data member <code class="literal">z</code> is held by class Y using 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: a raw pointer. Now we have a potential dangling pointer problem inside Y:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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> <span class="special">&gt;&gt;&gt;</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> </pre>
<p> <p>
For reference, here's the implementation of <code class="literal">f</code> again: For reference, here's the implementation of <code class="literal">f</code> again:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">X</span><span class="special">&amp;</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&amp;</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="identifier">X</span><span class="special">&amp;</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">Y</span><span class="special">&amp;</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="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="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="keyword">return</span> <span class="identifier">y</span><span class="special">.</span><span class="identifier">x</span><span class="special">;</span>
@@ -189,30 +174,33 @@ Namespaces are one honking great idea -- let's do more of those!
<p> <p>
Here's what's happening: Here's what's happening:
</p> </p>
<div class="orderedlist"><ol type="1"> <div class="orderedlist"><ol class="orderedlist" type="1">
<li> <li class="listitem">
<code class="literal">f</code> is called passing in a reference to <code class="literal">y</code> <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> and a pointer to <code class="literal">z</code>
</li> </li>
<li> <li class="listitem">
A pointer to <code class="literal">z</code> is held by <code class="literal">y</code> A pointer to <code class="literal">z</code> is held by <code class="literal">y</code>
</li> </li>
<li> <li class="listitem">
A reference to <code class="literal">y.x</code> is returned A reference to <code class="literal">y.x</code> is returned
</li> </li>
<li> <li class="listitem">
<code class="literal">z</code> is deleted. <code class="literal">y.z</code> is a dangling pointer <code class="literal">z</code> is deleted. <code class="literal">y.z</code> is a dangling
</li> pointer
<li> </li>
<code class="literal">y.z_value()</code> is called <li class="listitem">
</li> <code class="literal">y.z_value()</code> is called
<li> </li>
<code class="literal">z-&gt;value()</code> is called <li class="listitem">
</li> <code class="literal">z-&gt;value()</code> is called
<li><span class="bold"><strong>BOOM!</strong></span></li> </li>
<li class="listitem">
<span class="bold"><strong>BOOM!</strong></span>
</li>
</ol></div> </ol></div>
<a name="call_policies.call_policies"></a><h3> <h3>
<a name="id2648560"></a> <a name="call_policies.call_policies"></a>
Call Policies Call Policies
</h3> </h3>
<p> <p>
@@ -220,8 +208,7 @@ Namespaces are one honking great idea -- let's do more of those!
In our example, <code class="literal">return_internal_reference</code> and <code class="literal">with_custodian_and_ward</code> In our example, <code class="literal">return_internal_reference</code> and <code class="literal">with_custodian_and_ward</code>
are our friends: are our friends:
</p> </p>
<pre class="programlisting"> <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="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">&lt;</span><span class="number">1</span><span class="special">,</span> <span class="identifier">return_internal_reference</span><span class="special">&lt;</span><span class="number">1</span><span class="special">,</span>
<span class="identifier">with_custodian_and_ward</span><span class="special">&lt;</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">&gt;</span> <span class="special">&gt;());</span> <span class="identifier">with_custodian_and_ward</span><span class="special">&lt;</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">&gt;</span> <span class="special">&gt;());</span>
</pre> </pre>
@@ -229,8 +216,7 @@ Namespaces are one honking great idea -- let's do more of those!
What are the <code class="literal">1</code> and <code class="literal">2</code> parameters, you What are the <code class="literal">1</code> and <code class="literal">2</code> parameters, you
ask? ask?
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">return_internal_reference</span><span class="special">&lt;</span><span class="number">1</span>
<span class="identifier">return_internal_reference</span><span class="special">&lt;</span><span class="number">1</span>
</pre> </pre>
<p> <p>
Informs Boost.Python that the first argument, in our case <code class="literal">Y&amp; Informs Boost.Python that the first argument, in our case <code class="literal">Y&amp;
@@ -239,8 +225,7 @@ Namespaces are one honking great idea -- let's do more of those!
In short: "return an internal reference <code class="literal">X&amp;</code> owned In short: "return an internal reference <code class="literal">X&amp;</code> owned
by the 1st argument <code class="literal">Y&amp; y</code>". by the 1st argument <code class="literal">Y&amp; y</code>".
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">with_custodian_and_ward</span><span class="special">&lt;</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">&gt;</span>
<span class="identifier">with_custodian_and_ward</span><span class="special">&lt;</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">&gt;</span>
</pre> </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
@@ -252,8 +237,7 @@ Namespaces are one honking great idea -- let's do more of those!
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: or more policies can be composed by chaining. Here's the general syntax:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">policy1</span><span class="special">&lt;</span><span class="identifier">args</span><span class="special">...,</span>
<span class="identifier">policy1</span><span class="special">&lt;</span><span class="identifier">args</span><span class="special">...,</span>
<span class="identifier">policy2</span><span class="special">&lt;</span><span class="identifier">args</span><span class="special">...,</span> <span class="identifier">policy2</span><span class="special">&lt;</span><span class="identifier">args</span><span class="special">...,</span>
<span class="identifier">policy3</span><span class="special">&lt;</span><span class="identifier">args</span><span class="special">...&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">policy3</span><span class="special">&lt;</span><span class="identifier">args</span><span class="special">...&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;</span>
</pre> </pre>
@@ -261,48 +245,54 @@ Namespaces are one honking great idea -- let's do more of those!
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>. these can be found <a href="../../../../v2/reference.html#models_of_call_policies" target="_top">here</a>.
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li> <li class="listitem">
<span class="bold"><strong>with_custodian_and_ward</strong></span><br> Ties lifetimes <span class="bold"><strong>with_custodian_and_ward</strong></span>: Ties lifetimes
of the arguments of the arguments
</li> </li>
<li> <li class="listitem">
<span class="bold"><strong>with_custodian_and_ward_postcall</strong></span><br> <span class="bold"><strong>with_custodian_and_ward_postcall</strong></span>: Ties
Ties lifetimes of the arguments and results lifetimes of the arguments and results
</li> </li>
<li> <li class="listitem">
<span class="bold"><strong>return_internal_reference</strong></span><br> Ties lifetime <span class="bold"><strong>return_internal_reference</strong></span>: Ties lifetime
of one argument to that of result of one argument to that of result
</li> </li>
<li> <li class="listitem">
<span class="bold"><strong>return_value_policy&lt;T&gt; with T one of:</strong></span><br> <span class="bold"><strong>return_value_policy&lt;T&gt; with T one of:</strong></span>
</li> <div class="itemizedlist"><ul class="itemizedlist" type="circle">
<li> <li class="listitem">
<span class="bold"><strong>reference_existing_object</strong></span><br> naive <span class="bold"><strong>reference_existing_object</strong></span>: naive
(dangerous) approach (dangerous) approach
</li> </li>
<li> <li class="listitem">
<span class="bold"><strong>copy_const_reference</strong></span><br> Boost.Python <span class="bold"><strong>copy_const_reference</strong></span>: Boost.Python
v1 approach v1 approach
</li> </li>
<li> <li class="listitem">
<span class="bold"><strong>copy_non_const_reference</strong></span><br> <span class="bold"><strong>copy_non_const_reference</strong></span>:
</li> </li>
<li> <li class="listitem">
<span class="bold"><strong>manage_new_object</strong></span><br> Adopt a pointer <span class="bold"><strong>manage_new_object</strong></span>: Adopt a pointer
and hold the instance and hold the instance
</li> </li>
</ul></div>
</li>
</ul></div> </ul></div>
<div class="sidebar"> <div class="sidebar">
<p class="title"><b></b></p> <div class="titlepage"></div>
<p> <p>
<span class="inlinemediaobject"><img src="../images/smiley.png" alt="smiley"></span> <span class="bold"><strong>Remember the Zen, Luke:</strong></span><br> <span class="inlinemediaobject"><img src="../images/smiley.png" alt="smiley"></span> <span class="bold"><strong>Remember the Zen, Luke:</strong></span>
<br> "Explicit is better than implicit"<br> "In the face </p>
of ambiguity, refuse the temptation to guess"<br> <p>
"Explicit is better than implicit"
</p>
<p>
"In the face of ambiguity, refuse the temptation to guess"
</p> </p>
</div> </div>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -313,8 +303,7 @@ Namespaces are one honking great idea -- let's do more of those!
<p> <p>
We have here our C++ class: We have here our C++ class:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">X</span>
<span class="keyword">struct</span> <span class="identifier">X</span>
<span class="special">{</span> <span class="special">{</span>
<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">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> <span class="special">{</span>
@@ -338,11 +327,10 @@ Namespaces are one honking great idea -- let's do more of those!
<span class="special">};</span> <span class="special">};</span>
</pre> </pre>
<p> <p>
Class X has 4 overloaded functions. We shall start by introducing some member Class X has 4 overloaded functions. We will start by introducing some member
function pointer variables: function pointer variables:
</p> </p>
<pre class="programlisting"> <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="special">=</span> <span class="special">&amp;</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">fx1</span><span class="special">)(</span><span class="keyword">int</span><span class="special">)</span> <span class="special">=</span> <span class="special">&amp;</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">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">&amp;</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">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">&amp;</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">&amp;</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">&amp;</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">&amp;</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">&amp;</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span>
@@ -350,14 +338,13 @@ Namespaces are one honking great idea -- let's do more of those!
<p> <p>
With these in hand, we can proceed to define and wrap this for Python: With these in hand, we can proceed to define and wrap this for Python:
</p> </p>
<pre class="programlisting"> <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="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">.</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">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">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> <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> </pre>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -365,60 +352,55 @@ Namespaces are one honking great idea -- let's do more of those!
pointers carry no default argument info. Take a function <code class="literal">f</code> pointers carry no default argument info. Take a function <code class="literal">f</code>
with default arguments: with default arguments:
</p> </p>
<pre class="programlisting"> <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>
<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> </pre>
<p> <p>
But the type of a pointer to the function <code class="literal">f</code> has no information But the type of a pointer to the function <code class="literal">f</code> has no information
about its default arguments: about its default arguments:
</p> </p>
<pre class="programlisting"> <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>
<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! </pre>
</span></pre>
<p> <p>
When we pass this function pointer to the <code class="literal">def</code> function, When we pass this function pointer to the <code class="literal">def</code> function,
there is no way to retrieve the default arguments: there is no way to retrieve the default arguments:
</p> </p>
<pre class="programlisting"> <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>
<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! </pre>
</span></pre>
<p> <p>
Because of this, when wrapping C++ code, we had to resort to manual wrapping Because of this, when wrapping C++ code, we had to resort to manual wrapping
as outlined in the <a href="functions.html#python.overloading" title="Overloading">previous section</a>, as outlined in the <a class="link" href="functions.html#python.overloading" title="Overloading">previous section</a>,
or writing thin wrappers: or writing thin wrappers:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="comment">// write "thin wrappers"</span>
<span class="comment">// write "thin wrappers" <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="keyword">return</span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span> <span class="special">}</span>
</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="keyword">return</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="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> <span class="comment">/*...*/</span>
<span class="comment">// in module init <span class="comment">// in module init</span>
</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 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> <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 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> <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 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></pre> </pre>
<p> <p>
When you want to wrap functions (or member functions) that either: When you want to wrap functions (or member functions) that either:
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li> <li class="listitem">
have default arguments, or have default arguments, or
</li> </li>
<li> <li class="listitem">
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><h3> <h3>
<a name="id2650414"></a> <a name="default_arguments.boost_python_function_overloads"></a>
BOOST_PYTHON_FUNCTION_OVERLOADS BOOST_PYTHON_FUNCTION_OVERLOADS
</h3> </h3>
<p> <p>
Boost.Python now has a way to make it easier. For instance, given a function: Boost.Python now has a way to make it easier. For instance, given a function:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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="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">{</span> <span class="special">{</span>
<span class="comment">/*...*/</span> <span class="comment">/*...*/</span>
<span class="special">}</span> <span class="special">}</span>
@@ -426,8 +408,7 @@ Namespaces are one honking great idea -- let's do more of those!
<p> <p>
The macro invocation: The macro invocation:
</p> </p>
<pre class="programlisting"> <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>
<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> </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
@@ -437,11 +418,10 @@ Namespaces are one honking great idea -- let's do more of those!
number of arguments is 1 and the maximum number of arguments is 4. The <code class="literal">def(...)</code> number of arguments is 1 and the maximum number of arguments is 4. The <code class="literal">def(...)</code>
function will automatically add all the foo variants for us: function will automatically add all the foo variants for us:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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>
<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> </pre>
<a name="default_arguments.boost_python_member_function_overloads"></a><h3> <h3>
<a name="id2650701"></a> <a name="default_arguments.boost_python_member_function_overloads"></a>
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS
</h3> </h3>
<p> <p>
@@ -456,8 +436,7 @@ Namespaces are one honking great idea -- let's do more of those!
may be used to automatically create the thin wrappers for wrapping member may be used to automatically create the thin wrappers for wrapping member
functions. Let's have an example: functions. Let's have an example:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">george</span>
<span class="keyword">struct</span> <span class="identifier">george</span>
<span class="special">{</span> <span class="special">{</span>
<span class="keyword">void</span> <span class="keyword">void</span>
<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 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>
@@ -469,8 +448,7 @@ Namespaces are one honking great idea -- let's do more of those!
<p> <p>
The macro invocation: The macro invocation:
</p> </p>
<pre class="programlisting"> <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>
<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> </pre>
<p> <p>
will generate a set of thin wrappers for george's <code class="literal">wack_em</code> will generate a set of thin wrappers for george's <code class="literal">wack_em</code>
@@ -479,15 +457,14 @@ Namespaces are one honking great idea -- let's do more of those!
in a class named <code class="literal">george_overloads</code> that can then be used in a class named <code class="literal">george_overloads</code> that can then be used
as an argument to <code class="literal">def(...)</code>: as an argument to <code class="literal">def(...)</code>:
</p> </p>
<pre class="programlisting"> <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">&amp;</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>
<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">&amp;</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> </pre>
<p> <p>
See the <a href="../../../../v2/overloads.html#BOOST_PYTHON_FUNCTION_OVERLOADS-spec" target="_top">overloads See the <a href="../../../../v2/overloads.html#BOOST_PYTHON_FUNCTION_OVERLOADS-spec" target="_top">overloads
reference</a> for details. reference</a> for details.
</p> </p>
<a name="default_arguments.init_and_optional"></a><h3> <h3>
<a name="id2651031"></a> <a name="default_arguments.init_and_optional"></a>
init and optional init and optional
</h3> </h3>
<p> <p>
@@ -495,8 +472,7 @@ Namespaces are one honking great idea -- let's do more of those!
arguments or a sequence of overloads. Remember <code class="literal">init&lt;...&gt;</code>? arguments or a sequence of overloads. Remember <code class="literal">init&lt;...&gt;</code>?
For example, given a class X with a constructor: For example, given a class X with a constructor:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">X</span>
<span class="keyword">struct</span> <span class="identifier">X</span>
<span class="special">{</span> <span class="special">{</span>
<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="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="comment">/*...*/</span>
@@ -505,15 +481,14 @@ Namespaces are one honking great idea -- let's do more of those!
<p> <p>
You can easily add this constructor to Boost.Python in one shot: You can easily add this constructor to Boost.Python in one shot:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">optional</span><span class="special">&lt;</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">&gt;</span> <span class="special">&gt;())</span>
<span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">optional</span><span class="special">&lt;</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">&gt;</span> <span class="special">&gt;())</span>
</pre> </pre>
<p> <p>
Notice the use of <code class="literal">init&lt;...&gt;</code> and <code class="literal">optional&lt;...&gt;</code> Notice the use of <code class="literal">init&lt;...&gt;</code> and <code class="literal">optional&lt;...&gt;</code>
to signify the default (optional arguments). to signify the default (optional arguments).
</p> </p>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -522,8 +497,7 @@ Namespaces are one honking great idea -- let's do more of those!
used for overloaded functions and member functions with a common sequence used for overloaded functions and member functions with a common sequence
of initial arguments. Here is an example: of initial arguments. Here is an example:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span>
<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span>
<span class="special">{</span> <span class="special">{</span>
<span class="comment">/*...*/</span> <span class="comment">/*...*/</span>
<span class="special">}</span> <span class="special">}</span>
@@ -547,66 +521,66 @@ Namespaces are one honking great idea -- let's do more of those!
Like in the previous section, we can generate thin wrappers for these overloaded Like in the previous section, we can generate thin wrappers for these overloaded
functions in one-shot: functions in one-shot:
</p> </p>
<pre class="programlisting"> <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>
<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> </pre>
<p> <p>
Then... Then...
</p> </p>
<pre class="programlisting"> <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>
<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> </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. (0) arguments and a maximum of 3 arguments.
</p> </p>
<a name="auto_overloading.manual_wrapping"></a><h3> <h3>
<a name="id2651734"></a> <a name="auto_overloading.manual_wrapping"></a>
Manual Wrapping Manual Wrapping
</h3> </h3>
<p> <p>
It is important to emphasize however that <span class="bold"><strong>the overloaded It is important to emphasize however that <span class="bold"><strong>the overloaded
functions must have a common sequence of initial arguments</strong></span>. Otherwise, functions must have a common sequence of initial arguments</strong></span>. Otherwise,
our scheme above will not work. If this is not the case, we have to wrap our scheme above will not work. If this is not the case, we have to wrap
our functions <a href="functions.html#python.overloading" title="Overloading">manually</a>. our functions <a class="link" href="functions.html#python.overloading" title="Overloading">manually</a>.
</p> </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 <code class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</code> automatic wrapping through <code class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</code>
and its sister, <code class="literal">BOOST_PYTHON_FUNCTION_OVERLOADS</code>. Following and its sister, <code class="literal">BOOST_PYTHON_FUNCTION_OVERLOADS</code>. Following
up on our example presented in the section <a href="functions.html#python.overloading" title="Overloading">on up on our example presented in the section <a class="link" href="functions.html#python.overloading" title="Overloading">on
overloading</a>, since the first 4 overload functins have a common sequence overloading</a>, since the first 4 overload functins have a common sequence
of initial arguments, we can use <code class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</code> of initial arguments, we can use <code class="literal">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</code>
to automatically wrap the first three of the <code class="literal">def</code>s and to automatically wrap the first three of the <code class="literal">def</code>s and
manually wrap just the last. Here's how we'll do this: manually wrap just the last. Here's how we'll do this:
</p> </p>
<pre class="programlisting"> <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>
<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> </pre>
<p> <p>
Create a member function pointers as above for both X::f overloads: Create a member function pointers as above for both X::f overloads:
</p> </p>
<pre class="programlisting"> <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">&amp;</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">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">&amp;</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">&amp;</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">&amp;</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">;</span>
</pre> </pre>
<p> <p>
Then... Then...
</p> </p>
<pre class="programlisting"> <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">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> <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> </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 <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005 Joel
de Guzman, David Abrahams</small></td> de Guzman, David Abrahams<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
http://www.boost.org/LICENSE_1_0.txt </a>)
</p>
</div></td>
</tr></table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="exposing.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="object.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="exposing.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="object.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -1,98 +1,89 @@
<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=US-ASCII">
<title>Building Hello World</title> <title>Building Hello World</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css"> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="start" href="../index.html" title="Chapter 1. python 1.0"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="up" href="../index.html" title="Chapter 1. python 1.0"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="prev" href="../index.html" title="Chapter 1. python 1.0"> <link rel="prev" href="../index.html" title="Chapter&#160;1.&#160;python 2.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%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" 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.html">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="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="../../../../../../../more/faq.htm">FAQ</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="../index.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="exposing.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="../index.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="exposing.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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><h3> <h3>
<a name="id2595436"></a> <a name="hello.from_start_to_finish"></a>
From Start To Finish From Start To Finish
</h3> </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 will outline the steps necessary
necessary to achieve that. We shall use the build tool that comes bundled with to achieve that. We will use the build tool that comes bundled with every boost
every boost distribution: <span class="bold"><strong>bjam</strong></span>. distribution: <span class="bold"><strong>bjam</strong></span>.
</p> </p>
<div class="sidebar"> <div class="note"><table border="0" summary="Note">
<p class="title"><b></b></p> <tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top">
<p> <p>
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> <span class="bold"><strong>Building without bjam</strong></span><br> <span class="bold"><strong>Building without bjam</strong></span>
<br> Besides bjam, there are of course other ways to get your module built. </p>
What's written here should not be taken as "the one and only way".
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
bjam. 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
who had to use a different tool.
</p>
</div>
<p> <p>
We shall skip over the details. Our objective will be to simply create the Besides bjam, there are of course other ways to get your module built. What's
hello world module and run it in Python. For a complete reference to building written here should not be taken as "the one and only way". There
Boost.Python, check out: <a href="../../../../building.html" target="_top">building.html</a>. are of course other build tools apart from <code class="literal">bjam</code>.
After this brief <span class="emphasis"><em>bjam</em></span> tutorial, we should have built two </p>
DLLs:
</p>
<div class="itemizedlist"><ul type="disc">
<li>
boost_python.dll
</li>
<li>
hello.pyd
</li>
</ul></div>
<p> <p>
if you are on Windows, and Take note however that the preferred build tool for Boost.Python is bjam.
</p> There are so many ways to set up the build incorrectly. Experience shows
<div class="itemizedlist"><ul type="disc"> that 90% of the "I can't build Boost.Python" problems come from
<li> people who had to use a different tool.
libboost_python.so </p>
</li> </td></tr>
<li> </table></div>
hello.so
</li>
</ul></div>
<p> <p>
if you are on Unix. We will 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 building 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 the DLLs
and run a python program using the extension.
</p> </p>
<p> <p>
The tutorial example can be found in the directory: <code class="literal">libs/python/example/tutorial</code>. The tutorial example can be found in the directory: <code class="literal">libs/python/example/tutorial</code>.
There, you can find: There, you can find:
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li> <li class="listitem">
hello.cpp hello.cpp
</li> </li>
<li> <li class="listitem">
Jamfile hello.py
</li> </li>
<li class="listitem">
Jamroot
</li>
</ul></div> </ul></div>
<p> <p>
The <code class="literal">hello.cpp</code> file is our C++ hello world example. The The <code class="literal">hello.cpp</code> file is our C++ hello world example. The
<code class="literal">Jamfile</code> is a minimalist <span class="emphasis"><em>bjam</em></span> script <code class="literal">Jamroot</code> is a minimalist <span class="emphasis"><em>bjam</em></span> script
that builds the DLLs for us. that builds the DLLs for us. Finally, <code class="literal">hello.py</code> is our Python
program that uses the extension in <code class="literal">hello.cpp</code>.
</p> </p>
<p> <p>
Before anything else, you should have the bjam executable in your boost directory Before anything else, you should have the bjam executable in your boost directory
@@ -100,206 +91,105 @@
in 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 <a href="http://sourceforge.net/project/showfiles.php?group_id=7586" target="_top">here</a>. 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>.
</p> </p>
<a name="hello.let_s_jam_"></a><h3> <h3>
<a name="id2595623"></a> <a name="hello.let_s_jam_"></a>
Let's Jam! Let's Jam!
</h3> </h3>
<p> <p>
<span class="inlinemediaobject"><img src="../images/jam.png" alt="jam"></span> <span class="inlinemediaobject"><img src="../images/jam.png" alt="jam"></span>
</p> </p>
<p> <p>
Here is our minimalist Jamfile: <a href="../../../../../example/tutorial/Jamroot" target="_top">Here</a> is our minimalist
</p> Jamroot file. Simply copy the file and tweak <code class="literal">use-project boost</code>
<pre class="programlisting"># This is the top of our own project tree to where your boost root directory is and your OK.
project-root ;
import python ;
extension hello # Declare a Python extension called hello
: hello.cpp # source
# requirements and dependencies for Boost.Python extensions
&lt;template&gt;@boost/libs/python/build/extension
;
</pre>
<p>
First, we need to specify our location. You may place your project anywhere.
<code class="literal">project-root</code> allows you to do that.
</p>
<pre class="programlisting">project-root ;
</pre>
<p>
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>
and tweak the <code class="literal">path-global BOOST_ROOT</code> to where your boost
root directory is. The file has <a href="../../../../../example/tutorial/Jamrules" target="_top">detailed
instructions</a> you can follow.
</p> </p>
<p> <p>
Then we will import the definitions needed by Python modules: The comments contained in the Jamrules file above should be sufficient to get
you going.
</p> </p>
<pre class="programlisting">import python ; <h3>
</pre> <a name="hello.running_bjam"></a>
<p>
Finally we declare our <code class="literal">hello</code> extension:
</p>
<pre class="programlisting">extension hello # Declare a Python extension called hello
: hello.cpp # source
# requirements and dependencies for Boost.Python extensions
&lt;template&gt;@boost/libs/python/build/extension
;
</pre>
<p>
The last part tells BJam that we are depending on the Boost Python Library.
</p>
<a name="hello.running_bjam"></a><h3>
<a name="id2595752"></a>
Running bjam Running bjam
</h3> </h3>
<p> <p>
<span class="emphasis"><em>bjam</em></span> is run using your operating system's command line <span class="emphasis"><em>bjam</em></span> is run using your operating system's command line
interpreter. interpreter.
</p> </p>
<div class="blockquote"><blockquote class="blockquote"> <div class="blockquote"><blockquote class="blockquote"><p>
Start it up.
</p></blockquote></div>
<p> <p>
</p> A file called user-config.jam in your home directory is used to configure your
<p> tools. In Windows, your home directory can be found by typing:
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> </p>
<pre class="programlisting">C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat <pre class="programlisting">ECHO %HOMEDRIVE%%HOMEPATH%
</pre> </pre>
<p> <p>
Some environment variables will have to be setup for proper building of our into a command prompt window. Your file should at least have the rules for
Python modules. Example: your compiler and your python installation. A specific example of this on Windows
would be:
</p> </p>
<pre class="programlisting">set PYTHON_ROOT=c:/dev/tools/python <pre class="programlisting"># MSVC configuration
set PYTHON_VERSION=2.2 using msvc : 8.0 ;
# Python configuration
using python : 2.4 : C:<span class="emphasis"><em>dev/tools/Python</em></span> ;
</pre> </pre>
<p> <p>
The above assumes that the Python installation is in <code class="literal">c:/dev/tools/python</code> The first rule tells Bjam to use the MSVC 8.0 compiler and associated tools.
and that we are using Python version 2.2. You'll have to tweak these appropriately. The second rule provides information on Python, its version and where it is
</p> located. The above assumes that the Python installation is in <code class="literal">C:<span class="emphasis"><em>dev/tools\/Python</em></span></code>.
<div class="sidebar"> If you have one fairly "standard" python installation for your platform,
<p class="title"><b></b></p> you might not need to do this.
<p>
<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
have.
</p>
</div>
<p>
Take note that you may also do that through the Jamrules file we put in our
project as detailed above. The file has <a href="../../../../../example/tutorial/Jamrules" target="_top">detailed
instructions</a> you can follow.
</p> </p>
<p> <p>
Now we are ready... Be sure to <code class="literal">cd</code> to <code class="literal">libs/python/example/tutorial</code> Now we are ready... Be sure to <code class="literal">cd</code> to <code class="literal">libs/python/example/tutorial</code>
where the tutorial <code class="literal">"hello.cpp"</code> and the <code class="literal">"Jamfile"</code> where the tutorial <code class="literal">"hello.cpp"</code> and the <code class="literal">"Jamroot"</code>
is situated. is situated.
</p> </p>
<p> <p>
Finally: Finally:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">bjam</span>
<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> </pre>
<p>
We are again assuming that we are using Microsoft Visual C++ version 7.1. If
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> <p>
It should be building now: It should be building now:
</p> </p>
<pre class="programlisting">cd C:\dev\boost\libs\python\example\tutorial <pre class="programlisting">cd C:\dev\boost\libs\python\example\tutorial
bjam -sTOOLS=msvc bjam
...patience... ...patience...
...found 1703 targets... ...found 1101 targets...
...updating 40 targets... ...updating 35 targets...
</pre> </pre>
<p> <p>
And so on... Finally: And so on... Finally:
</p> </p>
<pre class="programlisting">Creating library bin\boost\libs\python\build\boost_python.dll\vc-7_1\debug\th <pre class="programlisting">Creating library <span class="emphasis"><em>path-to-boost_python.dll</em></span>
reading-multi\boost_python.lib and object bin\boost\libs\python\build\boost_pyth Creating library /path-to-hello_ext.exp/
on.dll\vc-7_1\debug\threading-multi\boost_python.exp **passed** ... hello.test
vc-C++ bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.obj ...updated 35 targets...
hello.cpp
vc-Link bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.pyd bin\tutori
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
b and object bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.exp
...updated 31 targets...
</pre> </pre>
<p> <p>
If all is well, you should now have: Or something similar. If all is well, you should now have built the DLLs and
run the Python program.
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="blockquote"><blockquote class="blockquote"><p>
<li> <span class="bold"><strong>There you go... Have fun!</strong></span>
boost_python.dll </p></blockquote></div>
</li>
<li>
hello.pyd
</li>
</ul></div>
<p>
if you are on Windows, and
</p>
<div class="itemizedlist"><ul type="disc">
<li>
libboost_python.so
</li>
<li>
hello.so
</li>
</ul></div>
<p>
if you are on Unix.
</p>
<p>
<code class="literal">boost_python.dll</code> and <code class="literal">hello.pyd</code> can be
found somewhere in your project's <code class="literal">bin</code> directory. After a
successful build, you make it possible for the system to find boost_python.dll
or libboost_python.so (usually done with LD_LIBRARY_PATH, 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.)
</p>
<p>
You may now fire up Python and run our hello module:
</p>
<p>
</p>
<pre class="programlisting">
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">hello</span>
<span class="special">&gt;&gt;&gt;</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 <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005 Joel
de Guzman, David Abrahams</small></td> de Guzman, David Abrahams<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
http://www.boost.org/LICENSE_1_0.txt </a>)
</p>
</div></td>
</tr></table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="../index.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="exposing.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="../index.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="exposing.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -1,28 +1,28 @@
<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=US-ASCII">
<title>Iterators</title> <title>Iterators</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css"> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="start" href="../index.html" title="Chapter 1. python 1.0"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="up" href="../index.html" title="Chapter 1. python 1.0"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;python 2.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%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" 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.html">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="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="../../../../../../../more/faq.htm">FAQ</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="embedding.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="exception.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="embedding.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="exception.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -32,63 +32,56 @@
<p> <p>
<span class="bold"><strong>C++ iterators:</strong></span> <span class="bold"><strong>C++ iterators:</strong></span>
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li> <li class="listitem">
C++ has 5 type categories (random-access, bidirectional, forward, input, C++ has 5 type categories (random-access, bidirectional, forward, input,
output) output)
</li> </li>
<li> <li class="listitem">
There are 2 Operation categories: reposition, access There are 2 Operation categories: reposition, access
</li> </li>
<li> <li class="listitem">
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> <p>
<span class="bold"><strong>Python Iterators:</strong></span> <span class="bold"><strong>Python Iterators:</strong></span>
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li> <li class="listitem">
1 category (forward) 1 category (forward)
</li> </li>
<li> <li class="listitem">
1 operation category (next()) 1 operation category (next())
</li> </li>
<li> <li class="listitem">
Raises StopIteration exception at end Raises StopIteration exception at end
</li> </li>
</ul></div> </ul></div>
<p> <p>
The typical Python iteration protocol: <code class="literal"><span class="bold"><strong>for y The typical Python iteration protocol: <code class="literal"><span class="bold"><strong>for y
in x...</strong></span></code> is as follows: in x...</strong></span></code> is as follows:
</p> </p>
<p> <pre class="programlisting"><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</span>
</p> <span class="keyword">try</span><span class="special">:</span>
<pre class="programlisting">
<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
</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="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 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> <span class="special">...</span> <span class="comment"># process y <span class="special">...</span> <span class="comment"># process y</span>
</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 class="keyword">except</span> <span class="identifier">StopIteration</span><span class="special">:</span> <span class="keyword">pass</span> <span class="comment"># iterator exhausted</span>
</span></pre> </pre>
<p> <p>
Boost.Python provides some mechanisms to make C++ iterators play along nicely Boost.Python provides some mechanisms to make C++ iterators play along nicely
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 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
is compatible with the Python iteration protocol. For example: is compatible with the Python iteration protocol. For example:
</p> </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">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;();</span>
</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">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;();</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">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> <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> </pre>
<p> <p>
Or for use in class_&lt;&gt;: Or for use in class_&lt;&gt;:
</p> </p>
<pre class="programlisting"> <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">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;())</span>
<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">&lt;</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;())</span>
</pre> </pre>
<p> <p>
<span class="bold"><strong>range</strong></span> <span class="bold"><strong>range</strong></span>
@@ -96,34 +89,34 @@
<p> <p>
We can create a Python savvy iterator using the range function: We can create a Python savvy iterator using the range function:
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li> <li class="listitem">
range(start, finish) range(start, finish)
</li> </li>
<li> <li class="listitem">
range&lt;Policies,Target&gt;(start, finish) range&lt;Policies,Target&gt;(start, finish)
</li> </li>
</ul></div> </ul></div>
<p> <p>
Here, start/finish may be one of: Here, start/finish may be one of:
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li> <li class="listitem">
member data pointers member data pointers
</li> </li>
<li> <li class="listitem">
member function pointers member function pointers
</li> </li>
<li> <li class="listitem">
adaptable function object (use Target parameter) adaptable function object (use Target parameter)
</li> </li>
</ul></div> </ul></div>
<p> <p>
<span class="bold"><strong>iterator</strong></span> <span class="bold"><strong>iterator</strong></span>
</p> </p>
<div class="itemizedlist"><ul type="disc"><li> <div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
iterator&lt;T, Policies&gt;() iterator&lt;T, Policies&gt;()
</li></ul></div> </li></ul></div>
<p> <p>
Given a container <code class="literal">T</code>, iterator is a shortcut that simply Given a container <code class="literal">T</code>, iterator is a shortcut that simply
calls <code class="literal">range</code> with &amp;T::begin, &amp;T::end. calls <code class="literal">range</code> with &amp;T::begin, &amp;T::end.
@@ -132,10 +125,7 @@
Let's put this into action... Here's an example from some hypothetical bogon Let's put this into action... Here's an example from some hypothetical bogon
Particle accelerator code: Particle accelerator code:
</p> </p>
<p> <pre class="programlisting"><span class="identifier">f</span> <span class="special">=</span> <span class="identifier">Field</span><span class="special">()</span>
</p>
<pre class="programlisting">
<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="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="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="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>
@@ -144,10 +134,7 @@
<p> <p>
Now, our C++ Wrapper: Now, our C++ Wrapper:
</p> </p>
<p> <pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">F</span><span class="special">&gt;(</span><span class="string">"Field"</span><span class="special">)</span>
</p>
<pre class="programlisting">
<span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">F</span><span class="special">&gt;(</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">(&amp;</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">p_begin</span><span class="special">,</span> <span class="special">&amp;</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">"pions"</span><span class="special">,</span> <span class="identifier">range</span><span class="special">(&amp;</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">p_begin</span><span class="special">,</span> <span class="special">&amp;</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">(&amp;</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">b_begin</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">b_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">(&amp;</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">b_begin</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">F</span><span class="special">::</span><span class="identifier">b_end</span><span class="special">));</span>
</pre> </pre>
@@ -161,41 +148,40 @@
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">&lt;&gt;</span></code>. 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">&lt;&gt;</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">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">assign</span><span class="special">()</span></code> to Python: 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">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</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">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
</p>
<pre class="programlisting">
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</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">&lt;</span><span class="identifier">T</span><span class="special">&gt;&amp;</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="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">&lt;</span><span class="identifier">T</span><span class="special">&gt;&amp;</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 class="comment">// Turn a Python sequence into an STL input range</span>
</span> <span class="identifier">stl_input_iterator</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</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">stl_input_iterator</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</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="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="special">}</span>
<span class="comment">// Part of the wrapper for list&lt;int&gt; <span class="comment">// Part of the wrapper for list&lt;int&gt;</span>
</span><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;(</span><span class="string">"list_int"</span><span class="special">)</span> <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;(</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">&amp;</span><span class="identifier">list_assign</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;)</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">&amp;</span><span class="identifier">list_assign</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;)</span>
<span class="comment">// ... <span class="comment">// ...</span>
</span> <span class="special">;</span> <span class="special">;</span>
</pre> </pre>
<p> <p>
Now in Python, we can assign any integer sequence to <code class="computeroutput"><span class="identifier">list_int</span></code> Now in Python, we can assign any integer sequence to <code class="computeroutput"><span class="identifier">list_int</span></code>
objects: 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>
</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> <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> </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 <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005 Joel
de Guzman, David Abrahams</small></td> de Guzman, David Abrahams<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
http://www.boost.org/LICENSE_1_0.txt </a>)
</p>
</div></td>
</tr></table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="embedding.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="exception.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="embedding.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="exception.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -1,41 +1,42 @@
<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=US-ASCII">
<title>Object Interface</title> <title>Object Interface</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css"> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="start" href="../index.html" title="Chapter 1. python 1.0"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="up" href="../index.html" title="Chapter 1. python 1.0"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="prev" href="functions.html" title="Functions"> <link rel="prev" href="functions.html" title="Functions">
<link rel="next" href="embedding.html" title="Embedding"> <link rel="next" href="embedding.html" title="Embedding">
</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%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" 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.html">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="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="../../../../../../../more/faq.htm">FAQ</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="functions.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="embedding.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="functions.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="embedding.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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.object"></a> Object Interface</h2></div></div></div> <a name="python.object"></a>Object Interface</h2></div></div></div>
<div class="toc"><dl> <div class="toc"><dl>
<dt><span class="section"><a href="object.html#python.basic_interface">Basic Interface</a></span></dt> <dt><span class="section"><a href="object.html#python.basic_interface">Basic Interface</a></span></dt>
<dt><span class="section"><a href="object.html#python.derived_object_types">Derived Object types</a></span></dt> <dt><span class="section"><a href="object.html#python.derived_object_types">Derived Object types</a></span></dt>
<dt><span class="section"><a href="object.html#python.extracting_c___objects">Extracting C++ objects</a></span></dt> <dt><span class="section"><a href="object.html#python.extracting_c___objects">Extracting C++ objects</a></span></dt>
<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>
<dt><span class="section"><a href="object.html#python.creating_python_object">Creating <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span></code> from <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span></code></a></span></dt>
</dl></div> </dl></div>
<p> <p>
Python is dynamically typed, unlike C++ which is statically typed. Python variables Python is dynamically typed, unlike C++ which is statically typed. Python variables
may hold an integer, a float, list, dict, tuple, str, long etc., among other may hold an integer, a float, list, dict, tuple, str, long etc., among other
things. In the viewpoint of Boost.Python and C++, these Pythonic variables things. In the viewpoint of Boost.Python and C++, these Pythonic variables
are just instances of class <code class="literal">object</code>. We shall see in this are just instances of class <code class="literal">object</code>. We will see in this
chapter how to deal with Python objects. chapter how to deal with Python objects.
</p> </p>
<p> <p>
@@ -47,7 +48,7 @@
<p> <p>
<span class="inlinemediaobject"><img src="../images/python.png" alt="python"></span> <span class="inlinemediaobject"><img src="../images/python.png" alt="python"></span>
</p> </p>
<div class="section" lang="en"> <div class="section">
<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>
@@ -60,10 +61,7 @@
<p> <p>
To illustrate, this Python code snippet: To illustrate, this Python code snippet:
</p> </p>
<p> <pre class="programlisting"><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>
</p>
<pre class="programlisting">
<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">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="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="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="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="keyword">else</span><span class="special">:</span>
@@ -76,10 +74,7 @@
<p> <p>
Can be rewritten in C++ using Boost.Python facilities this way: Can be rewritten in C++ using Boost.Python facilities this way:
</p> </p>
<p> <pre class="programlisting"><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>
</p>
<pre class="programlisting">
<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="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="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="keyword">else</span>
@@ -95,54 +90,51 @@
in C++, the look and feel should be immediately apparent to the Python coder. in C++, the look and feel should be immediately apparent to the Python coder.
</p> </p>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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 <code class="literal">object</code> types Boost.Python comes with a set of derived <code class="literal">object</code> types
corresponding to that of Python's: corresponding to that of Python's:
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li> <li class="listitem">
list list
</li> </li>
<li> <li class="listitem">
dict dict
</li> </li>
<li> <li class="listitem">
tuple tuple
</li> </li>
<li> <li class="listitem">
str str
</li> </li>
<li> <li class="listitem">
long_ long_
</li> </li>
<li> <li class="listitem">
enum enum
</li> </li>
</ul></div> </ul></div>
<p> <p>
These derived <code class="literal">object</code> types act like real Python types. These derived <code class="literal">object</code> types act like real Python types.
For instance: For instance:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">str</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">==&gt;</span> <span class="string">"1"</span>
<span class="identifier">str</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">==&gt;</span> <span class="string">"1"</span>
</pre> </pre>
<p> <p>
Wherever appropriate, a particular derived <code class="literal">object</code> has Wherever appropriate, a particular derived <code class="literal">object</code> has
corresponding Python type's methods. For instance, <code class="literal">dict</code> corresponding Python type's methods. For instance, <code class="literal">dict</code>
has a <code class="literal">keys()</code> method: has a <code class="literal">keys()</code> method:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">d</span><span class="special">.</span><span class="identifier">keys</span><span class="special">()</span>
<span class="identifier">d</span><span class="special">.</span><span class="identifier">keys</span><span class="special">()</span>
</pre> </pre>
<p> <p>
<code class="literal">make_tuple</code> is provided for declaring <span class="emphasis"><em>tuple literals</em></span>. <code class="literal">make_tuple</code> is provided for declaring <span class="emphasis"><em>tuple literals</em></span>.
Example: Example:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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 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>
</pre> </pre>
<p> <p>
In C++, when Boost.Python <code class="literal">object</code>s are used as arguments In C++, when Boost.Python <code class="literal">object</code>s are used as arguments
@@ -150,26 +142,23 @@
<code class="literal">f</code>, as declared below, is wrapped, it will only accept <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. instances of Python's <code class="literal">str</code> type and subtypes.
</p> </p>
<pre class="programlisting"> <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="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="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 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> <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 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> <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="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> <span class="special">}</span>
</pre> </pre>
<p> <p>
In finer detail: In finer detail:
</p> </p>
<pre class="programlisting"> <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>
<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> </pre>
<p> <p>
Illustrates that we provide versions of the str type's methods as C++ member Illustrates that we provide versions of the str type's methods as C++ member
functions. functions.
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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="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>
</pre> </pre>
<p> <p>
Demonstrates that you can write the C++ equivalent of <code class="literal">"format" Demonstrates that you can write the C++ equivalent of <code class="literal">"format"
@@ -177,7 +166,7 @@
do that in std C++. do that in std C++.
</p> </p>
<div class="sidebar"> <div class="sidebar">
<p class="title"><b></b></p> <div class="titlepage"></div>
<p> <p>
<span class="inlinemediaobject"><img src="../images/alert.png" alt="alert"></span> <span class="bold"><strong>Beware</strong></span> the common pitfall <span class="inlinemediaobject"><img src="../images/alert.png" alt="alert"></span> <span class="bold"><strong>Beware</strong></span> the common pitfall
of forgetting that the constructors of most of Python's mutable types make of forgetting that the constructors of most of Python's mutable types make
@@ -187,19 +176,17 @@
<p> <p>
Python: Python:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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 class="special">&gt;&gt;&gt;</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>
</span><span class="special">&gt;&gt;&gt;</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 </pre>
</span></pre>
<p> <p>
C++: C++:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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">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 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><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 </pre>
</span></pre> <h3>
<a name="derived_object_types.class__lt_t_gt__as_objects"></a><h3> <a name="derived_object_types.class__lt_t_gt__as_objects"></a>
<a name="id2653534"></a>
class_&lt;T&gt; as objects class_&lt;T&gt; as objects
</h3> </h3>
<p> <p>
@@ -210,8 +197,7 @@
<p> <p>
We can use this to create wrapped instances. Example: We can use this to create wrapped instances. Example:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">object</span> <span class="identifier">vec345</span> <span class="special">=</span> <span class="special">(</span>
<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">&lt;</span><span class="identifier">Vec2</span><span class="special">&gt;(</span><span class="string">"Vec2"</span><span class="special">,</span> <span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;())</span> <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Vec2</span><span class="special">&gt;(</span><span class="string">"Vec2"</span><span class="special">,</span> <span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;())</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">&amp;</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">"length"</span><span class="special">,</span> <span class="special">&amp;</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">&amp;</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">def_readonly</span><span class="special">(</span><span class="string">"angle"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">Point</span><span class="special">::</span><span class="identifier">angle</span><span class="special">)</span>
@@ -220,7 +206,7 @@
<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> <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> </pre>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -228,16 +214,14 @@
can be achieved with the <code class="literal">extract&lt;T&gt;</code> function. Consider can be achieved with the <code class="literal">extract&lt;T&gt;</code> function. Consider
the following: the following:
</p> </p>
<pre class="programlisting"> <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>
<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 </pre>
</span></pre>
<p> <p>
In the code above, we got a compiler error because Boost.Python <code class="literal">object</code> In the code above, we got a compiler error because Boost.Python <code class="literal">object</code>
can't be implicitly converted to <code class="literal">double</code>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: we wanted to do above can be achieved by writing:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">double</span> <span class="identifier">l</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;(</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="keyword">double</span> <span class="identifier">l</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;(</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">&amp;</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">Vec2</span><span class="special">&amp;&gt;(</span><span class="identifier">o</span><span class="special">);</span> <span class="identifier">Vec2</span><span class="special">&amp;</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">Vec2</span><span class="special">&amp;&gt;(</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> <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> </pre>
@@ -254,8 +238,7 @@
be extracted, an appropriate exception is thrown. To avoid an exception, be extracted, an appropriate exception is thrown. To avoid an exception,
we need to test for extractibility: we need to test for extractibility:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">Vec2</span><span class="special">&amp;&gt;</span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">o</span><span class="special">);</span>
<span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">Vec2</span><span class="special">&amp;&gt;</span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">o</span><span class="special">);</span>
<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> <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>
<span class="identifier">Vec2</span><span class="special">&amp;</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">();</span> <span class="special">...</span> <span class="identifier">Vec2</span><span class="special">&amp;</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">x</span><span class="special">();</span> <span class="special">...</span>
</pre> </pre>
@@ -263,12 +246,11 @@
<span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> The astute reader might have noticed that the <code class="literal">extract&lt;T&gt;</code> <span class="inlinemediaobject"><img src="../images/tip.png" alt="tip"></span> The astute reader might have noticed that the <code class="literal">extract&lt;T&gt;</code>
facility in fact solves the mutable copying problem: facility in fact solves the mutable copying problem:
</p> </p>
<pre class="programlisting"> <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">&lt;</span><span class="identifier">dict</span><span class="special">&gt;(</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">dict</span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">dict</span><span class="special">&gt;(</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>
<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__ ! </pre>
</span></pre>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -279,14 +261,12 @@
typing to C++'s strong static typing (in C++, ints cannot be implicitly converted typing to C++'s strong static typing (in C++, ints cannot be implicitly converted
to enums). To illustrate, given a C++ enum: to enums). To illustrate, given a C++ enum:
</p> </p>
<pre class="programlisting"> <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>
<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> </pre>
<p> <p>
the construct: the construct:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">enum_</span><span class="special">&lt;</span><span class="identifier">choice</span><span class="special">&gt;(</span><span class="string">"choice"</span><span class="special">)</span>
<span class="identifier">enum_</span><span class="special">&lt;</span><span class="identifier">choice</span><span class="special">&gt;(</span><span class="string">"choice"</span><span class="special">)</span>
<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">"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><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> <span class="special">;</span>
@@ -297,52 +277,84 @@
above creates a Python class derived from Python's <code class="literal">int</code> above creates a Python class derived from Python's <code class="literal">int</code>
type which is associated with the C++ type passed as its first parameter. type which is associated with the C++ type passed as its first parameter.
</p> </p>
<div class="sidebar"> <div class="note"><table border="0" summary="Note">
<p class="title"><b></b></p> <tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top">
<p> <p>
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> <span class="bold"><strong>what is a scope?</strong></span><br> <span class="bold"><strong>what is a scope?</strong></span>
<br> The scope is a class that has an associated global Python object which </p>
controls the Python namespace in which new extension classes and wrapped <p>
functions will be defined as attributes. Details can be found <a href="../../../../v2/scope.html" target="_top">here</a>. The scope is a class that has an associated global Python object which
</p> controls the Python namespace in which new extension classes and wrapped
</div> functions will be defined as attributes. Details can be found <a href="../../../../v2/scope.html" target="_top">here</a>.
</p>
</td></tr>
</table></div>
<p> <p>
You can access those values in Python as You can access those values in Python as
</p> </p>
<p> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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>
</p>
<pre class="programlisting">
<span class="special">&gt;&gt;&gt;</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">my_module</span><span class="special">.</span><span class="identifier">choice</span><span class="special">.</span><span class="identifier">red</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>
</pre> </pre>
<p> <p>
where my_module is the module where the enum is declared. You can also create where my_module is the module where the enum is declared. You can also create
a new scope around a class: 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">&lt;</span><span class="identifier">X</span><span class="special">&gt;(</span><span class="string">"X"</span><span class="special">)</span>
</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">&lt;</span><span class="identifier">X</span><span class="special">&gt;(</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><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> <span class="special">;</span>
<span class="comment">// Expose X::nested as X.nested <span class="comment">// Expose X::nested as X.nested</span>
</span><span class="identifier">enum_</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">nested</span><span class="special">&gt;(</span><span class="string">"nested"</span><span class="special">)</span> <span class="identifier">enum_</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">nested</span><span class="special">&gt;(</span><span class="string">"nested"</span><span class="special">)</span>
<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">"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><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> <span class="special">;</span>
</pre> </pre>
</div> </div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="python.creating_python_object"></a>Creating <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span></code> from <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span></code>
</h3></div></div></div>
<p>
When you want a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span></code> to manage a pointer to <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span></code>
pyobj one does:
</p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">o</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">handle</span><span class="special">&lt;&gt;(</span><span class="identifier">pyobj</span><span class="special">));</span>
</pre>
<p>
In this case, the <code class="computeroutput"><span class="identifier">o</span></code> object,
manages the <code class="computeroutput"><span class="identifier">pyobj</span></code>, it won&#8217;t
increase the reference count on construction.
</p>
<p>
Otherwise, to use a borrowed reference:
</p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">object</span> <span class="identifier">o</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">handle</span><span class="special">&lt;&gt;(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">borrowed</span><span class="special">(</span><span class="identifier">pyobj</span><span class="special">)));</span>
</pre>
<p>
In this case, <code class="computeroutput"><span class="identifier">Py_INCREF</span></code> is
called, so <code class="computeroutput"><span class="identifier">pyobj</span></code> is not destructed
when object o goes out of scope.
</p>
</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 <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005 Joel
de Guzman, David Abrahams</small></td> de Guzman, David Abrahams<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
http://www.boost.org/LICENSE_1_0.txt </a>)
</p>
</div></td>
</tr></table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="functions.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="embedding.html"><img src="../images/next.png" alt="Next"></a> <a accesskey="p" href="functions.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="embedding.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -1,29 +1,29 @@
<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=US-ASCII">
<title>General Techniques</title> <title>General Techniques</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css"> <link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="start" href="../index.html" title="Chapter 1. python 1.0"> <link rel="home" href="../index.html" title="Chapter&#160;1.&#160;python 2.0">
<link rel="up" href="../index.html" title="Chapter 1. python 1.0"> <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;python 2.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%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" 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.html">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="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="../../../../../../../more/faq.htm">FAQ</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> <td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="exception.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a> <a accesskey="p" href="exception.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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.techniques"></a> General Techniques</h2></div></div></div> <a name="python.techniques"></a>General Techniques</h2></div></div></div>
<div class="toc"><dl> <div class="toc"><dl>
<dt><span class="section"><a href="techniques.html#python.creating_packages">Creating Packages</a></span></dt> <dt><span class="section"><a href="techniques.html#python.creating_packages">Creating Packages</a></span></dt>
<dt><span class="section"><a href="techniques.html#python.extending_wrapped_objects_in_python">Extending Wrapped Objects in Python</a></span></dt> <dt><span class="section"><a href="techniques.html#python.extending_wrapped_objects_in_python">Extending Wrapped Objects in Python</a></span></dt>
@@ -33,7 +33,7 @@
Here are presented some useful techniques that you can use while wrapping code Here are presented some useful techniques that you can use while wrapping code
with Boost.Python. with Boost.Python.
</p> </p>
<div class="section" lang="en"> <div class="section">
<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>
@@ -53,8 +53,7 @@
<code class="literal">sounds</code>. Our library already has a neat C++ namespace hierarchy, <code class="literal">sounds</code>. Our library already has a neat C++ namespace hierarchy,
like so: like so:
</p> </p>
<pre class="programlisting"> <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">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">io</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>
</pre> </pre>
@@ -62,16 +61,14 @@
We would like to present this same hierarchy to the Python user, allowing We would like to present this same hierarchy to the Python user, allowing
him to write code like this: him to write code like this:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
<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>
<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 </pre>
</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: separately with Boost.Python, like this:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><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>
<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>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">core</span><span class="special">)</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="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="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>
@@ -96,15 +93,18 @@
Compiling these files will generate the following Python extensions: <code class="literal">core.pyd</code>, Compiling these files will generate the following Python extensions: <code class="literal">core.pyd</code>,
<code class="literal">io.pyd</code> and <code class="literal">filters.pyd</code>. <code class="literal">io.pyd</code> and <code class="literal">filters.pyd</code>.
</p> </p>
<div class="sidebar"> <div class="note"><table border="0" summary="Note">
<p class="title"><b></b></p> <tr>
<p> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> The extension <code class="literal">.pyd</code> is used for python <th align="left">Note</th>
extension modules, which are just shared libraries. Using the default for </tr>
your system, like <code class="literal">.so</code> for Unix and <code class="literal">.dll</code> <tr><td align="left" valign="top"><p>
for Windows, works just as well. The extension <code class="literal">.pyd</code> is used for python extension modules,
</p> which are just shared libraries. Using the default for your system, like
</div> <code class="literal">.so</code> for Unix and <code class="literal">.dll</code> for Windows,
works just as well.
</p></td></tr>
</table></div>
<p> <p>
Now, we create this directory structure for our Python package: Now, we create this directory structure for our Python package:
</p> </p>
@@ -124,10 +124,7 @@
into his <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: and fire up the interpreter:
</p> </p>
<p> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">io</span>
</p>
<pre class="programlisting">
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">io</span>
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span> <span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
<span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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> <span class="special">&gt;&gt;&gt;</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>
@@ -147,10 +144,7 @@
If we want this flexibility, we will have to complicate our package hierarchy If we want this flexibility, we will have to complicate our package hierarchy
a little. First, we will have to change the name of the extension modules: a little. First, we will have to change the name of the extension modules:
</p> </p>
<p> <pre class="programlisting"><span class="comment">/* file core.cpp */</span>
</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="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="special">...</span> <span class="special">...</span>
@@ -180,10 +174,7 @@
to each one. But if we leave it that way, the user will have to access the to each one. But if we leave it that way, the user will have to access the
functions in the core module with this syntax: functions in the core module with this syntax:
</p> </p>
<p> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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>
</p>
<pre class="programlisting">
<span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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> <span class="special">&gt;&gt;&gt;</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> </pre>
<p> <p>
@@ -191,17 +182,15 @@
magic: everything that is brought to the <code class="literal">__init__.py</code> namespace magic: everything that is brought to the <code class="literal">__init__.py</code> namespace
can be accessed directly by the user. So, all we have to do is bring the can be accessed directly by the user. So, all we have to do is bring the
entire namespace from <code class="literal">_core.pyd</code> to <code class="literal">core/__init__.py</code>. 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>: So add this line of code to <code class="literal">sounds/core/__init__.py</code>:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">from</span> <span class="identifier">_core</span> <span class="keyword">import</span> <span class="special">*</span>
<span class="keyword">from</span> <span class="identifier">_core</span> <span class="keyword">import</span> <span class="special">*</span>
</pre> </pre>
<p> <p>
We do the same for the other packages. Now the user accesses the functions We do the same for the other packages. Now the user accesses the functions
and classes in the extension modules like before: and classes in the extension modules like before:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
<span class="special">&gt;&gt;&gt;</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="special">&gt;&gt;&gt;</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> </pre>
<p> <p>
@@ -214,43 +203,39 @@
a file named <code class="literal">sounds/filters/echo_noise.py</code> and code our a file named <code class="literal">sounds/filters/echo_noise.py</code> and code our
function: function:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">import</span> <span class="identifier">_filters</span>
<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="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">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="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> <span class="keyword">return</span> <span class="identifier">s</span>
</pre> </pre>
<p> <p>
Next, we add this line to <code class="literal">sounds<span class="emphasis"><em>filters</em></span>__init__.py</code>: Next, we add this line to <code class="literal">sounds/filters/__init__.py</code>:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="keyword">from</span> <span class="identifier">echo_noise</span> <span class="keyword">import</span> <span class="identifier">echo_noise</span>
<span class="keyword">from</span> <span class="identifier">echo_noise</span> <span class="keyword">import</span> <span class="identifier">echo_noise</span>
</pre> </pre>
<p> <p>
And that's it. The user now accesses this function like any other function And that's it. The user now accesses this function like any other function
from the <code class="literal">filters</code> package: from the <code class="literal">filters</code> package:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">import</span> <span class="identifier">sounds</span><span class="special">.</span><span class="identifier">filters</span>
<span class="special">&gt;&gt;&gt;</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> <span class="special">&gt;&gt;&gt;</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> </pre>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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: even after it was already created:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="special">&gt;&gt;&gt;</span>
<span class="special">&gt;&gt;&gt;</span> <span class="comment"># a regular function <span class="special">&gt;&gt;&gt;</span> <span class="comment"># a regular function</span>
</span><span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="special">&gt;&gt;&gt;</span>
<span class="special">&gt;&gt;&gt;</span> <span class="comment"># now we turn it in a member function <span class="special">&gt;&gt;&gt;</span> <span class="comment"># now we turn it in a member function</span>
</span><span class="special">&gt;&gt;&gt;</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">&gt;&gt;&gt;</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">&gt;&gt;&gt;</span> <span class="special">&gt;&gt;&gt;</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">C</span><span class="special">()</span> <span class="special">&gt;&gt;&gt;</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">C</span><span class="special">()</span>
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">print</span> <span class="identifier">c</span> <span class="special">&gt;&gt;&gt;</span> <span class="keyword">print</span> <span class="identifier">c</span>
@@ -265,10 +250,7 @@
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 <code class="literal">point</code> in C++: we have a class <code class="literal">point</code> in C++:
</p> </p>
<p> <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">point</span> <span class="special">{...};</span>
</p>
<pre class="programlisting">
<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="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_geom</span><span class="special">)</span>
<span class="special">{</span> <span class="special">{</span>
@@ -276,48 +258,44 @@
<span class="special">}</span> <span class="special">}</span>
</pre> </pre>
<p> <p>
If we are using the technique from the previous session, <a href="techniques.html#python.creating_packages" title="Creating Packages">Creating If we are using the technique from the previous session, <a class="link" href="techniques.html#python.creating_packages" title="Creating Packages">Creating
Packages</a>, we can code directly into <code class="literal">geom/__init__.py</code>: 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>
</p>
<pre class="programlisting">
<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="comment"># a regular function</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">def</span> <span class="identifier">point_str</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
<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> <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>
<span class="comment"># now we turn it into a member function <span class="comment"># now we turn it into a member function</span>
</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> <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> </pre>
<p> <p>
<span class="bold"><strong>All</strong></span> point instances created from C++ will <span class="bold"><strong>All</strong></span> point instances created from C++ will
also have this member function! This technique has several advantages: also have this member function! This technique has several advantages:
</p> </p>
<div class="itemizedlist"><ul type="disc"> <div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li> <li class="listitem">
Cut down compile times to zero for these additional functions Cut down compile times to zero for these additional functions
</li> </li>
<li> <li class="listitem">
Reduce the memory footprint to virtually zero Reduce the memory footprint to virtually zero
</li> </li>
<li> <li class="listitem">
Minimize the need to recompile Minimize the need to recompile
</li> </li>
<li> <li class="listitem">
Rapid prototyping (you can move the code to C++ if required without changing Rapid prototyping (you can move the code to C++ if required without changing
the interface) the interface)
</li> </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. create a special metaclass that "injects" methods in other classes.
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="comment"># The one Boost.Python uses for all wrapped classes.</span>
<span class="comment"># The one Boost.Python uses for all wrapped classes. <span class="comment"># You can use here any class exported by Boost instead of "point"</span>
</span><span class="comment"># You can use here any class exported by Boost instead of "point" <span class="identifier">BoostPythonMetaclass</span> <span class="special">=</span> <span class="identifier">point</span><span class="special">.</span><span class="identifier">__class__</span>
</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">class</span> <span class="identifier">injector</span><span class="special">(</span><span class="identifier">object</span><span class="special">):</span> <span class="keyword">class</span> <span class="identifier">injector</span><span class="special">(</span><span class="identifier">object</span><span class="special">):</span>
<span class="keyword">class</span> <span class="identifier">__metaclass__</span><span class="special">(</span><span class="identifier">BoostPythonMetaclass</span><span class="special">):</span> <span class="keyword">class</span> <span class="identifier">__metaclass__</span><span class="special">(</span><span class="identifier">BoostPythonMetaclass</span><span class="special">):</span>
@@ -328,8 +306,8 @@
<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> <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>
<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> <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>
<span class="comment"># inject some methods in the point foo <span class="comment"># inject some methods in the point foo</span>
</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">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">def</span> <span class="identifier">__repr__</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span> <span class="keyword">def</span> <span class="identifier">__repr__</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
<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> <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>
<span class="keyword">def</span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span> <span class="keyword">def</span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
@@ -338,8 +316,7 @@
<p> <p>
Now let's see how it got: Now let's see how it got:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">print</span> <span class="identifier">point</span><span class="special">()</span>
<span class="special">&gt;&gt;&gt;</span> <span class="keyword">print</span> <span class="identifier">point</span><span class="special">()</span>
<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="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">&gt;&gt;&gt;</span> <span class="identifier">point</span><span class="special">().</span><span class="identifier">foo</span><span class="special">()</span> <span class="special">&gt;&gt;&gt;</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> <span class="identifier">foo</span><span class="special">!</span>
@@ -347,8 +324,7 @@
<p> <p>
Another useful idea is to replace constructors with factory functions: Another useful idea is to replace constructors with factory functions:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="identifier">_point</span> <span class="special">=</span> <span class="identifier">point</span>
<span class="identifier">_point</span> <span class="special">=</span> <span class="identifier">point</span>
<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> <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>
<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> <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>
@@ -360,7 +336,7 @@
support. support.
</p> </p>
</div> </div>
<div class="section" lang="en"> <div class="section">
<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>
@@ -369,10 +345,7 @@
can easily become too high. If this is causing you problems, you can split can easily become too high. If this is causing you problems, you can split
the class_ definitions in multiple files: the class_ definitions in multiple files:
</p> </p>
<p> <pre class="programlisting"><span class="comment">/* file point.cpp */</span>
</p>
<pre class="programlisting">
<span class="comment">/* file point.cpp */</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span> <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span> <span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
@@ -394,8 +367,7 @@
Now you create a file <code class="literal">main.cpp</code>, which contains the <code class="literal">BOOST_PYTHON_MODULE</code> 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. macro, and call the various export functions inside it.
</p> </p>
<pre class="programlisting"> <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_point</span><span class="special">();</span>
<span class="keyword">void</span> <span class="identifier">export_triangle</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="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">_geom</span><span class="special">)</span>
@@ -408,8 +380,7 @@
Compiling and linking together all this files produces the same result as Compiling and linking together all this files produces the same result as
the usual approach: the usual approach:
</p> </p>
<pre class="programlisting"> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span> <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">point</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">triangle</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span> <span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">triangle</span><span class="special">.</span><span class="identifier">h</span><span class="special">&gt;</span>
@@ -427,33 +398,43 @@
exporting it to Python at the same time: changes in a class will only demand 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. the compilation of a single cpp, instead of the entire wrapper code.
</p> </p>
<div class="sidebar"> <div class="note"><table border="0" summary="Note">
<p class="title"><b></b></p> <tr>
<p> <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<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>, <th align="left">Note</th>
take a look at the <code class="literal">--multiple</code> option, that generates the </tr>
wrappers in various files as demonstrated here. <tr><td align="left" valign="top"><p>
</p> If you're exporting your classes with <a href="../../../../../pyste/index.html" target="_top">Pyste</a>,
</div> take a look at the <code class="literal">--multiple</code> option, that generates
<div class="sidebar"> the wrappers in various files as demonstrated here.
<p class="title"><b></b></p> </p></td></tr>
<p> </table></div>
<span class="inlinemediaobject"><img src="../images/note.png" alt="note"></span> This method is useful too if you are getting the error <div class="note"><table border="0" summary="Note">
message <span class="emphasis"><em>"fatal error C1204:Compiler limit:internal structure <tr>
overflow"</em></span> when compiling a large source file, as explained <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
in the <a href="../../../../v2/faq.html#c1204" target="_top">FAQ</a>. <th align="left">Note</th>
</p> </tr>
</div> <tr><td align="left" valign="top"><p>
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></td></tr>
</table></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 <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005 Joel
de Guzman, David Abrahams</small></td> de Guzman, David Abrahams<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
http://www.boost.org/LICENSE_1_0.txt </a>)
</p>
</div></td>
</tr></table> </tr></table>
<hr> <hr>
<div class="spirit-nav"> <div class="spirit-nav">
<a accesskey="p" href="exception.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a> <a accesskey="p" href="exception.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a>
</div> </div>
</body> </body>
</html> </html>

View File

@@ -1,5 +1,5 @@
[library python [library python
[version 1.0] [version 2.0]
[authors [de Guzman, Joel], [Abrahams, David]] [authors [de Guzman, Joel], [Abrahams, David]]
[copyright 2002 2003 2004 2005 Joel de Guzman, David Abrahams] [copyright 2002 2003 2004 2005 Joel de Guzman, David Abrahams]
[category inter-language support] [category inter-language support]
@@ -49,10 +49,10 @@ Function:
can be exposed to Python by writing a Boost.Python wrapper: can be exposed to Python by writing a Boost.Python wrapper:
#include <boost/python.hpp> #include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello) BOOST_PYTHON_MODULE(hello_ext)
{ {
using namespace boost::python;
def("greet", greet); def("greet", greet);
} }
@@ -61,8 +61,8 @@ resulting DLL is now visible to Python. Here's a sample Python session:
[python] [python]
>>> import hello >>> import hello_ext
>>> print hello.greet() >>> print hello_ext.greet()
hello, world hello, world
[c++] [c++]
@@ -75,43 +75,39 @@ resulting DLL is now visible to Python. Here's a sample Python session:
[h2 From Start To Finish] [h2 From Start To Finish]
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 will outline the steps
necessary to achieve that. We shall use the build tool that comes bundled necessary to achieve that. We will use the build tool that comes bundled
with every boost distribution: [*bjam]. with every boost distribution: [*bjam].
[blurb __note__ [*Building without bjam]\n\n [note [*Building without bjam]
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". Besides bjam, there are of course other ways to get your module built.
There are of course other build tools apart from [^bjam].\n\n What's written here should not be taken as "the one and only way".
Take note however that the preferred build tool for Boost.Python is bjam. There are of course other build tools apart from [^bjam].
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 Take note however that the preferred build tool for Boost.Python is bjam.
who had to use a different tool. 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
who had to use a different tool.
] ]
We shall skip over the details. Our objective will be to simply create the We will skip over the details. Our objective will be to simply create
hello world module and run it in Python. For a complete reference to the hello world module and run it in Python. For a complete reference to
building Boost.Python, check out: [@../../../building.html building.html]. building Boost.Python, check out: [@../../../building.html
After this brief ['bjam] tutorial, we should have built two DLLs: building.html]. After this brief ['bjam] tutorial, we should have built
the DLLs and run a python program using the extension.
* boost_python.dll
* hello.pyd
if you are on Windows, and
* libboost_python.so
* hello.so
if you are on Unix.
The tutorial example can be found in the directory: The tutorial example can be found in the directory:
[^libs/python/example/tutorial]. There, you can find: [^libs/python/example/tutorial]. There, you can find:
* hello.cpp * hello.cpp
* Jamfile * hello.py
* Jamroot
The [^hello.cpp] file is our C++ hello world example. The [^Jamfile] is a The [^hello.cpp] file is our C++ hello world example. The [^Jamroot] is
minimalist ['bjam] script that builds the DLLs for us. a minimalist ['bjam] script that builds the DLLs for us. Finally,
[^hello.py] is our Python program that uses the extension in
[^hello.cpp].
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 or somewhere in your path such that [^bjam] can be executed in directory or somewhere in your path such that [^bjam] can be executed in
@@ -122,51 +118,12 @@ platforms. The complete list of Bjam executables can be found
[h2 Let's Jam!] [h2 Let's Jam!]
__jam__ __jam__
Here is our minimalist Jamfile: [@../../../../example/tutorial/Jamroot Here] is our minimalist Jamroot
file. Simply copy the file and tweak [^use-project boost] to where your
boost root directory is and your OK.
[pre The comments contained in the Jamrules file above should be sufficient
# This is the top of our own project tree to get you going.
project-root ;
import python ;
extension hello # Declare a Python extension called hello
: hello.cpp # source
# requirements and dependencies for Boost.Python extensions
<template>@boost/libs/python/build/extension
;
]
First, we need to specify our location. You may place your project anywhere.
[^project-root] allows you to do that.
[pre
project-root ;
]
By doing so, you'll need a Jamrules file. Simply copy the one in the
[@../../../../example/tutorial/Jamrules example/tutorial directory] and tweak
the [^path-global BOOST_ROOT] to where your boost root directory is. The file
has [@../../../../example/tutorial/Jamrules detailed instructions] you can follow.
Then we will import the definitions needed by Python modules:
[pre
import python ;
]
Finally we declare our [^hello] extension:
[pre
extension hello # Declare a Python extension called hello
: hello.cpp # source
# requirements and dependencies for Boost.Python extensions
<template>@boost/libs/python/build/extension
;
]
The last part tells BJam that we are depending on the Boost Python Library.
[h2 Running bjam] [h2 Running bjam]
@@ -174,98 +131,60 @@ The last part tells BJam that we are depending on the Boost Python Library.
[:Start it up.] [:Start it up.]
Make sure that the environment is set so that we can invoke the C++ A file called user-config.jam in your home directory is used to
compiler. With MSVC, that would mean running the [^Vcvars32.bat] batch configure your tools. In Windows, your home directory can be found by
file. For instance: typing:
[pre [pre
C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat ECHO %HOMEDRIVE%%HOMEPATH%
] ]
Some environment variables will have to be setup for proper building of our into a command prompt window. Your file should at least have the rules
Python modules. Example: for your compiler and your python installation. A specific example of
this on Windows would be:
[pre [pre
set PYTHON_ROOT=c:/dev/tools/python # MSVC configuration
set PYTHON_VERSION=2.2 using msvc : 8.0 ;
# Python configuration
using python : 2.4 : C:/dev/tools/Python/ ;
] ]
The above assumes that the Python installation is in [^c:/dev/tools/python] The first rule tells Bjam to use the MSVC 8.0 compiler and associated
and that we are using Python version 2.2. You'll have to tweak these tools. The second rule provides information on Python, its version and
appropriately. where it is located. The above assumes that the Python installation is
in [^C:/dev/tools\/Python/]. If you have one fairly "standard" python
[blurb __tip__ Be sure not to include a third number, e.g. [*not] "2.2.1", installation for your platform, you might not need to do this.
even if that's the version you have.]
Take note that you may also do that through the Jamrules file we put in
our project as detailed above. The file
has [@../../../../example/tutorial/Jamrules detailed instructions] you
can follow.
Now we are ready... Be sure to [^cd] to [^libs/python/example/tutorial] Now we are ready... Be sure to [^cd] to [^libs/python/example/tutorial]
where the tutorial [^"hello.cpp"] and the [^"Jamfile"] is situated. where the tutorial [^"hello.cpp"] and the [^"Jamroot"] is situated.
Finally: Finally:
bjam -sTOOLS=vc-7_1 bjam
We are again assuming that we are using Microsoft Visual C++ version 7.1. If
not, then you will have to specify the appropriate tool. See
[@../../../../../../tools/build/index.html Building Boost Libraries] for
further details.
It should be building now: It should be building now:
[pre [pre
cd C:\dev\boost\libs\python\example\tutorial cd C:\dev\boost\libs\python\example\tutorial
bjam -sTOOLS=msvc bjam
...patience... ...patience...
...found 1703 targets... ...found 1101 targets...
...updating 40 targets... ...updating 35 targets...
] ]
And so on... Finally: And so on... Finally:
[pre [pre
Creating library bin\boost\libs\python\build\boost_python.dll\vc-7_1\debug\th Creating library /path-to-boost_python.dll/
reading-multi\boost_python.lib and object bin\boost\libs\python\build\boost_pyth Creating library /path-to-'''hello_ext'''.exp/
on.dll\vc-7_1\debug\threading-multi\boost_python.exp '''**passed**''' ... hello.test
vc-C++ bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.obj ...updated 35 targets...
hello.cpp
vc-Link bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.pyd bin\tutori
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
b and object bin\tutorial\hello.pyd\vc-7_1\debug\threading-multi\hello.exp
...updated 31 targets...
] ]
If all is well, you should now have: Or something similar. If all is well, you should now have built the DLLs and
run the Python program.
* boost_python.dll
* hello.pyd
if you are on Windows, and
* libboost_python.so
* hello.so
if you are on Unix.
[^boost_python.dll] and [^hello.pyd] can be found somewhere in your project's
[^bin] directory. After a successful build, you make it possible for the system
to find boost_python.dll or libboost_python.so (usually done with LD_LIBRARY_PATH,
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:
[python]
>>> import hello
>>> print hello.greet()
hello, world
[c++]
[:[*There you go... Have fun!]] [:[*There you go... Have fun!]]
@@ -495,7 +414,7 @@ Doing so, we get some things for free:
Python via a pointer or reference to [^Base] can be passed where a pointer Python via a pointer or reference to [^Base] can be passed where a pointer
or reference to [^Derived] is expected. or reference to [^Derived] is expected.
Now, we shall expose the C++ free functions [^b] and [^d] and [^factory]: Now, we will expose the C++ free functions [^b] and [^d] and [^factory]:
def("b", b); def("b", b);
def("d", d); def("d", d);
@@ -505,7 +424,7 @@ Note that free function [^factory] is being used to generate new
instances of class [^Derived]. In such cases, we use instances of class [^Derived]. In such cases, we use
[^return_value_policy<manage_new_object>] to instruct Python to adopt [^return_value_policy<manage_new_object>] to instruct Python to adopt
the pointer to [^Base] and hold the instance in a new Python [^Base] the pointer to [^Base] and hold the instance in a new Python [^Base]
object until the the Python object is destroyed. We shall see more of object until the the Python object is destroyed. We will see more of
Boost.Python [link python.call_policies call policies] later. Boost.Python [link python.call_policies call policies] later.
// Tell Python to take ownership of factory's result // Tell Python to take ownership of factory's result
@@ -516,7 +435,7 @@ Boost.Python [link python.call_policies call policies] later.
[section Class Virtual Functions] [section Class Virtual Functions]
In this section, we shall learn how to make functions behave polymorphically In this section, we will 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 [^Base] class: to our [^Base] class:
@@ -526,13 +445,13 @@ to our [^Base] class:
virtual int f() = 0; virtual int f() = 0;
}; };
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 C++
design. In principle, it should be possible to expose the interface for a 3rd design. In principle, it should be possible to expose the interface for a 3rd
party library without changing it. It is not ideal to add anything to our class party library without changing it. It is not ideal to add anything to our class
`Base`. Yet, when you have a virtual function that's going to be overridden in `Base`. Yet, when you have a virtual function that's going to be overridden in
Python and called polymorphically *from C++*, we'll need to add some Python and called polymorphically *from C++*, we'll need to add some
scaffoldings to make things work properly. What we'll do is write a class scaffoldings to make things work properly. What we'll do is write a class
wrapper that derives from `Base` that will unintrusively hook into the virtual wrapper that derives from `Base` that will unintrusively hook into the virtual
functions so that a Python override may be called: functions so that a Python override may be called:
struct BaseWrap : Base, wrapper<Base> struct BaseWrap : Base, wrapper<Base>
@@ -548,8 +467,10 @@ inherited `wrapper<Base>` (See [@../../../v2/wrapper.html Wrapper]). The
`wrapper` template makes the job of wrapping classes that are meant to `wrapper` template makes the job of wrapping classes that are meant to
overridden in Python, easier. overridden in Python, easier.
[blurb __alert__ [*MSVC6/7 Workaround]\n\n [blurb __alert__ [*MSVC6/7 Workaround]
If you are using Microsoft Visual C++ 6 or 7, you have to write `f` as:\n\n
If you are using Microsoft Visual C++ 6 or 7, you have to write `f` as:
`return call<int>(this->get_override("f").ptr());`.] `return call<int>(this->get_override("f").ptr());`.]
BaseWrap's overridden virtual member function `f` in effect calls the BaseWrap's overridden virtual member function `f` in effect calls the
@@ -561,24 +482,25 @@ Finally, exposing `Base`:
.def("f", pure_virtual(&Base::f)) .def("f", pure_virtual(&Base::f))
; ;
`pure_virtual` signals Boost.Python that the function `f` is a pure virtual `pure_virtual` signals Boost.Python that the function `f` is a pure virtual
function. function.
[blurb __note__ [*member function and methods]\n\n Python, like [note [*member function and methods]
many object oriented languages uses the term [*methods]. Methods
correspond roughly to C++'s [*member functions]] Python, like many object oriented languages uses the term [*methods].
Methods correspond roughly to C++'s [*member functions]]
[endsect] [endsect]
[section Virtual Functions with Default Implementations] [section Virtual Functions with Default Implementations]
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 are
wrapped using Boost.Python's [@../../../v2/wrapper.html class wrapper] wrapped using Boost.Python's [@../../../v2/wrapper.html class wrapper]
facilities. If we wish to wrap [*non]-pure-virtual functions instead, the facilities. If we wish to wrap [*non]-pure-virtual functions instead, the
mechanism is a bit different. mechanism is a bit different.
Recall that in the [link python.class_virtual_functions previous section], we Recall that in the [link python.class_virtual_functions previous section], we
wrapped a class with a pure virtual function that we then implemented in C++, or wrapped a class with a pure virtual function that we then implemented in C++, or
Python classes derived from it. Our base class: Python classes derived from it. Our base class:
struct Base struct Base
@@ -596,7 +518,7 @@ not declared as pure virtual:
}; };
We wrap it this way: We wrap it this way:
struct BaseWrap : Base, wrapper<Base> struct BaseWrap : Base, wrapper<Base>
{ {
int f() int f()
@@ -608,24 +530,26 @@ We wrap it this way:
int default_f() { return this->Base::f(); } int default_f() { return this->Base::f(); }
}; };
Notice how we implemented `BaseWrap::f`. Now, we have to check if there is an Notice how we implemented `BaseWrap::f`. Now, we have to check if there is an
override for `f`. If none, then we call `Base::f()`. override for `f`. If none, then we call `Base::f()`.
[blurb __alert__ [*MSVC6/7 Workaround]\n\n [blurb __alert__ [*MSVC6/7 Workaround]
If you are using Microsoft Visual C++ 6 or 7, you have to rewrite the line If you are using Microsoft Visual C++ 6 or 7, you have to rewrite the line
with the `*note*` as:\n\n with the `*note*` as:
`return call<char const*>(f.ptr());`.] `return call<char const*>(f.ptr());`.]
Finally, exposing: Finally, exposing:
class_<BaseWrap, boost::noncopyable>("Base") class_<BaseWrap, boost::noncopyable>("Base")
.def("f", &Base::f, &BaseWrap::default_f) .def("f", &Base::f, &BaseWrap::default_f)
; ;
Take note that we expose both `&Base::f` and `&BaseWrap::default_f`. Take note that we expose both `&Base::f` and `&BaseWrap::default_f`.
Boost.Python needs to keep track of 1) the dispatch function [^f] and 2) the Boost.Python needs to keep track of 1) the dispatch function [^f] and 2) the
forwarding function to its default implementation [^default_f]. There's a forwarding function to its default implementation [^default_f]. There's a
special [^def] function for this purpose. special [^def] function for this purpose.
In Python, the results would be as expected: In Python, the results would be as expected:
@@ -715,7 +639,7 @@ that correspond to these Python ['special functions]. Example:
Need we say more? Need we say more?
[blurb __note__ What is the business of `operator<<`? [note What is the business of `operator<<`?
Well, the method `str` requires the `operator<<` to do its work (i.e. Well, the method `str` requires the `operator<<` to do its work (i.e.
`operator<<` is used by the method defined by `def(str(self))`.] `operator<<` is used by the method defined by `def(str(self))`.]
@@ -725,9 +649,9 @@ Well, the method `str` requires the `operator<<` to do its work (i.e.
[section Functions] [section Functions]
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. We shall see some facilities to make exposing C++ functions to detail. We will see some facilities to make exposing C++ functions to
Python safe from potential pifalls such as dangling pointers and Python safe from potential pifalls such as dangling pointers and
references. We shall also see facilities that will make it even easier for references. We will also see facilities that will make it even easier for
us to expose C++ functions that take advantage of C++ features such as us to expose C++ functions that take advantage of C++ features such as
overloading and default arguments. overloading and default arguments.
@@ -890,18 +814,21 @@ or more policies can be composed by chaining. Here's the general syntax:
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 [@../../../v2/reference.html#models_of_call_policies here]. these can be found [@../../../v2/reference.html#models_of_call_policies here].
* [*with_custodian_and_ward]\n Ties lifetimes of the arguments * [*with_custodian_and_ward]: Ties lifetimes of the arguments
* [*with_custodian_and_ward_postcall]\n Ties lifetimes of the arguments and results * [*with_custodian_and_ward_postcall]: Ties lifetimes of the arguments and results
* [*return_internal_reference]\n Ties lifetime of one argument to that of result * [*return_internal_reference]: Ties lifetime of one argument to that of result
* [*return_value_policy<T> with T one of:]\n * [*return_value_policy<T> with T one of:]
* [*reference_existing_object]\nnaive (dangerous) approach * [*reference_existing_object]: naive (dangerous) approach
* [*copy_const_reference]\nBoost.Python v1 approach * [*copy_const_reference]: Boost.Python v1 approach
* [*copy_non_const_reference]\n * [*copy_non_const_reference]:
* [*manage_new_object]\n Adopt a pointer and hold the instance * [*manage_new_object]: Adopt a pointer and hold the instance
[blurb :-) [*Remember the Zen, Luke:]\n\n [blurb :-) [*Remember the Zen, Luke:]
"Explicit is better than implicit"\n
"In the face of ambiguity, refuse the temptation to guess"\n] "Explicit is better than implicit"
"In the face of ambiguity, refuse the temptation to guess"
]
[endsect] [endsect]
[section Overloading] [section Overloading]
@@ -935,7 +862,7 @@ We have here our C++ class:
}; };
}; };
Class X has 4 overloaded functions. We shall start by introducing some Class X has 4 overloaded functions. We will start by introducing some
member function pointer variables: member function pointer variables:
bool (X::*fx1)(int) = &X::f; bool (X::*fx1)(int) = &X::f;
@@ -974,8 +901,8 @@ wrapping as outlined in the [link python.overloading previous section], or
writing thin wrappers: writing thin wrappers:
// write "thin wrappers" // write "thin wrappers"
int f1(int x) { f(x); } int f1(int x) { return f(x); }
int f2(int x, double y) { f(x,y); } int f2(int x, double y) { return f(x,y); }
/*...*/ /*...*/
@@ -1141,7 +1068,7 @@ Then...
Python is dynamically typed, unlike C++ which is statically typed. Python Python is dynamically typed, unlike C++ which is statically typed. Python
variables may hold an integer, a float, list, dict, tuple, str, long etc., variables may hold an integer, a float, list, dict, tuple, str, long etc.,
among other things. In the viewpoint of Boost.Python and C++, these among other things. In the viewpoint of Boost.Python and C++, these
Pythonic variables are just instances of class [^object]. We shall see in Pythonic variables are just instances of class [^object]. We will see in
this chapter how to deal with Python objects. this chapter how to deal with Python objects.
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
@@ -1290,14 +1217,14 @@ we wanted to do above can be achieved by writing:
Vec2& v = extract<Vec2&>(o); Vec2& v = extract<Vec2&>(o);
assert(l == v.length()); assert(l == v.length());
The first line attempts to extract the "length" attribute of the Boost.Python The first line attempts to extract the "length" attribute of the Boost.Python
[^object]. The second line attempts to ['extract] the [^Vec2] object from held [^object]. The second line attempts to ['extract] the [^Vec2] object from held
by the Boost.Python [^object]. by the Boost.Python [^object].
Take note that we said "attempt to" above. What if the Boost.Python [^object] Take note that we said "attempt to" above. What if the Boost.Python [^object]
does not really hold a [^Vec2] type? This is certainly a possibility considering does not really hold a [^Vec2] type? This is certainly a possibility considering
the dynamic nature of Python [^object]s. To be on the safe side, if the C++ type the dynamic nature of Python [^object]s. To be on the safe side, if the C++ type
can't be extracted, an appropriate exception is thrown. To avoid an exception, can't be extracted, an appropriate exception is thrown. To avoid an exception,
we need to test for extractibility: we need to test for extractibility:
extract<Vec2&> x(o); extract<Vec2&> x(o);
@@ -1335,10 +1262,12 @@ current [^scope()], which is usually the current module. The snippet above
creates a Python class derived from Python's [^int] type which is creates a Python class derived from Python's [^int] type which is
associated with the C++ type passed as its first parameter. associated with the C++ type passed as its first parameter.
[blurb __note__ [*what is a scope?]\n\n The scope is a class that has an [note [*what is a scope?]
associated global Python object which controls the Python namespace in
which new extension classes and wrapped functions will be defined as The scope is a class that has an associated global Python object which
attributes. Details can be found [@../../../v2/scope.html here].] controls the Python namespace in which new extension classes and wrapped
functions will be defined as attributes. Details can be found
[@../../../v2/scope.html here].]
You can access those values in Python as You can access those values in Python as
@@ -1373,6 +1302,23 @@ create a new scope around a class:
[def PyModule_GetDict [@http://www.python.org/doc/current/api/moduleObjects.html#l2h-594 PyModule_GetDict]] [def PyModule_GetDict [@http://www.python.org/doc/current/api/moduleObjects.html#l2h-594 PyModule_GetDict]]
[endsect] [endsect]
[section:creating_python_object Creating `boost::python::object` from `PyObject*`]
When you want a `boost::python::object` to manage a pointer to `PyObject*` pyobj one does:
boost::python::object o(boost::python::handle<>(pyobj));
In this case, the `o` object, manages the `pyobj`, it wont increase the reference count on construction.
Otherwise, to use a borrowed reference:
boost::python::object o(boost::python::handle<>(boost::python::borrowed(pyobj)));
In this case, `Py_INCREF` is called, so `pyobj` is not destructed when object o goes out of scope.
[endsect] [/ creating_python_object ]
[endsect] [/ Object Interface] [endsect] [/ Object Interface]
[section Embedding] [section Embedding]
@@ -1433,15 +1379,15 @@ Being able to build is nice, but there is nothing to build yet. Embedding
the Python interpreter into one of your C++ programs requires these 4 the Python interpreter into one of your C++ programs requires these 4
steps: steps:
# '''#include''' [^<boost/python.hpp>]\n\n # '''#include''' [^<boost/python.hpp>]
# Call Py_Initialize() to start the interpreter and create the [^__main__] module.\n\n # Call Py_Initialize() to start the interpreter and create the [^__main__] module.
# Call other Python C API routines to use the interpreter.\n\n # Call other Python C API routines to use the interpreter.
[/ # 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 [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.] interpreter. This may be fixed in a future version of boost.python.]
] ]
@@ -1452,10 +1398,10 @@ interpreter. This may be fixed in a future version of boost.python.]
[section Using the interpreter] [section Using the interpreter]
As you probably already know, objects in Python are reference-counted. As you probably already know, objects in Python are reference-counted.
Naturally, the [^PyObject]s of the Python/C API are also reference-counted. Naturally, the [^PyObject]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 in Python, the Python/C API requires you to do it automatic in Python, the Python C API requires you to do it
[@http://www.python.org/doc/current/api/refcounts.html by hand]. This is [@http://www.python.org/doc/current/c-api/refcounting.html by hand]. This is
messy and especially hard to get right in the presence of C++ exceptions. 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.
@@ -1499,10 +1445,10 @@ containing a phrase that is well-known in programming circles.
[h2 Manipulating Python objects] [h2 Manipulating Python objects]
Often we'd like to have a class to manipulate Python objects. Often we'd like to have a class to manipulate Python objects.
But we have already seen such a class above, and in the But we have already seen such a class above, and in the
[@python/object.html previous section]: the aptly named [^object] class [@python/object.html previous section]: the aptly named [^object] class
and its derivatives. We've already seen that they can be constructed from and its derivatives. We've already seen that they can be constructed from
a [^handle]. The following examples should further illustrate this fact: a [^handle]. The following examples should further illustrate this fact:
object main_module = import("__main__"); object main_module = import("__main__");
@@ -1537,7 +1483,7 @@ If an exception occurs in the evaluation of the python expression,
The [^error_already_set] exception class doesn't carry any information in itself. The [^error_already_set] exception class doesn't carry any information in itself.
To find out more about the Python exception that occurred, you need to use the To find out more about the Python exception that occurred, you need to use the
[@http://www.python.org/doc/api/exceptionHandling.html exception handling functions] [@http://www.python.org/doc/api/exceptionHandling.html exception handling functions]
of the Python/C API in your catch-statement. This can be as simple as calling of the Python C API in your catch-statement. This can be as simple as calling
[@http://www.python.org/doc/api/exceptionHandling.html#l2h-70 PyErr_Print()] to [@http://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 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
@@ -1650,7 +1596,7 @@ 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 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 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. container. We need to make a Python iterator look like an STL iterator.
For that, we use `stl_input_iterator<>`. Consider how we might For that, we use `stl_input_iterator<>`. Consider how we might
implement a function that exposes `std::list<int>::assign()` to implement a function that exposes `std::list<int>::assign()` to
Python: Python:
@@ -1754,7 +1700,7 @@ separately with Boost.Python, like this:
Compiling these files will generate the following Python extensions: Compiling these files will generate the following Python extensions:
[^core.pyd], [^io.pyd] and [^filters.pyd]. [^core.pyd], [^io.pyd] and [^filters.pyd].
[blurb __note__ The extension [^.pyd] is used for python extension modules, which [note The extension [^.pyd] is used for python extension modules, which
are just shared libraries. Using the default for your system, like [^.so] for are just shared libraries. Using the default for your system, like [^.so] for
Unix and [^.dll] for Windows, works just as well.] Unix and [^.dll] for Windows, works just as well.]
@@ -2025,11 +1971,11 @@ 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 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. the compilation of a single cpp, instead of the entire wrapper code.
[blurb __note__ If you're exporting your classes with [@../../../../pyste/index.html Pyste], [note If you're exporting your classes with [@../../../../pyste/index.html Pyste],
take a look at the [^--multiple] option, that generates the wrappers in take a look at the [^--multiple] option, that generates the wrappers in
various files as demonstrated here.] various files as demonstrated here.]
[blurb __note__ This method is useful too if you are getting the error message [note This method is useful too if you are getting the error message
['"fatal error C1204:Compiler limit:internal structure overflow"] when compiling ['"fatal error C1204:Compiler limit:internal structure overflow"] when compiling
a large source file, as explained in the [@../../../v2/faq.html#c1204 FAQ].] a large source file, as explained in the [@../../../v2/faq.html#c1204 FAQ].]

View File

@@ -11,8 +11,8 @@
<a href="doc/html/index.html">link</a> &nbsp;<hr> <a href="doc/html/index.html">link</a> &nbsp;<hr>
<p>© Copyright Beman Dawes, 2001</p> <p>© Copyright Beman Dawes, 2001</p>
<p>Distributed under the Boost Software License, Version 1.0. (See <p>Distributed under the Boost Software License, Version 1.0. (See
accompanying file <a href="../../../LICENSE_1_0.txt"> accompanying file <a href="../../../../LICENSE_1_0.txt">
LICENSE_1_0.txt</a> or copy at 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> <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</p>
</body> </body>
</html> </html>

View File

@@ -160,7 +160,7 @@ documentation).
13 November, 2002 13 November, 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>
2002. </i></p> 2002. </i></p>
</body> </body>
</html> </html>

View File

@@ -8,7 +8,7 @@
<meta name="generator" content= <meta name="generator" content=
"HTML Tidy for Windows (vers 1st August 2002), see www.w3.org"> "HTML Tidy for Windows (vers 1st August 2002), 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-8859-1">
<link rel="stylesheet" type="text/css" href=../../../../boost.css> <link rel="stylesheet" type="text/css" href="../../../../boost.css">
<title>Boost.Python - CallPolicies Concept</title> <title>Boost.Python - CallPolicies Concept</title>
</head> </head>
@@ -60,6 +60,7 @@
<li><code>postcall</code> - Python argument tuple and result management <li><code>postcall</code> - Python argument tuple and result management
after the wrapped object is invoked</li> after the wrapped object is invoked</li>
<li><code>extract_return_type</code> - metafunction for extracting the return type from a given signature type sequence</li>
</ol> </ol>
<h2><a name="composition"></a>CallPolicies Composition</h2> <h2><a name="composition"></a>CallPolicies Composition</h2>
@@ -132,7 +133,16 @@
reference count must be decremented; if another existing object is reference count must be decremented; if another existing object is
returned, its reference count must be incremented.</td> returned, its reference count must be incremented.</td>
</tr> </tr>
</table> <tr>
<td valign="top"><code>P::extract_return_type</code></td>
<td>A model of <a href=
"../../../mpl/doc/refmanual/metafunction.html">Metafunction</a>.</td>
<td>An MPL unary <a href=
"../../../mpl/doc/refmanual/metafunction.html">Metafunction</a> used extract the return type from a given signature. By default it is derived from mpl::front.</td>
</tr>
</table>
Models of CallPolicies are required to be <a href= Models of CallPolicies are required to be <a href=
"../../../utility/CopyConstructible.html">CopyConstructible</a>. "../../../utility/CopyConstructible.html">CopyConstructible</a>.
<hr> <hr>
@@ -144,7 +154,7 @@
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
<p>Permission to copy, use, modify, sell and distribute this software is <p>Permission to copy, use, modify, sell and distribute this software is
granted provided this copyright notice appears in all copies. This granted provided this copyright notice appears in all copies. This

View File

@@ -4,7 +4,7 @@
<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">
<link rel="stylesheet" type="text/css" href=../../../../boost.css> <link rel="stylesheet" type="text/css" href="../../../../boost.css">
<title>Boost.Python - Dereferenceable Concept</title> <title>Boost.Python - Dereferenceable Concept</title>
</head> </head>
<body link="#0000ff" vlink="#800080"> <body link="#0000ff" vlink="#800080">
@@ -63,7 +63,7 @@ type <code>T</code>. In addition, all pointers are Dereferenceable.
18 December, 2003 18 December, 2003
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002-2003. </i> Abrahams</a> 2002-2003. </i>
<p>Permission to copy, use, modify, sell <p>Permission to copy, use, modify, sell

4
doc/v2/Extractor.html Executable file → Normal file
View File

@@ -4,7 +4,7 @@
<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">
<link rel="stylesheet" type="text/css" href=../../../../boost.css> <link rel="stylesheet" type="text/css" href="../../../../boost.css">
<title>Boost.Python - Extractor Concept</title> <title>Boost.Python - Extractor Concept</title>
</head> </head>
<body link="#0000ff" vlink="#800080"> <body link="#0000ff" vlink="#800080">
@@ -85,7 +85,7 @@ are layout-compatible with PyObject.
13 November, 2002 13 November, 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002. </i> Abrahams</a> 2002. </i>
<p>Permission to copy, use, modify, sell <p>Permission to copy, use, modify, sell

4
doc/v2/HolderGenerator.html Executable file → Normal file
View File

@@ -4,7 +4,7 @@
<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">
<link rel="stylesheet" type="text/css" href=../../../../boost.css> <link rel="stylesheet" type="text/css" href="../../../../boost.css">
<title>Boost.Python - Holder Concept</title> <title>Boost.Python - Holder Concept</title>
</head> </head>
<body link="#0000ff" vlink="#800080"> <body link="#0000ff" vlink="#800080">
@@ -63,7 +63,7 @@ type.
13 November, 2002 13 November, 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002. </i> Abrahams</a> 2002. </i>
<p>Permission to copy, use, modify, sell <p>Permission to copy, use, modify, sell

View File

@@ -223,7 +223,7 @@ you'll just have to wait till next month (hopefully the beginning).
13 November, 2002 13 November, 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>
2002. </i></p> 2002. </i></p>
</body> </body>
</html> </html>

View File

@@ -231,7 +231,7 @@ worth doing anything about it.
13 November, 2002 13 November, 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>
2002. </i></p> 2002. </i></p>
</body> </body>
</html> </html>

View File

@@ -305,7 +305,7 @@ to these issues will probably have to be formalized before long.
13 November, 2002 13 November, 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>
2002. </i></p> 2002. </i></p>
</body> </body>
</html> </html>

View File

@@ -147,7 +147,7 @@ instances of the associated Python type will be considered a match.
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -1,10 +1,12 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Copyright David Abrahams 2006. Distributed under the Boost --> <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
<!-- Software License, Version 1.0. (See accompanying --> <!-- Software License, Version 1.0. (See accompanying -->
<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> <!-- 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">
<link rel="stylesheet" type="text/css" href=../../../../boost.css> <link rel="stylesheet" type="text/css" href="../../../../boost.css">
<title>Boost.Python - ResultConverter Concept</title> <title>Boost.Python - ResultConverter Concept</title>
</head> </head>
<body link="#0000ff" vlink="#800080"> <body link="#0000ff" vlink="#800080">
@@ -24,10 +26,12 @@
<dl class="page-index"> <dl class="page-index">
<dt><a href="#introduction">Introduction</a></dt> <dt><a href="#introduction">Introduction</a></dt>
<dt><a href="#concept-requirements">Concept Requirements</a></dt> <dt><a href="#concept-requirements">Concept Requirements</a></dt>
<dl class="page-index"> <dd>
<dt><a href="#ResultConverter-concept">ResultConverter Concept</a></dt> <dl class="page-index">
<dt><a href="#ResultConverterGenerator-concept">ResultConverterGenerator Concept</a></dt> <dt><a href="#ResultConverter-concept">ResultConverter Concept</a></dt>
</dl> <dt><a href="#ResultConverterGenerator-concept">ResultConverterGenerator Concept</a></dt>
</dl>
</dd>
</dl> </dl>
<h2><a name="introduction"></a>Introduction</h2> <h2><a name="introduction"></a>Introduction</h2>
@@ -79,6 +83,13 @@ denotes an object of type <code><b>R</b></code>.
href="http://www.python.org/doc/current/api/exceptionHandling.html#l2h-71">PyErr_Occurred</a> href="http://www.python.org/doc/current/api/exceptionHandling.html#l2h-71">PyErr_Occurred</a>
should return non-zero.</td> should return non-zero.</td>
</tr> </tr>
<tr>
<td valign="top"><code>c.get_pytype()</code></td>
<td><code>PyTypeObject const*</code></td>
<td>A pointer to a Python Type object corresponding to result of the conversion,
or <code>0</code>. Used for documentation generation. If <code>0</code> is returned
the generated type in the documentation will be <b>object</b> .</td>
</tr>
</table> </table>
<h3><a name="ResultConverterGenerator-concept"></a>ResultConverterGenerator Concept</h3> <h3><a name="ResultConverterGenerator-concept"></a>ResultConverterGenerator Concept</h3>
@@ -102,7 +113,7 @@ C++ function return type.
09 May, 2002 <!--Luann's birthday! --> 09 May, 2002 <!--Luann's birthday! -->
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002. </i> Abrahams</a> 2002. </i>
<p>Permission to copy, use, modify, sell <p>Permission to copy, use, modify, sell

View File

@@ -31,7 +31,7 @@
</table> </table>
<hr> <hr>
<p><a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> is <p><a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> is
the architect, designer, and implementor of <b>Boost.Python</b>.</p> the architect, designer, and implementor of <b>Boost.Python</b>.</p>
<p><a href="mailto:brett.calcott@paradise.net.nz">Brett Calcott</a> <p><a href="mailto:brett.calcott@paradise.net.nz">Brett Calcott</a>
@@ -47,7 +47,7 @@
argument support</a> and wrote the excellent <a href= argument support</a> and wrote the excellent <a href=
"../tutorial/index.html">tutorial documentation</a>.</p> "../tutorial/index.html">tutorial documentation</a>.</p>
<p><a href="../../../../people/ralf_w_grosse_kunstleve.htm">Ralf W. <p><a href="http://www.boost.org/people/ralf_w_grosse_kunstleve.htm">Ralf W.
Grosse-Kunstleve</a> implemented the <a href="pickle.html">pickle Grosse-Kunstleve</a> implemented the <a href="pickle.html">pickle
support</a>, and has enthusiastically supported the library since its support</a>, and has enthusiastically supported the library since its
birth, contributing to design decisions and providing invaluable birth, contributing to design decisions and providing invaluable
@@ -59,15 +59,15 @@
C++ and Python for solving the problems of large-scale software C++ and Python for solving the problems of large-scale software
construction.</p> construction.</p>
<p><a href="../../../../people/aleksey_gurtovoy.htm">Aleksey Gurtovoy</a> <p><a href="http://www.boost.org/people/aleksey_gurtovoy.htm">Aleksey Gurtovoy</a>
wrote an incredible C++ <a href="http://www.mywikinet.com/mpl">Template wrote an incredible C++ <a href="http://www.mywikinet.com/mpl">Template
Metaprogramming Library</a> which allows Boost.Python to perform much of Metaprogramming Library</a> which allows Boost.Python to perform much of
its compile-time magic. In addition, Aleksey very generously contributed its compile-time magic. In addition, Aleksey very generously contributed
his time and deep knowledge of the quirks of various buggy compilers to his time and deep knowledge of the quirks of various buggy compilers to
help us get around problems at crucial moments.</p> help us get around problems at crucial moments.</p>
<p><a href="../../../../people/paul_mensonides.htm">Paul Mensonides</a>, <p><a href="http://www.boost.org/people/paul_mensonides.htm">Paul Mensonides</a>,
building on the work <a href="../../../../people/vesa_karvonen.htm">Vesa building on the work <a href="http://www.boost.org/people/vesa_karvonen.htm">Vesa
Karvonen</a>, wrote a similarly amazing <a href= Karvonen</a>, wrote a similarly amazing <a href=
"../../../preprocessor/doc/index.html">Preprocessor Metaprogramming "../../../preprocessor/doc/index.html">Preprocessor Metaprogramming
Library</a>, and generously contributed the time and expertise to get it Library</a>, and generously contributed the time and expertise to get it
@@ -129,7 +129,7 @@
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -101,7 +101,7 @@ namespace boost { namespace python
struct arg struct arg
{ {
template &lt;class T&gt; template &lt;class T&gt;
arg &amp;perator = (T const &amp;value); arg &amp;operator = (T const &amp;value);
explicit arg (char const *name){elements[0].name = name;} explicit arg (char const *name){elements[0].name = name;}
}; };
@@ -193,7 +193,7 @@ BOOST_PYTHON_MODULE(xxx)
<p>Revised 01 August, 2003</p> <p>Revised 01 August, 2003</p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002-2003.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002-2003.</i></p>
</body> </body>
</html> </html>

View File

@@ -79,7 +79,7 @@ double apply2(PyObject* func, double x, double y)
9 May, 2002 <!-- Luann's birthday! --> 9 May, 2002 <!-- Luann's birthday! -->
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>
2002. </i></p> 2002. </i></p>
</body> </body>
</html> </html>

View File

@@ -155,7 +155,7 @@ BOOST_PYTHON_MODULE(my_module)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -248,7 +248,7 @@ void apply(PyObject* callable, X&amp; x)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -784,7 +784,7 @@ class_&lt;Derived, bases&lt;Base&gt; &gt;("Derived");
<!--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" --> 1 November, 2005 <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002.</i></p> Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -106,6 +106,33 @@
function from being treated as an exported symbol on platforms which function from being treated as an exported symbol on platforms which
support that distinction in-code</td> support that distinction in-code</td>
</tr> </tr>
<tr>
<td valign="top"><code>BOOST_PYTHON_ENABLE_CDECL</code></td>
<td valign="top" align="center"><i>not&nbsp;defined</i></td>
<td valign="top">If defined, allows functions using the <code>__cdecl
</code> calling convention to be wrapped.</td>
</tr>
<tr>
<td valign="top"><code>BOOST_PYTHON_ENABLE_STDCALL</code></td>
<td valign="top" align="center"><i>not&nbsp;defined</i></td>
<td valign="top">If defined, allows functions using the <code>__stdcall
</code> calling convention to be wrapped.</td>
</tr>
<tr>
<td valign="top"><code>BOOST_PYTHON_ENABLE_FASTCALL</code></td>
<td valign="top" align="center"><i>not&nbsp;defined</i></td>
<td valign="top">If defined, allows functions using the <code>__fastcall
</code> calling convention to be wrapped.</td>
</tr>
</table> </table>
<h2><a name="lib-defined-impl"></a>Library Defined Implementation <h2><a name="lib-defined-impl"></a>Library Defined Implementation
@@ -139,6 +166,41 @@
compares <code>typeid(T).name()</code> instead of using and comparing compares <code>typeid(T).name()</code> instead of using and comparing
the <code>std::type_info</code> objects directly.</td> the <code>std::type_info</code> objects directly.</td>
</tr> </tr>
<tr>
<td valign="top"><code>BOOST_PYTHON_NO_PY_SIGNATURES</code></td>
<td valign="top" align="center"><i>not&nbsp;defined</i></td>
<td valign="top">If defined for a module no pythonic signatures are generated
for the docstrings of the module functions, and no python type is associated with any
of the converters registered by the module. This also reduces the binary size of the
module by about 14% (gcc compiled).<br>
If defined for the boost_python runtime library, the default for the
<code>docstring_options.enable_py_signatures()</code> is set to <code>false</code>.
</td>
</tr>
<tr>
<td valign="top"><code>BOOST_PYTHON_SUPPORTS_PY_SIGNATURES</code></td>
<td valign="top" align="center"><i>defined if <code>BOOST_PYTHON_NO_PY_SIGNATURES</code> is undefined</i></td>
<td valign="top">This macro is defined to enable a smooth transition from older Boost.Python versions
which do not support pythonic signatures. For example usage see
<a href="pytype_function.html#examples">here</a>.
</td>
</tr>
<tr>
<td valign="top"><code>BOOST_PYTHON_PY_SIGNATURES_PROPER_INIT_SELF_TYPE</code></td>
<td valign="top" align="center"><i>not&nbsp;defined</i></td>
<td valign="top">If defined the python type of <code>__init__</code> method "self" parameters
is properly generated, otherwise <code><b>object</b></code> is used. It is undefined
by default because it increases the binary size of the module by about 14% (gcc compiled).</td>
</tr>
</table> </table>
<hr> <hr>
@@ -149,7 +211,7 @@
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -143,7 +143,7 @@ BOOST_PYTHON_MODULE(my_module)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -143,7 +143,7 @@ BOOST_PYTHON_MODULE(my_module)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -223,7 +223,7 @@ BOOST_PYTHON_MODULE_INIT(data_members_example)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -185,7 +185,7 @@ BOOST_PYTHON_MODULE(def_test)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -82,6 +82,7 @@ namespace boost { namespace python
static PyObject* postcall(PyObject*, PyObject* result); static PyObject* postcall(PyObject*, PyObject* result);
typedef <a href= typedef <a href=
"#default_result_converter-spec">default_result_converter</a> result_converter; "#default_result_converter-spec">default_result_converter</a> result_converter;
template &lt;class Sig&gt; struct extract_return_type : mpl::front&lt;Sig&gt;{};
}; };
}} }}
</pre> </pre>
@@ -161,11 +162,11 @@ struct return_value_policy : Base
<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 11 June, 2007
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002.</i> Distributed under the Boost Software License, Abrahams</a> 2002.</i> Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)</p> http://www.boost.org/LICENSE_1_0.txt)</p>

View File

@@ -96,7 +96,7 @@
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -146,7 +146,7 @@ dict swap_object_dict(object target, dict d)
<p>Revised 30 September, 2002</p> <p>Revised 30 September, 2002</p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -103,6 +103,8 @@ namespace boost { namespace python {
docstring_options(bool show_user_defined, bool show_signatures); docstring_options(bool show_user_defined, bool show_signatures);
docstring_options(bool show_user_defined, bool show_py_signatures, bool show_cpp_signatures);
~docstring_options(); ~docstring_options();
void void
@@ -117,6 +119,18 @@ namespace boost { namespace python {
void void
enable_signatures(); enable_signatures();
void
disable_py_signatures();
void
enable_py_signatures();
void
disable_cpp_signatures();
void
enable_cpp_signatures();
void void
disable_all(); disable_all();
@@ -139,7 +153,7 @@ docstring_options(bool show_all=true);
object which controls the appearance of function and object which controls the appearance of function and
member-function docstrings defined in the code that follows. If member-function docstrings defined in the code that follows. If
<code>show_all</code> is <code>true</code>, both the <code>show_all</code> is <code>true</code>, both the
user-defined docstrings and the automatically generated C++ user-defined docstrings and the automatically generated Python and C++
signatures are shown. If <code>show_all</code> is signatures are shown. If <code>show_all</code> is
<code>false</code> the <code>__doc__</code> attributes are <code>false</code> the <code>__doc__</code> attributes are
<code>None</code>.</dt> <code>None</code>.</dt>
@@ -154,12 +168,29 @@ docstring_options(bool show_user_defined, bool show_signatures);
member-function docstrings defined in the code that follows. member-function docstrings defined in the code that follows.
Iff <code>show_user_defined</code> is <code>true</code>, the Iff <code>show_user_defined</code> is <code>true</code>, the
user-defined docstrings are shown. Iff user-defined docstrings are shown. Iff
<code>show_signatures</code> is <code>true</code>, C++ <code>show_signatures</code> is <code>true</code>, Python and C++
signatures are automatically added. If both signatures are automatically added. If both
<code>show_user_defined</code> and <code>show_signatures</code> <code>show_user_defined</code> and <code>show_signatures</code>
are <code>false</code>, the <code>__doc__</code> attributes are are <code>false</code>, the <code>__doc__</code> attributes are
<code>None</code>.</dt> <code>None</code>.</dt>
</dl> </dl>
<pre>
docstring_options(bool show_user_defined, bool show_py_signatures, bool show_cpp_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_py_signatures</code> is <code>true</code>, Python
signatures are automatically added. Iff
<code>show_cpp_signatures</code> is <code>true</code>, C++
signatures are automatically added. If all parameters are
<code>false</code>, the <code>__doc__</code> attributes are
<code>None</code>.</dt>
</dl>
<h4><a name="docstring_options-spec-dtors" id= <h4><a name="docstring_options-spec-dtors" id=
"docstring_options-spec-dtors"></a>Class "docstring_options-spec-dtors"></a>Class
@@ -186,6 +217,10 @@ void disable_user_defined();
void enable_user_defined(); void enable_user_defined();
void disable_signatures(); void disable_signatures();
void enable_signatures(); void enable_signatures();
void disable_py_signatures();
void enable_py_signatures();
void disable_cpp_signatures();
void enable_cpp_signatures();
void disable_all(); void disable_all();
void enable_all(); void enable_all();
</pre> </pre>
@@ -196,7 +231,7 @@ void enable_all();
<code>*_user_defined()</code> and <code>*_signatures()</code> <code>*_user_defined()</code> and <code>*_signatures()</code>
member functions are provided for fine-grained control. The member functions are provided for fine-grained control. The
<code>*_all()</code> member functions are convenient shortcuts <code>*_all()</code> member functions are convenient shortcuts
to manipulate both settings simultaneously.</dt> to manipulate all settings simultaneously.</dt>
</dl> </dl>
<h2><a name="examples" id="examples"></a>Examples</h2> <h2><a name="examples" id="examples"></a>Examples</h2>
@@ -219,7 +254,7 @@ BOOST_PYTHON_MODULE(demo)
<pre> <pre>
&gt;&gt;&gt; import demo &gt;&gt;&gt; import demo
&gt;&gt;&gt; print demo.foo.__doc__ &gt;&gt;&gt; print demo.foo.__doc__
foo doc foo() -&gt; None : foo doc
C++ signature: C++ signature:
foo(void) -&gt; void foo(void) -&gt; void
</pre>If compiled with </pre>If compiled with
@@ -253,21 +288,33 @@ BOOST_PYTHON_MODULE(demo)
def("foo3", foo3, arg("f"), "foo3 doc"); def("foo3", foo3, arg("f"), "foo3 doc");
doc_options.enable_user_defined(); doc_options.enable_user_defined();
def("foo4", foo4, arg("d"), "foo4 doc"); def("foo4", foo4, arg("d"), "foo4 doc");
doc_options.enable_py_signatures();
def("foo5", foo4, arg("d"), "foo5 doc");
doc_options.disable_py_signatures();
doc_options.enable_cpp_signatures();
def("foo6", foo4, arg("d"), "foo6 doc");
} }
</pre>Python code: </pre>Python code:
<pre> <pre>
&gt;&gt;&gt; import demo &gt;&gt;&gt; import demo
&gt;&gt;&gt; print demo.foo1.__doc__ &gt;&gt;&gt; print demo.foo1.__doc__
foo1 doc foo1( (int)i) -&gt; int : foo1 doc
C++ signature: C++ signature:
foo1(int i) -&gt; int foo1(int i) -&gt; int
&gt;&gt;&gt; print demo.foo2.__doc__ &gt;&gt;&gt; print demo.foo2.__doc__
foo2( (int)l) -&gt; int :
C++ signature: C++ signature:
foo2(long l) -&gt; int foo2(long l) -&gt; int
&gt;&gt;&gt; print demo.foo3.__doc__ &gt;&gt;&gt; print demo.foo3.__doc__
None None
&gt;&gt;&gt; print demo.foo4.__doc__ &gt;&gt;&gt; print demo.foo4.__doc__
foo4 doc foo4 doc
&gt;&gt;&gt; print demo.foo5.__doc__
foo5( (float)d) -&gt; int : foo5 doc
&gt;&gt;&gt; print demo.foo6.__doc__
foo6 doc
C++ signature:
foo6(double d) -&gt; int
</pre> </pre>
<h4>Wrapping from multiple C++ scopes</h4> <h4>Wrapping from multiple C++ scopes</h4>
@@ -333,7 +380,7 @@ bar2 doc
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p> <!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/ralf_w_grosse_kunstleve.htm">Ralf W. "http://www.boost.org/people/ralf_w_grosse_kunstleve.htm">Ralf W.
Grosse-Kunstleve</a> 2006.</i></p> Grosse-Kunstleve</a> 2006.</i></p>
</body> </body>
</html> </html>

View File

@@ -89,7 +89,7 @@ namespace boost { namespace python
template &lt;class T&gt; template &lt;class T&gt;
class enum_ : public <a href="object.html#object-spec">object</a> class enum_ : public <a href="object.html#object-spec">object</a>
{ {
enum_(char const* name); enum_(char const* name, char const* doc = 0);
enum_&lt;T&gt;&amp; value(char const* name, T); enum_&lt;T&gt;&amp; value(char const* name, T);
enum_&lt;T&gt;&amp; export_values(); enum_&lt;T&gt;&amp; export_values();
}; };
@@ -99,7 +99,7 @@ namespace boost { namespace python
<h4><a name="enum_-spec-ctors"></a>Class template <code>enum_</code> <h4><a name="enum_-spec-ctors"></a>Class template <code>enum_</code>
constructors</h4> constructors</h4>
<pre> <pre>
enum_(char const* name); enum_(char const* name, char const* doc=0);
</pre> </pre>
<dl class="function-semantics"> <dl class="function-semantics">
@@ -131,7 +131,7 @@ inline enum_&lt;T&gt;&amp; value(char const* name, T x);
<dt><b>Effects:</b> adds an instance of the wrapped enumeration <dt><b>Effects:</b> adds an instance of the wrapped enumeration
type with value <code>x</code> to the type's dictionary as the type with value <code>x</code> to the type's dictionary as the
<code>name</code>d attribute</dt>. <code>name</code>d attribute.</dt>
<dt><b>Returns:</b> <code>*this</code></dt> <dt><b>Returns:</b> <code>*this</code></dt>
@@ -146,7 +146,7 @@ inline enum_&lt;T&gt;&amp; export_values();
<dt><b>Effects:</b> sets attributes in the current <a <dt><b>Effects:</b> sets attributes in the current <a
href="scope.html#scope-spec"><code>scope</code></a> with the href="scope.html#scope-spec"><code>scope</code></a> with the
same names and values as all enumeration values exposed so far same names and values as all enumeration values exposed so far
by calling <code>value()</code></dt>. by calling <code>value()</code>.</dt>
<dt><b>Returns:</b> <code>*this</code></dt> <dt><b>Returns:</b> <code>*this</code></dt>
@@ -228,7 +228,7 @@ TypeError: bad argument type for built-in operation
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -283,7 +283,7 @@ same_name2(PyObject* args, PyObject* keywords)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -144,7 +144,7 @@ BOOST_PYTHON_MODULE(exception_translator_ext)
<p>Revised 03 October, 2002</p> <p>Revised 03 October, 2002</p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -226,7 +226,7 @@ BOOST_PYTHON_MODULE(extract_ext)
<p>Revised 15 November, 2002</p> <p>Revised 15 November, 2002</p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -856,6 +856,6 @@ BOOST_PYTHON_MODULE(custom_string)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002-2006.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002-2006.</i></p>
</body> </body>
</html> </html>

View File

@@ -361,7 +361,7 @@
<p class="c3">&copy; Copyright <a href= <p class="c3">&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002. Distributed "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002. Distributed
under the Boost Software License, Version 1.0. (See accompanying file 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> LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</p>

View File

@@ -0,0 +1,216 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Copyright Nikolay Mladenov 2007. Distributed under the Boost -->
<!-- Software License, Version 1.0. (See accompanying -->
<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
<html>
<head>
<meta http-equiv="Content-Type" content=
"text/html; charset=us-ascii">
<link rel="stylesheet" type="text/css" href="../boost.css">
<title>Boost.Python -
&lt;boost/python/doobject/function_doc_signature.hpp&gt;</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
&lt;boost/python/object/function_doc_signature.hpp&gt;</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="#function_doc_signature_generator-spec">Class
<code>function_doc_signature_generator</code></a></dt>
<dd>
<dl class="page-index">
<dt><a href="#function_doc_signature_generator-spec-synopsis">Class
<code>function_doc_signature_generator</code> synopsis</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 docstrings with automatic
appending of Pythonic and C++ signatures. This feature is implemented
by <code>class function_doc_signature_generator</code>
The class uses all of the overloads, supplied arg names and default values, as well as
the user-defined docstrings, to generate documentation for a given function.</p>
<h2><a name="classes" id="classes"></a>Classes</h2>
<h3><a name="function_doc_signature_generator-spec" id=
"function_doc_signature_generator-spec"></a>Class
<code>function_doc_signature_generator</code></h3>
<p>
The class has only one public function which returns a list of strings documenting the
overloads of a function.
</p>
<h4><a name="function_doc_signature_generator-spec-synopsis" id=
"function_doc_signature_generator-spec-synopsis"></a>Class
<code>function_doc_signature_generator</code> synopsis</h4>
<pre>
namespace boost { namespace python { namespace objects {
class function_doc_signature_generator
{
public:
static list function_doc_signatures(function const *f);
};
}}}
</pre>
<h2><a name="examples" id="examples"></a>Examples</h2>
<h4>Docstrings generated with <code>function_doc_signature_generator</code></h4>
<pre>
#include &lt;boost/python/module.hpp&gt;
#include &lt;boost/python/def.hpp&gt;
#include &lt;boost/python/args.hpp&gt;
#include &lt;boost/python/tuple.hpp&gt;
#include &lt;boost/python/class.hpp&gt;
#include &lt;boost/python/overloads.hpp&gt;
#include &lt;boost/python/raw_function.hpp&gt;
using namespace boost::python;
tuple f(int x = 1, double y = 4.25, char const* z = "wow")
{
return make_tuple(x, y, z);
}
BOOST_PYTHON_FUNCTION_OVERLOADS(f_overloads, f, 0, 3)
struct X
{
tuple f(int x = 1, double y = 4.25, char const* z = "wow")
{
return make_tuple(x, y, z);
}
};
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_f_overloads, X::f, 0, 3)
tuple raw_func(tuple args, dict kw)
{
return make_tuple(args, kw);
}
BOOST_PYTHON_MODULE(args_ext)
{
def("f", f, (arg("x")=1, arg("y")=4.25, arg("z")="wow")
, "This is f's docstring"
);
def("raw", raw_function(raw_func));
def("f1", f, f_overloads("f1's docstring", args("x", "y", "z")));
class_&lt;X&gt;("X", "This is X's docstring", init&lt;&gt;(args("self")))
.def("f", &amp;X::f
, "This is X.f's docstring"
, args("self","x", "y", "z"))
;
}
</pre>
Python code:
<pre>
&gt;&gt;&gt; import args_ext
&gt;&gt;&gt; help(args_ext)
Help on module args_ext:
NAME
args_ext
FILE
args_ext.pyd
CLASSES
Boost.Python.instance(__builtin__.object)
X
class X(Boost.Python.instance)
| This is X's docstring
|
| Method resolution order:
| X
| Boost.Python.instance
| __builtin__.object
|
| Methods defined here:
|
| __init__(...)
| __init__( (object)self) -> None :
| C++ signature:
| void __init__(struct _object *)
|
| f(...)
| f( (X)self, (int)x, (float)y, (str)z) -> tuple : This is X.f's docstring
| C++ signature:
| class boost::python::tuple f(struct X {lvalue},int,double,char const *)
|
| .................
|
FUNCTIONS
f(...)
f([ (int)x=1 [, (float)y=4.25 [, (str)z='wow']]]) -> tuple : This is f's docstring
C++ signature:
class boost::python::tuple f([ int=1 [,double=4.25 [,char const *='wow']]])
f1(...)
f1([ (int)x [, (float)y [, (str)z]]]) -> tuple : f1's docstring
C++ signature:
class boost::python::tuple f1([ int [,double [,char const *]]])
raw(...)
object raw(tuple args, dict kwds) :
C++ signature:
object raw(tuple args, dict kwds)
</pre>
<p><i>&copy; Copyright <a href="mailto:nickm at sitius dot com">Nikolay Mladenov</a> 2007.</i></p>
</body>
</html>

View File

@@ -329,7 +329,7 @@ null_ok&lt;T&gt;* allow_null(T* p)
</p> </p>
<p class="c4">&copy; Copyright <a href= <p class="c4">&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002 "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002
.</p> .</p>
</body> </body>
</html> </html>

View File

@@ -218,7 +218,7 @@ BOOST_PYTHON_MODULE(back_references)
</p> </p>
<p class="c3">&copy; Copyright <a href= <p class="c3">&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002 "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002
.</p> .</p>
</body> </body>
</html> </html>

View File

@@ -157,7 +157,7 @@ BOOST_PYTHON_MODULE(implicit_ext)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -245,7 +245,7 @@ class_&lt;X&gt;("X", "This is X's docstring.",
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -213,7 +213,7 @@ struct pointer_holder : instance_holder
<p class="c4">&copy; Copyright <a href= <p class="c4">&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002. "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.
</body> </body>
</html> </html>

View File

@@ -392,7 +392,7 @@ BOOST_PYTHON_MODULE(demo)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -136,7 +136,7 @@ long zeroes(list l)
<p>Revised 1 October, 2002</p> <p>Revised 1 October, 2002</p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -113,7 +113,7 @@ python::long_ fact(long n)
<p>Revised 1 October, 2002</p> <p>Revised 1 October, 2002</p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -295,7 +295,7 @@ BOOST_PYTHON_MODULE(noddy_cache)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -96,10 +96,10 @@ template &lt;class F, class Policies, class Keywords, class Signature&gt;
to <code>f</code>. <ul> to <code>f</code>. <ul>
<li> If <code>policies</code> are supplied, it <li> If <code>policies</code> are supplied, it
will be applied to the function as described <a href= will be applied to the function as described <a href=
"CallPolicies.html">here</a>. "CallPolicies.html">here</a>.
<li>If <code>keywords</code> are <li>If <code>keywords</code> are
supplied, the keywords will be applied in order to the final supplied, the keywords will be applied in order to the final
arguments of the resulting function. arguments of the resulting function.
<li>If <code>Signature</code> <li>If <code>Signature</code>
is supplied, it should be an instance of an <a is supplied, it should be an instance of an <a
href="../../../mpl/doc/refmanual/front-extensible-sequence.html">MPL front-extensible href="../../../mpl/doc/refmanual/front-extensible-sequence.html">MPL front-extensible
@@ -125,36 +125,33 @@ template &lt;class F, class Policies, class Keywords, class Signature&gt;
</dl> </dl>
<pre> <pre>
<a name= <a name="make_constructor-spec">template &lt;class F&gt;</a>
"make_constructor-spec"></a>template &lt;class T, class ArgList, class Generator&gt; <a href="object.html#object-spec">object</a> make_constructor(F f)
<a href="object.html#object-spec">object</a> make_constructor();
template &lt;class F, class Policies&gt;
template &lt;class ArgList, class Generator, class Policies&gt;
<a href= <a href=
"object.html#object-spec">object</a> make_constructor(Policies const&amp; policies) "object.html#object-spec">object</a> make_constructor(F f, Policies const&amp; policies)
template &lt;class F, class Policies, class KeywordsOrSignature&gt;
<a href=
"object.html#object-spec">object</a> make_constructor(F f, Policies const&amp; policies, KeywordsOrSignature const&amp; ks)
template &lt;class F, class Policies, class Keywords, class Signature&gt;
<a href=
"object.html#object-spec">object</a> make_constructor(F f, Policies const&amp; policies, Keywords const&amp; kw, Signature const&amp; sig)
</pre> </pre>
<dl class="function-semantics"> <dl class="function-semantics">
<dt><b>Requires:</b> <code>T</code> is a class type. <dt><b>Requires:</b> <code>F</code> is a
<code>Policies</code> is a model of <a href= function pointer type. If <code>policies</code> are supplied, it must
"CallPolicies.html">CallPolicies</a>. <code>ArgList</code> is an <a be a model of <a href="CallPolicies.html">CallPolicies</a>. If
href="../../../mpl/doc/refmanual/forward-sequence.html">MPL sequence</a> of C++ argument <code>kewords</code> are supplied, it must be the result of a <a href=
types (<i>A1,&nbsp;A2,...&nbsp;AN</i>) such that if "args.html#keyword-expression"><em>keyword-expression</em></a>
<code>a1,&nbsp;a2</code>...&nbsp;<code>aN</code> are objects of type specifying no more arguments than the <a href=
<i>A1,&nbsp;A2,...&nbsp;AN</i> respectively, the expression <code>new "definitions.html#arity">arity</a> of <code>f</code>.</dt>
Generator::apply&lt;T&gt;::type(a1,&nbsp;a2</code>...&nbsp;<code>aN</code>)
is valid. Generator is a model of <a href=
"HolderGenerator.html">HolderGenerator</a>.</dt>
<dt><b>Effects:</b> Creates a Python callable object which, when called <dt><b>Effects:</b> Creates a Python callable object which, when called
from Python, expects its first argument to be a Boost.Python extension from Python, converts its arguments to C++ and calls <code>f</code>.</dt>
class object. It converts its remaining its arguments to C++ and passes
them to the constructor of a dynamically-allocated
<code>Generator::apply&lt;T&gt;::type</code> object, which is then
installed in the extension class object. In the second form, the
<code>policies</code> are applied to the arguments and result (<a href=
"http://www.python.org/doc/current/lib/bltin-null-object.html">None</a>)
of the Python callable object</dt>
<dt><b>Returns:</b> An instance of <a href= <dt><b>Returns:</b> An instance of <a href=
"object.html#object-spec">object</a> which holds the new Python "object.html#object-spec">object</a> which holds the new Python
@@ -186,7 +183,7 @@ BOOST_PYTHON_MODULE(make_function_test)
def("choose_function", choose_function); def("choose_function", choose_function);
} }
</pre> </pre>
It can be used this way in Python: It can be used this way in Python:
<pre> <pre>
&gt;&gt;&gt; from make_function_test import * &gt;&gt;&gt; from make_function_test import *
&gt;&gt;&gt; f = choose_function(1) &gt;&gt;&gt; f = choose_function(1)
@@ -204,7 +201,7 @@ BOOST_PYTHON_MODULE(make_function_test)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -139,7 +139,7 @@ BOOST_PYTHON_MODULE(my_module)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -103,7 +103,7 @@ RuntimeError: Unidentifiable C++ Exception
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002. </i> Distributed Abrahams</a> 2002. </i> Distributed
under the Boost Software License, Version 1.0. (See accompanying file 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> LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</p>

View File

@@ -270,7 +270,7 @@ void set_first_element(numeric::array&amp; y, double value)
<p>Revised 07 October, 2006</p> <p>Revised 07 October, 2006</p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002-2006.</i></p> Abrahams</a> 2002-2006.</i></p>
</body> </body>
</html> </html>

View File

@@ -74,6 +74,32 @@
</dl> </dl>
</dd> </dd>
<dt><a href="#const_objattribute_policies-spec">Class
<code>const_objattribute_policies</code></a></dt>
<dd>
<dl class="page-index">
<dt><a href="#const_objattribute_policies-spec-synopsis">Class
<code>const_objattribute_policies</code> synopsis</a></dt>
<dt><a href="#const_objattribute_policies-spec-statics">Class
<code>const_objattribute_policies</code> static functions</a></dt>
</dl>
</dd>
<dt><a href="#objattribute_policies-spec">Class
<code>objattribute_policies</code></a></dt>
<dd>
<dl class="page-index">
<dt><a href="#objattribute_policies-spec-synopsis">Class
<code>objattribute_policies</code> synopsis</a></dt>
<dt><a href="#objattribute_policies-spec-statics">Class
<code>objattribute_policies</code> static functions</a></dt>
</dl>
</dd>
<dt><a href="#const_item_policies-spec">Class <dt><a href="#const_item_policies-spec">Class
<code>const_item_policies</code></a></dt> <code>const_item_policies</code></a></dt>
@@ -328,6 +354,102 @@ static void del(object const&amp;target, char const* key);
<!-- end --> <!-- end -->
<!-- begin --> <!-- begin -->
<h3><a name="const_objattribute_policies-spec"></a>Class
<code>const_objattribute_policies</code></h3>
<p>The policies which are used for proxies representing an attribute
access to a <code>const&nbsp;object</code> when the attribute name is
given as a <code>const&nbsp;object</code>.</p>
<h4><a name="const_objattribute_policies-spec-synopsis"></a>Class
<code>const_objattribute_policies</code> synopsis</h4>
<pre>
namespace boost { namespace python { namespace api
{
struct const_objattribute_policies
{
typedef object const& key_type;
static object get(object const&amp; target, object const& key);
};
}}}
</pre>
<h4><a name="const_objattribute_policies-spec-statics"></a>Class
<code>const_objattribute_policies</code> static functions</h4>
<pre>
static object get(object const&amp; target, object const& key);
</pre>
<dl class="function-semantics">
<dt><b>Requires:</b> <code>key</code> is an <code>object</code>
holding a string.</dt>
<dt><b>Effects:</b> accesses the attribute of <code>target</code> named
by <code>key</code>.</dt>
<dt><b>Returns:</b> An <code>object</code> managing the result of the
attribute access.</dt>
<dt><b>Throws:</b> <code><a href=
"errors.html#error_already_set-spec">error_already_set</a></code> if a
Python exception is raised.</dt>
</dl>
<h3><a name="objattribute_policies-spec"></a>Class
<code>objattribute_policies</code></h3>
<p>The policies which are used for proxies representing an attribute
access to a mutable <code>object</code> when the attribute name is
given as a <code>const&nbsp;object</code>.</p>
<h4><a name="objattribute_policies-spec-synopsis"></a>Class
<code>objattribute_policies</code> synopsis</h4>
<pre>
namespace boost { namespace python { namespace api
{
struct objattribute_policies : const_objattribute_policies
{
static object const&amp; set(object const&amp; target, object const& key, object const&amp; value);
static void del(object const&amp;target, object const& key);
};
}}}
</pre>
<h4><a name="objattribute_policies-spec-statics"></a>Class
<code>objattribute_policies</code> static functions</h4>
<pre>
static object const&amp; set(object const&amp; target, object const& key, object const&amp; value);
</pre>
<dl class="function-semantics">
<dt><b>Requires:</b> <code>key</code> is an <code>object</code>
holding a string.</dt>
<dt><b>Effects:</b> sets the attribute of <code>target</code> named by
<code>key</code> to <code>value</code>.</dt>
<dt><b>Throws:</b> <code><a href=
"errors.html#error_already_set-spec">error_already_set</a></code> if a
Python exception is raised.</dt>
</dl>
<pre>
static void del(object const&amp;target, object const& key);
</pre>
<dl class="function-semantics">
<dt><b>Requires:</b> <code>key</code> is an <code>object</code>
holding a string.</dt>
<dt><b>Effects:</b> deletes the attribute of <code>target</code> named
by <code>key</code>.</dt>
<dt><b>Throws:</b> <code><a href=
"errors.html#error_already_set-spec">error_already_set</a></code> if a
Python exception is raised.</dt>
</dl>
<!-- end -->
<!-- begin -->
<h3><a name="const_item_policies-spec"></a>Class <h3><a name="const_item_policies-spec"></a>Class
<code>const_item_policies</code></h3> <code>const_item_policies</code></h3>
@@ -533,6 +655,11 @@ namespace boost { namespace python { namespace api
template &lt;class A0, class A1,...class An&gt; template &lt;class A0, class A1,...class An&gt;
object operator()(A0 const&amp;, A1 const&amp;,...An const&amp;) const; object operator()(A0 const&amp;, A1 const&amp;,...An const&amp;) const;
detail::args_proxy operator* () const;
object operator()(detail::args_proxy const &amp;args) const;
object operator()(detail::args_proxy const &amp;args,
detail::kwds_proxy const &amp;kwds) const;
// truth value testing // truth value testing
// //
typedef unspecified bool_type; typedef unspecified bool_type;
@@ -542,6 +669,8 @@ namespace boost { namespace python { namespace api
// //
proxy&lt;const_object_attribute&gt; attr(char const*) const; proxy&lt;const_object_attribute&gt; attr(char const*) const;
proxy&lt;object_attribute&gt; attr(char const*); proxy&lt;object_attribute&gt; attr(char const*);
proxy&lt;const_object_objattribute&gt; attr(object const&) const;
proxy&lt;object_objattribute&gt; attr(object const&);
// item access // item access
// //
@@ -580,6 +709,25 @@ object operator()(A0 const&amp; a1, A1 const&amp; a2,...An const&amp; aN) const;
call&lt;object&gt;(object(*static_cast&lt;U*&gt;(this)).ptr(), a1, call&lt;object&gt;(object(*static_cast&lt;U*&gt;(this)).ptr(), a1,
a2,...aN)</dt> a2,...aN)</dt>
</dl> </dl>
<pre>
object operator()(detail::args_proxy const &amp;args) const;
</pre>
<dl class="function-semantics">
<dt><b>Effects:</b>
call object with arguments given by the tuple <varname>args</varname></dt>
</dl>
<pre>
object operator()(detail::args_proxy const &amp;args,
detail::kwds_proxy const &amp;kwds) const;
</pre>
<dl class="function-semantics">
<dt><b>Effects:</b>
call object with arguments given by the tuple <varname>args</varname>, and named
arguments given by the dictionary <varname>kwds</varname></dt>
</dl>
<pre> <pre>
operator bool_type() const; operator bool_type() const;
</pre> </pre>
@@ -608,6 +756,21 @@ proxy&lt;object_attribute&gt; attr(char const* name);
<code>name</code> as its key.</dt> <code>name</code> as its key.</dt>
</dl> </dl>
<pre> <pre>
proxy&lt;const_object_objattribute&gt; attr(const object& name) const;
proxy&lt;object_objattribute&gt; attr(const object& name);
</pre>
<dl class="function-semantics">
<dt><b>Requires:</b> name is a <code>object</code> holding a string.</dt>
<dt><b>Effects:</b> accesses the named attribute of
<code>*this</code>.</dt>
<dt><b>Returns:</b> a proxy object which binds
<code>object(*static_cast&lt;U*&gt;(this))</code> as its target, and
<code>name</code> as its key.</dt>
</dl>
<pre>
template &lt;class T&gt; template &lt;class T&gt;
proxy&lt;const_object_item&gt; operator[](T const&amp; key) const; proxy&lt;const_object_item&gt; operator[](T const&amp; key) const;
template &lt;class T&gt; template &lt;class T&gt;
@@ -672,6 +835,8 @@ namespace boost { namespace python { namespace api
object&amp; operator=(object const&amp;); object&amp; operator=(object const&amp;);
PyObject* ptr() const; PyObject* ptr() const;
bool is_none() const;
}; };
}}} }}}
</pre> </pre>
@@ -732,6 +897,14 @@ PyObject* ptr() const;
<dt><b>Returns:</b> a pointer to the internally-held Python <dt><b>Returns:</b> a pointer to the internally-held Python
object.</dt> object.</dt>
</dl> </dl>
<pre>
bool is_none() const;
</pre>
<dl class="function-semantics">
<dt><b>Returns:</b> result of (ptr() == Py_None)</dt>
</dl>
<!-- --> <!-- -->
<h3><a name="proxy-spec"></a>Class template <code>proxy</code></h3> <h3><a name="proxy-spec"></a>Class template <code>proxy</code></h3>
@@ -938,11 +1111,11 @@ object sum_items(object seq)
</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 January, 2006 15 March, 2010
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2006.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2008.</i></p>
</body> </body>
</html> </html>

View File

@@ -915,7 +915,7 @@ BOOST_PYTHON_MODULE(demo)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -223,7 +223,7 @@ BOOST_PYTHON_MODULE(args_ext)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -1,112 +1,93 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<title>Boost.Python Pickle Support</title> <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Boost.Python Pickle Support</title>
</head>
<div> <body>
<div>
<img src="../../../../boost.png" alt="boost.png (6897 bytes)" align=
"center" width="277" height="86" />
<hr />
<img src="../../../../boost.png" <h1>Boost.Python Pickle Support</h1>Pickle is a Python module for object
alt="boost.png (6897 bytes)" serialization, also known as persistence, marshalling, or flattening.
align="center"
width="277" height="86">
<hr> <p>It is often necessary to save and restore the contents of an object to
<h1>Boost.Python Pickle Support</h1> a file. One approach to this problem is to write a pair of functions that
read and write data from a file in a special format. A powerful
alternative approach is to use Python's pickle module. Exploiting
Python's ability for introspection, the pickle module recursively
converts nearly arbitrary Python objects into a stream of bytes that can
be written to a file.</p>
Pickle is a Python module for object serialization, also known <p>The Boost Python Library supports the pickle module through the
as persistence, marshalling, or flattening. interface as described in detail in the <a href=
"http://www.python.org/doc/current/lib/module-pickle.html">Python Library
Reference for pickle.</a> This interface involves the special methods
<tt>__getinitargs__</tt>, <tt>__getstate__</tt> and <tt>__setstate__</tt>
as described in the following. Note that Boost.Python is also fully
compatible with Python's cPickle module.</p>
<hr />
<p> <h2>The Boost.Python Pickle Interface</h2>At the user level, the
It is often necessary to save and restore the contents of an object to Boost.Python pickle interface involves three special methods:
a file. One approach to this problem is to write a pair of functions
that read and write data from a file in a special format. A powerful
alternative approach is to use Python's pickle module. Exploiting
Python's ability for introspection, the pickle module recursively
converts nearly arbitrary Python objects into a stream of bytes that
can be written to a file.
<p> <dl>
The Boost Python Library supports the pickle module <dt><strong><tt>__getinitargs__</tt></strong></dt>
through the interface as described in detail in the
<a href="http://www.python.org/doc/current/lib/module-pickle.html"
>Python Library Reference for pickle.</a> This interface
involves the special methods <tt>__getinitargs__</tt>,
<tt>__getstate__</tt> and <tt>__setstate__</tt> as described
in the following. Note that Boost.Python is also fully compatible
with Python's cPickle module.
<hr> <dd>
<h2>The Boost.Python Pickle Interface</h2> When an instance of a Boost.Python extension class is pickled, the
pickler tests if the instance has a <tt>__getinitargs__</tt> method.
This method must return a Python tuple (it is most convenient to use
a boost::python::tuple). When the instance is restored by the
unpickler, the contents of this tuple are used as the arguments for
the class constructor.
At the user level, the Boost.Python pickle interface involves three special <p>If <tt>__getinitargs__</tt> is not defined, <tt>pickle.load</tt>
methods: will call the constructor (<tt>__init__</tt>) without arguments;
i.e., the object must be default-constructible.</p>
</dd>
<dl> <dt><strong><tt>__getstate__</tt></strong></dt>
<dt>
<strong><tt>__getinitargs__</tt></strong>
<dd>
When an instance of a Boost.Python extension class is pickled, the
pickler tests if the instance has a <tt>__getinitargs__</tt> method.
This method must return a Python tuple (it is most convenient to use
a boost::python::tuple). When the instance is restored by the
unpickler, the contents of this tuple are used as the arguments for
the class constructor.
<p> <dd>When an instance of a Boost.Python extension class is pickled, the
If <tt>__getinitargs__</tt> is not defined, <tt>pickle.load</tt> pickler tests if the instance has a <tt>__getstate__</tt> method. This
will call the constructor (<tt>__init__</tt>) without arguments; method should return a Python object representing the state of the
i.e., the object must be default-constructible. instance.</dd>
<p> <dt><strong><tt>__setstate__</tt></strong></dt>
<dt>
<strong><tt>__getstate__</tt></strong>
<dd> <dd>When an instance of a Boost.Python extension class is restored by
When an instance of a Boost.Python extension class is pickled, the the unpickler (<tt>pickle.load</tt>), it is first constructed using the
pickler tests if the instance has a <tt>__getstate__</tt> method. result of <tt>__getinitargs__</tt> as arguments (see above).
This method should return a Python object representing the state of Subsequently the unpickler tests if the new instance has a
the instance. <tt>__setstate__</tt> method. If so, this method is called with the
result of <tt>__getstate__</tt> (a Python object) as the argument.</dd>
</dl>The three special methods described above may be <tt>.def()</tt>'ed
individually by the user. However, Boost.Python provides an easy to use
high-level interface via the
<strong><tt>boost::python::pickle_suite</tt></strong> class that also
enforces consistency: <tt>__getstate__</tt> and <tt>__setstate__</tt>
must be defined as pairs. Use of this interface is demonstrated by the
following examples.
<hr />
<p> <h2>Examples</h2>There are three files in <tt>boost/libs/python/test</tt>
<dt> that show how to provide pickle support.
<strong><tt>__setstate__</tt></strong> <hr />
<dd> <h3><a href="../../test/pickle1.cpp"><tt>pickle1.cpp</tt></a></h3>The C++
When an instance of a Boost.Python extension class is restored by the class in this example can be fully restored by passing the appropriate
unpickler (<tt>pickle.load</tt>), it is first constructed using the argument to the constructor. Therefore it is sufficient to define the
result of <tt>__getinitargs__</tt> as arguments (see above). Subsequently pickle interface method <tt>__getinitargs__</tt>. This is done in the
the unpickler tests if the new instance has a <tt>__setstate__</tt> following way:
method. If so, this method is called with the result of
<tt>__getstate__</tt> (a Python object) as the argument.
</dl> <ul>
<li>1. Definition of the C++ pickle function:
The three special methods described above may be <tt>.def()</tt>'ed <pre>
individually by the user. However, Boost.Python provides an easy to use
high-level interface via the
<strong><tt>boost::python::pickle_suite</tt></strong> class that also
enforces consistency: <tt>__getstate__</tt> and <tt>__setstate__</tt>
must be defined as pairs. Use of this interface is demonstrated by the
following examples.
<hr>
<h2>Examples</h2>
There are three files in
<tt>boost/libs/python/test</tt> that show how to
provide pickle support.
<hr>
<h3><a href="../../test/pickle1.cpp"><tt>pickle1.cpp</tt></a></h3>
The C++ class in this example can be fully restored by passing the
appropriate argument to the constructor. Therefore it is sufficient
to define the pickle interface method <tt>__getinitargs__</tt>.
This is done in the following way:
<ul>
<li>1. Definition of the C++ pickle function:
<pre>
struct world_pickle_suite : boost::python::pickle_suite struct world_pickle_suite : boost::python::pickle_suite
{ {
static static
@@ -117,26 +98,28 @@ provide pickle support.
} }
}; };
</pre> </pre>
<li>2. Establishing the Python binding: </li>
<pre>
<li>2. Establishing the Python binding:
<pre>
class_&lt;world&gt;("world", args&lt;const std::string&amp;&gt;()) class_&lt;world&gt;("world", args&lt;const std::string&amp;&gt;())
// ... // ...
.def_pickle(world_pickle_suite()) .def_pickle(world_pickle_suite())
// ... // ...
</pre> </pre>
</ul> </li>
</ul>
<hr />
<hr> <h3><a href="../../test/pickle2.cpp"><tt>pickle2.cpp</tt></a></h3>The C++
<h3><a href="../../test/pickle2.cpp"><tt>pickle2.cpp</tt></a></h3> class in this example contains member data that cannot be restored by any
of the constructors. Therefore it is necessary to provide the
<tt>__getstate__</tt>/<tt>__setstate__</tt> pair of pickle interface
methods:
The C++ class in this example contains member data that cannot be <ul>
restored by any of the constructors. Therefore it is necessary to <li>1. Definition of the C++ pickle functions:
provide the <tt>__getstate__</tt>/<tt>__setstate__</tt> pair of <pre>
pickle interface methods:
<ul>
<li>1. Definition of the C++ pickle functions:
<pre>
struct world_pickle_suite : boost::python::pickle_suite struct world_pickle_suite : boost::python::pickle_suite
{ {
static static
@@ -161,92 +144,76 @@ provide pickle support.
} }
}; };
</pre> </pre>
<li>2. Establishing the Python bindings for the entire suite: </li>
<pre>
<li>2. Establishing the Python bindings for the entire suite:
<pre>
class_&lt;world&gt;("world", args&lt;const std::string&amp;&gt;()) class_&lt;world&gt;("world", args&lt;const std::string&amp;&gt;())
// ... // ...
.def_pickle(world_pickle_suite()) .def_pickle(world_pickle_suite())
// ... // ...
</pre> </pre>
</ul> </li>
</ul>
<p> <p>For simplicity, the <tt>__dict__</tt> is not included in the result of
For simplicity, the <tt>__dict__</tt> is not included in the result <tt>__getstate__</tt>. This is not generally recommended, but a valid
of <tt>__getstate__</tt>. This is not generally recommended, but a approach if it is anticipated that the object's <tt>__dict__</tt> will
valid approach if it is anticipated that the object's always be empty. Note that the safety guard described below will catch
<tt>__dict__</tt> will always be empty. Note that the safety guard the cases where this assumption is violated.</p>
described below will catch the cases where this assumption is violated. <hr />
<hr> <h3><a href="../../test/pickle3.cpp"><tt>pickle3.cpp</tt></a></h3>This
<h3><a href="../../test/pickle3.cpp"><tt>pickle3.cpp</tt></a></h3> example is similar to <a href=
"../../test/pickle2.cpp"><tt>pickle2.cpp</tt></a>. However, the object's
<tt>__dict__</tt> is included in the result of <tt>__getstate__</tt>.
This requires a little more code but is unavoidable if the object's
<tt>__dict__</tt> is not always empty.
<hr />
This example is similar to <a <h2>Pitfall and Safety Guard</h2>The pickle protocol described above has
href="../../test/pickle2.cpp"><tt>pickle2.cpp</tt></a>. However, the an important pitfall that the end user of a Boost.Python extension module
object's <tt>__dict__</tt> is included in the result of might not be aware of:
<tt>__getstate__</tt>. This requires a little more code but is
unavoidable if the object's <tt>__dict__</tt> is not always empty.
<hr> <p><strong><tt>__getstate__</tt> is defined and the instance's
<h2>Pitfall and Safety Guard</h2> <tt>__dict__</tt> is not empty.</strong></p>
The pickle protocol described above has an important pitfall that the <p>The author of a Boost.Python extension class might provide a
end user of a Boost.Python extension module might not be aware of: <tt>__getstate__</tt> method without considering the possibilities
<p> that:</p>
<strong>
<tt>__getstate__</tt> is defined and the instance's <tt>__dict__</tt>
is not empty.
</strong>
<p>
The author of a Boost.Python extension class might provide a <ul>
<tt>__getstate__</tt> method without considering the possibilities <li>his class is used in Python as a base class. Most likely the
that: <tt>__dict__</tt> of instances of the derived class needs to be pickled
in order to restore the instances correctly.</li>
<p> <li>the user adds items to the instance's <tt>__dict__</tt> directly.
<ul> Again, the <tt>__dict__</tt> of the instance then needs to be
<li> pickled.</li>
his class is used in Python as a base class. Most likely the </ul>
<tt>__dict__</tt> of instances of the derived class needs to be
pickled in order to restore the instances correctly.
<p> <p>To alert the user to this highly unobvious problem, a safety guard is
<li> provided. If <tt>__getstate__</tt> is defined and the instance's
the user adds items to the instance's <tt>__dict__</tt> directly. <tt>__dict__</tt> is not empty, Boost.Python tests if the class has an
Again, the <tt>__dict__</tt> of the instance then needs to be attribute <tt>__getstate_manages_dict__</tt>. An exception is raised if
pickled. this attribute is not defined:</p>
<pre>
</ul>
<p>
To alert the user to this highly unobvious problem, a safety guard is
provided. If <tt>__getstate__</tt> is defined and the instance's
<tt>__dict__</tt> is not empty, Boost.Python tests if the class has
an attribute <tt>__getstate_manages_dict__</tt>. An exception is
raised if this attribute is not defined:
<pre>
RuntimeError: Incomplete pickle support (__getstate_manages_dict__ not set) RuntimeError: Incomplete pickle support (__getstate_manages_dict__ not set)
</pre> </pre>To resolve this problem, it should first be established that the <tt>
__getstate__</tt> and <tt>__setstate__</tt> methods manage the
To resolve this problem, it should first be established that the instances's <tt>__dict__</tt> correctly. Note that this can be done
<tt>__getstate__</tt> and <tt>__setstate__</tt> methods manage the either at the C++ or the Python level. Finally, the safety guard should
instances's <tt>__dict__</tt> correctly. Note that this can be done intentionally be overridden. E.g. in C++ (from <a href=
either at the C++ or the Python level. Finally, the safety guard "../../test/pickle3.cpp"><tt>pickle3.cpp</tt></a>):
should intentionally be overridden. E.g. in C++ (from <pre>
<a href="../../test/pickle3.cpp"><tt>pickle3.cpp</tt></a>):
<pre>
struct world_pickle_suite : boost::python::pickle_suite struct world_pickle_suite : boost::python::pickle_suite
{ {
// ... // ...
static bool getstate_manages_dict() { return true; } static bool getstate_manages_dict() { return true; }
}; };
</pre> </pre>Alternatively in Python:
<pre>
Alternatively in Python:
<pre>
import your_bpl_module import your_bpl_module
class your_class(your_bpl_module.your_class): class your_class(your_bpl_module.your_class):
__getstate_manages_dict__ = 1 __getstate_manages_dict__ = 1
@@ -255,54 +222,41 @@ is not empty.
def __setstate__(self, state): def __setstate__(self, state):
# your code here # your code here
</pre> </pre>
<hr />
<hr> <h2>Practical Advice</h2>
<h2>Practical Advice</h2>
<ul> <ul>
<li> <li>In Boost.Python extension modules with many extension classes,
In Boost.Python extension modules with many extension classes, providing complete pickle support for all classes would be a
providing complete pickle support for all classes would be a significant overhead. In general complete pickle support should only be
significant overhead. In general complete pickle support should implemented for extension classes that will eventually be pickled.</li>
only be implemented for extension classes that will eventually
be pickled.
<p> <li>Avoid using <tt>__getstate__</tt> if the instance can also be
<li> reconstructed by way of <tt>__getinitargs__</tt>. This automatically
Avoid using <tt>__getstate__</tt> if the instance can also be avoids the pitfall described above.</li>
reconstructed by way of <tt>__getinitargs__</tt>. This automatically
avoids the pitfall described above.
<p> <li>If <tt>__getstate__</tt> is required, include the instance's
<li> <tt>__dict__</tt> in the Python object that is returned.</li>
If <tt>__getstate__</tt> is required, include the instance's </ul>
<tt>__dict__</tt> in the Python object that is returned. <hr />
</ul> <h2>Light-weight alternative: pickle support implemented in Python</h2>
<hr> <h3><a href="../../test/pickle4.cpp"><tt>pickle4.cpp</tt></a></h3>The
<h2>Light-weight alternative: pickle support implemented in Python</h2> <tt>pickle4.cpp</tt> example demonstrates an alternative technique for
implementing pickle support. First we direct Boost.Python via the
<h3><a href="../../test/pickle4.cpp"><tt>pickle4.cpp</tt></a></h3> <tt>class_::enable_pickling()</tt> member function to define only the
basic attributes required for pickling:
The <tt>pickle4.cpp</tt> example demonstrates an alternative technique <pre>
for implementing pickle support. First we direct Boost.Python via
the <tt>class_::enable_pickling()</tt> member function to define only
the basic attributes required for pickling:
<pre>
class_&lt;world&gt;("world", args&lt;const std::string&amp;&gt;()) class_&lt;world&gt;("world", args&lt;const std::string&amp;&gt;())
// ... // ...
.enable_pickling() .enable_pickling()
// ... // ...
</pre> </pre>This enables the standard Python pickle interface as described in the
Python documentation. By "injecting" a <tt>__getinitargs__</tt> method into
This enables the standard Python pickle interface as described the definition of the wrapped class we make all instances pickleable:
in the Python documentation. By &quot;injecting&quot; a <pre>
<tt>__getinitargs__</tt> method into the definition of the wrapped
class we make all instances pickleable:
<pre>
# import the wrapped world class # import the wrapped world class
from pickle4_ext import world from pickle4_ext import world
@@ -312,18 +266,15 @@ class we make all instances pickleable:
# now inject __getinitargs__ (Python is a dynamic language!) # now inject __getinitargs__ (Python is a dynamic language!)
world.__getinitargs__ = world_getinitargs world.__getinitargs__ = world_getinitargs
</pre> </pre>See also the <a href=
"../tutorial/doc/html/python/techniques.html#python.extending_wrapped_objects_in_python">
tutorial section</a> on injecting additional methods from Python.
<hr />
© Copyright Ralf W. Grosse-Kunstleve 2001-2004. Distributed under the
Boost Software License, Version 1.0. (See accompanying file
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
See also the <p>Updated: Feb 2004.</p>
<a href="../tutorial/doc/html/python/techniques.html#python.extending_wrapped_objects_in_python" </div>
>tutorial section</a> on injecting additional methods from Python. </body>
</html>
<hr>
&copy; Copyright Ralf W. Grosse-Kunstleve 2001-2004. Distributed under
the Boost Software License, Version 1.0. (See accompanying file
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
<p>
Updated: Feb 2004.
</div>

View File

@@ -129,7 +129,7 @@
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -111,7 +111,7 @@ BOOST_PYTHON_MODULE(pointee_demo)
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002. </i> Distributed Abrahams</a> 2002. </i> Distributed
under the Boost Software License, Version 1.0. (See accompanying file 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> LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</p>

View File

@@ -41,7 +41,7 @@ design decisions and links to relevant discussions.
13 November, 2002 13 November, 2002
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>
2002. </i></p> 2002. </i></p>
</body> </body>
</html> </html>

View File

@@ -258,7 +258,7 @@ void pass_as_arg(expensive_to_copy* x, PyObject* f)
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002. </i> Distributed Abrahams</a> 2002. </i> Distributed
under the Boost Software License, Version 1.0. (See accompanying file 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> LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</p>

View File

@@ -104,7 +104,7 @@
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

370
doc/v2/pytype_function.html Normal file
View File

@@ -0,0 +1,370 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Copyright Nikolay Mladenov 2007. Distributed under the Boost -->
<!-- Software License, Version 1.0. (See accompanying -->
<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
<html>
<head>
<meta http-equiv="Content-Type" content=
"text/html; charset=us-ascii">
<link rel="stylesheet" type="text/css" href="../boost.css">
<title>Boost.Python -
&lt;boost/python/doobject/pytype_function.hpp&gt;</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
&lt;boost/python/converter/pytype_function.hpp&gt;</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="#wrap_pytype-spec">Class
<code>wrap_pytype</code></a></dt>
<dd>
<dl class="page-index">
<dt><a href="#wrap_pytype-spec-synopsis">Class
<code>wrap_pytype</code> synopsis</a></dt>
</dl>
</dd>
</dl>
</dd>
<dd>
<dl class="page-index">
<dt><a href="#registered_pytype-spec">Class
<code>registered_pytype</code></a></dt>
<dd>
<dl class="page-index">
<dt><a href="#registered_pytype-spec-synopsis">Class
<code>registered_pytype</code> synopsis</a></dt>
</dl>
</dd>
</dl>
</dd>
<dd>
<dl class="page-index">
<dt><a href="#expected_from_python_type-spec">Class
<code>expected_from_python_type</code></a></dt>
<dd>
<dl class="page-index">
<dt><a href="#expected_from_python_type-spec-synopsis">Class
<code>expected_from_python_type</code> synopsis</a></dt>
</dl>
</dd>
</dl>
</dd>
<dd>
<dl class="page-index">
<dt><a href="#to_python_target_type-spec">Class
<code>to_python_target_type</code></a></dt>
<dd>
<dl class="page-index">
<dt><a href="#to_python_target_type-spec-synopsis">Class
<code>to_python_target_type</code> synopsis</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>To support Pythonic signatures the converters should supply a <code>get_pytype</code> function
returning a pointer to the associated <code>PyTypeObject</code>. See for example
<a href="ResultConverter.html#ResultConverter-concept">ResultConverter</a> or
<a href="to_python_converter.html#to_python_converter-spec">to_python_converter</a>.
The classes in this header file are meant to be used when implmenting <code>get_pytype</code>.
There are also <code>_direct</code> versions of the templates of <code>class T</code> which
should be used with undecorated type parameter, expected to be in the conversion registry when the module loads.
</p>
<h2><a name="classes" id="classes"></a>Classes</h2>
<h3><a name="wrap_pytype-spec" id=
"wrap_pytype-spec"></a>Class
<code>wrap_pytype</code></h3>
<p>
This template generates a static <code>get_pytype</code> member returning the template parameter.
</p>
<h4><a name="wrap_pytype-spec-synopsis" id=
"wrap_pytype-spec-synopsis"></a>Class
<code>wrap_pytype</code> synopsis</h4>
<pre>
namespace boost { namespace python { namespace converter{
template &lt; PyTypeObject const *pytype &gt;
class wrap_pytype
{
public:
static PyTypeObject const *get_pytype(){return pytype; }
};
}}}
</pre>
<h3><a name="registered_pytype-spec" id=
"registered_pytype-spec"></a>Class
<code>registered_pytype</code></h3>
<p>
This template should be used with template parameters which are (possibly decorated)
types exported to python using <a href="class.html"><code>class_</code></a>.
The generated a static <code>get_pytype</code> member
returns the corresponding python type.
</p>
<h4><a name="registered_pytype-spec-synopsis" id=
"registered_pytype-spec-synopsis"></a>Class
<code>registered_pytype</code> synopsis</h4>
<pre>
namespace boost { namespace python { namespace converter{
template &lt; class T &gt;
class registered_pytype
{
public:
static PyTypeObject const *get_pytype();
};
}}}
</pre>
<h3><a name="expected_from_python_type-spec" id=
"expected_from_python_type-spec"></a>Class
<code>expected_from_python_type</code></h3>
<p>
This template generates a static <code>get_pytype</code> member which inspects the registered
<code>from_python</code> converters for the type <code>T</code> and returns a matching python type.
</p>
<h4><a name="expected_from_python_type-spec-synopsis" id=
"expected_from_python_type-spec-synopsis"></a>Class
<code>expected_from_python_type</code> synopsis</h4>
<pre>
namespace boost { namespace python { namespace converter{
template &lt; class T &gt;
class expected_from_python_type
{
public:
static PyTypeObject const *get_pytype();
};
}}}
</pre>
<h3><a name="to_python_target_type-spec" id=
"to_python_target_type-spec"></a>Class
<code>to_python_target_type</code></h3>
<p>
This template generates a static <code>get_pytype</code> member returning the
python type to which T can be converted.
</p>
<h4><a name="to_python_target_type-spec-synopsis" id=
"to_python_target_type-spec-synopsis"></a>Class
<code>to_python_target_type</code> synopsis</h4>
<pre>
namespace boost { namespace python { namespace converter{
template &lt; class T &gt;
class to_python_target_type
{
public:
static PyTypeObject const *get_pytype();
};
}}}
</pre>
<h2><a name="examples" id="examples"></a>Examples</h2>
This example presumes that someone has implemented the standard <a href=
"http://www.python.org/doc/2.2/ext/dnt-basics.html">noddy example
module</a> from the Python documentation, and placed the corresponding
declarations in <code>"noddy.h"</code>. Because
<code>noddy_NoddyObject</code> is the ultimate trivial extension type,
the example is a bit contrived: it wraps a function for which all
information is contained in the <i>type</i> of its return value.
<h3>C++ module definition</h3>
<pre>
#include &lt;boost/python/reference.hpp&gt;
#include &lt;boost/python/module.hpp&gt;
#include "noddy.h"
struct tag {};
tag make_tag() { return tag(); }
using namespace boost::python;
struct tag_to_noddy
#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //unnecessary overhead if py signatures are not supported
: wrap_pytype<&amp;noddy_NoddyType> //inherits get_pytype from wrap_pytype
#endif
{
static PyObject* convert(tag const&amp; x)
{
return PyObject_New(noddy_NoddyObject, &amp;noddy_NoddyType);
}
};
BOOST_PYTHON_MODULE(to_python_converter)
{
def("make_tag", make_tag);
to_python_converter&lt;tag, tag_to_noddy
#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //invalid if py signatures are not supported
, true
#endif
&gt;(); //"true" because tag_to_noddy has member get_pytype
}
</pre>
<p>The following example registers to and from python converters using the templates
<code>expected_from_python_type</code> and <code>to_pyhton_target_type</code>.
</p>
<pre>
#include &lt;boost/python/module.hpp&gt;
#include &lt;boost/python/def.hpp&gt;
#include &lt;boost/python/extract.hpp&gt;
#include &lt;boost/python/to_python_converter.hpp&gt;
#include &lt;boost/python/class.hpp&gt;
using namespace boost::python;
struct A
{
};
struct B
{
A a;
B(const A& a_):a(a_){}
};
// Converter from A to python int
struct BToPython
#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //unnecessary overhead if py signatures are not supported
: converter::to_python_target_type&lt;A&gt; //inherits get_pytype
#endif
{
static PyObject* convert(const B& b)
{
return incref(object(b.a).ptr());
}
};
// Conversion from python int to A
struct BFromPython
{
BFromPython()
{
boost::python::converter::registry::push_back
( &amp;convertible
, &amp;construct
, type_id&lt; B &gt;()
#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //invalid if py signatures are not supported
, &amp;converter::expected_from_python_type&lt;A&gt;::get_pytype//convertible to A can be converted to B
#endif
);
}
static void* convertible(PyObject* obj_ptr)
{
extract&lt;const A&&gt; ex(obj_ptr);
if (!ex.check()) return 0;
return obj_ptr;
}
static void construct(
PyObject* obj_ptr,
converter::rvalue_from_python_stage1_data* data)
{
void* storage = (
(converter::rvalue_from_python_storage&lt; B &gt;*)data)-&gt; storage.bytes;
extract&lt;const A&&gt; ex(obj_ptr);
new (storage) B(ex());
data->convertible = storage;
}
};
B func(const B& b) { return b ; }
BOOST_PYTHON_MODULE(pytype_function_ext)
{
to_python_converter&lt; B , BToPython
#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //invalid if py signatures are not supported
,true
#endif
&gt;(); //has get_pytype
BFromPython();
class_&lt;A&gt;("A") ;
def("func", &amp;func);
}
&gt;&gt;&gt; from pytype_function_ext import *
&gt;&gt;&gt; print func.__doc__
func( (A)arg1) -> A :
C++ signature:
struct B func(struct B)
</pre>
<p><i>&copy; Copyright <a href="mailto:nickm at sitius dot com">Nikolay Mladenov</a> 2007.</i></p>
</body>
</html>

View File

@@ -112,7 +112,7 @@ Python:
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -609,6 +609,66 @@
</dl> </dl>
</dd> </dd>
<dd>
<a name="function_documentation"></a>
<h3>Function documentation</h3>
<dl class="index">
<dt><a href=
"function_doc_signature.html">function_doc_signature.hpp</a></dt>
<dd>
<dl class="index">
<dt><a href=
"function_doc_signature.html#classes">Classes</a></dt>
<dd>
<dl class="index">
<dt><a href=
"function_doc_signature.html#function_doc_signature_generator-spec">function_doc_signature_generator</a></dt>
</dl>
</dd>
</dl>
</dd>
</dl>
<dl class="index">
<dt><a href=
"pytype_function.html">pytype_function.hpp</a></dt>
<dd>
<dl class="index">
<dt><a href=
"pytype_function.html#classes">Classes</a></dt>
<dd>
<dl class="index">
<dt><a href=
"pytype_function.html#wrap_pytype-spec">wrap_pytype</a></dt>
</dl>
<dl class="index">
<dt><a href=
"pytype_function.html#expected_from_python_type-spec">expected_from_python_type</a></dt>
</dl>
<dl class="index">
<dt><a href=
"pytype_function.html#to_python_target_type-spec">to_python_target_type</a></dt>
</dl>
<dl class="index">
<dt><a href=
"pytype_function.html#registered_pytype-spec">registered_pytype</a></dt>
</dl>
</dd>
</dl>
</dd>
</dl>
</dd>
<dd> <dd>
<a name="models_of_call_policies"></a> <a name="models_of_call_policies"></a>
@@ -1125,7 +1185,7 @@
</p> </p>
<p class="c3">&copy; Copyright <a href= <p class="c3">&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002 "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002
.</p> .</p>
</body> </body>
</html> </html>

View File

@@ -174,7 +174,7 @@ BOOST_PYTHON_MODULE(singleton)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -154,7 +154,7 @@ TypeError: No to_python (by-value) converter found for C++ type: class boost::sh
24 Jun, 2003 24 Jun, 2003
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>
2002. </i></p> 2002. </i></p>
</body> </body>
</html> </html>

View File

@@ -125,6 +125,8 @@ namespace boost { namespace python
{ {
static PyObject* postcall(PyObject*, PyObject* result); static PyObject* postcall(PyObject*, PyObject* result);
struct result_converter{ template &lt;class T&gt; struct apply; }; struct result_converter{ template &lt;class T&gt; struct apply; };
template &lt;class Sig&gt; struct extract_return_type : mpl::at_c&lt;Sig, arg_pos&gt;{};
}; };
}} }}
</pre> </pre>
@@ -215,7 +217,7 @@ BOOST_PYTHON_MODULE(return_self_ext)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> and Nikolay "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> and Nikolay
Mladenov 2003. </i></p> Mladenov 2003. </i></p>
</body> </body>
</html> </html>

View File

@@ -143,7 +143,7 @@ BOOST_PYTHON_MODULE(my_module)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -167,12 +167,13 @@ PyObject* postcall(PyObject* args, PyObject* result);
class Bar class Bar
{ {
public:
Bar(int x) : x(x) {} Bar(int x) : x(x) {}
int get_x() const { return x; } int get_x() const { return x; }
void set_x(int x) { this-&gt;x = x; } void set_x(int x) { this-&gt;x = x; }
private: private:
int x; int x;
} };
class Foo class Foo
{ {
@@ -189,7 +190,7 @@ class Foo
using namespace boost::python; using namespace boost::python;
BOOST_PYTHON_MODULE(internal_refs) BOOST_PYTHON_MODULE(internal_refs)
{ {
class_&lt;Bar&gt;("Bar") class_&lt;Bar&gt;("Bar", init&lt;int&gt;())
.def("get_x", &amp;Bar::get_x) .def("get_x", &amp;Bar::get_x)
.def("set_x", &amp;Bar::set_x) .def("set_x", &amp;Bar::set_x)
; ;
@@ -223,7 +224,7 @@ BOOST_PYTHON_MODULE(internal_refs)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -161,7 +161,7 @@ BOOST_PYTHON_MODULE(my_module)
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -167,7 +167,7 @@ BOOST_PYTHON_MODULE(nested)
<p>Revised 09 October, 2002</p> <p>Revised 09 October, 2002</p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -85,7 +85,7 @@ namespace boost { namespace python
object stop(); object stop();
object step(); object step();
// The return type of slice::get_indicies() // The return type of slice::get_indices()
template &lt;typename RandomAccessIterator&gt; template &lt;typename RandomAccessIterator&gt;
struct range struct range
{ {
@@ -96,7 +96,7 @@ namespace boost { namespace python
template &lt;typename RandomAccessIterator&gt; template &lt;typename RandomAccessIterator&gt;
range&lt;RandomAccessIterator&gt; range&lt;RandomAccessIterator&gt;
get_indicies( get_indices(
RandomAccessIterator const&amp; begin, RandomAccessIterator const&amp; begin,
RandomAccessIterator const&amp; end); RandomAccessIterator const&amp; end);
}; };
@@ -164,7 +164,7 @@ slice object, but in practice they are usually integers.</dt>
<pre> <pre>
template &lt;typename RandomAccessIterator&gt; template &lt;typename RandomAccessIterator&gt;
slice::range&lt;RandomAccessIterator&gt; slice::range&lt;RandomAccessIterator&gt;
slice::get_indicies( slice::get_indices(
RandomAccessIterator const&amp; begin, RandomAccessIterator const&amp; begin,
RandomAccessIterator const&amp; end) const; RandomAccessIterator const&amp; end) const;
</pre> </pre>
@@ -173,8 +173,8 @@ slice::get_indicies(
Iterators that form a half-open range.</dt> Iterators that form a half-open range.</dt>
<dt><b>Effects:</b> Create a RandomAccessIterator pair that defines a <dt><b>Effects:</b> Create a RandomAccessIterator pair that defines a
fully-closed range within the [begin,end) range of its arguments.&nbsp; fully-closed range within the [begin,end) range of its arguments.&nbsp;
This function translates this slice's indicies while accounting for the This function translates this slice's indices while accounting for the
effects of any PyNone or negative indicies, and non-singular step sizes.</dt> effects of any PyNone or negative indices, and non-singular step sizes.</dt>
<dt><b>Returns:</b> a slice::range <dt><b>Returns:</b> a slice::range
that has been initialized with a non-zero value of step and a pair of that has been initialized with a non-zero value of step and a pair of
RandomAccessIterators that point within the range of this functions RandomAccessIterators that point within the range of this functions
@@ -182,7 +182,7 @@ arguments and define a closed interval.</dt>
<dt><b>Throws:</b> <a href="definitions.html#raise">Raises</a> a Python <code>TypeError</code> exception if any of this slice's arguments <dt><b>Throws:</b> <a href="definitions.html#raise">Raises</a> a Python <code>TypeError</code> exception if any of this slice's arguments
are neither references to <code>PyNone</code> nor convertible to <code>int</code>.&nbsp; Throws are neither references to <code>PyNone</code> nor convertible to <code>int</code>.&nbsp; Throws
<code>std::invalid_argument</code> if the resulting range would be empty.&nbsp; You <code>std::invalid_argument</code> if the resulting range would be empty.&nbsp; You
should always wrap calls to <code>slice::get_indicies()</code> should always wrap calls to <code>slice::get_indices()</code>
within <code>try { ...; } catch (std::invalid_argument) {}</code> to within <code>try { ...; } catch (std::invalid_argument) {}</code> to
handle this case and take appropriate action.</dt> handle this case and take appropriate action.</dt>
<dt><b>Rationale</b>: closed-interval: If <dt><b>Rationale</b>: closed-interval: If
@@ -221,7 +221,7 @@ double partial_sum(std::vector&lt;double&gt; const&amp; Foo, const slice index)
{ {
slice::range&lt;std::vector&lt;double&gt;::const_iterator&gt; bounds; slice::range&lt;std::vector&lt;double&gt;::const_iterator&gt; bounds;
try { try {
bounds = index.get_indicies&lt;&gt;(Foo.begin(), Foo.end()); bounds = index.get_indices&lt;&gt;(Foo.begin(), Foo.end());
} }
catch (std::invalid_argument) { catch (std::invalid_argument) {
return 0.0; return 0.0;

View File

@@ -90,7 +90,7 @@ ssize_t const ssize_t_min = INT_MIN;
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p> <!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/ralf_w_grosse_kunstleve.htm">Ralf W. "http://www.boost.org/people/ralf_w_grosse_kunstleve.htm">Ralf W.
Grosse-Kunstleve</a> 2006.</i></p> Grosse-Kunstleve</a> 2006.</i></p>
</body> </body>
</html> </html>

View File

@@ -231,7 +231,7 @@ str remove_angle_brackets(str x)
<p>Revised 3 October, 2002</p> <p>Revised 3 October, 2002</p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -108,6 +108,23 @@
<td>A class type whose static member function <code>convert</code> <td>A class type whose static member function <code>convert</code>
does the real work of the conversion.</td> does the real work of the conversion.</td>
</tr> </tr>
<tr>
<td><code>bool has_get_pytype = false</code></td>
<td>
<code>PyTypeObject const * p = Conversion::get_pytype() </code>.</td>
<td><b>Optional member</b> - if <code>Conversion</code> has <code>get_pytype</code> member supply
<code>true</code> for this parameters.
If present <code>get_pytype</code> is used to document the return type
of functions using this conversion. The <code>get_pytype</code> may be implemented
using the classes and functions
from <a href="pytype_function.html"><code>pytype_function.hpp</code></a>
<b>NOTE :</b> For backward compatibility this parameter may be passed after
checking if <code>BOOST_PYTHON_SUPPORTS_PY_SIGNATURES</code> is defined (see
<a href="pytype_function.html#examples">here</a>).
</td>
</tr>
</table> </table>
<h4><a name="to_python_converter-spec-synopsis"></a>Class template <h4><a name="to_python_converter-spec-synopsis"></a>Class template
@@ -115,7 +132,7 @@
<pre> <pre>
namespace boost { namespace python namespace boost { namespace python
{ {
template &lt;class T, class Conversion&gt; template &lt;class T, class Conversion, bool convertion_has_get_pytype_member=false&gt;
struct to_python_converter struct to_python_converter
{ {
to_python_converter(); to_python_converter();
@@ -160,12 +177,16 @@ struct tag_to_noddy
{ {
return PyObject_New(noddy_NoddyObject, &amp;noddy_NoddyType); return PyObject_New(noddy_NoddyObject, &amp;noddy_NoddyType);
} }
static PyTypeObject const* get_pytype()
{
return &amp;noddy_NoddyType;
}
}; };
BOOST_PYTHON_MODULE(to_python_converter) BOOST_PYTHON_MODULE(to_python_converter)
{ {
def("make_tag", make_tag); def("make_tag", make_tag);
to_python_converter&lt;tag, tag_to_noddy&gt;(); to_python_converter&lt;tag, tag_to_noddy, true&gt;(); //"true" because tag_to_noddy has member get_pytype
} }
</pre> </pre>
@@ -195,12 +216,12 @@ BOOST_PYTHON_MODULE(to_python_converter)
<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 11 June, 2007
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -189,7 +189,7 @@ struct reference_existing_object
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002. </i> Distributed Abrahams</a> 2002. </i> Distributed
under the Boost Software License, Version 1.0. (See accompanying file 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> LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</p>

View File

@@ -96,7 +96,7 @@ PyObject* operator()(argument_type x) const;
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002. </i> Distributed under the Boost Software License, Abrahams</a> 2002. </i> Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)</p> http://www.boost.org/LICENSE_1_0.txt)</p>

View File

@@ -133,7 +133,7 @@ tuple head_and_tail(object sequence)
<p>Revised 03 October, 2002</p> <p>Revised 03 October, 2002</p>
<p><i>&copy; Copyright <a href= <p><i>&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body> </body>
</html> </html>

View File

@@ -215,7 +215,7 @@ bool is_int(T x)
</p> </p>
<p class="c4">&copy; Copyright <a href= <p class="c4">&copy; Copyright <a href=
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002. Distributed under the Boost Software License, "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002. Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)< http://www.boost.org/LICENSE_1_0.txt)<

View File

@@ -91,7 +91,7 @@
<hr> <hr>
<h2> <h2>
<a name="introduction">Introduction</a> <a name="introduction">Introduction</a>
</h2>This header provides faciliites for establishing a lifetime </h2>This header provides facilities for establishing a lifetime
dependency between two of a function's Python argument or result objects. dependency between two of a function's Python argument or result objects.
The <i>ward</i> object will not be destroyed until after the custodian as The <i>ward</i> object will not be destroyed until after the custodian as
long as the <i>custodian</i> object supports <a href= long as the <i>custodian</i> object supports <a href=
@@ -363,7 +363,7 @@ struct return_internal_reference
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
</p> </p>
<p> <p>
<i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2002. </i> Abrahams</a> 2002. </i>
</p> </p>
</body> </body>

View File

@@ -231,7 +231,7 @@ BOOST_PYTHON_MODULE_INIT(polymorphism)
<!--webbot bot="Timestamp" endspan i-checksum="39359" --> <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
Abrahams</a> 2004</i> Distributed under the Boost Software License, Abrahams</a> 2004</i> Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)</p> http://www.boost.org/LICENSE_1_0.txt)</p>

5
example/Jamroot Executable file → Normal file
View File

@@ -11,7 +11,10 @@ use-project boost
# boost_python library from the project whose global ID is # boost_python library from the project whose global ID is
# /boost/python. # /boost/python.
project project
: requirements <library>/boost/python//boost_python ; : requirements <library>/boost/python//boost_python
<implicit-dependency>/boost//headers
: usage-requirements <implicit-dependency>/boost//headers
;
# Declare the three extension modules. You can specify multiple # Declare the three extension modules. You can specify multiple
# source files after the colon separated by spaces. # source files after the colon separated by spaces.

Some files were not shown because too many files have changed in this diff Show More