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,650 @@
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/descriptor_pool.h"
29
+
30
+ #include "google/protobuf/descriptor.upbdefs.h"
31
+ #include "python/convert.h"
32
+ #include "python/descriptor.h"
33
+ #include "python/message.h"
34
+ #include "python/protobuf.h"
35
+ #include "upb/def.h"
36
+ #include "upb/util/def_to_proto.h"
37
+
38
+ // -----------------------------------------------------------------------------
39
+ // DescriptorPool
40
+ // -----------------------------------------------------------------------------
41
+
42
+ typedef struct {
43
+ PyObject_HEAD;
44
+ upb_DefPool* symtab;
45
+ PyObject* db; // The DescriptorDatabase underlying this pool. May be NULL.
46
+ } PyUpb_DescriptorPool;
47
+
48
+ PyObject* PyUpb_DescriptorPool_GetDefaultPool() {
49
+ PyUpb_ModuleState* s = PyUpb_ModuleState_Get();
50
+ return s->default_pool;
51
+ }
52
+
53
+ const upb_MessageDef* PyUpb_DescriptorPool_GetFileProtoDef(void) {
54
+ PyUpb_ModuleState* s = PyUpb_ModuleState_Get();
55
+ if (!s->c_descriptor_symtab) {
56
+ s->c_descriptor_symtab = upb_DefPool_New();
57
+ }
58
+ return google_protobuf_FileDescriptorProto_getmsgdef(s->c_descriptor_symtab);
59
+ }
60
+
61
+ static PyObject* PyUpb_DescriptorPool_DoCreateWithCache(
62
+ PyTypeObject* type, PyObject* db, PyUpb_WeakMap* obj_cache) {
63
+ PyUpb_DescriptorPool* pool = (void*)PyType_GenericAlloc(type, 0);
64
+ pool->symtab = upb_DefPool_New();
65
+ pool->db = db;
66
+ Py_XINCREF(pool->db);
67
+ PyUpb_WeakMap_Add(obj_cache, pool->symtab, &pool->ob_base);
68
+ return &pool->ob_base;
69
+ }
70
+
71
+ static PyObject* PyUpb_DescriptorPool_DoCreate(PyTypeObject* type,
72
+ PyObject* db) {
73
+ return PyUpb_DescriptorPool_DoCreateWithCache(type, db,
74
+ PyUpb_ObjCache_Instance());
75
+ }
76
+
77
+ upb_DefPool* PyUpb_DescriptorPool_GetSymtab(PyObject* pool) {
78
+ return ((PyUpb_DescriptorPool*)pool)->symtab;
79
+ }
80
+
81
+ static int PyUpb_DescriptorPool_Traverse(PyUpb_DescriptorPool* self,
82
+ visitproc visit, void* arg) {
83
+ Py_VISIT(self->db);
84
+ return 0;
85
+ }
86
+
87
+ static int PyUpb_DescriptorPool_Clear(PyUpb_DescriptorPool* self) {
88
+ Py_CLEAR(self->db);
89
+ return 0;
90
+ }
91
+
92
+ PyObject* PyUpb_DescriptorPool_Get(const upb_DefPool* symtab) {
93
+ PyObject* pool = PyUpb_ObjCache_Get(symtab);
94
+ assert(pool);
95
+ return pool;
96
+ }
97
+
98
+ static void PyUpb_DescriptorPool_Dealloc(PyUpb_DescriptorPool* self) {
99
+ PyUpb_DescriptorPool_Clear(self);
100
+ upb_DefPool_Free(self->symtab);
101
+ PyUpb_ObjCache_Delete(self->symtab);
102
+ PyUpb_Dealloc(self);
103
+ }
104
+
105
+ /*
106
+ * DescriptorPool.__new__()
107
+ *
108
+ * Implements:
109
+ * DescriptorPool(descriptor_db=None)
110
+ */
111
+ static PyObject* PyUpb_DescriptorPool_New(PyTypeObject* type, PyObject* args,
112
+ PyObject* kwargs) {
113
+ char* kwlist[] = {"descriptor_db", 0};
114
+ PyObject* db = NULL;
115
+
116
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &db)) {
117
+ return NULL;
118
+ }
119
+
120
+ if (db == Py_None) db = NULL;
121
+ return PyUpb_DescriptorPool_DoCreate(type, db);
122
+ }
123
+
124
+ static PyObject* PyUpb_DescriptorPool_DoAdd(PyObject* _self,
125
+ PyObject* file_desc);
126
+
127
+ static bool PyUpb_DescriptorPool_TryLoadFileProto(PyUpb_DescriptorPool* self,
128
+ PyObject* proto) {
129
+ if (proto == NULL) {
130
+ if (PyErr_ExceptionMatches(PyExc_KeyError)) {
131
+ // Expected error: item was simply not found.
132
+ PyErr_Clear();
133
+ return true; // We didn't accomplish our goal, but we didn't error out.
134
+ }
135
+ return false;
136
+ }
137
+ if (proto == Py_None) return true;
138
+ PyObject* ret = PyUpb_DescriptorPool_DoAdd((PyObject*)self, proto);
139
+ bool ok = ret != NULL;
140
+ Py_XDECREF(ret);
141
+ return ok;
142
+ }
143
+
144
+ static bool PyUpb_DescriptorPool_TryLoadSymbol(PyUpb_DescriptorPool* self,
145
+ PyObject* sym) {
146
+ if (!self->db) return false;
147
+ PyObject* file_proto =
148
+ PyObject_CallMethod(self->db, "FindFileContainingSymbol", "O", sym);
149
+ bool ret = PyUpb_DescriptorPool_TryLoadFileProto(self, file_proto);
150
+ Py_XDECREF(file_proto);
151
+ return ret;
152
+ }
153
+
154
+ static bool PyUpb_DescriptorPool_TryLoadFilename(PyUpb_DescriptorPool* self,
155
+ PyObject* filename) {
156
+ if (!self->db) return false;
157
+ PyObject* file_proto =
158
+ PyObject_CallMethod(self->db, "FindFileByName", "O", filename);
159
+ bool ret = PyUpb_DescriptorPool_TryLoadFileProto(self, file_proto);
160
+ Py_XDECREF(file_proto);
161
+ return ret;
162
+ }
163
+
164
+ bool PyUpb_DescriptorPool_CheckNoDatabase(PyObject* _self) { return true; }
165
+
166
+ static bool PyUpb_DescriptorPool_LoadDependentFiles(
167
+ PyUpb_DescriptorPool* self, google_protobuf_FileDescriptorProto* proto) {
168
+ size_t n;
169
+ const upb_StringView* deps =
170
+ google_protobuf_FileDescriptorProto_dependency(proto, &n);
171
+ for (size_t i = 0; i < n; i++) {
172
+ const upb_FileDef* dep = upb_DefPool_FindFileByNameWithSize(
173
+ self->symtab, deps[i].data, deps[i].size);
174
+ if (!dep) {
175
+ PyObject* filename =
176
+ PyUnicode_FromStringAndSize(deps[i].data, deps[i].size);
177
+ if (!filename) return false;
178
+ bool ok = PyUpb_DescriptorPool_TryLoadFilename(self, filename);
179
+ Py_DECREF(filename);
180
+ if (!ok) return false;
181
+ }
182
+ }
183
+ return true;
184
+ }
185
+
186
+ static PyObject* PyUpb_DescriptorPool_DoAddSerializedFile(
187
+ PyObject* _self, PyObject* serialized_pb) {
188
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
189
+ upb_Arena* arena = upb_Arena_New();
190
+ if (!arena) PYUPB_RETURN_OOM;
191
+ PyObject* result = NULL;
192
+
193
+ char* buf;
194
+ Py_ssize_t size;
195
+ if (PyBytes_AsStringAndSize(serialized_pb, &buf, &size) < 0) {
196
+ goto done;
197
+ }
198
+
199
+ google_protobuf_FileDescriptorProto* proto =
200
+ google_protobuf_FileDescriptorProto_parse(buf, size, arena);
201
+ if (!proto) {
202
+ PyErr_SetString(PyExc_TypeError, "Couldn't parse file content!");
203
+ goto done;
204
+ }
205
+
206
+ upb_StringView name = google_protobuf_FileDescriptorProto_name(proto);
207
+ const upb_FileDef* file =
208
+ upb_DefPool_FindFileByNameWithSize(self->symtab, name.data, name.size);
209
+
210
+ if (file) {
211
+ // If the existing file is equal to the new file, then silently ignore the
212
+ // duplicate add.
213
+ google_protobuf_FileDescriptorProto* existing =
214
+ upb_FileDef_ToProto(file, arena);
215
+ if (!existing) {
216
+ PyErr_SetNone(PyExc_MemoryError);
217
+ goto done;
218
+ }
219
+ const upb_MessageDef* m = PyUpb_DescriptorPool_GetFileProtoDef();
220
+ if (PyUpb_Message_IsEqual(proto, existing, m)) {
221
+ Py_INCREF(Py_None);
222
+ result = Py_None;
223
+ goto done;
224
+ }
225
+ }
226
+
227
+ if (self->db) {
228
+ if (!PyUpb_DescriptorPool_LoadDependentFiles(self, proto)) goto done;
229
+ }
230
+
231
+ upb_Status status;
232
+ upb_Status_Clear(&status);
233
+
234
+ const upb_FileDef* filedef =
235
+ upb_DefPool_AddFile(self->symtab, proto, &status);
236
+ if (!filedef) {
237
+ PyErr_Format(PyExc_TypeError,
238
+ "Couldn't build proto file into descriptor pool: %s",
239
+ upb_Status_ErrorMessage(&status));
240
+ goto done;
241
+ }
242
+
243
+ result = PyUpb_FileDescriptor_Get(filedef);
244
+
245
+ done:
246
+ upb_Arena_Free(arena);
247
+ return result;
248
+ }
249
+
250
+ static PyObject* PyUpb_DescriptorPool_DoAdd(PyObject* _self,
251
+ PyObject* file_desc) {
252
+ if (!PyUpb_CMessage_Verify(file_desc)) return NULL;
253
+ const upb_MessageDef* m = PyUpb_CMessage_GetMsgdef(file_desc);
254
+ const char* file_proto_name =
255
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE ".FileDescriptorProto";
256
+ if (strcmp(upb_MessageDef_FullName(m), file_proto_name) != 0) {
257
+ return PyErr_Format(PyExc_TypeError, "Can only add FileDescriptorProto");
258
+ }
259
+ PyObject* subargs = PyTuple_New(0);
260
+ if (!subargs) return NULL;
261
+ PyObject* serialized =
262
+ PyUpb_CMessage_SerializeToString(file_desc, subargs, NULL);
263
+ Py_DECREF(subargs);
264
+ if (!serialized) return NULL;
265
+ PyObject* ret = PyUpb_DescriptorPool_DoAddSerializedFile(_self, serialized);
266
+ Py_DECREF(serialized);
267
+ return ret;
268
+ }
269
+
270
+ /*
271
+ * PyUpb_DescriptorPool_AddSerializedFile()
272
+ *
273
+ * Implements:
274
+ * DescriptorPool.AddSerializedFile(self, serialized_file_descriptor)
275
+ *
276
+ * Adds the given serialized FileDescriptorProto to the pool.
277
+ */
278
+ static PyObject* PyUpb_DescriptorPool_AddSerializedFile(
279
+ PyObject* _self, PyObject* serialized_pb) {
280
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
281
+ if (self->db) {
282
+ PyErr_SetString(
283
+ PyExc_ValueError,
284
+ "Cannot call AddSerializedFile on a DescriptorPool that uses a "
285
+ "DescriptorDatabase. Add your file to the underlying database.");
286
+ return false;
287
+ }
288
+ return PyUpb_DescriptorPool_DoAddSerializedFile(_self, serialized_pb);
289
+ }
290
+
291
+ static PyObject* PyUpb_DescriptorPool_Add(PyObject* _self,
292
+ PyObject* file_desc) {
293
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
294
+ if (self->db) {
295
+ PyErr_SetString(
296
+ PyExc_ValueError,
297
+ "Cannot call Add on a DescriptorPool that uses a DescriptorDatabase. "
298
+ "Add your file to the underlying database.");
299
+ return false;
300
+ }
301
+ return PyUpb_DescriptorPool_DoAdd(_self, file_desc);
302
+ }
303
+
304
+ /*
305
+ * PyUpb_DescriptorPool_FindFileByName()
306
+ *
307
+ * Implements:
308
+ * DescriptorPool.FindFileByName(self, name)
309
+ */
310
+ static PyObject* PyUpb_DescriptorPool_FindFileByName(PyObject* _self,
311
+ PyObject* arg) {
312
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
313
+
314
+ const char* name = PyUpb_VerifyStrData(arg);
315
+ if (!name) return NULL;
316
+
317
+ const upb_FileDef* file = upb_DefPool_FindFileByName(self->symtab, name);
318
+ if (file == NULL && self->db) {
319
+ if (!PyUpb_DescriptorPool_TryLoadFilename(self, arg)) return NULL;
320
+ file = upb_DefPool_FindFileByName(self->symtab, name);
321
+ }
322
+ if (file == NULL) {
323
+ return PyErr_Format(PyExc_KeyError, "Couldn't find file %.200s", name);
324
+ }
325
+
326
+ return PyUpb_FileDescriptor_Get(file);
327
+ }
328
+
329
+ /*
330
+ * PyUpb_DescriptorPool_FindExtensionByName()
331
+ *
332
+ * Implements:
333
+ * DescriptorPool.FindExtensionByName(self, name)
334
+ */
335
+ static PyObject* PyUpb_DescriptorPool_FindExtensionByName(PyObject* _self,
336
+ PyObject* arg) {
337
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
338
+
339
+ const char* name = PyUpb_VerifyStrData(arg);
340
+ if (!name) return NULL;
341
+
342
+ const upb_FieldDef* field =
343
+ upb_DefPool_FindExtensionByName(self->symtab, name);
344
+ if (field == NULL && self->db) {
345
+ if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL;
346
+ field = upb_DefPool_FindExtensionByName(self->symtab, name);
347
+ }
348
+ if (field == NULL) {
349
+ return PyErr_Format(PyExc_KeyError, "Couldn't find extension %.200s", name);
350
+ }
351
+
352
+ return PyUpb_FieldDescriptor_Get(field);
353
+ }
354
+
355
+ /*
356
+ * PyUpb_DescriptorPool_FindMessageTypeByName()
357
+ *
358
+ * Implements:
359
+ * DescriptorPool.FindMessageTypeByName(self, name)
360
+ */
361
+ static PyObject* PyUpb_DescriptorPool_FindMessageTypeByName(PyObject* _self,
362
+ PyObject* arg) {
363
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
364
+
365
+ const char* name = PyUpb_VerifyStrData(arg);
366
+ if (!name) return NULL;
367
+
368
+ const upb_MessageDef* m = upb_DefPool_FindMessageByName(self->symtab, name);
369
+ if (m == NULL && self->db) {
370
+ if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL;
371
+ m = upb_DefPool_FindMessageByName(self->symtab, name);
372
+ }
373
+ if (m == NULL) {
374
+ return PyErr_Format(PyExc_KeyError, "Couldn't find message %.200s", name);
375
+ }
376
+
377
+ return PyUpb_Descriptor_Get(m);
378
+ }
379
+
380
+ // Splits a dotted symbol like foo.bar.baz on the last dot. Returns the portion
381
+ // after the last dot (baz) and updates `*parent_size` to the length of the
382
+ // parent (foo.bar). Returns NULL if no dots were present.
383
+ static const char* PyUpb_DescriptorPool_SplitSymbolName(const char* sym,
384
+ size_t* parent_size) {
385
+ const char* last_dot = strrchr(sym, '.');
386
+ if (!last_dot) return NULL;
387
+ *parent_size = last_dot - sym;
388
+ return last_dot + 1;
389
+ }
390
+
391
+ /*
392
+ * PyUpb_DescriptorPool_FindFieldByName()
393
+ *
394
+ * Implements:
395
+ * DescriptorPool.FindFieldByName(self, name)
396
+ */
397
+ static PyObject* PyUpb_DescriptorPool_FindFieldByName(PyObject* _self,
398
+ PyObject* arg) {
399
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
400
+
401
+ const char* name = PyUpb_VerifyStrData(arg);
402
+ if (!name) return NULL;
403
+
404
+ size_t parent_size;
405
+ const char* child = PyUpb_DescriptorPool_SplitSymbolName(name, &parent_size);
406
+ const upb_FieldDef* f = NULL;
407
+ if (child) {
408
+ const upb_MessageDef* parent =
409
+ upb_DefPool_FindMessageByNameWithSize(self->symtab, name, parent_size);
410
+ if (parent == NULL && self->db) {
411
+ if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL;
412
+ parent = upb_DefPool_FindMessageByNameWithSize(self->symtab, name,
413
+ parent_size);
414
+ }
415
+ if (parent) {
416
+ f = upb_MessageDef_FindFieldByName(parent, child);
417
+ }
418
+ }
419
+
420
+ if (!f) {
421
+ return PyErr_Format(PyExc_KeyError, "Couldn't find message %.200s", name);
422
+ }
423
+
424
+ return PyUpb_FieldDescriptor_Get(f);
425
+ }
426
+
427
+ /*
428
+ * PyUpb_DescriptorPool_FindEnumTypeByName()
429
+ *
430
+ * Implements:
431
+ * DescriptorPool.FindEnumTypeByName(self, name)
432
+ */
433
+ static PyObject* PyUpb_DescriptorPool_FindEnumTypeByName(PyObject* _self,
434
+ PyObject* arg) {
435
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
436
+
437
+ const char* name = PyUpb_VerifyStrData(arg);
438
+ if (!name) return NULL;
439
+
440
+ const upb_EnumDef* e = upb_DefPool_FindEnumByName(self->symtab, name);
441
+ if (e == NULL && self->db) {
442
+ if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL;
443
+ e = upb_DefPool_FindEnumByName(self->symtab, name);
444
+ }
445
+ if (e == NULL) {
446
+ return PyErr_Format(PyExc_KeyError, "Couldn't find enum %.200s", name);
447
+ }
448
+
449
+ return PyUpb_EnumDescriptor_Get(e);
450
+ }
451
+
452
+ /*
453
+ * PyUpb_DescriptorPool_FindOneofByName()
454
+ *
455
+ * Implements:
456
+ * DescriptorPool.FindOneofByName(self, name)
457
+ */
458
+ static PyObject* PyUpb_DescriptorPool_FindOneofByName(PyObject* _self,
459
+ PyObject* arg) {
460
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
461
+
462
+ const char* name = PyUpb_VerifyStrData(arg);
463
+ if (!name) return NULL;
464
+
465
+ size_t parent_size;
466
+ const char* child = PyUpb_DescriptorPool_SplitSymbolName(name, &parent_size);
467
+
468
+ if (child) {
469
+ const upb_MessageDef* parent =
470
+ upb_DefPool_FindMessageByNameWithSize(self->symtab, name, parent_size);
471
+ if (parent == NULL && self->db) {
472
+ if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL;
473
+ parent = upb_DefPool_FindMessageByNameWithSize(self->symtab, name,
474
+ parent_size);
475
+ }
476
+ if (parent) {
477
+ const upb_OneofDef* o = upb_MessageDef_FindOneofByName(parent, child);
478
+ return PyUpb_OneofDescriptor_Get(o);
479
+ }
480
+ }
481
+
482
+ return PyErr_Format(PyExc_KeyError, "Couldn't find oneof %.200s", name);
483
+ }
484
+
485
+ static PyObject* PyUpb_DescriptorPool_FindServiceByName(PyObject* _self,
486
+ PyObject* arg) {
487
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
488
+
489
+ const char* name = PyUpb_VerifyStrData(arg);
490
+ if (!name) return NULL;
491
+
492
+ const upb_ServiceDef* s = upb_DefPool_FindServiceByName(self->symtab, name);
493
+ if (s == NULL && self->db) {
494
+ if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL;
495
+ s = upb_DefPool_FindServiceByName(self->symtab, name);
496
+ }
497
+ if (s == NULL) {
498
+ return PyErr_Format(PyExc_KeyError, "Couldn't find service %.200s", name);
499
+ }
500
+
501
+ return PyUpb_ServiceDescriptor_Get(s);
502
+ }
503
+
504
+ static PyObject* PyUpb_DescriptorPool_FindMethodByName(PyObject* _self,
505
+ PyObject* arg) {
506
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
507
+
508
+ const char* name = PyUpb_VerifyStrData(arg);
509
+ if (!name) return NULL;
510
+ size_t parent_size;
511
+ const char* child = PyUpb_DescriptorPool_SplitSymbolName(name, &parent_size);
512
+
513
+ if (!child) goto err;
514
+ const upb_ServiceDef* parent =
515
+ upb_DefPool_FindServiceByNameWithSize(self->symtab, name, parent_size);
516
+ if (parent == NULL && self->db) {
517
+ if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL;
518
+ parent =
519
+ upb_DefPool_FindServiceByNameWithSize(self->symtab, name, parent_size);
520
+ }
521
+ if (!parent) goto err;
522
+ const upb_MethodDef* m = upb_ServiceDef_FindMethodByName(parent, child);
523
+ if (!m) goto err;
524
+ return PyUpb_MethodDescriptor_Get(m);
525
+
526
+ err:
527
+ return PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name);
528
+ }
529
+
530
+ static PyObject* PyUpb_DescriptorPool_FindFileContainingSymbol(PyObject* _self,
531
+ PyObject* arg) {
532
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
533
+
534
+ const char* name = PyUpb_VerifyStrData(arg);
535
+ if (!name) return NULL;
536
+
537
+ const upb_FileDef* f =
538
+ upb_DefPool_FindFileContainingSymbol(self->symtab, name);
539
+ if (f == NULL && self->db) {
540
+ if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL;
541
+ f = upb_DefPool_FindFileContainingSymbol(self->symtab, name);
542
+ }
543
+ if (f == NULL) {
544
+ return PyErr_Format(PyExc_KeyError, "Couldn't find symbol %.200s", name);
545
+ }
546
+
547
+ return PyUpb_FileDescriptor_Get(f);
548
+ }
549
+
550
+ static PyObject* PyUpb_DescriptorPool_FindExtensionByNumber(PyObject* _self,
551
+ PyObject* args) {
552
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
553
+ PyObject* message_descriptor;
554
+ int number;
555
+ if (!PyArg_ParseTuple(args, "Oi", &message_descriptor, &number)) {
556
+ return NULL;
557
+ }
558
+
559
+ const upb_FieldDef* f = upb_DefPool_FindExtensionByNumber(
560
+ self->symtab, PyUpb_Descriptor_GetDef(message_descriptor), number);
561
+ if (f == NULL) {
562
+ return PyErr_Format(PyExc_KeyError, "Couldn't find Extension %d", number);
563
+ }
564
+
565
+ return PyUpb_FieldDescriptor_Get(f);
566
+ }
567
+
568
+ static PyObject* PyUpb_DescriptorPool_FindAllExtensions(PyObject* _self,
569
+ PyObject* msg_desc) {
570
+ PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self;
571
+ const upb_MessageDef* m = PyUpb_Descriptor_GetDef(msg_desc);
572
+ size_t n;
573
+ const upb_FieldDef** ext = upb_DefPool_GetAllExtensions(self->symtab, m, &n);
574
+ PyObject* ret = PyList_New(n);
575
+ if (!ret) goto done;
576
+ for (size_t i = 0; i < n; i++) {
577
+ PyObject* field = PyUpb_FieldDescriptor_Get(ext[i]);
578
+ if (!field) {
579
+ Py_DECREF(ret);
580
+ ret = NULL;
581
+ goto done;
582
+ }
583
+ PyList_SetItem(ret, i, field);
584
+ }
585
+ done:
586
+ free(ext);
587
+ return ret;
588
+ }
589
+
590
+ static PyMethodDef PyUpb_DescriptorPool_Methods[] = {
591
+ {"Add", PyUpb_DescriptorPool_Add, METH_O,
592
+ "Adds the FileDescriptorProto and its types to this pool."},
593
+ {"AddSerializedFile", PyUpb_DescriptorPool_AddSerializedFile, METH_O,
594
+ "Adds a serialized FileDescriptorProto to this pool."},
595
+ {"FindFileByName", PyUpb_DescriptorPool_FindFileByName, METH_O,
596
+ "Searches for a file descriptor by its .proto name."},
597
+ {"FindMessageTypeByName", PyUpb_DescriptorPool_FindMessageTypeByName,
598
+ METH_O, "Searches for a message descriptor by full name."},
599
+ {"FindFieldByName", PyUpb_DescriptorPool_FindFieldByName, METH_O,
600
+ "Searches for a field descriptor by full name."},
601
+ {"FindExtensionByName", PyUpb_DescriptorPool_FindExtensionByName, METH_O,
602
+ "Searches for extension descriptor by full name."},
603
+ {"FindEnumTypeByName", PyUpb_DescriptorPool_FindEnumTypeByName, METH_O,
604
+ "Searches for enum type descriptor by full name."},
605
+ {"FindOneofByName", PyUpb_DescriptorPool_FindOneofByName, METH_O,
606
+ "Searches for oneof descriptor by full name."},
607
+ {"FindServiceByName", PyUpb_DescriptorPool_FindServiceByName, METH_O,
608
+ "Searches for service descriptor by full name."},
609
+ {"FindMethodByName", PyUpb_DescriptorPool_FindMethodByName, METH_O,
610
+ "Searches for method descriptor by full name."},
611
+ {"FindFileContainingSymbol", PyUpb_DescriptorPool_FindFileContainingSymbol,
612
+ METH_O, "Gets the FileDescriptor containing the specified symbol."},
613
+ {"FindExtensionByNumber", PyUpb_DescriptorPool_FindExtensionByNumber,
614
+ METH_VARARGS, "Gets the extension descriptor for the given number."},
615
+ {"FindAllExtensions", PyUpb_DescriptorPool_FindAllExtensions, METH_O,
616
+ "Gets all known extensions of the given message descriptor."},
617
+ {NULL}};
618
+
619
+ static PyType_Slot PyUpb_DescriptorPool_Slots[] = {
620
+ {Py_tp_clear, PyUpb_DescriptorPool_Clear},
621
+ {Py_tp_dealloc, PyUpb_DescriptorPool_Dealloc},
622
+ {Py_tp_methods, PyUpb_DescriptorPool_Methods},
623
+ {Py_tp_new, PyUpb_DescriptorPool_New},
624
+ {Py_tp_traverse, PyUpb_DescriptorPool_Traverse},
625
+ {0, NULL}};
626
+
627
+ static PyType_Spec PyUpb_DescriptorPool_Spec = {
628
+ PYUPB_MODULE_NAME ".DescriptorPool",
629
+ sizeof(PyUpb_DescriptorPool),
630
+ 0, // tp_itemsize
631
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
632
+ PyUpb_DescriptorPool_Slots,
633
+ };
634
+
635
+ // -----------------------------------------------------------------------------
636
+ // Top Level
637
+ // -----------------------------------------------------------------------------
638
+
639
+ bool PyUpb_InitDescriptorPool(PyObject* m) {
640
+ PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m);
641
+ PyTypeObject* descriptor_pool_type =
642
+ PyUpb_AddClass(m, &PyUpb_DescriptorPool_Spec);
643
+
644
+ if (!descriptor_pool_type) return false;
645
+
646
+ state->default_pool = PyUpb_DescriptorPool_DoCreateWithCache(
647
+ descriptor_pool_type, NULL, state->obj_cache);
648
+ return state->default_pool &&
649
+ PyModule_AddObject(m, "default_pool", state->default_pool) == 0;
650
+ }
@@ -0,0 +1,48 @@
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
+ #ifndef PYUPB_DESCRIPTOR_POOL_H__
29
+ #define PYUPB_DESCRIPTOR_POOL_H__
30
+
31
+ #include <stdbool.h>
32
+
33
+ #include "protobuf.h"
34
+
35
+ // Returns a Python wrapper object for the given symtab. The symtab must have
36
+ // been created from a Python DescriptorPool originally.
37
+ PyObject* PyUpb_DescriptorPool_Get(const upb_DefPool* symtab);
38
+
39
+ // Given a Python DescriptorPool, returns the underlying symtab.
40
+ upb_DefPool* PyUpb_DescriptorPool_GetSymtab(PyObject* pool);
41
+
42
+ // Returns the default DescriptorPool (a global singleton).
43
+ PyObject* PyUpb_DescriptorPool_GetDefaultPool(void);
44
+
45
+ // Module-level init.
46
+ bool PyUpb_InitDescriptorPool(PyObject* m);
47
+
48
+ #endif // PYUPB_DESCRIPTOR_POOL_H__