netgen-mesher 6.2.2506.post35.dev0__cp314-cp314-win_amd64.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.
Files changed (340) hide show
  1. netgen/NgOCC.py +7 -0
  2. netgen/__init__.py +114 -0
  3. netgen/__init__.pyi +22 -0
  4. netgen/__main__.py +53 -0
  5. netgen/cmake/NetgenConfig.cmake +79 -0
  6. netgen/cmake/netgen-targets-release.cmake +69 -0
  7. netgen/cmake/netgen-targets.cmake +146 -0
  8. netgen/config/__init__.py +1 -0
  9. netgen/config/__init__.pyi +52 -0
  10. netgen/config/__main__.py +4 -0
  11. netgen/config/config.py +68 -0
  12. netgen/config/config.pyi +54 -0
  13. netgen/csg.py +25 -0
  14. netgen/geom2d.py +178 -0
  15. netgen/gui.py +82 -0
  16. netgen/include/core/archive.hpp +1256 -0
  17. netgen/include/core/array.hpp +1760 -0
  18. netgen/include/core/autodiff.hpp +1131 -0
  19. netgen/include/core/autodiffdiff.hpp +733 -0
  20. netgen/include/core/bitarray.hpp +240 -0
  21. netgen/include/core/concurrentqueue.h +3619 -0
  22. netgen/include/core/exception.hpp +145 -0
  23. netgen/include/core/flags.hpp +199 -0
  24. netgen/include/core/hashtable.hpp +1281 -0
  25. netgen/include/core/localheap.hpp +318 -0
  26. netgen/include/core/logging.hpp +117 -0
  27. netgen/include/core/memtracer.hpp +221 -0
  28. netgen/include/core/mpi4py_pycapi.h +245 -0
  29. netgen/include/core/mpi_wrapper.hpp +643 -0
  30. netgen/include/core/ng_mpi.hpp +94 -0
  31. netgen/include/core/ng_mpi_generated_declarations.hpp +155 -0
  32. netgen/include/core/ng_mpi_native.hpp +25 -0
  33. netgen/include/core/ngcore.hpp +32 -0
  34. netgen/include/core/ngcore_api.hpp +152 -0
  35. netgen/include/core/ngstream.hpp +115 -0
  36. netgen/include/core/paje_trace.hpp +279 -0
  37. netgen/include/core/profiler.hpp +382 -0
  38. netgen/include/core/python_ngcore.hpp +457 -0
  39. netgen/include/core/ranges.hpp +109 -0
  40. netgen/include/core/register_archive.hpp +100 -0
  41. netgen/include/core/signal.hpp +82 -0
  42. netgen/include/core/simd.hpp +160 -0
  43. netgen/include/core/simd_arm64.hpp +407 -0
  44. netgen/include/core/simd_avx.hpp +394 -0
  45. netgen/include/core/simd_avx512.hpp +285 -0
  46. netgen/include/core/simd_generic.hpp +1053 -0
  47. netgen/include/core/simd_math.hpp +178 -0
  48. netgen/include/core/simd_sse.hpp +289 -0
  49. netgen/include/core/statushandler.hpp +37 -0
  50. netgen/include/core/symboltable.hpp +153 -0
  51. netgen/include/core/table.hpp +810 -0
  52. netgen/include/core/taskmanager.hpp +1161 -0
  53. netgen/include/core/type_traits.hpp +65 -0
  54. netgen/include/core/utils.hpp +385 -0
  55. netgen/include/core/version.hpp +102 -0
  56. netgen/include/core/xbool.hpp +47 -0
  57. netgen/include/csg/algprim.hpp +563 -0
  58. netgen/include/csg/brick.hpp +150 -0
  59. netgen/include/csg/csg.hpp +43 -0
  60. netgen/include/csg/csgeom.hpp +389 -0
  61. netgen/include/csg/csgparser.hpp +101 -0
  62. netgen/include/csg/curve2d.hpp +67 -0
  63. netgen/include/csg/edgeflw.hpp +112 -0
  64. netgen/include/csg/explicitcurve2d.hpp +113 -0
  65. netgen/include/csg/extrusion.hpp +185 -0
  66. netgen/include/csg/gencyl.hpp +70 -0
  67. netgen/include/csg/geoml.hpp +16 -0
  68. netgen/include/csg/identify.hpp +213 -0
  69. netgen/include/csg/manifold.hpp +29 -0
  70. netgen/include/csg/meshsurf.hpp +46 -0
  71. netgen/include/csg/polyhedra.hpp +121 -0
  72. netgen/include/csg/revolution.hpp +180 -0
  73. netgen/include/csg/singularref.hpp +84 -0
  74. netgen/include/csg/solid.hpp +295 -0
  75. netgen/include/csg/specpoin.hpp +194 -0
  76. netgen/include/csg/spline3d.hpp +99 -0
  77. netgen/include/csg/splinesurface.hpp +85 -0
  78. netgen/include/csg/surface.hpp +394 -0
  79. netgen/include/csg/triapprox.hpp +63 -0
  80. netgen/include/csg/vscsg.hpp +34 -0
  81. netgen/include/general/autodiff.hpp +356 -0
  82. netgen/include/general/autoptr.hpp +39 -0
  83. netgen/include/general/gzstream.h +121 -0
  84. netgen/include/general/hashtabl.hpp +1692 -0
  85. netgen/include/general/myadt.hpp +48 -0
  86. netgen/include/general/mystring.hpp +226 -0
  87. netgen/include/general/netgenout.hpp +205 -0
  88. netgen/include/general/ngarray.hpp +797 -0
  89. netgen/include/general/ngbitarray.hpp +149 -0
  90. netgen/include/general/ngpython.hpp +74 -0
  91. netgen/include/general/optmem.hpp +44 -0
  92. netgen/include/general/parthreads.hpp +138 -0
  93. netgen/include/general/seti.hpp +50 -0
  94. netgen/include/general/sort.hpp +47 -0
  95. netgen/include/general/spbita2d.hpp +59 -0
  96. netgen/include/general/stack.hpp +114 -0
  97. netgen/include/general/table.hpp +280 -0
  98. netgen/include/general/template.hpp +509 -0
  99. netgen/include/geom2d/csg2d.hpp +750 -0
  100. netgen/include/geom2d/geometry2d.hpp +280 -0
  101. netgen/include/geom2d/spline2d.hpp +234 -0
  102. netgen/include/geom2d/vsgeom2d.hpp +28 -0
  103. netgen/include/gprim/adtree.hpp +1392 -0
  104. netgen/include/gprim/geom2d.hpp +858 -0
  105. netgen/include/gprim/geom3d.hpp +749 -0
  106. netgen/include/gprim/geomfuncs.hpp +212 -0
  107. netgen/include/gprim/geomobjects.hpp +544 -0
  108. netgen/include/gprim/geomops.hpp +404 -0
  109. netgen/include/gprim/geomtest3d.hpp +101 -0
  110. netgen/include/gprim/gprim.hpp +33 -0
  111. netgen/include/gprim/spline.hpp +778 -0
  112. netgen/include/gprim/splinegeometry.hpp +73 -0
  113. netgen/include/gprim/transform3d.hpp +216 -0
  114. netgen/include/include/acisgeom.hpp +3 -0
  115. netgen/include/include/csg.hpp +1 -0
  116. netgen/include/include/geometry2d.hpp +1 -0
  117. netgen/include/include/gprim.hpp +1 -0
  118. netgen/include/include/incopengl.hpp +62 -0
  119. netgen/include/include/inctcl.hpp +13 -0
  120. netgen/include/include/incvis.hpp +6 -0
  121. netgen/include/include/linalg.hpp +1 -0
  122. netgen/include/include/meshing.hpp +1 -0
  123. netgen/include/include/myadt.hpp +1 -0
  124. netgen/include/include/mydefs.hpp +70 -0
  125. netgen/include/include/mystdlib.h +59 -0
  126. netgen/include/include/netgen_config.hpp +27 -0
  127. netgen/include/include/netgen_version.hpp +9 -0
  128. netgen/include/include/nginterface_v2_impl.hpp +395 -0
  129. netgen/include/include/ngsimd.hpp +1 -0
  130. netgen/include/include/occgeom.hpp +1 -0
  131. netgen/include/include/opti.hpp +1 -0
  132. netgen/include/include/parallel.hpp +1 -0
  133. netgen/include/include/stlgeom.hpp +1 -0
  134. netgen/include/include/visual.hpp +1 -0
  135. netgen/include/interface/rw_medit.hpp +11 -0
  136. netgen/include/interface/writeuser.hpp +80 -0
  137. netgen/include/linalg/densemat.hpp +414 -0
  138. netgen/include/linalg/linalg.hpp +29 -0
  139. netgen/include/linalg/opti.hpp +142 -0
  140. netgen/include/linalg/polynomial.hpp +47 -0
  141. netgen/include/linalg/vector.hpp +217 -0
  142. netgen/include/meshing/adfront2.hpp +274 -0
  143. netgen/include/meshing/adfront3.hpp +332 -0
  144. netgen/include/meshing/basegeom.hpp +370 -0
  145. netgen/include/meshing/bcfunctions.hpp +53 -0
  146. netgen/include/meshing/bisect.hpp +72 -0
  147. netgen/include/meshing/boundarylayer.hpp +113 -0
  148. netgen/include/meshing/classifyhpel.hpp +1984 -0
  149. netgen/include/meshing/clusters.hpp +46 -0
  150. netgen/include/meshing/curvedelems.hpp +274 -0
  151. netgen/include/meshing/delaunay2d.hpp +73 -0
  152. netgen/include/meshing/fieldlines.hpp +103 -0
  153. netgen/include/meshing/findip.hpp +198 -0
  154. netgen/include/meshing/findip2.hpp +103 -0
  155. netgen/include/meshing/geomsearch.hpp +69 -0
  156. netgen/include/meshing/global.hpp +54 -0
  157. netgen/include/meshing/hpref_hex.hpp +330 -0
  158. netgen/include/meshing/hpref_prism.hpp +3405 -0
  159. netgen/include/meshing/hpref_pyramid.hpp +154 -0
  160. netgen/include/meshing/hpref_quad.hpp +2082 -0
  161. netgen/include/meshing/hpref_segm.hpp +122 -0
  162. netgen/include/meshing/hpref_tet.hpp +4230 -0
  163. netgen/include/meshing/hpref_trig.hpp +848 -0
  164. netgen/include/meshing/hprefinement.hpp +366 -0
  165. netgen/include/meshing/improve2.hpp +178 -0
  166. netgen/include/meshing/improve3.hpp +151 -0
  167. netgen/include/meshing/localh.hpp +223 -0
  168. netgen/include/meshing/meshclass.hpp +1076 -0
  169. netgen/include/meshing/meshfunc.hpp +47 -0
  170. netgen/include/meshing/meshing.hpp +63 -0
  171. netgen/include/meshing/meshing2.hpp +163 -0
  172. netgen/include/meshing/meshing3.hpp +123 -0
  173. netgen/include/meshing/meshtool.hpp +90 -0
  174. netgen/include/meshing/meshtype.hpp +1930 -0
  175. netgen/include/meshing/msghandler.hpp +62 -0
  176. netgen/include/meshing/paralleltop.hpp +172 -0
  177. netgen/include/meshing/python_mesh.hpp +206 -0
  178. netgen/include/meshing/ruler2.hpp +172 -0
  179. netgen/include/meshing/ruler3.hpp +211 -0
  180. netgen/include/meshing/soldata.hpp +141 -0
  181. netgen/include/meshing/specials.hpp +17 -0
  182. netgen/include/meshing/surfacegeom.hpp +73 -0
  183. netgen/include/meshing/topology.hpp +1003 -0
  184. netgen/include/meshing/validate.hpp +21 -0
  185. netgen/include/meshing/visual_interface.hpp +71 -0
  186. netgen/include/mydefs.hpp +70 -0
  187. netgen/include/nginterface.h +474 -0
  188. netgen/include/nginterface_v2.hpp +406 -0
  189. netgen/include/nglib.h +697 -0
  190. netgen/include/nglib_occ.h +50 -0
  191. netgen/include/occ/occ_edge.hpp +47 -0
  192. netgen/include/occ/occ_face.hpp +52 -0
  193. netgen/include/occ/occ_solid.hpp +23 -0
  194. netgen/include/occ/occ_utils.hpp +376 -0
  195. netgen/include/occ/occ_vertex.hpp +30 -0
  196. netgen/include/occ/occgeom.hpp +659 -0
  197. netgen/include/occ/occmeshsurf.hpp +168 -0
  198. netgen/include/occ/vsocc.hpp +33 -0
  199. netgen/include/pybind11/LICENSE +29 -0
  200. netgen/include/pybind11/attr.h +722 -0
  201. netgen/include/pybind11/buffer_info.h +208 -0
  202. netgen/include/pybind11/cast.h +2361 -0
  203. netgen/include/pybind11/chrono.h +228 -0
  204. netgen/include/pybind11/common.h +2 -0
  205. netgen/include/pybind11/complex.h +74 -0
  206. netgen/include/pybind11/conduit/README.txt +15 -0
  207. netgen/include/pybind11/conduit/pybind11_conduit_v1.h +116 -0
  208. netgen/include/pybind11/conduit/pybind11_platform_abi_id.h +87 -0
  209. netgen/include/pybind11/conduit/wrap_include_python_h.h +72 -0
  210. netgen/include/pybind11/critical_section.h +56 -0
  211. netgen/include/pybind11/detail/class.h +823 -0
  212. netgen/include/pybind11/detail/common.h +1348 -0
  213. netgen/include/pybind11/detail/cpp_conduit.h +75 -0
  214. netgen/include/pybind11/detail/descr.h +226 -0
  215. netgen/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h +39 -0
  216. netgen/include/pybind11/detail/exception_translation.h +71 -0
  217. netgen/include/pybind11/detail/function_record_pyobject.h +191 -0
  218. netgen/include/pybind11/detail/init.h +538 -0
  219. netgen/include/pybind11/detail/internals.h +799 -0
  220. netgen/include/pybind11/detail/native_enum_data.h +209 -0
  221. netgen/include/pybind11/detail/pybind11_namespace_macros.h +82 -0
  222. netgen/include/pybind11/detail/struct_smart_holder.h +378 -0
  223. netgen/include/pybind11/detail/type_caster_base.h +1591 -0
  224. netgen/include/pybind11/detail/typeid.h +65 -0
  225. netgen/include/pybind11/detail/using_smart_holder.h +22 -0
  226. netgen/include/pybind11/detail/value_and_holder.h +90 -0
  227. netgen/include/pybind11/eigen/common.h +9 -0
  228. netgen/include/pybind11/eigen/matrix.h +723 -0
  229. netgen/include/pybind11/eigen/tensor.h +521 -0
  230. netgen/include/pybind11/eigen.h +12 -0
  231. netgen/include/pybind11/embed.h +320 -0
  232. netgen/include/pybind11/eval.h +161 -0
  233. netgen/include/pybind11/functional.h +147 -0
  234. netgen/include/pybind11/gil.h +199 -0
  235. netgen/include/pybind11/gil_safe_call_once.h +102 -0
  236. netgen/include/pybind11/gil_simple.h +37 -0
  237. netgen/include/pybind11/iostream.h +265 -0
  238. netgen/include/pybind11/native_enum.h +67 -0
  239. netgen/include/pybind11/numpy.h +2312 -0
  240. netgen/include/pybind11/operators.h +202 -0
  241. netgen/include/pybind11/options.h +92 -0
  242. netgen/include/pybind11/pybind11.h +3645 -0
  243. netgen/include/pybind11/pytypes.h +2680 -0
  244. netgen/include/pybind11/stl/filesystem.h +114 -0
  245. netgen/include/pybind11/stl.h +666 -0
  246. netgen/include/pybind11/stl_bind.h +858 -0
  247. netgen/include/pybind11/subinterpreter.h +299 -0
  248. netgen/include/pybind11/trampoline_self_life_support.h +65 -0
  249. netgen/include/pybind11/type_caster_pyobject_ptr.h +61 -0
  250. netgen/include/pybind11/typing.h +298 -0
  251. netgen/include/pybind11/warnings.h +75 -0
  252. netgen/include/stlgeom/meshstlsurface.hpp +67 -0
  253. netgen/include/stlgeom/stlgeom.hpp +491 -0
  254. netgen/include/stlgeom/stlline.hpp +193 -0
  255. netgen/include/stlgeom/stltool.hpp +331 -0
  256. netgen/include/stlgeom/stltopology.hpp +419 -0
  257. netgen/include/stlgeom/vsstl.hpp +58 -0
  258. netgen/include/visualization/meshdoc.hpp +42 -0
  259. netgen/include/visualization/mvdraw.hpp +325 -0
  260. netgen/include/visualization/vispar.hpp +128 -0
  261. netgen/include/visualization/visual.hpp +28 -0
  262. netgen/include/visualization/visual_api.hpp +10 -0
  263. netgen/include/visualization/vssolution.hpp +399 -0
  264. netgen/lib/libnggui.lib +0 -0
  265. netgen/lib/ngcore.lib +0 -0
  266. netgen/lib/nglib.lib +0 -0
  267. netgen/lib/togl.lib +0 -0
  268. netgen/libnggui.dll +0 -0
  269. netgen/libngguipy.lib +0 -0
  270. netgen/libngguipy.pyd +0 -0
  271. netgen/libngpy/_NgOCC.pyi +1545 -0
  272. netgen/libngpy/__init__.pyi +7 -0
  273. netgen/libngpy/_csg.pyi +259 -0
  274. netgen/libngpy/_geom2d.pyi +323 -0
  275. netgen/libngpy/_meshing.pyi +1111 -0
  276. netgen/libngpy/_stl.pyi +131 -0
  277. netgen/libngpy.lib +0 -0
  278. netgen/libngpy.pyd +0 -0
  279. netgen/meshing.py +65 -0
  280. netgen/ngcore.dll +0 -0
  281. netgen/nglib.dll +0 -0
  282. netgen/occ.py +52 -0
  283. netgen/read_gmsh.py +259 -0
  284. netgen/read_meshio.py +22 -0
  285. netgen/stl.py +2 -0
  286. netgen/togl.dll +0 -0
  287. netgen/version.py +2 -0
  288. netgen/webgui.py +529 -0
  289. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/boundarycondition.geo +16 -0
  290. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/boxcyl.geo +32 -0
  291. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/circle_on_cube.geo +27 -0
  292. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cone.geo +13 -0
  293. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cube.geo +16 -0
  294. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubeandring.geo +55 -0
  295. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubeandspheres.geo +21 -0
  296. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubemcyl.geo +18 -0
  297. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubemsphere.geo +19 -0
  298. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cylinder.geo +12 -0
  299. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cylsphere.geo +12 -0
  300. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/doc/ng4.pdf +0 -0
  301. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/ellipsoid.geo +8 -0
  302. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/ellipticcyl.geo +10 -0
  303. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/extrusion.geo +99 -0
  304. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/fichera.geo +24 -0
  305. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/frame.step +11683 -0
  306. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/hinge.stl +8486 -0
  307. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/lshape3d.geo +26 -0
  308. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/manyholes.geo +26 -0
  309. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/manyholes2.geo +26 -0
  310. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/matrix.geo +27 -0
  311. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/ortho.geo +11 -0
  312. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/part1.stl +2662 -0
  313. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/period.geo +33 -0
  314. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/py_tutorials/exportNeutral.py +26 -0
  315. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/py_tutorials/mesh.py +19 -0
  316. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/py_tutorials/shaft.geo +65 -0
  317. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/revolution.geo +18 -0
  318. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/screw.step +1694 -0
  319. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/sculpture.geo +13 -0
  320. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/shaft.geo +65 -0
  321. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/shell.geo +10 -0
  322. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/sphere.geo +8 -0
  323. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/sphereincube.geo +17 -0
  324. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/square.in2d +35 -0
  325. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/squarecircle.in2d +48 -0
  326. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/squarehole.in2d +47 -0
  327. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/torus.geo +8 -0
  328. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/trafo.geo +57 -0
  329. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/twobricks.geo +15 -0
  330. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/twocubes.geo +18 -0
  331. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/twocyl.geo +16 -0
  332. netgen_mesher-6.2.2506.post35.dev0.dist-info/METADATA +15 -0
  333. netgen_mesher-6.2.2506.post35.dev0.dist-info/RECORD +340 -0
  334. netgen_mesher-6.2.2506.post35.dev0.dist-info/WHEEL +5 -0
  335. netgen_mesher-6.2.2506.post35.dev0.dist-info/entry_points.txt +2 -0
  336. netgen_mesher-6.2.2506.post35.dev0.dist-info/licenses/AUTHORS +1 -0
  337. netgen_mesher-6.2.2506.post35.dev0.dist-info/licenses/LICENSE +504 -0
  338. netgen_mesher-6.2.2506.post35.dev0.dist-info/top_level.txt +2 -0
  339. pyngcore/__init__.py +1 -0
  340. pyngcore/pyngcore.cp314-win_amd64.pyd +0 -0
@@ -0,0 +1,199 @@
1
+ /*
2
+ pybind11/gil.h: RAII helpers for managing the GIL
3
+
4
+ Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
+
6
+ All rights reserved. Use of this source code is governed by a
7
+ BSD-style license that can be found in the LICENSE file.
8
+ */
9
+
10
+ #pragma once
11
+
12
+ #if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
13
+
14
+ # include "detail/common.h"
15
+ # include "gil_simple.h"
16
+
17
+ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
18
+
19
+ using gil_scoped_acquire = gil_scoped_acquire_simple;
20
+ using gil_scoped_release = gil_scoped_release_simple;
21
+
22
+ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
23
+
24
+ #else
25
+
26
+ # include "detail/common.h"
27
+ # include "detail/internals.h"
28
+
29
+ # include <cassert>
30
+
31
+ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
32
+
33
+ PYBIND11_NAMESPACE_BEGIN(detail)
34
+
35
+ PYBIND11_WARNING_PUSH
36
+ PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
37
+
38
+ // forward declarations
39
+ PyThreadState *get_thread_state_unchecked();
40
+
41
+ PYBIND11_WARNING_POP
42
+
43
+ PYBIND11_NAMESPACE_END(detail)
44
+
45
+ /* The functions below essentially reproduce the PyGILState_* API using a RAII
46
+ * pattern, but there are a few important differences:
47
+ *
48
+ * 1. When acquiring the GIL from an non-main thread during the finalization
49
+ * phase, the GILState API blindly terminates the calling thread, which
50
+ * is often not what is wanted. This API does not do this.
51
+ *
52
+ * 2. The gil_scoped_release function can optionally cut the relationship
53
+ * of a PyThreadState and its associated thread, which allows moving it to
54
+ * another thread (this is a fairly rare/advanced use case).
55
+ *
56
+ * 3. The reference count of an acquired thread state can be controlled. This
57
+ * can be handy to prevent cases where callbacks issued from an external
58
+ * thread would otherwise constantly construct and destroy thread state data
59
+ * structures.
60
+ *
61
+ * See the Python bindings of NanoGUI (http://github.com/wjakob/nanogui) for an
62
+ * example which uses features 2 and 3 to migrate the Python thread of
63
+ * execution to another thread (to run the event loop on the original thread,
64
+ * in this case).
65
+ */
66
+
67
+ class gil_scoped_acquire {
68
+ public:
69
+ PYBIND11_NOINLINE gil_scoped_acquire() {
70
+ auto &internals = detail::get_internals();
71
+ tstate = internals.tstate.get();
72
+
73
+ if (!tstate) {
74
+ /* Check if the GIL was acquired using the PyGILState_* API instead (e.g. if
75
+ calling from a Python thread). Since we use a different key, this ensures
76
+ we don't create a new thread state and deadlock in PyEval_AcquireThread
77
+ below. Note we don't save this state with internals.tstate, since we don't
78
+ create it we would fail to clear it (its reference count should be > 0). */
79
+ tstate = PyGILState_GetThisThreadState();
80
+ }
81
+
82
+ if (!tstate) {
83
+ tstate = PyThreadState_New(internals.istate);
84
+ # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
85
+ if (!tstate) {
86
+ pybind11_fail("scoped_acquire: could not create thread state!");
87
+ }
88
+ # endif
89
+ tstate->gilstate_counter = 0;
90
+ internals.tstate = tstate;
91
+ } else {
92
+ release = detail::get_thread_state_unchecked() != tstate;
93
+ }
94
+
95
+ if (release) {
96
+ PyEval_AcquireThread(tstate);
97
+ }
98
+
99
+ inc_ref();
100
+ }
101
+
102
+ gil_scoped_acquire(const gil_scoped_acquire &) = delete;
103
+ gil_scoped_acquire &operator=(const gil_scoped_acquire &) = delete;
104
+
105
+ void inc_ref() { ++tstate->gilstate_counter; }
106
+
107
+ PYBIND11_NOINLINE void dec_ref() {
108
+ --tstate->gilstate_counter;
109
+ # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
110
+ if (detail::get_thread_state_unchecked() != tstate) {
111
+ pybind11_fail("scoped_acquire::dec_ref(): thread state must be current!");
112
+ }
113
+ if (tstate->gilstate_counter < 0) {
114
+ pybind11_fail("scoped_acquire::dec_ref(): reference count underflow!");
115
+ }
116
+ # endif
117
+ if (tstate->gilstate_counter == 0) {
118
+ # if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
119
+ if (!release) {
120
+ pybind11_fail("scoped_acquire::dec_ref(): internal error!");
121
+ }
122
+ # endif
123
+ PyThreadState_Clear(tstate);
124
+ if (active) {
125
+ PyThreadState_DeleteCurrent();
126
+ }
127
+ detail::get_internals().tstate.reset();
128
+ release = false;
129
+ }
130
+ }
131
+
132
+ /// This method will disable the PyThreadState_DeleteCurrent call and the
133
+ /// GIL won't be released. This method should be used if the interpreter
134
+ /// could be shutting down when this is called, as thread deletion is not
135
+ /// allowed during shutdown. Check _Py_IsFinalizing() on Python 3.7+, and
136
+ /// protect subsequent code.
137
+ PYBIND11_NOINLINE void disarm() { active = false; }
138
+
139
+ PYBIND11_NOINLINE ~gil_scoped_acquire() {
140
+ dec_ref();
141
+ if (release) {
142
+ PyEval_SaveThread();
143
+ }
144
+ }
145
+
146
+ private:
147
+ PyThreadState *tstate = nullptr;
148
+ bool release = true;
149
+ bool active = true;
150
+ };
151
+
152
+ class gil_scoped_release {
153
+ public:
154
+ // PRECONDITION: The GIL must be held when this constructor is called.
155
+ explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
156
+ assert(PyGILState_Check());
157
+ // `get_internals()` must be called here unconditionally in order to initialize
158
+ // `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an
159
+ // initialization race could occur as multiple threads try `gil_scoped_acquire`.
160
+ auto &internals = detail::get_internals();
161
+ // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
162
+ tstate = PyEval_SaveThread();
163
+ if (disassoc) {
164
+ internals.tstate.reset();
165
+ }
166
+ }
167
+
168
+ gil_scoped_release(const gil_scoped_release &) = delete;
169
+ gil_scoped_release &operator=(const gil_scoped_release &) = delete;
170
+
171
+ /// This method will disable the PyThreadState_DeleteCurrent call and the
172
+ /// GIL won't be acquired. This method should be used if the interpreter
173
+ /// could be shutting down when this is called, as thread deletion is not
174
+ /// allowed during shutdown. Check _Py_IsFinalizing() on Python 3.7+, and
175
+ /// protect subsequent code.
176
+ PYBIND11_NOINLINE void disarm() { active = false; }
177
+
178
+ ~gil_scoped_release() {
179
+ if (!tstate) {
180
+ return;
181
+ }
182
+ // `PyEval_RestoreThread()` should not be called if runtime is finalizing
183
+ if (active) {
184
+ PyEval_RestoreThread(tstate);
185
+ }
186
+ if (disassoc) {
187
+ detail::get_internals().tstate = tstate;
188
+ }
189
+ }
190
+
191
+ private:
192
+ PyThreadState *tstate;
193
+ bool disassoc;
194
+ bool active = true;
195
+ };
196
+
197
+ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
198
+
199
+ #endif // !PYBIND11_SIMPLE_GIL_MANAGEMENT
@@ -0,0 +1,102 @@
1
+ // Copyright (c) 2023 The pybind Community.
2
+
3
+ #pragma once
4
+
5
+ #include "detail/common.h"
6
+ #include "gil.h"
7
+
8
+ #include <cassert>
9
+ #include <mutex>
10
+
11
+ #ifdef Py_GIL_DISABLED
12
+ # include <atomic>
13
+ #endif
14
+
15
+ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
16
+
17
+ // Use the `gil_safe_call_once_and_store` class below instead of the naive
18
+ //
19
+ // static auto imported_obj = py::module_::import("module_name"); // BAD, DO NOT USE!
20
+ //
21
+ // which has two serious issues:
22
+ //
23
+ // 1. Py_DECREF() calls potentially after the Python interpreter was finalized already, and
24
+ // 2. deadlocks in multi-threaded processes (because of missing lock ordering).
25
+ //
26
+ // The following alternative avoids both problems:
27
+ //
28
+ // PYBIND11_CONSTINIT static py::gil_safe_call_once_and_store<py::object> storage;
29
+ // auto &imported_obj = storage // Do NOT make this `static`!
30
+ // .call_once_and_store_result([]() {
31
+ // return py::module_::import("module_name");
32
+ // })
33
+ // .get_stored();
34
+ //
35
+ // The parameter of `call_once_and_store_result()` must be callable. It can make
36
+ // CPython API calls, and in particular, it can temporarily release the GIL.
37
+ //
38
+ // `T` can be any C++ type, it does not have to involve CPython API types.
39
+ //
40
+ // The behavior with regard to signals, e.g. `SIGINT` (`KeyboardInterrupt`),
41
+ // is not ideal. If the main thread is the one to actually run the `Callable`,
42
+ // then a `KeyboardInterrupt` will interrupt it if it is running normal Python
43
+ // code. The situation is different if a non-main thread runs the
44
+ // `Callable`, and then the main thread starts waiting for it to complete:
45
+ // a `KeyboardInterrupt` will not interrupt the non-main thread, but it will
46
+ // get processed only when it is the main thread's turn again and it is running
47
+ // normal Python code. However, this will be unnoticeable for quick call-once
48
+ // functions, which is usually the case.
49
+ //
50
+ // For in-depth background, see docs/advanced/deadlock.md
51
+ template <typename T>
52
+ class gil_safe_call_once_and_store {
53
+ public:
54
+ // PRECONDITION: The GIL must be held when `call_once_and_store_result()` is called.
55
+ template <typename Callable>
56
+ gil_safe_call_once_and_store &call_once_and_store_result(Callable &&fn) {
57
+ if (!is_initialized_) { // This read is guarded by the GIL.
58
+ // Multiple threads may enter here, because the GIL is released in the next line and
59
+ // CPython API calls in the `fn()` call below may release and reacquire the GIL.
60
+ gil_scoped_release gil_rel; // Needed to establish lock ordering.
61
+ std::call_once(once_flag_, [&] {
62
+ // Only one thread will ever enter here.
63
+ gil_scoped_acquire gil_acq;
64
+ ::new (storage_) T(fn()); // fn may release, but will reacquire, the GIL.
65
+ is_initialized_ = true; // This write is guarded by the GIL.
66
+ });
67
+ // All threads will observe `is_initialized_` as true here.
68
+ }
69
+ // Intentionally not returning `T &` to ensure the calling code is self-documenting.
70
+ return *this;
71
+ }
72
+
73
+ // This must only be called after `call_once_and_store_result()` was called.
74
+ T &get_stored() {
75
+ assert(is_initialized_);
76
+ PYBIND11_WARNING_PUSH
77
+ #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5
78
+ // Needed for gcc 4.8.5
79
+ PYBIND11_WARNING_DISABLE_GCC("-Wstrict-aliasing")
80
+ #endif
81
+ return *reinterpret_cast<T *>(storage_);
82
+ PYBIND11_WARNING_POP
83
+ }
84
+
85
+ constexpr gil_safe_call_once_and_store() = default;
86
+ PYBIND11_DTOR_CONSTEXPR ~gil_safe_call_once_and_store() = default;
87
+
88
+ private:
89
+ alignas(T) char storage_[sizeof(T)] = {};
90
+ std::once_flag once_flag_ = {};
91
+ #ifdef Py_GIL_DISABLED
92
+ std::atomic_bool
93
+ #else
94
+ bool
95
+ #endif
96
+ is_initialized_{false};
97
+ // The `is_initialized_`-`storage_` pair is very similar to `std::optional`,
98
+ // but the latter does not have the triviality properties of former,
99
+ // therefore `std::optional` is not a viable alternative here.
100
+ };
101
+
102
+ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
@@ -0,0 +1,37 @@
1
+ // Copyright (c) 2016-2025 The Pybind Development Team.
2
+ // All rights reserved. Use of this source code is governed by a
3
+ // BSD-style license that can be found in the LICENSE file.
4
+
5
+ #pragma once
6
+
7
+ #include "detail/common.h"
8
+
9
+ #include <cassert>
10
+
11
+ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
12
+
13
+ class gil_scoped_acquire_simple {
14
+ PyGILState_STATE state;
15
+
16
+ public:
17
+ gil_scoped_acquire_simple() : state{PyGILState_Ensure()} {}
18
+ gil_scoped_acquire_simple(const gil_scoped_acquire_simple &) = delete;
19
+ gil_scoped_acquire_simple &operator=(const gil_scoped_acquire_simple &) = delete;
20
+ ~gil_scoped_acquire_simple() { PyGILState_Release(state); }
21
+ };
22
+
23
+ class gil_scoped_release_simple {
24
+ PyThreadState *state;
25
+
26
+ public:
27
+ // PRECONDITION: The GIL must be held when this constructor is called.
28
+ gil_scoped_release_simple() {
29
+ assert(PyGILState_Check());
30
+ state = PyEval_SaveThread();
31
+ }
32
+ gil_scoped_release_simple(const gil_scoped_release_simple &) = delete;
33
+ gil_scoped_release_simple &operator=(const gil_scoped_release_simple &) = delete;
34
+ ~gil_scoped_release_simple() { PyEval_RestoreThread(state); }
35
+ };
36
+
37
+ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
@@ -0,0 +1,265 @@
1
+ /*
2
+ pybind11/iostream.h -- Tools to assist with redirecting cout and cerr to Python
3
+
4
+ Copyright (c) 2017 Henry F. Schreiner
5
+
6
+ All rights reserved. Use of this source code is governed by a
7
+ BSD-style license that can be found in the LICENSE file.
8
+
9
+ WARNING: The implementation in this file is NOT thread safe. Multiple
10
+ threads writing to a redirected ostream concurrently cause data races
11
+ and potentially buffer overflows. Therefore it is currently a requirement
12
+ that all (possibly) concurrent redirected ostream writes are protected by
13
+ a mutex.
14
+ #HelpAppreciated: Work on iostream.h thread safety.
15
+ For more background see the discussions under
16
+ https://github.com/pybind/pybind11/pull/2982 and
17
+ https://github.com/pybind/pybind11/pull/2995.
18
+ */
19
+
20
+ #pragma once
21
+
22
+ #include "pybind11.h"
23
+
24
+ #include <algorithm>
25
+ #include <cstring>
26
+ #include <iostream>
27
+ #include <iterator>
28
+ #include <memory>
29
+ #include <ostream>
30
+ #include <streambuf>
31
+ #include <string>
32
+ #include <utility>
33
+
34
+ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
35
+ PYBIND11_NAMESPACE_BEGIN(detail)
36
+
37
+ // Buffer that writes to Python instead of C++
38
+ class pythonbuf : public std::streambuf {
39
+ private:
40
+ using traits_type = std::streambuf::traits_type;
41
+
42
+ const size_t buf_size;
43
+ std::unique_ptr<char[]> d_buffer;
44
+ object pywrite;
45
+ object pyflush;
46
+
47
+ int overflow(int c) override {
48
+ if (!traits_type::eq_int_type(c, traits_type::eof())) {
49
+ *pptr() = traits_type::to_char_type(c);
50
+ pbump(1);
51
+ }
52
+ return sync() == 0 ? traits_type::not_eof(c) : traits_type::eof();
53
+ }
54
+
55
+ // Computes how many bytes at the end of the buffer are part of an
56
+ // incomplete sequence of UTF-8 bytes.
57
+ // Precondition: pbase() < pptr()
58
+ size_t utf8_remainder() const {
59
+ const auto rbase = std::reverse_iterator<char *>(pbase());
60
+ const auto rpptr = std::reverse_iterator<char *>(pptr());
61
+ auto is_ascii = [](char c) { return (static_cast<unsigned char>(c) & 0x80) == 0x00; };
62
+ auto is_leading = [](char c) { return (static_cast<unsigned char>(c) & 0xC0) == 0xC0; };
63
+ auto is_leading_2b = [](char c) { return static_cast<unsigned char>(c) <= 0xDF; };
64
+ auto is_leading_3b = [](char c) { return static_cast<unsigned char>(c) <= 0xEF; };
65
+ // If the last character is ASCII, there are no incomplete code points
66
+ if (is_ascii(*rpptr)) {
67
+ return 0;
68
+ }
69
+ // Otherwise, work back from the end of the buffer and find the first
70
+ // UTF-8 leading byte
71
+ const auto rpend = rbase - rpptr >= 3 ? rpptr + 3 : rbase;
72
+ const auto leading = std::find_if(rpptr, rpend, is_leading);
73
+ if (leading == rbase) {
74
+ return 0;
75
+ }
76
+ const auto dist = static_cast<size_t>(leading - rpptr);
77
+ size_t remainder = 0;
78
+
79
+ if (dist == 0) {
80
+ remainder = 1; // 1-byte code point is impossible
81
+ } else if (dist == 1) {
82
+ remainder = is_leading_2b(*leading) ? 0 : dist + 1;
83
+ } else if (dist == 2) {
84
+ remainder = is_leading_3b(*leading) ? 0 : dist + 1;
85
+ }
86
+ // else if (dist >= 3), at least 4 bytes before encountering an UTF-8
87
+ // leading byte, either no remainder or invalid UTF-8.
88
+ // Invalid UTF-8 will cause an exception later when converting
89
+ // to a Python string, so that's not handled here.
90
+ return remainder;
91
+ }
92
+
93
+ // This function must be non-virtual to be called in a destructor.
94
+ int _sync() {
95
+ if (pbase() != pptr()) { // If buffer is not empty
96
+ gil_scoped_acquire tmp;
97
+ // This subtraction cannot be negative, so dropping the sign.
98
+ auto size = static_cast<size_t>(pptr() - pbase());
99
+ size_t remainder = utf8_remainder();
100
+
101
+ if (size > remainder) {
102
+ str line(pbase(), size - remainder);
103
+ pywrite(std::move(line));
104
+ pyflush();
105
+ }
106
+
107
+ // Copy the remainder at the end of the buffer to the beginning:
108
+ if (remainder > 0) {
109
+ std::memmove(pbase(), pptr() - remainder, remainder);
110
+ }
111
+ setp(pbase(), epptr());
112
+ pbump(static_cast<int>(remainder));
113
+ }
114
+ return 0;
115
+ }
116
+
117
+ int sync() override { return _sync(); }
118
+
119
+ public:
120
+ explicit pythonbuf(const object &pyostream, size_t buffer_size = 1024)
121
+ : buf_size(buffer_size), d_buffer(new char[buf_size]), pywrite(pyostream.attr("write")),
122
+ pyflush(pyostream.attr("flush")) {
123
+ setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
124
+ }
125
+
126
+ pythonbuf(pythonbuf &&) = default;
127
+
128
+ /// Sync before destroy
129
+ ~pythonbuf() override { _sync(); }
130
+ };
131
+
132
+ PYBIND11_NAMESPACE_END(detail)
133
+
134
+ /** \rst
135
+ This a move-only guard that redirects output.
136
+
137
+ .. code-block:: cpp
138
+
139
+ #include <pybind11/iostream.h>
140
+
141
+ ...
142
+
143
+ {
144
+ py::scoped_ostream_redirect output;
145
+ std::cout << "Hello, World!"; // Python stdout
146
+ } // <-- return std::cout to normal
147
+
148
+ You can explicitly pass the c++ stream and the python object,
149
+ for example to guard stderr instead.
150
+
151
+ .. code-block:: cpp
152
+
153
+ {
154
+ py::scoped_ostream_redirect output{
155
+ std::cerr, py::module::import("sys").attr("stderr")};
156
+ std::cout << "Hello, World!";
157
+ }
158
+ \endrst */
159
+ class scoped_ostream_redirect {
160
+ protected:
161
+ std::streambuf *old;
162
+ std::ostream &costream;
163
+ detail::pythonbuf buffer;
164
+
165
+ public:
166
+ explicit scoped_ostream_redirect(std::ostream &costream = std::cout,
167
+ const object &pyostream
168
+ = module_::import("sys").attr("stdout"))
169
+ : costream(costream), buffer(pyostream) {
170
+ old = costream.rdbuf(&buffer);
171
+ }
172
+
173
+ ~scoped_ostream_redirect() { costream.rdbuf(old); }
174
+
175
+ scoped_ostream_redirect(const scoped_ostream_redirect &) = delete;
176
+ scoped_ostream_redirect(scoped_ostream_redirect &&other) = default;
177
+ scoped_ostream_redirect &operator=(const scoped_ostream_redirect &) = delete;
178
+ scoped_ostream_redirect &operator=(scoped_ostream_redirect &&) = delete;
179
+ };
180
+
181
+ /** \rst
182
+ Like `scoped_ostream_redirect`, but redirects cerr by default. This class
183
+ is provided primary to make ``py::call_guard`` easier to make.
184
+
185
+ .. code-block:: cpp
186
+
187
+ m.def("noisy_func", &noisy_func,
188
+ py::call_guard<scoped_ostream_redirect,
189
+ scoped_estream_redirect>());
190
+
191
+ \endrst */
192
+ class scoped_estream_redirect : public scoped_ostream_redirect {
193
+ public:
194
+ explicit scoped_estream_redirect(std::ostream &costream = std::cerr,
195
+ const object &pyostream
196
+ = module_::import("sys").attr("stderr"))
197
+ : scoped_ostream_redirect(costream, pyostream) {}
198
+ };
199
+
200
+ PYBIND11_NAMESPACE_BEGIN(detail)
201
+
202
+ // Class to redirect output as a context manager. C++ backend.
203
+ class OstreamRedirect {
204
+ bool do_stdout_;
205
+ bool do_stderr_;
206
+ std::unique_ptr<scoped_ostream_redirect> redirect_stdout;
207
+ std::unique_ptr<scoped_estream_redirect> redirect_stderr;
208
+
209
+ public:
210
+ explicit OstreamRedirect(bool do_stdout = true, bool do_stderr = true)
211
+ : do_stdout_(do_stdout), do_stderr_(do_stderr) {}
212
+
213
+ void enter() {
214
+ if (do_stdout_) {
215
+ redirect_stdout.reset(new scoped_ostream_redirect());
216
+ }
217
+ if (do_stderr_) {
218
+ redirect_stderr.reset(new scoped_estream_redirect());
219
+ }
220
+ }
221
+
222
+ void exit() {
223
+ redirect_stdout.reset();
224
+ redirect_stderr.reset();
225
+ }
226
+ };
227
+
228
+ PYBIND11_NAMESPACE_END(detail)
229
+
230
+ /** \rst
231
+ This is a helper function to add a C++ redirect context manager to Python
232
+ instead of using a C++ guard. To use it, add the following to your binding code:
233
+
234
+ .. code-block:: cpp
235
+
236
+ #include <pybind11/iostream.h>
237
+
238
+ ...
239
+
240
+ py::add_ostream_redirect(m, "ostream_redirect");
241
+
242
+ You now have a Python context manager that redirects your output:
243
+
244
+ .. code-block:: python
245
+
246
+ with m.ostream_redirect():
247
+ m.print_to_cout_function()
248
+
249
+ This manager can optionally be told which streams to operate on:
250
+
251
+ .. code-block:: python
252
+
253
+ with m.ostream_redirect(stdout=true, stderr=true):
254
+ m.noisy_function_with_error_printing()
255
+
256
+ \endrst */
257
+ inline class_<detail::OstreamRedirect>
258
+ add_ostream_redirect(module_ m, const std::string &name = "ostream_redirect") {
259
+ return class_<detail::OstreamRedirect>(std::move(m), name.c_str(), module_local())
260
+ .def(init<bool, bool>(), arg("stdout") = true, arg("stderr") = true)
261
+ .def("__enter__", &detail::OstreamRedirect::enter)
262
+ .def("__exit__", [](detail::OstreamRedirect &self_, const args &) { self_.exit(); });
263
+ }
264
+
265
+ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
@@ -0,0 +1,67 @@
1
+ // Copyright (c) 2022-2025 The pybind Community.
2
+ // All rights reserved. Use of this source code is governed by a
3
+ // BSD-style license that can be found in the LICENSE file.
4
+
5
+ #pragma once
6
+
7
+ #include "detail/common.h"
8
+ #include "detail/native_enum_data.h"
9
+ #include "detail/type_caster_base.h"
10
+ #include "cast.h"
11
+
12
+ #include <cassert>
13
+ #include <limits>
14
+ #include <type_traits>
15
+ #include <typeindex>
16
+
17
+ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
18
+
19
+ /// Conversions between Python's native (stdlib) enum types and C++ enums.
20
+ template <typename EnumType>
21
+ class native_enum : public detail::native_enum_data {
22
+ public:
23
+ using Underlying = typename std::underlying_type<EnumType>::type;
24
+
25
+ native_enum(const object &parent_scope,
26
+ const char *name,
27
+ const char *native_type_name,
28
+ const char *class_doc = "")
29
+ : detail::native_enum_data(
30
+ parent_scope, name, native_type_name, class_doc, std::type_index(typeid(EnumType))) {
31
+ if (detail::get_local_type_info(typeid(EnumType)) != nullptr
32
+ || detail::get_global_type_info(typeid(EnumType)) != nullptr) {
33
+ pybind11_fail(
34
+ "pybind11::native_enum<...>(\"" + enum_name_encoded
35
+ + "\") is already registered as a `pybind11::enum_` or `pybind11::class_`!");
36
+ }
37
+ if (detail::global_internals_native_enum_type_map_contains(enum_type_index)) {
38
+ pybind11_fail("pybind11::native_enum<...>(\"" + enum_name_encoded
39
+ + "\") is already registered!");
40
+ }
41
+ arm_finalize_check();
42
+ }
43
+
44
+ /// Export enumeration entries into the parent scope
45
+ native_enum &export_values() {
46
+ assert(!export_values_flag); // Catch redundant calls.
47
+ export_values_flag = true;
48
+ return *this;
49
+ }
50
+
51
+ /// Add an enumeration entry
52
+ native_enum &value(char const *name, EnumType value, const char *doc = nullptr) {
53
+ // Disarm for the case that the native_enum_data dtor runs during exception unwinding.
54
+ disarm_finalize_check("value after finalize");
55
+ members.append(make_tuple(name, static_cast<Underlying>(value)));
56
+ if (doc) {
57
+ member_docs.append(make_tuple(name, doc));
58
+ }
59
+ arm_finalize_check(); // There was no exception.
60
+ return *this;
61
+ }
62
+
63
+ native_enum(const native_enum &) = delete;
64
+ native_enum &operator=(const native_enum &) = delete;
65
+ };
66
+
67
+ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)