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,262 @@
1
+ .. _embedding:
2
+
3
+ Embedding the interpreter
4
+ #########################
5
+
6
+ While pybind11 is mainly focused on extending Python using C++, it's also
7
+ possible to do the reverse: embed the Python interpreter into a C++ program.
8
+ All of the other documentation pages still apply here, so refer to them for
9
+ general pybind11 usage. This section will cover a few extra things required
10
+ for embedding.
11
+
12
+ Getting started
13
+ ===============
14
+
15
+ A basic executable with an embedded interpreter can be created with just a few
16
+ lines of CMake and the ``pybind11::embed`` target, as shown below. For more
17
+ information, see :doc:`/compiling`.
18
+
19
+ .. code-block:: cmake
20
+
21
+ cmake_minimum_required(VERSION 3.4)
22
+ project(example)
23
+
24
+ find_package(pybind11 REQUIRED) # or `add_subdirectory(pybind11)`
25
+
26
+ add_executable(example main.cpp)
27
+ target_link_libraries(example PRIVATE pybind11::embed)
28
+
29
+ The essential structure of the ``main.cpp`` file looks like this:
30
+
31
+ .. code-block:: cpp
32
+
33
+ #include <pybind11/embed.h> // everything needed for embedding
34
+ namespace py = pybind11;
35
+
36
+ int main() {
37
+ py::scoped_interpreter guard{}; // start the interpreter and keep it alive
38
+
39
+ py::print("Hello, World!"); // use the Python API
40
+ }
41
+
42
+ The interpreter must be initialized before using any Python API, which includes
43
+ all the functions and classes in pybind11. The RAII guard class ``scoped_interpreter``
44
+ takes care of the interpreter lifetime. After the guard is destroyed, the interpreter
45
+ shuts down and clears its memory. No Python functions can be called after this.
46
+
47
+ Executing Python code
48
+ =====================
49
+
50
+ There are a few different ways to run Python code. One option is to use ``eval``,
51
+ ``exec`` or ``eval_file``, as explained in :ref:`eval`. Here is a quick example in
52
+ the context of an executable with an embedded interpreter:
53
+
54
+ .. code-block:: cpp
55
+
56
+ #include <pybind11/embed.h>
57
+ namespace py = pybind11;
58
+
59
+ int main() {
60
+ py::scoped_interpreter guard{};
61
+
62
+ py::exec(R"(
63
+ kwargs = dict(name="World", number=42)
64
+ message = "Hello, {name}! The answer is {number}".format(**kwargs)
65
+ print(message)
66
+ )");
67
+ }
68
+
69
+ Alternatively, similar results can be achieved using pybind11's API (see
70
+ :doc:`/advanced/pycpp/index` for more details).
71
+
72
+ .. code-block:: cpp
73
+
74
+ #include <pybind11/embed.h>
75
+ namespace py = pybind11;
76
+ using namespace py::literals;
77
+
78
+ int main() {
79
+ py::scoped_interpreter guard{};
80
+
81
+ auto kwargs = py::dict("name"_a="World", "number"_a=42);
82
+ auto message = "Hello, {name}! The answer is {number}"_s.format(**kwargs);
83
+ py::print(message);
84
+ }
85
+
86
+ The two approaches can also be combined:
87
+
88
+ .. code-block:: cpp
89
+
90
+ #include <pybind11/embed.h>
91
+ #include <iostream>
92
+
93
+ namespace py = pybind11;
94
+ using namespace py::literals;
95
+
96
+ int main() {
97
+ py::scoped_interpreter guard{};
98
+
99
+ auto locals = py::dict("name"_a="World", "number"_a=42);
100
+ py::exec(R"(
101
+ message = "Hello, {name}! The answer is {number}".format(**locals())
102
+ )", py::globals(), locals);
103
+
104
+ auto message = locals["message"].cast<std::string>();
105
+ std::cout << message;
106
+ }
107
+
108
+ Importing modules
109
+ =================
110
+
111
+ Python modules can be imported using ``module_::import()``:
112
+
113
+ .. code-block:: cpp
114
+
115
+ py::module_ sys = py::module_::import("sys");
116
+ py::print(sys.attr("path"));
117
+
118
+ For convenience, the current working directory is included in ``sys.path`` when
119
+ embedding the interpreter. This makes it easy to import local Python files:
120
+
121
+ .. code-block:: python
122
+
123
+ """calc.py located in the working directory"""
124
+
125
+
126
+ def add(i, j):
127
+ return i + j
128
+
129
+
130
+ .. code-block:: cpp
131
+
132
+ py::module_ calc = py::module_::import("calc");
133
+ py::object result = calc.attr("add")(1, 2);
134
+ int n = result.cast<int>();
135
+ assert(n == 3);
136
+
137
+ Modules can be reloaded using ``module_::reload()`` if the source is modified e.g.
138
+ by an external process. This can be useful in scenarios where the application
139
+ imports a user defined data processing script which needs to be updated after
140
+ changes by the user. Note that this function does not reload modules recursively.
141
+
142
+ .. _embedding_modules:
143
+
144
+ Adding embedded modules
145
+ =======================
146
+
147
+ Embedded binary modules can be added using the ``PYBIND11_EMBEDDED_MODULE`` macro.
148
+ Note that the definition must be placed at global scope. They can be imported
149
+ like any other module.
150
+
151
+ .. code-block:: cpp
152
+
153
+ #include <pybind11/embed.h>
154
+ namespace py = pybind11;
155
+
156
+ PYBIND11_EMBEDDED_MODULE(fast_calc, m) {
157
+ // `m` is a `py::module_` which is used to bind functions and classes
158
+ m.def("add", [](int i, int j) {
159
+ return i + j;
160
+ });
161
+ }
162
+
163
+ int main() {
164
+ py::scoped_interpreter guard{};
165
+
166
+ auto fast_calc = py::module_::import("fast_calc");
167
+ auto result = fast_calc.attr("add")(1, 2).cast<int>();
168
+ assert(result == 3);
169
+ }
170
+
171
+ Unlike extension modules where only a single binary module can be created, on
172
+ the embedded side an unlimited number of modules can be added using multiple
173
+ ``PYBIND11_EMBEDDED_MODULE`` definitions (as long as they have unique names).
174
+
175
+ These modules are added to Python's list of builtins, so they can also be
176
+ imported in pure Python files loaded by the interpreter. Everything interacts
177
+ naturally:
178
+
179
+ .. code-block:: python
180
+
181
+ """py_module.py located in the working directory"""
182
+ import cpp_module
183
+
184
+ a = cpp_module.a
185
+ b = a + 1
186
+
187
+
188
+ .. code-block:: cpp
189
+
190
+ #include <pybind11/embed.h>
191
+ namespace py = pybind11;
192
+
193
+ PYBIND11_EMBEDDED_MODULE(cpp_module, m) {
194
+ m.attr("a") = 1;
195
+ }
196
+
197
+ int main() {
198
+ py::scoped_interpreter guard{};
199
+
200
+ auto py_module = py::module_::import("py_module");
201
+
202
+ auto locals = py::dict("fmt"_a="{} + {} = {}", **py_module.attr("__dict__"));
203
+ assert(locals["a"].cast<int>() == 1);
204
+ assert(locals["b"].cast<int>() == 2);
205
+
206
+ py::exec(R"(
207
+ c = a + b
208
+ message = fmt.format(a, b, c)
209
+ )", py::globals(), locals);
210
+
211
+ assert(locals["c"].cast<int>() == 3);
212
+ assert(locals["message"].cast<std::string>() == "1 + 2 = 3");
213
+ }
214
+
215
+
216
+ Interpreter lifetime
217
+ ====================
218
+
219
+ The Python interpreter shuts down when ``scoped_interpreter`` is destroyed. After
220
+ this, creating a new instance will restart the interpreter. Alternatively, the
221
+ ``initialize_interpreter`` / ``finalize_interpreter`` pair of functions can be used
222
+ to directly set the state at any time.
223
+
224
+ Modules created with pybind11 can be safely re-initialized after the interpreter
225
+ has been restarted. However, this may not apply to third-party extension modules.
226
+ The issue is that Python itself cannot completely unload extension modules and
227
+ there are several caveats with regard to interpreter restarting. In short, not
228
+ all memory may be freed, either due to Python reference cycles or user-created
229
+ global data. All the details can be found in the CPython documentation.
230
+
231
+ .. warning::
232
+
233
+ Creating two concurrent ``scoped_interpreter`` guards is a fatal error. So is
234
+ calling ``initialize_interpreter`` for a second time after the interpreter
235
+ has already been initialized.
236
+
237
+ Do not use the raw CPython API functions ``Py_Initialize`` and
238
+ ``Py_Finalize`` as these do not properly handle the lifetime of
239
+ pybind11's internal data.
240
+
241
+
242
+ Sub-interpreter support
243
+ =======================
244
+
245
+ Creating multiple copies of ``scoped_interpreter`` is not possible because it
246
+ represents the main Python interpreter. Sub-interpreters are something different
247
+ and they do permit the existence of multiple interpreters. This is an advanced
248
+ feature of the CPython API and should be handled with care. pybind11 does not
249
+ currently offer a C++ interface for sub-interpreters, so refer to the CPython
250
+ documentation for all the details regarding this feature.
251
+
252
+ We'll just mention a couple of caveats the sub-interpreters support in pybind11:
253
+
254
+ 1. Sub-interpreters will not receive independent copies of embedded modules.
255
+ Instead, these are shared and modifications in one interpreter may be
256
+ reflected in another.
257
+
258
+ 2. Managing multiple threads, multiple interpreters and the GIL can be
259
+ challenging and there are several caveats here, even within the pure
260
+ CPython API (please refer to the Python docs for details). As for
261
+ pybind11, keep in mind that ``gil_scoped_release`` and ``gil_scoped_acquire``
262
+ do not take sub-interpreters into account.
@@ -0,0 +1,396 @@
1
+ Exceptions
2
+ ##########
3
+
4
+ Built-in C++ to Python exception translation
5
+ ============================================
6
+
7
+ When Python calls C++ code through pybind11, pybind11 provides a C++ exception handler
8
+ that will trap C++ exceptions, translate them to the corresponding Python exception,
9
+ and raise them so that Python code can handle them.
10
+
11
+ pybind11 defines translations for ``std::exception`` and its standard
12
+ subclasses, and several special exception classes that translate to specific
13
+ Python exceptions. Note that these are not actually Python exceptions, so they
14
+ cannot be examined using the Python C API. Instead, they are pure C++ objects
15
+ that pybind11 will translate the corresponding Python exception when they arrive
16
+ at its exception handler.
17
+
18
+ .. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
19
+
20
+ +--------------------------------------+--------------------------------------+
21
+ | Exception thrown by C++ | Translated to Python exception type |
22
+ +======================================+======================================+
23
+ | :class:`std::exception` | ``RuntimeError`` |
24
+ +--------------------------------------+--------------------------------------+
25
+ | :class:`std::bad_alloc` | ``MemoryError`` |
26
+ +--------------------------------------+--------------------------------------+
27
+ | :class:`std::domain_error` | ``ValueError`` |
28
+ +--------------------------------------+--------------------------------------+
29
+ | :class:`std::invalid_argument` | ``ValueError`` |
30
+ +--------------------------------------+--------------------------------------+
31
+ | :class:`std::length_error` | ``ValueError`` |
32
+ +--------------------------------------+--------------------------------------+
33
+ | :class:`std::out_of_range` | ``IndexError`` |
34
+ +--------------------------------------+--------------------------------------+
35
+ | :class:`std::range_error` | ``ValueError`` |
36
+ +--------------------------------------+--------------------------------------+
37
+ | :class:`std::overflow_error` | ``OverflowError`` |
38
+ +--------------------------------------+--------------------------------------+
39
+ | :class:`pybind11::stop_iteration` | ``StopIteration`` (used to implement |
40
+ | | custom iterators) |
41
+ +--------------------------------------+--------------------------------------+
42
+ | :class:`pybind11::index_error` | ``IndexError`` (used to indicate out |
43
+ | | of bounds access in ``__getitem__``, |
44
+ | | ``__setitem__``, etc.) |
45
+ +--------------------------------------+--------------------------------------+
46
+ | :class:`pybind11::key_error` | ``KeyError`` (used to indicate out |
47
+ | | of bounds access in ``__getitem__``, |
48
+ | | ``__setitem__`` in dict-like |
49
+ | | objects, etc.) |
50
+ +--------------------------------------+--------------------------------------+
51
+ | :class:`pybind11::value_error` | ``ValueError`` (used to indicate |
52
+ | | wrong value passed in |
53
+ | | ``container.remove(...)``) |
54
+ +--------------------------------------+--------------------------------------+
55
+ | :class:`pybind11::type_error` | ``TypeError`` |
56
+ +--------------------------------------+--------------------------------------+
57
+ | :class:`pybind11::buffer_error` | ``BufferError`` |
58
+ +--------------------------------------+--------------------------------------+
59
+ | :class:`pybind11::import_error` | ``import_error`` |
60
+ +--------------------------------------+--------------------------------------+
61
+ | Any other exception | ``RuntimeError`` |
62
+ +--------------------------------------+--------------------------------------+
63
+
64
+ Exception translation is not bidirectional. That is, *catching* the C++
65
+ exceptions defined above above will not trap exceptions that originate from
66
+ Python. For that, catch :class:`pybind11::error_already_set`. See :ref:`below
67
+ <handling_python_exceptions_cpp>` for further details.
68
+
69
+ There is also a special exception :class:`cast_error` that is thrown by
70
+ :func:`handle::call` when the input arguments cannot be converted to Python
71
+ objects.
72
+
73
+ Registering custom translators
74
+ ==============================
75
+
76
+ If the default exception conversion policy described above is insufficient,
77
+ pybind11 also provides support for registering custom exception translators.
78
+ Similar to pybind11 classes, exception translators can be local to the module
79
+ they are defined in or global to the entire python session. To register a simple
80
+ exception conversion that translates a C++ exception into a new Python exception
81
+ using the C++ exception's ``what()`` method, a helper function is available:
82
+
83
+ .. code-block:: cpp
84
+
85
+ py::register_exception<CppExp>(module, "PyExp");
86
+
87
+ This call creates a Python exception class with the name ``PyExp`` in the given
88
+ module and automatically converts any encountered exceptions of type ``CppExp``
89
+ into Python exceptions of type ``PyExp``.
90
+
91
+ A matching function is available for registering a local exception translator:
92
+
93
+ .. code-block:: cpp
94
+
95
+ py::register_local_exception<CppExp>(module, "PyExp");
96
+
97
+
98
+ It is possible to specify base class for the exception using the third
99
+ parameter, a ``handle``:
100
+
101
+ .. code-block:: cpp
102
+
103
+ py::register_exception<CppExp>(module, "PyExp", PyExc_RuntimeError);
104
+ py::register_local_exception<CppExp>(module, "PyExp", PyExc_RuntimeError);
105
+
106
+ Then ``PyExp`` can be caught both as ``PyExp`` and ``RuntimeError``.
107
+
108
+ The class objects of the built-in Python exceptions are listed in the Python
109
+ documentation on `Standard Exceptions <https://docs.python.org/3/c-api/exceptions.html#standard-exceptions>`_.
110
+ The default base class is ``PyExc_Exception``.
111
+
112
+ When more advanced exception translation is needed, the functions
113
+ ``py::register_exception_translator(translator)`` and
114
+ ``py::register_local_exception_translator(translator)`` can be used to register
115
+ functions that can translate arbitrary exception types (and which may include
116
+ additional logic to do so). The functions takes a stateless callable (e.g. a
117
+ function pointer or a lambda function without captured variables) with the call
118
+ signature ``void(std::exception_ptr)``.
119
+
120
+ When a C++ exception is thrown, the registered exception translators are tried
121
+ in reverse order of registration (i.e. the last registered translator gets the
122
+ first shot at handling the exception). All local translators will be tried
123
+ before a global translator is tried.
124
+
125
+ Inside the translator, ``std::rethrow_exception`` should be used within
126
+ a try block to re-throw the exception. One or more catch clauses to catch
127
+ the appropriate exceptions should then be used with each clause using
128
+ ``PyErr_SetString`` to set a Python exception or ``ex(string)`` to set
129
+ the python exception to a custom exception type (see below).
130
+
131
+ To declare a custom Python exception type, declare a ``py::exception`` variable
132
+ and use this in the associated exception translator (note: it is often useful
133
+ to make this a static declaration when using it inside a lambda expression
134
+ without requiring capturing).
135
+
136
+ The following example demonstrates this for a hypothetical exception classes
137
+ ``MyCustomException`` and ``OtherException``: the first is translated to a
138
+ custom python exception ``MyCustomError``, while the second is translated to a
139
+ standard python RuntimeError:
140
+
141
+ .. code-block:: cpp
142
+
143
+ static py::exception<MyCustomException> exc(m, "MyCustomError");
144
+ py::register_exception_translator([](std::exception_ptr p) {
145
+ try {
146
+ if (p) std::rethrow_exception(p);
147
+ } catch (const MyCustomException &e) {
148
+ exc(e.what());
149
+ } catch (const OtherException &e) {
150
+ PyErr_SetString(PyExc_RuntimeError, e.what());
151
+ }
152
+ });
153
+
154
+ Multiple exceptions can be handled by a single translator, as shown in the
155
+ example above. If the exception is not caught by the current translator, the
156
+ previously registered one gets a chance.
157
+
158
+ If none of the registered exception translators is able to handle the
159
+ exception, it is handled by the default converter as described in the previous
160
+ section.
161
+
162
+ .. seealso::
163
+
164
+ The file :file:`tests/test_exceptions.cpp` contains examples
165
+ of various custom exception translators and custom exception types.
166
+
167
+ .. note::
168
+
169
+ Call either ``PyErr_SetString`` or a custom exception's call
170
+ operator (``exc(string)``) for every exception caught in a custom exception
171
+ translator. Failure to do so will cause Python to crash with ``SystemError:
172
+ error return without exception set``.
173
+
174
+ Exceptions that you do not plan to handle should simply not be caught, or
175
+ may be explicitly (re-)thrown to delegate it to the other,
176
+ previously-declared existing exception translators.
177
+
178
+ Note that ``libc++`` and ``libstdc++`` `behave differently <https://stackoverflow.com/questions/19496643/using-clang-fvisibility-hidden-and-typeinfo-and-type-erasure/28827430>`_
179
+ with ``-fvisibility=hidden``. Therefore exceptions that are used across ABI boundaries need to be explicitly exported, as exercised in ``tests/test_exceptions.h``.
180
+ See also: "Problems with C++ exceptions" under `GCC Wiki <https://gcc.gnu.org/wiki/Visibility>`_.
181
+
182
+
183
+ Local vs Global Exception Translators
184
+ =====================================
185
+
186
+ When a global exception translator is registered, it will be applied across all
187
+ modules in the reverse order of registration. This can create behavior where the
188
+ order of module import influences how exceptions are translated.
189
+
190
+ If module1 has the following translator:
191
+
192
+ .. code-block:: cpp
193
+
194
+ py::register_exception_translator([](std::exception_ptr p) {
195
+ try {
196
+ if (p) std::rethrow_exception(p);
197
+ } catch (const std::invalid_argument &e) {
198
+ PyErr_SetString("module1 handled this")
199
+ }
200
+ }
201
+
202
+ and module2 has the following similar translator:
203
+
204
+ .. code-block:: cpp
205
+
206
+ py::register_exception_translator([](std::exception_ptr p) {
207
+ try {
208
+ if (p) std::rethrow_exception(p);
209
+ } catch (const std::invalid_argument &e) {
210
+ PyErr_SetString("module2 handled this")
211
+ }
212
+ }
213
+
214
+ then which translator handles the invalid_argument will be determined by the
215
+ order that module1 and module2 are imported. Since exception translators are
216
+ applied in the reverse order of registration, which ever module was imported
217
+ last will "win" and that translator will be applied.
218
+
219
+ If there are multiple pybind11 modules that share exception types (either
220
+ standard built-in or custom) loaded into a single python instance and
221
+ consistent error handling behavior is needed, then local translators should be
222
+ used.
223
+
224
+ Changing the previous example to use ``register_local_exception_translator``
225
+ would mean that when invalid_argument is thrown in the module2 code, the
226
+ module2 translator will always handle it, while in module1, the module1
227
+ translator will do the same.
228
+
229
+ .. _handling_python_exceptions_cpp:
230
+
231
+ Handling exceptions from Python in C++
232
+ ======================================
233
+
234
+ When C++ calls Python functions, such as in a callback function or when
235
+ manipulating Python objects, and Python raises an ``Exception``, pybind11
236
+ converts the Python exception into a C++ exception of type
237
+ :class:`pybind11::error_already_set` whose payload contains a C++ string textual
238
+ summary and the actual Python exception. ``error_already_set`` is used to
239
+ propagate Python exception back to Python (or possibly, handle them in C++).
240
+
241
+ .. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
242
+
243
+ +--------------------------------------+--------------------------------------+
244
+ | Exception raised in Python | Thrown as C++ exception type |
245
+ +======================================+======================================+
246
+ | Any Python ``Exception`` | :class:`pybind11::error_already_set` |
247
+ +--------------------------------------+--------------------------------------+
248
+
249
+ For example:
250
+
251
+ .. code-block:: cpp
252
+
253
+ try {
254
+ // open("missing.txt", "r")
255
+ auto file = py::module_::import("io").attr("open")("missing.txt", "r");
256
+ auto text = file.attr("read")();
257
+ file.attr("close")();
258
+ } catch (py::error_already_set &e) {
259
+ if (e.matches(PyExc_FileNotFoundError)) {
260
+ py::print("missing.txt not found");
261
+ } else if (e.matches(PyExc_PermissionError)) {
262
+ py::print("missing.txt found but not accessible");
263
+ } else {
264
+ throw;
265
+ }
266
+ }
267
+
268
+ Note that C++ to Python exception translation does not apply here, since that is
269
+ a method for translating C++ exceptions to Python, not vice versa. The error raised
270
+ from Python is always ``error_already_set``.
271
+
272
+ This example illustrates this behavior:
273
+
274
+ .. code-block:: cpp
275
+
276
+ try {
277
+ py::eval("raise ValueError('The Ring')");
278
+ } catch (py::value_error &boromir) {
279
+ // Boromir never gets the ring
280
+ assert(false);
281
+ } catch (py::error_already_set &frodo) {
282
+ // Frodo gets the ring
283
+ py::print("I will take the ring");
284
+ }
285
+
286
+ try {
287
+ // py::value_error is a request for pybind11 to raise a Python exception
288
+ throw py::value_error("The ball");
289
+ } catch (py::error_already_set &cat) {
290
+ // cat won't catch the ball since
291
+ // py::value_error is not a Python exception
292
+ assert(false);
293
+ } catch (py::value_error &dog) {
294
+ // dog will catch the ball
295
+ py::print("Run Spot run");
296
+ throw; // Throw it again (pybind11 will raise ValueError)
297
+ }
298
+
299
+ Handling errors from the Python C API
300
+ =====================================
301
+
302
+ Where possible, use :ref:`pybind11 wrappers <wrappers>` instead of calling
303
+ the Python C API directly. When calling the Python C API directly, in
304
+ addition to manually managing reference counts, one must follow the pybind11
305
+ error protocol, which is outlined here.
306
+
307
+ After calling the Python C API, if Python returns an error,
308
+ ``throw py::error_already_set();``, which allows pybind11 to deal with the
309
+ exception and pass it back to the Python interpreter. This includes calls to
310
+ the error setting functions such as ``PyErr_SetString``.
311
+
312
+ .. code-block:: cpp
313
+
314
+ PyErr_SetString(PyExc_TypeError, "C API type error demo");
315
+ throw py::error_already_set();
316
+
317
+ // But it would be easier to simply...
318
+ throw py::type_error("pybind11 wrapper type error");
319
+
320
+ Alternately, to ignore the error, call `PyErr_Clear
321
+ <https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Clear>`_.
322
+
323
+ Any Python error must be thrown or cleared, or Python/pybind11 will be left in
324
+ an invalid state.
325
+
326
+ Chaining exceptions ('raise from')
327
+ ==================================
328
+
329
+ In Python 3.3 a mechanism for indicating that exceptions were caused by other
330
+ exceptions was introduced:
331
+
332
+ .. code-block:: py
333
+
334
+ try:
335
+ print(1 / 0)
336
+ except Exception as exc:
337
+ raise RuntimeError("could not divide by zero") from exc
338
+
339
+ To do a similar thing in pybind11, you can use the ``py::raise_from`` function. It
340
+ sets the current python error indicator, so to continue propagating the exception
341
+ you should ``throw py::error_already_set()`` (Python 3 only).
342
+
343
+ .. code-block:: cpp
344
+
345
+ try {
346
+ py::eval("print(1 / 0"));
347
+ } catch (py::error_already_set &e) {
348
+ py::raise_from(e, PyExc_RuntimeError, "could not divide by zero");
349
+ throw py::error_already_set();
350
+ }
351
+
352
+ .. versionadded:: 2.8
353
+
354
+ .. _unraisable_exceptions:
355
+
356
+ Handling unraisable exceptions
357
+ ==============================
358
+
359
+ If a Python function invoked from a C++ destructor or any function marked
360
+ ``noexcept(true)`` (collectively, "noexcept functions") throws an exception, there
361
+ is no way to propagate the exception, as such functions may not throw.
362
+ Should they throw or fail to catch any exceptions in their call graph,
363
+ the C++ runtime calls ``std::terminate()`` to abort immediately.
364
+
365
+ Similarly, Python exceptions raised in a class's ``__del__`` method do not
366
+ propagate, but are logged by Python as an unraisable error. In Python 3.8+, a
367
+ `system hook is triggered
368
+ <https://docs.python.org/3/library/sys.html#sys.unraisablehook>`_
369
+ and an auditing event is logged.
370
+
371
+ Any noexcept function should have a try-catch block that traps
372
+ class:`error_already_set` (or any other exception that can occur). Note that
373
+ pybind11 wrappers around Python exceptions such as
374
+ :class:`pybind11::value_error` are *not* Python exceptions; they are C++
375
+ exceptions that pybind11 catches and converts to Python exceptions. Noexcept
376
+ functions cannot propagate these exceptions either. A useful approach is to
377
+ convert them to Python exceptions and then ``discard_as_unraisable`` as shown
378
+ below.
379
+
380
+ .. code-block:: cpp
381
+
382
+ void nonthrowing_func() noexcept(true) {
383
+ try {
384
+ // ...
385
+ } catch (py::error_already_set &eas) {
386
+ // Discard the Python error using Python APIs, using the C++ magic
387
+ // variable __func__. Python already knows the type and value and of the
388
+ // exception object.
389
+ eas.discard_as_unraisable(__func__);
390
+ } catch (const std::exception &e) {
391
+ // Log and discard C++ exceptions.
392
+ third_party::log(e);
393
+ }
394
+ }
395
+
396
+ .. versionadded:: 2.6