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,1432 @@
1
+ /*
2
+ pybind11/cast.h: Partial template specializations to cast between
3
+ C++ and Python types
4
+
5
+ Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
6
+
7
+ All rights reserved. Use of this source code is governed by a
8
+ BSD-style license that can be found in the LICENSE file.
9
+ */
10
+
11
+ #pragma once
12
+
13
+ #include "pytypes.h"
14
+ #include "detail/common.h"
15
+ #include "detail/descr.h"
16
+ #include "detail/type_caster_base.h"
17
+ #include "detail/typeid.h"
18
+ #include <array>
19
+ #include <cstring>
20
+ #include <functional>
21
+ #include <iosfwd>
22
+ #include <iterator>
23
+ #include <memory>
24
+ #include <string>
25
+ #include <tuple>
26
+ #include <type_traits>
27
+ #include <utility>
28
+ #include <vector>
29
+
30
+ #if defined(PYBIND11_CPP17)
31
+ # if defined(__has_include)
32
+ # if __has_include(<string_view>)
33
+ # define PYBIND11_HAS_STRING_VIEW
34
+ # endif
35
+ # elif defined(_MSC_VER)
36
+ # define PYBIND11_HAS_STRING_VIEW
37
+ # endif
38
+ #endif
39
+ #ifdef PYBIND11_HAS_STRING_VIEW
40
+ #include <string_view>
41
+ #endif
42
+
43
+ #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
44
+ # define PYBIND11_HAS_U8STRING
45
+ #endif
46
+
47
+ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
48
+ PYBIND11_NAMESPACE_BEGIN(detail)
49
+
50
+ template <typename type, typename SFINAE = void> class type_caster : public type_caster_base<type> { };
51
+ template <typename type> using make_caster = type_caster<intrinsic_t<type>>;
52
+
53
+ // Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T
54
+ template <typename T> typename make_caster<T>::template cast_op_type<T> cast_op(make_caster<T> &caster) {
55
+ return caster.operator typename make_caster<T>::template cast_op_type<T>();
56
+ }
57
+ template <typename T> typename make_caster<T>::template cast_op_type<typename std::add_rvalue_reference<T>::type>
58
+ cast_op(make_caster<T> &&caster) {
59
+ return std::move(caster).operator
60
+ typename make_caster<T>::template cast_op_type<typename std::add_rvalue_reference<T>::type>();
61
+ }
62
+
63
+ template <typename type> class type_caster<std::reference_wrapper<type>> {
64
+ private:
65
+ using caster_t = make_caster<type>;
66
+ caster_t subcaster;
67
+ using reference_t = type&;
68
+ using subcaster_cast_op_type =
69
+ typename caster_t::template cast_op_type<reference_t>;
70
+
71
+ static_assert(std::is_same<typename std::remove_const<type>::type &, subcaster_cast_op_type>::value ||
72
+ std::is_same<reference_t, subcaster_cast_op_type>::value,
73
+ "std::reference_wrapper<T> caster requires T to have a caster with an "
74
+ "`operator T &()` or `operator const T &()`");
75
+ public:
76
+ bool load(handle src, bool convert) { return subcaster.load(src, convert); }
77
+ static constexpr auto name = caster_t::name;
78
+ static handle cast(const std::reference_wrapper<type> &src, return_value_policy policy, handle parent) {
79
+ // It is definitely wrong to take ownership of this pointer, so mask that rvp
80
+ if (policy == return_value_policy::take_ownership || policy == return_value_policy::automatic)
81
+ policy = return_value_policy::automatic_reference;
82
+ return caster_t::cast(&src.get(), policy, parent);
83
+ }
84
+ template <typename T> using cast_op_type = std::reference_wrapper<type>;
85
+ explicit operator std::reference_wrapper<type>() { return cast_op<type &>(subcaster); }
86
+ };
87
+
88
+ #define PYBIND11_TYPE_CASTER(type, py_name) \
89
+ protected: \
90
+ type value; \
91
+ \
92
+ public: \
93
+ static constexpr auto name = py_name; \
94
+ template <typename T_, enable_if_t<std::is_same<type, remove_cv_t<T_>>::value, int> = 0> \
95
+ static handle cast(T_ *src, return_value_policy policy, handle parent) { \
96
+ if (!src) \
97
+ return none().release(); \
98
+ if (policy == return_value_policy::take_ownership) { \
99
+ auto h = cast(std::move(*src), policy, parent); \
100
+ delete src; \
101
+ return h; \
102
+ } \
103
+ return cast(*src, policy, parent); \
104
+ } \
105
+ operator type *() { return &value; } /* NOLINT(bugprone-macro-parentheses) */ \
106
+ operator type &() { return value; } /* NOLINT(bugprone-macro-parentheses) */ \
107
+ operator type &&() && { return std::move(value); } /* NOLINT(bugprone-macro-parentheses) */ \
108
+ template <typename T_> \
109
+ using cast_op_type = pybind11::detail::movable_cast_op_type<T_>
110
+
111
+ template <typename CharT> using is_std_char_type = any_of<
112
+ std::is_same<CharT, char>, /* std::string */
113
+ #if defined(PYBIND11_HAS_U8STRING)
114
+ std::is_same<CharT, char8_t>, /* std::u8string */
115
+ #endif
116
+ std::is_same<CharT, char16_t>, /* std::u16string */
117
+ std::is_same<CharT, char32_t>, /* std::u32string */
118
+ std::is_same<CharT, wchar_t> /* std::wstring */
119
+ >;
120
+
121
+
122
+ template <typename T>
123
+ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_type<T>::value>> {
124
+ using _py_type_0 = conditional_t<sizeof(T) <= sizeof(long), long, long long>;
125
+ using _py_type_1 = conditional_t<std::is_signed<T>::value, _py_type_0, typename std::make_unsigned<_py_type_0>::type>;
126
+ using py_type = conditional_t<std::is_floating_point<T>::value, double, _py_type_1>;
127
+ public:
128
+
129
+ bool load(handle src, bool convert) {
130
+ py_type py_value;
131
+
132
+ if (!src)
133
+ return false;
134
+
135
+ #if !defined(PYPY_VERSION)
136
+ auto index_check = [](PyObject *o) { return PyIndex_Check(o); };
137
+ #else
138
+ // In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`,
139
+ // while CPython only considers the existence of `nb_index`/`__index__`.
140
+ auto index_check = [](PyObject *o) { return hasattr(o, "__index__"); };
141
+ #endif
142
+
143
+ if (std::is_floating_point<T>::value) {
144
+ if (convert || PyFloat_Check(src.ptr()))
145
+ py_value = (py_type) PyFloat_AsDouble(src.ptr());
146
+ else
147
+ return false;
148
+ } else if (PyFloat_Check(src.ptr())
149
+ || (!convert && !PYBIND11_LONG_CHECK(src.ptr()) && !index_check(src.ptr()))) {
150
+ return false;
151
+ } else {
152
+ handle src_or_index = src;
153
+ #if PY_VERSION_HEX < 0x03080000
154
+ object index;
155
+ if (!PYBIND11_LONG_CHECK(src.ptr())) { // So: index_check(src.ptr())
156
+ index = reinterpret_steal<object>(PyNumber_Index(src.ptr()));
157
+ if (!index) {
158
+ PyErr_Clear();
159
+ if (!convert)
160
+ return false;
161
+ }
162
+ else {
163
+ src_or_index = index;
164
+ }
165
+ }
166
+ #endif
167
+ if (std::is_unsigned<py_type>::value) {
168
+ py_value = as_unsigned<py_type>(src_or_index.ptr());
169
+ } else { // signed integer:
170
+ py_value = sizeof(T) <= sizeof(long)
171
+ ? (py_type) PyLong_AsLong(src_or_index.ptr())
172
+ : (py_type) PYBIND11_LONG_AS_LONGLONG(src_or_index.ptr());
173
+ }
174
+ }
175
+
176
+ // Python API reported an error
177
+ bool py_err = py_value == (py_type) -1 && PyErr_Occurred();
178
+
179
+ // Check to see if the conversion is valid (integers should match exactly)
180
+ // Signed/unsigned checks happen elsewhere
181
+ if (py_err || (std::is_integral<T>::value && sizeof(py_type) != sizeof(T) && py_value != (py_type) (T) py_value)) {
182
+ PyErr_Clear();
183
+ if (py_err && convert && (PyNumber_Check(src.ptr()) != 0)) {
184
+ auto tmp = reinterpret_steal<object>(std::is_floating_point<T>::value
185
+ ? PyNumber_Float(src.ptr())
186
+ : PyNumber_Long(src.ptr()));
187
+ PyErr_Clear();
188
+ return load(tmp, false);
189
+ }
190
+ return false;
191
+ }
192
+
193
+ value = (T) py_value;
194
+ return true;
195
+ }
196
+
197
+ template<typename U = T>
198
+ static typename std::enable_if<std::is_floating_point<U>::value, handle>::type
199
+ cast(U src, return_value_policy /* policy */, handle /* parent */) {
200
+ return PyFloat_FromDouble((double) src);
201
+ }
202
+
203
+ template<typename U = T>
204
+ static typename std::enable_if<!std::is_floating_point<U>::value && std::is_signed<U>::value && (sizeof(U) <= sizeof(long)), handle>::type
205
+ cast(U src, return_value_policy /* policy */, handle /* parent */) {
206
+ return PYBIND11_LONG_FROM_SIGNED((long) src);
207
+ }
208
+
209
+ template<typename U = T>
210
+ static typename std::enable_if<!std::is_floating_point<U>::value && std::is_unsigned<U>::value && (sizeof(U) <= sizeof(unsigned long)), handle>::type
211
+ cast(U src, return_value_policy /* policy */, handle /* parent */) {
212
+ return PYBIND11_LONG_FROM_UNSIGNED((unsigned long) src);
213
+ }
214
+
215
+ template<typename U = T>
216
+ static typename std::enable_if<!std::is_floating_point<U>::value && std::is_signed<U>::value && (sizeof(U) > sizeof(long)), handle>::type
217
+ cast(U src, return_value_policy /* policy */, handle /* parent */) {
218
+ return PyLong_FromLongLong((long long) src);
219
+ }
220
+
221
+ template<typename U = T>
222
+ static typename std::enable_if<!std::is_floating_point<U>::value && std::is_unsigned<U>::value && (sizeof(U) > sizeof(unsigned long)), handle>::type
223
+ cast(U src, return_value_policy /* policy */, handle /* parent */) {
224
+ return PyLong_FromUnsignedLongLong((unsigned long long) src);
225
+ }
226
+
227
+ PYBIND11_TYPE_CASTER(T, _<std::is_integral<T>::value>("int", "float"));
228
+ };
229
+
230
+ template<typename T> struct void_caster {
231
+ public:
232
+ bool load(handle src, bool) {
233
+ if (src && src.is_none())
234
+ return true;
235
+ return false;
236
+ }
237
+ static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
238
+ return none().inc_ref();
239
+ }
240
+ PYBIND11_TYPE_CASTER(T, _("None"));
241
+ };
242
+
243
+ template <> class type_caster<void_type> : public void_caster<void_type> {};
244
+
245
+ template <> class type_caster<void> : public type_caster<void_type> {
246
+ public:
247
+ using type_caster<void_type>::cast;
248
+
249
+ bool load(handle h, bool) {
250
+ if (!h) {
251
+ return false;
252
+ }
253
+ if (h.is_none()) {
254
+ value = nullptr;
255
+ return true;
256
+ }
257
+
258
+ /* Check if this is a capsule */
259
+ if (isinstance<capsule>(h)) {
260
+ value = reinterpret_borrow<capsule>(h);
261
+ return true;
262
+ }
263
+
264
+ /* Check if this is a C++ type */
265
+ auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
266
+ if (bases.size() == 1) { // Only allowing loading from a single-value type
267
+ value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
268
+ return true;
269
+ }
270
+
271
+ /* Fail */
272
+ return false;
273
+ }
274
+
275
+ static handle cast(const void *ptr, return_value_policy /* policy */, handle /* parent */) {
276
+ if (ptr)
277
+ return capsule(ptr).release();
278
+ return none().inc_ref();
279
+ }
280
+
281
+ template <typename T> using cast_op_type = void*&;
282
+ explicit operator void *&() { return value; }
283
+ static constexpr auto name = _("capsule");
284
+ private:
285
+ void *value = nullptr;
286
+ };
287
+
288
+ template <> class type_caster<std::nullptr_t> : public void_caster<std::nullptr_t> { };
289
+
290
+ template <> class type_caster<bool> {
291
+ public:
292
+ bool load(handle src, bool convert) {
293
+ if (!src) return false;
294
+ if (src.ptr() == Py_True) {
295
+ value = true;
296
+ return true;
297
+ }
298
+ if (src.ptr() == Py_False) {
299
+ value = false;
300
+ return true;
301
+ }
302
+ if (convert || (std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name) == 0)) {
303
+ // (allow non-implicit conversion for numpy booleans)
304
+
305
+ Py_ssize_t res = -1;
306
+ if (src.is_none()) {
307
+ res = 0; // None is implicitly converted to False
308
+ }
309
+ #if defined(PYPY_VERSION)
310
+ // On PyPy, check that "__bool__" (or "__nonzero__" on Python 2.7) attr exists
311
+ else if (hasattr(src, PYBIND11_BOOL_ATTR)) {
312
+ res = PyObject_IsTrue(src.ptr());
313
+ }
314
+ #else
315
+ // Alternate approach for CPython: this does the same as the above, but optimized
316
+ // using the CPython API so as to avoid an unneeded attribute lookup.
317
+ else if (auto tp_as_number = src.ptr()->ob_type->tp_as_number) {
318
+ if (PYBIND11_NB_BOOL(tp_as_number)) {
319
+ res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr());
320
+ }
321
+ }
322
+ #endif
323
+ if (res == 0 || res == 1) {
324
+ value = (res != 0);
325
+ return true;
326
+ }
327
+ PyErr_Clear();
328
+ }
329
+ return false;
330
+ }
331
+ static handle cast(bool src, return_value_policy /* policy */, handle /* parent */) {
332
+ return handle(src ? Py_True : Py_False).inc_ref();
333
+ }
334
+ PYBIND11_TYPE_CASTER(bool, _("bool"));
335
+ };
336
+
337
+ // Helper class for UTF-{8,16,32} C++ stl strings:
338
+ template <typename StringType, bool IsView = false> struct string_caster {
339
+ using CharT = typename StringType::value_type;
340
+
341
+ // Simplify life by being able to assume standard char sizes (the standard only guarantees
342
+ // minimums, but Python requires exact sizes)
343
+ static_assert(!std::is_same<CharT, char>::value || sizeof(CharT) == 1, "Unsupported char size != 1");
344
+ #if defined(PYBIND11_HAS_U8STRING)
345
+ static_assert(!std::is_same<CharT, char8_t>::value || sizeof(CharT) == 1, "Unsupported char8_t size != 1");
346
+ #endif
347
+ static_assert(!std::is_same<CharT, char16_t>::value || sizeof(CharT) == 2, "Unsupported char16_t size != 2");
348
+ static_assert(!std::is_same<CharT, char32_t>::value || sizeof(CharT) == 4, "Unsupported char32_t size != 4");
349
+ // wchar_t can be either 16 bits (Windows) or 32 (everywhere else)
350
+ static_assert(!std::is_same<CharT, wchar_t>::value || sizeof(CharT) == 2 || sizeof(CharT) == 4,
351
+ "Unsupported wchar_t size != 2/4");
352
+ static constexpr size_t UTF_N = 8 * sizeof(CharT);
353
+
354
+ bool load(handle src, bool) {
355
+ #if PY_MAJOR_VERSION < 3
356
+ object temp;
357
+ #endif
358
+ handle load_src = src;
359
+ if (!src) {
360
+ return false;
361
+ }
362
+ if (!PyUnicode_Check(load_src.ptr())) {
363
+ #if PY_MAJOR_VERSION >= 3
364
+ return load_bytes(load_src);
365
+ #else
366
+ if (std::is_same<CharT, char>::value) {
367
+ return load_bytes(load_src);
368
+ }
369
+
370
+ // The below is a guaranteed failure in Python 3 when PyUnicode_Check returns false
371
+ if (!PYBIND11_BYTES_CHECK(load_src.ptr()))
372
+ return false;
373
+
374
+ temp = reinterpret_steal<object>(PyUnicode_FromObject(load_src.ptr()));
375
+ if (!temp) { PyErr_Clear(); return false; }
376
+ load_src = temp;
377
+ #endif
378
+ }
379
+
380
+ #if PY_VERSION_HEX >= 0x03030000
381
+ // On Python >= 3.3, for UTF-8 we avoid the need for a temporary `bytes`
382
+ // object by using `PyUnicode_AsUTF8AndSize`.
383
+ if (PYBIND11_SILENCE_MSVC_C4127(UTF_N == 8)) {
384
+ Py_ssize_t size = -1;
385
+ const auto *buffer
386
+ = reinterpret_cast<const CharT *>(PyUnicode_AsUTF8AndSize(load_src.ptr(), &size));
387
+ if (!buffer) {
388
+ PyErr_Clear();
389
+ return false;
390
+ }
391
+ value = StringType(buffer, static_cast<size_t>(size));
392
+ return true;
393
+ }
394
+ #endif
395
+
396
+ auto utfNbytes = reinterpret_steal<object>(PyUnicode_AsEncodedString(
397
+ load_src.ptr(), UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr));
398
+ if (!utfNbytes) { PyErr_Clear(); return false; }
399
+
400
+ const auto *buffer = reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
401
+ size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT);
402
+ // Skip BOM for UTF-16/32
403
+ if (PYBIND11_SILENCE_MSVC_C4127(UTF_N > 8)) {
404
+ buffer++;
405
+ length--;
406
+ }
407
+ value = StringType(buffer, length);
408
+
409
+ // If we're loading a string_view we need to keep the encoded Python object alive:
410
+ if (IsView)
411
+ loader_life_support::add_patient(utfNbytes);
412
+
413
+ return true;
414
+ }
415
+
416
+ static handle cast(const StringType &src, return_value_policy /* policy */, handle /* parent */) {
417
+ const char *buffer = reinterpret_cast<const char *>(src.data());
418
+ auto nbytes = ssize_t(src.size() * sizeof(CharT));
419
+ handle s = decode_utfN(buffer, nbytes);
420
+ if (!s) throw error_already_set();
421
+ return s;
422
+ }
423
+
424
+ PYBIND11_TYPE_CASTER(StringType, _(PYBIND11_STRING_NAME));
425
+
426
+ private:
427
+ static handle decode_utfN(const char *buffer, ssize_t nbytes) {
428
+ #if !defined(PYPY_VERSION)
429
+ return
430
+ UTF_N == 8 ? PyUnicode_DecodeUTF8(buffer, nbytes, nullptr) :
431
+ UTF_N == 16 ? PyUnicode_DecodeUTF16(buffer, nbytes, nullptr, nullptr) :
432
+ PyUnicode_DecodeUTF32(buffer, nbytes, nullptr, nullptr);
433
+ #else
434
+ // PyPy segfaults when on PyUnicode_DecodeUTF16 (and possibly on PyUnicode_DecodeUTF32 as well),
435
+ // so bypass the whole thing by just passing the encoding as a string value, which works properly:
436
+ return PyUnicode_Decode(buffer, nbytes, UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr);
437
+ #endif
438
+ }
439
+
440
+ // When loading into a std::string or char*, accept a bytes object as-is (i.e.
441
+ // without any encoding/decoding attempt). For other C++ char sizes this is a no-op.
442
+ // which supports loading a unicode from a str, doesn't take this path.
443
+ template <typename C = CharT>
444
+ bool load_bytes(enable_if_t<std::is_same<C, char>::value, handle> src) {
445
+ if (PYBIND11_BYTES_CHECK(src.ptr())) {
446
+ // We were passed a Python 3 raw bytes; accept it into a std::string or char*
447
+ // without any encoding attempt.
448
+ const char *bytes = PYBIND11_BYTES_AS_STRING(src.ptr());
449
+ if (bytes) {
450
+ value = StringType(bytes, (size_t) PYBIND11_BYTES_SIZE(src.ptr()));
451
+ return true;
452
+ }
453
+ }
454
+
455
+ return false;
456
+ }
457
+
458
+ template <typename C = CharT>
459
+ bool load_bytes(enable_if_t<!std::is_same<C, char>::value, handle>) { return false; }
460
+ };
461
+
462
+ template <typename CharT, class Traits, class Allocator>
463
+ struct type_caster<std::basic_string<CharT, Traits, Allocator>, enable_if_t<is_std_char_type<CharT>::value>>
464
+ : string_caster<std::basic_string<CharT, Traits, Allocator>> {};
465
+
466
+ #ifdef PYBIND11_HAS_STRING_VIEW
467
+ template <typename CharT, class Traits>
468
+ struct type_caster<std::basic_string_view<CharT, Traits>, enable_if_t<is_std_char_type<CharT>::value>>
469
+ : string_caster<std::basic_string_view<CharT, Traits>, true> {};
470
+ #endif
471
+
472
+ // Type caster for C-style strings. We basically use a std::string type caster, but also add the
473
+ // ability to use None as a nullptr char* (which the string caster doesn't allow).
474
+ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type<CharT>::value>> {
475
+ using StringType = std::basic_string<CharT>;
476
+ using StringCaster = type_caster<StringType>;
477
+ StringCaster str_caster;
478
+ bool none = false;
479
+ CharT one_char = 0;
480
+ public:
481
+ bool load(handle src, bool convert) {
482
+ if (!src) return false;
483
+ if (src.is_none()) {
484
+ // Defer accepting None to other overloads (if we aren't in convert mode):
485
+ if (!convert) return false;
486
+ none = true;
487
+ return true;
488
+ }
489
+ return str_caster.load(src, convert);
490
+ }
491
+
492
+ static handle cast(const CharT *src, return_value_policy policy, handle parent) {
493
+ if (src == nullptr) return pybind11::none().inc_ref();
494
+ return StringCaster::cast(StringType(src), policy, parent);
495
+ }
496
+
497
+ static handle cast(CharT src, return_value_policy policy, handle parent) {
498
+ if (std::is_same<char, CharT>::value) {
499
+ handle s = PyUnicode_DecodeLatin1((const char *) &src, 1, nullptr);
500
+ if (!s) throw error_already_set();
501
+ return s;
502
+ }
503
+ return StringCaster::cast(StringType(1, src), policy, parent);
504
+ }
505
+
506
+ explicit operator CharT *() {
507
+ return none ? nullptr : const_cast<CharT *>(static_cast<StringType &>(str_caster).c_str());
508
+ }
509
+ explicit operator CharT &() {
510
+ if (none)
511
+ throw value_error("Cannot convert None to a character");
512
+
513
+ auto &value = static_cast<StringType &>(str_caster);
514
+ size_t str_len = value.size();
515
+ if (str_len == 0)
516
+ throw value_error("Cannot convert empty string to a character");
517
+
518
+ // If we're in UTF-8 mode, we have two possible failures: one for a unicode character that
519
+ // is too high, and one for multiple unicode characters (caught later), so we need to figure
520
+ // out how long the first encoded character is in bytes to distinguish between these two
521
+ // errors. We also allow want to allow unicode characters U+0080 through U+00FF, as those
522
+ // can fit into a single char value.
523
+ if (PYBIND11_SILENCE_MSVC_C4127(StringCaster::UTF_N == 8) && str_len > 1 && str_len <= 4) {
524
+ auto v0 = static_cast<unsigned char>(value[0]);
525
+ // low bits only: 0-127
526
+ // 0b110xxxxx - start of 2-byte sequence
527
+ // 0b1110xxxx - start of 3-byte sequence
528
+ // 0b11110xxx - start of 4-byte sequence
529
+ size_t char0_bytes = (v0 & 0x80) == 0 ? 1
530
+ : (v0 & 0xE0) == 0xC0 ? 2
531
+ : (v0 & 0xF0) == 0xE0 ? 3
532
+ : 4;
533
+
534
+ if (char0_bytes == str_len) {
535
+ // If we have a 128-255 value, we can decode it into a single char:
536
+ if (char0_bytes == 2 && (v0 & 0xFC) == 0xC0) { // 0x110000xx 0x10xxxxxx
537
+ one_char = static_cast<CharT>(((v0 & 3) << 6) + (static_cast<unsigned char>(value[1]) & 0x3F));
538
+ return one_char;
539
+ }
540
+ // Otherwise we have a single character, but it's > U+00FF
541
+ throw value_error("Character code point not in range(0x100)");
542
+ }
543
+ }
544
+
545
+ // UTF-16 is much easier: we can only have a surrogate pair for values above U+FFFF, thus a
546
+ // surrogate pair with total length 2 instantly indicates a range error (but not a "your
547
+ // string was too long" error).
548
+ else if (PYBIND11_SILENCE_MSVC_C4127(StringCaster::UTF_N == 16) && str_len == 2) {
549
+ one_char = static_cast<CharT>(value[0]);
550
+ if (one_char >= 0xD800 && one_char < 0xE000)
551
+ throw value_error("Character code point not in range(0x10000)");
552
+ }
553
+
554
+ if (str_len != 1)
555
+ throw value_error("Expected a character, but multi-character string found");
556
+
557
+ one_char = value[0];
558
+ return one_char;
559
+ }
560
+
561
+ static constexpr auto name = _(PYBIND11_STRING_NAME);
562
+ template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
563
+ };
564
+
565
+ // Base implementation for std::tuple and std::pair
566
+ template <template<typename...> class Tuple, typename... Ts> class tuple_caster {
567
+ using type = Tuple<Ts...>;
568
+ static constexpr auto size = sizeof...(Ts);
569
+ using indices = make_index_sequence<size>;
570
+ public:
571
+
572
+ bool load(handle src, bool convert) {
573
+ if (!isinstance<sequence>(src))
574
+ return false;
575
+ const auto seq = reinterpret_borrow<sequence>(src);
576
+ if (seq.size() != size)
577
+ return false;
578
+ return load_impl(seq, convert, indices{});
579
+ }
580
+
581
+ template <typename T>
582
+ static handle cast(T &&src, return_value_policy policy, handle parent) {
583
+ return cast_impl(std::forward<T>(src), policy, parent, indices{});
584
+ }
585
+
586
+ // copied from the PYBIND11_TYPE_CASTER macro
587
+ template <typename T>
588
+ static handle cast(T *src, return_value_policy policy, handle parent) {
589
+ if (!src) return none().release();
590
+ if (policy == return_value_policy::take_ownership) {
591
+ auto h = cast(std::move(*src), policy, parent);
592
+ delete src;
593
+ return h;
594
+ }
595
+ return cast(*src, policy, parent);
596
+ }
597
+
598
+ static constexpr auto name = _("Tuple[") + concat(make_caster<Ts>::name...) + _("]");
599
+
600
+ template <typename T> using cast_op_type = type;
601
+
602
+ explicit operator type() & { return implicit_cast(indices{}); }
603
+ explicit operator type() && { return std::move(*this).implicit_cast(indices{}); }
604
+
605
+ protected:
606
+ template <size_t... Is>
607
+ type implicit_cast(index_sequence<Is...>) & { return type(cast_op<Ts>(std::get<Is>(subcasters))...); }
608
+ template <size_t... Is>
609
+ type implicit_cast(index_sequence<Is...>) && { return type(cast_op<Ts>(std::move(std::get<Is>(subcasters)))...); }
610
+
611
+ static constexpr bool load_impl(const sequence &, bool, index_sequence<>) { return true; }
612
+
613
+ template <size_t... Is>
614
+ bool load_impl(const sequence &seq, bool convert, index_sequence<Is...>) {
615
+ #ifdef __cpp_fold_expressions
616
+ if ((... || !std::get<Is>(subcasters).load(seq[Is], convert)))
617
+ return false;
618
+ #else
619
+ for (bool r : {std::get<Is>(subcasters).load(seq[Is], convert)...})
620
+ if (!r)
621
+ return false;
622
+ #endif
623
+ return true;
624
+ }
625
+
626
+ /* Implementation: Convert a C++ tuple into a Python tuple */
627
+ template <typename T, size_t... Is>
628
+ static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence<Is...>) {
629
+ PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent);
630
+ PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(policy, parent);
631
+ std::array<object, size> entries{{
632
+ reinterpret_steal<object>(make_caster<Ts>::cast(std::get<Is>(std::forward<T>(src)), policy, parent))...
633
+ }};
634
+ for (const auto &entry: entries)
635
+ if (!entry)
636
+ return handle();
637
+ tuple result(size);
638
+ int counter = 0;
639
+ for (auto & entry: entries)
640
+ PyTuple_SET_ITEM(result.ptr(), counter++, entry.release().ptr());
641
+ return result.release();
642
+ }
643
+
644
+ Tuple<make_caster<Ts>...> subcasters;
645
+ };
646
+
647
+ template <typename T1, typename T2> class type_caster<std::pair<T1, T2>>
648
+ : public tuple_caster<std::pair, T1, T2> {};
649
+
650
+ template <typename... Ts> class type_caster<std::tuple<Ts...>>
651
+ : public tuple_caster<std::tuple, Ts...> {};
652
+
653
+ /// Helper class which abstracts away certain actions. Users can provide specializations for
654
+ /// custom holders, but it's only necessary if the type has a non-standard interface.
655
+ template <typename T>
656
+ struct holder_helper {
657
+ static auto get(const T &p) -> decltype(p.get()) { return p.get(); }
658
+ };
659
+
660
+ /// Type caster for holder types like std::shared_ptr, etc.
661
+ /// The SFINAE hook is provided to help work around the current lack of support
662
+ /// for smart-pointer interoperability. Please consider it an implementation
663
+ /// detail that may change in the future, as formal support for smart-pointer
664
+ /// interoperability is added into pybind11.
665
+ template <typename type, typename holder_type, typename SFINAE = void>
666
+ struct copyable_holder_caster : public type_caster_base<type> {
667
+ public:
668
+ using base = type_caster_base<type>;
669
+ static_assert(std::is_base_of<base, type_caster<type>>::value,
670
+ "Holder classes are only supported for custom types");
671
+ using base::base;
672
+ using base::cast;
673
+ using base::typeinfo;
674
+ using base::value;
675
+
676
+ bool load(handle src, bool convert) {
677
+ return base::template load_impl<copyable_holder_caster<type, holder_type>>(src, convert);
678
+ }
679
+
680
+ explicit operator type*() { return this->value; }
681
+ // static_cast works around compiler error with MSVC 17 and CUDA 10.2
682
+ // see issue #2180
683
+ explicit operator type&() { return *(static_cast<type *>(this->value)); }
684
+ explicit operator holder_type*() { return std::addressof(holder); }
685
+ explicit operator holder_type&() { return holder; }
686
+
687
+ static handle cast(const holder_type &src, return_value_policy, handle) {
688
+ const auto *ptr = holder_helper<holder_type>::get(src);
689
+ return type_caster_base<type>::cast_holder(ptr, &src);
690
+ }
691
+
692
+ protected:
693
+ friend class type_caster_generic;
694
+ void check_holder_compat() {
695
+ if (typeinfo->default_holder)
696
+ throw cast_error("Unable to load a custom holder type from a default-holder instance");
697
+ }
698
+
699
+ bool load_value(value_and_holder &&v_h) {
700
+ if (v_h.holder_constructed()) {
701
+ value = v_h.value_ptr();
702
+ holder = v_h.template holder<holder_type>();
703
+ return true;
704
+ }
705
+ throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
706
+ #if defined(NDEBUG)
707
+ "(compile in debug mode for type information)");
708
+ #else
709
+ "of type '"
710
+ + type_id<holder_type>() + "''");
711
+ #endif
712
+ }
713
+
714
+ template <typename T = holder_type, detail::enable_if_t<!std::is_constructible<T, const T &, type*>::value, int> = 0>
715
+ bool try_implicit_casts(handle, bool) { return false; }
716
+
717
+ template <typename T = holder_type, detail::enable_if_t<std::is_constructible<T, const T &, type*>::value, int> = 0>
718
+ bool try_implicit_casts(handle src, bool convert) {
719
+ for (auto &cast : typeinfo->implicit_casts) {
720
+ copyable_holder_caster sub_caster(*cast.first);
721
+ if (sub_caster.load(src, convert)) {
722
+ value = cast.second(sub_caster.value);
723
+ holder = holder_type(sub_caster.holder, (type *) value);
724
+ return true;
725
+ }
726
+ }
727
+ return false;
728
+ }
729
+
730
+ static bool try_direct_conversions(handle) { return false; }
731
+
732
+
733
+ holder_type holder;
734
+ };
735
+
736
+ /// Specialize for the common std::shared_ptr, so users don't need to
737
+ template <typename T>
738
+ class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::shared_ptr<T>> { };
739
+
740
+ /// Type caster for holder types like std::unique_ptr.
741
+ /// Please consider the SFINAE hook an implementation detail, as explained
742
+ /// in the comment for the copyable_holder_caster.
743
+ template <typename type, typename holder_type, typename SFINAE = void>
744
+ struct move_only_holder_caster {
745
+ static_assert(std::is_base_of<type_caster_base<type>, type_caster<type>>::value,
746
+ "Holder classes are only supported for custom types");
747
+
748
+ static handle cast(holder_type &&src, return_value_policy, handle) {
749
+ auto *ptr = holder_helper<holder_type>::get(src);
750
+ return type_caster_base<type>::cast_holder(ptr, std::addressof(src));
751
+ }
752
+ static constexpr auto name = type_caster_base<type>::name;
753
+ };
754
+
755
+ template <typename type, typename deleter>
756
+ class type_caster<std::unique_ptr<type, deleter>>
757
+ : public move_only_holder_caster<type, std::unique_ptr<type, deleter>> { };
758
+
759
+ template <typename type, typename holder_type>
760
+ using type_caster_holder = conditional_t<is_copy_constructible<holder_type>::value,
761
+ copyable_holder_caster<type, holder_type>,
762
+ move_only_holder_caster<type, holder_type>>;
763
+
764
+ template <typename T, bool Value = false> struct always_construct_holder { static constexpr bool value = Value; };
765
+
766
+ /// Create a specialization for custom holder types (silently ignores std::shared_ptr)
767
+ #define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type, ...) \
768
+ namespace pybind11 { namespace detail { \
769
+ template <typename type> \
770
+ struct always_construct_holder<holder_type> : always_construct_holder<void, ##__VA_ARGS__> { }; \
771
+ template <typename type> \
772
+ class type_caster<holder_type, enable_if_t<!is_shared_ptr<holder_type>::value>> \
773
+ : public type_caster_holder<type, holder_type> { }; \
774
+ }}
775
+
776
+ // PYBIND11_DECLARE_HOLDER_TYPE holder types:
777
+ template <typename base, typename holder> struct is_holder_type :
778
+ std::is_base_of<detail::type_caster_holder<base, holder>, detail::type_caster<holder>> {};
779
+ // Specialization for always-supported unique_ptr holders:
780
+ template <typename base, typename deleter> struct is_holder_type<base, std::unique_ptr<base, deleter>> :
781
+ std::true_type {};
782
+
783
+ template <typename T> struct handle_type_name { static constexpr auto name = _<T>(); };
784
+ template <> struct handle_type_name<bytes> { static constexpr auto name = _(PYBIND11_BYTES_NAME); };
785
+ template <> struct handle_type_name<int_> { static constexpr auto name = _("int"); };
786
+ template <> struct handle_type_name<iterable> { static constexpr auto name = _("Iterable"); };
787
+ template <> struct handle_type_name<iterator> { static constexpr auto name = _("Iterator"); };
788
+ template <> struct handle_type_name<none> { static constexpr auto name = _("None"); };
789
+ template <> struct handle_type_name<args> { static constexpr auto name = _("*args"); };
790
+ template <> struct handle_type_name<kwargs> { static constexpr auto name = _("**kwargs"); };
791
+
792
+ template <typename type>
793
+ struct pyobject_caster {
794
+ template <typename T = type, enable_if_t<std::is_same<T, handle>::value, int> = 0>
795
+ bool load(handle src, bool /* convert */) { value = src; return static_cast<bool>(value); }
796
+
797
+ template <typename T = type, enable_if_t<std::is_base_of<object, T>::value, int> = 0>
798
+ bool load(handle src, bool /* convert */) {
799
+ #if PY_MAJOR_VERSION < 3 && !defined(PYBIND11_STR_LEGACY_PERMISSIVE)
800
+ // For Python 2, without this implicit conversion, Python code would
801
+ // need to be cluttered with six.ensure_text() or similar, only to be
802
+ // un-cluttered later after Python 2 support is dropped.
803
+ if (PYBIND11_SILENCE_MSVC_C4127(std::is_same<T, str>::value) && isinstance<bytes>(src)) {
804
+ PyObject *str_from_bytes = PyUnicode_FromEncodedObject(src.ptr(), "utf-8", nullptr);
805
+ if (!str_from_bytes) throw error_already_set();
806
+ value = reinterpret_steal<type>(str_from_bytes);
807
+ return true;
808
+ }
809
+ #endif
810
+ if (!isinstance<type>(src))
811
+ return false;
812
+ value = reinterpret_borrow<type>(src);
813
+ return true;
814
+ }
815
+
816
+ static handle cast(const handle &src, return_value_policy /* policy */, handle /* parent */) {
817
+ return src.inc_ref();
818
+ }
819
+ PYBIND11_TYPE_CASTER(type, handle_type_name<type>::name);
820
+ };
821
+
822
+ template <typename T>
823
+ class type_caster<T, enable_if_t<is_pyobject<T>::value>> : public pyobject_caster<T> { };
824
+
825
+ // Our conditions for enabling moving are quite restrictive:
826
+ // At compile time:
827
+ // - T needs to be a non-const, non-pointer, non-reference type
828
+ // - type_caster<T>::operator T&() must exist
829
+ // - the type must be move constructible (obviously)
830
+ // At run-time:
831
+ // - if the type is non-copy-constructible, the object must be the sole owner of the type (i.e. it
832
+ // must have ref_count() == 1)h
833
+ // If any of the above are not satisfied, we fall back to copying.
834
+ template <typename T> using move_is_plain_type = satisfies_none_of<T,
835
+ std::is_void, std::is_pointer, std::is_reference, std::is_const
836
+ >;
837
+ template <typename T, typename SFINAE = void> struct move_always : std::false_type {};
838
+ template <typename T> struct move_always<T, enable_if_t<all_of<
839
+ move_is_plain_type<T>,
840
+ negation<is_copy_constructible<T>>,
841
+ std::is_move_constructible<T>,
842
+ std::is_same<decltype(std::declval<make_caster<T>>().operator T&()), T&>
843
+ >::value>> : std::true_type {};
844
+ template <typename T, typename SFINAE = void> struct move_if_unreferenced : std::false_type {};
845
+ template <typename T> struct move_if_unreferenced<T, enable_if_t<all_of<
846
+ move_is_plain_type<T>,
847
+ negation<move_always<T>>,
848
+ std::is_move_constructible<T>,
849
+ std::is_same<decltype(std::declval<make_caster<T>>().operator T&()), T&>
850
+ >::value>> : std::true_type {};
851
+ template <typename T> using move_never = none_of<move_always<T>, move_if_unreferenced<T>>;
852
+
853
+ // Detect whether returning a `type` from a cast on type's type_caster is going to result in a
854
+ // reference or pointer to a local variable of the type_caster. Basically, only
855
+ // non-reference/pointer `type`s and reference/pointers from a type_caster_generic are safe;
856
+ // everything else returns a reference/pointer to a local variable.
857
+ template <typename type> using cast_is_temporary_value_reference = bool_constant<
858
+ (std::is_reference<type>::value || std::is_pointer<type>::value) &&
859
+ !std::is_base_of<type_caster_generic, make_caster<type>>::value &&
860
+ !std::is_same<intrinsic_t<type>, void>::value
861
+ >;
862
+
863
+ // When a value returned from a C++ function is being cast back to Python, we almost always want to
864
+ // force `policy = move`, regardless of the return value policy the function/method was declared
865
+ // with.
866
+ template <typename Return, typename SFINAE = void> struct return_value_policy_override {
867
+ static return_value_policy policy(return_value_policy p) { return p; }
868
+ };
869
+
870
+ template <typename Return> struct return_value_policy_override<Return,
871
+ detail::enable_if_t<std::is_base_of<type_caster_generic, make_caster<Return>>::value, void>> {
872
+ static return_value_policy policy(return_value_policy p) {
873
+ return !std::is_lvalue_reference<Return>::value &&
874
+ !std::is_pointer<Return>::value
875
+ ? return_value_policy::move : p;
876
+ }
877
+ };
878
+
879
+ // Basic python -> C++ casting; throws if casting fails
880
+ template <typename T, typename SFINAE> type_caster<T, SFINAE> &load_type(type_caster<T, SFINAE> &conv, const handle &handle) {
881
+ if (!conv.load(handle, true)) {
882
+ #if defined(NDEBUG)
883
+ throw cast_error("Unable to cast Python instance to C++ type (compile in debug mode for details)");
884
+ #else
885
+ throw cast_error("Unable to cast Python instance of type " +
886
+ (std::string) str(type::handle_of(handle)) + " to C++ type '" + type_id<T>() + "'");
887
+ #endif
888
+ }
889
+ return conv;
890
+ }
891
+ // Wrapper around the above that also constructs and returns a type_caster
892
+ template <typename T> make_caster<T> load_type(const handle &handle) {
893
+ make_caster<T> conv;
894
+ load_type(conv, handle);
895
+ return conv;
896
+ }
897
+
898
+ PYBIND11_NAMESPACE_END(detail)
899
+
900
+ // pytype -> C++ type
901
+ template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0>
902
+ T cast(const handle &handle) {
903
+ using namespace detail;
904
+ static_assert(!cast_is_temporary_value_reference<T>::value,
905
+ "Unable to cast type to reference: value is local to type caster");
906
+ return cast_op<T>(load_type<T>(handle));
907
+ }
908
+
909
+ // pytype -> pytype (calls converting constructor)
910
+ template <typename T, detail::enable_if_t<detail::is_pyobject<T>::value, int> = 0>
911
+ T cast(const handle &handle) { return T(reinterpret_borrow<object>(handle)); }
912
+
913
+ // C++ type -> py::object
914
+ template <typename T, detail::enable_if_t<!detail::is_pyobject<T>::value, int> = 0>
915
+ object cast(T &&value, return_value_policy policy = return_value_policy::automatic_reference,
916
+ handle parent = handle()) {
917
+ using no_ref_T = typename std::remove_reference<T>::type;
918
+ if (policy == return_value_policy::automatic)
919
+ policy = std::is_pointer<no_ref_T>::value ? return_value_policy::take_ownership :
920
+ std::is_lvalue_reference<T>::value ? return_value_policy::copy : return_value_policy::move;
921
+ else if (policy == return_value_policy::automatic_reference)
922
+ policy = std::is_pointer<no_ref_T>::value ? return_value_policy::reference :
923
+ std::is_lvalue_reference<T>::value ? return_value_policy::copy : return_value_policy::move;
924
+ return reinterpret_steal<object>(detail::make_caster<T>::cast(std::forward<T>(value), policy, parent));
925
+ }
926
+
927
+ template <typename T> T handle::cast() const { return pybind11::cast<T>(*this); }
928
+ template <> inline void handle::cast() const { return; }
929
+
930
+ template <typename T>
931
+ detail::enable_if_t<!detail::move_never<T>::value, T> move(object &&obj) {
932
+ if (obj.ref_count() > 1)
933
+ #if defined(NDEBUG)
934
+ throw cast_error("Unable to cast Python instance to C++ rvalue: instance has multiple references"
935
+ " (compile in debug mode for details)");
936
+ #else
937
+ throw cast_error("Unable to move from Python " + (std::string) str(type::handle_of(obj)) +
938
+ " instance to C++ " + type_id<T>() + " instance: instance has multiple references");
939
+ #endif
940
+
941
+ // Move into a temporary and return that, because the reference may be a local value of `conv`
942
+ T ret = std::move(detail::load_type<T>(obj).operator T&());
943
+ return ret;
944
+ }
945
+
946
+ // Calling cast() on an rvalue calls pybind11::cast with the object rvalue, which does:
947
+ // - If we have to move (because T has no copy constructor), do it. This will fail if the moved
948
+ // object has multiple references, but trying to copy will fail to compile.
949
+ // - If both movable and copyable, check ref count: if 1, move; otherwise copy
950
+ // - Otherwise (not movable), copy.
951
+ template <typename T> detail::enable_if_t<detail::move_always<T>::value, T> cast(object &&object) {
952
+ return move<T>(std::move(object));
953
+ }
954
+ template <typename T> detail::enable_if_t<detail::move_if_unreferenced<T>::value, T> cast(object &&object) {
955
+ if (object.ref_count() > 1)
956
+ return cast<T>(object);
957
+ return move<T>(std::move(object));
958
+ }
959
+ template <typename T> detail::enable_if_t<detail::move_never<T>::value, T> cast(object &&object) {
960
+ return cast<T>(object);
961
+ }
962
+
963
+ template <typename T> T object::cast() const & { return pybind11::cast<T>(*this); }
964
+ template <typename T> T object::cast() && { return pybind11::cast<T>(std::move(*this)); }
965
+ template <> inline void object::cast() const & { return; }
966
+ template <> inline void object::cast() && { return; }
967
+
968
+ PYBIND11_NAMESPACE_BEGIN(detail)
969
+
970
+ // Declared in pytypes.h:
971
+ template <typename T, enable_if_t<!is_pyobject<T>::value, int>>
972
+ object object_or_cast(T &&o) { return pybind11::cast(std::forward<T>(o)); }
973
+
974
+ struct override_unused {}; // Placeholder type for the unneeded (and dead code) static variable in the PYBIND11_OVERRIDE_OVERRIDE macro
975
+ template <typename ret_type> using override_caster_t = conditional_t<
976
+ cast_is_temporary_value_reference<ret_type>::value, make_caster<ret_type>, override_unused>;
977
+
978
+ // Trampoline use: for reference/pointer types to value-converted values, we do a value cast, then
979
+ // store the result in the given variable. For other types, this is a no-op.
980
+ template <typename T> enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&o, make_caster<T> &caster) {
981
+ return cast_op<T>(load_type(caster, o));
982
+ }
983
+ template <typename T> enable_if_t<!cast_is_temporary_value_reference<T>::value, T> cast_ref(object &&, override_unused &) {
984
+ pybind11_fail("Internal error: cast_ref fallback invoked"); }
985
+
986
+ // Trampoline use: Having a pybind11::cast with an invalid reference type is going to static_assert, even
987
+ // though if it's in dead code, so we provide a "trampoline" to pybind11::cast that only does anything in
988
+ // cases where pybind11::cast is valid.
989
+ template <typename T> enable_if_t<!cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&o) {
990
+ return pybind11::cast<T>(std::move(o)); }
991
+ template <typename T> enable_if_t<cast_is_temporary_value_reference<T>::value, T> cast_safe(object &&) {
992
+ pybind11_fail("Internal error: cast_safe fallback invoked"); }
993
+ template <> inline void cast_safe<void>(object &&) {}
994
+
995
+ PYBIND11_NAMESPACE_END(detail)
996
+
997
+ // The overloads could coexist, i.e. the #if is not strictly speaking needed,
998
+ // but it is an easy minor optimization.
999
+ #if defined(NDEBUG)
1000
+ inline cast_error cast_error_unable_to_convert_call_arg() {
1001
+ return cast_error(
1002
+ "Unable to convert call argument to Python object (compile in debug mode for details)");
1003
+ }
1004
+ #else
1005
+ inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name,
1006
+ const std::string &type) {
1007
+ return cast_error("Unable to convert call argument '" + name + "' of type '" + type
1008
+ + "' to Python object");
1009
+ }
1010
+ #endif
1011
+
1012
+ template <return_value_policy policy = return_value_policy::automatic_reference>
1013
+ tuple make_tuple() { return tuple(0); }
1014
+
1015
+ template <return_value_policy policy = return_value_policy::automatic_reference,
1016
+ typename... Args> tuple make_tuple(Args&&... args_) {
1017
+ constexpr size_t size = sizeof...(Args);
1018
+ std::array<object, size> args {
1019
+ { reinterpret_steal<object>(detail::make_caster<Args>::cast(
1020
+ std::forward<Args>(args_), policy, nullptr))... }
1021
+ };
1022
+ for (size_t i = 0; i < args.size(); i++) {
1023
+ if (!args[i]) {
1024
+ #if defined(NDEBUG)
1025
+ throw cast_error_unable_to_convert_call_arg();
1026
+ #else
1027
+ std::array<std::string, size> argtypes { {type_id<Args>()...} };
1028
+ throw cast_error_unable_to_convert_call_arg(std::to_string(i), argtypes[i]);
1029
+ #endif
1030
+ }
1031
+ }
1032
+ tuple result(size);
1033
+ int counter = 0;
1034
+ for (auto &arg_value : args)
1035
+ PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr());
1036
+ return result;
1037
+ }
1038
+
1039
+ /// \ingroup annotations
1040
+ /// Annotation for arguments
1041
+ struct arg {
1042
+ /// Constructs an argument with the name of the argument; if null or omitted, this is a positional argument.
1043
+ constexpr explicit arg(const char *name = nullptr) : name(name), flag_noconvert(false), flag_none(true) { }
1044
+ /// Assign a value to this argument
1045
+ template <typename T> arg_v operator=(T &&value) const;
1046
+ /// Indicate that the type should not be converted in the type caster
1047
+ arg &noconvert(bool flag = true) { flag_noconvert = flag; return *this; }
1048
+ /// Indicates that the argument should/shouldn't allow None (e.g. for nullable pointer args)
1049
+ arg &none(bool flag = true) { flag_none = flag; return *this; }
1050
+
1051
+ const char *name; ///< If non-null, this is a named kwargs argument
1052
+ bool flag_noconvert : 1; ///< If set, do not allow conversion (requires a supporting type caster!)
1053
+ bool flag_none : 1; ///< If set (the default), allow None to be passed to this argument
1054
+ };
1055
+
1056
+ /// \ingroup annotations
1057
+ /// Annotation for arguments with values
1058
+ struct arg_v : arg {
1059
+ private:
1060
+ template <typename T>
1061
+ arg_v(arg &&base, T &&x, const char *descr = nullptr)
1062
+ : arg(base),
1063
+ value(reinterpret_steal<object>(
1064
+ detail::make_caster<T>::cast(x, return_value_policy::automatic, {})
1065
+ )),
1066
+ descr(descr)
1067
+ #if !defined(NDEBUG)
1068
+ , type(type_id<T>())
1069
+ #endif
1070
+ {
1071
+ // Workaround! See:
1072
+ // https://github.com/pybind/pybind11/issues/2336
1073
+ // https://github.com/pybind/pybind11/pull/2685#issuecomment-731286700
1074
+ if (PyErr_Occurred()) {
1075
+ PyErr_Clear();
1076
+ }
1077
+ }
1078
+
1079
+ public:
1080
+ /// Direct construction with name, default, and description
1081
+ template <typename T>
1082
+ arg_v(const char *name, T &&x, const char *descr = nullptr)
1083
+ : arg_v(arg(name), std::forward<T>(x), descr) { }
1084
+
1085
+ /// Called internally when invoking `py::arg("a") = value`
1086
+ template <typename T>
1087
+ arg_v(const arg &base, T &&x, const char *descr = nullptr)
1088
+ : arg_v(arg(base), std::forward<T>(x), descr) { }
1089
+
1090
+ /// Same as `arg::noconvert()`, but returns *this as arg_v&, not arg&
1091
+ arg_v &noconvert(bool flag = true) { arg::noconvert(flag); return *this; }
1092
+
1093
+ /// Same as `arg::nonone()`, but returns *this as arg_v&, not arg&
1094
+ arg_v &none(bool flag = true) { arg::none(flag); return *this; }
1095
+
1096
+ /// The default value
1097
+ object value;
1098
+ /// The (optional) description of the default value
1099
+ const char *descr;
1100
+ #if !defined(NDEBUG)
1101
+ /// The C++ type name of the default value (only available when compiled in debug mode)
1102
+ std::string type;
1103
+ #endif
1104
+ };
1105
+
1106
+ /// \ingroup annotations
1107
+ /// Annotation indicating that all following arguments are keyword-only; the is the equivalent of an
1108
+ /// unnamed '*' argument (in Python 3)
1109
+ struct kw_only {};
1110
+
1111
+ /// \ingroup annotations
1112
+ /// Annotation indicating that all previous arguments are positional-only; the is the equivalent of an
1113
+ /// unnamed '/' argument (in Python 3.8)
1114
+ struct pos_only {};
1115
+
1116
+ template <typename T>
1117
+ arg_v arg::operator=(T &&value) const {
1118
+ return {*this, std::forward<T>(value)};
1119
+ }
1120
+
1121
+ /// Alias for backward compatibility -- to be removed in version 2.0
1122
+ template <typename /*unused*/> using arg_t = arg_v;
1123
+
1124
+ inline namespace literals {
1125
+ /** \rst
1126
+ String literal version of `arg`
1127
+ \endrst */
1128
+ constexpr arg operator"" _a(const char *name, size_t) { return arg(name); }
1129
+ } // namespace literals
1130
+
1131
+ PYBIND11_NAMESPACE_BEGIN(detail)
1132
+
1133
+ // forward declaration (definition in attr.h)
1134
+ struct function_record;
1135
+
1136
+ /// Internal data associated with a single function call
1137
+ struct function_call {
1138
+ function_call(const function_record &f, handle p); // Implementation in attr.h
1139
+
1140
+ /// The function data:
1141
+ const function_record &func;
1142
+
1143
+ /// Arguments passed to the function:
1144
+ std::vector<handle> args;
1145
+
1146
+ /// The `convert` value the arguments should be loaded with
1147
+ std::vector<bool> args_convert;
1148
+
1149
+ /// Extra references for the optional `py::args` and/or `py::kwargs` arguments (which, if
1150
+ /// present, are also in `args` but without a reference).
1151
+ object args_ref, kwargs_ref;
1152
+
1153
+ /// The parent, if any
1154
+ handle parent;
1155
+
1156
+ /// If this is a call to an initializer, this argument contains `self`
1157
+ handle init_self;
1158
+ };
1159
+
1160
+
1161
+ /// Helper class which loads arguments for C++ functions called from Python
1162
+ template <typename... Args>
1163
+ class argument_loader {
1164
+ using indices = make_index_sequence<sizeof...(Args)>;
1165
+
1166
+ template <typename Arg> using argument_is_args = std::is_same<intrinsic_t<Arg>, args>;
1167
+ template <typename Arg> using argument_is_kwargs = std::is_same<intrinsic_t<Arg>, kwargs>;
1168
+ // Get args/kwargs argument positions relative to the end of the argument list:
1169
+ static constexpr auto args_pos = constexpr_first<argument_is_args, Args...>() - (int) sizeof...(Args),
1170
+ kwargs_pos = constexpr_first<argument_is_kwargs, Args...>() - (int) sizeof...(Args);
1171
+
1172
+ static constexpr bool args_kwargs_are_last = kwargs_pos >= - 1 && args_pos >= kwargs_pos - 1;
1173
+
1174
+ static_assert(args_kwargs_are_last, "py::args/py::kwargs are only permitted as the last argument(s) of a function");
1175
+
1176
+ public:
1177
+ static constexpr bool has_kwargs = kwargs_pos < 0;
1178
+ static constexpr bool has_args = args_pos < 0;
1179
+
1180
+ static constexpr auto arg_names = concat(type_descr(make_caster<Args>::name)...);
1181
+
1182
+ bool load_args(function_call &call) {
1183
+ return load_impl_sequence(call, indices{});
1184
+ }
1185
+
1186
+ template <typename Return, typename Guard, typename Func>
1187
+ // NOLINTNEXTLINE(readability-const-return-type)
1188
+ enable_if_t<!std::is_void<Return>::value, Return> call(Func &&f) && {
1189
+ return std::move(*this).template call_impl<remove_cv_t<Return>>(std::forward<Func>(f), indices{}, Guard{});
1190
+ }
1191
+
1192
+ template <typename Return, typename Guard, typename Func>
1193
+ enable_if_t<std::is_void<Return>::value, void_type> call(Func &&f) && {
1194
+ std::move(*this).template call_impl<remove_cv_t<Return>>(std::forward<Func>(f), indices{}, Guard{});
1195
+ return void_type();
1196
+ }
1197
+
1198
+ private:
1199
+
1200
+ static bool load_impl_sequence(function_call &, index_sequence<>) { return true; }
1201
+
1202
+ template <size_t... Is>
1203
+ bool load_impl_sequence(function_call &call, index_sequence<Is...>) {
1204
+ #ifdef __cpp_fold_expressions
1205
+ if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])))
1206
+ return false;
1207
+ #else
1208
+ for (bool r : {std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])...})
1209
+ if (!r)
1210
+ return false;
1211
+ #endif
1212
+ return true;
1213
+ }
1214
+
1215
+ template <typename Return, typename Func, size_t... Is, typename Guard>
1216
+ Return call_impl(Func &&f, index_sequence<Is...>, Guard &&) && {
1217
+ return std::forward<Func>(f)(cast_op<Args>(std::move(std::get<Is>(argcasters)))...);
1218
+ }
1219
+
1220
+ std::tuple<make_caster<Args>...> argcasters;
1221
+ };
1222
+
1223
+ /// Helper class which collects only positional arguments for a Python function call.
1224
+ /// A fancier version below can collect any argument, but this one is optimal for simple calls.
1225
+ template <return_value_policy policy>
1226
+ class simple_collector {
1227
+ public:
1228
+ template <typename... Ts>
1229
+ explicit simple_collector(Ts &&...values)
1230
+ : m_args(pybind11::make_tuple<policy>(std::forward<Ts>(values)...)) { }
1231
+
1232
+ const tuple &args() const & { return m_args; }
1233
+ dict kwargs() const { return {}; }
1234
+
1235
+ tuple args() && { return std::move(m_args); }
1236
+
1237
+ /// Call a Python function and pass the collected arguments
1238
+ object call(PyObject *ptr) const {
1239
+ PyObject *result = PyObject_CallObject(ptr, m_args.ptr());
1240
+ if (!result)
1241
+ throw error_already_set();
1242
+ return reinterpret_steal<object>(result);
1243
+ }
1244
+
1245
+ private:
1246
+ tuple m_args;
1247
+ };
1248
+
1249
+ /// Helper class which collects positional, keyword, * and ** arguments for a Python function call
1250
+ template <return_value_policy policy>
1251
+ class unpacking_collector {
1252
+ public:
1253
+ template <typename... Ts>
1254
+ explicit unpacking_collector(Ts &&...values) {
1255
+ // Tuples aren't (easily) resizable so a list is needed for collection,
1256
+ // but the actual function call strictly requires a tuple.
1257
+ auto args_list = list();
1258
+ using expander = int[];
1259
+ (void) expander{0, (process(args_list, std::forward<Ts>(values)), 0)...};
1260
+
1261
+ m_args = std::move(args_list);
1262
+ }
1263
+
1264
+ const tuple &args() const & { return m_args; }
1265
+ const dict &kwargs() const & { return m_kwargs; }
1266
+
1267
+ tuple args() && { return std::move(m_args); }
1268
+ dict kwargs() && { return std::move(m_kwargs); }
1269
+
1270
+ /// Call a Python function and pass the collected arguments
1271
+ object call(PyObject *ptr) const {
1272
+ PyObject *result = PyObject_Call(ptr, m_args.ptr(), m_kwargs.ptr());
1273
+ if (!result)
1274
+ throw error_already_set();
1275
+ return reinterpret_steal<object>(result);
1276
+ }
1277
+
1278
+ private:
1279
+ template <typename T>
1280
+ void process(list &args_list, T &&x) {
1281
+ auto o = reinterpret_steal<object>(detail::make_caster<T>::cast(std::forward<T>(x), policy, {}));
1282
+ if (!o) {
1283
+ #if defined(NDEBUG)
1284
+ throw cast_error_unable_to_convert_call_arg();
1285
+ #else
1286
+ throw cast_error_unable_to_convert_call_arg(
1287
+ std::to_string(args_list.size()), type_id<T>());
1288
+ #endif
1289
+ }
1290
+ args_list.append(o);
1291
+ }
1292
+
1293
+ void process(list &args_list, detail::args_proxy ap) {
1294
+ for (auto a : ap)
1295
+ args_list.append(a);
1296
+ }
1297
+
1298
+ void process(list &/*args_list*/, arg_v a) {
1299
+ if (!a.name)
1300
+ #if defined(NDEBUG)
1301
+ nameless_argument_error();
1302
+ #else
1303
+ nameless_argument_error(a.type);
1304
+ #endif
1305
+
1306
+ if (m_kwargs.contains(a.name)) {
1307
+ #if defined(NDEBUG)
1308
+ multiple_values_error();
1309
+ #else
1310
+ multiple_values_error(a.name);
1311
+ #endif
1312
+ }
1313
+ if (!a.value) {
1314
+ #if defined(NDEBUG)
1315
+ throw cast_error_unable_to_convert_call_arg();
1316
+ #else
1317
+ throw cast_error_unable_to_convert_call_arg(a.name, a.type);
1318
+ #endif
1319
+ }
1320
+ m_kwargs[a.name] = a.value;
1321
+ }
1322
+
1323
+ void process(list &/*args_list*/, detail::kwargs_proxy kp) {
1324
+ if (!kp)
1325
+ return;
1326
+ for (auto k : reinterpret_borrow<dict>(kp)) {
1327
+ if (m_kwargs.contains(k.first)) {
1328
+ #if defined(NDEBUG)
1329
+ multiple_values_error();
1330
+ #else
1331
+ multiple_values_error(str(k.first));
1332
+ #endif
1333
+ }
1334
+ m_kwargs[k.first] = k.second;
1335
+ }
1336
+ }
1337
+
1338
+ [[noreturn]] static void nameless_argument_error() {
1339
+ throw type_error("Got kwargs without a name; only named arguments "
1340
+ "may be passed via py::arg() to a python function call. "
1341
+ "(compile in debug mode for details)");
1342
+ }
1343
+ [[noreturn]] static void nameless_argument_error(const std::string &type) {
1344
+ throw type_error("Got kwargs without a name of type '" + type + "'; only named "
1345
+ "arguments may be passed via py::arg() to a python function call. ");
1346
+ }
1347
+ [[noreturn]] static void multiple_values_error() {
1348
+ throw type_error("Got multiple values for keyword argument "
1349
+ "(compile in debug mode for details)");
1350
+ }
1351
+
1352
+ [[noreturn]] static void multiple_values_error(const std::string &name) {
1353
+ throw type_error("Got multiple values for keyword argument '" + name + "'");
1354
+ }
1355
+
1356
+ private:
1357
+ tuple m_args;
1358
+ dict m_kwargs;
1359
+ };
1360
+
1361
+ // [workaround(intel)] Separate function required here
1362
+ // We need to put this into a separate function because the Intel compiler
1363
+ // fails to compile enable_if_t<!all_of<is_positional<Args>...>::value>
1364
+ // (tested with ICC 2021.1 Beta 20200827).
1365
+ template <typename... Args>
1366
+ constexpr bool args_are_all_positional()
1367
+ {
1368
+ return all_of<is_positional<Args>...>::value;
1369
+ }
1370
+
1371
+ /// Collect only positional arguments for a Python function call
1372
+ template <return_value_policy policy, typename... Args,
1373
+ typename = enable_if_t<args_are_all_positional<Args...>()>>
1374
+ simple_collector<policy> collect_arguments(Args &&...args) {
1375
+ return simple_collector<policy>(std::forward<Args>(args)...);
1376
+ }
1377
+
1378
+ /// Collect all arguments, including keywords and unpacking (only instantiated when needed)
1379
+ template <return_value_policy policy, typename... Args,
1380
+ typename = enable_if_t<!args_are_all_positional<Args...>()>>
1381
+ unpacking_collector<policy> collect_arguments(Args &&...args) {
1382
+ // Following argument order rules for generalized unpacking according to PEP 448
1383
+ static_assert(
1384
+ constexpr_last<is_positional, Args...>() < constexpr_first<is_keyword_or_ds, Args...>()
1385
+ && constexpr_last<is_s_unpacking, Args...>() < constexpr_first<is_ds_unpacking, Args...>(),
1386
+ "Invalid function call: positional args must precede keywords and ** unpacking; "
1387
+ "* unpacking must precede ** unpacking"
1388
+ );
1389
+ return unpacking_collector<policy>(std::forward<Args>(args)...);
1390
+ }
1391
+
1392
+ template <typename Derived>
1393
+ template <return_value_policy policy, typename... Args>
1394
+ object object_api<Derived>::operator()(Args &&...args) const {
1395
+ #if !defined(NDEBUG) && PY_VERSION_HEX >= 0x03060000
1396
+ if (!PyGILState_Check()) {
1397
+ pybind11_fail("pybind11::object_api<>::operator() PyGILState_Check() failure.");
1398
+ }
1399
+ #endif
1400
+ return detail::collect_arguments<policy>(std::forward<Args>(args)...).call(derived().ptr());
1401
+ }
1402
+
1403
+ template <typename Derived>
1404
+ template <return_value_policy policy, typename... Args>
1405
+ object object_api<Derived>::call(Args &&...args) const {
1406
+ return operator()<policy>(std::forward<Args>(args)...);
1407
+ }
1408
+
1409
+ PYBIND11_NAMESPACE_END(detail)
1410
+
1411
+
1412
+ template<typename T>
1413
+ handle type::handle_of() {
1414
+ static_assert(
1415
+ std::is_base_of<detail::type_caster_generic, detail::make_caster<T>>::value,
1416
+ "py::type::of<T> only supports the case where T is a registered C++ types."
1417
+ );
1418
+
1419
+ return detail::get_type_handle(typeid(T), true);
1420
+ }
1421
+
1422
+
1423
+ #define PYBIND11_MAKE_OPAQUE(...) \
1424
+ namespace pybind11 { namespace detail { \
1425
+ template<> class type_caster<__VA_ARGS__> : public type_caster_base<__VA_ARGS__> { }; \
1426
+ }}
1427
+
1428
+ /// Lets you pass a type containing a `,` through a macro parameter without needing a separate
1429
+ /// typedef, e.g.: `PYBIND11_OVERRIDE(PYBIND11_TYPE(ReturnType<A, B>), PYBIND11_TYPE(Parent<C, D>), f, arg)`
1430
+ #define PYBIND11_TYPE(...) __VA_ARGS__
1431
+
1432
+ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)