xcoll 0.4.0__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of xcoll might be problematic. Click here for more details.

Files changed (324) hide show
  1. xcoll/__init__.py +2 -1
  2. xcoll/beam_elements/__init__.py +7 -1
  3. xcoll/beam_elements/absorber.py +2 -2
  4. xcoll/beam_elements/base.py +105 -67
  5. xcoll/beam_elements/blowup.py +198 -0
  6. xcoll/beam_elements/{collimators_src → elements_src}/black_absorber.h +21 -3
  7. xcoll/beam_elements/{collimators_src → elements_src}/black_crystal.h +20 -2
  8. xcoll/beam_elements/elements_src/blowup.h +42 -0
  9. xcoll/beam_elements/elements_src/emittance_monitor.h +109 -0
  10. xcoll/beam_elements/{collimators_src → elements_src}/everest_block.h +19 -2
  11. xcoll/beam_elements/{collimators_src → elements_src}/everest_collimator.h +19 -3
  12. xcoll/beam_elements/{collimators_src → elements_src}/everest_crystal.h +30 -9
  13. xcoll/beam_elements/everest.py +5 -6
  14. xcoll/beam_elements/monitor.py +428 -0
  15. xcoll/colldb.py +103 -74
  16. xcoll/general.py +4 -4
  17. xcoll/initial_distribution.py +18 -6
  18. xcoll/install.py +3 -1
  19. xcoll/interaction_record/interaction_record.py +126 -80
  20. xcoll/interaction_record/interaction_record_src/interaction_record.h +43 -43
  21. xcoll/line_tools.py +8 -9
  22. xcoll/lossmap.py +48 -38
  23. xcoll/scattering_routines/everest/amorphous.h +4 -11
  24. xcoll/scattering_routines/everest/channeling.h +3 -8
  25. xcoll/scattering_routines/everest/everest.h +4 -1
  26. xcoll/scattering_routines/everest/jaw.h +4 -3
  27. xcoll/scattering_routines/everest/materials.py +35 -15
  28. xcoll/scattering_routines/everest/multiple_coulomb_scattering.h +2 -2
  29. xcoll/scattering_routines/everest/nuclear_interaction.h +1 -1
  30. xcoll/scattering_routines/everest/properties.h +6 -1
  31. xcoll/scattering_routines/fluka/flukaio/lib/libFlukaIO64.a +0 -0
  32. xcoll/scattering_routines/geant4/collimasim/.git +1 -0
  33. xcoll/scattering_routines/geant4/collimasim/.gitignore +12 -0
  34. xcoll/scattering_routines/geant4/collimasim/.gitmodules +3 -0
  35. xcoll/scattering_routines/geant4/collimasim/CMakeLists.txt +26 -0
  36. xcoll/scattering_routines/geant4/collimasim/README.md +21 -0
  37. xcoll/scattering_routines/geant4/collimasim/docs/Makefile +20 -0
  38. xcoll/scattering_routines/geant4/collimasim/docs/make.bat +35 -0
  39. xcoll/scattering_routines/geant4/collimasim/docs/source/collimasim.rst +10 -0
  40. xcoll/scattering_routines/geant4/collimasim/docs/source/conf.py +59 -0
  41. xcoll/scattering_routines/geant4/collimasim/docs/source/index.rst +26 -0
  42. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.appveyor.yml +37 -0
  43. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.clang-format +19 -0
  44. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.clang-tidy +65 -0
  45. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.cmake-format.yaml +73 -0
  46. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.git +1 -0
  47. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/CODEOWNERS +9 -0
  48. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/CONTRIBUTING.md +386 -0
  49. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/ISSUE_TEMPLATE/bug-report.yml +45 -0
  50. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/ISSUE_TEMPLATE/config.yml +8 -0
  51. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/dependabot.yml +16 -0
  52. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/labeler.yml +8 -0
  53. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/labeler_merged.yml +3 -0
  54. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/pull_request_template.md +19 -0
  55. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/ci.yml +969 -0
  56. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/configure.yml +84 -0
  57. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/format.yml +48 -0
  58. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/labeler.yml +16 -0
  59. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/pip.yml +103 -0
  60. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.gitignore +45 -0
  61. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.pre-commit-config.yaml +151 -0
  62. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.readthedocs.yml +3 -0
  63. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/CMakeLists.txt +297 -0
  64. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/LICENSE +29 -0
  65. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/MANIFEST.in +6 -0
  66. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/README.rst +180 -0
  67. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/Doxyfile +23 -0
  68. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/Makefile +192 -0
  69. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/_static/theme_overrides.css +11 -0
  70. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/chrono.rst +81 -0
  71. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/custom.rst +93 -0
  72. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/eigen.rst +310 -0
  73. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/functional.rst +109 -0
  74. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/index.rst +43 -0
  75. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/overview.rst +171 -0
  76. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/stl.rst +251 -0
  77. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/strings.rst +305 -0
  78. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/classes.rst +1297 -0
  79. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/embedding.rst +262 -0
  80. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/exceptions.rst +396 -0
  81. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/functions.rst +568 -0
  82. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/misc.rst +337 -0
  83. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/index.rst +13 -0
  84. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/numpy.rst +463 -0
  85. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/object.rst +286 -0
  86. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/utilities.rst +155 -0
  87. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/smart_ptrs.rst +174 -0
  88. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/basics.rst +308 -0
  89. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/benchmark.py +91 -0
  90. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/benchmark.rst +95 -0
  91. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/changelog.rst +2050 -0
  92. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/classes.rst +542 -0
  93. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/cmake/index.rst +8 -0
  94. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/compiling.rst +648 -0
  95. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/conf.py +381 -0
  96. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/faq.rst +343 -0
  97. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/index.rst +48 -0
  98. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/installing.rst +105 -0
  99. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/limitations.rst +72 -0
  100. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11-logo.png +0 -0
  101. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python1.png +0 -0
  102. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python1.svg +427 -0
  103. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python2.png +0 -0
  104. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python2.svg +427 -0
  105. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/reference.rst +130 -0
  106. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/release.rst +96 -0
  107. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/requirements.txt +8 -0
  108. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/upgrade.rst +548 -0
  109. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/attr.h +605 -0
  110. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/buffer_info.h +144 -0
  111. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/cast.h +1432 -0
  112. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/chrono.h +213 -0
  113. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/common.h +2 -0
  114. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/complex.h +65 -0
  115. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/class.h +709 -0
  116. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/common.h +1021 -0
  117. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/descr.h +104 -0
  118. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/init.h +346 -0
  119. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/internals.h +467 -0
  120. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/type_caster_base.h +978 -0
  121. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/typeid.h +55 -0
  122. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/eigen.h +606 -0
  123. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/embed.h +284 -0
  124. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/eval.h +163 -0
  125. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/functional.h +121 -0
  126. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/gil.h +193 -0
  127. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/iostream.h +275 -0
  128. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/numpy.h +1741 -0
  129. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/operators.h +163 -0
  130. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/options.h +65 -0
  131. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/pybind11.h +2497 -0
  132. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/pytypes.h +1879 -0
  133. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl/filesystem.h +103 -0
  134. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl.h +375 -0
  135. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl_bind.h +747 -0
  136. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/noxfile.py +88 -0
  137. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/__init__.py +11 -0
  138. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/__main__.py +52 -0
  139. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/_version.py +12 -0
  140. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/_version.pyi +6 -0
  141. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/commands.py +21 -0
  142. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/py.typed +0 -0
  143. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/setup_helpers.py +482 -0
  144. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/setup_helpers.pyi +63 -0
  145. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pyproject.toml +41 -0
  146. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/setup.cfg +56 -0
  147. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/setup.py +155 -0
  148. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/CMakeLists.txt +503 -0
  149. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/conftest.py +208 -0
  150. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/constructor_stats.h +275 -0
  151. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/cross_module_gil_utils.cpp +73 -0
  152. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/env.py +33 -0
  153. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_python_package/pytest.ini +0 -0
  154. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_python_package/test_files.py +279 -0
  155. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_setuptools/pytest.ini +0 -0
  156. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_setuptools/test_setuphelper.py +143 -0
  157. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/local_bindings.h +85 -0
  158. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/object.h +179 -0
  159. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_cross_module_tests.cpp +151 -0
  160. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_tests.cpp +91 -0
  161. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_tests.h +85 -0
  162. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pytest.ini +19 -0
  163. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/requirements.txt +12 -0
  164. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_async.cpp +26 -0
  165. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_async.py +25 -0
  166. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_buffers.cpp +216 -0
  167. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_buffers.py +163 -0
  168. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_builtin_casters.cpp +286 -0
  169. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_builtin_casters.py +536 -0
  170. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_call_policies.cpp +107 -0
  171. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_call_policies.py +248 -0
  172. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_callbacks.cpp +227 -0
  173. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_callbacks.py +202 -0
  174. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_chrono.cpp +84 -0
  175. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_chrono.py +210 -0
  176. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_class.cpp +550 -0
  177. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_class.py +473 -0
  178. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/CMakeLists.txt +84 -0
  179. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/embed.cpp +21 -0
  180. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt +28 -0
  181. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_function/CMakeLists.txt +39 -0
  182. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_target/CMakeLists.txt +46 -0
  183. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/main.cpp +6 -0
  184. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt +41 -0
  185. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_function/CMakeLists.txt +35 -0
  186. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_target/CMakeLists.txt +41 -0
  187. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/test.py +10 -0
  188. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_constants_and_functions.cpp +165 -0
  189. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_constants_and_functions.py +53 -0
  190. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_copy_move.cpp +238 -0
  191. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_copy_move.py +126 -0
  192. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_casters.cpp +141 -0
  193. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_casters.py +117 -0
  194. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_setup.cpp +41 -0
  195. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_setup.py +50 -0
  196. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_docstring_options.cpp +69 -0
  197. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_docstring_options.py +42 -0
  198. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eigen.cpp +348 -0
  199. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eigen.py +771 -0
  200. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/CMakeLists.txt +47 -0
  201. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/catch.cpp +22 -0
  202. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/external_module.cpp +23 -0
  203. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/test_interpreter.cpp +326 -0
  204. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/test_interpreter.py +15 -0
  205. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_enum.cpp +148 -0
  206. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_enum.py +272 -0
  207. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval.cpp +119 -0
  208. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval.py +51 -0
  209. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval_call.py +5 -0
  210. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.cpp +285 -0
  211. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.h +12 -0
  212. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.py +265 -0
  213. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_factory_constructors.cpp +397 -0
  214. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_factory_constructors.py +520 -0
  215. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_gil_scoped.cpp +49 -0
  216. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_gil_scoped.py +94 -0
  217. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_iostream.cpp +125 -0
  218. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_iostream.py +331 -0
  219. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_kwargs_and_defaults.cpp +153 -0
  220. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_kwargs_and_defaults.py +284 -0
  221. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_local_bindings.cpp +107 -0
  222. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_local_bindings.py +257 -0
  223. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_methods_and_attributes.cpp +412 -0
  224. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_methods_and_attributes.py +517 -0
  225. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_modules.cpp +102 -0
  226. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_modules.py +92 -0
  227. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_multiple_inheritance.cpp +233 -0
  228. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_multiple_inheritance.py +360 -0
  229. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_array.cpp +472 -0
  230. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_array.py +593 -0
  231. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_dtypes.cpp +524 -0
  232. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_dtypes.py +441 -0
  233. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_vectorize.cpp +103 -0
  234. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_vectorize.py +267 -0
  235. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_opaque_types.cpp +73 -0
  236. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_opaque_types.py +59 -0
  237. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_operator_overloading.cpp +235 -0
  238. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_operator_overloading.py +146 -0
  239. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pickling.cpp +189 -0
  240. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pickling.py +82 -0
  241. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pytypes.cpp +560 -0
  242. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pytypes.py +651 -0
  243. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_sequences_and_iterators.cpp +500 -0
  244. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_sequences_and_iterators.py +253 -0
  245. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_smart_ptr.cpp +452 -0
  246. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_smart_ptr.py +318 -0
  247. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl.cpp +342 -0
  248. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl.py +291 -0
  249. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl_binders.cpp +131 -0
  250. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl_binders.py +318 -0
  251. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_tagbased_polymorphic.cpp +144 -0
  252. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_tagbased_polymorphic.py +29 -0
  253. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_thread.cpp +66 -0
  254. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_thread.py +44 -0
  255. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_union.cpp +22 -0
  256. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_union.py +9 -0
  257. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_virtual_functions.cpp +510 -0
  258. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_virtual_functions.py +408 -0
  259. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/valgrind-numpy-scipy.supp +140 -0
  260. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/valgrind-python.supp +117 -0
  261. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindCatch.cmake +70 -0
  262. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindEigen3.cmake +86 -0
  263. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindPythonLibsNew.cmake +257 -0
  264. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/check-style.sh +44 -0
  265. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/cmake_uninstall.cmake.in +23 -0
  266. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/libsize.py +39 -0
  267. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/make_changelog.py +64 -0
  268. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Common.cmake +402 -0
  269. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Config.cmake.in +233 -0
  270. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11NewTools.cmake +276 -0
  271. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Tools.cmake +214 -0
  272. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pyproject.toml +3 -0
  273. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/setup_global.py.in +65 -0
  274. xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/setup_main.py.in +41 -0
  275. xcoll/scattering_routines/geant4/collimasim/pyproject.toml +8 -0
  276. xcoll/scattering_routines/geant4/collimasim/setup.py +144 -0
  277. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSPyATInterface.cpp +403 -0
  278. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSPyATInterface.hh +100 -0
  279. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSXtrackInterface.cpp +763 -0
  280. xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSXtrackInterface.hh +118 -0
  281. xcoll/scattering_routines/geant4/collimasim/src/collimasim/__init__.py +8 -0
  282. xcoll/scattering_routines/geant4/collimasim/src/collimasim/bindings.cpp +63 -0
  283. xcoll/scattering_routines/geant4/collimasim/src/collimasim/pyCollimatorPass.py +142 -0
  284. xcoll/scattering_routines/geant4/collimasim/src/collimasim/xtrack_collimator.py +556 -0
  285. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/PKG-INFO +6 -0
  286. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/SOURCES.txt +24 -0
  287. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/dependency_links.txt +1 -0
  288. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/not-zip-safe +1 -0
  289. xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/top_level.txt +1 -0
  290. xcoll/scattering_routines/geant4/collimasim/tests/README.md +25 -0
  291. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_forions.dat +25 -0
  292. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_new_example.dat +18 -0
  293. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_old_example.dat +68 -0
  294. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_testing.dat +15 -0
  295. xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_yaml_example.yaml +110 -0
  296. xcoll/scattering_routines/geant4/collimasim/tests/resources/collgaps.dat +7 -0
  297. xcoll/scattering_routines/geant4/collimasim/tests/resources/collgaps_pyat_test.dat +3 -0
  298. xcoll/scattering_routines/geant4/collimasim/tests/resources/collonly_twiss_file_example.tfs +54 -0
  299. xcoll/scattering_routines/geant4/collimasim/tests/resources/settings.gmad +3 -0
  300. xcoll/scattering_routines/geant4/collimasim/tests/resources/settings_black_absorber.gmad +3 -0
  301. xcoll/scattering_routines/geant4/collimasim/tests/resources/settings_ions.gmad +5 -0
  302. xcoll/scattering_routines/geant4/collimasim/tests/resources/twiss_file_testing.tfs +51 -0
  303. xcoll/scattering_routines/geant4/collimasim/tests/test_pyat.py +65 -0
  304. xcoll/scattering_routines/geant4/collimasim/tests/test_pyat_passmethod.py +59 -0
  305. xcoll/scattering_routines/geant4/collimasim/tests/test_pyat_tracking.py +102 -0
  306. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack.py +75 -0
  307. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_angle.py +74 -0
  308. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_colldb_load.py +84 -0
  309. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_interaction.py +159 -0
  310. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_interaction_ion.py +99 -0
  311. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_ions.py +78 -0
  312. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_lost_energy.py +88 -0
  313. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tilt.py +80 -0
  314. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tracking.py +97 -0
  315. xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tracking_ions.py +96 -0
  316. xcoll/scattering_routines/geometry/collimator_geometry.h +9 -10
  317. xcoll/scattering_routines/geometry/crystal_geometry.h +9 -6
  318. {xcoll-0.4.0.dist-info → xcoll-0.5.0.dist-info}/METADATA +1 -1
  319. xcoll-0.5.0.dist-info/RECORD +413 -0
  320. xcoll/scattering_routines/fluka/build_fluka_input.py +0 -58
  321. xcoll-0.4.0.dist-info/RECORD +0 -126
  322. {xcoll-0.4.0.dist-info → xcoll-0.5.0.dist-info}/LICENSE +0 -0
  323. {xcoll-0.4.0.dist-info → xcoll-0.5.0.dist-info}/NOTICE +0 -0
  324. {xcoll-0.4.0.dist-info → xcoll-0.5.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1,286 @@
1
+ Python types
2
+ ############
3
+
4
+ .. _wrappers:
5
+
6
+ Available wrappers
7
+ ==================
8
+
9
+ All major Python types are available as thin C++ wrapper classes. These
10
+ can also be used as function parameters -- see :ref:`python_objects_as_args`.
11
+
12
+ Available types include :class:`handle`, :class:`object`, :class:`bool_`,
13
+ :class:`int_`, :class:`float_`, :class:`str`, :class:`bytes`, :class:`tuple`,
14
+ :class:`list`, :class:`dict`, :class:`slice`, :class:`none`, :class:`capsule`,
15
+ :class:`iterable`, :class:`iterator`, :class:`function`, :class:`buffer`,
16
+ :class:`array`, and :class:`array_t`.
17
+
18
+ .. warning::
19
+
20
+ Be sure to review the :ref:`pytypes_gotchas` before using this heavily in
21
+ your C++ API.
22
+
23
+ .. _instantiating_compound_types:
24
+
25
+ Instantiating compound Python types from C++
26
+ ============================================
27
+
28
+ Dictionaries can be initialized in the :class:`dict` constructor:
29
+
30
+ .. code-block:: cpp
31
+
32
+ using namespace pybind11::literals; // to bring in the `_a` literal
33
+ py::dict d("spam"_a=py::none(), "eggs"_a=42);
34
+
35
+ A tuple of python objects can be instantiated using :func:`py::make_tuple`:
36
+
37
+ .. code-block:: cpp
38
+
39
+ py::tuple tup = py::make_tuple(42, py::none(), "spam");
40
+
41
+ Each element is converted to a supported Python type.
42
+
43
+ A `simple namespace`_ can be instantiated using
44
+
45
+ .. code-block:: cpp
46
+
47
+ using namespace pybind11::literals; // to bring in the `_a` literal
48
+ py::object SimpleNamespace = py::module_::import("types").attr("SimpleNamespace");
49
+ py::object ns = SimpleNamespace("spam"_a=py::none(), "eggs"_a=42);
50
+
51
+ Attributes on a namespace can be modified with the :func:`py::delattr`,
52
+ :func:`py::getattr`, and :func:`py::setattr` functions. Simple namespaces can
53
+ be useful as lightweight stand-ins for class instances.
54
+
55
+ .. _simple namespace: https://docs.python.org/3/library/types.html#types.SimpleNamespace
56
+
57
+ .. _casting_back_and_forth:
58
+
59
+ Casting back and forth
60
+ ======================
61
+
62
+ In this kind of mixed code, it is often necessary to convert arbitrary C++
63
+ types to Python, which can be done using :func:`py::cast`:
64
+
65
+ .. code-block:: cpp
66
+
67
+ MyClass *cls = ...;
68
+ py::object obj = py::cast(cls);
69
+
70
+ The reverse direction uses the following syntax:
71
+
72
+ .. code-block:: cpp
73
+
74
+ py::object obj = ...;
75
+ MyClass *cls = obj.cast<MyClass *>();
76
+
77
+ When conversion fails, both directions throw the exception :class:`cast_error`.
78
+
79
+ .. _python_libs:
80
+
81
+ Accessing Python libraries from C++
82
+ ===================================
83
+
84
+ It is also possible to import objects defined in the Python standard
85
+ library or available in the current Python environment (``sys.path``) and work
86
+ with these in C++.
87
+
88
+ This example obtains a reference to the Python ``Decimal`` class.
89
+
90
+ .. code-block:: cpp
91
+
92
+ // Equivalent to "from decimal import Decimal"
93
+ py::object Decimal = py::module_::import("decimal").attr("Decimal");
94
+
95
+ .. code-block:: cpp
96
+
97
+ // Try to import scipy
98
+ py::object scipy = py::module_::import("scipy");
99
+ return scipy.attr("__version__");
100
+
101
+
102
+ .. _calling_python_functions:
103
+
104
+ Calling Python functions
105
+ ========================
106
+
107
+ It is also possible to call Python classes, functions and methods
108
+ via ``operator()``.
109
+
110
+ .. code-block:: cpp
111
+
112
+ // Construct a Python object of class Decimal
113
+ py::object pi = Decimal("3.14159");
114
+
115
+ .. code-block:: cpp
116
+
117
+ // Use Python to make our directories
118
+ py::object os = py::module_::import("os");
119
+ py::object makedirs = os.attr("makedirs");
120
+ makedirs("/tmp/path/to/somewhere");
121
+
122
+ One can convert the result obtained from Python to a pure C++ version
123
+ if a ``py::class_`` or type conversion is defined.
124
+
125
+ .. code-block:: cpp
126
+
127
+ py::function f = <...>;
128
+ py::object result_py = f(1234, "hello", some_instance);
129
+ MyClass &result = result_py.cast<MyClass>();
130
+
131
+ .. _calling_python_methods:
132
+
133
+ Calling Python methods
134
+ ========================
135
+
136
+ To call an object's method, one can again use ``.attr`` to obtain access to the
137
+ Python method.
138
+
139
+ .. code-block:: cpp
140
+
141
+ // Calculate e^π in decimal
142
+ py::object exp_pi = pi.attr("exp")();
143
+ py::print(py::str(exp_pi));
144
+
145
+ In the example above ``pi.attr("exp")`` is a *bound method*: it will always call
146
+ the method for that same instance of the class. Alternately one can create an
147
+ *unbound method* via the Python class (instead of instance) and pass the ``self``
148
+ object explicitly, followed by other arguments.
149
+
150
+ .. code-block:: cpp
151
+
152
+ py::object decimal_exp = Decimal.attr("exp");
153
+
154
+ // Compute the e^n for n=0..4
155
+ for (int n = 0; n < 5; n++) {
156
+ py::print(decimal_exp(Decimal(n));
157
+ }
158
+
159
+ Keyword arguments
160
+ =================
161
+
162
+ Keyword arguments are also supported. In Python, there is the usual call syntax:
163
+
164
+ .. code-block:: python
165
+
166
+ def f(number, say, to):
167
+ ... # function code
168
+
169
+
170
+ f(1234, say="hello", to=some_instance) # keyword call in Python
171
+
172
+ In C++, the same call can be made using:
173
+
174
+ .. code-block:: cpp
175
+
176
+ using namespace pybind11::literals; // to bring in the `_a` literal
177
+ f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++
178
+
179
+ Unpacking arguments
180
+ ===================
181
+
182
+ Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with
183
+ other arguments:
184
+
185
+ .. code-block:: cpp
186
+
187
+ // * unpacking
188
+ py::tuple args = py::make_tuple(1234, "hello", some_instance);
189
+ f(*args);
190
+
191
+ // ** unpacking
192
+ py::dict kwargs = py::dict("number"_a=1234, "say"_a="hello", "to"_a=some_instance);
193
+ f(**kwargs);
194
+
195
+ // mixed keywords, * and ** unpacking
196
+ py::tuple args = py::make_tuple(1234);
197
+ py::dict kwargs = py::dict("to"_a=some_instance);
198
+ f(*args, "say"_a="hello", **kwargs);
199
+
200
+ Generalized unpacking according to PEP448_ is also supported:
201
+
202
+ .. code-block:: cpp
203
+
204
+ py::dict kwargs1 = py::dict("number"_a=1234);
205
+ py::dict kwargs2 = py::dict("to"_a=some_instance);
206
+ f(**kwargs1, "say"_a="hello", **kwargs2);
207
+
208
+ .. seealso::
209
+
210
+ The file :file:`tests/test_pytypes.cpp` contains a complete
211
+ example that demonstrates passing native Python types in more detail. The
212
+ file :file:`tests/test_callbacks.cpp` presents a few examples of calling
213
+ Python functions from C++, including keywords arguments and unpacking.
214
+
215
+ .. _PEP448: https://www.python.org/dev/peps/pep-0448/
216
+
217
+ .. _implicit_casting:
218
+
219
+ Implicit casting
220
+ ================
221
+
222
+ When using the C++ interface for Python types, or calling Python functions,
223
+ objects of type :class:`object` are returned. It is possible to invoke implicit
224
+ conversions to subclasses like :class:`dict`. The same holds for the proxy objects
225
+ returned by ``operator[]`` or ``obj.attr()``.
226
+ Casting to subtypes improves code readability and allows values to be passed to
227
+ C++ functions that require a specific subtype rather than a generic :class:`object`.
228
+
229
+ .. code-block:: cpp
230
+
231
+ #include <pybind11/numpy.h>
232
+ using namespace pybind11::literals;
233
+
234
+ py::module_ os = py::module_::import("os");
235
+ py::module_ path = py::module_::import("os.path"); // like 'import os.path as path'
236
+ py::module_ np = py::module_::import("numpy"); // like 'import numpy as np'
237
+
238
+ py::str curdir_abs = path.attr("abspath")(path.attr("curdir"));
239
+ py::print(py::str("Current directory: ") + curdir_abs);
240
+ py::dict environ = os.attr("environ");
241
+ py::print(environ["HOME"]);
242
+ py::array_t<float> arr = np.attr("ones")(3, "dtype"_a="float32");
243
+ py::print(py::repr(arr + py::int_(1)));
244
+
245
+ These implicit conversions are available for subclasses of :class:`object`; there
246
+ is no need to call ``obj.cast()`` explicitly as for custom classes, see
247
+ :ref:`casting_back_and_forth`.
248
+
249
+ .. note::
250
+ If a trivial conversion via move constructor is not possible, both implicit and
251
+ explicit casting (calling ``obj.cast()``) will attempt a "rich" conversion.
252
+ For instance, ``py::list env = os.attr("environ");`` will succeed and is
253
+ equivalent to the Python code ``env = list(os.environ)`` that produces a
254
+ list of the dict keys.
255
+
256
+ .. TODO: Adapt text once PR #2349 has landed
257
+
258
+ Handling exceptions
259
+ ===================
260
+
261
+ Python exceptions from wrapper classes will be thrown as a ``py::error_already_set``.
262
+ See :ref:`Handling exceptions from Python in C++
263
+ <handling_python_exceptions_cpp>` for more information on handling exceptions
264
+ raised when calling C++ wrapper classes.
265
+
266
+ .. _pytypes_gotchas:
267
+
268
+ Gotchas
269
+ =======
270
+
271
+ Default-Constructed Wrappers
272
+ ----------------------------
273
+
274
+ When a wrapper type is default-constructed, it is **not** a valid Python object (i.e. it is not ``py::none()``). It is simply the same as
275
+ ``PyObject*`` null pointer. To check for this, use
276
+ ``static_cast<bool>(my_wrapper)``.
277
+
278
+ Assigning py::none() to wrappers
279
+ --------------------------------
280
+
281
+ You may be tempted to use types like ``py::str`` and ``py::dict`` in C++
282
+ signatures (either pure C++, or in bound signatures), and assign them default
283
+ values of ``py::none()``. However, in a best case scenario, it will fail fast
284
+ because ``None`` is not convertible to that type (e.g. ``py::dict``), or in a
285
+ worse case scenario, it will silently work but corrupt the types you want to
286
+ work with (e.g. ``py::str(py::none())`` will yield ``"None"`` in Python).
@@ -0,0 +1,155 @@
1
+ Utilities
2
+ #########
3
+
4
+ Using Python's print function in C++
5
+ ====================================
6
+
7
+ The usual way to write output in C++ is using ``std::cout`` while in Python one
8
+ would use ``print``. Since these methods use different buffers, mixing them can
9
+ lead to output order issues. To resolve this, pybind11 modules can use the
10
+ :func:`py::print` function which writes to Python's ``sys.stdout`` for consistency.
11
+
12
+ Python's ``print`` function is replicated in the C++ API including optional
13
+ keyword arguments ``sep``, ``end``, ``file``, ``flush``. Everything works as
14
+ expected in Python:
15
+
16
+ .. code-block:: cpp
17
+
18
+ py::print(1, 2.0, "three"); // 1 2.0 three
19
+ py::print(1, 2.0, "three", "sep"_a="-"); // 1-2.0-three
20
+
21
+ auto args = py::make_tuple("unpacked", true);
22
+ py::print("->", *args, "end"_a="<-"); // -> unpacked True <-
23
+
24
+ .. _ostream_redirect:
25
+
26
+ Capturing standard output from ostream
27
+ ======================================
28
+
29
+ Often, a library will use the streams ``std::cout`` and ``std::cerr`` to print,
30
+ but this does not play well with Python's standard ``sys.stdout`` and ``sys.stderr``
31
+ redirection. Replacing a library's printing with ``py::print <print>`` may not
32
+ be feasible. This can be fixed using a guard around the library function that
33
+ redirects output to the corresponding Python streams:
34
+
35
+ .. code-block:: cpp
36
+
37
+ #include <pybind11/iostream.h>
38
+
39
+ ...
40
+
41
+ // Add a scoped redirect for your noisy code
42
+ m.def("noisy_func", []() {
43
+ py::scoped_ostream_redirect stream(
44
+ std::cout, // std::ostream&
45
+ py::module_::import("sys").attr("stdout") // Python output
46
+ );
47
+ call_noisy_func();
48
+ });
49
+
50
+ .. warning::
51
+
52
+ The implementation in ``pybind11/iostream.h`` is NOT thread safe. Multiple
53
+ threads writing to a redirected ostream concurrently cause data races
54
+ and potentially buffer overflows. Therefore it is currently a requirement
55
+ that all (possibly) concurrent redirected ostream writes are protected by
56
+ a mutex. #HelpAppreciated: Work on iostream.h thread safety. For more
57
+ background see the discussions under
58
+ `PR #2982 <https://github.com/pybind/pybind11/pull/2982>`_ and
59
+ `PR #2995 <https://github.com/pybind/pybind11/pull/2995>`_.
60
+
61
+ This method respects flushes on the output streams and will flush if needed
62
+ when the scoped guard is destroyed. This allows the output to be redirected in
63
+ real time, such as to a Jupyter notebook. The two arguments, the C++ stream and
64
+ the Python output, are optional, and default to standard output if not given. An
65
+ extra type, ``py::scoped_estream_redirect <scoped_estream_redirect>``, is identical
66
+ except for defaulting to ``std::cerr`` and ``sys.stderr``; this can be useful with
67
+ ``py::call_guard``, which allows multiple items, but uses the default constructor:
68
+
69
+ .. code-block:: cpp
70
+
71
+ // Alternative: Call single function using call guard
72
+ m.def("noisy_func", &call_noisy_function,
73
+ py::call_guard<py::scoped_ostream_redirect,
74
+ py::scoped_estream_redirect>());
75
+
76
+ The redirection can also be done in Python with the addition of a context
77
+ manager, using the ``py::add_ostream_redirect() <add_ostream_redirect>`` function:
78
+
79
+ .. code-block:: cpp
80
+
81
+ py::add_ostream_redirect(m, "ostream_redirect");
82
+
83
+ The name in Python defaults to ``ostream_redirect`` if no name is passed. This
84
+ creates the following context manager in Python:
85
+
86
+ .. code-block:: python
87
+
88
+ with ostream_redirect(stdout=True, stderr=True):
89
+ noisy_function()
90
+
91
+ It defaults to redirecting both streams, though you can use the keyword
92
+ arguments to disable one of the streams if needed.
93
+
94
+ .. note::
95
+
96
+ The above methods will not redirect C-level output to file descriptors, such
97
+ as ``fprintf``. For those cases, you'll need to redirect the file
98
+ descriptors either directly in C or with Python's ``os.dup2`` function
99
+ in an operating-system dependent way.
100
+
101
+ .. _eval:
102
+
103
+ Evaluating Python expressions from strings and files
104
+ ====================================================
105
+
106
+ pybind11 provides the ``eval``, ``exec`` and ``eval_file`` functions to evaluate
107
+ Python expressions and statements. The following example illustrates how they
108
+ can be used.
109
+
110
+ .. code-block:: cpp
111
+
112
+ // At beginning of file
113
+ #include <pybind11/eval.h>
114
+
115
+ ...
116
+
117
+ // Evaluate in scope of main module
118
+ py::object scope = py::module_::import("__main__").attr("__dict__");
119
+
120
+ // Evaluate an isolated expression
121
+ int result = py::eval("my_variable + 10", scope).cast<int>();
122
+
123
+ // Evaluate a sequence of statements
124
+ py::exec(
125
+ "print('Hello')\n"
126
+ "print('world!');",
127
+ scope);
128
+
129
+ // Evaluate the statements in an separate Python file on disk
130
+ py::eval_file("script.py", scope);
131
+
132
+ C++11 raw string literals are also supported and quite handy for this purpose.
133
+ The only requirement is that the first statement must be on a new line following
134
+ the raw string delimiter ``R"(``, ensuring all lines have common leading indent:
135
+
136
+ .. code-block:: cpp
137
+
138
+ py::exec(R"(
139
+ x = get_answer()
140
+ if x == 42:
141
+ print('Hello World!')
142
+ else:
143
+ print('Bye!')
144
+ )", scope
145
+ );
146
+
147
+ .. note::
148
+
149
+ `eval` and `eval_file` accept a template parameter that describes how the
150
+ string/file should be interpreted. Possible choices include ``eval_expr``
151
+ (isolated expression), ``eval_single_statement`` (a single statement, return
152
+ value is always ``none``), and ``eval_statements`` (sequence of statements,
153
+ return value is always ``none``). `eval` defaults to ``eval_expr``,
154
+ `eval_file` defaults to ``eval_statements`` and `exec` is just a shortcut
155
+ for ``eval<eval_statements>``.
@@ -0,0 +1,174 @@
1
+ Smart pointers
2
+ ##############
3
+
4
+ std::unique_ptr
5
+ ===============
6
+
7
+ Given a class ``Example`` with Python bindings, it's possible to return
8
+ instances wrapped in C++11 unique pointers, like so
9
+
10
+ .. code-block:: cpp
11
+
12
+ std::unique_ptr<Example> create_example() { return std::unique_ptr<Example>(new Example()); }
13
+
14
+ .. code-block:: cpp
15
+
16
+ m.def("create_example", &create_example);
17
+
18
+ In other words, there is nothing special that needs to be done. While returning
19
+ unique pointers in this way is allowed, it is *illegal* to use them as function
20
+ arguments. For instance, the following function signature cannot be processed
21
+ by pybind11.
22
+
23
+ .. code-block:: cpp
24
+
25
+ void do_something_with_example(std::unique_ptr<Example> ex) { ... }
26
+
27
+ The above signature would imply that Python needs to give up ownership of an
28
+ object that is passed to this function, which is generally not possible (for
29
+ instance, the object might be referenced elsewhere).
30
+
31
+ std::shared_ptr
32
+ ===============
33
+
34
+ The binding generator for classes, :class:`class_`, can be passed a template
35
+ type that denotes a special *holder* type that is used to manage references to
36
+ the object. If no such holder type template argument is given, the default for
37
+ a type named ``Type`` is ``std::unique_ptr<Type>``, which means that the object
38
+ is deallocated when Python's reference count goes to zero.
39
+
40
+ It is possible to switch to other types of reference counting wrappers or smart
41
+ pointers, which is useful in codebases that rely on them. For instance, the
42
+ following snippet causes ``std::shared_ptr`` to be used instead.
43
+
44
+ .. code-block:: cpp
45
+
46
+ py::class_<Example, std::shared_ptr<Example> /* <- holder type */> obj(m, "Example");
47
+
48
+ Note that any particular class can only be associated with a single holder type.
49
+
50
+ One potential stumbling block when using holder types is that they need to be
51
+ applied consistently. Can you guess what's broken about the following binding
52
+ code?
53
+
54
+ .. code-block:: cpp
55
+
56
+ class Child { };
57
+
58
+ class Parent {
59
+ public:
60
+ Parent() : child(std::make_shared<Child>()) { }
61
+ Child *get_child() { return child.get(); } /* Hint: ** DON'T DO THIS ** */
62
+ private:
63
+ std::shared_ptr<Child> child;
64
+ };
65
+
66
+ PYBIND11_MODULE(example, m) {
67
+ py::class_<Child, std::shared_ptr<Child>>(m, "Child");
68
+
69
+ py::class_<Parent, std::shared_ptr<Parent>>(m, "Parent")
70
+ .def(py::init<>())
71
+ .def("get_child", &Parent::get_child);
72
+ }
73
+
74
+ The following Python code will cause undefined behavior (and likely a
75
+ segmentation fault).
76
+
77
+ .. code-block:: python
78
+
79
+ from example import Parent
80
+
81
+ print(Parent().get_child())
82
+
83
+ The problem is that ``Parent::get_child()`` returns a pointer to an instance of
84
+ ``Child``, but the fact that this instance is already managed by
85
+ ``std::shared_ptr<...>`` is lost when passing raw pointers. In this case,
86
+ pybind11 will create a second independent ``std::shared_ptr<...>`` that also
87
+ claims ownership of the pointer. In the end, the object will be freed **twice**
88
+ since these shared pointers have no way of knowing about each other.
89
+
90
+ There are two ways to resolve this issue:
91
+
92
+ 1. For types that are managed by a smart pointer class, never use raw pointers
93
+ in function arguments or return values. In other words: always consistently
94
+ wrap pointers into their designated holder types (such as
95
+ ``std::shared_ptr<...>``). In this case, the signature of ``get_child()``
96
+ should be modified as follows:
97
+
98
+ .. code-block:: cpp
99
+
100
+ std::shared_ptr<Child> get_child() { return child; }
101
+
102
+ 2. Adjust the definition of ``Child`` by specifying
103
+ ``std::enable_shared_from_this<T>`` (see cppreference_ for details) as a
104
+ base class. This adds a small bit of information to ``Child`` that allows
105
+ pybind11 to realize that there is already an existing
106
+ ``std::shared_ptr<...>`` and communicate with it. In this case, the
107
+ declaration of ``Child`` should look as follows:
108
+
109
+ .. _cppreference: http://en.cppreference.com/w/cpp/memory/enable_shared_from_this
110
+
111
+ .. code-block:: cpp
112
+
113
+ class Child : public std::enable_shared_from_this<Child> { };
114
+
115
+ .. _smart_pointers:
116
+
117
+ Custom smart pointers
118
+ =====================
119
+
120
+ pybind11 supports ``std::unique_ptr`` and ``std::shared_ptr`` right out of the
121
+ box. For any other custom smart pointer, transparent conversions can be enabled
122
+ using a macro invocation similar to the following. It must be declared at the
123
+ top namespace level before any binding code:
124
+
125
+ .. code-block:: cpp
126
+
127
+ PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
128
+
129
+ The first argument of :func:`PYBIND11_DECLARE_HOLDER_TYPE` should be a
130
+ placeholder name that is used as a template parameter of the second argument.
131
+ Thus, feel free to use any identifier, but use it consistently on both sides;
132
+ also, don't use the name of a type that already exists in your codebase.
133
+
134
+ The macro also accepts a third optional boolean parameter that is set to false
135
+ by default. Specify
136
+
137
+ .. code-block:: cpp
138
+
139
+ PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>, true);
140
+
141
+ if ``SmartPtr<T>`` can always be initialized from a ``T*`` pointer without the
142
+ risk of inconsistencies (such as multiple independent ``SmartPtr`` instances
143
+ believing that they are the sole owner of the ``T*`` pointer). A common
144
+ situation where ``true`` should be passed is when the ``T`` instances use
145
+ *intrusive* reference counting.
146
+
147
+ Please take a look at the :ref:`macro_notes` before using this feature.
148
+
149
+ By default, pybind11 assumes that your custom smart pointer has a standard
150
+ interface, i.e. provides a ``.get()`` member function to access the underlying
151
+ raw pointer. If this is not the case, pybind11's ``holder_helper`` must be
152
+ specialized:
153
+
154
+ .. code-block:: cpp
155
+
156
+ // Always needed for custom holder types
157
+ PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
158
+
159
+ // Only needed if the type's `.get()` goes by another name
160
+ namespace pybind11 { namespace detail {
161
+ template <typename T>
162
+ struct holder_helper<SmartPtr<T>> { // <-- specialization
163
+ static const T *get(const SmartPtr<T> &p) { return p.getPointer(); }
164
+ };
165
+ }}
166
+
167
+ The above specialization informs pybind11 that the custom ``SmartPtr`` class
168
+ provides ``.get()`` functionality via ``.getPointer()``.
169
+
170
+ .. seealso::
171
+
172
+ The file :file:`tests/test_smart_ptr.cpp` contains a complete example
173
+ that demonstrates how to work with custom reference-counting holder types
174
+ in more detail.