ruby_memprofiler_pprof 0.0.1

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 (200) hide show
  1. checksums.yaml +7 -0
  2. data/ext/ruby_memprofiler_pprof/backtrace.c +429 -0
  3. data/ext/ruby_memprofiler_pprof/collector.c +1055 -0
  4. data/ext/ruby_memprofiler_pprof/compat.c +182 -0
  5. data/ext/ruby_memprofiler_pprof/extconf.rb +72 -0
  6. data/ext/ruby_memprofiler_pprof/pprof.upb.c +170 -0
  7. data/ext/ruby_memprofiler_pprof/pprof.upb.h +848 -0
  8. data/ext/ruby_memprofiler_pprof/pprof_out.c +285 -0
  9. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.c +11 -0
  10. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.h +301 -0
  11. data/ext/ruby_memprofiler_pprof/strtab.c +391 -0
  12. data/ext/ruby_memprofiler_pprof/vendor/upb/BUILD +719 -0
  13. data/ext/ruby_memprofiler_pprof/vendor/upb/CONTRIBUTING.md +37 -0
  14. data/ext/ruby_memprofiler_pprof/vendor/upb/DESIGN.md +201 -0
  15. data/ext/ruby_memprofiler_pprof/vendor/upb/LICENSE +26 -0
  16. data/ext/ruby_memprofiler_pprof/vendor/upb/README.md +78 -0
  17. data/ext/ruby_memprofiler_pprof/vendor/upb/WORKSPACE +58 -0
  18. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/BUILD +53 -0
  19. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/amalgamate.py +129 -0
  20. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/build_defs.bzl +160 -0
  21. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/lua.BUILD +127 -0
  22. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/protobuf.patch +54 -0
  23. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/py_proto_library.bzl +137 -0
  24. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/python_downloads.bzl +84 -0
  25. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/system_python.bzl +101 -0
  26. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/upb_proto_library.bzl +388 -0
  27. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/workspace_deps.bzl +89 -0
  28. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD +252 -0
  29. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD.googleapis +54 -0
  30. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/benchmark.cc +333 -0
  31. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/build_defs.bzl +88 -0
  32. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/compare.py +118 -0
  33. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor.proto +888 -0
  34. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor_sv.proto +890 -0
  35. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/empty.proto +6 -0
  36. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_protobuf_binary_cc.py +64 -0
  37. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_synthetic_protos.py +118 -0
  38. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_upb_binary_c.py +65 -0
  39. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/BUILD.bazel +102 -0
  40. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/README.md +23 -0
  41. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/build_defs.bzl +73 -0
  42. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/make_cmakelists.py +340 -0
  43. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test.py +57 -0
  44. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test_lib.py +186 -0
  45. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/render.py +43 -0
  46. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/style-guide.md +65 -0
  47. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/vs-cpp-protos.md +255 -0
  48. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/wrapping-upb.md +444 -0
  49. data/ext/ruby_memprofiler_pprof/vendor/upb/python/BUILD +216 -0
  50. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.c +394 -0
  51. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.h +63 -0
  52. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.c +1694 -0
  53. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.h +80 -0
  54. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.c +704 -0
  55. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.h +114 -0
  56. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.c +650 -0
  57. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.h +48 -0
  58. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/BUILD.bazel +193 -0
  59. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/dist.bzl +190 -0
  60. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.c +247 -0
  61. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.h +39 -0
  62. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.c +522 -0
  63. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.h +66 -0
  64. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.c +1909 -0
  65. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.h +101 -0
  66. data/ext/ruby_memprofiler_pprof/vendor/upb/python/minimal_test.py +183 -0
  67. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/BUILD +70 -0
  68. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/README.md +11 -0
  69. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_database_test_wrapper.py +30 -0
  70. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_pool_test_wrapper.py +45 -0
  71. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_test_wrapper.py +46 -0
  72. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/generator_test_wrapper.py +30 -0
  73. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/json_format_test_wrapper.py +30 -0
  74. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/keywords_test_wrapper.py +30 -0
  75. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_factory_test_wrapper.py +37 -0
  76. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_test_wrapper.py +52 -0
  77. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/proto_builder_test_wrapper.py +32 -0
  78. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/pyproto_test_wrapper.bzl +36 -0
  79. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/reflection_test_wrapper.py +45 -0
  80. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/service_reflection_test_wrapper.py +30 -0
  81. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/symbol_database_test_wrapper.py +30 -0
  82. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_encoding_test_wrapper.py +30 -0
  83. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_format_test_wrapper.py +30 -0
  84. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/unknown_fields_test_wrapper.py +30 -0
  85. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/well_known_types_test_wrapper.py +36 -0
  86. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/wire_format_test_wrapper.py +30 -0
  87. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.c +350 -0
  88. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.h +230 -0
  89. data/ext/ruby_memprofiler_pprof/vendor/upb/python/py_extension.bzl +55 -0
  90. data/ext/ruby_memprofiler_pprof/vendor/upb/python/python_api.h +61 -0
  91. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.c +828 -0
  92. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.h +69 -0
  93. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.c +404 -0
  94. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.h +39 -0
  95. data/ext/ruby_memprofiler_pprof/vendor/upb/python/version_script.lds +6 -0
  96. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/LICENSE +32 -0
  97. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/README.google +9 -0
  98. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/console.lua +156 -0
  99. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/lunit.lua +725 -0
  100. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/BUILD +19 -0
  101. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/LICENSE +21 -0
  102. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/naive.c +92 -0
  103. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-neon.c +157 -0
  104. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-sse.c +170 -0
  105. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/utf8_range.h +9 -0
  106. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/BUILD.bazel +129 -0
  107. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/README.md +8 -0
  108. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/def.c +939 -0
  109. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/lua_proto_library.bzl +138 -0
  110. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/main.c +83 -0
  111. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/msg.c +1118 -0
  112. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test.proto +69 -0
  113. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test_upb.lua +846 -0
  114. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.c +258 -0
  115. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.h +132 -0
  116. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.lua +58 -0
  117. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upbc.cc +134 -0
  118. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.c +192 -0
  119. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.h +174 -0
  120. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb.c +346 -0
  121. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb_failures.txt +1 -0
  122. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.c +1221 -0
  123. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.h +94 -0
  124. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.c +1055 -0
  125. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.h +153 -0
  126. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_internal.h +211 -0
  127. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.c +3262 -0
  128. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.h +414 -0
  129. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.hpp +438 -0
  130. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/empty.proto +1 -0
  131. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.c +604 -0
  132. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.h +71 -0
  133. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/BUILD +13 -0
  134. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/file_descriptor_parsenew_fuzzer.cc +43 -0
  135. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.c +1509 -0
  136. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.h +47 -0
  137. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.c +776 -0
  138. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.h +62 -0
  139. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.c +1147 -0
  140. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.h +189 -0
  141. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.hpp +112 -0
  142. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.c +363 -0
  143. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.h +263 -0
  144. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_internal.h +59 -0
  145. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_test.cc +425 -0
  146. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_test.cc +230 -0
  147. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.c +428 -0
  148. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.h +114 -0
  149. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_internal.h +836 -0
  150. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.cc +491 -0
  151. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.proto +195 -0
  152. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_def.inc +261 -0
  153. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_undef.inc +62 -0
  154. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.c +323 -0
  155. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.h +109 -0
  156. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.hpp +37 -0
  157. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table.c +926 -0
  158. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table_internal.h +385 -0
  159. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test.proto +74 -0
  160. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.cc +186 -0
  161. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.proto +12 -0
  162. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_generated_code.cc +977 -0
  163. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_table.cc +580 -0
  164. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.c +472 -0
  165. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.h +64 -0
  166. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.c +362 -0
  167. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.h +378 -0
  168. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.hpp +115 -0
  169. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb_internal.h +68 -0
  170. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/BUILD +121 -0
  171. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/README.md +7 -0
  172. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.c +300 -0
  173. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.h +66 -0
  174. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare_test.cc +236 -0
  175. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.c +572 -0
  176. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.h +62 -0
  177. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_public_import_test.proto +32 -0
  178. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_regular_import_test.proto +36 -0
  179. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.cc +143 -0
  180. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.proto +119 -0
  181. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_weak_import_test.proto +28 -0
  182. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_wweak_import_test.proto +28 -0
  183. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.c +311 -0
  184. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.h +94 -0
  185. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.cc +202 -0
  186. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.proto +48 -0
  187. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/BUILD +78 -0
  188. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.cc +77 -0
  189. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.h +112 -0
  190. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upb.cc +1997 -0
  191. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upbdefs.cc +193 -0
  192. data/lib/ruby_memprofiler_pprof/atfork.rb +77 -0
  193. data/lib/ruby_memprofiler_pprof/block_flusher.rb +61 -0
  194. data/lib/ruby_memprofiler_pprof/file_flusher.rb +45 -0
  195. data/lib/ruby_memprofiler_pprof/profile_app.rb +30 -0
  196. data/lib/ruby_memprofiler_pprof/profile_data.rb +18 -0
  197. data/lib/ruby_memprofiler_pprof/version.rb +5 -0
  198. data/lib/ruby_memprofiler_pprof.rb +8 -0
  199. data/libexec/ruby_memprofiler_pprof_profile +16 -0
  200. metadata +257 -0
@@ -0,0 +1,828 @@
1
+ /*
2
+ * Copyright (c) 2009-2021, Google LLC
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ * * Redistributions of source code must retain the above copyright
8
+ * notice, this list of conditions and the following disclaimer.
9
+ * * Redistributions in binary form must reproduce the above copyright
10
+ * notice, this list of conditions and the following disclaimer in the
11
+ * documentation and/or other materials provided with the distribution.
12
+ * * Neither the name of Google LLC nor the
13
+ * names of its contributors may be used to endorse or promote products
14
+ * derived from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include "python/repeated.h"
29
+
30
+ #include "python/convert.h"
31
+ #include "python/message.h"
32
+ #include "python/protobuf.h"
33
+
34
+ static PyObject* PyUpb_RepeatedCompositeContainer_Append(PyObject* _self,
35
+ PyObject* value);
36
+ static PyObject* PyUpb_RepeatedScalarContainer_Append(PyObject* _self,
37
+ PyObject* value);
38
+
39
+ // For an expression like:
40
+ // foo[index]
41
+ //
42
+ // Converts `index` to an effective i/count/step, for a repeated field
43
+ // field of size `size`.
44
+ static bool IndexToRange(PyObject* index, Py_ssize_t size, Py_ssize_t* i,
45
+ Py_ssize_t* count, Py_ssize_t* step) {
46
+ assert(i && count && step);
47
+ if (PySlice_Check(index)) {
48
+ Py_ssize_t start, stop;
49
+ if (PySlice_Unpack(index, &start, &stop, step) < 0) return false;
50
+ *count = PySlice_AdjustIndices(size, &start, &stop, *step);
51
+ *i = start;
52
+ } else {
53
+ *i = PyNumber_AsSsize_t(index, PyExc_IndexError);
54
+
55
+ if (*i == -1 && PyErr_Occurred()) {
56
+ PyErr_SetString(PyExc_TypeError, "list indices must be integers");
57
+ return false;
58
+ }
59
+
60
+ if (*i < 0) *i += size;
61
+ *step = 0;
62
+ *count = 1;
63
+
64
+ if (*i < 0 || size <= *i) {
65
+ PyErr_Format(PyExc_IndexError, "list index out of range");
66
+ return false;
67
+ }
68
+ }
69
+ return true;
70
+ }
71
+
72
+ // Wrapper for a repeated field.
73
+ typedef struct {
74
+ PyObject_HEAD;
75
+ PyObject* arena;
76
+ // The field descriptor (PyObject*).
77
+ // The low bit indicates whether the container is reified (see ptr below).
78
+ // - low bit set: repeated field is a stub (no underlying data).
79
+ // - low bit clear: repeated field is reified (points to upb_Array).
80
+ uintptr_t field;
81
+ union {
82
+ PyObject* parent; // stub: owning pointer to parent message.
83
+ upb_Array* arr; // reified: the data for this array.
84
+ } ptr;
85
+ } PyUpb_RepeatedContainer;
86
+
87
+ static bool PyUpb_RepeatedContainer_IsStub(PyUpb_RepeatedContainer* self) {
88
+ return self->field & 1;
89
+ }
90
+
91
+ static PyObject* PyUpb_RepeatedContainer_GetFieldDescriptor(
92
+ PyUpb_RepeatedContainer* self) {
93
+ return (PyObject*)(self->field & ~(uintptr_t)1);
94
+ }
95
+
96
+ static const upb_FieldDef* PyUpb_RepeatedContainer_GetField(
97
+ PyUpb_RepeatedContainer* self) {
98
+ return PyUpb_FieldDescriptor_GetDef(
99
+ PyUpb_RepeatedContainer_GetFieldDescriptor(self));
100
+ }
101
+
102
+ // If the repeated field is reified, returns it. Otherwise, returns NULL.
103
+ // If NULL is returned, the object is empty and has no underlying data.
104
+ static upb_Array* PyUpb_RepeatedContainer_GetIfReified(
105
+ PyUpb_RepeatedContainer* self) {
106
+ return PyUpb_RepeatedContainer_IsStub(self) ? NULL : self->ptr.arr;
107
+ }
108
+
109
+ void PyUpb_RepeatedContainer_Reify(PyObject* _self, upb_Array* arr) {
110
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
111
+ assert(PyUpb_RepeatedContainer_IsStub(self));
112
+ if (!arr) {
113
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
114
+ upb_Arena* arena = PyUpb_Arena_Get(self->arena);
115
+ arr = upb_Array_New(arena, upb_FieldDef_CType(f));
116
+ }
117
+ PyUpb_ObjCache_Add(arr, &self->ob_base);
118
+ Py_DECREF(self->ptr.parent);
119
+ self->ptr.arr = arr; // Overwrites self->ptr.parent.
120
+ self->field &= ~(uintptr_t)1;
121
+ assert(!PyUpb_RepeatedContainer_IsStub(self));
122
+ }
123
+
124
+ upb_Array* PyUpb_RepeatedContainer_EnsureReified(PyObject* _self) {
125
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
126
+ upb_Array* arr = PyUpb_RepeatedContainer_GetIfReified(self);
127
+ if (arr) return arr; // Already writable.
128
+
129
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
130
+ upb_Arena* arena = PyUpb_Arena_Get(self->arena);
131
+ arr = upb_Array_New(arena, upb_FieldDef_CType(f));
132
+ PyUpb_CMessage_SetConcreteSubobj(self->ptr.parent, f,
133
+ (upb_MessageValue){.array_val = arr});
134
+ PyUpb_RepeatedContainer_Reify((PyObject*)self, arr);
135
+ return arr;
136
+ }
137
+
138
+ static void PyUpb_RepeatedContainer_Dealloc(PyObject* _self) {
139
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
140
+ Py_DECREF(self->arena);
141
+ if (PyUpb_RepeatedContainer_IsStub(self)) {
142
+ PyUpb_CMessage_CacheDelete(self->ptr.parent,
143
+ PyUpb_RepeatedContainer_GetField(self));
144
+ Py_DECREF(self->ptr.parent);
145
+ } else {
146
+ PyUpb_ObjCache_Delete(self->ptr.arr);
147
+ }
148
+ Py_DECREF(PyUpb_RepeatedContainer_GetFieldDescriptor(self));
149
+ PyUpb_Dealloc(self);
150
+ }
151
+
152
+ static PyTypeObject* PyUpb_RepeatedContainer_GetClass(const upb_FieldDef* f) {
153
+ assert(upb_FieldDef_IsRepeated(f) && !upb_FieldDef_IsMap(f));
154
+ PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
155
+ return upb_FieldDef_IsSubMessage(f) ? state->repeated_composite_container_type
156
+ : state->repeated_scalar_container_type;
157
+ }
158
+
159
+ static Py_ssize_t PyUpb_RepeatedContainer_Length(PyObject* self) {
160
+ upb_Array* arr =
161
+ PyUpb_RepeatedContainer_GetIfReified((PyUpb_RepeatedContainer*)self);
162
+ return arr ? upb_Array_Size(arr) : 0;
163
+ }
164
+
165
+ PyObject* PyUpb_RepeatedContainer_NewStub(PyObject* parent,
166
+ const upb_FieldDef* f,
167
+ PyObject* arena) {
168
+ // We only create stubs when the parent is reified, by convention. However
169
+ // this is not an invariant: the parent could become reified at any time.
170
+ assert(PyUpb_CMessage_GetIfReified(parent) == NULL);
171
+ PyTypeObject* cls = PyUpb_RepeatedContainer_GetClass(f);
172
+ PyUpb_RepeatedContainer* repeated = (void*)PyType_GenericAlloc(cls, 0);
173
+ repeated->arena = arena;
174
+ repeated->field = (uintptr_t)PyUpb_FieldDescriptor_Get(f) | 1;
175
+ repeated->ptr.parent = parent;
176
+ Py_INCREF(arena);
177
+ Py_INCREF(parent);
178
+ return &repeated->ob_base;
179
+ }
180
+
181
+ PyObject* PyUpb_RepeatedContainer_GetOrCreateWrapper(upb_Array* arr,
182
+ const upb_FieldDef* f,
183
+ PyObject* arena) {
184
+ PyObject* ret = PyUpb_ObjCache_Get(arr);
185
+ if (ret) return ret;
186
+
187
+ PyTypeObject* cls = PyUpb_RepeatedContainer_GetClass(f);
188
+ PyUpb_RepeatedContainer* repeated = (void*)PyType_GenericAlloc(cls, 0);
189
+ repeated->arena = arena;
190
+ repeated->field = (uintptr_t)PyUpb_FieldDescriptor_Get(f);
191
+ repeated->ptr.arr = arr;
192
+ ret = &repeated->ob_base;
193
+ Py_INCREF(arena);
194
+ PyUpb_ObjCache_Add(arr, ret);
195
+ return ret;
196
+ }
197
+
198
+ static PyObject* PyUpb_RepeatedContainer_MergeFrom(PyObject* _self,
199
+ PyObject* args);
200
+
201
+ PyObject* PyUpb_RepeatedContainer_DeepCopy(PyObject* _self, PyObject* value) {
202
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
203
+ PyUpb_RepeatedContainer* clone =
204
+ (void*)PyType_GenericAlloc(Py_TYPE(_self), 0);
205
+ if (clone == NULL) return NULL;
206
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
207
+ clone->arena = PyUpb_Arena_New();
208
+ clone->field = (uintptr_t)PyUpb_FieldDescriptor_Get(f);
209
+ clone->ptr.arr =
210
+ upb_Array_New(PyUpb_Arena_Get(clone->arena), upb_FieldDef_CType(f));
211
+ PyUpb_ObjCache_Add(clone->ptr.arr, (PyObject*)clone);
212
+ PyObject* result = PyUpb_RepeatedContainer_MergeFrom((PyObject*)clone, _self);
213
+ if (!result) {
214
+ Py_DECREF(clone);
215
+ return NULL;
216
+ }
217
+ Py_DECREF(result);
218
+ return (PyObject*)clone;
219
+ }
220
+
221
+ PyObject* PyUpb_RepeatedContainer_Extend(PyObject* _self, PyObject* value) {
222
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
223
+ upb_Array* arr = PyUpb_RepeatedContainer_EnsureReified(_self);
224
+ size_t start_size = upb_Array_Size(arr);
225
+ PyObject* it = PyObject_GetIter(value);
226
+ if (!it) {
227
+ PyErr_SetString(PyExc_TypeError, "Value must be iterable");
228
+ return NULL;
229
+ }
230
+
231
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
232
+ bool submsg = upb_FieldDef_IsSubMessage(f);
233
+ PyObject* e;
234
+
235
+ while ((e = PyIter_Next(it))) {
236
+ PyObject* ret;
237
+ if (submsg) {
238
+ ret = PyUpb_RepeatedCompositeContainer_Append(_self, e);
239
+ } else {
240
+ ret = PyUpb_RepeatedScalarContainer_Append(_self, e);
241
+ }
242
+ Py_XDECREF(ret);
243
+ Py_DECREF(e);
244
+ }
245
+
246
+ Py_DECREF(it);
247
+
248
+ if (PyErr_Occurred()) {
249
+ upb_Array_Resize(arr, start_size, NULL);
250
+ return NULL;
251
+ }
252
+
253
+ Py_RETURN_NONE;
254
+ }
255
+
256
+ static PyObject* PyUpb_RepeatedContainer_Item(PyObject* _self,
257
+ Py_ssize_t index) {
258
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
259
+ upb_Array* arr = PyUpb_RepeatedContainer_GetIfReified(self);
260
+ Py_ssize_t size = arr ? upb_Array_Size(arr) : 0;
261
+ if (index < 0 || index >= size) {
262
+ PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index);
263
+ return NULL;
264
+ }
265
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
266
+ return PyUpb_UpbToPy(upb_Array_Get(arr, index), f, self->arena);
267
+ }
268
+
269
+ PyObject* PyUpb_RepeatedContainer_ToList(PyObject* _self) {
270
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
271
+ upb_Array* arr = PyUpb_RepeatedContainer_GetIfReified(self);
272
+ if (!arr) return PyList_New(0);
273
+
274
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
275
+ size_t n = upb_Array_Size(arr);
276
+ PyObject* list = PyList_New(n);
277
+ for (size_t i = 0; i < n; i++) {
278
+ PyObject* val = PyUpb_UpbToPy(upb_Array_Get(arr, i), f, self->arena);
279
+ if (!val) {
280
+ Py_DECREF(list);
281
+ return NULL;
282
+ }
283
+ PyList_SetItem(list, i, val);
284
+ }
285
+ return list;
286
+ }
287
+
288
+ static PyObject* PyUpb_RepeatedContainer_Repr(PyObject* _self) {
289
+ PyObject* list = PyUpb_RepeatedContainer_ToList(_self);
290
+ if (!list) return NULL;
291
+ assert(!PyErr_Occurred());
292
+ PyObject* repr = PyObject_Repr(list);
293
+ Py_DECREF(list);
294
+ return repr;
295
+ }
296
+
297
+ static PyObject* PyUpb_RepeatedContainer_RichCompare(PyObject* _self,
298
+ PyObject* _other,
299
+ int opid) {
300
+ if (opid != Py_EQ && opid != Py_NE) {
301
+ Py_INCREF(Py_NotImplemented);
302
+ return Py_NotImplemented;
303
+ }
304
+ PyObject* list1 = PyUpb_RepeatedContainer_ToList(_self);
305
+ PyObject* list2 = _other;
306
+ PyObject* del = NULL;
307
+ if (PyObject_TypeCheck(_other, _self->ob_type)) {
308
+ del = list2 = PyUpb_RepeatedContainer_ToList(_other);
309
+ }
310
+ PyObject* ret = PyObject_RichCompare(list1, list2, opid);
311
+ Py_DECREF(list1);
312
+ Py_XDECREF(del);
313
+ return ret;
314
+ }
315
+
316
+ static PyObject* PyUpb_RepeatedContainer_Subscript(PyObject* _self,
317
+ PyObject* key) {
318
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
319
+ upb_Array* arr = PyUpb_RepeatedContainer_GetIfReified(self);
320
+ Py_ssize_t size = arr ? upb_Array_Size(arr) : 0;
321
+ Py_ssize_t idx, count, step;
322
+ if (!IndexToRange(key, size, &idx, &count, &step)) return NULL;
323
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
324
+ if (step == 0) {
325
+ return PyUpb_UpbToPy(upb_Array_Get(arr, idx), f, self->arena);
326
+ } else {
327
+ PyObject* list = PyList_New(count);
328
+ for (Py_ssize_t i = 0; i < count; i++, idx += step) {
329
+ upb_MessageValue msgval = upb_Array_Get(self->ptr.arr, idx);
330
+ PyObject* item = PyUpb_UpbToPy(msgval, f, self->arena);
331
+ if (!item) {
332
+ Py_DECREF(list);
333
+ return NULL;
334
+ }
335
+ PyList_SetItem(list, i, item);
336
+ }
337
+ return list;
338
+ }
339
+ }
340
+
341
+ static int PyUpb_RepeatedContainer_SetSubscript(
342
+ PyUpb_RepeatedContainer* self, upb_Array* arr, const upb_FieldDef* f,
343
+ Py_ssize_t idx, Py_ssize_t count, Py_ssize_t step, PyObject* value) {
344
+ upb_Arena* arena = PyUpb_Arena_Get(self->arena);
345
+ if (upb_FieldDef_IsSubMessage(f)) {
346
+ PyErr_SetString(PyExc_TypeError, "does not support assignment");
347
+ return -1;
348
+ }
349
+
350
+ if (step == 0) {
351
+ // Set single value.
352
+ upb_MessageValue msgval;
353
+ if (!PyUpb_PyToUpb(value, f, &msgval, arena)) return -1;
354
+ upb_Array_Set(arr, idx, msgval);
355
+ return 0;
356
+ }
357
+
358
+ // Set range.
359
+ PyObject* seq =
360
+ PySequence_Fast(value, "must assign iterable to extended slice");
361
+ PyObject* item = NULL;
362
+ int ret = -1;
363
+ if (!seq) goto err;
364
+ Py_ssize_t seq_size = PySequence_Size(seq);
365
+ if (seq_size != count) {
366
+ if (step == 1) {
367
+ // We must shift the tail elements (either right or left).
368
+ size_t tail = upb_Array_Size(arr) - (idx + count);
369
+ upb_Array_Resize(arr, idx + seq_size + tail, arena);
370
+ upb_Array_Move(arr, idx + seq_size, idx + count, tail);
371
+ count = seq_size;
372
+ } else {
373
+ PyErr_Format(PyExc_ValueError,
374
+ "attempt to assign sequence of %zd to extended slice "
375
+ "of size %zd",
376
+ seq_size, count);
377
+ goto err;
378
+ }
379
+ }
380
+ for (Py_ssize_t i = 0; i < count; i++, idx += step) {
381
+ upb_MessageValue msgval;
382
+ item = PySequence_GetItem(seq, i);
383
+ if (!item) goto err;
384
+ // XXX: if this fails we can leave the list partially mutated.
385
+ if (!PyUpb_PyToUpb(item, f, &msgval, arena)) goto err;
386
+ Py_DECREF(item);
387
+ item = NULL;
388
+ upb_Array_Set(arr, idx, msgval);
389
+ }
390
+ ret = 0;
391
+
392
+ err:
393
+ Py_XDECREF(seq);
394
+ Py_XDECREF(item);
395
+ return ret;
396
+ }
397
+
398
+ static int PyUpb_RepeatedContainer_DeleteSubscript(upb_Array* arr,
399
+ Py_ssize_t idx,
400
+ Py_ssize_t count,
401
+ Py_ssize_t step) {
402
+ // Normalize direction: deletion is order-independent.
403
+ Py_ssize_t start = idx;
404
+ if (step < 0) {
405
+ Py_ssize_t end = start + step * (count - 1);
406
+ start = end;
407
+ step = -step;
408
+ }
409
+
410
+ size_t dst = start;
411
+ size_t src;
412
+ if (step > 1) {
413
+ // Move elements between steps:
414
+ //
415
+ // src
416
+ // |
417
+ // |------X---X---X---X------------------------------|
418
+ // |
419
+ // dst <-------- tail -------------->
420
+ src = start + 1;
421
+ for (Py_ssize_t i = 1; i < count; i++, dst += step - 1, src += step) {
422
+ upb_Array_Move(arr, dst, src, step);
423
+ }
424
+ } else {
425
+ src = start + count;
426
+ }
427
+
428
+ // Move tail.
429
+ size_t tail = upb_Array_Size(arr) - src;
430
+ size_t new_size = dst + tail;
431
+ assert(new_size == upb_Array_Size(arr) - count);
432
+ upb_Array_Move(arr, dst, src, tail);
433
+ upb_Array_Resize(arr, new_size, NULL);
434
+ return 0;
435
+ }
436
+
437
+ static int PyUpb_RepeatedContainer_AssignSubscript(PyObject* _self,
438
+ PyObject* key,
439
+ PyObject* value) {
440
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
441
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
442
+ upb_Array* arr = PyUpb_RepeatedContainer_GetIfReified(self);
443
+ Py_ssize_t size = arr ? upb_Array_Size(arr) : 0;
444
+ Py_ssize_t idx, count, step;
445
+ if (!IndexToRange(key, size, &idx, &count, &step)) return -1;
446
+ if (value) {
447
+ return PyUpb_RepeatedContainer_SetSubscript(self, arr, f, idx, count, step,
448
+ value);
449
+ } else {
450
+ return PyUpb_RepeatedContainer_DeleteSubscript(arr, idx, count, step);
451
+ }
452
+ }
453
+
454
+ static PyObject* PyUpb_RepeatedContainer_Pop(PyObject* _self, PyObject* args) {
455
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
456
+ Py_ssize_t index = -1;
457
+ if (!PyArg_ParseTuple(args, "|n", &index)) return NULL;
458
+ upb_Array* arr = PyUpb_RepeatedContainer_EnsureReified(_self);
459
+ size_t size = upb_Array_Size(arr);
460
+ if (index < 0) index += size;
461
+ if (index >= size) index = size - 1;
462
+ PyObject* ret = PyUpb_RepeatedContainer_Item(_self, index);
463
+ if (!ret) return NULL;
464
+ upb_Array_Delete(self->ptr.arr, index, 1);
465
+ return ret;
466
+ }
467
+
468
+ static PyObject* PyUpb_RepeatedContainer_Remove(PyObject* _self,
469
+ PyObject* value) {
470
+ upb_Array* arr = PyUpb_RepeatedContainer_EnsureReified(_self);
471
+ Py_ssize_t match_index = -1;
472
+ Py_ssize_t n = PyUpb_RepeatedContainer_Length(_self);
473
+ for (Py_ssize_t i = 0; i < n; ++i) {
474
+ PyObject* elem = PyUpb_RepeatedContainer_Item(_self, i);
475
+ if (!elem) return NULL;
476
+ int eq = PyObject_RichCompareBool(elem, value, Py_EQ);
477
+ Py_DECREF(elem);
478
+ if (eq) {
479
+ match_index = i;
480
+ break;
481
+ }
482
+ }
483
+ if (match_index == -1) {
484
+ PyErr_SetString(PyExc_ValueError, "remove(x): x not in container");
485
+ return NULL;
486
+ }
487
+ if (PyUpb_RepeatedContainer_DeleteSubscript(arr, match_index, 1, 1) < 0) {
488
+ return NULL;
489
+ }
490
+ Py_RETURN_NONE;
491
+ }
492
+
493
+ // A helper function used only for Sort().
494
+ static bool PyUpb_RepeatedContainer_Assign(PyObject* _self, PyObject* list) {
495
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
496
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
497
+ upb_Array* arr = PyUpb_RepeatedContainer_EnsureReified(_self);
498
+ Py_ssize_t size = PyList_Size(list);
499
+ bool submsg = upb_FieldDef_IsSubMessage(f);
500
+ upb_Arena* arena = PyUpb_Arena_Get(self->arena);
501
+ for (Py_ssize_t i = 0; i < size; ++i) {
502
+ PyObject* obj = PyList_GetItem(list, i);
503
+ upb_MessageValue msgval;
504
+ if (submsg) {
505
+ msgval.msg_val = PyUpb_CMessage_GetIfReified(obj);
506
+ assert(msgval.msg_val);
507
+ } else {
508
+ if (!PyUpb_PyToUpb(obj, f, &msgval, arena)) return false;
509
+ }
510
+ upb_Array_Set(arr, i, msgval);
511
+ }
512
+ return true;
513
+ }
514
+
515
+ static PyObject* PyUpb_RepeatedContainer_Sort(PyObject* pself, PyObject* args,
516
+ PyObject* kwds) {
517
+ // Support the old sort_function argument for backwards
518
+ // compatibility.
519
+ if (kwds != NULL) {
520
+ PyObject* sort_func = PyDict_GetItemString(kwds, "sort_function");
521
+ if (sort_func != NULL) {
522
+ // Must set before deleting as sort_func is a borrowed reference
523
+ // and kwds might be the only thing keeping it alive.
524
+ if (PyDict_SetItemString(kwds, "cmp", sort_func) == -1) return NULL;
525
+ if (PyDict_DelItemString(kwds, "sort_function") == -1) return NULL;
526
+ }
527
+ }
528
+
529
+ PyObject* ret = NULL;
530
+ PyObject* full_slice = NULL;
531
+ PyObject* list = NULL;
532
+ PyObject* m = NULL;
533
+ PyObject* res = NULL;
534
+ if ((full_slice = PySlice_New(NULL, NULL, NULL)) &&
535
+ (list = PyUpb_RepeatedContainer_Subscript(pself, full_slice)) &&
536
+ (m = PyObject_GetAttrString(list, "sort")) &&
537
+ (res = PyObject_Call(m, args, kwds)) &&
538
+ PyUpb_RepeatedContainer_Assign(pself, list)) {
539
+ Py_INCREF(Py_None);
540
+ ret = Py_None;
541
+ }
542
+
543
+ Py_XDECREF(full_slice);
544
+ Py_XDECREF(list);
545
+ Py_XDECREF(m);
546
+ Py_XDECREF(res);
547
+ return ret;
548
+ }
549
+
550
+ static PyObject* PyUpb_RepeatedContainer_Reverse(PyObject* _self) {
551
+ upb_Array* arr = PyUpb_RepeatedContainer_EnsureReified(_self);
552
+ size_t n = upb_Array_Size(arr);
553
+ size_t half = n / 2; // Rounds down.
554
+ for (size_t i = 0; i < half; i++) {
555
+ size_t i2 = n - i - 1;
556
+ upb_MessageValue val1 = upb_Array_Get(arr, i);
557
+ upb_MessageValue val2 = upb_Array_Get(arr, i2);
558
+ upb_Array_Set(arr, i, val2);
559
+ upb_Array_Set(arr, i2, val1);
560
+ }
561
+ Py_RETURN_NONE;
562
+ }
563
+
564
+ static PyObject* PyUpb_RepeatedContainer_MergeFrom(PyObject* _self,
565
+ PyObject* args) {
566
+ return PyUpb_RepeatedContainer_Extend(_self, args);
567
+ }
568
+
569
+ // -----------------------------------------------------------------------------
570
+ // RepeatedCompositeContainer
571
+ // -----------------------------------------------------------------------------
572
+
573
+ static PyObject* PyUpb_RepeatedCompositeContainer_AppendNew(PyObject* _self) {
574
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
575
+ upb_Array* arr = PyUpb_RepeatedContainer_EnsureReified(_self);
576
+ if (!arr) return NULL;
577
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
578
+ upb_Arena* arena = PyUpb_Arena_Get(self->arena);
579
+ const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
580
+ upb_Message* msg = upb_Message_New(m, arena);
581
+ upb_MessageValue msgval = {.msg_val = msg};
582
+ upb_Array_Append(arr, msgval, arena);
583
+ return PyUpb_CMessage_Get(msg, m, self->arena);
584
+ }
585
+
586
+ PyObject* PyUpb_RepeatedCompositeContainer_Add(PyObject* _self, PyObject* args,
587
+ PyObject* kwargs) {
588
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
589
+ PyObject* py_msg = PyUpb_RepeatedCompositeContainer_AppendNew(_self);
590
+ if (!py_msg) return NULL;
591
+ if (PyUpb_CMessage_InitAttributes(py_msg, args, kwargs) < 0) {
592
+ Py_DECREF(py_msg);
593
+ upb_Array_Delete(self->ptr.arr, upb_Array_Size(self->ptr.arr) - 1, 1);
594
+ return NULL;
595
+ }
596
+ return py_msg;
597
+ }
598
+
599
+ static PyObject* PyUpb_RepeatedCompositeContainer_Append(PyObject* _self,
600
+ PyObject* value) {
601
+ if (!PyUpb_CMessage_Verify(value)) return NULL;
602
+ PyObject* py_msg = PyUpb_RepeatedCompositeContainer_AppendNew(_self);
603
+ if (!py_msg) return NULL;
604
+ PyObject* none = PyUpb_CMessage_MergeFrom(py_msg, value);
605
+ if (!none) {
606
+ Py_DECREF(py_msg);
607
+ return NULL;
608
+ }
609
+ Py_DECREF(none);
610
+ return py_msg;
611
+ }
612
+
613
+ static PyObject* PyUpb_RepeatedContainer_Insert(PyObject* _self,
614
+ PyObject* args) {
615
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
616
+ Py_ssize_t index;
617
+ PyObject* value;
618
+ if (!PyArg_ParseTuple(args, "nO", &index, &value)) return NULL;
619
+ upb_Array* arr = PyUpb_RepeatedContainer_EnsureReified(_self);
620
+ if (!arr) return NULL;
621
+
622
+ // Normalize index.
623
+ Py_ssize_t size = upb_Array_Size(arr);
624
+ if (index < 0) index += size;
625
+ if (index < 0) index = 0;
626
+ if (index > size) index = size;
627
+
628
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
629
+ upb_MessageValue msgval;
630
+ upb_Arena* arena = PyUpb_Arena_Get(self->arena);
631
+ if (upb_FieldDef_IsSubMessage(f)) {
632
+ // Create message.
633
+ const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
634
+ upb_Message* msg = upb_Message_New(m, arena);
635
+ PyObject* py_msg = PyUpb_CMessage_Get(msg, m, self->arena);
636
+ PyObject* ret = PyUpb_CMessage_MergeFrom(py_msg, value);
637
+ Py_DECREF(py_msg);
638
+ if (!ret) return NULL;
639
+ Py_DECREF(ret);
640
+ msgval.msg_val = msg;
641
+ } else {
642
+ if (!PyUpb_PyToUpb(value, f, &msgval, arena)) return NULL;
643
+ }
644
+
645
+ upb_Array_Insert(arr, index, 1, arena);
646
+ upb_Array_Set(arr, index, msgval);
647
+
648
+ Py_RETURN_NONE;
649
+ }
650
+
651
+ static PyMethodDef PyUpb_RepeatedCompositeContainer_Methods[] = {
652
+ {"__deepcopy__", PyUpb_RepeatedContainer_DeepCopy, METH_VARARGS,
653
+ "Makes a deep copy of the class."},
654
+ {"add", (PyCFunction)PyUpb_RepeatedCompositeContainer_Add,
655
+ METH_VARARGS | METH_KEYWORDS, "Adds an object to the repeated container."},
656
+ {"append", PyUpb_RepeatedCompositeContainer_Append, METH_O,
657
+ "Appends a message to the end of the repeated container."},
658
+ {"insert", PyUpb_RepeatedContainer_Insert, METH_VARARGS,
659
+ "Inserts a message before the specified index."},
660
+ {"extend", PyUpb_RepeatedContainer_Extend, METH_O,
661
+ "Adds objects to the repeated container."},
662
+ {"pop", PyUpb_RepeatedContainer_Pop, METH_VARARGS,
663
+ "Removes an object from the repeated container and returns it."},
664
+ {"remove", PyUpb_RepeatedContainer_Remove, METH_O,
665
+ "Removes an object from the repeated container."},
666
+ {"sort", (PyCFunction)PyUpb_RepeatedContainer_Sort,
667
+ METH_VARARGS | METH_KEYWORDS, "Sorts the repeated container."},
668
+ {"reverse", (PyCFunction)PyUpb_RepeatedContainer_Reverse, METH_NOARGS,
669
+ "Reverses elements order of the repeated container."},
670
+ {"MergeFrom", PyUpb_RepeatedContainer_MergeFrom, METH_O,
671
+ "Adds objects to the repeated container."},
672
+ {NULL, NULL}};
673
+
674
+ static PyType_Slot PyUpb_RepeatedCompositeContainer_Slots[] = {
675
+ {Py_tp_dealloc, PyUpb_RepeatedContainer_Dealloc},
676
+ {Py_tp_methods, PyUpb_RepeatedCompositeContainer_Methods},
677
+ {Py_sq_length, PyUpb_RepeatedContainer_Length},
678
+ {Py_sq_item, PyUpb_RepeatedContainer_Item},
679
+ {Py_mp_length, PyUpb_RepeatedContainer_Length},
680
+ {Py_tp_repr, PyUpb_RepeatedContainer_Repr},
681
+ {Py_mp_subscript, PyUpb_RepeatedContainer_Subscript},
682
+ {Py_mp_ass_subscript, PyUpb_RepeatedContainer_AssignSubscript},
683
+ {Py_tp_new, PyUpb_Forbidden_New},
684
+ {Py_tp_richcompare, PyUpb_RepeatedContainer_RichCompare},
685
+ {Py_tp_hash, PyObject_HashNotImplemented},
686
+ {0, NULL}};
687
+
688
+ static PyType_Spec PyUpb_RepeatedCompositeContainer_Spec = {
689
+ PYUPB_MODULE_NAME ".RepeatedCompositeContainer",
690
+ sizeof(PyUpb_RepeatedContainer),
691
+ 0, // tp_itemsize
692
+ Py_TPFLAGS_DEFAULT,
693
+ PyUpb_RepeatedCompositeContainer_Slots,
694
+ };
695
+
696
+ // -----------------------------------------------------------------------------
697
+ // RepeatedScalarContainer
698
+ // -----------------------------------------------------------------------------
699
+
700
+ static PyObject* PyUpb_RepeatedScalarContainer_Append(PyObject* _self,
701
+ PyObject* value) {
702
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
703
+ upb_Array* arr = PyUpb_RepeatedContainer_EnsureReified(_self);
704
+ upb_Arena* arena = PyUpb_Arena_Get(self->arena);
705
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
706
+ upb_MessageValue msgval;
707
+ if (!PyUpb_PyToUpb(value, f, &msgval, arena)) {
708
+ return NULL;
709
+ }
710
+ upb_Array_Append(arr, msgval, arena);
711
+ Py_RETURN_NONE;
712
+ }
713
+
714
+ static int PyUpb_RepeatedScalarContainer_AssignItem(PyObject* _self,
715
+ Py_ssize_t index,
716
+ PyObject* item) {
717
+ PyUpb_RepeatedContainer* self = (PyUpb_RepeatedContainer*)_self;
718
+ upb_Array* arr = PyUpb_RepeatedContainer_GetIfReified(self);
719
+ Py_ssize_t size = arr ? upb_Array_Size(arr) : 0;
720
+ if (index < 0 || index >= size) {
721
+ PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index);
722
+ return -1;
723
+ }
724
+ const upb_FieldDef* f = PyUpb_RepeatedContainer_GetField(self);
725
+ upb_MessageValue msgval;
726
+ upb_Arena* arena = PyUpb_Arena_Get(self->arena);
727
+ if (!PyUpb_PyToUpb(item, f, &msgval, arena)) {
728
+ return -1;
729
+ }
730
+ upb_Array_Set(self->ptr.arr, index, msgval);
731
+ return 0;
732
+ }
733
+
734
+ static PyObject* PyUpb_RepeatedScalarContainer_Reduce(PyObject* unused_self,
735
+ PyObject* unused_other) {
736
+ PyObject* pickle_module = PyImport_ImportModule("pickle");
737
+ if (!pickle_module) return NULL;
738
+ PyObject* pickle_error = PyObject_GetAttrString(pickle_module, "PickleError");
739
+ Py_DECREF(pickle_module);
740
+ if (!pickle_error) return NULL;
741
+ PyErr_Format(pickle_error,
742
+ "can't pickle repeated message fields, convert to list first");
743
+ Py_DECREF(pickle_error);
744
+ return NULL;
745
+ }
746
+
747
+ static PyMethodDef PyUpb_RepeatedScalarContainer_Methods[] = {
748
+ {"__deepcopy__", PyUpb_RepeatedContainer_DeepCopy, METH_VARARGS,
749
+ "Makes a deep copy of the class."},
750
+ {"__reduce__", PyUpb_RepeatedScalarContainer_Reduce, METH_NOARGS,
751
+ "Outputs picklable representation of the repeated field."},
752
+ {"append", PyUpb_RepeatedScalarContainer_Append, METH_O,
753
+ "Appends an object to the repeated container."},
754
+ {"extend", PyUpb_RepeatedContainer_Extend, METH_O,
755
+ "Appends objects to the repeated container."},
756
+ {"insert", PyUpb_RepeatedContainer_Insert, METH_VARARGS,
757
+ "Inserts an object at the specified position in the container."},
758
+ {"pop", PyUpb_RepeatedContainer_Pop, METH_VARARGS,
759
+ "Removes an object from the repeated container and returns it."},
760
+ {"remove", PyUpb_RepeatedContainer_Remove, METH_O,
761
+ "Removes an object from the repeated container."},
762
+ {"sort", (PyCFunction)PyUpb_RepeatedContainer_Sort,
763
+ METH_VARARGS | METH_KEYWORDS, "Sorts the repeated container."},
764
+ {"reverse", (PyCFunction)PyUpb_RepeatedContainer_Reverse, METH_NOARGS,
765
+ "Reverses elements order of the repeated container."},
766
+ {"MergeFrom", PyUpb_RepeatedContainer_MergeFrom, METH_O,
767
+ "Merges a repeated container into the current container."},
768
+ {NULL, NULL}};
769
+
770
+ static PyType_Slot PyUpb_RepeatedScalarContainer_Slots[] = {
771
+ {Py_tp_dealloc, PyUpb_RepeatedContainer_Dealloc},
772
+ {Py_tp_methods, PyUpb_RepeatedScalarContainer_Methods},
773
+ {Py_tp_new, PyUpb_Forbidden_New},
774
+ {Py_tp_repr, PyUpb_RepeatedContainer_Repr},
775
+ {Py_sq_length, PyUpb_RepeatedContainer_Length},
776
+ {Py_sq_item, PyUpb_RepeatedContainer_Item},
777
+ {Py_sq_ass_item, PyUpb_RepeatedScalarContainer_AssignItem},
778
+ {Py_mp_length, PyUpb_RepeatedContainer_Length},
779
+ {Py_mp_subscript, PyUpb_RepeatedContainer_Subscript},
780
+ {Py_mp_ass_subscript, PyUpb_RepeatedContainer_AssignSubscript},
781
+ {Py_tp_richcompare, PyUpb_RepeatedContainer_RichCompare},
782
+ {Py_tp_hash, PyObject_HashNotImplemented},
783
+ {0, NULL}};
784
+
785
+ static PyType_Spec PyUpb_RepeatedScalarContainer_Spec = {
786
+ PYUPB_MODULE_NAME ".RepeatedScalarContainer",
787
+ sizeof(PyUpb_RepeatedContainer),
788
+ 0, // tp_itemsize
789
+ Py_TPFLAGS_DEFAULT,
790
+ PyUpb_RepeatedScalarContainer_Slots,
791
+ };
792
+
793
+ // -----------------------------------------------------------------------------
794
+ // Top Level
795
+ // -----------------------------------------------------------------------------
796
+
797
+ static bool PyUpb_Repeated_RegisterAsSequence(PyUpb_ModuleState* state) {
798
+ PyObject* collections = NULL;
799
+ PyObject* seq = NULL;
800
+ PyObject* ret1 = NULL;
801
+ PyObject* ret2 = NULL;
802
+ PyTypeObject* type1 = state->repeated_scalar_container_type;
803
+ PyTypeObject* type2 = state->repeated_composite_container_type;
804
+
805
+ bool ok = (collections = PyImport_ImportModule("collections.abc")) &&
806
+ (seq = PyObject_GetAttrString(collections, "MutableSequence")) &&
807
+ (ret1 = PyObject_CallMethod(seq, "register", "O", type1)) &&
808
+ (ret2 = PyObject_CallMethod(seq, "register", "O", type2));
809
+
810
+ Py_XDECREF(collections);
811
+ Py_XDECREF(seq);
812
+ Py_XDECREF(ret1);
813
+ Py_XDECREF(ret2);
814
+ return ok;
815
+ }
816
+
817
+ bool PyUpb_Repeated_Init(PyObject* m) {
818
+ PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m);
819
+
820
+ state->repeated_composite_container_type =
821
+ PyUpb_AddClass(m, &PyUpb_RepeatedCompositeContainer_Spec);
822
+ state->repeated_scalar_container_type =
823
+ PyUpb_AddClass(m, &PyUpb_RepeatedScalarContainer_Spec);
824
+
825
+ return state->repeated_composite_container_type &&
826
+ state->repeated_scalar_container_type &&
827
+ PyUpb_Repeated_RegisterAsSequence(state);
828
+ }