Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
358 lines
12 KiB
358 lines
12 KiB
1 year ago
|
// Protocol Buffers - Google's data interchange format
|
||
|
// Copyright 2023 Google LLC. All rights reserved.
|
||
|
// https://developers.google.com/protocol-buffers/
|
||
|
//
|
||
|
// Redistribution and use in source and binary forms, with or without
|
||
|
// modification, are permitted provided that the following conditions are
|
||
|
// met:
|
||
|
//
|
||
|
// * Redistributions of source code must retain the above copyright
|
||
|
// notice, this list of conditions and the following disclaimer.
|
||
|
// * Redistributions in binary form must reproduce the above
|
||
|
// copyright notice, this list of conditions and the following disclaimer
|
||
|
// in the documentation and/or other materials provided with the
|
||
|
// distribution.
|
||
|
// * Neither the name of Google LLC nor the names of its
|
||
|
// contributors may be used to endorse or promote products derived from
|
||
|
// this software without specific prior written permission.
|
||
|
//
|
||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
3 years ago
|
|
||
3 years ago
|
#include "python/protobuf.h"
|
||
3 years ago
|
|
||
3 years ago
|
#include "python/descriptor.h"
|
||
|
#include "python/descriptor_containers.h"
|
||
|
#include "python/descriptor_pool.h"
|
||
3 years ago
|
#include "python/extension_dict.h"
|
||
3 years ago
|
#include "python/map.h"
|
||
3 years ago
|
#include "python/message.h"
|
||
|
#include "python/repeated.h"
|
||
3 years ago
|
#include "python/unknown_fields.h"
|
||
3 years ago
|
|
||
3 years ago
|
static void PyUpb_ModuleDealloc(void* module) {
|
||
|
PyUpb_ModuleState* s = PyModule_GetState(module);
|
||
3 years ago
|
PyUpb_WeakMap_Free(s->obj_cache);
|
||
3 years ago
|
if (s->c_descriptor_symtab) {
|
||
3 years ago
|
upb_DefPool_Free(s->c_descriptor_symtab);
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
PyObject* PyUpb_SetAllowOversizeProtos(PyObject* m, PyObject* arg) {
|
||
|
if (!arg || !PyBool_Check(arg)) {
|
||
|
PyErr_SetString(PyExc_TypeError,
|
||
|
"Argument to SetAllowOversizeProtos must be boolean");
|
||
|
return NULL;
|
||
|
}
|
||
|
PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
|
||
|
state->allow_oversize_protos = PyObject_IsTrue(arg);
|
||
|
Py_INCREF(arg);
|
||
|
return arg;
|
||
|
}
|
||
|
|
||
|
static PyMethodDef PyUpb_ModuleMethods[] = {
|
||
|
{"SetAllowOversizeProtos", PyUpb_SetAllowOversizeProtos, METH_O,
|
||
|
"Enable/disable oversize proto parsing."},
|
||
|
{NULL, NULL}};
|
||
|
|
||
3 years ago
|
static struct PyModuleDef module_def = {PyModuleDef_HEAD_INIT,
|
||
|
PYUPB_MODULE_NAME,
|
||
|
"Protobuf Module",
|
||
|
sizeof(PyUpb_ModuleState),
|
||
3 years ago
|
PyUpb_ModuleMethods, // m_methods
|
||
3 years ago
|
NULL, // m_slots
|
||
|
NULL, // m_traverse
|
||
|
NULL, // m_clear
|
||
3 years ago
|
PyUpb_ModuleDealloc};
|
||
|
|
||
|
// -----------------------------------------------------------------------------
|
||
|
// ModuleState
|
||
|
// -----------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
PyUpb_ModuleState* PyUpb_ModuleState_MaybeGet(void) {
|
||
|
PyObject* module = PyState_FindModule(&module_def);
|
||
|
return module ? PyModule_GetState(module) : NULL;
|
||
|
}
|
||
|
|
||
3 years ago
|
PyUpb_ModuleState* PyUpb_ModuleState_GetFromModule(PyObject* module) {
|
||
|
PyUpb_ModuleState* state = PyModule_GetState(module);
|
||
3 years ago
|
assert(state);
|
||
|
assert(PyModule_GetDef(module) == &module_def);
|
||
|
return state;
|
||
|
}
|
||
|
|
||
3 years ago
|
PyUpb_ModuleState* PyUpb_ModuleState_Get(void) {
|
||
|
PyObject* module = PyState_FindModule(&module_def);
|
||
3 years ago
|
assert(module);
|
||
3 years ago
|
return PyUpb_ModuleState_GetFromModule(module);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
PyObject* PyUpb_GetWktBases(PyUpb_ModuleState* state) {
|
||
3 years ago
|
if (!state->wkt_bases) {
|
||
3 years ago
|
PyObject* wkt_module = PyImport_ImportModule(PYUPB_PROTOBUF_INTERNAL_PACKAGE
|
||
|
".well_known_types");
|
||
3 years ago
|
|
||
|
if (wkt_module == NULL) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
state->wkt_bases = PyObject_GetAttrString(wkt_module, "WKTBASES");
|
||
3 years ago
|
PyObject* m = PyState_FindModule(&module_def);
|
||
3 years ago
|
// Reparent ownership to m.
|
||
3 years ago
|
PyModule_AddObject(m, "__internal_wktbases", state->wkt_bases);
|
||
|
Py_DECREF(wkt_module);
|
||
|
}
|
||
|
|
||
|
return state->wkt_bases;
|
||
|
}
|
||
|
|
||
3 years ago
|
// -----------------------------------------------------------------------------
|
||
3 years ago
|
// WeakMap
|
||
3 years ago
|
// -----------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
struct PyUpb_WeakMap {
|
||
|
upb_inttable table;
|
||
3 years ago
|
upb_Arena* arena;
|
||
3 years ago
|
};
|
||
|
|
||
3 years ago
|
PyUpb_WeakMap* PyUpb_WeakMap_New(void) {
|
||
|
upb_Arena* arena = upb_Arena_New();
|
||
|
PyUpb_WeakMap* map = upb_Arena_Malloc(arena, sizeof(*map));
|
||
3 years ago
|
map->arena = arena;
|
||
|
upb_inttable_init(&map->table, map->arena);
|
||
|
return map;
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
void PyUpb_WeakMap_Free(PyUpb_WeakMap* map) { upb_Arena_Free(map->arena); }
|
||
3 years ago
|
|
||
3 years ago
|
// To give better entropy in the table key, we shift away low bits that are
|
||
|
// always zero.
|
||
|
static const int PyUpb_PtrShift = (sizeof(void*) == 4) ? 2 : 3;
|
||
|
|
||
3 years ago
|
uintptr_t PyUpb_WeakMap_GetKey(const void* key) {
|
||
3 years ago
|
uintptr_t n = (uintptr_t)key;
|
||
3 years ago
|
assert((n & ((1 << PyUpb_PtrShift) - 1)) == 0);
|
||
|
return n >> PyUpb_PtrShift;
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
void PyUpb_WeakMap_Add(PyUpb_WeakMap* map, const void* key, PyObject* py_obj) {
|
||
3 years ago
|
upb_inttable_insert(&map->table, PyUpb_WeakMap_GetKey(key),
|
||
|
upb_value_ptr(py_obj), map->arena);
|
||
|
}
|
||
|
|
||
3 years ago
|
void PyUpb_WeakMap_Delete(PyUpb_WeakMap* map, const void* key) {
|
||
3 years ago
|
upb_value val;
|
||
3 years ago
|
bool removed =
|
||
|
upb_inttable_remove(&map->table, PyUpb_WeakMap_GetKey(key), &val);
|
||
|
(void)removed;
|
||
|
assert(removed);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
void PyUpb_WeakMap_TryDelete(PyUpb_WeakMap* map, const void* key) {
|
||
3 years ago
|
upb_inttable_remove(&map->table, PyUpb_WeakMap_GetKey(key), NULL);
|
||
|
}
|
||
|
|
||
3 years ago
|
PyObject* PyUpb_WeakMap_Get(PyUpb_WeakMap* map, const void* key) {
|
||
3 years ago
|
upb_value val;
|
||
3 years ago
|
if (upb_inttable_lookup(&map->table, PyUpb_WeakMap_GetKey(key), &val)) {
|
||
3 years ago
|
PyObject* ret = upb_value_getptr(val);
|
||
3 years ago
|
Py_INCREF(ret);
|
||
|
return ret;
|
||
|
} else {
|
||
|
return NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
bool PyUpb_WeakMap_Next(PyUpb_WeakMap* map, const void** key, PyObject** obj,
|
||
|
intptr_t* iter) {
|
||
3 years ago
|
uintptr_t u_key;
|
||
|
upb_value val;
|
||
2 years ago
|
if (!upb_inttable_next(&map->table, &u_key, &val, iter)) return false;
|
||
3 years ago
|
*key = (void*)(u_key << PyUpb_PtrShift);
|
||
3 years ago
|
*obj = upb_value_getptr(val);
|
||
|
return true;
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
void PyUpb_WeakMap_DeleteIter(PyUpb_WeakMap* map, intptr_t* iter) {
|
||
3 years ago
|
upb_inttable_removeiter(&map->table, iter);
|
||
|
}
|
||
|
|
||
|
// -----------------------------------------------------------------------------
|
||
|
// ObjCache
|
||
|
// -----------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
PyUpb_WeakMap* PyUpb_ObjCache_Instance(void) {
|
||
|
PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
|
||
3 years ago
|
return state->obj_cache;
|
||
|
}
|
||
|
|
||
3 years ago
|
void PyUpb_ObjCache_Add(const void* key, PyObject* py_obj) {
|
||
3 years ago
|
PyUpb_WeakMap_Add(PyUpb_ObjCache_Instance(), key, py_obj);
|
||
|
}
|
||
|
|
||
3 years ago
|
void PyUpb_ObjCache_Delete(const void* key) {
|
||
|
PyUpb_ModuleState* state = PyUpb_ModuleState_MaybeGet();
|
||
3 years ago
|
if (!state) {
|
||
|
// During the shutdown sequence, our object's Dealloc() methods can be
|
||
|
// called *after* our module Dealloc() method has been called. At that
|
||
|
// point our state will be NULL and there is nothing to delete out of the
|
||
|
// map.
|
||
|
return;
|
||
|
}
|
||
|
PyUpb_WeakMap_Delete(state->obj_cache, key);
|
||
|
}
|
||
|
|
||
3 years ago
|
PyObject* PyUpb_ObjCache_Get(const void* key) {
|
||
3 years ago
|
return PyUpb_WeakMap_Get(PyUpb_ObjCache_Instance(), key);
|
||
|
}
|
||
|
|
||
3 years ago
|
// -----------------------------------------------------------------------------
|
||
|
// Arena
|
||
|
// -----------------------------------------------------------------------------
|
||
|
|
||
|
typedef struct {
|
||
3 years ago
|
PyObject_HEAD;
|
||
|
upb_Arena* arena;
|
||
3 years ago
|
} PyUpb_Arena;
|
||
|
|
||
3 years ago
|
PyObject* PyUpb_Arena_New(void) {
|
||
|
PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
|
||
|
PyUpb_Arena* arena = (void*)PyType_GenericAlloc(state->arena_type, 0);
|
||
3 years ago
|
arena->arena = upb_Arena_New();
|
||
3 years ago
|
return &arena->ob_base;
|
||
|
}
|
||
|
|
||
3 years ago
|
static void PyUpb_Arena_Dealloc(PyObject* self) {
|
||
3 years ago
|
upb_Arena_Free(PyUpb_Arena_Get(self));
|
||
3 years ago
|
PyUpb_Dealloc(self);
|
||
|
}
|
||
|
|
||
3 years ago
|
upb_Arena* PyUpb_Arena_Get(PyObject* arena) {
|
||
3 years ago
|
return ((PyUpb_Arena*)arena)->arena;
|
||
|
}
|
||
|
|
||
|
static PyType_Slot PyUpb_Arena_Slots[] = {
|
||
|
{Py_tp_dealloc, PyUpb_Arena_Dealloc},
|
||
|
{0, NULL},
|
||
|
};
|
||
|
|
||
|
static PyType_Spec PyUpb_Arena_Spec = {
|
||
|
PYUPB_MODULE_NAME ".Arena",
|
||
|
sizeof(PyUpb_Arena),
|
||
|
0, // itemsize
|
||
|
Py_TPFLAGS_DEFAULT,
|
||
|
PyUpb_Arena_Slots,
|
||
|
};
|
||
|
|
||
3 years ago
|
static bool PyUpb_InitArena(PyObject* m) {
|
||
|
PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m);
|
||
3 years ago
|
state->arena_type = PyUpb_AddClass(m, &PyUpb_Arena_Spec);
|
||
|
return state->arena_type;
|
||
|
}
|
||
|
|
||
3 years ago
|
// -----------------------------------------------------------------------------
|
||
|
// Utilities
|
||
|
// -----------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
PyTypeObject* AddObject(PyObject* m, const char* name, PyType_Spec* spec) {
|
||
|
PyObject* type = PyType_FromSpec(spec);
|
||
|
return type && PyModule_AddObject(m, name, type) == 0 ? (PyTypeObject*)type
|
||
3 years ago
|
: NULL;
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
static const char* PyUpb_GetClassName(PyType_Spec* spec) {
|
||
3 years ago
|
// spec->name contains a fully-qualified name, like:
|
||
|
// google.protobuf.pyext._message.FooBar
|
||
|
//
|
||
|
// Find the rightmost '.' to get "FooBar".
|
||
3 years ago
|
const char* name = strrchr(spec->name, '.');
|
||
3 years ago
|
assert(name);
|
||
|
return name + 1;
|
||
|
}
|
||
|
|
||
3 years ago
|
PyTypeObject* PyUpb_AddClass(PyObject* m, PyType_Spec* spec) {
|
||
|
PyObject* type = PyType_FromSpec(spec);
|
||
|
const char* name = PyUpb_GetClassName(spec);
|
||
3 years ago
|
if (PyModule_AddObject(m, name, type) < 0) {
|
||
|
Py_XDECREF(type);
|
||
|
return NULL;
|
||
|
}
|
||
|
return (PyTypeObject*)type;
|
||
3 years ago
|
}
|
||
|
|
||
|
PyTypeObject* PyUpb_AddClassWithBases(PyObject* m, PyType_Spec* spec,
|
||
|
PyObject* bases) {
|
||
|
PyObject* type = PyType_FromSpecWithBases(spec, bases);
|
||
|
const char* name = PyUpb_GetClassName(spec);
|
||
3 years ago
|
if (PyModule_AddObject(m, name, type) < 0) {
|
||
|
Py_XDECREF(type);
|
||
|
return NULL;
|
||
|
}
|
||
|
return (PyTypeObject*)type;
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
const char* PyUpb_GetStrData(PyObject* obj) {
|
||
3 years ago
|
if (PyUnicode_Check(obj)) {
|
||
|
return PyUnicode_AsUTF8AndSize(obj, NULL);
|
||
|
} else if (PyBytes_Check(obj)) {
|
||
|
return PyBytes_AsString(obj);
|
||
|
} else {
|
||
|
return NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
const char* PyUpb_VerifyStrData(PyObject* obj) {
|
||
3 years ago
|
const char* ret = PyUpb_GetStrData(obj);
|
||
|
if (ret) return ret;
|
||
|
PyErr_Format(PyExc_TypeError, "Expected string: %S", obj);
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
3 years ago
|
PyObject* PyUpb_Forbidden_New(PyObject* cls, PyObject* args, PyObject* kwds) {
|
||
|
PyObject* name = PyObject_GetAttrString(cls, "__name__");
|
||
3 years ago
|
PyErr_Format(PyExc_RuntimeError,
|
||
|
"Objects of type %U may not be created directly.", name);
|
||
|
Py_XDECREF(name);
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
3 years ago
|
// -----------------------------------------------------------------------------
|
||
|
// Module Entry Point
|
||
|
// -----------------------------------------------------------------------------
|
||
|
|
||
3 years ago
|
__attribute__((visibility("default"))) PyMODINIT_FUNC PyInit__message(void) {
|
||
3 years ago
|
PyObject* m = PyModule_Create(&module_def);
|
||
3 years ago
|
if (!m) return NULL;
|
||
|
|
||
3 years ago
|
PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m);
|
||
3 years ago
|
|
||
3 years ago
|
state->allow_oversize_protos = false;
|
||
3 years ago
|
state->wkt_bases = NULL;
|
||
|
state->obj_cache = PyUpb_WeakMap_New();
|
||
3 years ago
|
state->c_descriptor_symtab = NULL;
|
||
3 years ago
|
|
||
3 years ago
|
if (!PyUpb_InitDescriptorContainers(m) || !PyUpb_InitDescriptorPool(m) ||
|
||
3 years ago
|
!PyUpb_InitDescriptor(m) || !PyUpb_InitArena(m) ||
|
||
|
!PyUpb_InitExtensionDict(m) || !PyUpb_Map_Init(m) ||
|
||
3 years ago
|
!PyUpb_InitMessage(m) || !PyUpb_Repeated_Init(m) ||
|
||
|
!PyUpb_UnknownFields_Init(m)) {
|
||
3 years ago
|
Py_DECREF(m);
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
3 years ago
|
// Temporary: an cookie we can use in the tests to ensure we are testing upb
|
||
|
// and not another protobuf library on the system.
|
||
3 years ago
|
PyModule_AddIntConstant(m, "_IS_UPB", 1);
|
||
3 years ago
|
|
||
3 years ago
|
return m;
|
||
|
}
|