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,350 @@
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/protobuf.h"
29
+
30
+ #include "python/descriptor.h"
31
+ #include "python/descriptor_containers.h"
32
+ #include "python/descriptor_pool.h"
33
+ #include "python/extension_dict.h"
34
+ #include "python/map.h"
35
+ #include "python/message.h"
36
+ #include "python/repeated.h"
37
+ #include "python/unknown_fields.h"
38
+
39
+ static void PyUpb_ModuleDealloc(void* module) {
40
+ PyUpb_ModuleState* s = PyModule_GetState(module);
41
+ PyUpb_WeakMap_Free(s->obj_cache);
42
+ if (s->c_descriptor_symtab) {
43
+ upb_DefPool_Free(s->c_descriptor_symtab);
44
+ }
45
+ }
46
+
47
+ PyObject* PyUpb_SetAllowOversizeProtos(PyObject* m, PyObject* arg) {
48
+ if (!arg || !PyBool_Check(arg)) {
49
+ PyErr_SetString(PyExc_TypeError,
50
+ "Argument to SetAllowOversizeProtos must be boolean");
51
+ return NULL;
52
+ }
53
+ PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
54
+ state->allow_oversize_protos = PyObject_IsTrue(arg);
55
+ Py_INCREF(arg);
56
+ return arg;
57
+ }
58
+
59
+ static PyMethodDef PyUpb_ModuleMethods[] = {
60
+ {"SetAllowOversizeProtos", PyUpb_SetAllowOversizeProtos, METH_O,
61
+ "Enable/disable oversize proto parsing."},
62
+ {NULL, NULL}};
63
+
64
+ static struct PyModuleDef module_def = {PyModuleDef_HEAD_INIT,
65
+ PYUPB_MODULE_NAME,
66
+ "Protobuf Module",
67
+ sizeof(PyUpb_ModuleState),
68
+ PyUpb_ModuleMethods, // m_methods
69
+ NULL, // m_slots
70
+ NULL, // m_traverse
71
+ NULL, // m_clear
72
+ PyUpb_ModuleDealloc};
73
+
74
+ // -----------------------------------------------------------------------------
75
+ // ModuleState
76
+ // -----------------------------------------------------------------------------
77
+
78
+ PyUpb_ModuleState* PyUpb_ModuleState_MaybeGet(void) {
79
+ PyObject* module = PyState_FindModule(&module_def);
80
+ return module ? PyModule_GetState(module) : NULL;
81
+ }
82
+
83
+ PyUpb_ModuleState* PyUpb_ModuleState_GetFromModule(PyObject* module) {
84
+ PyUpb_ModuleState* state = PyModule_GetState(module);
85
+ assert(state);
86
+ assert(PyModule_GetDef(module) == &module_def);
87
+ return state;
88
+ }
89
+
90
+ PyUpb_ModuleState* PyUpb_ModuleState_Get(void) {
91
+ PyObject* module = PyState_FindModule(&module_def);
92
+ assert(module);
93
+ return PyUpb_ModuleState_GetFromModule(module);
94
+ }
95
+
96
+ PyObject* PyUpb_GetWktBases(PyUpb_ModuleState* state) {
97
+ if (!state->wkt_bases) {
98
+ PyObject* wkt_module = PyImport_ImportModule(PYUPB_PROTOBUF_INTERNAL_PACKAGE
99
+ ".well_known_types");
100
+
101
+ if (wkt_module == NULL) {
102
+ return false;
103
+ }
104
+
105
+ state->wkt_bases = PyObject_GetAttrString(wkt_module, "WKTBASES");
106
+ PyObject* m = PyState_FindModule(&module_def);
107
+ // Reparent ownership to m.
108
+ PyModule_AddObject(m, "__internal_wktbases", state->wkt_bases);
109
+ Py_DECREF(wkt_module);
110
+ }
111
+
112
+ return state->wkt_bases;
113
+ }
114
+
115
+ // -----------------------------------------------------------------------------
116
+ // WeakMap
117
+ // -----------------------------------------------------------------------------
118
+
119
+ struct PyUpb_WeakMap {
120
+ upb_inttable table;
121
+ upb_Arena* arena;
122
+ };
123
+
124
+ PyUpb_WeakMap* PyUpb_WeakMap_New(void) {
125
+ upb_Arena* arena = upb_Arena_New();
126
+ PyUpb_WeakMap* map = upb_Arena_Malloc(arena, sizeof(*map));
127
+ map->arena = arena;
128
+ upb_inttable_init(&map->table, map->arena);
129
+ return map;
130
+ }
131
+
132
+ void PyUpb_WeakMap_Free(PyUpb_WeakMap* map) { upb_Arena_Free(map->arena); }
133
+
134
+ uintptr_t PyUpb_WeakMap_GetKey(const void* key) {
135
+ uintptr_t n = (uintptr_t)key;
136
+ assert((n & 7) == 0);
137
+ return n >> 3;
138
+ }
139
+
140
+ void PyUpb_WeakMap_Add(PyUpb_WeakMap* map, const void* key, PyObject* py_obj) {
141
+ upb_inttable_insert(&map->table, PyUpb_WeakMap_GetKey(key),
142
+ upb_value_ptr(py_obj), map->arena);
143
+ }
144
+
145
+ void PyUpb_WeakMap_Delete(PyUpb_WeakMap* map, const void* key) {
146
+ upb_value val;
147
+ bool removed =
148
+ upb_inttable_remove(&map->table, PyUpb_WeakMap_GetKey(key), &val);
149
+ (void)removed;
150
+ assert(removed);
151
+ }
152
+
153
+ void PyUpb_WeakMap_TryDelete(PyUpb_WeakMap* map, const void* key) {
154
+ upb_inttable_remove(&map->table, PyUpb_WeakMap_GetKey(key), NULL);
155
+ }
156
+
157
+ PyObject* PyUpb_WeakMap_Get(PyUpb_WeakMap* map, const void* key) {
158
+ upb_value val;
159
+ if (upb_inttable_lookup(&map->table, PyUpb_WeakMap_GetKey(key), &val)) {
160
+ PyObject* ret = upb_value_getptr(val);
161
+ Py_INCREF(ret);
162
+ return ret;
163
+ } else {
164
+ return NULL;
165
+ }
166
+ }
167
+
168
+ bool PyUpb_WeakMap_Next(PyUpb_WeakMap* map, const void** key, PyObject** obj,
169
+ intptr_t* iter) {
170
+ uintptr_t u_key;
171
+ upb_value val;
172
+ if (!upb_inttable_next2(&map->table, &u_key, &val, iter)) return false;
173
+ *key = (void*)(u_key << 3);
174
+ *obj = upb_value_getptr(val);
175
+ return true;
176
+ }
177
+
178
+ void PyUpb_WeakMap_DeleteIter(PyUpb_WeakMap* map, intptr_t* iter) {
179
+ upb_inttable_removeiter(&map->table, iter);
180
+ }
181
+
182
+ // -----------------------------------------------------------------------------
183
+ // ObjCache
184
+ // -----------------------------------------------------------------------------
185
+
186
+ PyUpb_WeakMap* PyUpb_ObjCache_Instance(void) {
187
+ PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
188
+ return state->obj_cache;
189
+ }
190
+
191
+ void PyUpb_ObjCache_Add(const void* key, PyObject* py_obj) {
192
+ PyUpb_WeakMap_Add(PyUpb_ObjCache_Instance(), key, py_obj);
193
+ }
194
+
195
+ void PyUpb_ObjCache_Delete(const void* key) {
196
+ PyUpb_ModuleState* state = PyUpb_ModuleState_MaybeGet();
197
+ if (!state) {
198
+ // During the shutdown sequence, our object's Dealloc() methods can be
199
+ // called *after* our module Dealloc() method has been called. At that
200
+ // point our state will be NULL and there is nothing to delete out of the
201
+ // map.
202
+ return;
203
+ }
204
+ PyUpb_WeakMap_Delete(state->obj_cache, key);
205
+ }
206
+
207
+ PyObject* PyUpb_ObjCache_Get(const void* key) {
208
+ return PyUpb_WeakMap_Get(PyUpb_ObjCache_Instance(), key);
209
+ }
210
+
211
+ // -----------------------------------------------------------------------------
212
+ // Arena
213
+ // -----------------------------------------------------------------------------
214
+
215
+ typedef struct {
216
+ PyObject_HEAD;
217
+ upb_Arena* arena;
218
+ } PyUpb_Arena;
219
+
220
+ PyObject* PyUpb_Arena_New(void) {
221
+ PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
222
+ PyUpb_Arena* arena = (void*)PyType_GenericAlloc(state->arena_type, 0);
223
+ arena->arena = upb_Arena_New();
224
+ return &arena->ob_base;
225
+ }
226
+
227
+ static void PyUpb_Arena_Dealloc(PyObject* self) {
228
+ upb_Arena_Free(PyUpb_Arena_Get(self));
229
+ PyUpb_Dealloc(self);
230
+ }
231
+
232
+ upb_Arena* PyUpb_Arena_Get(PyObject* arena) {
233
+ return ((PyUpb_Arena*)arena)->arena;
234
+ }
235
+
236
+ static PyType_Slot PyUpb_Arena_Slots[] = {
237
+ {Py_tp_dealloc, PyUpb_Arena_Dealloc},
238
+ {0, NULL},
239
+ };
240
+
241
+ static PyType_Spec PyUpb_Arena_Spec = {
242
+ PYUPB_MODULE_NAME ".Arena",
243
+ sizeof(PyUpb_Arena),
244
+ 0, // itemsize
245
+ Py_TPFLAGS_DEFAULT,
246
+ PyUpb_Arena_Slots,
247
+ };
248
+
249
+ static bool PyUpb_InitArena(PyObject* m) {
250
+ PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m);
251
+ state->arena_type = PyUpb_AddClass(m, &PyUpb_Arena_Spec);
252
+ return state->arena_type;
253
+ }
254
+
255
+ // -----------------------------------------------------------------------------
256
+ // Utilities
257
+ // -----------------------------------------------------------------------------
258
+
259
+ PyTypeObject* AddObject(PyObject* m, const char* name, PyType_Spec* spec) {
260
+ PyObject* type = PyType_FromSpec(spec);
261
+ return type && PyModule_AddObject(m, name, type) == 0 ? (PyTypeObject*)type
262
+ : NULL;
263
+ }
264
+
265
+ static const char* PyUpb_GetClassName(PyType_Spec* spec) {
266
+ // spec->name contains a fully-qualified name, like:
267
+ // google.protobuf.pyext._message.FooBar
268
+ //
269
+ // Find the rightmost '.' to get "FooBar".
270
+ const char* name = strrchr(spec->name, '.');
271
+ assert(name);
272
+ return name + 1;
273
+ }
274
+
275
+ PyTypeObject* PyUpb_AddClass(PyObject* m, PyType_Spec* spec) {
276
+ PyObject* type = PyType_FromSpec(spec);
277
+ const char* name = PyUpb_GetClassName(spec);
278
+ if (PyModule_AddObject(m, name, type) < 0) {
279
+ Py_XDECREF(type);
280
+ return NULL;
281
+ }
282
+ return (PyTypeObject*)type;
283
+ }
284
+
285
+ PyTypeObject* PyUpb_AddClassWithBases(PyObject* m, PyType_Spec* spec,
286
+ PyObject* bases) {
287
+ PyObject* type = PyType_FromSpecWithBases(spec, bases);
288
+ const char* name = PyUpb_GetClassName(spec);
289
+ if (PyModule_AddObject(m, name, type) < 0) {
290
+ Py_XDECREF(type);
291
+ return NULL;
292
+ }
293
+ return (PyTypeObject*)type;
294
+ }
295
+
296
+ const char* PyUpb_GetStrData(PyObject* obj) {
297
+ if (PyUnicode_Check(obj)) {
298
+ return PyUnicode_AsUTF8AndSize(obj, NULL);
299
+ } else if (PyBytes_Check(obj)) {
300
+ return PyBytes_AsString(obj);
301
+ } else {
302
+ return NULL;
303
+ }
304
+ }
305
+
306
+ const char* PyUpb_VerifyStrData(PyObject* obj) {
307
+ const char* ret = PyUpb_GetStrData(obj);
308
+ if (ret) return ret;
309
+ PyErr_Format(PyExc_TypeError, "Expected string: %S", obj);
310
+ return NULL;
311
+ }
312
+
313
+ PyObject* PyUpb_Forbidden_New(PyObject* cls, PyObject* args, PyObject* kwds) {
314
+ PyObject* name = PyObject_GetAttrString(cls, "__name__");
315
+ PyErr_Format(PyExc_RuntimeError,
316
+ "Objects of type %U may not be created directly.", name);
317
+ Py_XDECREF(name);
318
+ return NULL;
319
+ }
320
+
321
+ // -----------------------------------------------------------------------------
322
+ // Module Entry Point
323
+ // -----------------------------------------------------------------------------
324
+
325
+ __attribute__((visibility("default"))) PyMODINIT_FUNC PyInit__message(void) {
326
+ PyObject* m = PyModule_Create(&module_def);
327
+ if (!m) return NULL;
328
+
329
+ PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m);
330
+
331
+ state->allow_oversize_protos = false;
332
+ state->wkt_bases = NULL;
333
+ state->obj_cache = PyUpb_WeakMap_New();
334
+ state->c_descriptor_symtab = NULL;
335
+
336
+ if (!PyUpb_InitDescriptorContainers(m) || !PyUpb_InitDescriptorPool(m) ||
337
+ !PyUpb_InitDescriptor(m) || !PyUpb_InitArena(m) ||
338
+ !PyUpb_InitExtensionDict(m) || !PyUpb_Map_Init(m) ||
339
+ !PyUpb_InitMessage(m) || !PyUpb_Repeated_Init(m) ||
340
+ !PyUpb_UnknownFields_Init(m)) {
341
+ Py_DECREF(m);
342
+ return NULL;
343
+ }
344
+
345
+ // Temporary: an cookie we can use in the tests to ensure we are testing upb
346
+ // and not another protobuf library on the system.
347
+ PyModule_AddIntConstant(m, "_IS_UPB", 1);
348
+
349
+ return m;
350
+ }
@@ -0,0 +1,230 @@
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_PROTOBUF_H__
29
+ #define PYUPB_PROTOBUF_H__
30
+
31
+ #include <stdbool.h>
32
+
33
+ #include "python/descriptor.h"
34
+ #include "python/python_api.h"
35
+ #include "upb/table_internal.h"
36
+
37
+ // begin:github_only
38
+ #define PYUPB_PROTOBUF_PUBLIC_PACKAGE "google.protobuf"
39
+ #define PYUPB_PROTOBUF_INTERNAL_PACKAGE "google.protobuf.internal"
40
+ #define PYUPB_DESCRIPTOR_PROTO_PACKAGE "google.protobuf"
41
+ #define PYUPB_DESCRIPTOR_MODULE "google.protobuf.descriptor_pb2"
42
+ #define PYUPB_MODULE_NAME "google._upb._message"
43
+ // end:github_only
44
+
45
+ // begin:google_only
46
+ // #define PYUPB_PROTOBUF_PUBLIC_PACKAGE "google3.net.proto2.python.public"
47
+ // #define PYUPB_PROTOBUF_INTERNAL_PACKAGE "google3.net.proto2.python.internal"
48
+ // #define PYUPB_DESCRIPTOR_PROTO_PACKAGE "proto2"
49
+ // #define PYUPB_DESCRIPTOR_MODULE "google3.net.proto2.proto.descriptor_pb2"
50
+ // #define PYUPB_MODULE_NAME "google3.third_party.upb.python._message"
51
+ // end:google_only
52
+
53
+ #define PYUPB_RETURN_OOM return PyErr_SetNone(PyExc_MemoryError), NULL
54
+
55
+ struct PyUpb_WeakMap;
56
+ typedef struct PyUpb_WeakMap PyUpb_WeakMap;
57
+
58
+ // -----------------------------------------------------------------------------
59
+ // ModuleState
60
+ // -----------------------------------------------------------------------------
61
+
62
+ // We store all "global" state in this struct instead of using (C) global
63
+ // variables. This makes this extension compatible with sub-interpreters.
64
+
65
+ typedef struct {
66
+ // From descriptor.c
67
+ PyTypeObject* descriptor_types[kPyUpb_Descriptor_Count];
68
+
69
+ // From descriptor_containers.c
70
+ PyTypeObject* by_name_map_type;
71
+ PyTypeObject* by_number_map_type;
72
+ PyTypeObject* descriptor_iterator_type;
73
+ PyTypeObject* generic_sequence_type;
74
+
75
+ // From descriptor_pool.c
76
+ PyObject* default_pool;
77
+
78
+ // From descriptor_pool.c
79
+ PyTypeObject* descriptor_pool_type;
80
+ upb_DefPool* c_descriptor_symtab;
81
+
82
+ // From extension_dict.c
83
+ PyTypeObject* extension_dict_type;
84
+ PyTypeObject* extension_iterator_type;
85
+
86
+ // From map.c
87
+ PyTypeObject* map_iterator_type;
88
+ PyTypeObject* message_map_container_type;
89
+ PyTypeObject* scalar_map_container_type;
90
+
91
+ // From message.c
92
+ PyObject* decode_error_class;
93
+ PyObject* descriptor_string;
94
+ PyObject* encode_error_class;
95
+ PyObject* enum_type_wrapper_class;
96
+ PyObject* message_class;
97
+ PyTypeObject* cmessage_type;
98
+ PyTypeObject* message_meta_type;
99
+ PyObject* listfields_item_key;
100
+
101
+ // From protobuf.c
102
+ bool allow_oversize_protos;
103
+ PyObject* wkt_bases;
104
+ PyTypeObject* arena_type;
105
+ PyUpb_WeakMap* obj_cache;
106
+
107
+ // From repeated.c
108
+ PyTypeObject* repeated_composite_container_type;
109
+ PyTypeObject* repeated_scalar_container_type;
110
+
111
+ // From unknown_fields.c
112
+ PyTypeObject* unknown_fields_type;
113
+ PyObject* unknown_field_type;
114
+ } PyUpb_ModuleState;
115
+
116
+ // Returns the global state object from the current interpreter. The current
117
+ // interpreter is looked up from thread-local state.
118
+ PyUpb_ModuleState* PyUpb_ModuleState_Get(void);
119
+ PyUpb_ModuleState* PyUpb_ModuleState_GetFromModule(PyObject* module);
120
+
121
+ // Returns NULL if module state is not yet available (during startup).
122
+ // Any use of the module state during startup needs to be passed explicitly.
123
+ PyUpb_ModuleState* PyUpb_ModuleState_MaybeGet(void);
124
+
125
+ // Returns:
126
+ // from google.protobuf.internal.well_known_types import WKTBASES
127
+ //
128
+ // This has to be imported lazily rather than at module load time, because
129
+ // otherwise it would cause a circular import.
130
+ PyObject* PyUpb_GetWktBases(PyUpb_ModuleState* state);
131
+
132
+ // -----------------------------------------------------------------------------
133
+ // WeakMap
134
+ // -----------------------------------------------------------------------------
135
+
136
+ // A WeakMap maps C pointers to the corresponding Python wrapper object. We
137
+ // want a consistent Python wrapper object for each C object, both to save
138
+ // memory and to provide object stability (ie. x is x).
139
+ //
140
+ // Each wrapped object should add itself to the map when it is constructed and
141
+ // remove itself from the map when it is destroyed. The map is weak so it does
142
+ // not take references to the cached objects.
143
+
144
+ PyUpb_WeakMap* PyUpb_WeakMap_New(void);
145
+ void PyUpb_WeakMap_Free(PyUpb_WeakMap* map);
146
+
147
+ // Adds the given object to the map, indexed by the given key.
148
+ void PyUpb_WeakMap_Add(PyUpb_WeakMap* map, const void* key, PyObject* py_obj);
149
+
150
+ // Removes the given key from the cache. It must exist in the cache currently.
151
+ void PyUpb_WeakMap_Delete(PyUpb_WeakMap* map, const void* key);
152
+ void PyUpb_WeakMap_TryDelete(PyUpb_WeakMap* map, const void* key);
153
+
154
+ // Returns a new reference to an object if it exists, otherwise returns NULL.
155
+ PyObject* PyUpb_WeakMap_Get(PyUpb_WeakMap* map, const void* key);
156
+
157
+ #define PYUPB_WEAKMAP_BEGIN UPB_INTTABLE_BEGIN
158
+
159
+ // Iteration over the weak map, eg.
160
+ //
161
+ // intptr_t it = PYUPB_WEAKMAP_BEGIN;
162
+ // while (PyUpb_WeakMap_Next(map, &key, &obj, &it)) {
163
+ // // ...
164
+ // }
165
+ //
166
+ // Note that the callee does not own a ref on the returned `obj`.
167
+ bool PyUpb_WeakMap_Next(PyUpb_WeakMap* map, const void** key, PyObject** obj,
168
+ intptr_t* iter);
169
+ void PyUpb_WeakMap_DeleteIter(PyUpb_WeakMap* map, intptr_t* iter);
170
+
171
+ // -----------------------------------------------------------------------------
172
+ // ObjCache
173
+ // -----------------------------------------------------------------------------
174
+
175
+ // The object cache is a global WeakMap for mapping upb objects to the
176
+ // corresponding wrapper.
177
+ void PyUpb_ObjCache_Add(const void* key, PyObject* py_obj);
178
+ void PyUpb_ObjCache_Delete(const void* key);
179
+ PyObject* PyUpb_ObjCache_Get(const void* key); // returns NULL if not present.
180
+ PyUpb_WeakMap* PyUpb_ObjCache_Instance(void);
181
+
182
+ // -----------------------------------------------------------------------------
183
+ // Arena
184
+ // -----------------------------------------------------------------------------
185
+
186
+ PyObject* PyUpb_Arena_New(void);
187
+ upb_Arena* PyUpb_Arena_Get(PyObject* arena);
188
+
189
+ // -----------------------------------------------------------------------------
190
+ // Utilities
191
+ // -----------------------------------------------------------------------------
192
+
193
+ PyTypeObject* AddObject(PyObject* m, const char* name, PyType_Spec* spec);
194
+
195
+ // Creates a Python type from `spec` and adds it to the given module `m`.
196
+ PyTypeObject* PyUpb_AddClass(PyObject* m, PyType_Spec* spec);
197
+
198
+ // Like PyUpb_AddClass(), but allows you to specify a tuple of base classes
199
+ // in `bases`.
200
+ PyTypeObject* PyUpb_AddClassWithBases(PyObject* m, PyType_Spec* spec,
201
+ PyObject* bases);
202
+
203
+ // A function that implements the tp_new slot for types that we do not allow
204
+ // users to create directly. This will immediately fail with an error message.
205
+ PyObject* PyUpb_Forbidden_New(PyObject* cls, PyObject* args, PyObject* kwds);
206
+
207
+ // Our standard dealloc func. It follows the guidance defined in:
208
+ // https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
209
+ // However it tests Py_TPFLAGS_HEAPTYPE dynamically so that a single dealloc
210
+ // function can work for any type.
211
+ static inline void PyUpb_Dealloc(void* self) {
212
+ PyTypeObject* tp = Py_TYPE(self);
213
+ assert(PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE);
214
+ freefunc tp_free = (freefunc)PyType_GetSlot(tp, Py_tp_free);
215
+ tp_free(self);
216
+ Py_DECREF(tp);
217
+ }
218
+
219
+ // Equivalent to the Py_NewRef() function introduced in Python 3.10. If/when we
220
+ // drop support for Python <3.10, we can remove this function and replace all
221
+ // callers with Py_NewRef().
222
+ static inline PyObject* PyUpb_NewRef(PyObject* obj) {
223
+ Py_INCREF(obj);
224
+ return obj;
225
+ }
226
+
227
+ const char* PyUpb_GetStrData(PyObject* obj);
228
+ const char* PyUpb_VerifyStrData(PyObject* obj);
229
+
230
+ #endif // PYUPB_PROTOBUF_H__
@@ -0,0 +1,55 @@
1
+ """Macro to support py_extension """
2
+
3
+ load("@bazel_skylib//lib:selects.bzl", "selects")
4
+
5
+ def py_extension(name, srcs, copts, deps = []):
6
+ """Creates a C++ library to extend python
7
+
8
+ Args:
9
+ name: Name of the target
10
+ srcs: List of source files to create the target
11
+ copts: List of C++ compile options to use
12
+ deps: Libraries that the target depends on
13
+ """
14
+
15
+ native.cc_binary(
16
+ name = name + "_binary",
17
+ srcs = srcs,
18
+ copts = copts + ["-fvisibility=hidden"],
19
+ linkopts = selects.with_or({
20
+ ("//python/dist:osx-x86_64_cpu", "//python/dist:osx-aarch64_cpu"): ["-undefined", "dynamic_lookup"],
21
+ "//conditions:default": [],
22
+ }),
23
+ linkshared = True,
24
+ linkstatic = True,
25
+ deps = deps + select({
26
+ "//python:limited_api_3.7": ["@python-3.7.0//:python_headers"],
27
+ "//python:full_api_3.7_win32": ["@nuget_python_i686_3.7.0//:python_full_api"],
28
+ "//python:full_api_3.7_win64": ["@nuget_python_x86-64_3.7.0//:python_full_api"],
29
+ "//python:full_api_3.8_win32": ["@nuget_python_i686_3.8.0//:python_full_api"],
30
+ "//python:full_api_3.8_win64": ["@nuget_python_x86-64_3.8.0//:python_full_api"],
31
+ "//python:full_api_3.9_win32": ["@nuget_python_i686_3.9.0//:python_full_api"],
32
+ "//python:full_api_3.9_win64": ["@nuget_python_x86-64_3.9.0//:python_full_api"],
33
+ "//python:limited_api_3.10_win32": ["@nuget_python_i686_3.10.0//:python_limited_api"],
34
+ "//python:limited_api_3.10_win64": ["@nuget_python_x86-64_3.10.0//:python_limited_api"],
35
+ "//conditions:default": ["@system_python//:python_headers"],
36
+ }),
37
+ )
38
+
39
+ EXT_SUFFIX = ".abi3.so"
40
+ output_file = "google/_upb/" + name + EXT_SUFFIX
41
+
42
+ native.genrule(
43
+ name = "copy" + name,
44
+ srcs = [":" + name + "_binary"],
45
+ outs = [output_file],
46
+ cmd = "cp $< $@",
47
+ visibility = ["//python:__subpackages__"],
48
+ )
49
+
50
+ native.py_library(
51
+ name = name,
52
+ data = [output_file],
53
+ imports = ["."],
54
+ visibility = ["//python:__subpackages__"],
55
+ )
@@ -0,0 +1,61 @@
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_PYTHON_H__
29
+ #define PYUPB_PYTHON_H__
30
+
31
+ // We restrict ourselves to the limited API, so that a single build can be
32
+ // ABI-compatible with a wide range of Python versions.
33
+ //
34
+ // The build system will define Py_LIMITED_API as appropriate (see BUILD). We
35
+ // only want to define it for our distribution packages, since we can do some
36
+ // extra assertions when Py_LIMITED_API is not defined. Also Py_LIMITED_API is
37
+ // incompatible with Py_DEBUG.
38
+
39
+ // #define Py_LIMITED_API <val> // Defined by build system when appropriate.
40
+
41
+ #include "Python.h"
42
+
43
+ // Ideally we could restrict ourselves to the limited API of 3.7, but this is
44
+ // a very important function that was not officially added to the limited API
45
+ // until 3.10. Without this function, there is no way of getting data from a
46
+ // Python `str` object without a copy.
47
+ //
48
+ // While this function was not *officially* added to the limited API until
49
+ // Python 3.10, In practice it has been stable since Python 3.1.
50
+ // https://bugs.python.org/issue41784
51
+ //
52
+ // On Linux/ELF and macOS/Mach-O, we can get away with using this function with
53
+ // the limited API prior to 3.10.
54
+
55
+ #if (defined(__linux__) || defined(__APPLE__)) && defined(Py_LIMITED_API) && \
56
+ Py_LIMITED_API < 0x03100000
57
+ PyAPI_FUNC(const char*)
58
+ PyUnicode_AsUTF8AndSize(PyObject* unicode, Py_ssize_t* size);
59
+ #endif
60
+
61
+ #endif // PYUPB_PYTHON_H__