protobuf.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #ifndef PYUPB_PROTOBUF_H__
  28. #define PYUPB_PROTOBUF_H__
  29. #include <stdbool.h>
  30. #include "python/descriptor.h"
  31. #include "python/python.h"
  32. #include "upb/table_internal.h"
  33. #define PYUPB_MODULE_NAME "google.protobuf.pyext._message"
  34. #define PYUPB_RETURN_OOM return PyErr_SetNone(PyExc_MemoryError), NULL
  35. struct PyUpb_WeakMap;
  36. typedef struct PyUpb_WeakMap PyUpb_WeakMap;
  37. // -----------------------------------------------------------------------------
  38. // ModuleState
  39. // -----------------------------------------------------------------------------
  40. // We store all "global" state in this struct instead of using (C) global
  41. // variables. This makes this extension compatible with sub-interpreters.
  42. typedef struct {
  43. // From descriptor.c
  44. PyTypeObject* descriptor_types[kPyUpb_Descriptor_Count];
  45. // From descriptor_containers.c
  46. PyTypeObject* by_name_map_type;
  47. PyTypeObject* by_number_map_type;
  48. PyTypeObject* descriptor_iterator_type;
  49. PyTypeObject* generic_sequence_type;
  50. // From descriptor_pool.c
  51. PyObject* default_pool;
  52. // From descriptor_pool.c
  53. PyTypeObject* descriptor_pool_type;
  54. upb_DefPool* c_descriptor_symtab;
  55. // From extension_dict.c
  56. PyTypeObject* extension_dict_type;
  57. PyTypeObject* extension_iterator_type;
  58. // From map.c
  59. PyTypeObject* map_iterator_type;
  60. PyTypeObject* message_map_container_type;
  61. PyTypeObject* scalar_map_container_type;
  62. // From message.c
  63. PyObject* decode_error_class;
  64. PyObject* descriptor_string;
  65. PyObject* encode_error_class;
  66. PyObject* enum_type_wrapper_class;
  67. PyObject* message_class;
  68. PyTypeObject* cmessage_type;
  69. PyTypeObject* message_meta_type;
  70. PyObject* listfields_item_key;
  71. // From protobuf.c
  72. bool allow_oversize_protos;
  73. PyObject* wkt_bases;
  74. PyTypeObject* arena_type;
  75. PyUpb_WeakMap* obj_cache;
  76. // From repeated.c
  77. PyTypeObject* repeated_composite_container_type;
  78. PyTypeObject* repeated_scalar_container_type;
  79. } PyUpb_ModuleState;
  80. // Returns the global state object from the current interpreter. The current
  81. // interpreter is looked up from thread-local state.
  82. PyUpb_ModuleState* PyUpb_ModuleState_Get(void);
  83. PyUpb_ModuleState* PyUpb_ModuleState_GetFromModule(PyObject* module);
  84. // Returns NULL if module state is not yet available (during startup).
  85. // Any use of the module state during startup needs to be passed explicitly.
  86. PyUpb_ModuleState* PyUpb_ModuleState_MaybeGet(void);
  87. // Returns:
  88. // from google.protobuf.internal.well_known_types import WKTBASES
  89. //
  90. // This has to be imported lazily rather than at module load time, because
  91. // otherwise it would cause a circular import.
  92. PyObject* PyUpb_GetWktBases(PyUpb_ModuleState* state);
  93. // -----------------------------------------------------------------------------
  94. // WeakMap
  95. // -----------------------------------------------------------------------------
  96. // A WeakMap maps C pointers to the corresponding Python wrapper object. We
  97. // want a consistent Python wrapper object for each C object, both to save
  98. // memory and to provide object stability (ie. x is x).
  99. //
  100. // Each wrapped object should add itself to the map when it is constructed and
  101. // remove itself from the map when it is destroyed. The map is weak so it does
  102. // not take references to the cached objects.
  103. PyUpb_WeakMap* PyUpb_WeakMap_New(void);
  104. void PyUpb_WeakMap_Free(PyUpb_WeakMap* map);
  105. // Adds the given object to the map, indexed by the given key.
  106. void PyUpb_WeakMap_Add(PyUpb_WeakMap* map, const void* key, PyObject* py_obj);
  107. // Removes the given key from the cache. It must exist in the cache currently.
  108. void PyUpb_WeakMap_Delete(PyUpb_WeakMap* map, const void* key);
  109. void PyUpb_WeakMap_TryDelete(PyUpb_WeakMap* map, const void* key);
  110. // Returns a new reference to an object if it exists, otherwise returns NULL.
  111. PyObject* PyUpb_WeakMap_Get(PyUpb_WeakMap* map, const void* key);
  112. #define PYUPB_WEAKMAP_BEGIN UPB_INTTABLE_BEGIN
  113. // Iteration over the weak map, eg.
  114. //
  115. // intptr_t it = PYUPB_WEAKMAP_BEGIN;
  116. // while (PyUpb_WeakMap_Next(map, &key, &obj, &it)) {
  117. // // ...
  118. // }
  119. //
  120. // Note that the callee does not own a ref on the returned `obj`.
  121. bool PyUpb_WeakMap_Next(PyUpb_WeakMap* map, const void** key, PyObject** obj,
  122. intptr_t* iter);
  123. void PyUpb_WeakMap_DeleteIter(PyUpb_WeakMap* map, intptr_t* iter);
  124. // -----------------------------------------------------------------------------
  125. // ObjCache
  126. // -----------------------------------------------------------------------------
  127. // The object cache is a global WeakMap for mapping upb objects to the
  128. // corresponding wrapper.
  129. void PyUpb_ObjCache_Add(const void* key, PyObject* py_obj);
  130. void PyUpb_ObjCache_Delete(const void* key);
  131. PyObject* PyUpb_ObjCache_Get(const void* key); // returns NULL if not present.
  132. PyUpb_WeakMap* PyUpb_ObjCache_Instance(void);
  133. // -----------------------------------------------------------------------------
  134. // Arena
  135. // -----------------------------------------------------------------------------
  136. PyObject* PyUpb_Arena_New(void);
  137. upb_Arena* PyUpb_Arena_Get(PyObject* arena);
  138. // -----------------------------------------------------------------------------
  139. // Utilities
  140. // -----------------------------------------------------------------------------
  141. PyTypeObject* AddObject(PyObject* m, const char* name, PyType_Spec* spec);
  142. // Creates a Python type from `spec` and adds it to the given module `m`.
  143. PyTypeObject* PyUpb_AddClass(PyObject* m, PyType_Spec* spec);
  144. // Like PyUpb_AddClass(), but allows you to specify a tuple of base classes
  145. // in `bases`.
  146. PyTypeObject* PyUpb_AddClassWithBases(PyObject* m, PyType_Spec* spec,
  147. PyObject* bases);
  148. // A function that implements the tp_new slot for types that we do not allow
  149. // users to create directly. This will immediately fail with an error message.
  150. PyObject* PyUpb_Forbidden_New(PyObject* cls, PyObject* args, PyObject* kwds);
  151. // Our standard dealloc func. It follows the guidance defined in:
  152. // https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc
  153. // However it tests Py_TPFLAGS_HEAPTYPE dynamically so that a single dealloc
  154. // function can work for any type.
  155. static inline void PyUpb_Dealloc(void* self) {
  156. PyTypeObject* tp = Py_TYPE(self);
  157. assert(PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE);
  158. freefunc tp_free = PyType_GetSlot(tp, Py_tp_free);
  159. tp_free(self);
  160. Py_DECREF(tp);
  161. }
  162. // Equivalent to the Py_NewRef() function introduced in Python 3.10. If/when we
  163. // drop support for Python <3.10, we can remove this function and replace all
  164. // callers with Py_NewRef().
  165. static inline PyObject* PyUpb_NewRef(PyObject* obj) {
  166. Py_INCREF(obj);
  167. return obj;
  168. }
  169. const char* PyUpb_GetStrData(PyObject* obj);
  170. const char* PyUpb_VerifyStrData(PyObject* obj);
  171. #endif // PYUPB_PROTOBUF_H__