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,704 @@
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 "descriptor_containers.h"
29
+
30
+ #include "descriptor.h"
31
+ #include "protobuf.h"
32
+ #include "upb/def.h"
33
+
34
+ // -----------------------------------------------------------------------------
35
+ // DescriptorIterator
36
+ // -----------------------------------------------------------------------------
37
+
38
+ typedef struct {
39
+ PyObject_HEAD;
40
+ const PyUpb_GenericSequence_Funcs* funcs;
41
+ const void* parent; // upb_MessageDef*, upb_DefPool*, etc.
42
+ PyObject* parent_obj; // Python object that keeps parent alive, we own a ref.
43
+ int index; // Current iterator index.
44
+ } PyUpb_DescriptorIterator;
45
+
46
+ PyUpb_DescriptorIterator* PyUpb_DescriptorIterator_Self(PyObject* obj) {
47
+ assert(Py_TYPE(obj) == PyUpb_ModuleState_Get()->descriptor_iterator_type);
48
+ return (PyUpb_DescriptorIterator*)obj;
49
+ }
50
+
51
+ static void PyUpb_DescriptorIterator_Dealloc(PyObject* _self) {
52
+ PyUpb_DescriptorIterator* self = PyUpb_DescriptorIterator_Self(_self);
53
+ Py_DECREF(self->parent_obj);
54
+ PyUpb_Dealloc(self);
55
+ }
56
+
57
+ PyObject* PyUpb_DescriptorIterator_New(const PyUpb_GenericSequence_Funcs* funcs,
58
+ const void* parent,
59
+ PyObject* parent_obj) {
60
+ PyUpb_ModuleState* s = PyUpb_ModuleState_Get();
61
+ PyUpb_DescriptorIterator* iter =
62
+ (void*)PyType_GenericAlloc(s->descriptor_iterator_type, 0);
63
+ iter->funcs = funcs;
64
+ iter->parent = parent;
65
+ iter->parent_obj = parent_obj;
66
+ iter->index = 0;
67
+ Py_INCREF(iter->parent_obj);
68
+ return &iter->ob_base;
69
+ }
70
+
71
+ PyObject* PyUpb_DescriptorIterator_IterNext(PyObject* _self) {
72
+ PyUpb_DescriptorIterator* self = PyUpb_DescriptorIterator_Self(_self);
73
+ int size = self->funcs->get_elem_count(self->parent);
74
+ if (self->index >= size) return NULL;
75
+ const void* elem = self->funcs->index(self->parent, self->index);
76
+ self->index++;
77
+ return self->funcs->get_elem_wrapper(elem);
78
+ }
79
+
80
+ static PyType_Slot PyUpb_DescriptorIterator_Slots[] = {
81
+ {Py_tp_dealloc, PyUpb_DescriptorIterator_Dealloc},
82
+ {Py_tp_iter, PyObject_SelfIter},
83
+ {Py_tp_iternext, PyUpb_DescriptorIterator_IterNext},
84
+ {0, NULL}};
85
+
86
+ static PyType_Spec PyUpb_DescriptorIterator_Spec = {
87
+ PYUPB_MODULE_NAME ".DescriptorIterator", // tp_name
88
+ sizeof(PyUpb_DescriptorIterator), // tp_basicsize
89
+ 0, // tp_itemsize
90
+ Py_TPFLAGS_DEFAULT, // tp_flags
91
+ PyUpb_DescriptorIterator_Slots,
92
+ };
93
+
94
+ // -----------------------------------------------------------------------------
95
+ // GenericSequence
96
+ // -----------------------------------------------------------------------------
97
+
98
+ typedef struct {
99
+ PyObject_HEAD;
100
+ const PyUpb_GenericSequence_Funcs* funcs;
101
+ const void* parent; // upb_MessageDef*, upb_DefPool*, etc.
102
+ PyObject* parent_obj; // Python object that keeps parent alive, we own a ref.
103
+ } PyUpb_GenericSequence;
104
+
105
+ PyUpb_GenericSequence* PyUpb_GenericSequence_Self(PyObject* obj) {
106
+ assert(Py_TYPE(obj) == PyUpb_ModuleState_Get()->generic_sequence_type);
107
+ return (PyUpb_GenericSequence*)obj;
108
+ }
109
+
110
+ static void PyUpb_GenericSequence_Dealloc(PyObject* _self) {
111
+ PyUpb_GenericSequence* self = PyUpb_GenericSequence_Self(_self);
112
+ Py_CLEAR(self->parent_obj);
113
+ PyUpb_Dealloc(self);
114
+ }
115
+
116
+ PyObject* PyUpb_GenericSequence_New(const PyUpb_GenericSequence_Funcs* funcs,
117
+ const void* parent, PyObject* parent_obj) {
118
+ PyUpb_ModuleState* s = PyUpb_ModuleState_Get();
119
+ PyUpb_GenericSequence* seq =
120
+ (PyUpb_GenericSequence*)PyType_GenericAlloc(s->generic_sequence_type, 0);
121
+ seq->funcs = funcs;
122
+ seq->parent = parent;
123
+ seq->parent_obj = parent_obj;
124
+ Py_INCREF(parent_obj);
125
+ return &seq->ob_base;
126
+ }
127
+
128
+ static Py_ssize_t PyUpb_GenericSequence_Length(PyObject* _self) {
129
+ PyUpb_GenericSequence* self = PyUpb_GenericSequence_Self(_self);
130
+ return self->funcs->get_elem_count(self->parent);
131
+ }
132
+
133
+ static PyObject* PyUpb_GenericSequence_GetItem(PyObject* _self,
134
+ Py_ssize_t index) {
135
+ PyUpb_GenericSequence* self = PyUpb_GenericSequence_Self(_self);
136
+ Py_ssize_t size = self->funcs->get_elem_count(self->parent);
137
+ if (index < 0 || index >= size) {
138
+ PyErr_Format(PyExc_IndexError, "list index (%zd) out of range", index);
139
+ return NULL;
140
+ }
141
+ const void* elem = self->funcs->index(self->parent, index);
142
+ return self->funcs->get_elem_wrapper(elem);
143
+ }
144
+
145
+ // A sequence container can only be equal to another sequence container, or (for
146
+ // backward compatibility) to a list containing the same items.
147
+ // Returns 1 if equal, 0 if unequal, -1 on error.
148
+ static int PyUpb_GenericSequence_IsEqual(PyUpb_GenericSequence* self,
149
+ PyObject* other) {
150
+ // Check the identity of C++ pointers.
151
+ if (PyObject_TypeCheck(other, Py_TYPE(self))) {
152
+ PyUpb_GenericSequence* other_seq = (void*)other;
153
+ return self->parent == other_seq->parent && self->funcs == other_seq->funcs;
154
+ }
155
+
156
+ if (!PyList_Check(other)) return 0;
157
+
158
+ // return list(self) == other
159
+ // We can clamp `i` to int because GenericSequence uses int for size (this
160
+ // is useful when we do int iteration below).
161
+ int n = PyUpb_GenericSequence_Length((PyObject*)self);
162
+ if ((Py_ssize_t)n != PyList_Size(other)) {
163
+ return false;
164
+ }
165
+
166
+ PyObject* item1;
167
+ for (int i = 0; i < n; i++) {
168
+ item1 = PyUpb_GenericSequence_GetItem((PyObject*)self, i);
169
+ PyObject* item2 = PyList_GetItem(other, i);
170
+ if (!item1 || !item2) goto error;
171
+ int cmp = PyObject_RichCompareBool(item1, item2, Py_EQ);
172
+ Py_DECREF(item1);
173
+ if (cmp != 1) return cmp;
174
+ }
175
+ // All items were found and equal
176
+ return 1;
177
+
178
+ error:
179
+ Py_XDECREF(item1);
180
+ return -1;
181
+ }
182
+
183
+ static PyObject* PyUpb_GenericSequence_RichCompare(PyObject* _self,
184
+ PyObject* other, int opid) {
185
+ PyUpb_GenericSequence* self = PyUpb_GenericSequence_Self(_self);
186
+ if (opid != Py_EQ && opid != Py_NE) {
187
+ Py_RETURN_NOTIMPLEMENTED;
188
+ }
189
+ bool ret = PyUpb_GenericSequence_IsEqual(self, other);
190
+ if (opid == Py_NE) ret = !ret;
191
+ return PyBool_FromLong(ret);
192
+ }
193
+
194
+ // Linear search. Could optimize this in some cases (defs that have index),
195
+ // but not all (FileDescriptor.dependencies).
196
+ static int PyUpb_GenericSequence_Find(PyObject* _self, PyObject* item) {
197
+ PyUpb_GenericSequence* self = PyUpb_GenericSequence_Self(_self);
198
+ const void* item_ptr = PyUpb_AnyDescriptor_GetDef(item);
199
+ int count = self->funcs->get_elem_count(self->parent);
200
+ for (int i = 0; i < count; i++) {
201
+ if (self->funcs->index(self->parent, i) == item_ptr) {
202
+ return i;
203
+ }
204
+ }
205
+ return -1;
206
+ }
207
+
208
+ static PyObject* PyUpb_GenericSequence_Index(PyObject* self, PyObject* item) {
209
+ int position = PyUpb_GenericSequence_Find(self, item);
210
+ if (position < 0) {
211
+ PyErr_SetNone(PyExc_ValueError);
212
+ return NULL;
213
+ } else {
214
+ return PyLong_FromLong(position);
215
+ }
216
+ }
217
+
218
+ static PyObject* PyUpb_GenericSequence_Count(PyObject* _self, PyObject* item) {
219
+ PyUpb_GenericSequence* self = PyUpb_GenericSequence_Self(_self);
220
+ const void* item_ptr = PyUpb_AnyDescriptor_GetDef(item);
221
+ int n = self->funcs->get_elem_count(self->parent);
222
+ int count = 0;
223
+ for (int i = 0; i < n; i++) {
224
+ if (self->funcs->index(self->parent, i) == item_ptr) {
225
+ count++;
226
+ }
227
+ }
228
+ return PyLong_FromLong(count);
229
+ }
230
+
231
+ static PyObject* PyUpb_GenericSequence_Append(PyObject* self, PyObject* args) {
232
+ PyErr_Format(PyExc_TypeError, "'%R' is not a mutable sequence", self);
233
+ return NULL;
234
+ }
235
+
236
+ static PyMethodDef PyUpb_GenericSequence_Methods[] = {
237
+ {"index", PyUpb_GenericSequence_Index, METH_O},
238
+ {"count", PyUpb_GenericSequence_Count, METH_O},
239
+ {"append", PyUpb_GenericSequence_Append, METH_O},
240
+ // This was implemented for Python/C++ but so far has not been required.
241
+ //{ "__reversed__", (PyCFunction)Reversed, METH_NOARGS, },
242
+ {NULL}};
243
+
244
+ static PyType_Slot PyUpb_GenericSequence_Slots[] = {
245
+ {Py_tp_dealloc, &PyUpb_GenericSequence_Dealloc},
246
+ {Py_tp_methods, &PyUpb_GenericSequence_Methods},
247
+ {Py_sq_length, PyUpb_GenericSequence_Length},
248
+ {Py_sq_item, PyUpb_GenericSequence_GetItem},
249
+ {Py_tp_richcompare, &PyUpb_GenericSequence_RichCompare},
250
+ // These were implemented for Python/C++ but so far have not been required.
251
+ // {Py_tp_repr, &PyUpb_GenericSequence_Repr},
252
+ // {Py_sq_contains, PyUpb_GenericSequence_Contains},
253
+ // {Py_mp_subscript, PyUpb_GenericSequence_Subscript},
254
+ // {Py_mp_ass_subscript, PyUpb_GenericSequence_AssignSubscript},
255
+ {0, NULL},
256
+ };
257
+
258
+ static PyType_Spec PyUpb_GenericSequence_Spec = {
259
+ PYUPB_MODULE_NAME "._GenericSequence", // tp_name
260
+ sizeof(PyUpb_GenericSequence), // tp_basicsize
261
+ 0, // tp_itemsize
262
+ Py_TPFLAGS_DEFAULT, // tp_flags
263
+ PyUpb_GenericSequence_Slots,
264
+ };
265
+
266
+ // -----------------------------------------------------------------------------
267
+ // ByNameMap
268
+ // -----------------------------------------------------------------------------
269
+
270
+ typedef struct {
271
+ PyObject_HEAD;
272
+ const PyUpb_ByNameMap_Funcs* funcs;
273
+ const void* parent; // upb_MessageDef*, upb_DefPool*, etc.
274
+ PyObject* parent_obj; // Python object that keeps parent alive, we own a ref.
275
+ } PyUpb_ByNameMap;
276
+
277
+ PyUpb_ByNameMap* PyUpb_ByNameMap_Self(PyObject* obj) {
278
+ assert(Py_TYPE(obj) == PyUpb_ModuleState_Get()->by_name_map_type);
279
+ return (PyUpb_ByNameMap*)obj;
280
+ }
281
+
282
+ static void PyUpb_ByNameMap_Dealloc(PyObject* _self) {
283
+ PyUpb_ByNameMap* self = PyUpb_ByNameMap_Self(_self);
284
+ Py_DECREF(self->parent_obj);
285
+ PyUpb_Dealloc(self);
286
+ }
287
+
288
+ PyObject* PyUpb_ByNameMap_New(const PyUpb_ByNameMap_Funcs* funcs,
289
+ const void* parent, PyObject* parent_obj) {
290
+ PyUpb_ModuleState* s = PyUpb_ModuleState_Get();
291
+ PyUpb_ByNameMap* map = (void*)PyType_GenericAlloc(s->by_name_map_type, 0);
292
+ map->funcs = funcs;
293
+ map->parent = parent;
294
+ map->parent_obj = parent_obj;
295
+ Py_INCREF(parent_obj);
296
+ return &map->ob_base;
297
+ }
298
+
299
+ static Py_ssize_t PyUpb_ByNameMap_Length(PyObject* _self) {
300
+ PyUpb_ByNameMap* self = PyUpb_ByNameMap_Self(_self);
301
+ return self->funcs->base.get_elem_count(self->parent);
302
+ }
303
+
304
+ static PyObject* PyUpb_ByNameMap_Subscript(PyObject* _self, PyObject* key) {
305
+ PyUpb_ByNameMap* self = PyUpb_ByNameMap_Self(_self);
306
+ const char* name = PyUpb_GetStrData(key);
307
+ const void* elem = name ? self->funcs->lookup(self->parent, name) : NULL;
308
+
309
+ if (elem) {
310
+ return self->funcs->base.get_elem_wrapper(elem);
311
+ } else {
312
+ PyErr_SetObject(PyExc_KeyError, key);
313
+ return NULL;
314
+ }
315
+ }
316
+
317
+ static int PyUpb_ByNameMap_AssignSubscript(PyObject* self, PyObject* key,
318
+ PyObject* value) {
319
+ PyErr_Format(PyExc_TypeError, PYUPB_MODULE_NAME
320
+ ".ByNameMap' object does not support item assignment");
321
+ return -1;
322
+ }
323
+
324
+ static int PyUpb_ByNameMap_Contains(PyObject* _self, PyObject* key) {
325
+ PyUpb_ByNameMap* self = PyUpb_ByNameMap_Self(_self);
326
+ const char* name = PyUpb_GetStrData(key);
327
+ const void* elem = name ? self->funcs->lookup(self->parent, name) : NULL;
328
+ return elem ? 1 : 0;
329
+ }
330
+
331
+ static PyObject* PyUpb_ByNameMap_Get(PyObject* _self, PyObject* args) {
332
+ PyUpb_ByNameMap* self = PyUpb_ByNameMap_Self(_self);
333
+ PyObject* key;
334
+ PyObject* default_value = Py_None;
335
+ if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &default_value)) {
336
+ return NULL;
337
+ }
338
+
339
+ const char* name = PyUpb_GetStrData(key);
340
+ const void* elem = name ? self->funcs->lookup(self->parent, name) : NULL;
341
+
342
+ if (elem) {
343
+ return self->funcs->base.get_elem_wrapper(elem);
344
+ } else {
345
+ Py_INCREF(default_value);
346
+ return default_value;
347
+ }
348
+ }
349
+
350
+ static PyObject* PyUpb_ByNameMap_GetIter(PyObject* _self) {
351
+ PyUpb_ByNameMap* self = PyUpb_ByNameMap_Self(_self);
352
+ return PyUpb_DescriptorIterator_New(&self->funcs->base, self->parent,
353
+ self->parent_obj);
354
+ }
355
+
356
+ static PyObject* PyUpb_ByNameMap_Keys(PyObject* _self, PyObject* args) {
357
+ PyUpb_ByNameMap* self = PyUpb_ByNameMap_Self(_self);
358
+ int n = self->funcs->base.get_elem_count(self->parent);
359
+ PyObject* ret = PyList_New(n);
360
+ if (!ret) return NULL;
361
+ for (int i = 0; i < n; i++) {
362
+ const void* elem = self->funcs->base.index(self->parent, i);
363
+ PyObject* key = PyUnicode_FromString(self->funcs->get_elem_name(elem));
364
+ if (!key) goto error;
365
+ PyList_SetItem(ret, i, key);
366
+ }
367
+ return ret;
368
+
369
+ error:
370
+ Py_XDECREF(ret);
371
+ return NULL;
372
+ }
373
+
374
+ static PyObject* PyUpb_ByNameMap_Values(PyObject* _self, PyObject* args) {
375
+ PyUpb_ByNameMap* self = PyUpb_ByNameMap_Self(_self);
376
+ int n = self->funcs->base.get_elem_count(self->parent);
377
+ PyObject* ret = PyList_New(n);
378
+ if (!ret) return NULL;
379
+ for (int i = 0; i < n; i++) {
380
+ const void* elem = self->funcs->base.index(self->parent, i);
381
+ PyObject* py_elem = self->funcs->base.get_elem_wrapper(elem);
382
+ if (!py_elem) goto error;
383
+ PyList_SetItem(ret, i, py_elem);
384
+ }
385
+ return ret;
386
+
387
+ error:
388
+ Py_XDECREF(ret);
389
+ return NULL;
390
+ }
391
+
392
+ static PyObject* PyUpb_ByNameMap_Items(PyObject* _self, PyObject* args) {
393
+ PyUpb_ByNameMap* self = (PyUpb_ByNameMap*)_self;
394
+ int n = self->funcs->base.get_elem_count(self->parent);
395
+ PyObject* ret = PyList_New(n);
396
+ PyObject* item;
397
+ PyObject* py_elem;
398
+ if (!ret) return NULL;
399
+ for (int i = 0; i < n; i++) {
400
+ const void* elem = self->funcs->base.index(self->parent, i);
401
+ item = PyTuple_New(2);
402
+ py_elem = self->funcs->base.get_elem_wrapper(elem);
403
+ if (!item || !py_elem) goto error;
404
+ PyTuple_SetItem(item, 0,
405
+ PyUnicode_FromString(self->funcs->get_elem_name(elem)));
406
+ PyTuple_SetItem(item, 1, py_elem);
407
+ PyList_SetItem(ret, i, item);
408
+ }
409
+ return ret;
410
+
411
+ error:
412
+ Py_XDECREF(py_elem);
413
+ Py_XDECREF(item);
414
+ Py_XDECREF(ret);
415
+ return NULL;
416
+ }
417
+
418
+ // A mapping container can only be equal to another mapping container, or (for
419
+ // backward compatibility) to a dict containing the same items.
420
+ // Returns 1 if equal, 0 if unequal, -1 on error.
421
+ static int PyUpb_ByNameMap_IsEqual(PyUpb_ByNameMap* self, PyObject* other) {
422
+ // Check the identity of C++ pointers.
423
+ if (PyObject_TypeCheck(other, Py_TYPE(self))) {
424
+ PyUpb_ByNameMap* other_map = (void*)other;
425
+ return self->parent == other_map->parent && self->funcs == other_map->funcs;
426
+ }
427
+
428
+ if (!PyDict_Check(other)) return 0;
429
+
430
+ PyObject* self_dict = PyDict_New();
431
+ PyDict_Merge(self_dict, (PyObject*)self, 0);
432
+ int eq = PyObject_RichCompareBool(self_dict, other, Py_EQ);
433
+ Py_DECREF(self_dict);
434
+ return eq;
435
+ }
436
+
437
+ static PyObject* PyUpb_ByNameMap_RichCompare(PyObject* _self, PyObject* other,
438
+ int opid) {
439
+ PyUpb_ByNameMap* self = PyUpb_ByNameMap_Self(_self);
440
+ if (opid != Py_EQ && opid != Py_NE) {
441
+ Py_RETURN_NOTIMPLEMENTED;
442
+ }
443
+ bool ret = PyUpb_ByNameMap_IsEqual(self, other);
444
+ if (opid == Py_NE) ret = !ret;
445
+ return PyBool_FromLong(ret);
446
+ }
447
+
448
+ static PyMethodDef PyUpb_ByNameMap_Methods[] = {
449
+ {"get", (PyCFunction)&PyUpb_ByNameMap_Get, METH_VARARGS},
450
+ {"keys", PyUpb_ByNameMap_Keys, METH_NOARGS},
451
+ {"values", PyUpb_ByNameMap_Values, METH_NOARGS},
452
+ {"items", PyUpb_ByNameMap_Items, METH_NOARGS},
453
+ {NULL}};
454
+
455
+ static PyType_Slot PyUpb_ByNameMap_Slots[] = {
456
+ {Py_mp_ass_subscript, PyUpb_ByNameMap_AssignSubscript},
457
+ {Py_mp_length, PyUpb_ByNameMap_Length},
458
+ {Py_mp_subscript, PyUpb_ByNameMap_Subscript},
459
+ {Py_sq_contains, &PyUpb_ByNameMap_Contains},
460
+ {Py_tp_dealloc, &PyUpb_ByNameMap_Dealloc},
461
+ {Py_tp_iter, PyUpb_ByNameMap_GetIter},
462
+ {Py_tp_methods, &PyUpb_ByNameMap_Methods},
463
+ {Py_tp_richcompare, &PyUpb_ByNameMap_RichCompare},
464
+ {0, NULL},
465
+ };
466
+
467
+ static PyType_Spec PyUpb_ByNameMap_Spec = {
468
+ PYUPB_MODULE_NAME "._ByNameMap", // tp_name
469
+ sizeof(PyUpb_ByNameMap), // tp_basicsize
470
+ 0, // tp_itemsize
471
+ Py_TPFLAGS_DEFAULT, // tp_flags
472
+ PyUpb_ByNameMap_Slots,
473
+ };
474
+
475
+ // -----------------------------------------------------------------------------
476
+ // ByNumberMap
477
+ // -----------------------------------------------------------------------------
478
+
479
+ typedef struct {
480
+ PyObject_HEAD;
481
+ const PyUpb_ByNumberMap_Funcs* funcs;
482
+ const void* parent; // upb_MessageDef*, upb_DefPool*, etc.
483
+ PyObject* parent_obj; // Python object that keeps parent alive, we own a ref.
484
+ } PyUpb_ByNumberMap;
485
+
486
+ PyUpb_ByNumberMap* PyUpb_ByNumberMap_Self(PyObject* obj) {
487
+ assert(Py_TYPE(obj) == PyUpb_ModuleState_Get()->by_number_map_type);
488
+ return (PyUpb_ByNumberMap*)obj;
489
+ }
490
+
491
+ PyObject* PyUpb_ByNumberMap_New(const PyUpb_ByNumberMap_Funcs* funcs,
492
+ const void* parent, PyObject* parent_obj) {
493
+ PyUpb_ModuleState* s = PyUpb_ModuleState_Get();
494
+ PyUpb_ByNumberMap* map = (void*)PyType_GenericAlloc(s->by_number_map_type, 0);
495
+ map->funcs = funcs;
496
+ map->parent = parent;
497
+ map->parent_obj = parent_obj;
498
+ Py_INCREF(parent_obj);
499
+ return &map->ob_base;
500
+ }
501
+
502
+ static void PyUpb_ByNumberMap_Dealloc(PyObject* _self) {
503
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
504
+ Py_DECREF(self->parent_obj);
505
+ PyUpb_Dealloc(self);
506
+ }
507
+
508
+ static Py_ssize_t PyUpb_ByNumberMap_Length(PyObject* _self) {
509
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
510
+ return self->funcs->base.get_elem_count(self->parent);
511
+ }
512
+
513
+ static const void* PyUpb_ByNumberMap_LookupHelper(PyUpb_ByNumberMap* self,
514
+ PyObject* key) {
515
+ long num = PyLong_AsLong(key);
516
+ if (num == -1 && PyErr_Occurred()) {
517
+ PyErr_Clear();
518
+ return NULL;
519
+ } else {
520
+ return self->funcs->lookup(self->parent, num);
521
+ }
522
+ }
523
+
524
+ static PyObject* PyUpb_ByNumberMap_Subscript(PyObject* _self, PyObject* key) {
525
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
526
+ const void* elem = PyUpb_ByNumberMap_LookupHelper(self, key);
527
+ if (elem) {
528
+ return self->funcs->base.get_elem_wrapper(elem);
529
+ } else {
530
+ PyErr_SetObject(PyExc_KeyError, key);
531
+ return NULL;
532
+ }
533
+ }
534
+
535
+ static int PyUpb_ByNumberMap_AssignSubscript(PyObject* self, PyObject* key,
536
+ PyObject* value) {
537
+ PyErr_Format(PyExc_TypeError, PYUPB_MODULE_NAME
538
+ ".ByNumberMap' object does not support item assignment");
539
+ return -1;
540
+ }
541
+
542
+ static PyObject* PyUpb_ByNumberMap_Get(PyObject* _self, PyObject* args) {
543
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
544
+ PyObject* key;
545
+ PyObject* default_value = Py_None;
546
+ if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &default_value)) {
547
+ return NULL;
548
+ }
549
+
550
+ const void* elem = PyUpb_ByNumberMap_LookupHelper(self, key);
551
+ if (elem) {
552
+ return self->funcs->base.get_elem_wrapper(elem);
553
+ } else {
554
+ return PyUpb_NewRef(default_value);
555
+ }
556
+ }
557
+
558
+ static PyObject* PyUpb_ByNumberMap_GetIter(PyObject* _self) {
559
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
560
+ return PyUpb_DescriptorIterator_New(&self->funcs->base, self->parent,
561
+ self->parent_obj);
562
+ }
563
+
564
+ static PyObject* PyUpb_ByNumberMap_Keys(PyObject* _self, PyObject* args) {
565
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
566
+ int n = self->funcs->base.get_elem_count(self->parent);
567
+ PyObject* ret = PyList_New(n);
568
+ if (!ret) return NULL;
569
+ for (int i = 0; i < n; i++) {
570
+ const void* elem = self->funcs->base.index(self->parent, i);
571
+ PyObject* key = PyLong_FromLong(self->funcs->get_elem_num(elem));
572
+ if (!key) goto error;
573
+ PyList_SetItem(ret, i, key);
574
+ }
575
+ return ret;
576
+
577
+ error:
578
+ Py_XDECREF(ret);
579
+ return NULL;
580
+ }
581
+
582
+ static PyObject* PyUpb_ByNumberMap_Values(PyObject* _self, PyObject* args) {
583
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
584
+ int n = self->funcs->base.get_elem_count(self->parent);
585
+ PyObject* ret = PyList_New(n);
586
+ if (!ret) return NULL;
587
+ for (int i = 0; i < n; i++) {
588
+ const void* elem = self->funcs->base.index(self->parent, i);
589
+ PyObject* py_elem = self->funcs->base.get_elem_wrapper(elem);
590
+ if (!py_elem) goto error;
591
+ PyList_SetItem(ret, i, py_elem);
592
+ }
593
+ return ret;
594
+
595
+ error:
596
+ Py_XDECREF(ret);
597
+ return NULL;
598
+ }
599
+
600
+ static PyObject* PyUpb_ByNumberMap_Items(PyObject* _self, PyObject* args) {
601
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
602
+ int n = self->funcs->base.get_elem_count(self->parent);
603
+ PyObject* ret = PyList_New(n);
604
+ PyObject* item;
605
+ PyObject* py_elem;
606
+ if (!ret) return NULL;
607
+ for (int i = 0; i < n; i++) {
608
+ const void* elem = self->funcs->base.index(self->parent, i);
609
+ int number = self->funcs->get_elem_num(elem);
610
+ item = PyTuple_New(2);
611
+ py_elem = self->funcs->base.get_elem_wrapper(elem);
612
+ if (!item || !py_elem) goto error;
613
+ PyTuple_SetItem(item, 0, PyLong_FromLong(number));
614
+ PyTuple_SetItem(item, 1, py_elem);
615
+ PyList_SetItem(ret, i, item);
616
+ }
617
+ return ret;
618
+
619
+ error:
620
+ Py_XDECREF(py_elem);
621
+ Py_XDECREF(item);
622
+ Py_XDECREF(ret);
623
+ return NULL;
624
+ }
625
+
626
+ static int PyUpb_ByNumberMap_Contains(PyObject* _self, PyObject* key) {
627
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
628
+ const void* elem = PyUpb_ByNumberMap_LookupHelper(self, key);
629
+ return elem ? 1 : 0;
630
+ }
631
+
632
+ // A mapping container can only be equal to another mapping container, or (for
633
+ // backward compatibility) to a dict containing the same items.
634
+ // Returns 1 if equal, 0 if unequal, -1 on error.
635
+ static int PyUpb_ByNumberMap_IsEqual(PyUpb_ByNumberMap* self, PyObject* other) {
636
+ // Check the identity of C++ pointers.
637
+ if (PyObject_TypeCheck(other, Py_TYPE(self))) {
638
+ PyUpb_ByNumberMap* other_map = (void*)other;
639
+ return self->parent == other_map->parent && self->funcs == other_map->funcs;
640
+ }
641
+
642
+ if (!PyDict_Check(other)) return 0;
643
+
644
+ PyObject* self_dict = PyDict_New();
645
+ PyDict_Merge(self_dict, (PyObject*)self, 0);
646
+ int eq = PyObject_RichCompareBool(self_dict, other, Py_EQ);
647
+ Py_DECREF(self_dict);
648
+ return eq;
649
+ }
650
+
651
+ static PyObject* PyUpb_ByNumberMap_RichCompare(PyObject* _self, PyObject* other,
652
+ int opid) {
653
+ PyUpb_ByNumberMap* self = PyUpb_ByNumberMap_Self(_self);
654
+ if (opid != Py_EQ && opid != Py_NE) {
655
+ Py_RETURN_NOTIMPLEMENTED;
656
+ }
657
+ bool ret = PyUpb_ByNumberMap_IsEqual(self, other);
658
+ if (opid == Py_NE) ret = !ret;
659
+ return PyBool_FromLong(ret);
660
+ }
661
+
662
+ static PyMethodDef PyUpb_ByNumberMap_Methods[] = {
663
+ {"get", (PyCFunction)&PyUpb_ByNumberMap_Get, METH_VARARGS},
664
+ {"keys", PyUpb_ByNumberMap_Keys, METH_NOARGS},
665
+ {"values", PyUpb_ByNumberMap_Values, METH_NOARGS},
666
+ {"items", PyUpb_ByNumberMap_Items, METH_NOARGS},
667
+ {NULL}};
668
+
669
+ static PyType_Slot PyUpb_ByNumberMap_Slots[] = {
670
+ {Py_mp_ass_subscript, PyUpb_ByNumberMap_AssignSubscript},
671
+ {Py_mp_length, PyUpb_ByNumberMap_Length},
672
+ {Py_mp_subscript, PyUpb_ByNumberMap_Subscript},
673
+ {Py_sq_contains, &PyUpb_ByNumberMap_Contains},
674
+ {Py_tp_dealloc, &PyUpb_ByNumberMap_Dealloc},
675
+ {Py_tp_iter, PyUpb_ByNumberMap_GetIter},
676
+ {Py_tp_methods, &PyUpb_ByNumberMap_Methods},
677
+ {Py_tp_richcompare, &PyUpb_ByNumberMap_RichCompare},
678
+ {0, NULL},
679
+ };
680
+
681
+ static PyType_Spec PyUpb_ByNumberMap_Spec = {
682
+ PYUPB_MODULE_NAME "._ByNumberMap", // tp_name
683
+ sizeof(PyUpb_ByNumberMap), // tp_basicsize
684
+ 0, // tp_itemsize
685
+ Py_TPFLAGS_DEFAULT, // tp_flags
686
+ PyUpb_ByNumberMap_Slots,
687
+ };
688
+
689
+ // -----------------------------------------------------------------------------
690
+ // Top Level
691
+ // -----------------------------------------------------------------------------
692
+
693
+ bool PyUpb_InitDescriptorContainers(PyObject* m) {
694
+ PyUpb_ModuleState* s = PyUpb_ModuleState_GetFromModule(m);
695
+
696
+ s->by_name_map_type = PyUpb_AddClass(m, &PyUpb_ByNameMap_Spec);
697
+ s->by_number_map_type = PyUpb_AddClass(m, &PyUpb_ByNumberMap_Spec);
698
+ s->descriptor_iterator_type =
699
+ PyUpb_AddClass(m, &PyUpb_DescriptorIterator_Spec);
700
+ s->generic_sequence_type = PyUpb_AddClass(m, &PyUpb_GenericSequence_Spec);
701
+
702
+ return s->by_name_map_type && s->by_number_map_type &&
703
+ s->descriptor_iterator_type && s->generic_sequence_type;
704
+ }