netgen-mesher 6.2.2505.post48.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.
Files changed (144) hide show
  1. netgen/__init__.pyi +3 -3
  2. netgen/cmake/NetgenConfig.cmake +9 -8
  3. netgen/config/__init__.pyi +7 -7
  4. netgen/config/config.py +6 -6
  5. netgen/config/config.pyi +7 -7
  6. netgen/include/core/archive.hpp +18 -3
  7. netgen/include/core/array.hpp +20 -4
  8. netgen/include/core/autodiff.hpp +9 -11
  9. netgen/include/core/autodiffdiff.hpp +0 -2
  10. netgen/include/core/bitarray.hpp +1 -1
  11. netgen/include/core/flags.hpp +1 -1
  12. netgen/include/core/hashtable.hpp +1 -1
  13. netgen/include/core/memtracer.hpp +7 -7
  14. netgen/include/core/ngcore.hpp +5 -0
  15. netgen/include/core/ngcore_api.hpp +11 -0
  16. netgen/include/core/register_archive.hpp +8 -0
  17. netgen/include/core/simd_arm64.hpp +100 -5
  18. netgen/include/core/simd_generic.hpp +187 -6
  19. netgen/include/core/simd_math.hpp +3 -2
  20. netgen/include/core/statushandler.hpp +37 -0
  21. netgen/include/core/table.hpp +2 -2
  22. netgen/include/core/taskmanager.hpp +34 -1
  23. netgen/include/include/netgen_version.hpp +3 -3
  24. netgen/include/meshing/basegeom.hpp +1 -4
  25. netgen/include/meshing/global.hpp +0 -17
  26. netgen/include/meshing/hpref_tet.hpp +41 -0
  27. netgen/include/meshing/hprefinement.hpp +2 -0
  28. netgen/include/meshing/meshtype.hpp +1 -0
  29. netgen/include/meshing/msghandler.hpp +9 -6
  30. netgen/include/meshing/topology.hpp +2 -2
  31. netgen/include/nginterface.h +3 -2
  32. netgen/include/occ/occ_utils.hpp +26 -0
  33. netgen/include/occ/occgeom.hpp +8 -0
  34. netgen/include/pybind11/attr.h +40 -8
  35. netgen/include/pybind11/buffer_info.h +14 -14
  36. netgen/include/pybind11/cast.h +553 -29
  37. netgen/include/pybind11/chrono.h +4 -1
  38. netgen/include/pybind11/conduit/README.txt +15 -0
  39. netgen/include/pybind11/conduit/pybind11_conduit_v1.h +116 -0
  40. netgen/include/pybind11/conduit/pybind11_platform_abi_id.h +87 -0
  41. netgen/include/pybind11/conduit/wrap_include_python_h.h +72 -0
  42. netgen/include/pybind11/critical_section.h +56 -0
  43. netgen/include/pybind11/detail/class.h +172 -97
  44. netgen/include/pybind11/detail/common.h +270 -189
  45. netgen/include/pybind11/detail/cpp_conduit.h +75 -0
  46. netgen/include/pybind11/detail/descr.h +55 -0
  47. netgen/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h +39 -0
  48. netgen/include/pybind11/detail/exception_translation.h +71 -0
  49. netgen/include/pybind11/detail/function_record_pyobject.h +191 -0
  50. netgen/include/pybind11/detail/init.h +113 -9
  51. netgen/include/pybind11/detail/internals.h +479 -344
  52. netgen/include/pybind11/detail/native_enum_data.h +209 -0
  53. netgen/include/pybind11/detail/pybind11_namespace_macros.h +82 -0
  54. netgen/include/pybind11/detail/struct_smart_holder.h +378 -0
  55. netgen/include/pybind11/detail/type_caster_base.h +506 -133
  56. netgen/include/pybind11/detail/using_smart_holder.h +22 -0
  57. netgen/include/pybind11/detail/value_and_holder.h +90 -0
  58. netgen/include/pybind11/eigen/matrix.h +19 -10
  59. netgen/include/pybind11/eigen/tensor.h +15 -11
  60. netgen/include/pybind11/embed.h +50 -46
  61. netgen/include/pybind11/eval.h +11 -6
  62. netgen/include/pybind11/functional.h +58 -49
  63. netgen/include/pybind11/gil.h +34 -82
  64. netgen/include/pybind11/gil_safe_call_once.h +12 -1
  65. netgen/include/pybind11/gil_simple.h +37 -0
  66. netgen/include/pybind11/native_enum.h +67 -0
  67. netgen/include/pybind11/numpy.h +272 -93
  68. netgen/include/pybind11/pybind11.h +947 -265
  69. netgen/include/pybind11/pytypes.h +127 -21
  70. netgen/include/pybind11/stl/filesystem.h +23 -25
  71. netgen/include/pybind11/stl.h +277 -59
  72. netgen/include/pybind11/stl_bind.h +42 -7
  73. netgen/include/pybind11/subinterpreter.h +299 -0
  74. netgen/include/pybind11/trampoline_self_life_support.h +65 -0
  75. netgen/include/pybind11/typing.h +177 -4
  76. netgen/include/pybind11/warnings.h +75 -0
  77. netgen/lib/libnggui.lib +0 -0
  78. netgen/lib/ngcore.lib +0 -0
  79. netgen/lib/nglib.lib +0 -0
  80. netgen/libnggui.dll +0 -0
  81. netgen/libngguipy.pyd +0 -0
  82. netgen/libngpy/_NgOCC.pyi +220 -141
  83. netgen/libngpy/_csg.pyi +26 -26
  84. netgen/libngpy/_geom2d.pyi +34 -25
  85. netgen/libngpy/_meshing.pyi +262 -111
  86. netgen/libngpy/_stl.pyi +3 -4
  87. netgen/libngpy.pyd +0 -0
  88. netgen/ngcore.dll +0 -0
  89. netgen/nglib.dll +0 -0
  90. netgen/read_gmsh.py +41 -0
  91. netgen/togl.dll +0 -0
  92. netgen/version.py +1 -1
  93. netgen/webgui.py +38 -2
  94. {netgen_mesher-6.2.2505.post48.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/METADATA +2 -1
  95. {netgen_mesher-6.2.2505.post48.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/RECORD +144 -124
  96. pyngcore/pyngcore.cp313-win_amd64.pyd +0 -0
  97. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/boundarycondition.geo +0 -0
  98. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/boxcyl.geo +0 -0
  99. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/circle_on_cube.geo +0 -0
  100. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cone.geo +0 -0
  101. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cube.geo +0 -0
  102. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cubeandring.geo +0 -0
  103. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cubeandspheres.geo +0 -0
  104. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cubemcyl.geo +0 -0
  105. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cubemsphere.geo +0 -0
  106. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cylinder.geo +0 -0
  107. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/cylsphere.geo +0 -0
  108. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/doc/ng4.pdf +0 -0
  109. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/ellipsoid.geo +0 -0
  110. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/ellipticcyl.geo +0 -0
  111. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/extrusion.geo +0 -0
  112. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/fichera.geo +0 -0
  113. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/frame.step +0 -0
  114. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/hinge.stl +0 -0
  115. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/lshape3d.geo +0 -0
  116. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/manyholes.geo +0 -0
  117. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/manyholes2.geo +0 -0
  118. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/matrix.geo +0 -0
  119. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/ortho.geo +0 -0
  120. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/part1.stl +0 -0
  121. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/period.geo +0 -0
  122. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/py_tutorials/exportNeutral.py +0 -0
  123. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/py_tutorials/mesh.py +0 -0
  124. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/py_tutorials/shaft.geo +0 -0
  125. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/revolution.geo +0 -0
  126. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/screw.step +0 -0
  127. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/sculpture.geo +0 -0
  128. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/shaft.geo +0 -0
  129. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/shell.geo +0 -0
  130. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/sphere.geo +0 -0
  131. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/sphereincube.geo +0 -0
  132. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/square.in2d +0 -0
  133. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/squarecircle.in2d +0 -0
  134. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/squarehole.in2d +0 -0
  135. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/torus.geo +0 -0
  136. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/trafo.geo +0 -0
  137. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/twobricks.geo +0 -0
  138. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/twocubes.geo +0 -0
  139. {netgen_mesher-6.2.2505.post48.dev0.data → netgen_mesher-6.2.2506.post48.dev0.data}/data/share/netgen/twocyl.geo +0 -0
  140. {netgen_mesher-6.2.2505.post48.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/AUTHORS +0 -0
  141. {netgen_mesher-6.2.2505.post48.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/LICENSE +0 -0
  142. {netgen_mesher-6.2.2505.post48.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/WHEEL +0 -0
  143. {netgen_mesher-6.2.2505.post48.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/entry_points.txt +0 -0
  144. {netgen_mesher-6.2.2505.post48.dev0.dist-info → netgen_mesher-6.2.2506.post48.dev0.dist-info}/top_level.txt +0 -0
@@ -11,10 +11,14 @@
11
11
 
12
12
  #include "pybind11.h"
13
13
  #include "detail/common.h"
14
+ #include "detail/descr.h"
15
+ #include "detail/type_caster_base.h"
14
16
 
15
17
  #include <deque>
18
+ #include <initializer_list>
16
19
  #include <list>
17
20
  #include <map>
21
+ #include <memory>
18
22
  #include <ostream>
19
23
  #include <set>
20
24
  #include <unordered_map>
@@ -35,6 +39,103 @@
35
39
  PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
36
40
  PYBIND11_NAMESPACE_BEGIN(detail)
37
41
 
42
+ //
43
+ // Begin: Equivalent of
44
+ // https://github.com/google/clif/blob/ae4eee1de07cdf115c0c9bf9fec9ff28efce6f6c/clif/python/runtime.cc#L388-L438
45
+ /*
46
+ The three `object_is_convertible_to_*()` functions below are
47
+ the result of converging the behaviors of pybind11 and PyCLIF
48
+ (http://github.com/google/clif).
49
+
50
+ Originally PyCLIF was extremely far on the permissive side of the spectrum,
51
+ while pybind11 was very far on the strict side. Originally PyCLIF accepted any
52
+ Python iterable as input for a C++ `vector`/`set`/`map` argument, as long as
53
+ the elements were convertible. The obvious (in hindsight) problem was that
54
+ any empty Python iterable could be passed to any of these C++ types, e.g. `{}`
55
+ was accepted for C++ `vector`/`set` arguments, or `[]` for C++ `map` arguments.
56
+
57
+ The functions below strike a practical permissive-vs-strict compromise,
58
+ informed by tens of thousands of use cases in the wild. A main objective is
59
+ to prevent accidents and improve readability:
60
+
61
+ - Python literals must match the C++ types.
62
+
63
+ - For C++ `set`: The potentially reducing conversion from a Python sequence
64
+ (e.g. Python `list` or `tuple`) to a C++ `set` must be explicit, by going
65
+ through a Python `set`.
66
+
67
+ - However, a Python `set` can still be passed to a C++ `vector`. The rationale
68
+ is that this conversion is not reducing. Implicit conversions of this kind
69
+ are also fairly commonly used, therefore enforcing explicit conversions
70
+ would have an unfavorable cost : benefit ratio; more sloppily speaking,
71
+ such an enforcement would be more annoying than helpful.
72
+
73
+ Additional checks have been added to allow types derived from `collections.abc.Set` and
74
+ `collections.abc.Mapping` (`collections.abc.Sequence` is already allowed by `PySequence_Check`).
75
+ */
76
+
77
+ inline bool object_is_instance_with_one_of_tp_names(PyObject *obj,
78
+ std::initializer_list<const char *> tp_names) {
79
+ if (PyType_Check(obj)) {
80
+ return false;
81
+ }
82
+ const char *obj_tp_name = Py_TYPE(obj)->tp_name;
83
+ for (const auto *tp_name : tp_names) {
84
+ if (std::strcmp(obj_tp_name, tp_name) == 0) {
85
+ return true;
86
+ }
87
+ }
88
+ return false;
89
+ }
90
+
91
+ inline bool object_is_convertible_to_std_vector(const handle &src) {
92
+ // Allow sequence-like objects, but not (byte-)string-like objects.
93
+ if (PySequence_Check(src.ptr()) != 0) {
94
+ return !PyUnicode_Check(src.ptr()) && !PyBytes_Check(src.ptr());
95
+ }
96
+ // Allow generators, set/frozenset and several common iterable types.
97
+ return (PyGen_Check(src.ptr()) != 0) || (PyAnySet_Check(src.ptr()) != 0)
98
+ || object_is_instance_with_one_of_tp_names(
99
+ src.ptr(), {"dict_keys", "dict_values", "dict_items", "map", "zip"});
100
+ }
101
+
102
+ inline bool object_is_convertible_to_std_set(const handle &src, bool convert) {
103
+ // Allow set/frozenset and dict keys.
104
+ // In convert mode: also allow types derived from collections.abc.Set.
105
+ return ((PyAnySet_Check(src.ptr()) != 0)
106
+ || object_is_instance_with_one_of_tp_names(src.ptr(), {"dict_keys"}))
107
+ || (convert && isinstance(src, module_::import("collections.abc").attr("Set")));
108
+ }
109
+
110
+ inline bool object_is_convertible_to_std_map(const handle &src, bool convert) {
111
+ // Allow dict.
112
+ if (PyDict_Check(src.ptr())) {
113
+ return true;
114
+ }
115
+ // Allow types conforming to Mapping Protocol.
116
+ // According to https://docs.python.org/3/c-api/mapping.html, `PyMappingCheck()` checks for
117
+ // `__getitem__()` without checking the type of keys. In order to restrict the allowed types
118
+ // closer to actual Mapping-like types, we also check for the `items()` method.
119
+ if (PyMapping_Check(src.ptr()) != 0) {
120
+ PyObject *items = PyObject_GetAttrString(src.ptr(), "items");
121
+ if (items != nullptr) {
122
+ bool is_convertible = (PyCallable_Check(items) != 0);
123
+ Py_DECREF(items);
124
+ if (is_convertible) {
125
+ return true;
126
+ }
127
+ } else {
128
+ PyErr_Clear();
129
+ }
130
+ }
131
+ // In convert mode: Allow types derived from collections.abc.Mapping
132
+ return convert && isinstance(src, module_::import("collections.abc").attr("Mapping"));
133
+ }
134
+
135
+ //
136
+ // End: Equivalent of clif/python/runtime.cc
137
+ //
138
+
38
139
  /// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for
39
140
  /// forwarding a container element). Typically used indirect via forwarded_type(), below.
40
141
  template <typename T, typename U>
@@ -66,17 +167,10 @@ private:
66
167
  }
67
168
  void reserve_maybe(const anyset &, void *) {}
68
169
 
69
- public:
70
- bool load(handle src, bool convert) {
71
- if (!isinstance<anyset>(src)) {
72
- return false;
73
- }
74
- auto s = reinterpret_borrow<anyset>(src);
75
- value.clear();
76
- reserve_maybe(s, &value);
77
- for (auto entry : s) {
170
+ bool convert_iterable(const iterable &itbl, bool convert) {
171
+ for (const auto &it : itbl) {
78
172
  key_conv conv;
79
- if (!conv.load(entry, convert)) {
173
+ if (!conv.load(it, convert)) {
80
174
  return false;
81
175
  }
82
176
  value.insert(cast_op<Key &&>(std::move(conv)));
@@ -84,6 +178,29 @@ public:
84
178
  return true;
85
179
  }
86
180
 
181
+ bool convert_anyset(const anyset &s, bool convert) {
182
+ value.clear();
183
+ reserve_maybe(s, &value);
184
+ return convert_iterable(s, convert);
185
+ }
186
+
187
+ public:
188
+ bool load(handle src, bool convert) {
189
+ if (!object_is_convertible_to_std_set(src, convert)) {
190
+ return false;
191
+ }
192
+ if (isinstance<anyset>(src)) {
193
+ value.clear();
194
+ return convert_anyset(reinterpret_borrow<anyset>(src), convert);
195
+ }
196
+ if (!convert) {
197
+ return false;
198
+ }
199
+ assert(isinstance<iterable>(src));
200
+ value.clear();
201
+ return convert_iterable(reinterpret_borrow<iterable>(src), convert);
202
+ }
203
+
87
204
  template <typename T>
88
205
  static handle cast(T &&src, return_value_policy policy, handle parent) {
89
206
  if (!std::is_lvalue_reference<T>::value) {
@@ -100,7 +217,9 @@ public:
100
217
  return s.release();
101
218
  }
102
219
 
103
- PYBIND11_TYPE_CASTER(type, const_name("set[") + key_conv::name + const_name("]"));
220
+ PYBIND11_TYPE_CASTER(type,
221
+ io_name("collections.abc.Set", "set") + const_name("[") + key_conv::name
222
+ + const_name("]"));
104
223
  };
105
224
 
106
225
  template <typename Type, typename Key, typename Value>
@@ -115,15 +234,10 @@ private:
115
234
  }
116
235
  void reserve_maybe(const dict &, void *) {}
117
236
 
118
- public:
119
- bool load(handle src, bool convert) {
120
- if (!isinstance<dict>(src)) {
121
- return false;
122
- }
123
- auto d = reinterpret_borrow<dict>(src);
237
+ bool convert_elements(const dict &d, bool convert) {
124
238
  value.clear();
125
239
  reserve_maybe(d, &value);
126
- for (auto it : d) {
240
+ for (const auto &it : d) {
127
241
  key_conv kconv;
128
242
  value_conv vconv;
129
243
  if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert)) {
@@ -134,6 +248,25 @@ public:
134
248
  return true;
135
249
  }
136
250
 
251
+ public:
252
+ bool load(handle src, bool convert) {
253
+ if (!object_is_convertible_to_std_map(src, convert)) {
254
+ return false;
255
+ }
256
+ if (isinstance<dict>(src)) {
257
+ return convert_elements(reinterpret_borrow<dict>(src), convert);
258
+ }
259
+ if (!convert) {
260
+ return false;
261
+ }
262
+ auto items = reinterpret_steal<object>(PyMapping_Items(src.ptr()));
263
+ if (!items) {
264
+ throw error_already_set();
265
+ }
266
+ assert(isinstance<iterable>(items));
267
+ return convert_elements(dict(reinterpret_borrow<iterable>(items)), convert);
268
+ }
269
+
137
270
  template <typename T>
138
271
  static handle cast(T &&src, return_value_policy policy, handle parent) {
139
272
  dict d;
@@ -157,7 +290,8 @@ public:
157
290
  }
158
291
 
159
292
  PYBIND11_TYPE_CASTER(Type,
160
- const_name("dict[") + key_conv::name + const_name(", ") + value_conv::name
293
+ io_name("collections.abc.Mapping", "dict") + const_name("[")
294
+ + key_conv::name + const_name(", ") + value_conv::name
161
295
  + const_name("]"));
162
296
  };
163
297
 
@@ -166,13 +300,35 @@ struct list_caster {
166
300
  using value_conv = make_caster<Value>;
167
301
 
168
302
  bool load(handle src, bool convert) {
169
- if (!isinstance<sequence>(src) || isinstance<bytes>(src) || isinstance<str>(src)) {
303
+ if (!object_is_convertible_to_std_vector(src)) {
170
304
  return false;
171
305
  }
172
- auto s = reinterpret_borrow<sequence>(src);
306
+ if (isinstance<sequence>(src)) {
307
+ return convert_elements(src, convert);
308
+ }
309
+ if (!convert) {
310
+ return false;
311
+ }
312
+ // Designed to be behavior-equivalent to passing tuple(src) from Python:
313
+ // The conversion to a tuple will first exhaust the generator object, to ensure that
314
+ // the generator is not left in an unpredictable (to the caller) partially-consumed
315
+ // state.
316
+ assert(isinstance<iterable>(src));
317
+ return convert_elements(tuple(reinterpret_borrow<iterable>(src)), convert);
318
+ }
319
+
320
+ private:
321
+ template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
322
+ void reserve_maybe(const sequence &s, Type *) {
323
+ value.reserve(s.size());
324
+ }
325
+ void reserve_maybe(const sequence &, void *) {}
326
+
327
+ bool convert_elements(handle seq, bool convert) {
328
+ auto s = reinterpret_borrow<sequence>(seq);
173
329
  value.clear();
174
330
  reserve_maybe(s, &value);
175
- for (const auto &it : s) {
331
+ for (const auto &it : seq) {
176
332
  value_conv conv;
177
333
  if (!conv.load(it, convert)) {
178
334
  return false;
@@ -182,13 +338,6 @@ struct list_caster {
182
338
  return true;
183
339
  }
184
340
 
185
- private:
186
- template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
187
- void reserve_maybe(const sequence &s, Type *) {
188
- value.reserve(s.size());
189
- }
190
- void reserve_maybe(const sequence &, void *) {}
191
-
192
341
  public:
193
342
  template <typename T>
194
343
  static handle cast(T &&src, return_value_policy policy, handle parent) {
@@ -208,7 +357,9 @@ public:
208
357
  return l.release();
209
358
  }
210
359
 
211
- PYBIND11_TYPE_CASTER(Type, const_name("list[") + value_conv::name + const_name("]"));
360
+ PYBIND11_TYPE_CASTER(Type,
361
+ io_name("collections.abc.Sequence", "list") + const_name("[")
362
+ + value_conv::name + const_name("]"));
212
363
  };
213
364
 
214
365
  template <typename Type, typename Alloc>
@@ -220,43 +371,87 @@ struct type_caster<std::deque<Type, Alloc>> : list_caster<std::deque<Type, Alloc
220
371
  template <typename Type, typename Alloc>
221
372
  struct type_caster<std::list<Type, Alloc>> : list_caster<std::list<Type, Alloc>, Type> {};
222
373
 
374
+ template <typename ArrayType, typename V, size_t... I>
375
+ ArrayType vector_to_array_impl(V &&v, index_sequence<I...>) {
376
+ return {{std::move(v[I])...}};
377
+ }
378
+
379
+ // Based on https://en.cppreference.com/w/cpp/container/array/to_array
380
+ template <typename ArrayType, size_t N, typename V>
381
+ ArrayType vector_to_array(V &&v) {
382
+ return vector_to_array_impl<ArrayType, V>(std::forward<V>(v), make_index_sequence<N>{});
383
+ }
384
+
223
385
  template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0>
224
386
  struct array_caster {
225
387
  using value_conv = make_caster<Value>;
226
388
 
227
389
  private:
228
- template <bool R = Resizable>
229
- bool require_size(enable_if_t<R, size_t> size) {
230
- if (value.size() != size) {
231
- value.resize(size);
390
+ std::unique_ptr<ArrayType> value;
391
+
392
+ template <bool R = Resizable, enable_if_t<R, int> = 0>
393
+ bool convert_elements(handle seq, bool convert) {
394
+ auto l = reinterpret_borrow<sequence>(seq);
395
+ value.reset(new ArrayType{});
396
+ // Using `resize` to preserve the behavior exactly as it was before PR #5305
397
+ // For the `resize` to work, `Value` must be default constructible.
398
+ // For `std::valarray`, this is a requirement:
399
+ // https://en.cppreference.com/w/cpp/named_req/NumericType
400
+ value->resize(l.size());
401
+ size_t ctr = 0;
402
+ for (const auto &it : l) {
403
+ value_conv conv;
404
+ if (!conv.load(it, convert)) {
405
+ return false;
406
+ }
407
+ (*value)[ctr++] = cast_op<Value &&>(std::move(conv));
232
408
  }
233
409
  return true;
234
410
  }
235
- template <bool R = Resizable>
236
- bool require_size(enable_if_t<!R, size_t> size) {
237
- return size == Size;
238
- }
239
411
 
240
- public:
241
- bool load(handle src, bool convert) {
242
- if (!isinstance<sequence>(src)) {
412
+ template <bool R = Resizable, enable_if_t<!R, int> = 0>
413
+ bool convert_elements(handle seq, bool convert) {
414
+ auto l = reinterpret_borrow<sequence>(seq);
415
+ if (l.size() != Size) {
243
416
  return false;
244
417
  }
245
- auto l = reinterpret_borrow<sequence>(src);
246
- if (!require_size(l.size())) {
247
- return false;
248
- }
249
- size_t ctr = 0;
250
- for (const auto &it : l) {
418
+ // The `temp` storage is needed to support `Value` types that are not
419
+ // default-constructible.
420
+ // Deliberate choice: no template specializations, for simplicity, and
421
+ // because the compile time overhead for the specializations is deemed
422
+ // more significant than the runtime overhead for the `temp` storage.
423
+ std::vector<Value> temp;
424
+ temp.reserve(l.size());
425
+ for (auto it : l) {
251
426
  value_conv conv;
252
427
  if (!conv.load(it, convert)) {
253
428
  return false;
254
429
  }
255
- value[ctr++] = cast_op<Value &&>(std::move(conv));
430
+ temp.emplace_back(cast_op<Value &&>(std::move(conv)));
256
431
  }
432
+ value.reset(new ArrayType(vector_to_array<ArrayType, Size>(std::move(temp))));
257
433
  return true;
258
434
  }
259
435
 
436
+ public:
437
+ bool load(handle src, bool convert) {
438
+ if (!object_is_convertible_to_std_vector(src)) {
439
+ return false;
440
+ }
441
+ if (isinstance<sequence>(src)) {
442
+ return convert_elements(src, convert);
443
+ }
444
+ if (!convert) {
445
+ return false;
446
+ }
447
+ // Designed to be behavior-equivalent to passing tuple(src) from Python:
448
+ // The conversion to a tuple will first exhaust the generator object, to ensure that
449
+ // the generator is not left in an unpredictable (to the caller) partially-consumed
450
+ // state.
451
+ assert(isinstance<iterable>(src));
452
+ return convert_elements(tuple(reinterpret_borrow<iterable>(src)), convert);
453
+ }
454
+
260
455
  template <typename T>
261
456
  static handle cast(T &&src, return_value_policy policy, handle parent) {
262
457
  list l(src.size());
@@ -272,12 +467,38 @@ public:
272
467
  return l.release();
273
468
  }
274
469
 
275
- PYBIND11_TYPE_CASTER(ArrayType,
276
- const_name<Resizable>(const_name(""), const_name("Annotated["))
277
- + const_name("list[") + value_conv::name + const_name("]")
278
- + const_name<Resizable>(const_name(""),
279
- const_name(", FixedSize(")
280
- + const_name<Size>() + const_name(")]")));
470
+ // Code copied from PYBIND11_TYPE_CASTER macro.
471
+ // Intentionally preserving the behavior exactly as it was before PR #5305
472
+ template <typename T_, enable_if_t<std::is_same<ArrayType, remove_cv_t<T_>>::value, int> = 0>
473
+ static handle cast(T_ *src, return_value_policy policy, handle parent) {
474
+ if (!src) {
475
+ return none().release();
476
+ }
477
+ if (policy == return_value_policy::take_ownership) {
478
+ auto h = cast(std::move(*src), policy, parent);
479
+ delete src; // WARNING: Assumes `src` was allocated with `new`.
480
+ return h;
481
+ }
482
+ return cast(*src, policy, parent);
483
+ }
484
+
485
+ // NOLINTNEXTLINE(google-explicit-constructor)
486
+ operator ArrayType *() { return &(*value); }
487
+ // NOLINTNEXTLINE(google-explicit-constructor)
488
+ operator ArrayType &() { return *value; }
489
+ // NOLINTNEXTLINE(google-explicit-constructor)
490
+ operator ArrayType &&() && { return std::move(*value); }
491
+
492
+ template <typename T_>
493
+ using cast_op_type = movable_cast_op_type<T_>;
494
+
495
+ static constexpr auto name
496
+ = const_name<Resizable>(const_name(""), const_name("typing.Annotated["))
497
+ + io_name("collections.abc.Sequence", "list") + const_name("[") + value_conv::name
498
+ + const_name("]")
499
+ + const_name<Resizable>(const_name(""),
500
+ const_name(", \"FixedSize(") + const_name<Size>()
501
+ + const_name(")\"]"));
281
502
  };
282
503
 
283
504
  template <typename Type, size_t Size>
@@ -336,7 +557,7 @@ struct optional_caster {
336
557
  return true;
337
558
  }
338
559
 
339
- PYBIND11_TYPE_CASTER(Type, const_name("Optional[") + value_conv::name + const_name("]"));
560
+ PYBIND11_TYPE_CASTER(Type, value_conv::name | make_caster<none>::name);
340
561
  };
341
562
 
342
563
  #if defined(PYBIND11_HAS_OPTIONAL)
@@ -420,10 +641,7 @@ struct variant_caster<V<Ts...>> {
420
641
  }
421
642
 
422
643
  using Type = V<Ts...>;
423
- PYBIND11_TYPE_CASTER(Type,
424
- const_name("Union[")
425
- + ::pybind11::detail::concat(make_caster<Ts>::name...)
426
- + const_name("]"));
644
+ PYBIND11_TYPE_CASTER(Type, ::pybind11::detail::union_concat(make_caster<Ts>::name...));
427
645
  };
428
646
 
429
647
  #if defined(PYBIND11_HAS_VARIANT)
@@ -158,8 +158,7 @@ void vector_modifiers(
158
158
  return v.release();
159
159
  }));
160
160
 
161
- cl.def(
162
- "clear", [](Vector &v) { v.clear(); }, "Clear the contents");
161
+ cl.def("clear", [](Vector &v) { v.clear(); }, "Clear the contents");
163
162
 
164
163
  cl.def(
165
164
  "extend",
@@ -181,7 +180,7 @@ void vector_modifiers(
181
180
  v.end());
182
181
  try {
183
182
  v.shrink_to_fit();
184
- } catch (const std::exception &) {
183
+ } catch (const std::exception &) { // NOLINT(bugprone-empty-catch)
185
184
  // Do nothing
186
185
  }
187
186
  throw;
@@ -488,7 +487,7 @@ PYBIND11_NAMESPACE_END(detail)
488
487
  //
489
488
  // std::vector
490
489
  //
491
- template <typename Vector, typename holder_type = std::unique_ptr<Vector>, typename... Args>
490
+ template <typename Vector, typename holder_type = default_holder_type<Vector>, typename... Args>
492
491
  class_<Vector, holder_type> bind_vector(handle scope, std::string const &name, Args &&...args) {
493
492
  using Class_ = class_<Vector, holder_type>;
494
493
 
@@ -695,9 +694,43 @@ struct ItemsViewImpl : public detail::items_view {
695
694
  Map &map;
696
695
  };
697
696
 
697
+ inline str format_message_key_error_key_object(handle py_key) {
698
+ str message = "pybind11::bind_map key";
699
+ if (!py_key) {
700
+ return message;
701
+ }
702
+ try {
703
+ message = str(py_key);
704
+ } catch (const std::exception &) {
705
+ try {
706
+ message = repr(py_key);
707
+ } catch (const std::exception &) {
708
+ return message;
709
+ }
710
+ }
711
+ const ssize_t cut_length = 100;
712
+ if (len(message) > 2 * cut_length + 3) {
713
+ return str(message[slice(0, cut_length, 1)]) + str("✄✄✄")
714
+ + str(message[slice(-cut_length, static_cast<ssize_t>(len(message)), 1)]);
715
+ }
716
+ return message;
717
+ }
718
+
719
+ template <typename KeyType>
720
+ str format_message_key_error(const KeyType &key) {
721
+ object py_key;
722
+ try {
723
+ py_key = cast(key);
724
+ } catch (const std::exception &) {
725
+ do { // Trick to avoid "empty catch" warning/error.
726
+ } while (false);
727
+ }
728
+ return format_message_key_error_key_object(py_key);
729
+ }
730
+
698
731
  PYBIND11_NAMESPACE_END(detail)
699
732
 
700
- template <typename Map, typename holder_type = std::unique_ptr<Map>, typename... Args>
733
+ template <typename Map, typename holder_type = default_holder_type<Map>, typename... Args>
701
734
  class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args &&...args) {
702
735
  using KeyType = typename Map::key_type;
703
736
  using MappedType = typename Map::mapped_type;
@@ -786,7 +819,8 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args &&
786
819
  [](Map &m, const KeyType &k) -> MappedType & {
787
820
  auto it = m.find(k);
788
821
  if (it == m.end()) {
789
- throw key_error();
822
+ set_error(PyExc_KeyError, detail::format_message_key_error(k));
823
+ throw error_already_set();
790
824
  }
791
825
  return it->second;
792
826
  },
@@ -809,7 +843,8 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args &&
809
843
  cl.def("__delitem__", [](Map &m, const KeyType &k) {
810
844
  auto it = m.find(k);
811
845
  if (it == m.end()) {
812
- throw key_error();
846
+ set_error(PyExc_KeyError, detail::format_message_key_error(k));
847
+ throw error_already_set();
813
848
  }
814
849
  m.erase(it);
815
850
  });