netgen-mesher 6.2.2504.post11.dev0__cp313-cp313-win_amd64.whl → 6.2.2506.post48.dev0__cp313-cp313-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/__init__.pyi +3 -3
- netgen/cmake/NetgenConfig.cmake +10 -9
- netgen/config/__init__.pyi +8 -8
- netgen/config/config.py +7 -7
- netgen/config/config.pyi +8 -8
- netgen/include/core/archive.hpp +18 -3
- netgen/include/core/array.hpp +20 -4
- netgen/include/core/autodiff.hpp +9 -11
- netgen/include/core/autodiffdiff.hpp +0 -2
- netgen/include/core/bitarray.hpp +1 -1
- netgen/include/core/flags.hpp +1 -1
- netgen/include/core/hashtable.hpp +1 -1
- netgen/include/core/memtracer.hpp +7 -7
- netgen/include/core/ngcore.hpp +5 -0
- netgen/include/core/ngcore_api.hpp +11 -0
- netgen/include/core/paje_trace.hpp +9 -8
- netgen/include/core/profiler.hpp +5 -5
- netgen/include/core/register_archive.hpp +8 -0
- netgen/include/core/simd.hpp +69 -1
- netgen/include/core/simd_arm64.hpp +205 -1
- netgen/include/core/simd_avx.hpp +72 -4
- netgen/include/core/simd_avx512.hpp +9 -0
- netgen/include/core/simd_generic.hpp +274 -8
- netgen/include/core/simd_math.hpp +178 -0
- netgen/include/core/simd_sse.hpp +11 -1
- netgen/include/core/statushandler.hpp +37 -0
- netgen/include/core/table.hpp +3 -2
- netgen/include/core/taskmanager.hpp +34 -1
- netgen/include/core/utils.hpp +3 -8
- netgen/include/include/netgen_version.hpp +4 -4
- netgen/include/meshing/basegeom.hpp +1 -4
- netgen/include/meshing/global.hpp +0 -17
- netgen/include/meshing/hpref_tet.hpp +41 -0
- netgen/include/meshing/hprefinement.hpp +2 -0
- netgen/include/meshing/meshtype.hpp +2 -1
- netgen/include/meshing/msghandler.hpp +9 -6
- netgen/include/meshing/topology.hpp +2 -2
- netgen/include/nginterface.h +3 -2
- netgen/include/occ/occ_utils.hpp +26 -0
- netgen/include/occ/occgeom.hpp +8 -0
- netgen/include/pybind11/attr.h +40 -8
- netgen/include/pybind11/buffer_info.h +14 -14
- netgen/include/pybind11/cast.h +553 -29
- netgen/include/pybind11/chrono.h +4 -1
- 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 +172 -97
- netgen/include/pybind11/detail/common.h +270 -189
- netgen/include/pybind11/detail/cpp_conduit.h +75 -0
- netgen/include/pybind11/detail/descr.h +55 -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 +113 -9
- netgen/include/pybind11/detail/internals.h +479 -344
- 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 +506 -133
- netgen/include/pybind11/detail/using_smart_holder.h +22 -0
- netgen/include/pybind11/detail/value_and_holder.h +90 -0
- netgen/include/pybind11/eigen/matrix.h +19 -10
- netgen/include/pybind11/eigen/tensor.h +15 -11
- netgen/include/pybind11/embed.h +50 -46
- netgen/include/pybind11/eval.h +11 -6
- netgen/include/pybind11/functional.h +58 -49
- netgen/include/pybind11/gil.h +34 -82
- netgen/include/pybind11/gil_safe_call_once.h +12 -1
- netgen/include/pybind11/gil_simple.h +37 -0
- netgen/include/pybind11/native_enum.h +67 -0
- netgen/include/pybind11/numpy.h +272 -93
- netgen/include/pybind11/pybind11.h +947 -265
- netgen/include/pybind11/pytypes.h +127 -21
- netgen/include/pybind11/stl/filesystem.h +23 -25
- netgen/include/pybind11/stl.h +277 -59
- netgen/include/pybind11/stl_bind.h +42 -7
- netgen/include/pybind11/subinterpreter.h +299 -0
- netgen/include/pybind11/trampoline_self_life_support.h +65 -0
- netgen/include/pybind11/typing.h +177 -4
- netgen/include/pybind11/warnings.h +75 -0
- netgen/include/visualization/mvdraw.hpp +48 -12
- netgen/include/visualization/vssolution.hpp +3 -1
- netgen/lib/libnggui.lib +0 -0
- netgen/lib/ngcore.lib +0 -0
- netgen/lib/nglib.lib +0 -0
- netgen/libnggui.dll +0 -0
- netgen/libngguipy.pyd +0 -0
- netgen/libngpy/_NgOCC.pyi +224 -139
- netgen/libngpy/_csg.pyi +26 -26
- netgen/libngpy/_geom2d.pyi +34 -25
- netgen/libngpy/_meshing.pyi +262 -111
- netgen/libngpy/_stl.pyi +3 -4
- netgen/libngpy.pyd +0 -0
- netgen/ngcore.dll +0 -0
- netgen/nglib.dll +0 -0
- netgen/read_gmsh.py +41 -0
- netgen/togl.dll +0 -0
- netgen/version.py +1 -1
- netgen/webgui.py +38 -2
- {netgen_mesher-6.2.2504.post11.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/METADATA +2 -1
- {netgen_mesher-6.2.2504.post11.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/RECORD +153 -132
- pyngcore/pyngcore.cp313-win_amd64.pyd +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/boundarycondition.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/boxcyl.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/circle_on_cube.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cone.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cube.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cubeandring.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cubeandspheres.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cubemcyl.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cubemsphere.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cylinder.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cylsphere.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/doc/ng4.pdf +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/ellipsoid.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/ellipticcyl.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/extrusion.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/fichera.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/frame.step +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/hinge.stl +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/lshape3d.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/manyholes.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/manyholes2.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/matrix.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/ortho.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/part1.stl +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/period.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/py_tutorials/exportNeutral.py +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/py_tutorials/mesh.py +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/py_tutorials/shaft.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/revolution.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/screw.step +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/sculpture.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/shaft.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/shell.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/sphere.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/sphereincube.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/square.in2d +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/squarecircle.in2d +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/squarehole.in2d +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/torus.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/trafo.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/twobricks.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/twocubes.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/twocyl.geo +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/AUTHORS +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/LICENSE +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/WHEEL +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/entry_points.txt +0 -0
- {netgen_mesher-6.2.2504.post11.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/*
|
|
2
|
+
pybind11/subinterpreter.h: Support for creating and using subinterpreters
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2025 The Pybind Development Team.
|
|
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
|
+
#include "detail/common.h"
|
|
13
|
+
#include "detail/internals.h"
|
|
14
|
+
#include "gil.h"
|
|
15
|
+
|
|
16
|
+
#include <stdexcept>
|
|
17
|
+
|
|
18
|
+
#ifndef PYBIND11_HAS_SUBINTERPRETER_SUPPORT
|
|
19
|
+
# error "This platform does not support subinterpreters, do not include this file."
|
|
20
|
+
#endif
|
|
21
|
+
|
|
22
|
+
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
23
|
+
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
24
|
+
inline PyInterpreterState *get_interpreter_state_unchecked() {
|
|
25
|
+
auto cur_tstate = get_thread_state_unchecked();
|
|
26
|
+
if (cur_tstate)
|
|
27
|
+
return cur_tstate->interp;
|
|
28
|
+
else
|
|
29
|
+
return nullptr;
|
|
30
|
+
}
|
|
31
|
+
PYBIND11_NAMESPACE_END(detail)
|
|
32
|
+
|
|
33
|
+
class subinterpreter;
|
|
34
|
+
|
|
35
|
+
/// Activate the subinterpreter and acquire its GIL, while also releasing any GIL and interpreter
|
|
36
|
+
/// currently held. Upon exiting the scope, the previous subinterpreter (if any) and its
|
|
37
|
+
/// associated GIL are restored to their state as they were before the scope was entered.
|
|
38
|
+
class subinterpreter_scoped_activate {
|
|
39
|
+
public:
|
|
40
|
+
explicit subinterpreter_scoped_activate(subinterpreter const &si);
|
|
41
|
+
~subinterpreter_scoped_activate();
|
|
42
|
+
|
|
43
|
+
subinterpreter_scoped_activate(subinterpreter_scoped_activate &&) = delete;
|
|
44
|
+
subinterpreter_scoped_activate(subinterpreter_scoped_activate const &) = delete;
|
|
45
|
+
subinterpreter_scoped_activate &operator=(subinterpreter_scoped_activate &) = delete;
|
|
46
|
+
subinterpreter_scoped_activate &operator=(subinterpreter_scoped_activate const &) = delete;
|
|
47
|
+
|
|
48
|
+
private:
|
|
49
|
+
PyThreadState *old_tstate_ = nullptr;
|
|
50
|
+
PyThreadState *tstate_ = nullptr;
|
|
51
|
+
PyGILState_STATE gil_state_;
|
|
52
|
+
bool simple_gil_ = false;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/// Holds a Python subinterpreter instance
|
|
56
|
+
class subinterpreter {
|
|
57
|
+
public:
|
|
58
|
+
/// empty/unusable, but move-assignable. use create() to create a subinterpreter.
|
|
59
|
+
subinterpreter() = default;
|
|
60
|
+
|
|
61
|
+
subinterpreter(subinterpreter const ©) = delete;
|
|
62
|
+
subinterpreter &operator=(subinterpreter const ©) = delete;
|
|
63
|
+
|
|
64
|
+
subinterpreter(subinterpreter &&old) noexcept
|
|
65
|
+
: istate_(old.istate_), creation_tstate_(old.creation_tstate_) {
|
|
66
|
+
old.istate_ = nullptr;
|
|
67
|
+
old.creation_tstate_ = nullptr;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
subinterpreter &operator=(subinterpreter &&old) noexcept {
|
|
71
|
+
std::swap(old.istate_, istate_);
|
|
72
|
+
std::swap(old.creation_tstate_, creation_tstate_);
|
|
73
|
+
return *this;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// Create a new subinterpreter with the specified configuration
|
|
77
|
+
/// @note This function acquires (and then releases) the main interpreter GIL, but the main
|
|
78
|
+
/// interpreter and its GIL are not required to be held prior to calling this function.
|
|
79
|
+
static inline subinterpreter create(PyInterpreterConfig const &cfg) {
|
|
80
|
+
|
|
81
|
+
error_scope err_scope;
|
|
82
|
+
subinterpreter result;
|
|
83
|
+
{
|
|
84
|
+
// we must hold the main GIL in order to create a subinterpreter
|
|
85
|
+
subinterpreter_scoped_activate main_guard(main());
|
|
86
|
+
|
|
87
|
+
auto prev_tstate = PyThreadState_Get();
|
|
88
|
+
|
|
89
|
+
PyStatus status;
|
|
90
|
+
|
|
91
|
+
{
|
|
92
|
+
/*
|
|
93
|
+
Several internal CPython modules are lacking proper subinterpreter support in 3.12
|
|
94
|
+
even though it is "stable" in that version. This most commonly seems to cause
|
|
95
|
+
crashes when two interpreters concurrently initialize, which imports several things
|
|
96
|
+
(like builtins, unicode, codecs).
|
|
97
|
+
*/
|
|
98
|
+
#if PY_VERSION_HEX < 0x030D0000 && defined(Py_MOD_PER_INTERPRETER_GIL_SUPPORTED)
|
|
99
|
+
static std::mutex one_at_a_time;
|
|
100
|
+
std::lock_guard<std::mutex> guard(one_at_a_time);
|
|
101
|
+
#endif
|
|
102
|
+
status = Py_NewInterpreterFromConfig(&result.creation_tstate_, &cfg);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// this doesn't raise a normal Python exception, it provides an exit() status code.
|
|
106
|
+
if (PyStatus_Exception(status)) {
|
|
107
|
+
pybind11_fail("failed to create new sub-interpreter");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// upon success, the new interpreter is activated in this thread
|
|
111
|
+
result.istate_ = result.creation_tstate_->interp;
|
|
112
|
+
detail::get_num_interpreters_seen() += 1; // there are now many interpreters
|
|
113
|
+
detail::get_internals(); // initialize internals.tstate, amongst other things...
|
|
114
|
+
|
|
115
|
+
// In 3.13+ this state should be deleted right away, and the memory will be reused for
|
|
116
|
+
// the next threadstate on this interpreter. However, on 3.12 we cannot do that, we
|
|
117
|
+
// must keep it around (but not use it) ... see destructor.
|
|
118
|
+
#if PY_VERSION_HEX >= 0x030D0000
|
|
119
|
+
PyThreadState_Clear(result.creation_tstate_);
|
|
120
|
+
PyThreadState_DeleteCurrent();
|
|
121
|
+
#endif
|
|
122
|
+
|
|
123
|
+
// we have to switch back to main, and then the scopes will handle cleanup
|
|
124
|
+
PyThreadState_Swap(prev_tstate);
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/// Calls create() with a default configuration of an isolated interpreter that disallows fork,
|
|
130
|
+
/// exec, and Python threads.
|
|
131
|
+
static inline subinterpreter create() {
|
|
132
|
+
// same as the default config in the python docs
|
|
133
|
+
PyInterpreterConfig cfg;
|
|
134
|
+
std::memset(&cfg, 0, sizeof(cfg));
|
|
135
|
+
cfg.allow_threads = 1;
|
|
136
|
+
cfg.check_multi_interp_extensions = 1;
|
|
137
|
+
cfg.gil = PyInterpreterConfig_OWN_GIL;
|
|
138
|
+
return create(cfg);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
~subinterpreter() {
|
|
142
|
+
if (!creation_tstate_) {
|
|
143
|
+
// non-owning wrapper, do nothing.
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
PyThreadState *destroy_tstate;
|
|
148
|
+
PyThreadState *old_tstate;
|
|
149
|
+
|
|
150
|
+
// Python 3.12 requires us to keep the original PyThreadState alive until we are ready to
|
|
151
|
+
// destroy the interpreter. We prefer to use that to destroy the interpreter.
|
|
152
|
+
#if PY_VERSION_HEX < 0x030D0000
|
|
153
|
+
// The tstate passed to Py_EndInterpreter MUST have been created on the current OS thread.
|
|
154
|
+
bool same_thread = false;
|
|
155
|
+
# ifdef PY_HAVE_THREAD_NATIVE_ID
|
|
156
|
+
same_thread = PyThread_get_thread_native_id() == creation_tstate_->native_thread_id;
|
|
157
|
+
# endif
|
|
158
|
+
if (same_thread) {
|
|
159
|
+
// OK it is safe to use the creation state here
|
|
160
|
+
destroy_tstate = creation_tstate_;
|
|
161
|
+
old_tstate = PyThreadState_Swap(destroy_tstate);
|
|
162
|
+
} else {
|
|
163
|
+
// We have to make a new tstate on this thread and use that.
|
|
164
|
+
destroy_tstate = PyThreadState_New(istate_);
|
|
165
|
+
old_tstate = PyThreadState_Swap(destroy_tstate);
|
|
166
|
+
|
|
167
|
+
// We can use the one we just created, so we must delete the creation state.
|
|
168
|
+
PyThreadState_Clear(creation_tstate_);
|
|
169
|
+
PyThreadState_Delete(creation_tstate_);
|
|
170
|
+
}
|
|
171
|
+
#else
|
|
172
|
+
destroy_tstate = PyThreadState_New(istate_);
|
|
173
|
+
old_tstate = PyThreadState_Swap(destroy_tstate);
|
|
174
|
+
#endif
|
|
175
|
+
|
|
176
|
+
bool switch_back = old_tstate && old_tstate->interp != istate_;
|
|
177
|
+
|
|
178
|
+
// Internals always exists in the subinterpreter, this class enforces it when it creates
|
|
179
|
+
// the subinterpreter. Even if it didn't, this only creates the pointer-to-pointer, not the
|
|
180
|
+
// internals themselves.
|
|
181
|
+
detail::get_internals_pp_manager().get_pp();
|
|
182
|
+
detail::get_local_internals_pp_manager().get_pp();
|
|
183
|
+
|
|
184
|
+
// End it
|
|
185
|
+
Py_EndInterpreter(destroy_tstate);
|
|
186
|
+
|
|
187
|
+
// It's possible for the internals to be created during endinterpreter (e.g. if a
|
|
188
|
+
// py::capsule calls `get_internals()` during destruction), so we destroy afterward.
|
|
189
|
+
detail::get_internals_pp_manager().destroy();
|
|
190
|
+
detail::get_local_internals_pp_manager().destroy();
|
|
191
|
+
|
|
192
|
+
// switch back to the old tstate and old GIL (if there was one)
|
|
193
|
+
if (switch_back)
|
|
194
|
+
PyThreadState_Swap(old_tstate);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/// Get a handle to the main interpreter that can be used with subinterpreter_scoped_activate
|
|
198
|
+
/// Note that destructing the handle is a noop, the main interpreter can only be ended by
|
|
199
|
+
/// py::finalize_interpreter()
|
|
200
|
+
static subinterpreter main() {
|
|
201
|
+
subinterpreter m;
|
|
202
|
+
m.istate_ = PyInterpreterState_Main();
|
|
203
|
+
m.disarm(); // make destruct a noop
|
|
204
|
+
return m;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/// Get a non-owning wrapper of the currently active interpreter (if any)
|
|
208
|
+
static subinterpreter current() {
|
|
209
|
+
subinterpreter c;
|
|
210
|
+
c.istate_ = detail::get_interpreter_state_unchecked();
|
|
211
|
+
c.disarm(); // make destruct a noop, we don't own this...
|
|
212
|
+
return c;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// Get the numerical identifier for the sub-interpreter
|
|
216
|
+
int64_t id() const {
|
|
217
|
+
if (istate_ != nullptr)
|
|
218
|
+
return PyInterpreterState_GetID(istate_);
|
|
219
|
+
else
|
|
220
|
+
return -1; // CPython uses one-up numbers from 0, so negative should be safe to return
|
|
221
|
+
// here.
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/// Get the interpreter's state dict. This interpreter's GIL must be held before calling!
|
|
225
|
+
dict state_dict() { return reinterpret_borrow<dict>(PyInterpreterState_GetDict(istate_)); }
|
|
226
|
+
|
|
227
|
+
/// abandon cleanup of this subinterpreter (leak it). this might be needed during
|
|
228
|
+
/// finalization...
|
|
229
|
+
void disarm() { creation_tstate_ = nullptr; }
|
|
230
|
+
|
|
231
|
+
/// An empty wrapper cannot be activated
|
|
232
|
+
bool empty() const { return istate_ == nullptr; }
|
|
233
|
+
|
|
234
|
+
/// Is this wrapper non-empty
|
|
235
|
+
explicit operator bool() const { return !empty(); }
|
|
236
|
+
|
|
237
|
+
private:
|
|
238
|
+
friend class subinterpreter_scoped_activate;
|
|
239
|
+
PyInterpreterState *istate_ = nullptr;
|
|
240
|
+
PyThreadState *creation_tstate_ = nullptr;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
class scoped_subinterpreter {
|
|
244
|
+
public:
|
|
245
|
+
scoped_subinterpreter() : si_(subinterpreter::create()), scope_(si_) {}
|
|
246
|
+
|
|
247
|
+
explicit scoped_subinterpreter(PyInterpreterConfig const &cfg)
|
|
248
|
+
: si_(subinterpreter::create(cfg)), scope_(si_) {}
|
|
249
|
+
|
|
250
|
+
private:
|
|
251
|
+
subinterpreter si_;
|
|
252
|
+
subinterpreter_scoped_activate scope_;
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
inline subinterpreter_scoped_activate::subinterpreter_scoped_activate(subinterpreter const &si) {
|
|
256
|
+
if (!si.istate_) {
|
|
257
|
+
pybind11_fail("null subinterpreter");
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (detail::get_interpreter_state_unchecked() == si.istate_) {
|
|
261
|
+
// we are already on this interpreter, make sure we hold the GIL
|
|
262
|
+
simple_gil_ = true;
|
|
263
|
+
gil_state_ = PyGILState_Ensure();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// we can't really interact with the interpreter at all until we switch to it
|
|
268
|
+
// not even to, for example, look in its state dict or touch its internals
|
|
269
|
+
tstate_ = PyThreadState_New(si.istate_);
|
|
270
|
+
|
|
271
|
+
// make the interpreter active and acquire the GIL
|
|
272
|
+
old_tstate_ = PyThreadState_Swap(tstate_);
|
|
273
|
+
|
|
274
|
+
// save this in internals for scoped_gil calls
|
|
275
|
+
detail::get_internals().tstate = tstate_;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
inline subinterpreter_scoped_activate::~subinterpreter_scoped_activate() {
|
|
279
|
+
if (simple_gil_) {
|
|
280
|
+
// We were on this interpreter already, so just make sure the GIL goes back as it was
|
|
281
|
+
PyGILState_Release(gil_state_);
|
|
282
|
+
} else {
|
|
283
|
+
if (tstate_) {
|
|
284
|
+
#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
|
|
285
|
+
if (detail::get_thread_state_unchecked() != tstate_) {
|
|
286
|
+
pybind11_fail("~subinterpreter_scoped_activate: thread state must be current!");
|
|
287
|
+
}
|
|
288
|
+
#endif
|
|
289
|
+
detail::get_internals().tstate.reset();
|
|
290
|
+
PyThreadState_Clear(tstate_);
|
|
291
|
+
PyThreadState_DeleteCurrent();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Go back the previous interpreter (if any) and acquire THAT gil
|
|
295
|
+
PyThreadState_Swap(old_tstate_);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Copyright (c) 2021 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
|
+
#include "detail/using_smart_holder.h"
|
|
9
|
+
#include "detail/value_and_holder.h"
|
|
10
|
+
|
|
11
|
+
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
12
|
+
|
|
13
|
+
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
14
|
+
// PYBIND11:REMINDER: Needs refactoring of existing pybind11 code.
|
|
15
|
+
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo);
|
|
16
|
+
PYBIND11_NAMESPACE_END(detail)
|
|
17
|
+
|
|
18
|
+
// The original core idea for this struct goes back to PyCLIF:
|
|
19
|
+
// https://github.com/google/clif/blob/07f95d7e69dca2fcf7022978a55ef3acff506c19/clif/python/runtime.cc#L37
|
|
20
|
+
// URL provided here mainly to give proper credit.
|
|
21
|
+
struct trampoline_self_life_support {
|
|
22
|
+
// NOTE: PYBIND11_INTERNALS_VERSION needs to be bumped if changes are made to this struct.
|
|
23
|
+
detail::value_and_holder v_h;
|
|
24
|
+
|
|
25
|
+
trampoline_self_life_support() = default;
|
|
26
|
+
|
|
27
|
+
void activate_life_support(const detail::value_and_holder &v_h_) {
|
|
28
|
+
Py_INCREF((PyObject *) v_h_.inst);
|
|
29
|
+
v_h = v_h_;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
void deactivate_life_support() {
|
|
33
|
+
Py_DECREF((PyObject *) v_h.inst);
|
|
34
|
+
v_h = detail::value_and_holder();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
~trampoline_self_life_support() {
|
|
38
|
+
if (v_h.inst != nullptr && v_h.vh != nullptr) {
|
|
39
|
+
void *value_void_ptr = v_h.value_ptr();
|
|
40
|
+
if (value_void_ptr != nullptr) {
|
|
41
|
+
PyGILState_STATE threadstate = PyGILState_Ensure();
|
|
42
|
+
v_h.value_ptr() = nullptr;
|
|
43
|
+
v_h.holder<smart_holder>().release_disowned();
|
|
44
|
+
detail::deregister_instance(v_h.inst, value_void_ptr, v_h.type);
|
|
45
|
+
Py_DECREF((PyObject *) v_h.inst); // Must be after deregister.
|
|
46
|
+
PyGILState_Release(threadstate);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// For the next two, the default implementations generate undefined behavior (ASAN failures
|
|
52
|
+
// manually verified). The reason is that v_h needs to be kept default-initialized.
|
|
53
|
+
trampoline_self_life_support(const trampoline_self_life_support &) {}
|
|
54
|
+
trampoline_self_life_support(trampoline_self_life_support &&) noexcept {}
|
|
55
|
+
|
|
56
|
+
// These should never be needed (please provide test cases if you think they are).
|
|
57
|
+
trampoline_self_life_support &operator=(const trampoline_self_life_support &) = delete;
|
|
58
|
+
trampoline_self_life_support &operator=(trampoline_self_life_support &&) = delete;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
62
|
+
using get_trampoline_self_life_support_fn = trampoline_self_life_support *(*) (void *);
|
|
63
|
+
PYBIND11_NAMESPACE_END(detail)
|
|
64
|
+
|
|
65
|
+
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|
netgen/include/pybind11/typing.h
CHANGED
|
@@ -14,6 +14,15 @@
|
|
|
14
14
|
#include "cast.h"
|
|
15
15
|
#include "pytypes.h"
|
|
16
16
|
|
|
17
|
+
#include <algorithm>
|
|
18
|
+
|
|
19
|
+
#if defined(__cpp_nontype_template_args) && __cpp_nontype_template_args >= 201911L
|
|
20
|
+
# define PYBIND11_TYPING_H_HAS_STRING_LITERAL
|
|
21
|
+
# include <numeric>
|
|
22
|
+
# include <ranges>
|
|
23
|
+
# include <string_view>
|
|
24
|
+
#endif
|
|
25
|
+
|
|
17
26
|
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
18
27
|
PYBIND11_NAMESPACE_BEGIN(typing)
|
|
19
28
|
|
|
@@ -63,6 +72,74 @@ class Callable<Return(Args...)> : public function {
|
|
|
63
72
|
using function::function;
|
|
64
73
|
};
|
|
65
74
|
|
|
75
|
+
template <typename T>
|
|
76
|
+
class Type : public type {
|
|
77
|
+
using type::type;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
template <typename... Types>
|
|
81
|
+
class Union : public object {
|
|
82
|
+
PYBIND11_OBJECT_DEFAULT(Union, object, PyObject_Type)
|
|
83
|
+
using object::object;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
template <typename T>
|
|
87
|
+
class Optional : public object {
|
|
88
|
+
PYBIND11_OBJECT_DEFAULT(Optional, object, PyObject_Type)
|
|
89
|
+
using object::object;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
template <typename T>
|
|
93
|
+
class Final : public object {
|
|
94
|
+
PYBIND11_OBJECT_DEFAULT(Final, object, PyObject_Type)
|
|
95
|
+
using object::object;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
template <typename T>
|
|
99
|
+
class ClassVar : public object {
|
|
100
|
+
PYBIND11_OBJECT_DEFAULT(ClassVar, object, PyObject_Type)
|
|
101
|
+
using object::object;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
template <typename T>
|
|
105
|
+
class TypeGuard : public bool_ {
|
|
106
|
+
using bool_::bool_;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
template <typename T>
|
|
110
|
+
class TypeIs : public bool_ {
|
|
111
|
+
using bool_::bool_;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
class NoReturn : public none {
|
|
115
|
+
using none::none;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
class Never : public none {
|
|
119
|
+
using none::none;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
|
|
123
|
+
template <size_t N>
|
|
124
|
+
struct StringLiteral {
|
|
125
|
+
constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); }
|
|
126
|
+
char name[N];
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
template <StringLiteral... StrLits>
|
|
130
|
+
class Literal : public object {
|
|
131
|
+
PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type)
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// Example syntax for creating a TypeVar.
|
|
135
|
+
// typedef typing::TypeVar<"T"> TypeVarT;
|
|
136
|
+
template <StringLiteral>
|
|
137
|
+
class TypeVar : public object {
|
|
138
|
+
PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type)
|
|
139
|
+
using object::object;
|
|
140
|
+
};
|
|
141
|
+
#endif
|
|
142
|
+
|
|
66
143
|
PYBIND11_NAMESPACE_END(typing)
|
|
67
144
|
|
|
68
145
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
@@ -105,21 +182,117 @@ struct handle_type_name<typing::Set<T>> {
|
|
|
105
182
|
|
|
106
183
|
template <typename T>
|
|
107
184
|
struct handle_type_name<typing::Iterable<T>> {
|
|
108
|
-
static constexpr auto name
|
|
185
|
+
static constexpr auto name
|
|
186
|
+
= const_name("collections.abc.Iterable[") + make_caster<T>::name + const_name("]");
|
|
109
187
|
};
|
|
110
188
|
|
|
111
189
|
template <typename T>
|
|
112
190
|
struct handle_type_name<typing::Iterator<T>> {
|
|
113
|
-
static constexpr auto name
|
|
191
|
+
static constexpr auto name
|
|
192
|
+
= const_name("collections.abc.Iterator[") + make_caster<T>::name + const_name("]");
|
|
114
193
|
};
|
|
115
194
|
|
|
116
195
|
template <typename Return, typename... Args>
|
|
117
196
|
struct handle_type_name<typing::Callable<Return(Args...)>> {
|
|
118
197
|
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
|
|
119
198
|
static constexpr auto name
|
|
120
|
-
= const_name("Callable[[")
|
|
121
|
-
+
|
|
199
|
+
= const_name("collections.abc.Callable[[")
|
|
200
|
+
+ ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster<Args>::name)...)
|
|
201
|
+
+ const_name("], ") + ::pybind11::detail::return_descr(make_caster<retval_type>::name)
|
|
202
|
+
+ const_name("]");
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
template <typename Return>
|
|
206
|
+
struct handle_type_name<typing::Callable<Return(ellipsis)>> {
|
|
207
|
+
// PEP 484 specifies this syntax for defining only return types of callables
|
|
208
|
+
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
|
|
209
|
+
static constexpr auto name = const_name("collections.abc.Callable[..., ")
|
|
210
|
+
+ ::pybind11::detail::return_descr(make_caster<retval_type>::name)
|
|
211
|
+
+ const_name("]");
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
template <typename T>
|
|
215
|
+
struct handle_type_name<typing::Type<T>> {
|
|
216
|
+
static constexpr auto name = const_name("type[") + make_caster<T>::name + const_name("]");
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
template <typename... Types>
|
|
220
|
+
struct handle_type_name<typing::Union<Types...>> {
|
|
221
|
+
static constexpr auto name = ::pybind11::detail::union_concat(make_caster<Types>::name...);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
template <typename T>
|
|
225
|
+
struct handle_type_name<typing::Optional<T>> {
|
|
226
|
+
static constexpr auto name = make_caster<T>::name | make_caster<none>::name;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
template <typename T>
|
|
230
|
+
struct handle_type_name<typing::Final<T>> {
|
|
231
|
+
static constexpr auto name = const_name("typing.Final[")
|
|
232
|
+
+ ::pybind11::detail::return_descr(make_caster<T>::name)
|
|
233
|
+
+ const_name("]");
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
template <typename T>
|
|
237
|
+
struct handle_type_name<typing::ClassVar<T>> {
|
|
238
|
+
static constexpr auto name
|
|
239
|
+
= const_name("typing.ClassVar[") + make_caster<T>::name + const_name("]");
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
template <typename T>
|
|
243
|
+
struct handle_type_name<typing::TypeGuard<T>> {
|
|
244
|
+
static constexpr auto name = const_name(PYBIND11_TYPE_GUARD_TYPE_HINT) + const_name("[")
|
|
245
|
+
+ make_caster<T>::name + const_name("]");
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
template <typename T>
|
|
249
|
+
struct handle_type_name<typing::TypeIs<T>> {
|
|
250
|
+
static constexpr auto name = const_name(PYBIND11_TYPE_IS_TYPE_HINT) + const_name("[")
|
|
251
|
+
+ make_caster<T>::name + const_name("]");
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
template <>
|
|
255
|
+
struct handle_type_name<typing::NoReturn> {
|
|
256
|
+
static constexpr auto name = const_name("typing.NoReturn");
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
template <>
|
|
260
|
+
struct handle_type_name<typing::Never> {
|
|
261
|
+
static constexpr auto name = const_name(PYBIND11_NEVER_TYPE_HINT);
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
#if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
|
|
265
|
+
template <typing::StringLiteral StrLit>
|
|
266
|
+
consteval auto sanitize_string_literal() {
|
|
267
|
+
constexpr std::string_view v(StrLit.name);
|
|
268
|
+
constexpr std::string_view special_chars("!@%{}-");
|
|
269
|
+
constexpr auto num_special_chars = std::accumulate(
|
|
270
|
+
special_chars.begin(), special_chars.end(), (size_t) 0, [&v](auto acc, const char &c) {
|
|
271
|
+
return std::move(acc) + std::ranges::count(v, c);
|
|
272
|
+
});
|
|
273
|
+
char result[v.size() + num_special_chars + 1];
|
|
274
|
+
size_t i = 0;
|
|
275
|
+
for (auto c : StrLit.name) {
|
|
276
|
+
if (special_chars.find(c) != std::string_view::npos) {
|
|
277
|
+
result[i++] = '!';
|
|
278
|
+
}
|
|
279
|
+
result[i++] = c;
|
|
280
|
+
}
|
|
281
|
+
return typing::StringLiteral(result);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
template <typing::StringLiteral... Literals>
|
|
285
|
+
struct handle_type_name<typing::Literal<Literals...>> {
|
|
286
|
+
static constexpr auto name
|
|
287
|
+
= const_name("typing.Literal[")
|
|
288
|
+
+ pybind11::detail::concat(const_name(sanitize_string_literal<Literals>().name)...)
|
|
289
|
+
+ const_name("]");
|
|
290
|
+
};
|
|
291
|
+
template <typing::StringLiteral StrLit>
|
|
292
|
+
struct handle_type_name<typing::TypeVar<StrLit>> {
|
|
293
|
+
static constexpr auto name = const_name(sanitize_string_literal<StrLit>().name);
|
|
122
294
|
};
|
|
295
|
+
#endif
|
|
123
296
|
|
|
124
297
|
PYBIND11_NAMESPACE_END(detail)
|
|
125
298
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
pybind11/warnings.h: Python warnings wrappers.
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2024 Jan Iwaszkiewicz <jiwaszkiewicz6@gmail.com>
|
|
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
|
+
#include "pybind11.h"
|
|
13
|
+
#include "detail/common.h"
|
|
14
|
+
|
|
15
|
+
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
16
|
+
|
|
17
|
+
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
18
|
+
|
|
19
|
+
inline bool PyWarning_Check(PyObject *obj) {
|
|
20
|
+
int result = PyObject_IsSubclass(obj, PyExc_Warning);
|
|
21
|
+
if (result == 1) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
if (result == -1) {
|
|
25
|
+
raise_from(PyExc_SystemError,
|
|
26
|
+
"pybind11::detail::PyWarning_Check(): PyObject_IsSubclass() call failed.");
|
|
27
|
+
throw error_already_set();
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
PYBIND11_NAMESPACE_END(detail)
|
|
33
|
+
|
|
34
|
+
PYBIND11_NAMESPACE_BEGIN(warnings)
|
|
35
|
+
|
|
36
|
+
inline object
|
|
37
|
+
new_warning_type(handle scope, const char *name, handle base = PyExc_RuntimeWarning) {
|
|
38
|
+
if (!detail::PyWarning_Check(base.ptr())) {
|
|
39
|
+
pybind11_fail("pybind11::warnings::new_warning_type(): cannot create custom warning, base "
|
|
40
|
+
"must be a subclass of "
|
|
41
|
+
"PyExc_Warning!");
|
|
42
|
+
}
|
|
43
|
+
if (hasattr(scope, name)) {
|
|
44
|
+
pybind11_fail("pybind11::warnings::new_warning_type(): an attribute with name \""
|
|
45
|
+
+ std::string(name) + "\" exists already.");
|
|
46
|
+
}
|
|
47
|
+
std::string full_name = scope.attr("__name__").cast<std::string>() + std::string(".") + name;
|
|
48
|
+
handle h(PyErr_NewException(full_name.c_str(), base.ptr(), nullptr));
|
|
49
|
+
if (!h) {
|
|
50
|
+
raise_from(PyExc_SystemError,
|
|
51
|
+
"pybind11::warnings::new_warning_type(): PyErr_NewException() call failed.");
|
|
52
|
+
throw error_already_set();
|
|
53
|
+
}
|
|
54
|
+
auto obj = reinterpret_steal<object>(h);
|
|
55
|
+
scope.attr(name) = obj;
|
|
56
|
+
return obj;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Similar to Python `warnings.warn()`
|
|
60
|
+
inline void
|
|
61
|
+
warn(const char *message, handle category = PyExc_RuntimeWarning, int stack_level = 2) {
|
|
62
|
+
if (!detail::PyWarning_Check(category.ptr())) {
|
|
63
|
+
pybind11_fail(
|
|
64
|
+
"pybind11::warnings::warn(): cannot raise warning, category must be a subclass of "
|
|
65
|
+
"PyExc_Warning!");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (PyErr_WarnEx(category.ptr(), message, stack_level) == -1) {
|
|
69
|
+
throw error_already_set();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
PYBIND11_NAMESPACE_END(warnings)
|
|
74
|
+
|
|
75
|
+
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|