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.
- netgen/NgOCC.py +7 -0
- netgen/__init__.py +114 -0
- netgen/__init__.pyi +22 -0
- netgen/__main__.py +53 -0
- netgen/cmake/NetgenConfig.cmake +79 -0
- netgen/cmake/netgen-targets-release.cmake +69 -0
- netgen/cmake/netgen-targets.cmake +146 -0
- netgen/config/__init__.py +1 -0
- netgen/config/__init__.pyi +52 -0
- netgen/config/__main__.py +4 -0
- netgen/config/config.py +68 -0
- netgen/config/config.pyi +54 -0
- netgen/csg.py +25 -0
- netgen/geom2d.py +178 -0
- netgen/gui.py +82 -0
- netgen/include/core/archive.hpp +1256 -0
- netgen/include/core/array.hpp +1760 -0
- netgen/include/core/autodiff.hpp +1131 -0
- netgen/include/core/autodiffdiff.hpp +733 -0
- netgen/include/core/bitarray.hpp +240 -0
- netgen/include/core/concurrentqueue.h +3619 -0
- netgen/include/core/exception.hpp +145 -0
- netgen/include/core/flags.hpp +199 -0
- netgen/include/core/hashtable.hpp +1281 -0
- netgen/include/core/localheap.hpp +318 -0
- netgen/include/core/logging.hpp +117 -0
- netgen/include/core/memtracer.hpp +221 -0
- netgen/include/core/mpi4py_pycapi.h +245 -0
- netgen/include/core/mpi_wrapper.hpp +643 -0
- netgen/include/core/ng_mpi.hpp +94 -0
- netgen/include/core/ng_mpi_generated_declarations.hpp +155 -0
- netgen/include/core/ng_mpi_native.hpp +25 -0
- netgen/include/core/ngcore.hpp +32 -0
- netgen/include/core/ngcore_api.hpp +152 -0
- netgen/include/core/ngstream.hpp +115 -0
- netgen/include/core/paje_trace.hpp +279 -0
- netgen/include/core/profiler.hpp +382 -0
- netgen/include/core/python_ngcore.hpp +457 -0
- netgen/include/core/ranges.hpp +109 -0
- netgen/include/core/register_archive.hpp +100 -0
- netgen/include/core/signal.hpp +82 -0
- netgen/include/core/simd.hpp +160 -0
- netgen/include/core/simd_arm64.hpp +407 -0
- netgen/include/core/simd_avx.hpp +394 -0
- netgen/include/core/simd_avx512.hpp +285 -0
- netgen/include/core/simd_generic.hpp +1053 -0
- netgen/include/core/simd_math.hpp +178 -0
- netgen/include/core/simd_sse.hpp +289 -0
- netgen/include/core/statushandler.hpp +37 -0
- netgen/include/core/symboltable.hpp +153 -0
- netgen/include/core/table.hpp +810 -0
- netgen/include/core/taskmanager.hpp +1161 -0
- netgen/include/core/type_traits.hpp +65 -0
- netgen/include/core/utils.hpp +385 -0
- netgen/include/core/version.hpp +102 -0
- netgen/include/core/xbool.hpp +47 -0
- netgen/include/csg/algprim.hpp +563 -0
- netgen/include/csg/brick.hpp +150 -0
- netgen/include/csg/csg.hpp +43 -0
- netgen/include/csg/csgeom.hpp +389 -0
- netgen/include/csg/csgparser.hpp +101 -0
- netgen/include/csg/curve2d.hpp +67 -0
- netgen/include/csg/edgeflw.hpp +112 -0
- netgen/include/csg/explicitcurve2d.hpp +113 -0
- netgen/include/csg/extrusion.hpp +185 -0
- netgen/include/csg/gencyl.hpp +70 -0
- netgen/include/csg/geoml.hpp +16 -0
- netgen/include/csg/identify.hpp +213 -0
- netgen/include/csg/manifold.hpp +29 -0
- netgen/include/csg/meshsurf.hpp +46 -0
- netgen/include/csg/polyhedra.hpp +121 -0
- netgen/include/csg/revolution.hpp +180 -0
- netgen/include/csg/singularref.hpp +84 -0
- netgen/include/csg/solid.hpp +295 -0
- netgen/include/csg/specpoin.hpp +194 -0
- netgen/include/csg/spline3d.hpp +99 -0
- netgen/include/csg/splinesurface.hpp +85 -0
- netgen/include/csg/surface.hpp +394 -0
- netgen/include/csg/triapprox.hpp +63 -0
- netgen/include/csg/vscsg.hpp +34 -0
- netgen/include/general/autodiff.hpp +356 -0
- netgen/include/general/autoptr.hpp +39 -0
- netgen/include/general/gzstream.h +121 -0
- netgen/include/general/hashtabl.hpp +1692 -0
- netgen/include/general/myadt.hpp +48 -0
- netgen/include/general/mystring.hpp +226 -0
- netgen/include/general/netgenout.hpp +205 -0
- netgen/include/general/ngarray.hpp +797 -0
- netgen/include/general/ngbitarray.hpp +149 -0
- netgen/include/general/ngpython.hpp +74 -0
- netgen/include/general/optmem.hpp +44 -0
- netgen/include/general/parthreads.hpp +138 -0
- netgen/include/general/seti.hpp +50 -0
- netgen/include/general/sort.hpp +47 -0
- netgen/include/general/spbita2d.hpp +59 -0
- netgen/include/general/stack.hpp +114 -0
- netgen/include/general/table.hpp +280 -0
- netgen/include/general/template.hpp +509 -0
- netgen/include/geom2d/csg2d.hpp +750 -0
- netgen/include/geom2d/geometry2d.hpp +280 -0
- netgen/include/geom2d/spline2d.hpp +234 -0
- netgen/include/geom2d/vsgeom2d.hpp +28 -0
- netgen/include/gprim/adtree.hpp +1392 -0
- netgen/include/gprim/geom2d.hpp +858 -0
- netgen/include/gprim/geom3d.hpp +749 -0
- netgen/include/gprim/geomfuncs.hpp +212 -0
- netgen/include/gprim/geomobjects.hpp +544 -0
- netgen/include/gprim/geomops.hpp +404 -0
- netgen/include/gprim/geomtest3d.hpp +101 -0
- netgen/include/gprim/gprim.hpp +33 -0
- netgen/include/gprim/spline.hpp +778 -0
- netgen/include/gprim/splinegeometry.hpp +73 -0
- netgen/include/gprim/transform3d.hpp +216 -0
- netgen/include/include/acisgeom.hpp +3 -0
- netgen/include/include/csg.hpp +1 -0
- netgen/include/include/geometry2d.hpp +1 -0
- netgen/include/include/gprim.hpp +1 -0
- netgen/include/include/incopengl.hpp +62 -0
- netgen/include/include/inctcl.hpp +13 -0
- netgen/include/include/incvis.hpp +6 -0
- netgen/include/include/linalg.hpp +1 -0
- netgen/include/include/meshing.hpp +1 -0
- netgen/include/include/myadt.hpp +1 -0
- netgen/include/include/mydefs.hpp +70 -0
- netgen/include/include/mystdlib.h +59 -0
- netgen/include/include/netgen_config.hpp +27 -0
- netgen/include/include/netgen_version.hpp +9 -0
- netgen/include/include/nginterface_v2_impl.hpp +395 -0
- netgen/include/include/ngsimd.hpp +1 -0
- netgen/include/include/occgeom.hpp +1 -0
- netgen/include/include/opti.hpp +1 -0
- netgen/include/include/parallel.hpp +1 -0
- netgen/include/include/stlgeom.hpp +1 -0
- netgen/include/include/visual.hpp +1 -0
- netgen/include/interface/rw_medit.hpp +11 -0
- netgen/include/interface/writeuser.hpp +80 -0
- netgen/include/linalg/densemat.hpp +414 -0
- netgen/include/linalg/linalg.hpp +29 -0
- netgen/include/linalg/opti.hpp +142 -0
- netgen/include/linalg/polynomial.hpp +47 -0
- netgen/include/linalg/vector.hpp +217 -0
- netgen/include/meshing/adfront2.hpp +274 -0
- netgen/include/meshing/adfront3.hpp +332 -0
- netgen/include/meshing/basegeom.hpp +370 -0
- netgen/include/meshing/bcfunctions.hpp +53 -0
- netgen/include/meshing/bisect.hpp +72 -0
- netgen/include/meshing/boundarylayer.hpp +113 -0
- netgen/include/meshing/classifyhpel.hpp +1984 -0
- netgen/include/meshing/clusters.hpp +46 -0
- netgen/include/meshing/curvedelems.hpp +274 -0
- netgen/include/meshing/delaunay2d.hpp +73 -0
- netgen/include/meshing/fieldlines.hpp +103 -0
- netgen/include/meshing/findip.hpp +198 -0
- netgen/include/meshing/findip2.hpp +103 -0
- netgen/include/meshing/geomsearch.hpp +69 -0
- netgen/include/meshing/global.hpp +54 -0
- netgen/include/meshing/hpref_hex.hpp +330 -0
- netgen/include/meshing/hpref_prism.hpp +3405 -0
- netgen/include/meshing/hpref_pyramid.hpp +154 -0
- netgen/include/meshing/hpref_quad.hpp +2082 -0
- netgen/include/meshing/hpref_segm.hpp +122 -0
- netgen/include/meshing/hpref_tet.hpp +4230 -0
- netgen/include/meshing/hpref_trig.hpp +848 -0
- netgen/include/meshing/hprefinement.hpp +366 -0
- netgen/include/meshing/improve2.hpp +178 -0
- netgen/include/meshing/improve3.hpp +151 -0
- netgen/include/meshing/localh.hpp +223 -0
- netgen/include/meshing/meshclass.hpp +1076 -0
- netgen/include/meshing/meshfunc.hpp +47 -0
- netgen/include/meshing/meshing.hpp +63 -0
- netgen/include/meshing/meshing2.hpp +163 -0
- netgen/include/meshing/meshing3.hpp +123 -0
- netgen/include/meshing/meshtool.hpp +90 -0
- netgen/include/meshing/meshtype.hpp +1930 -0
- netgen/include/meshing/msghandler.hpp +62 -0
- netgen/include/meshing/paralleltop.hpp +172 -0
- netgen/include/meshing/python_mesh.hpp +206 -0
- netgen/include/meshing/ruler2.hpp +172 -0
- netgen/include/meshing/ruler3.hpp +211 -0
- netgen/include/meshing/soldata.hpp +141 -0
- netgen/include/meshing/specials.hpp +17 -0
- netgen/include/meshing/surfacegeom.hpp +73 -0
- netgen/include/meshing/topology.hpp +1003 -0
- netgen/include/meshing/validate.hpp +21 -0
- netgen/include/meshing/visual_interface.hpp +71 -0
- netgen/include/mydefs.hpp +70 -0
- netgen/include/nginterface.h +474 -0
- netgen/include/nginterface_v2.hpp +406 -0
- netgen/include/nglib.h +697 -0
- netgen/include/nglib_occ.h +50 -0
- netgen/include/occ/occ_edge.hpp +47 -0
- netgen/include/occ/occ_face.hpp +52 -0
- netgen/include/occ/occ_solid.hpp +23 -0
- netgen/include/occ/occ_utils.hpp +376 -0
- netgen/include/occ/occ_vertex.hpp +30 -0
- netgen/include/occ/occgeom.hpp +659 -0
- netgen/include/occ/occmeshsurf.hpp +168 -0
- netgen/include/occ/vsocc.hpp +33 -0
- netgen/include/pybind11/LICENSE +29 -0
- netgen/include/pybind11/attr.h +722 -0
- netgen/include/pybind11/buffer_info.h +208 -0
- netgen/include/pybind11/cast.h +2361 -0
- netgen/include/pybind11/chrono.h +228 -0
- netgen/include/pybind11/common.h +2 -0
- netgen/include/pybind11/complex.h +74 -0
- netgen/include/pybind11/conduit/README.txt +15 -0
- netgen/include/pybind11/conduit/pybind11_conduit_v1.h +116 -0
- netgen/include/pybind11/conduit/pybind11_platform_abi_id.h +87 -0
- netgen/include/pybind11/conduit/wrap_include_python_h.h +72 -0
- netgen/include/pybind11/critical_section.h +56 -0
- netgen/include/pybind11/detail/class.h +823 -0
- netgen/include/pybind11/detail/common.h +1348 -0
- netgen/include/pybind11/detail/cpp_conduit.h +75 -0
- netgen/include/pybind11/detail/descr.h +226 -0
- netgen/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h +39 -0
- netgen/include/pybind11/detail/exception_translation.h +71 -0
- netgen/include/pybind11/detail/function_record_pyobject.h +191 -0
- netgen/include/pybind11/detail/init.h +538 -0
- netgen/include/pybind11/detail/internals.h +799 -0
- netgen/include/pybind11/detail/native_enum_data.h +209 -0
- netgen/include/pybind11/detail/pybind11_namespace_macros.h +82 -0
- netgen/include/pybind11/detail/struct_smart_holder.h +378 -0
- netgen/include/pybind11/detail/type_caster_base.h +1591 -0
- netgen/include/pybind11/detail/typeid.h +65 -0
- netgen/include/pybind11/detail/using_smart_holder.h +22 -0
- netgen/include/pybind11/detail/value_and_holder.h +90 -0
- netgen/include/pybind11/eigen/common.h +9 -0
- netgen/include/pybind11/eigen/matrix.h +723 -0
- netgen/include/pybind11/eigen/tensor.h +521 -0
- netgen/include/pybind11/eigen.h +12 -0
- netgen/include/pybind11/embed.h +320 -0
- netgen/include/pybind11/eval.h +161 -0
- netgen/include/pybind11/functional.h +147 -0
- netgen/include/pybind11/gil.h +199 -0
- netgen/include/pybind11/gil_safe_call_once.h +102 -0
- netgen/include/pybind11/gil_simple.h +37 -0
- netgen/include/pybind11/iostream.h +265 -0
- netgen/include/pybind11/native_enum.h +67 -0
- netgen/include/pybind11/numpy.h +2312 -0
- netgen/include/pybind11/operators.h +202 -0
- netgen/include/pybind11/options.h +92 -0
- netgen/include/pybind11/pybind11.h +3645 -0
- netgen/include/pybind11/pytypes.h +2680 -0
- netgen/include/pybind11/stl/filesystem.h +114 -0
- netgen/include/pybind11/stl.h +666 -0
- netgen/include/pybind11/stl_bind.h +858 -0
- netgen/include/pybind11/subinterpreter.h +299 -0
- netgen/include/pybind11/trampoline_self_life_support.h +65 -0
- netgen/include/pybind11/type_caster_pyobject_ptr.h +61 -0
- netgen/include/pybind11/typing.h +298 -0
- netgen/include/pybind11/warnings.h +75 -0
- netgen/include/stlgeom/meshstlsurface.hpp +67 -0
- netgen/include/stlgeom/stlgeom.hpp +491 -0
- netgen/include/stlgeom/stlline.hpp +193 -0
- netgen/include/stlgeom/stltool.hpp +331 -0
- netgen/include/stlgeom/stltopology.hpp +419 -0
- netgen/include/stlgeom/vsstl.hpp +58 -0
- netgen/include/visualization/meshdoc.hpp +42 -0
- netgen/include/visualization/mvdraw.hpp +325 -0
- netgen/include/visualization/vispar.hpp +128 -0
- netgen/include/visualization/visual.hpp +28 -0
- netgen/include/visualization/visual_api.hpp +10 -0
- netgen/include/visualization/vssolution.hpp +399 -0
- netgen/lib/libnggui.lib +0 -0
- netgen/lib/ngcore.lib +0 -0
- netgen/lib/nglib.lib +0 -0
- netgen/lib/togl.lib +0 -0
- netgen/libnggui.dll +0 -0
- netgen/libngguipy.lib +0 -0
- netgen/libngguipy.pyd +0 -0
- netgen/libngpy/_NgOCC.pyi +1545 -0
- netgen/libngpy/__init__.pyi +7 -0
- netgen/libngpy/_csg.pyi +259 -0
- netgen/libngpy/_geom2d.pyi +323 -0
- netgen/libngpy/_meshing.pyi +1111 -0
- netgen/libngpy/_stl.pyi +131 -0
- netgen/libngpy.lib +0 -0
- netgen/libngpy.pyd +0 -0
- netgen/meshing.py +65 -0
- netgen/ngcore.dll +0 -0
- netgen/nglib.dll +0 -0
- netgen/occ.py +52 -0
- netgen/read_gmsh.py +259 -0
- netgen/read_meshio.py +22 -0
- netgen/stl.py +2 -0
- netgen/togl.dll +0 -0
- netgen/version.py +2 -0
- netgen/webgui.py +529 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/boundarycondition.geo +16 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/boxcyl.geo +32 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/circle_on_cube.geo +27 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cone.geo +13 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cube.geo +16 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubeandring.geo +55 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubeandspheres.geo +21 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubemcyl.geo +18 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubemsphere.geo +19 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cylinder.geo +12 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cylsphere.geo +12 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/doc/ng4.pdf +0 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/ellipsoid.geo +8 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/ellipticcyl.geo +10 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/extrusion.geo +99 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/fichera.geo +24 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/frame.step +11683 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/hinge.stl +8486 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/lshape3d.geo +26 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/manyholes.geo +26 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/manyholes2.geo +26 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/matrix.geo +27 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/ortho.geo +11 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/part1.stl +2662 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/period.geo +33 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/py_tutorials/exportNeutral.py +26 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/py_tutorials/mesh.py +19 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/py_tutorials/shaft.geo +65 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/revolution.geo +18 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/screw.step +1694 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/sculpture.geo +13 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/shaft.geo +65 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/shell.geo +10 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/sphere.geo +8 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/sphereincube.geo +17 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/square.in2d +35 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/squarecircle.in2d +48 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/squarehole.in2d +47 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/torus.geo +8 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/trafo.geo +57 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/twobricks.geo +15 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/twocubes.geo +18 -0
- netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/twocyl.geo +16 -0
- netgen_mesher-6.2.2506.post35.dev0.dist-info/METADATA +15 -0
- netgen_mesher-6.2.2506.post35.dev0.dist-info/RECORD +340 -0
- netgen_mesher-6.2.2506.post35.dev0.dist-info/WHEEL +5 -0
- netgen_mesher-6.2.2506.post35.dev0.dist-info/entry_points.txt +2 -0
- netgen_mesher-6.2.2506.post35.dev0.dist-info/licenses/AUTHORS +1 -0
- netgen_mesher-6.2.2506.post35.dev0.dist-info/licenses/LICENSE +504 -0
- netgen_mesher-6.2.2506.post35.dev0.dist-info/top_level.txt +2 -0
- pyngcore/__init__.py +1 -0
- 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)
|