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,1694 @@
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.h"
29
+
30
+ #include "python/convert.h"
31
+ #include "python/descriptor_containers.h"
32
+ #include "python/descriptor_pool.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
+ // DescriptorBase
40
+ // -----------------------------------------------------------------------------
41
+
42
+ // This representation is used by all concrete descriptors.
43
+
44
+ typedef struct {
45
+ PyObject_HEAD;
46
+ PyObject* pool; // We own a ref.
47
+ const void* def; // Type depends on the class. Kept alive by "pool".
48
+ PyObject* options; // NULL if not present or not cached.
49
+ } PyUpb_DescriptorBase;
50
+
51
+ PyObject* PyUpb_AnyDescriptor_GetPool(PyObject* desc) {
52
+ PyUpb_DescriptorBase* base = (void*)desc;
53
+ return base->pool;
54
+ }
55
+
56
+ const void* PyUpb_AnyDescriptor_GetDef(PyObject* desc) {
57
+ PyUpb_DescriptorBase* base = (void*)desc;
58
+ return base->def;
59
+ }
60
+
61
+ static PyUpb_DescriptorBase* PyUpb_DescriptorBase_DoCreate(
62
+ PyUpb_DescriptorType type, const void* def, const upb_FileDef* file) {
63
+ PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
64
+ PyTypeObject* type_obj = state->descriptor_types[type];
65
+ assert(def);
66
+
67
+ PyUpb_DescriptorBase* base = (void*)PyType_GenericAlloc(type_obj, 0);
68
+ base->pool = PyUpb_DescriptorPool_Get(upb_FileDef_Pool(file));
69
+ base->def = def;
70
+ base->options = NULL;
71
+
72
+ PyUpb_ObjCache_Add(def, &base->ob_base);
73
+ return base;
74
+ }
75
+
76
+ // Returns a Python object wrapping |def|, of descriptor type |type|. If a
77
+ // wrapper was previously created for this def, returns it, otherwise creates a
78
+ // new wrapper.
79
+ static PyObject* PyUpb_DescriptorBase_Get(PyUpb_DescriptorType type,
80
+ const void* def,
81
+ const upb_FileDef* file) {
82
+ PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)PyUpb_ObjCache_Get(def);
83
+
84
+ if (!base) {
85
+ base = PyUpb_DescriptorBase_DoCreate(type, def, file);
86
+ }
87
+
88
+ return &base->ob_base;
89
+ }
90
+
91
+ static PyUpb_DescriptorBase* PyUpb_DescriptorBase_Check(
92
+ PyObject* obj, PyUpb_DescriptorType type) {
93
+ PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
94
+ PyTypeObject* type_obj = state->descriptor_types[type];
95
+ if (!PyObject_TypeCheck(obj, type_obj)) {
96
+ PyErr_Format(PyExc_TypeError, "Expected object of type %S, but got %R",
97
+ type_obj, obj);
98
+ return NULL;
99
+ }
100
+ return (PyUpb_DescriptorBase*)obj;
101
+ }
102
+
103
+ static PyObject* PyUpb_DescriptorBase_GetOptions(PyUpb_DescriptorBase* self,
104
+ const upb_Message* opts,
105
+ const upb_MiniTable* layout,
106
+ const char* msg_name) {
107
+ if (!self->options) {
108
+ // Load descriptors protos if they are not loaded already. We have to do
109
+ // this lazily, otherwise, it would lead to circular imports.
110
+ PyObject* mod = PyImport_ImportModule(PYUPB_DESCRIPTOR_MODULE);
111
+ Py_DECREF(mod);
112
+
113
+ // Find the correct options message.
114
+ PyObject* default_pool = PyUpb_DescriptorPool_GetDefaultPool();
115
+ const upb_DefPool* symtab = PyUpb_DescriptorPool_GetSymtab(default_pool);
116
+ const upb_MessageDef* m = upb_DefPool_FindMessageByName(symtab, msg_name);
117
+ assert(m);
118
+
119
+ // Copy the options message from C to Python using serialize+parse.
120
+ // We don't wrap the C object directly because there is no guarantee that
121
+ // the descriptor_pb2 that was loaded at runtime has the same members or
122
+ // layout as the C types that were compiled in.
123
+ size_t size;
124
+ PyObject* py_arena = PyUpb_Arena_New();
125
+ upb_Arena* arena = PyUpb_Arena_Get(py_arena);
126
+ char* pb = upb_Encode(opts, layout, 0, arena, &size);
127
+ upb_Message* opts2 = upb_Message_New(m, arena);
128
+ assert(opts2);
129
+ bool ok = upb_Decode(pb, size, opts2, upb_MessageDef_MiniTable(m),
130
+ upb_DefPool_ExtensionRegistry(symtab), 0,
131
+ arena) == kUpb_DecodeStatus_Ok;
132
+ (void)ok;
133
+ assert(ok);
134
+
135
+ self->options = PyUpb_CMessage_Get(opts2, m, py_arena);
136
+ Py_DECREF(py_arena);
137
+ }
138
+
139
+ Py_INCREF(self->options);
140
+ return self->options;
141
+ }
142
+
143
+ typedef void* PyUpb_ToProto_Func(const void* def, upb_Arena* arena);
144
+
145
+ static PyObject* PyUpb_DescriptorBase_GetSerializedProto(
146
+ PyObject* _self, PyUpb_ToProto_Func* func, const upb_MiniTable* layout) {
147
+ PyUpb_DescriptorBase* self = (void*)_self;
148
+ upb_Arena* arena = upb_Arena_New();
149
+ if (!arena) PYUPB_RETURN_OOM;
150
+ upb_Message* proto = func(self->def, arena);
151
+ if (!proto) goto oom;
152
+ size_t size;
153
+ char* pb = upb_Encode(proto, layout, 0, arena, &size);
154
+ if (!pb) goto oom;
155
+ PyObject* str = PyBytes_FromStringAndSize(pb, size);
156
+ upb_Arena_Free(arena);
157
+ return str;
158
+
159
+ oom:
160
+ upb_Arena_Free(arena);
161
+ PyErr_SetNone(PyExc_MemoryError);
162
+ return NULL;
163
+ }
164
+
165
+ static PyObject* PyUpb_DescriptorBase_CopyToProto(PyObject* _self,
166
+ PyUpb_ToProto_Func* func,
167
+ const upb_MiniTable* layout,
168
+ const char* expected_type,
169
+ PyObject* py_proto) {
170
+ if (!PyUpb_CMessage_Verify(py_proto)) return NULL;
171
+ const upb_MessageDef* m = PyUpb_CMessage_GetMsgdef(py_proto);
172
+ const char* type = upb_MessageDef_FullName(m);
173
+ if (strcmp(type, expected_type) != 0) {
174
+ PyErr_Format(
175
+ PyExc_TypeError,
176
+ "CopyToProto: message is of incorrect type '%s' (expected '%s'", type,
177
+ expected_type);
178
+ return NULL;
179
+ }
180
+ PyObject* serialized =
181
+ PyUpb_DescriptorBase_GetSerializedProto(_self, func, layout);
182
+ if (!serialized) return NULL;
183
+ PyObject* ret = PyUpb_CMessage_MergeFromString(py_proto, serialized);
184
+ Py_DECREF(serialized);
185
+ return ret;
186
+ }
187
+
188
+ static void PyUpb_DescriptorBase_Dealloc(PyUpb_DescriptorBase* base) {
189
+ PyUpb_ObjCache_Delete(base->def);
190
+ Py_DECREF(base->pool);
191
+ Py_XDECREF(base->options);
192
+ PyUpb_Dealloc(base);
193
+ }
194
+
195
+ #define DESCRIPTOR_BASE_SLOTS \
196
+ {Py_tp_new, (void*)&PyUpb_Forbidden_New}, { \
197
+ Py_tp_dealloc, (void*)&PyUpb_DescriptorBase_Dealloc \
198
+ }
199
+
200
+ // -----------------------------------------------------------------------------
201
+ // Descriptor
202
+ // -----------------------------------------------------------------------------
203
+
204
+ PyObject* PyUpb_Descriptor_Get(const upb_MessageDef* m) {
205
+ assert(m);
206
+ const upb_FileDef* file = upb_MessageDef_File(m);
207
+ return PyUpb_DescriptorBase_Get(kPyUpb_Descriptor, m, file);
208
+ }
209
+
210
+ PyObject* PyUpb_Descriptor_GetClass(const upb_MessageDef* m) {
211
+ PyObject* ret = PyUpb_ObjCache_Get(upb_MessageDef_MiniTable(m));
212
+ assert(ret);
213
+ return ret;
214
+ }
215
+
216
+ // The LookupNested*() functions provide name lookup for entities nested inside
217
+ // a message. This uses the symtab's table, which requires that the symtab is
218
+ // not being mutated concurrently. We can guarantee this for Python-owned
219
+ // symtabs, but upb cannot guarantee it in general for an arbitrary
220
+ // `const upb_MessageDef*`.
221
+
222
+ static const void* PyUpb_Descriptor_LookupNestedMessage(const upb_MessageDef* m,
223
+ const char* name) {
224
+ const upb_FileDef* filedef = upb_MessageDef_File(m);
225
+ const upb_DefPool* symtab = upb_FileDef_Pool(filedef);
226
+ PyObject* qname =
227
+ PyUnicode_FromFormat("%s.%s", upb_MessageDef_FullName(m), name);
228
+ const upb_MessageDef* ret = upb_DefPool_FindMessageByName(
229
+ symtab, PyUnicode_AsUTF8AndSize(qname, NULL));
230
+ Py_DECREF(qname);
231
+ return ret;
232
+ }
233
+
234
+ static const void* PyUpb_Descriptor_LookupNestedEnum(const upb_MessageDef* m,
235
+ const char* name) {
236
+ const upb_FileDef* filedef = upb_MessageDef_File(m);
237
+ const upb_DefPool* symtab = upb_FileDef_Pool(filedef);
238
+ PyObject* qname =
239
+ PyUnicode_FromFormat("%s.%s", upb_MessageDef_FullName(m), name);
240
+ const upb_EnumDef* ret =
241
+ upb_DefPool_FindEnumByName(symtab, PyUnicode_AsUTF8AndSize(qname, NULL));
242
+ Py_DECREF(qname);
243
+ return ret;
244
+ }
245
+
246
+ static const void* PyUpb_Descriptor_LookupNestedExtension(
247
+ const upb_MessageDef* m, const char* name) {
248
+ const upb_FileDef* filedef = upb_MessageDef_File(m);
249
+ const upb_DefPool* symtab = upb_FileDef_Pool(filedef);
250
+ PyObject* qname =
251
+ PyUnicode_FromFormat("%s.%s", upb_MessageDef_FullName(m), name);
252
+ const upb_FieldDef* ret = upb_DefPool_FindExtensionByName(
253
+ symtab, PyUnicode_AsUTF8AndSize(qname, NULL));
254
+ Py_DECREF(qname);
255
+ return ret;
256
+ }
257
+
258
+ static PyObject* PyUpb_Descriptor_GetExtensionRanges(PyObject* _self,
259
+ void* closure) {
260
+ PyUpb_DescriptorBase* self = (PyUpb_DescriptorBase*)_self;
261
+ int n = upb_MessageDef_ExtensionRangeCount(self->def);
262
+ PyObject* range_list = PyList_New(n);
263
+
264
+ for (int i = 0; i < n; i++) {
265
+ const upb_ExtensionRange* range =
266
+ upb_MessageDef_ExtensionRange(self->def, i);
267
+ PyObject* start = PyLong_FromLong(upb_ExtensionRange_Start(range));
268
+ PyObject* end = PyLong_FromLong(upb_ExtensionRange_End(range));
269
+ PyList_SetItem(range_list, i, PyTuple_Pack(2, start, end));
270
+ }
271
+
272
+ return range_list;
273
+ }
274
+
275
+ static PyObject* PyUpb_Descriptor_GetExtensions(PyObject* _self,
276
+ void* closure) {
277
+ PyUpb_DescriptorBase* self = (void*)_self;
278
+ static PyUpb_GenericSequence_Funcs funcs = {
279
+ (void*)&upb_MessageDef_NestedExtensionCount,
280
+ (void*)&upb_MessageDef_NestedExtension,
281
+ (void*)&PyUpb_FieldDescriptor_Get,
282
+ };
283
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
284
+ }
285
+
286
+ static PyObject* PyUpb_Descriptor_GetExtensionsByName(PyObject* _self,
287
+ void* closure) {
288
+ PyUpb_DescriptorBase* self = (void*)_self;
289
+ static PyUpb_ByNameMap_Funcs funcs = {
290
+ {
291
+ (void*)&upb_MessageDef_NestedExtensionCount,
292
+ (void*)&upb_MessageDef_NestedExtension,
293
+ (void*)&PyUpb_FieldDescriptor_Get,
294
+ },
295
+ (void*)&PyUpb_Descriptor_LookupNestedExtension,
296
+ (void*)&upb_FieldDef_Name,
297
+ };
298
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
299
+ }
300
+
301
+ static PyObject* PyUpb_Descriptor_GetEnumTypes(PyObject* _self, void* closure) {
302
+ PyUpb_DescriptorBase* self = (void*)_self;
303
+ static PyUpb_GenericSequence_Funcs funcs = {
304
+ (void*)&upb_MessageDef_NestedEnumCount,
305
+ (void*)&upb_MessageDef_NestedEnum,
306
+ (void*)&PyUpb_EnumDescriptor_Get,
307
+ };
308
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
309
+ }
310
+
311
+ static PyObject* PyUpb_Descriptor_GetOneofs(PyObject* _self, void* closure) {
312
+ PyUpb_DescriptorBase* self = (void*)_self;
313
+ static PyUpb_GenericSequence_Funcs funcs = {
314
+ (void*)&upb_MessageDef_OneofCount,
315
+ (void*)&upb_MessageDef_Oneof,
316
+ (void*)&PyUpb_OneofDescriptor_Get,
317
+ };
318
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
319
+ }
320
+
321
+ static PyObject* PyUpb_Descriptor_GetOptions(PyObject* _self, PyObject* args) {
322
+ PyUpb_DescriptorBase* self = (void*)_self;
323
+ return PyUpb_DescriptorBase_GetOptions(
324
+ self, upb_MessageDef_Options(self->def),
325
+ &google_protobuf_MessageOptions_msginit,
326
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE ".MessageOptions");
327
+ }
328
+
329
+ static PyObject* PyUpb_Descriptor_CopyToProto(PyObject* _self,
330
+ PyObject* py_proto) {
331
+ return PyUpb_DescriptorBase_CopyToProto(
332
+ _self, (PyUpb_ToProto_Func*)&upb_MessageDef_ToProto,
333
+ &google_protobuf_DescriptorProto_msginit,
334
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE ".DescriptorProto", py_proto);
335
+ }
336
+
337
+ static PyObject* PyUpb_Descriptor_EnumValueName(PyObject* _self,
338
+ PyObject* args) {
339
+ PyUpb_DescriptorBase* self = (void*)_self;
340
+ const char* enum_name;
341
+ int number;
342
+ if (!PyArg_ParseTuple(args, "si", &enum_name, &number)) return NULL;
343
+ const upb_EnumDef* e =
344
+ PyUpb_Descriptor_LookupNestedEnum(self->def, enum_name);
345
+ if (!e) {
346
+ PyErr_SetString(PyExc_KeyError, enum_name);
347
+ return NULL;
348
+ }
349
+ const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(e, number);
350
+ if (!ev) {
351
+ PyErr_Format(PyExc_KeyError, "%d", number);
352
+ return NULL;
353
+ }
354
+ return PyUnicode_FromString(upb_EnumValueDef_Name(ev));
355
+ }
356
+
357
+ static PyObject* PyUpb_Descriptor_GetFieldsByName(PyObject* _self,
358
+ void* closure) {
359
+ PyUpb_DescriptorBase* self = (void*)_self;
360
+ static PyUpb_ByNameMap_Funcs funcs = {
361
+ {
362
+ (void*)&upb_MessageDef_FieldCount,
363
+ (void*)&upb_MessageDef_Field,
364
+ (void*)&PyUpb_FieldDescriptor_Get,
365
+ },
366
+ (void*)&upb_MessageDef_FindFieldByName,
367
+ (void*)&upb_FieldDef_Name,
368
+ };
369
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
370
+ }
371
+
372
+ static PyObject* PyUpb_Descriptor_GetFieldsByCamelCaseName(PyObject* _self,
373
+ void* closure) {
374
+ PyUpb_DescriptorBase* self = (void*)_self;
375
+ static PyUpb_ByNameMap_Funcs funcs = {
376
+ {
377
+ (void*)&upb_MessageDef_FieldCount,
378
+ (void*)&upb_MessageDef_Field,
379
+ (void*)&PyUpb_FieldDescriptor_Get,
380
+ },
381
+ (void*)&upb_MessageDef_FindByJsonName,
382
+ (void*)&upb_FieldDef_JsonName,
383
+ };
384
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
385
+ }
386
+
387
+ static PyObject* PyUpb_Descriptor_GetFieldsByNumber(PyObject* _self,
388
+ void* closure) {
389
+ static PyUpb_ByNumberMap_Funcs funcs = {
390
+ {
391
+ (void*)&upb_MessageDef_FieldCount,
392
+ (void*)&upb_MessageDef_Field,
393
+ (void*)&PyUpb_FieldDescriptor_Get,
394
+ },
395
+ (void*)&upb_MessageDef_FindFieldByNumber,
396
+ (void*)&upb_FieldDef_Number,
397
+ };
398
+ PyUpb_DescriptorBase* self = (void*)_self;
399
+ return PyUpb_ByNumberMap_New(&funcs, self->def, self->pool);
400
+ }
401
+
402
+ static PyObject* PyUpb_Descriptor_GetNestedTypes(PyObject* _self,
403
+ void* closure) {
404
+ PyUpb_DescriptorBase* self = (void*)_self;
405
+ static PyUpb_GenericSequence_Funcs funcs = {
406
+ (void*)&upb_MessageDef_NestedMessageCount,
407
+ (void*)&upb_MessageDef_NestedMessage,
408
+ (void*)&PyUpb_Descriptor_Get,
409
+ };
410
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
411
+ }
412
+
413
+ static PyObject* PyUpb_Descriptor_GetNestedTypesByName(PyObject* _self,
414
+ void* closure) {
415
+ PyUpb_DescriptorBase* self = (void*)_self;
416
+ static PyUpb_ByNameMap_Funcs funcs = {
417
+ {
418
+ (void*)&upb_MessageDef_NestedMessageCount,
419
+ (void*)&upb_MessageDef_NestedMessage,
420
+ (void*)&PyUpb_Descriptor_Get,
421
+ },
422
+ (void*)&PyUpb_Descriptor_LookupNestedMessage,
423
+ (void*)&upb_MessageDef_Name,
424
+ };
425
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
426
+ }
427
+
428
+ static PyObject* PyUpb_Descriptor_GetContainingType(PyObject* _self,
429
+ void* closure) {
430
+ // upb does not natively store the lexical parent of a message type, but we
431
+ // can derive it with some string manipulation and a lookup.
432
+ PyUpb_DescriptorBase* self = (void*)_self;
433
+ const upb_MessageDef* m = self->def;
434
+ const upb_FileDef* file = upb_MessageDef_File(m);
435
+ const upb_DefPool* symtab = upb_FileDef_Pool(file);
436
+ const char* full_name = upb_MessageDef_FullName(m);
437
+ const char* last_dot = strrchr(full_name, '.');
438
+ if (!last_dot) Py_RETURN_NONE;
439
+ const upb_MessageDef* parent = upb_DefPool_FindMessageByNameWithSize(
440
+ symtab, full_name, last_dot - full_name);
441
+ if (!parent) Py_RETURN_NONE;
442
+ return PyUpb_Descriptor_Get(parent);
443
+ }
444
+
445
+ static PyObject* PyUpb_Descriptor_GetEnumTypesByName(PyObject* _self,
446
+ void* closure) {
447
+ PyUpb_DescriptorBase* self = (void*)_self;
448
+ static PyUpb_ByNameMap_Funcs funcs = {
449
+ {
450
+ (void*)&upb_MessageDef_NestedEnumCount,
451
+ (void*)&upb_MessageDef_NestedEnum,
452
+ (void*)&PyUpb_EnumDescriptor_Get,
453
+ },
454
+ (void*)&PyUpb_Descriptor_LookupNestedEnum,
455
+ (void*)&upb_EnumDef_Name,
456
+ };
457
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
458
+ }
459
+
460
+ static PyObject* PyUpb_Descriptor_GetIsExtendable(PyObject* _self,
461
+ void* closure) {
462
+ PyUpb_DescriptorBase* self = (void*)_self;
463
+ if (upb_MessageDef_ExtensionRangeCount(self->def) > 0) {
464
+ Py_RETURN_TRUE;
465
+ } else {
466
+ Py_RETURN_FALSE;
467
+ }
468
+ }
469
+
470
+ static PyObject* PyUpb_Descriptor_GetFullName(PyObject* self, void* closure) {
471
+ const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
472
+ return PyUnicode_FromString(upb_MessageDef_FullName(msgdef));
473
+ }
474
+
475
+ static PyObject* PyUpb_Descriptor_GetConcreteClass(PyObject* self,
476
+ void* closure) {
477
+ const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
478
+ return PyUpb_Descriptor_GetClass(msgdef);
479
+ }
480
+
481
+ static PyObject* PyUpb_Descriptor_GetFile(PyObject* self, void* closure) {
482
+ const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
483
+ return PyUpb_FileDescriptor_Get(upb_MessageDef_File(msgdef));
484
+ }
485
+
486
+ static PyObject* PyUpb_Descriptor_GetFields(PyObject* _self, void* closure) {
487
+ PyUpb_DescriptorBase* self = (void*)_self;
488
+ static PyUpb_GenericSequence_Funcs funcs = {
489
+ (void*)&upb_MessageDef_FieldCount,
490
+ (void*)&upb_MessageDef_Field,
491
+ (void*)&PyUpb_FieldDescriptor_Get,
492
+ };
493
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
494
+ }
495
+
496
+ static PyObject* PyUpb_Descriptor_GetHasOptions(PyObject* _self,
497
+ void* closure) {
498
+ PyUpb_DescriptorBase* self = (void*)_self;
499
+ return PyBool_FromLong(upb_MessageDef_HasOptions(self->def));
500
+ }
501
+
502
+ static PyObject* PyUpb_Descriptor_GetName(PyObject* self, void* closure) {
503
+ const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
504
+ return PyUnicode_FromString(upb_MessageDef_Name(msgdef));
505
+ }
506
+
507
+ static PyObject* PyUpb_Descriptor_GetEnumValuesByName(PyObject* _self,
508
+ void* closure) {
509
+ // upb does not natively store any table containing all nested values.
510
+ // Consider:
511
+ // message M {
512
+ // enum E1 {
513
+ // A = 0;
514
+ // B = 1;
515
+ // }
516
+ // enum E2 {
517
+ // C = 0;
518
+ // D = 1;
519
+ // }
520
+ // }
521
+ //
522
+ // In this case, upb stores tables for E1 and E2, but it does not store a
523
+ // table for M that combines them (it is rarely needed and costs precious
524
+ // space and time to build).
525
+ //
526
+ // To work around this, we build an actual Python dict whenever a user
527
+ // actually asks for this.
528
+ PyUpb_DescriptorBase* self = (void*)_self;
529
+ PyObject* ret = PyDict_New();
530
+ if (!ret) return NULL;
531
+ int enum_count = upb_MessageDef_NestedEnumCount(self->def);
532
+ for (int i = 0; i < enum_count; i++) {
533
+ const upb_EnumDef* e = upb_MessageDef_NestedEnum(self->def, i);
534
+ int value_count = upb_EnumDef_ValueCount(e);
535
+ for (int j = 0; j < value_count; j++) {
536
+ // Collisions should be impossible here, as uniqueness is checked by
537
+ // protoc (this is an invariant of the protobuf language). However this
538
+ // uniqueness constraint is not currently checked by upb/def.c at load
539
+ // time, so if the user supplies a manually-constructed descriptor that
540
+ // does not respect this constraint, a collision could be possible and the
541
+ // last-defined enumerator would win. This could be seen as an argument
542
+ // for having upb actually build the table at load time, thus checking the
543
+ // constraint proactively, but upb is always checking a subset of the full
544
+ // validation performed by C++, and we have to pick and choose the biggest
545
+ // bang for the buck.
546
+ const upb_EnumValueDef* ev = upb_EnumDef_Value(e, j);
547
+ const char* name = upb_EnumValueDef_Name(ev);
548
+ PyObject* val = PyUpb_EnumValueDescriptor_Get(ev);
549
+ if (!val || PyDict_SetItemString(ret, name, val) < 0) {
550
+ Py_XDECREF(val);
551
+ Py_DECREF(ret);
552
+ return NULL;
553
+ }
554
+ Py_DECREF(val);
555
+ }
556
+ }
557
+ return ret;
558
+ }
559
+
560
+ static PyObject* PyUpb_Descriptor_GetOneofsByName(PyObject* _self,
561
+ void* closure) {
562
+ PyUpb_DescriptorBase* self = (void*)_self;
563
+ static PyUpb_ByNameMap_Funcs funcs = {
564
+ {
565
+ (void*)&upb_MessageDef_OneofCount,
566
+ (void*)&upb_MessageDef_Oneof,
567
+ (void*)&PyUpb_OneofDescriptor_Get,
568
+ },
569
+ (void*)&upb_MessageDef_FindOneofByName,
570
+ (void*)&upb_OneofDef_Name,
571
+ };
572
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
573
+ }
574
+
575
+ static PyObject* PyUpb_Descriptor_GetSyntax(PyObject* self, void* closure) {
576
+ const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
577
+ const char* syntax =
578
+ upb_MessageDef_Syntax(msgdef) == kUpb_Syntax_Proto2 ? "proto2" : "proto3";
579
+ return PyUnicode_InternFromString(syntax);
580
+ }
581
+
582
+ static PyGetSetDef PyUpb_Descriptor_Getters[] = {
583
+ {"name", PyUpb_Descriptor_GetName, NULL, "Last name"},
584
+ {"full_name", PyUpb_Descriptor_GetFullName, NULL, "Full name"},
585
+ {"_concrete_class", PyUpb_Descriptor_GetConcreteClass, NULL,
586
+ "concrete class"},
587
+ {"file", PyUpb_Descriptor_GetFile, NULL, "File descriptor"},
588
+ {"fields", PyUpb_Descriptor_GetFields, NULL, "Fields sequence"},
589
+ {"fields_by_name", PyUpb_Descriptor_GetFieldsByName, NULL,
590
+ "Fields by name"},
591
+ {"fields_by_camelcase_name", PyUpb_Descriptor_GetFieldsByCamelCaseName,
592
+ NULL, "Fields by camelCase name"},
593
+ {"fields_by_number", PyUpb_Descriptor_GetFieldsByNumber, NULL,
594
+ "Fields by number"},
595
+ {"nested_types", PyUpb_Descriptor_GetNestedTypes, NULL,
596
+ "Nested types sequence"},
597
+ {"nested_types_by_name", PyUpb_Descriptor_GetNestedTypesByName, NULL,
598
+ "Nested types by name"},
599
+ {"extensions", PyUpb_Descriptor_GetExtensions, NULL, "Extensions Sequence"},
600
+ {"extensions_by_name", PyUpb_Descriptor_GetExtensionsByName, NULL,
601
+ "Extensions by name"},
602
+ {"extension_ranges", PyUpb_Descriptor_GetExtensionRanges, NULL,
603
+ "Extension ranges"},
604
+ {"enum_types", PyUpb_Descriptor_GetEnumTypes, NULL, "Enum sequence"},
605
+ {"enum_types_by_name", PyUpb_Descriptor_GetEnumTypesByName, NULL,
606
+ "Enum types by name"},
607
+ {"enum_values_by_name", PyUpb_Descriptor_GetEnumValuesByName, NULL,
608
+ "Enum values by name"},
609
+ {"oneofs_by_name", PyUpb_Descriptor_GetOneofsByName, NULL,
610
+ "Oneofs by name"},
611
+ {"oneofs", PyUpb_Descriptor_GetOneofs, NULL, "Oneofs Sequence"},
612
+ {"containing_type", PyUpb_Descriptor_GetContainingType, NULL,
613
+ "Containing type"},
614
+ {"is_extendable", PyUpb_Descriptor_GetIsExtendable, NULL},
615
+ {"has_options", PyUpb_Descriptor_GetHasOptions, NULL, "Has Options"},
616
+ {"syntax", &PyUpb_Descriptor_GetSyntax, NULL, "Syntax"},
617
+ {NULL}};
618
+
619
+ static PyMethodDef PyUpb_Descriptor_Methods[] = {
620
+ {"GetOptions", PyUpb_Descriptor_GetOptions, METH_NOARGS},
621
+ {"CopyToProto", PyUpb_Descriptor_CopyToProto, METH_O},
622
+ {"EnumValueName", PyUpb_Descriptor_EnumValueName, METH_VARARGS},
623
+ {NULL}};
624
+
625
+ static PyType_Slot PyUpb_Descriptor_Slots[] = {
626
+ DESCRIPTOR_BASE_SLOTS,
627
+ {Py_tp_methods, PyUpb_Descriptor_Methods},
628
+ {Py_tp_getset, PyUpb_Descriptor_Getters},
629
+ {0, NULL}};
630
+
631
+ static PyType_Spec PyUpb_Descriptor_Spec = {
632
+ PYUPB_MODULE_NAME ".Descriptor", // tp_name
633
+ sizeof(PyUpb_DescriptorBase), // tp_basicsize
634
+ 0, // tp_itemsize
635
+ Py_TPFLAGS_DEFAULT, // tp_flags
636
+ PyUpb_Descriptor_Slots,
637
+ };
638
+
639
+ const upb_MessageDef* PyUpb_Descriptor_GetDef(PyObject* _self) {
640
+ PyUpb_DescriptorBase* self =
641
+ PyUpb_DescriptorBase_Check(_self, kPyUpb_Descriptor);
642
+ return self ? self->def : NULL;
643
+ }
644
+
645
+ // -----------------------------------------------------------------------------
646
+ // EnumDescriptor
647
+ // -----------------------------------------------------------------------------
648
+
649
+ PyObject* PyUpb_EnumDescriptor_Get(const upb_EnumDef* enumdef) {
650
+ const upb_FileDef* file = upb_EnumDef_File(enumdef);
651
+ return PyUpb_DescriptorBase_Get(kPyUpb_EnumDescriptor, enumdef, file);
652
+ }
653
+
654
+ const upb_EnumDef* PyUpb_EnumDescriptor_GetDef(PyObject* _self) {
655
+ PyUpb_DescriptorBase* self =
656
+ PyUpb_DescriptorBase_Check(_self, kPyUpb_EnumDescriptor);
657
+ return self ? self->def : NULL;
658
+ }
659
+
660
+ static PyObject* PyUpb_EnumDescriptor_GetFullName(PyObject* self,
661
+ void* closure) {
662
+ const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(self);
663
+ return PyUnicode_FromString(upb_EnumDef_FullName(enumdef));
664
+ }
665
+
666
+ static PyObject* PyUpb_EnumDescriptor_GetName(PyObject* self, void* closure) {
667
+ const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(self);
668
+ return PyUnicode_FromString(upb_EnumDef_Name(enumdef));
669
+ }
670
+
671
+ static PyObject* PyUpb_EnumDescriptor_GetFile(PyObject* self, void* closure) {
672
+ const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(self);
673
+ return PyUpb_FileDescriptor_Get(upb_EnumDef_File(enumdef));
674
+ }
675
+
676
+ static PyObject* PyUpb_EnumDescriptor_GetValues(PyObject* _self,
677
+ void* closure) {
678
+ PyUpb_DescriptorBase* self = (void*)_self;
679
+ static PyUpb_GenericSequence_Funcs funcs = {
680
+ (void*)&upb_EnumDef_ValueCount,
681
+ (void*)&upb_EnumDef_Value,
682
+ (void*)&PyUpb_EnumValueDescriptor_Get,
683
+ };
684
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
685
+ }
686
+
687
+ static PyObject* PyUpb_EnumDescriptor_GetValuesByName(PyObject* _self,
688
+ void* closure) {
689
+ static PyUpb_ByNameMap_Funcs funcs = {
690
+ {
691
+ (void*)&upb_EnumDef_ValueCount,
692
+ (void*)&upb_EnumDef_Value,
693
+ (void*)&PyUpb_EnumValueDescriptor_Get,
694
+ },
695
+ (void*)&upb_EnumDef_FindValueByName,
696
+ (void*)&upb_EnumValueDef_Name,
697
+ };
698
+ PyUpb_DescriptorBase* self = (void*)_self;
699
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
700
+ }
701
+
702
+ static PyObject* PyUpb_EnumDescriptor_GetValuesByNumber(PyObject* _self,
703
+ void* closure) {
704
+ static PyUpb_ByNumberMap_Funcs funcs = {
705
+ {
706
+ (void*)&upb_EnumDef_ValueCount,
707
+ (void*)&upb_EnumDef_Value,
708
+ (void*)&PyUpb_EnumValueDescriptor_Get,
709
+ },
710
+ (void*)&upb_EnumDef_FindValueByNumber,
711
+ (void*)&upb_EnumValueDef_Number,
712
+ };
713
+ PyUpb_DescriptorBase* self = (void*)_self;
714
+ return PyUpb_ByNumberMap_New(&funcs, self->def, self->pool);
715
+ }
716
+
717
+ static PyObject* PyUpb_EnumDescriptor_GetContainingType(PyObject* _self,
718
+ void* closure) {
719
+ PyUpb_DescriptorBase* self = (void*)_self;
720
+ const upb_MessageDef* m = upb_EnumDef_ContainingType(self->def);
721
+ if (!m) Py_RETURN_NONE;
722
+ return PyUpb_Descriptor_Get(m);
723
+ }
724
+
725
+ static PyObject* PyUpb_EnumDescriptor_GetHasOptions(PyObject* _self,
726
+ void* closure) {
727
+ PyUpb_DescriptorBase* self = (void*)_self;
728
+ return PyBool_FromLong(upb_EnumDef_HasOptions(self->def));
729
+ }
730
+
731
+ static PyObject* PyUpb_EnumDescriptor_GetOptions(PyObject* _self,
732
+ PyObject* args) {
733
+ PyUpb_DescriptorBase* self = (void*)_self;
734
+ return PyUpb_DescriptorBase_GetOptions(self, upb_EnumDef_Options(self->def),
735
+ &google_protobuf_EnumOptions_msginit,
736
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE
737
+ ".EnumOptions");
738
+ }
739
+
740
+ static PyObject* PyUpb_EnumDescriptor_CopyToProto(PyObject* _self,
741
+ PyObject* py_proto) {
742
+ return PyUpb_DescriptorBase_CopyToProto(
743
+ _self, (PyUpb_ToProto_Func*)&upb_EnumDef_ToProto,
744
+ &google_protobuf_EnumDescriptorProto_msginit,
745
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumDescriptorProto", py_proto);
746
+ }
747
+
748
+ static PyGetSetDef PyUpb_EnumDescriptor_Getters[] = {
749
+ {"full_name", PyUpb_EnumDescriptor_GetFullName, NULL, "Full name"},
750
+ {"name", PyUpb_EnumDescriptor_GetName, NULL, "last name"},
751
+ {"file", PyUpb_EnumDescriptor_GetFile, NULL, "File descriptor"},
752
+ {"values", PyUpb_EnumDescriptor_GetValues, NULL, "values"},
753
+ {"values_by_name", PyUpb_EnumDescriptor_GetValuesByName, NULL,
754
+ "Enum values by name"},
755
+ {"values_by_number", PyUpb_EnumDescriptor_GetValuesByNumber, NULL,
756
+ "Enum values by number"},
757
+ {"containing_type", PyUpb_EnumDescriptor_GetContainingType, NULL,
758
+ "Containing type"},
759
+ {"has_options", PyUpb_EnumDescriptor_GetHasOptions, NULL, "Has Options"},
760
+ {NULL}};
761
+
762
+ static PyMethodDef PyUpb_EnumDescriptor_Methods[] = {
763
+ {"GetOptions", PyUpb_EnumDescriptor_GetOptions, METH_NOARGS},
764
+ {"CopyToProto", PyUpb_EnumDescriptor_CopyToProto, METH_O},
765
+ {NULL}};
766
+
767
+ static PyType_Slot PyUpb_EnumDescriptor_Slots[] = {
768
+ DESCRIPTOR_BASE_SLOTS,
769
+ {Py_tp_methods, PyUpb_EnumDescriptor_Methods},
770
+ {Py_tp_getset, PyUpb_EnumDescriptor_Getters},
771
+ {0, NULL}};
772
+
773
+ static PyType_Spec PyUpb_EnumDescriptor_Spec = {
774
+ PYUPB_MODULE_NAME ".EnumDescriptor", // tp_name
775
+ sizeof(PyUpb_DescriptorBase), // tp_basicsize
776
+ 0, // tp_itemsize
777
+ Py_TPFLAGS_DEFAULT, // tp_flags
778
+ PyUpb_EnumDescriptor_Slots,
779
+ };
780
+
781
+ // -----------------------------------------------------------------------------
782
+ // EnumValueDescriptor
783
+ // -----------------------------------------------------------------------------
784
+
785
+ PyObject* PyUpb_EnumValueDescriptor_Get(const upb_EnumValueDef* ev) {
786
+ const upb_FileDef* file = upb_EnumDef_File(upb_EnumValueDef_Enum(ev));
787
+ return PyUpb_DescriptorBase_Get(kPyUpb_EnumValueDescriptor, ev, file);
788
+ }
789
+
790
+ static PyObject* PyUpb_EnumValueDescriptor_GetName(PyObject* self,
791
+ void* closure) {
792
+ PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self;
793
+ return PyUnicode_FromString(upb_EnumValueDef_Name(base->def));
794
+ }
795
+
796
+ static PyObject* PyUpb_EnumValueDescriptor_GetNumber(PyObject* self,
797
+ void* closure) {
798
+ PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self;
799
+ return PyLong_FromLong(upb_EnumValueDef_Number(base->def));
800
+ }
801
+
802
+ static PyObject* PyUpb_EnumValueDescriptor_GetIndex(PyObject* self,
803
+ void* closure) {
804
+ PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self;
805
+ return PyLong_FromLong(upb_EnumValueDef_Index(base->def));
806
+ }
807
+
808
+ static PyObject* PyUpb_EnumValueDescriptor_GetType(PyObject* self,
809
+ void* closure) {
810
+ PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self;
811
+ return PyUpb_EnumDescriptor_Get(upb_EnumValueDef_Enum(base->def));
812
+ }
813
+
814
+ static PyObject* PyUpb_EnumValueDescriptor_GetHasOptions(PyObject* _self,
815
+ void* closure) {
816
+ PyUpb_DescriptorBase* self = (void*)_self;
817
+ return PyBool_FromLong(upb_EnumValueDef_HasOptions(self->def));
818
+ }
819
+
820
+ static PyObject* PyUpb_EnumValueDescriptor_GetOptions(PyObject* _self,
821
+ PyObject* args) {
822
+ PyUpb_DescriptorBase* self = (void*)_self;
823
+ return PyUpb_DescriptorBase_GetOptions(
824
+ self, upb_EnumValueDef_Options(self->def),
825
+ &google_protobuf_EnumValueOptions_msginit,
826
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumValueOptions");
827
+ }
828
+
829
+ static PyGetSetDef PyUpb_EnumValueDescriptor_Getters[] = {
830
+ {"name", PyUpb_EnumValueDescriptor_GetName, NULL, "name"},
831
+ {"number", PyUpb_EnumValueDescriptor_GetNumber, NULL, "number"},
832
+ {"index", PyUpb_EnumValueDescriptor_GetIndex, NULL, "index"},
833
+ {"type", PyUpb_EnumValueDescriptor_GetType, NULL, "index"},
834
+ {"has_options", PyUpb_EnumValueDescriptor_GetHasOptions, NULL,
835
+ "Has Options"},
836
+ {NULL}};
837
+
838
+ static PyMethodDef PyUpb_EnumValueDescriptor_Methods[] = {
839
+ {
840
+ "GetOptions",
841
+ PyUpb_EnumValueDescriptor_GetOptions,
842
+ METH_NOARGS,
843
+ },
844
+ {NULL}};
845
+
846
+ static PyType_Slot PyUpb_EnumValueDescriptor_Slots[] = {
847
+ DESCRIPTOR_BASE_SLOTS,
848
+ {Py_tp_methods, PyUpb_EnumValueDescriptor_Methods},
849
+ {Py_tp_getset, PyUpb_EnumValueDescriptor_Getters},
850
+ {0, NULL}};
851
+
852
+ static PyType_Spec PyUpb_EnumValueDescriptor_Spec = {
853
+ PYUPB_MODULE_NAME ".EnumValueDescriptor", // tp_name
854
+ sizeof(PyUpb_DescriptorBase), // tp_basicsize
855
+ 0, // tp_itemsize
856
+ Py_TPFLAGS_DEFAULT, // tp_flags
857
+ PyUpb_EnumValueDescriptor_Slots,
858
+ };
859
+
860
+ // -----------------------------------------------------------------------------
861
+ // FieldDescriptor
862
+ // -----------------------------------------------------------------------------
863
+
864
+ const upb_FieldDef* PyUpb_FieldDescriptor_GetDef(PyObject* _self) {
865
+ PyUpb_DescriptorBase* self =
866
+ PyUpb_DescriptorBase_Check(_self, kPyUpb_FieldDescriptor);
867
+ return self ? self->def : NULL;
868
+ }
869
+
870
+ PyObject* PyUpb_FieldDescriptor_Get(const upb_FieldDef* field) {
871
+ const upb_FileDef* file = upb_FieldDef_File(field);
872
+ return PyUpb_DescriptorBase_Get(kPyUpb_FieldDescriptor, field, file);
873
+ }
874
+
875
+ static PyObject* PyUpb_FieldDescriptor_GetFullName(PyUpb_DescriptorBase* self,
876
+ void* closure) {
877
+ return PyUnicode_FromString(upb_FieldDef_FullName(self->def));
878
+ }
879
+
880
+ static PyObject* PyUpb_FieldDescriptor_GetName(PyUpb_DescriptorBase* self,
881
+ void* closure) {
882
+ return PyUnicode_FromString(upb_FieldDef_Name(self->def));
883
+ }
884
+
885
+ static PyObject* PyUpb_FieldDescriptor_GetCamelCaseName(
886
+ PyUpb_DescriptorBase* self, void* closure) {
887
+ // TODO: Ok to use jsonname here?
888
+ return PyUnicode_FromString(upb_FieldDef_JsonName(self->def));
889
+ }
890
+
891
+ static PyObject* PyUpb_FieldDescriptor_GetJsonName(PyUpb_DescriptorBase* self,
892
+ void* closure) {
893
+ return PyUnicode_FromString(upb_FieldDef_JsonName(self->def));
894
+ }
895
+
896
+ static PyObject* PyUpb_FieldDescriptor_GetFile(PyUpb_DescriptorBase* self,
897
+ void* closure) {
898
+ const upb_FileDef* file = upb_FieldDef_File(self->def);
899
+ if (!file) Py_RETURN_NONE;
900
+ return PyUpb_FileDescriptor_Get(file);
901
+ }
902
+
903
+ static PyObject* PyUpb_FieldDescriptor_GetType(PyUpb_DescriptorBase* self,
904
+ void* closure) {
905
+ return PyLong_FromLong(upb_FieldDef_Type(self->def));
906
+ }
907
+
908
+ static PyObject* PyUpb_FieldDescriptor_GetCppType(PyUpb_DescriptorBase* self,
909
+ void* closure) {
910
+ // Enum values copied from descriptor.h in C++.
911
+ enum CppType {
912
+ CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32
913
+ CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
914
+ CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32
915
+ CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64
916
+ CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE
917
+ CPPTYPE_FLOAT = 6, // TYPE_FLOAT
918
+ CPPTYPE_BOOL = 7, // TYPE_BOOL
919
+ CPPTYPE_ENUM = 8, // TYPE_ENUM
920
+ CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES
921
+ CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP
922
+ };
923
+ static const uint8_t cpp_types[] = {
924
+ -1,
925
+ [kUpb_CType_Int32] = CPPTYPE_INT32,
926
+ [kUpb_CType_Int64] = CPPTYPE_INT64,
927
+ [kUpb_CType_UInt32] = CPPTYPE_UINT32,
928
+ [kUpb_CType_UInt64] = CPPTYPE_UINT64,
929
+ [kUpb_CType_Double] = CPPTYPE_DOUBLE,
930
+ [kUpb_CType_Float] = CPPTYPE_FLOAT,
931
+ [kUpb_CType_Bool] = CPPTYPE_BOOL,
932
+ [kUpb_CType_Enum] = CPPTYPE_ENUM,
933
+ [kUpb_CType_String] = CPPTYPE_STRING,
934
+ [kUpb_CType_Bytes] = CPPTYPE_STRING,
935
+ [kUpb_CType_Message] = CPPTYPE_MESSAGE,
936
+ };
937
+ return PyLong_FromLong(cpp_types[upb_FieldDef_CType(self->def)]);
938
+ }
939
+
940
+ static PyObject* PyUpb_FieldDescriptor_GetLabel(PyUpb_DescriptorBase* self,
941
+ void* closure) {
942
+ return PyLong_FromLong(upb_FieldDef_Label(self->def));
943
+ }
944
+
945
+ static PyObject* PyUpb_FieldDescriptor_GetIsExtension(
946
+ PyUpb_DescriptorBase* self, void* closure) {
947
+ return PyBool_FromLong(upb_FieldDef_IsExtension(self->def));
948
+ }
949
+
950
+ static PyObject* PyUpb_FieldDescriptor_GetNumber(PyUpb_DescriptorBase* self,
951
+ void* closure) {
952
+ return PyLong_FromLong(upb_FieldDef_Number(self->def));
953
+ }
954
+
955
+ static PyObject* PyUpb_FieldDescriptor_GetIndex(PyUpb_DescriptorBase* self,
956
+ void* closure) {
957
+ return PyLong_FromLong(upb_FieldDef_Index(self->def));
958
+ }
959
+
960
+ static PyObject* PyUpb_FieldDescriptor_GetMessageType(
961
+ PyUpb_DescriptorBase* self, void* closure) {
962
+ const upb_MessageDef* subdef = upb_FieldDef_MessageSubDef(self->def);
963
+ if (!subdef) Py_RETURN_NONE;
964
+ return PyUpb_Descriptor_Get(subdef);
965
+ }
966
+
967
+ static PyObject* PyUpb_FieldDescriptor_GetEnumType(PyUpb_DescriptorBase* self,
968
+ void* closure) {
969
+ const upb_EnumDef* enumdef = upb_FieldDef_EnumSubDef(self->def);
970
+ if (!enumdef) Py_RETURN_NONE;
971
+ return PyUpb_EnumDescriptor_Get(enumdef);
972
+ }
973
+
974
+ static PyObject* PyUpb_FieldDescriptor_GetContainingType(
975
+ PyUpb_DescriptorBase* self, void* closure) {
976
+ const upb_MessageDef* m = upb_FieldDef_ContainingType(self->def);
977
+ if (!m) Py_RETURN_NONE;
978
+ return PyUpb_Descriptor_Get(m);
979
+ }
980
+
981
+ static PyObject* PyUpb_FieldDescriptor_GetExtensionScope(
982
+ PyUpb_DescriptorBase* self, void* closure) {
983
+ const upb_MessageDef* m = upb_FieldDef_ExtensionScope(self->def);
984
+ if (!m) Py_RETURN_NONE;
985
+ return PyUpb_Descriptor_Get(m);
986
+ }
987
+
988
+ static PyObject* PyUpb_FieldDescriptor_HasDefaultValue(
989
+ PyUpb_DescriptorBase* self, void* closure) {
990
+ return PyBool_FromLong(upb_FieldDef_HasDefault(self->def));
991
+ }
992
+
993
+ static PyObject* PyUpb_FieldDescriptor_GetDefaultValue(
994
+ PyUpb_DescriptorBase* self, void* closure) {
995
+ const upb_FieldDef* f = self->def;
996
+ if (upb_FieldDef_IsRepeated(f)) return PyList_New(0);
997
+ if (upb_FieldDef_IsSubMessage(f)) Py_RETURN_NONE;
998
+ return PyUpb_UpbToPy(upb_FieldDef_Default(self->def), self->def, NULL);
999
+ }
1000
+
1001
+ static PyObject* PyUpb_FieldDescriptor_GetContainingOneof(
1002
+ PyUpb_DescriptorBase* self, void* closure) {
1003
+ const upb_OneofDef* oneof = upb_FieldDef_ContainingOneof(self->def);
1004
+ if (!oneof) Py_RETURN_NONE;
1005
+ return PyUpb_OneofDescriptor_Get(oneof);
1006
+ }
1007
+
1008
+ static PyObject* PyUpb_FieldDescriptor_GetHasOptions(
1009
+ PyUpb_DescriptorBase* _self, void* closure) {
1010
+ PyUpb_DescriptorBase* self = (void*)_self;
1011
+ return PyBool_FromLong(upb_FieldDef_HasOptions(self->def));
1012
+ }
1013
+
1014
+ static PyObject* PyUpb_FieldDescriptor_GetHasPresence(
1015
+ PyUpb_DescriptorBase* _self, void* closure) {
1016
+ PyUpb_DescriptorBase* self = (void*)_self;
1017
+ return PyBool_FromLong(upb_FieldDef_HasPresence(self->def));
1018
+ }
1019
+
1020
+ static PyObject* PyUpb_FieldDescriptor_GetOptions(PyObject* _self,
1021
+ PyObject* args) {
1022
+ PyUpb_DescriptorBase* self = (void*)_self;
1023
+ return PyUpb_DescriptorBase_GetOptions(self, upb_FieldDef_Options(self->def),
1024
+ &google_protobuf_FieldOptions_msginit,
1025
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE
1026
+ ".FieldOptions");
1027
+ }
1028
+
1029
+ static PyGetSetDef PyUpb_FieldDescriptor_Getters[] = {
1030
+ {"full_name", (getter)PyUpb_FieldDescriptor_GetFullName, NULL, "Full name"},
1031
+ {"name", (getter)PyUpb_FieldDescriptor_GetName, NULL, "Unqualified name"},
1032
+ {"camelcase_name", (getter)PyUpb_FieldDescriptor_GetCamelCaseName, NULL,
1033
+ "CamelCase name"},
1034
+ {"json_name", (getter)PyUpb_FieldDescriptor_GetJsonName, NULL, "Json name"},
1035
+ {"file", (getter)PyUpb_FieldDescriptor_GetFile, NULL, "File Descriptor"},
1036
+ {"type", (getter)PyUpb_FieldDescriptor_GetType, NULL, "Type"},
1037
+ {"cpp_type", (getter)PyUpb_FieldDescriptor_GetCppType, NULL, "C++ Type"},
1038
+ {"label", (getter)PyUpb_FieldDescriptor_GetLabel, NULL, "Label"},
1039
+ {"number", (getter)PyUpb_FieldDescriptor_GetNumber, NULL, "Number"},
1040
+ {"index", (getter)PyUpb_FieldDescriptor_GetIndex, NULL, "Index"},
1041
+ {"default_value", (getter)PyUpb_FieldDescriptor_GetDefaultValue, NULL,
1042
+ "Default Value"},
1043
+ {"has_default_value", (getter)PyUpb_FieldDescriptor_HasDefaultValue},
1044
+ {"is_extension", (getter)PyUpb_FieldDescriptor_GetIsExtension, NULL, "ID"},
1045
+ // TODO(https://github.com/protocolbuffers/upb/issues/459)
1046
+ //{ "id", (getter)GetID, NULL, "ID"},
1047
+ {"message_type", (getter)PyUpb_FieldDescriptor_GetMessageType, NULL,
1048
+ "Message type"},
1049
+ {"enum_type", (getter)PyUpb_FieldDescriptor_GetEnumType, NULL, "Enum type"},
1050
+ {"containing_type", (getter)PyUpb_FieldDescriptor_GetContainingType, NULL,
1051
+ "Containing type"},
1052
+ {"extension_scope", (getter)PyUpb_FieldDescriptor_GetExtensionScope, NULL,
1053
+ "Extension scope"},
1054
+ {"containing_oneof", (getter)PyUpb_FieldDescriptor_GetContainingOneof, NULL,
1055
+ "Containing oneof"},
1056
+ {"has_options", (getter)PyUpb_FieldDescriptor_GetHasOptions, NULL,
1057
+ "Has Options"},
1058
+ {"has_presence", (getter)PyUpb_FieldDescriptor_GetHasPresence, NULL,
1059
+ "Has Presence"},
1060
+ // TODO(https://github.com/protocolbuffers/upb/issues/459)
1061
+ //{ "_options",
1062
+ //(getter)NULL, (setter)SetOptions, "Options"}, { "_serialized_options",
1063
+ //(getter)NULL, (setter)SetSerializedOptions, "Serialized Options"},
1064
+ {NULL}};
1065
+
1066
+ static PyMethodDef PyUpb_FieldDescriptor_Methods[] = {
1067
+ {
1068
+ "GetOptions",
1069
+ PyUpb_FieldDescriptor_GetOptions,
1070
+ METH_NOARGS,
1071
+ },
1072
+ {NULL}};
1073
+
1074
+ static PyType_Slot PyUpb_FieldDescriptor_Slots[] = {
1075
+ DESCRIPTOR_BASE_SLOTS,
1076
+ {Py_tp_methods, PyUpb_FieldDescriptor_Methods},
1077
+ {Py_tp_getset, PyUpb_FieldDescriptor_Getters},
1078
+ {0, NULL}};
1079
+
1080
+ static PyType_Spec PyUpb_FieldDescriptor_Spec = {
1081
+ PYUPB_MODULE_NAME ".FieldDescriptor",
1082
+ sizeof(PyUpb_DescriptorBase),
1083
+ 0, // tp_itemsize
1084
+ Py_TPFLAGS_DEFAULT,
1085
+ PyUpb_FieldDescriptor_Slots,
1086
+ };
1087
+
1088
+ // -----------------------------------------------------------------------------
1089
+ // FileDescriptor
1090
+ // -----------------------------------------------------------------------------
1091
+
1092
+ PyObject* PyUpb_FileDescriptor_Get(const upb_FileDef* file) {
1093
+ return PyUpb_DescriptorBase_Get(kPyUpb_FileDescriptor, file, file);
1094
+ }
1095
+
1096
+ // These are not provided on upb_FileDef because they use the underlying
1097
+ // symtab's hash table. This works for Python because everything happens under
1098
+ // the GIL, but in general the caller has to guarantee that the symtab is not
1099
+ // being mutated concurrently.
1100
+ typedef const void* PyUpb_FileDescriptor_LookupFunc(const upb_DefPool*,
1101
+ const char*);
1102
+
1103
+ static const void* PyUpb_FileDescriptor_NestedLookup(
1104
+ const upb_FileDef* filedef, const char* name,
1105
+ PyUpb_FileDescriptor_LookupFunc* func) {
1106
+ const upb_DefPool* symtab = upb_FileDef_Pool(filedef);
1107
+ const char* package = upb_FileDef_Package(filedef);
1108
+ if (package) {
1109
+ PyObject* qname = PyUnicode_FromFormat("%s.%s", package, name);
1110
+ const void* ret = func(symtab, PyUnicode_AsUTF8AndSize(qname, NULL));
1111
+ Py_DECREF(qname);
1112
+ return ret;
1113
+ } else {
1114
+ return func(symtab, name);
1115
+ }
1116
+ }
1117
+
1118
+ static const void* PyUpb_FileDescriptor_LookupMessage(
1119
+ const upb_FileDef* filedef, const char* name) {
1120
+ return PyUpb_FileDescriptor_NestedLookup(
1121
+ filedef, name, (void*)&upb_DefPool_FindMessageByName);
1122
+ }
1123
+
1124
+ static const void* PyUpb_FileDescriptor_LookupEnum(const upb_FileDef* filedef,
1125
+ const char* name) {
1126
+ return PyUpb_FileDescriptor_NestedLookup(filedef, name,
1127
+ (void*)&upb_DefPool_FindEnumByName);
1128
+ }
1129
+
1130
+ static const void* PyUpb_FileDescriptor_LookupExtension(
1131
+ const upb_FileDef* filedef, const char* name) {
1132
+ return PyUpb_FileDescriptor_NestedLookup(
1133
+ filedef, name, (void*)&upb_DefPool_FindExtensionByName);
1134
+ }
1135
+
1136
+ static const void* PyUpb_FileDescriptor_LookupService(
1137
+ const upb_FileDef* filedef, const char* name) {
1138
+ return PyUpb_FileDescriptor_NestedLookup(
1139
+ filedef, name, (void*)&upb_DefPool_FindServiceByName);
1140
+ }
1141
+
1142
+ static PyObject* PyUpb_FileDescriptor_GetName(PyUpb_DescriptorBase* self,
1143
+ void* closure) {
1144
+ return PyUnicode_FromString(upb_FileDef_Name(self->def));
1145
+ }
1146
+
1147
+ static PyObject* PyUpb_FileDescriptor_GetPool(PyObject* _self, void* closure) {
1148
+ PyUpb_DescriptorBase* self = (PyUpb_DescriptorBase*)_self;
1149
+ Py_INCREF(self->pool);
1150
+ return self->pool;
1151
+ }
1152
+
1153
+ static PyObject* PyUpb_FileDescriptor_GetPackage(PyObject* _self,
1154
+ void* closure) {
1155
+ PyUpb_DescriptorBase* self = (PyUpb_DescriptorBase*)_self;
1156
+ return PyUnicode_FromString(upb_FileDef_Package(self->def));
1157
+ }
1158
+
1159
+ static PyObject* PyUpb_FileDescriptor_GetSerializedPb(PyObject* self,
1160
+ void* closure) {
1161
+ return PyUpb_DescriptorBase_GetSerializedProto(
1162
+ self, (PyUpb_ToProto_Func*)&upb_FileDef_ToProto,
1163
+ &google_protobuf_FileDescriptorProto_msginit);
1164
+ }
1165
+
1166
+ static PyObject* PyUpb_FileDescriptor_GetMessageTypesByName(PyObject* _self,
1167
+ void* closure) {
1168
+ static PyUpb_ByNameMap_Funcs funcs = {
1169
+ {
1170
+ (void*)&upb_FileDef_TopLevelMessageCount,
1171
+ (void*)&upb_FileDef_TopLevelMessage,
1172
+ (void*)&PyUpb_Descriptor_Get,
1173
+ },
1174
+ (void*)&PyUpb_FileDescriptor_LookupMessage,
1175
+ (void*)&upb_MessageDef_Name,
1176
+ };
1177
+ PyUpb_DescriptorBase* self = (void*)_self;
1178
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
1179
+ }
1180
+
1181
+ static PyObject* PyUpb_FileDescriptor_GetEnumTypesByName(PyObject* _self,
1182
+ void* closure) {
1183
+ static PyUpb_ByNameMap_Funcs funcs = {
1184
+ {
1185
+ (void*)&upb_FileDef_TopLevelEnumCount,
1186
+ (void*)&upb_FileDef_TopLevelEnum,
1187
+ (void*)&PyUpb_EnumDescriptor_Get,
1188
+ },
1189
+ (void*)&PyUpb_FileDescriptor_LookupEnum,
1190
+ (void*)&upb_EnumDef_Name,
1191
+ };
1192
+ PyUpb_DescriptorBase* self = (void*)_self;
1193
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
1194
+ }
1195
+
1196
+ static PyObject* PyUpb_FileDescriptor_GetExtensionsByName(PyObject* _self,
1197
+ void* closure) {
1198
+ static PyUpb_ByNameMap_Funcs funcs = {
1199
+ {
1200
+ (void*)&upb_FileDef_TopLevelExtensionCount,
1201
+ (void*)&upb_FileDef_TopLevelExtension,
1202
+ (void*)&PyUpb_FieldDescriptor_Get,
1203
+ },
1204
+ (void*)&PyUpb_FileDescriptor_LookupExtension,
1205
+ (void*)&upb_FieldDef_Name,
1206
+ };
1207
+ PyUpb_DescriptorBase* self = (void*)_self;
1208
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
1209
+ }
1210
+
1211
+ static PyObject* PyUpb_FileDescriptor_GetServicesByName(PyObject* _self,
1212
+ void* closure) {
1213
+ static PyUpb_ByNameMap_Funcs funcs = {
1214
+ {
1215
+ (void*)&upb_FileDef_ServiceCount,
1216
+ (void*)&upb_FileDef_Service,
1217
+ (void*)&PyUpb_ServiceDescriptor_Get,
1218
+ },
1219
+ (void*)&PyUpb_FileDescriptor_LookupService,
1220
+ (void*)&upb_ServiceDef_Name,
1221
+ };
1222
+ PyUpb_DescriptorBase* self = (void*)_self;
1223
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
1224
+ }
1225
+
1226
+ static PyObject* PyUpb_FileDescriptor_GetDependencies(PyObject* _self,
1227
+ void* closure) {
1228
+ PyUpb_DescriptorBase* self = (void*)_self;
1229
+ static PyUpb_GenericSequence_Funcs funcs = {
1230
+ (void*)&upb_FileDef_DependencyCount,
1231
+ (void*)&upb_FileDef_Dependency,
1232
+ (void*)&PyUpb_FileDescriptor_Get,
1233
+ };
1234
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
1235
+ }
1236
+
1237
+ static PyObject* PyUpb_FileDescriptor_GetPublicDependencies(PyObject* _self,
1238
+ void* closure) {
1239
+ PyUpb_DescriptorBase* self = (void*)_self;
1240
+ static PyUpb_GenericSequence_Funcs funcs = {
1241
+ (void*)&upb_FileDef_PublicDependencyCount,
1242
+ (void*)&upb_FileDef_PublicDependency,
1243
+ (void*)&PyUpb_FileDescriptor_Get,
1244
+ };
1245
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
1246
+ }
1247
+
1248
+ static PyObject* PyUpb_FileDescriptor_GetSyntax(PyObject* _self,
1249
+ void* closure) {
1250
+ PyUpb_DescriptorBase* self = (void*)_self;
1251
+ const char* syntax =
1252
+ upb_FileDef_Syntax(self->def) == kUpb_Syntax_Proto2 ? "proto2" : "proto3";
1253
+ return PyUnicode_FromString(syntax);
1254
+ }
1255
+
1256
+ static PyObject* PyUpb_FileDescriptor_GetHasOptions(PyObject* _self,
1257
+ void* closure) {
1258
+ PyUpb_DescriptorBase* self = (void*)_self;
1259
+ return PyBool_FromLong(upb_FileDef_HasOptions(self->def));
1260
+ }
1261
+
1262
+ static PyObject* PyUpb_FileDescriptor_GetOptions(PyObject* _self,
1263
+ PyObject* args) {
1264
+ PyUpb_DescriptorBase* self = (void*)_self;
1265
+ return PyUpb_DescriptorBase_GetOptions(self, upb_FileDef_Options(self->def),
1266
+ &google_protobuf_FileOptions_msginit,
1267
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE
1268
+ ".FileOptions");
1269
+ }
1270
+
1271
+ static PyObject* PyUpb_FileDescriptor_CopyToProto(PyObject* _self,
1272
+ PyObject* py_proto) {
1273
+ return PyUpb_DescriptorBase_CopyToProto(
1274
+ _self, (PyUpb_ToProto_Func*)&upb_FileDef_ToProto,
1275
+ &google_protobuf_FileDescriptorProto_msginit,
1276
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE ".FileDescriptorProto", py_proto);
1277
+ }
1278
+
1279
+ static PyGetSetDef PyUpb_FileDescriptor_Getters[] = {
1280
+ {"pool", PyUpb_FileDescriptor_GetPool, NULL, "pool"},
1281
+ {"name", (getter)PyUpb_FileDescriptor_GetName, NULL, "name"},
1282
+ {"package", PyUpb_FileDescriptor_GetPackage, NULL, "package"},
1283
+ {"serialized_pb", PyUpb_FileDescriptor_GetSerializedPb},
1284
+ {"message_types_by_name", PyUpb_FileDescriptor_GetMessageTypesByName, NULL,
1285
+ "Messages by name"},
1286
+ {"enum_types_by_name", PyUpb_FileDescriptor_GetEnumTypesByName, NULL,
1287
+ "Enums by name"},
1288
+ {"extensions_by_name", PyUpb_FileDescriptor_GetExtensionsByName, NULL,
1289
+ "Extensions by name"},
1290
+ {"services_by_name", PyUpb_FileDescriptor_GetServicesByName, NULL,
1291
+ "Services by name"},
1292
+ {"dependencies", PyUpb_FileDescriptor_GetDependencies, NULL,
1293
+ "Dependencies"},
1294
+ {"public_dependencies", PyUpb_FileDescriptor_GetPublicDependencies, NULL,
1295
+ "Dependencies"},
1296
+ {"has_options", PyUpb_FileDescriptor_GetHasOptions, NULL, "Has Options"},
1297
+ {"syntax", PyUpb_FileDescriptor_GetSyntax, (setter)NULL, "Syntax"},
1298
+ {NULL},
1299
+ };
1300
+
1301
+ static PyMethodDef PyUpb_FileDescriptor_Methods[] = {
1302
+ {"GetOptions", PyUpb_FileDescriptor_GetOptions, METH_NOARGS},
1303
+ {"CopyToProto", PyUpb_FileDescriptor_CopyToProto, METH_O},
1304
+ {NULL}};
1305
+
1306
+ static PyType_Slot PyUpb_FileDescriptor_Slots[] = {
1307
+ DESCRIPTOR_BASE_SLOTS,
1308
+ {Py_tp_methods, PyUpb_FileDescriptor_Methods},
1309
+ {Py_tp_getset, PyUpb_FileDescriptor_Getters},
1310
+ {0, NULL}};
1311
+
1312
+ static PyType_Spec PyUpb_FileDescriptor_Spec = {
1313
+ PYUPB_MODULE_NAME ".FileDescriptor", // tp_name
1314
+ sizeof(PyUpb_DescriptorBase), // tp_basicsize
1315
+ 0, // tp_itemsize
1316
+ Py_TPFLAGS_DEFAULT, // tp_flags
1317
+ PyUpb_FileDescriptor_Slots,
1318
+ };
1319
+
1320
+ const upb_FileDef* PyUpb_FileDescriptor_GetDef(PyObject* _self) {
1321
+ PyUpb_DescriptorBase* self =
1322
+ PyUpb_DescriptorBase_Check(_self, kPyUpb_FileDescriptor);
1323
+ return self ? self->def : NULL;
1324
+ }
1325
+
1326
+ // -----------------------------------------------------------------------------
1327
+ // MethodDescriptor
1328
+ // -----------------------------------------------------------------------------
1329
+
1330
+ const upb_MethodDef* PyUpb_MethodDescriptor_GetDef(PyObject* _self) {
1331
+ PyUpb_DescriptorBase* self =
1332
+ PyUpb_DescriptorBase_Check(_self, kPyUpb_MethodDescriptor);
1333
+ return self ? self->def : NULL;
1334
+ }
1335
+
1336
+ PyObject* PyUpb_MethodDescriptor_Get(const upb_MethodDef* m) {
1337
+ const upb_FileDef* file = upb_ServiceDef_File(upb_MethodDef_Service(m));
1338
+ return PyUpb_DescriptorBase_Get(kPyUpb_MethodDescriptor, m, file);
1339
+ }
1340
+
1341
+ static PyObject* PyUpb_MethodDescriptor_GetName(PyObject* self, void* closure) {
1342
+ const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self);
1343
+ return PyUnicode_FromString(upb_MethodDef_Name(m));
1344
+ }
1345
+
1346
+ static PyObject* PyUpb_MethodDescriptor_GetFullName(PyObject* self,
1347
+ void* closure) {
1348
+ const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self);
1349
+ return PyUnicode_FromString(upb_MethodDef_FullName(m));
1350
+ }
1351
+
1352
+ static PyObject* PyUpb_MethodDescriptor_GetIndex(PyObject* self,
1353
+ void* closure) {
1354
+ const upb_MethodDef* oneof = PyUpb_MethodDescriptor_GetDef(self);
1355
+ return PyLong_FromLong(upb_MethodDef_Index(oneof));
1356
+ }
1357
+
1358
+ static PyObject* PyUpb_MethodDescriptor_GetContainingService(PyObject* self,
1359
+ void* closure) {
1360
+ const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self);
1361
+ return PyUpb_ServiceDescriptor_Get(upb_MethodDef_Service(m));
1362
+ }
1363
+
1364
+ static PyObject* PyUpb_MethodDescriptor_GetInputType(PyObject* self,
1365
+ void* closure) {
1366
+ const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self);
1367
+ return PyUpb_Descriptor_Get(upb_MethodDef_InputType(m));
1368
+ }
1369
+
1370
+ static PyObject* PyUpb_MethodDescriptor_GetOutputType(PyObject* self,
1371
+ void* closure) {
1372
+ const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self);
1373
+ return PyUpb_Descriptor_Get(upb_MethodDef_OutputType(m));
1374
+ }
1375
+
1376
+ static PyObject* PyUpb_MethodDescriptor_GetOptions(PyObject* _self,
1377
+ PyObject* args) {
1378
+ PyUpb_DescriptorBase* self = (void*)_self;
1379
+ return PyUpb_DescriptorBase_GetOptions(self, upb_MethodDef_Options(self->def),
1380
+ &google_protobuf_MethodOptions_msginit,
1381
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE
1382
+ ".MethodOptions");
1383
+ }
1384
+
1385
+ static PyObject* PyUpb_MethodDescriptor_CopyToProto(PyObject* _self,
1386
+ PyObject* py_proto) {
1387
+ return PyUpb_DescriptorBase_CopyToProto(
1388
+ _self, (PyUpb_ToProto_Func*)&upb_MethodDef_ToProto,
1389
+ &google_protobuf_MethodDescriptorProto_msginit,
1390
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE ".MethodDescriptorProto", py_proto);
1391
+ }
1392
+
1393
+ static PyGetSetDef PyUpb_MethodDescriptor_Getters[] = {
1394
+ {"name", PyUpb_MethodDescriptor_GetName, NULL, "Name", NULL},
1395
+ {"full_name", PyUpb_MethodDescriptor_GetFullName, NULL, "Full name", NULL},
1396
+ {"index", PyUpb_MethodDescriptor_GetIndex, NULL, "Index", NULL},
1397
+ {"containing_service", PyUpb_MethodDescriptor_GetContainingService, NULL,
1398
+ "Containing service", NULL},
1399
+ {"input_type", PyUpb_MethodDescriptor_GetInputType, NULL, "Input type",
1400
+ NULL},
1401
+ {"output_type", PyUpb_MethodDescriptor_GetOutputType, NULL, "Output type",
1402
+ NULL},
1403
+ {NULL}};
1404
+
1405
+ static PyMethodDef PyUpb_MethodDescriptor_Methods[] = {
1406
+ {"GetOptions", PyUpb_MethodDescriptor_GetOptions, METH_NOARGS},
1407
+ {"CopyToProto", PyUpb_MethodDescriptor_CopyToProto, METH_O},
1408
+ {NULL}};
1409
+
1410
+ static PyType_Slot PyUpb_MethodDescriptor_Slots[] = {
1411
+ DESCRIPTOR_BASE_SLOTS,
1412
+ {Py_tp_methods, PyUpb_MethodDescriptor_Methods},
1413
+ {Py_tp_getset, PyUpb_MethodDescriptor_Getters},
1414
+ {0, NULL}};
1415
+
1416
+ static PyType_Spec PyUpb_MethodDescriptor_Spec = {
1417
+ PYUPB_MODULE_NAME ".MethodDescriptor", // tp_name
1418
+ sizeof(PyUpb_DescriptorBase), // tp_basicsize
1419
+ 0, // tp_itemsize
1420
+ Py_TPFLAGS_DEFAULT, // tp_flags
1421
+ PyUpb_MethodDescriptor_Slots,
1422
+ };
1423
+
1424
+ // -----------------------------------------------------------------------------
1425
+ // OneofDescriptor
1426
+ // -----------------------------------------------------------------------------
1427
+
1428
+ const upb_OneofDef* PyUpb_OneofDescriptor_GetDef(PyObject* _self) {
1429
+ PyUpb_DescriptorBase* self =
1430
+ PyUpb_DescriptorBase_Check(_self, kPyUpb_OneofDescriptor);
1431
+ return self ? self->def : NULL;
1432
+ }
1433
+
1434
+ PyObject* PyUpb_OneofDescriptor_Get(const upb_OneofDef* oneof) {
1435
+ const upb_FileDef* file =
1436
+ upb_MessageDef_File(upb_OneofDef_ContainingType(oneof));
1437
+ return PyUpb_DescriptorBase_Get(kPyUpb_OneofDescriptor, oneof, file);
1438
+ }
1439
+
1440
+ static PyObject* PyUpb_OneofDescriptor_GetName(PyObject* self, void* closure) {
1441
+ const upb_OneofDef* oneof = PyUpb_OneofDescriptor_GetDef(self);
1442
+ return PyUnicode_FromString(upb_OneofDef_Name(oneof));
1443
+ }
1444
+
1445
+ static PyObject* PyUpb_OneofDescriptor_GetFullName(PyObject* self,
1446
+ void* closure) {
1447
+ const upb_OneofDef* oneof = PyUpb_OneofDescriptor_GetDef(self);
1448
+ return PyUnicode_FromFormat(
1449
+ "%s.%s", upb_MessageDef_FullName(upb_OneofDef_ContainingType(oneof)),
1450
+ upb_OneofDef_Name(oneof));
1451
+ }
1452
+
1453
+ static PyObject* PyUpb_OneofDescriptor_GetIndex(PyObject* self, void* closure) {
1454
+ const upb_OneofDef* oneof = PyUpb_OneofDescriptor_GetDef(self);
1455
+ return PyLong_FromLong(upb_OneofDef_Index(oneof));
1456
+ }
1457
+
1458
+ static PyObject* PyUpb_OneofDescriptor_GetContainingType(PyObject* self,
1459
+ void* closure) {
1460
+ const upb_OneofDef* oneof = PyUpb_OneofDescriptor_GetDef(self);
1461
+ return PyUpb_Descriptor_Get(upb_OneofDef_ContainingType(oneof));
1462
+ }
1463
+
1464
+ static PyObject* PyUpb_OneofDescriptor_GetHasOptions(PyObject* _self,
1465
+ void* closure) {
1466
+ PyUpb_DescriptorBase* self = (void*)_self;
1467
+ return PyBool_FromLong(upb_OneofDef_HasOptions(self->def));
1468
+ }
1469
+
1470
+ static PyObject* PyUpb_OneofDescriptor_GetFields(PyObject* _self,
1471
+ void* closure) {
1472
+ PyUpb_DescriptorBase* self = (void*)_self;
1473
+ static PyUpb_GenericSequence_Funcs funcs = {
1474
+ (void*)&upb_OneofDef_FieldCount,
1475
+ (void*)&upb_OneofDef_Field,
1476
+ (void*)&PyUpb_FieldDescriptor_Get,
1477
+ };
1478
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
1479
+ }
1480
+
1481
+ static PyObject* PyUpb_OneofDescriptor_GetOptions(PyObject* _self,
1482
+ PyObject* args) {
1483
+ PyUpb_DescriptorBase* self = (void*)_self;
1484
+ return PyUpb_DescriptorBase_GetOptions(self, upb_OneofDef_Options(self->def),
1485
+ &google_protobuf_OneofOptions_msginit,
1486
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE
1487
+ ".OneofOptions");
1488
+ }
1489
+
1490
+ static PyGetSetDef PyUpb_OneofDescriptor_Getters[] = {
1491
+ {"name", PyUpb_OneofDescriptor_GetName, NULL, "Name"},
1492
+ {"full_name", PyUpb_OneofDescriptor_GetFullName, NULL, "Full name"},
1493
+ {"index", PyUpb_OneofDescriptor_GetIndex, NULL, "Index"},
1494
+ {"containing_type", PyUpb_OneofDescriptor_GetContainingType, NULL,
1495
+ "Containing type"},
1496
+ {"has_options", PyUpb_OneofDescriptor_GetHasOptions, NULL, "Has Options"},
1497
+ {"fields", PyUpb_OneofDescriptor_GetFields, NULL, "Fields"},
1498
+ {NULL}};
1499
+
1500
+ static PyMethodDef PyUpb_OneofDescriptor_Methods[] = {
1501
+ {"GetOptions", PyUpb_OneofDescriptor_GetOptions, METH_NOARGS}, {NULL}};
1502
+
1503
+ static PyType_Slot PyUpb_OneofDescriptor_Slots[] = {
1504
+ DESCRIPTOR_BASE_SLOTS,
1505
+ {Py_tp_methods, PyUpb_OneofDescriptor_Methods},
1506
+ {Py_tp_getset, PyUpb_OneofDescriptor_Getters},
1507
+ {0, NULL}};
1508
+
1509
+ static PyType_Spec PyUpb_OneofDescriptor_Spec = {
1510
+ PYUPB_MODULE_NAME ".OneofDescriptor", // tp_name
1511
+ sizeof(PyUpb_DescriptorBase), // tp_basicsize
1512
+ 0, // tp_itemsize
1513
+ Py_TPFLAGS_DEFAULT, // tp_flags
1514
+ PyUpb_OneofDescriptor_Slots,
1515
+ };
1516
+
1517
+ // -----------------------------------------------------------------------------
1518
+ // ServiceDescriptor
1519
+ // -----------------------------------------------------------------------------
1520
+
1521
+ const upb_ServiceDef* PyUpb_ServiceDescriptor_GetDef(PyObject* _self) {
1522
+ PyUpb_DescriptorBase* self =
1523
+ PyUpb_DescriptorBase_Check(_self, kPyUpb_ServiceDescriptor);
1524
+ return self ? self->def : NULL;
1525
+ }
1526
+
1527
+ PyObject* PyUpb_ServiceDescriptor_Get(const upb_ServiceDef* s) {
1528
+ const upb_FileDef* file = upb_ServiceDef_File(s);
1529
+ return PyUpb_DescriptorBase_Get(kPyUpb_ServiceDescriptor, s, file);
1530
+ }
1531
+
1532
+ static PyObject* PyUpb_ServiceDescriptor_GetFullName(PyObject* self,
1533
+ void* closure) {
1534
+ const upb_ServiceDef* s = PyUpb_ServiceDescriptor_GetDef(self);
1535
+ return PyUnicode_FromString(upb_ServiceDef_FullName(s));
1536
+ }
1537
+
1538
+ static PyObject* PyUpb_ServiceDescriptor_GetName(PyObject* self,
1539
+ void* closure) {
1540
+ const upb_ServiceDef* s = PyUpb_ServiceDescriptor_GetDef(self);
1541
+ return PyUnicode_FromString(upb_ServiceDef_Name(s));
1542
+ }
1543
+
1544
+ static PyObject* PyUpb_ServiceDescriptor_GetFile(PyObject* self,
1545
+ void* closure) {
1546
+ const upb_ServiceDef* s = PyUpb_ServiceDescriptor_GetDef(self);
1547
+ return PyUpb_FileDescriptor_Get(upb_ServiceDef_File(s));
1548
+ }
1549
+
1550
+ static PyObject* PyUpb_ServiceDescriptor_GetIndex(PyObject* self,
1551
+ void* closure) {
1552
+ const upb_ServiceDef* s = PyUpb_ServiceDescriptor_GetDef(self);
1553
+ return PyLong_FromLong(upb_ServiceDef_Index(s));
1554
+ }
1555
+
1556
+ static PyObject* PyUpb_ServiceDescriptor_GetMethods(PyObject* _self,
1557
+ void* closure) {
1558
+ PyUpb_DescriptorBase* self = (void*)_self;
1559
+ static PyUpb_GenericSequence_Funcs funcs = {
1560
+ (void*)&upb_ServiceDef_MethodCount,
1561
+ (void*)&upb_ServiceDef_Method,
1562
+ (void*)&PyUpb_MethodDescriptor_Get,
1563
+ };
1564
+ return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
1565
+ }
1566
+
1567
+ static PyObject* PyUpb_ServiceDescriptor_GetMethodsByName(PyObject* _self,
1568
+ void* closure) {
1569
+ static PyUpb_ByNameMap_Funcs funcs = {
1570
+ {
1571
+ (void*)&upb_ServiceDef_MethodCount,
1572
+ (void*)&upb_ServiceDef_Method,
1573
+ (void*)&PyUpb_MethodDescriptor_Get,
1574
+ },
1575
+ (void*)&upb_ServiceDef_FindMethodByName,
1576
+ (void*)&upb_MethodDef_Name,
1577
+ };
1578
+ PyUpb_DescriptorBase* self = (void*)_self;
1579
+ return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
1580
+ }
1581
+
1582
+ static PyObject* PyUpb_ServiceDescriptor_GetOptions(PyObject* _self,
1583
+ PyObject* args) {
1584
+ PyUpb_DescriptorBase* self = (void*)_self;
1585
+ return PyUpb_DescriptorBase_GetOptions(
1586
+ self, upb_ServiceDef_Options(self->def),
1587
+ &google_protobuf_ServiceOptions_msginit,
1588
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE ".ServiceOptions");
1589
+ }
1590
+
1591
+ static PyObject* PyUpb_ServiceDescriptor_CopyToProto(PyObject* _self,
1592
+ PyObject* py_proto) {
1593
+ return PyUpb_DescriptorBase_CopyToProto(
1594
+ _self, (PyUpb_ToProto_Func*)&upb_ServiceDef_ToProto,
1595
+ &google_protobuf_ServiceDescriptorProto_msginit,
1596
+ PYUPB_DESCRIPTOR_PROTO_PACKAGE ".ServiceDescriptorProto", py_proto);
1597
+ }
1598
+
1599
+ static PyObject* PyUpb_ServiceDescriptor_FindMethodByName(PyObject* _self,
1600
+ PyObject* py_name) {
1601
+ PyUpb_DescriptorBase* self = (void*)_self;
1602
+ const char* name = PyUnicode_AsUTF8AndSize(py_name, NULL);
1603
+ if (!name) return NULL;
1604
+ const upb_MethodDef* method =
1605
+ upb_ServiceDef_FindMethodByName(self->def, name);
1606
+ if (method == NULL) {
1607
+ return PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name);
1608
+ }
1609
+ return PyUpb_MethodDescriptor_Get(method);
1610
+ }
1611
+
1612
+ static PyGetSetDef PyUpb_ServiceDescriptor_Getters[] = {
1613
+ {"name", PyUpb_ServiceDescriptor_GetName, NULL, "Name", NULL},
1614
+ {"full_name", PyUpb_ServiceDescriptor_GetFullName, NULL, "Full name", NULL},
1615
+ {"file", PyUpb_ServiceDescriptor_GetFile, NULL, "File descriptor"},
1616
+ {"index", PyUpb_ServiceDescriptor_GetIndex, NULL, "Index", NULL},
1617
+ {"methods", PyUpb_ServiceDescriptor_GetMethods, NULL, "Methods", NULL},
1618
+ {"methods_by_name", PyUpb_ServiceDescriptor_GetMethodsByName, NULL,
1619
+ "Methods by name", NULL},
1620
+ {NULL}};
1621
+
1622
+ static PyMethodDef PyUpb_ServiceDescriptor_Methods[] = {
1623
+ {"GetOptions", PyUpb_ServiceDescriptor_GetOptions, METH_NOARGS},
1624
+ {"CopyToProto", PyUpb_ServiceDescriptor_CopyToProto, METH_O},
1625
+ {"FindMethodByName", PyUpb_ServiceDescriptor_FindMethodByName, METH_O},
1626
+ {NULL}};
1627
+
1628
+ static PyType_Slot PyUpb_ServiceDescriptor_Slots[] = {
1629
+ DESCRIPTOR_BASE_SLOTS,
1630
+ {Py_tp_methods, PyUpb_ServiceDescriptor_Methods},
1631
+ {Py_tp_getset, PyUpb_ServiceDescriptor_Getters},
1632
+ {0, NULL}};
1633
+
1634
+ static PyType_Spec PyUpb_ServiceDescriptor_Spec = {
1635
+ PYUPB_MODULE_NAME ".ServiceDescriptor", // tp_name
1636
+ sizeof(PyUpb_DescriptorBase), // tp_basicsize
1637
+ 0, // tp_itemsize
1638
+ Py_TPFLAGS_DEFAULT, // tp_flags
1639
+ PyUpb_ServiceDescriptor_Slots,
1640
+ };
1641
+
1642
+ // -----------------------------------------------------------------------------
1643
+ // Top Level
1644
+ // -----------------------------------------------------------------------------
1645
+
1646
+ static bool PyUpb_SetIntAttr(PyObject* obj, const char* name, int val) {
1647
+ PyObject* num = PyLong_FromLong(val);
1648
+ if (!num) return false;
1649
+ int status = PyObject_SetAttrString(obj, name, num);
1650
+ Py_DECREF(num);
1651
+ return status >= 0;
1652
+ }
1653
+
1654
+ // These must be in the same order as PyUpb_DescriptorType in the header.
1655
+ static PyType_Spec* desc_specs[] = {
1656
+ &PyUpb_Descriptor_Spec, &PyUpb_EnumDescriptor_Spec,
1657
+ &PyUpb_EnumValueDescriptor_Spec, &PyUpb_FieldDescriptor_Spec,
1658
+ &PyUpb_FileDescriptor_Spec, &PyUpb_MethodDescriptor_Spec,
1659
+ &PyUpb_OneofDescriptor_Spec, &PyUpb_ServiceDescriptor_Spec,
1660
+ };
1661
+
1662
+ bool PyUpb_InitDescriptor(PyObject* m) {
1663
+ PyUpb_ModuleState* s = PyUpb_ModuleState_GetFromModule(m);
1664
+
1665
+ for (size_t i = 0; i < kPyUpb_Descriptor_Count; i++) {
1666
+ s->descriptor_types[i] = PyUpb_AddClass(m, desc_specs[i]);
1667
+ if (!s->descriptor_types[i]) {
1668
+ return false;
1669
+ }
1670
+ }
1671
+
1672
+ PyObject* fd = (PyObject*)s->descriptor_types[kPyUpb_FieldDescriptor];
1673
+ return PyUpb_SetIntAttr(fd, "LABEL_OPTIONAL", kUpb_Label_Optional) &&
1674
+ PyUpb_SetIntAttr(fd, "LABEL_REPEATED", kUpb_Label_Repeated) &&
1675
+ PyUpb_SetIntAttr(fd, "LABEL_REQUIRED", kUpb_Label_Required) &&
1676
+ PyUpb_SetIntAttr(fd, "TYPE_BOOL", kUpb_FieldType_Bool) &&
1677
+ PyUpb_SetIntAttr(fd, "TYPE_BYTES", kUpb_FieldType_Bytes) &&
1678
+ PyUpb_SetIntAttr(fd, "TYPE_DOUBLE", kUpb_FieldType_Double) &&
1679
+ PyUpb_SetIntAttr(fd, "TYPE_ENUM", kUpb_FieldType_Enum) &&
1680
+ PyUpb_SetIntAttr(fd, "TYPE_FIXED32", kUpb_FieldType_Fixed32) &&
1681
+ PyUpb_SetIntAttr(fd, "TYPE_FIXED64", kUpb_FieldType_Fixed64) &&
1682
+ PyUpb_SetIntAttr(fd, "TYPE_FLOAT", kUpb_FieldType_Float) &&
1683
+ PyUpb_SetIntAttr(fd, "TYPE_GROUP", kUpb_FieldType_Group) &&
1684
+ PyUpb_SetIntAttr(fd, "TYPE_INT32", kUpb_FieldType_Int32) &&
1685
+ PyUpb_SetIntAttr(fd, "TYPE_INT64", kUpb_FieldType_Int64) &&
1686
+ PyUpb_SetIntAttr(fd, "TYPE_MESSAGE", kUpb_FieldType_Message) &&
1687
+ PyUpb_SetIntAttr(fd, "TYPE_SFIXED32", kUpb_FieldType_SFixed32) &&
1688
+ PyUpb_SetIntAttr(fd, "TYPE_SFIXED64", kUpb_FieldType_SFixed64) &&
1689
+ PyUpb_SetIntAttr(fd, "TYPE_SINT32", kUpb_FieldType_SInt32) &&
1690
+ PyUpb_SetIntAttr(fd, "TYPE_SINT64", kUpb_FieldType_SInt64) &&
1691
+ PyUpb_SetIntAttr(fd, "TYPE_STRING", kUpb_FieldType_String) &&
1692
+ PyUpb_SetIntAttr(fd, "TYPE_UINT32", kUpb_FieldType_UInt32) &&
1693
+ PyUpb_SetIntAttr(fd, "TYPE_UINT64", kUpb_FieldType_UInt64);
1694
+ }