xcoll 0.5.12__py3-none-any.whl → 0.6.1__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.
- xcoll/__init__.py +5 -18
- xcoll/__init__.py.orig +26 -0
- xcoll/beam_elements/__init__.py +1 -0
- xcoll/beam_elements/absorber.py +12 -2
- xcoll/beam_elements/base.py +162 -62
- xcoll/beam_elements/blowup.py +1 -0
- xcoll/beam_elements/elements_src/black_absorber.h +57 -58
- xcoll/beam_elements/elements_src/black_crystal.h +49 -50
- xcoll/beam_elements/elements_src/everest_block.h +21 -11
- xcoll/beam_elements/elements_src/everest_collimator.h +100 -91
- xcoll/beam_elements/elements_src/everest_crystal.h +145 -140
- xcoll/beam_elements/elements_src/transparent_collimator.h +126 -0
- xcoll/beam_elements/elements_src/transparent_crystal.h +118 -0
- xcoll/beam_elements/everest.py +16 -5
- xcoll/beam_elements/monitor.py +1 -0
- xcoll/beam_elements/transparent.py +83 -0
- xcoll/colldb.py +15 -6
- xcoll/general.py +1 -1
- xcoll/general.py.orig +20 -0
- xcoll/headers/particle_states.py +51 -0
- xcoll/initial_distribution.py +121 -91
- xcoll/initial_distribution.py.orig +256 -0
- xcoll/interaction_record/interaction_record.py +2 -1
- xcoll/interaction_record/interaction_types.py +2 -2
- xcoll/line_tools.py +163 -73
- xcoll/lossmap.py +519 -127
- xcoll/plot.py +109 -0
- xcoll/rf_sweep.py +6 -0
- xcoll/scattering_routines/engine.py +414 -217
- xcoll/scattering_routines/environment.py +297 -0
- xcoll/scattering_routines/everest/amorphous.h +95 -71
- xcoll/scattering_routines/everest/{channeling.h → channelling.h} +121 -112
- xcoll/scattering_routines/everest/constants.h +1 -1
- xcoll/scattering_routines/everest/crystal_parameters.h +9 -9
- xcoll/scattering_routines/everest/everest.h +8 -3
- xcoll/scattering_routines/everest/everest.py +2 -1
- xcoll/scattering_routines/everest/ionisation_loss.h +141 -0
- xcoll/scattering_routines/everest/jaw.h +19 -24
- xcoll/scattering_routines/everest/materials.py +2 -0
- xcoll/scattering_routines/everest/multiple_coulomb_scattering.h +2 -2
- xcoll/scattering_routines/everest/nuclear_interaction.h +35 -19
- xcoll/scattering_routines/everest/properties.h +3 -72
- xcoll/xaux.py +65 -109
- {xcoll-0.5.12.dist-info → xcoll-0.6.1.dist-info}/METADATA +5 -5
- xcoll-0.6.1.dist-info/RECORD +138 -0
- xcoll/_manager.py +0 -22
- xcoll/headers/particle_states.h +0 -25
- xcoll/install.py +0 -35
- xcoll/scattering_routines/geant4/collimasim/.git +0 -1
- xcoll/scattering_routines/geant4/collimasim/.gitignore +0 -12
- xcoll/scattering_routines/geant4/collimasim/.gitmodules +0 -3
- xcoll/scattering_routines/geant4/collimasim/CMakeLists.txt +0 -26
- xcoll/scattering_routines/geant4/collimasim/README.md +0 -21
- xcoll/scattering_routines/geant4/collimasim/docs/Makefile +0 -20
- xcoll/scattering_routines/geant4/collimasim/docs/make.bat +0 -35
- xcoll/scattering_routines/geant4/collimasim/docs/source/collimasim.rst +0 -10
- xcoll/scattering_routines/geant4/collimasim/docs/source/conf.py +0 -59
- xcoll/scattering_routines/geant4/collimasim/docs/source/index.rst +0 -26
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.appveyor.yml +0 -37
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.clang-format +0 -19
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.clang-tidy +0 -65
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.cmake-format.yaml +0 -73
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.git +0 -1
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/CODEOWNERS +0 -9
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/CONTRIBUTING.md +0 -386
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/ISSUE_TEMPLATE/bug-report.yml +0 -45
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/ISSUE_TEMPLATE/config.yml +0 -8
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/dependabot.yml +0 -16
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/labeler.yml +0 -8
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/labeler_merged.yml +0 -3
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/pull_request_template.md +0 -19
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/ci.yml +0 -969
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/configure.yml +0 -84
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/format.yml +0 -48
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/labeler.yml +0 -16
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.github/workflows/pip.yml +0 -103
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.gitignore +0 -45
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.pre-commit-config.yaml +0 -151
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/.readthedocs.yml +0 -3
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/CMakeLists.txt +0 -297
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/LICENSE +0 -29
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/MANIFEST.in +0 -6
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/README.rst +0 -180
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/Doxyfile +0 -23
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/Makefile +0 -192
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/_static/theme_overrides.css +0 -11
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/chrono.rst +0 -81
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/custom.rst +0 -93
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/eigen.rst +0 -310
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/functional.rst +0 -109
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/index.rst +0 -43
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/overview.rst +0 -171
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/stl.rst +0 -251
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/cast/strings.rst +0 -305
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/classes.rst +0 -1297
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/embedding.rst +0 -262
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/exceptions.rst +0 -396
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/functions.rst +0 -568
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/misc.rst +0 -337
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/index.rst +0 -13
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/numpy.rst +0 -463
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/object.rst +0 -286
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/pycpp/utilities.rst +0 -155
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/advanced/smart_ptrs.rst +0 -174
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/basics.rst +0 -308
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/benchmark.py +0 -91
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/benchmark.rst +0 -95
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/changelog.rst +0 -2050
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/classes.rst +0 -542
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/cmake/index.rst +0 -8
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/compiling.rst +0 -648
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/conf.py +0 -381
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/faq.rst +0 -343
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/index.rst +0 -48
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/installing.rst +0 -105
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/limitations.rst +0 -72
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11-logo.png +0 -0
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python1.png +0 -0
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python1.svg +0 -427
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python2.png +0 -0
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/pybind11_vs_boost_python2.svg +0 -427
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/reference.rst +0 -130
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/release.rst +0 -96
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/requirements.txt +0 -8
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/docs/upgrade.rst +0 -548
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/attr.h +0 -605
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/buffer_info.h +0 -144
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/cast.h +0 -1432
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/chrono.h +0 -213
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/common.h +0 -2
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/complex.h +0 -65
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/class.h +0 -709
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/common.h +0 -1021
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/descr.h +0 -104
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/init.h +0 -346
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/internals.h +0 -467
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/type_caster_base.h +0 -978
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/detail/typeid.h +0 -55
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/eigen.h +0 -606
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/embed.h +0 -284
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/eval.h +0 -163
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/functional.h +0 -121
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/gil.h +0 -193
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/iostream.h +0 -275
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/numpy.h +0 -1741
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/operators.h +0 -163
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/options.h +0 -65
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/pybind11.h +0 -2497
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/pytypes.h +0 -1879
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl/filesystem.h +0 -103
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl.h +0 -375
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/include/pybind11/stl_bind.h +0 -747
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/noxfile.py +0 -88
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/__init__.py +0 -11
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/__main__.py +0 -52
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/_version.py +0 -12
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/_version.pyi +0 -6
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/commands.py +0 -21
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/py.typed +0 -0
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/setup_helpers.py +0 -482
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pybind11/setup_helpers.pyi +0 -63
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/pyproject.toml +0 -41
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/setup.cfg +0 -56
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/setup.py +0 -155
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/CMakeLists.txt +0 -503
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/conftest.py +0 -208
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/constructor_stats.h +0 -275
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/cross_module_gil_utils.cpp +0 -73
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/env.py +0 -33
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_python_package/pytest.ini +0 -0
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_python_package/test_files.py +0 -279
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_setuptools/pytest.ini +0 -0
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/extra_setuptools/test_setuphelper.py +0 -143
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/local_bindings.h +0 -85
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/object.h +0 -179
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_cross_module_tests.cpp +0 -151
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_tests.cpp +0 -91
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pybind11_tests.h +0 -85
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/pytest.ini +0 -19
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/requirements.txt +0 -12
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_async.cpp +0 -26
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_async.py +0 -25
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_buffers.cpp +0 -216
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_buffers.py +0 -163
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_builtin_casters.cpp +0 -286
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_builtin_casters.py +0 -536
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_call_policies.cpp +0 -107
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_call_policies.py +0 -248
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_callbacks.cpp +0 -227
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_callbacks.py +0 -202
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_chrono.cpp +0 -84
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_chrono.py +0 -210
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_class.cpp +0 -550
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_class.py +0 -473
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/CMakeLists.txt +0 -84
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/embed.cpp +0 -21
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_embed/CMakeLists.txt +0 -28
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_function/CMakeLists.txt +0 -39
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/installed_target/CMakeLists.txt +0 -46
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/main.cpp +0 -6
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt +0 -41
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_function/CMakeLists.txt +0 -35
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/subdirectory_target/CMakeLists.txt +0 -41
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_cmake_build/test.py +0 -10
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_constants_and_functions.cpp +0 -165
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_constants_and_functions.py +0 -53
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_copy_move.cpp +0 -238
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_copy_move.py +0 -126
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_casters.cpp +0 -141
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_casters.py +0 -117
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_setup.cpp +0 -41
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_custom_type_setup.py +0 -50
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_docstring_options.cpp +0 -69
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_docstring_options.py +0 -42
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eigen.cpp +0 -348
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eigen.py +0 -771
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/CMakeLists.txt +0 -47
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/catch.cpp +0 -22
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/external_module.cpp +0 -23
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/test_interpreter.cpp +0 -326
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_embed/test_interpreter.py +0 -15
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_enum.cpp +0 -148
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_enum.py +0 -272
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval.cpp +0 -119
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval.py +0 -51
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_eval_call.py +0 -5
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.cpp +0 -285
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.h +0 -12
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_exceptions.py +0 -265
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_factory_constructors.cpp +0 -397
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_factory_constructors.py +0 -520
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_gil_scoped.cpp +0 -49
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_gil_scoped.py +0 -94
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_iostream.cpp +0 -125
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_iostream.py +0 -331
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_kwargs_and_defaults.cpp +0 -153
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_kwargs_and_defaults.py +0 -284
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_local_bindings.cpp +0 -107
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_local_bindings.py +0 -257
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_methods_and_attributes.cpp +0 -412
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_methods_and_attributes.py +0 -517
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_modules.cpp +0 -102
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_modules.py +0 -92
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_multiple_inheritance.cpp +0 -233
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_multiple_inheritance.py +0 -360
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_array.cpp +0 -472
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_array.py +0 -593
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_dtypes.cpp +0 -524
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_dtypes.py +0 -441
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_vectorize.cpp +0 -103
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_numpy_vectorize.py +0 -267
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_opaque_types.cpp +0 -73
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_opaque_types.py +0 -59
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_operator_overloading.cpp +0 -235
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_operator_overloading.py +0 -146
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pickling.cpp +0 -189
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pickling.py +0 -82
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pytypes.cpp +0 -560
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_pytypes.py +0 -651
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_sequences_and_iterators.cpp +0 -500
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_sequences_and_iterators.py +0 -253
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_smart_ptr.cpp +0 -452
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_smart_ptr.py +0 -318
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl.cpp +0 -342
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl.py +0 -291
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl_binders.cpp +0 -131
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_stl_binders.py +0 -318
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_tagbased_polymorphic.cpp +0 -144
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_tagbased_polymorphic.py +0 -29
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_thread.cpp +0 -66
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_thread.py +0 -44
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_union.cpp +0 -22
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_union.py +0 -9
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_virtual_functions.cpp +0 -510
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/test_virtual_functions.py +0 -408
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/valgrind-numpy-scipy.supp +0 -140
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tests/valgrind-python.supp +0 -117
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindCatch.cmake +0 -70
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindEigen3.cmake +0 -86
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/FindPythonLibsNew.cmake +0 -257
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/check-style.sh +0 -44
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/cmake_uninstall.cmake.in +0 -23
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/libsize.py +0 -39
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/make_changelog.py +0 -64
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Common.cmake +0 -402
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Config.cmake.in +0 -233
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11NewTools.cmake +0 -276
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pybind11Tools.cmake +0 -214
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/pyproject.toml +0 -3
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/setup_global.py.in +0 -65
- xcoll/scattering_routines/geant4/collimasim/lib/pybind11/tools/setup_main.py.in +0 -41
- xcoll/scattering_routines/geant4/collimasim/pyproject.toml +0 -8
- xcoll/scattering_routines/geant4/collimasim/setup.py +0 -144
- xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSPyATInterface.cpp +0 -403
- xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSPyATInterface.hh +0 -100
- xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSXtrackInterface.cpp +0 -763
- xcoll/scattering_routines/geant4/collimasim/src/collimasim/BDSXtrackInterface.hh +0 -118
- xcoll/scattering_routines/geant4/collimasim/src/collimasim/__init__.py +0 -8
- xcoll/scattering_routines/geant4/collimasim/src/collimasim/bindings.cpp +0 -63
- xcoll/scattering_routines/geant4/collimasim/src/collimasim/pyCollimatorPass.py +0 -142
- xcoll/scattering_routines/geant4/collimasim/src/collimasim/xtrack_collimator.py +0 -556
- xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/PKG-INFO +0 -6
- xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/SOURCES.txt +0 -24
- xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/dependency_links.txt +0 -1
- xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/not-zip-safe +0 -1
- xcoll/scattering_routines/geant4/collimasim/src/collimasim.egg-info/top_level.txt +0 -1
- xcoll/scattering_routines/geant4/collimasim/tests/README.md +0 -25
- xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_forions.dat +0 -25
- xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_new_example.dat +0 -18
- xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_old_example.dat +0 -68
- xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_testing.dat +0 -15
- xcoll/scattering_routines/geant4/collimasim/tests/resources/CollDB_yaml_example.yaml +0 -110
- xcoll/scattering_routines/geant4/collimasim/tests/resources/collgaps.dat +0 -7
- xcoll/scattering_routines/geant4/collimasim/tests/resources/collgaps_pyat_test.dat +0 -3
- xcoll/scattering_routines/geant4/collimasim/tests/resources/collonly_twiss_file_example.tfs +0 -54
- xcoll/scattering_routines/geant4/collimasim/tests/resources/settings.gmad +0 -3
- xcoll/scattering_routines/geant4/collimasim/tests/resources/settings_black_absorber.gmad +0 -3
- xcoll/scattering_routines/geant4/collimasim/tests/resources/settings_ions.gmad +0 -5
- xcoll/scattering_routines/geant4/collimasim/tests/resources/twiss_file_testing.tfs +0 -51
- xcoll/scattering_routines/geant4/collimasim/tests/test_pyat.py +0 -65
- xcoll/scattering_routines/geant4/collimasim/tests/test_pyat_passmethod.py +0 -59
- xcoll/scattering_routines/geant4/collimasim/tests/test_pyat_tracking.py +0 -102
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack.py +0 -75
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_angle.py +0 -74
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_colldb_load.py +0 -84
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_interaction.py +0 -159
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_interaction_ion.py +0 -99
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_ions.py +0 -78
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_lost_energy.py +0 -88
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tilt.py +0 -80
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tracking.py +0 -97
- xcoll/scattering_routines/geant4/collimasim/tests/test_xtrack_tracking_ions.py +0 -96
- xcoll-0.5.12.dist-info/RECORD +0 -415
- {xcoll-0.5.12.dist-info → xcoll-0.6.1.dist-info}/LICENSE +0 -0
- {xcoll-0.5.12.dist-info → xcoll-0.6.1.dist-info}/NOTICE +0 -0
- {xcoll-0.5.12.dist-info → xcoll-0.6.1.dist-info}/WHEEL +0 -0
|
@@ -1,262 +0,0 @@
|
|
|
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.
|
|
@@ -1,396 +0,0 @@
|
|
|
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
|