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.
257 lines
9.8 KiB
257 lines
9.8 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
|
|
||
|
#include "python/extension_dict.h"
|
||
|
|
||
|
#include "python/message.h"
|
||
|
#include "python/protobuf.h"
|
||
2 years ago
|
#include "upb/reflection/def.h"
|
||
3 years ago
|
|
||
|
// -----------------------------------------------------------------------------
|
||
|
// ExtensionDict
|
||
|
// -----------------------------------------------------------------------------
|
||
|
|
||
|
typedef struct {
|
||
3 years ago
|
PyObject_HEAD;
|
||
3 years ago
|
PyObject* msg; // Owning ref to our parent pessage.
|
||
|
} PyUpb_ExtensionDict;
|
||
|
|
||
|
PyObject* PyUpb_ExtensionDict_New(PyObject* msg) {
|
||
|
PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
|
||
|
PyUpb_ExtensionDict* ext_dict =
|
||
|
(void*)PyType_GenericAlloc(state->extension_dict_type, 0);
|
||
|
ext_dict->msg = msg;
|
||
|
Py_INCREF(ext_dict->msg);
|
||
|
return &ext_dict->ob_base;
|
||
|
}
|
||
|
|
||
|
static PyObject* PyUpb_ExtensionDict_FindExtensionByName(PyObject* _self,
|
||
|
PyObject* key) {
|
||
|
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self;
|
||
|
const char* name = PyUpb_GetStrData(key);
|
||
1 year ago
|
if (!name) {
|
||
|
PyErr_Format(PyExc_TypeError, "_FindExtensionByName expect a str");
|
||
|
return NULL;
|
||
|
}
|
||
3 years ago
|
const upb_MessageDef* m = PyUpb_Message_GetMsgdef(self->msg);
|
||
3 years ago
|
const upb_FileDef* file = upb_MessageDef_File(m);
|
||
|
const upb_DefPool* symtab = upb_FileDef_Pool(file);
|
||
|
const upb_FieldDef* ext = upb_DefPool_FindExtensionByName(symtab, name);
|
||
3 years ago
|
if (ext) {
|
||
|
return PyUpb_FieldDescriptor_Get(ext);
|
||
|
} else {
|
||
|
Py_RETURN_NONE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static PyObject* PyUpb_ExtensionDict_FindExtensionByNumber(PyObject* _self,
|
||
|
PyObject* arg) {
|
||
|
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self;
|
||
3 years ago
|
const upb_MessageDef* m = PyUpb_Message_GetMsgdef(self->msg);
|
||
3 years ago
|
const upb_MiniTable* l = upb_MessageDef_MiniTable(m);
|
||
|
const upb_FileDef* file = upb_MessageDef_File(m);
|
||
|
const upb_DefPool* symtab = upb_FileDef_Pool(file);
|
||
3 years ago
|
const upb_ExtensionRegistry* reg = upb_DefPool_ExtensionRegistry(symtab);
|
||
3 years ago
|
int64_t number = PyLong_AsLong(arg);
|
||
1 year ago
|
if (number == -1 && PyErr_Occurred()) return NULL;
|
||
2 years ago
|
const upb_MiniTableExtension* ext =
|
||
|
(upb_MiniTableExtension*)upb_ExtensionRegistry_Lookup(reg, l, number);
|
||
3 years ago
|
if (ext) {
|
||
2 years ago
|
const upb_FieldDef* f = upb_DefPool_FindExtensionByMiniTable(symtab, ext);
|
||
3 years ago
|
return PyUpb_FieldDescriptor_Get(f);
|
||
|
} else {
|
||
|
Py_RETURN_NONE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void PyUpb_ExtensionDict_Dealloc(PyUpb_ExtensionDict* self) {
|
||
3 years ago
|
PyUpb_Message_ClearExtensionDict(self->msg);
|
||
3 years ago
|
Py_DECREF(self->msg);
|
||
|
PyUpb_Dealloc(self);
|
||
|
}
|
||
|
|
||
3 years ago
|
static PyObject* PyUpb_ExtensionDict_RichCompare(PyObject* _self,
|
||
|
PyObject* _other, int opid) {
|
||
|
// Only equality comparisons are implemented.
|
||
|
if (opid != Py_EQ && opid != Py_NE) {
|
||
|
Py_INCREF(Py_NotImplemented);
|
||
|
return Py_NotImplemented;
|
||
|
}
|
||
|
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self;
|
||
|
bool equals = false;
|
||
|
if (PyObject_TypeCheck(_other, Py_TYPE(_self))) {
|
||
|
PyUpb_ExtensionDict* other = (PyUpb_ExtensionDict*)_other;
|
||
|
equals = self->msg == other->msg;
|
||
|
}
|
||
|
bool ret = opid == Py_EQ ? equals : !equals;
|
||
|
return PyBool_FromLong(ret);
|
||
|
}
|
||
|
|
||
3 years ago
|
static int PyUpb_ExtensionDict_Contains(PyObject* _self, PyObject* key) {
|
||
|
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self;
|
||
3 years ago
|
const upb_FieldDef* f = PyUpb_Message_GetExtensionDef(self->msg, key);
|
||
3 years ago
|
if (!f) return -1;
|
||
3 years ago
|
upb_Message* msg = PyUpb_Message_GetIfReified(self->msg);
|
||
3 years ago
|
if (!msg) return 0;
|
||
3 years ago
|
if (upb_FieldDef_IsRepeated(f)) {
|
||
2 years ago
|
upb_MessageValue val = upb_Message_GetFieldByDef(msg, f);
|
||
3 years ago
|
return upb_Array_Size(val.array_val) > 0;
|
||
3 years ago
|
} else {
|
||
2 years ago
|
return upb_Message_HasFieldByDef(msg, f);
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
static Py_ssize_t PyUpb_ExtensionDict_Length(PyObject* _self) {
|
||
|
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self;
|
||
3 years ago
|
upb_Message* msg = PyUpb_Message_GetIfReified(self->msg);
|
||
3 years ago
|
return msg ? upb_Message_ExtensionCount(msg) : 0;
|
||
3 years ago
|
}
|
||
|
|
||
|
static PyObject* PyUpb_ExtensionDict_Subscript(PyObject* _self, PyObject* key) {
|
||
|
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self;
|
||
3 years ago
|
const upb_FieldDef* f = PyUpb_Message_GetExtensionDef(self->msg, key);
|
||
3 years ago
|
if (!f) return NULL;
|
||
3 years ago
|
return PyUpb_Message_GetFieldValue(self->msg, f);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
static int PyUpb_ExtensionDict_AssignSubscript(PyObject* _self, PyObject* key,
|
||
|
PyObject* val) {
|
||
3 years ago
|
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self;
|
||
3 years ago
|
const upb_FieldDef* f = PyUpb_Message_GetExtensionDef(self->msg, key);
|
||
3 years ago
|
if (!f) return -1;
|
||
3 years ago
|
if (val) {
|
||
3 years ago
|
return PyUpb_Message_SetFieldValue(self->msg, f, val, PyExc_TypeError);
|
||
3 years ago
|
} else {
|
||
3 years ago
|
PyUpb_Message_DoClearField(self->msg, f);
|
||
3 years ago
|
return 0;
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
static PyObject* PyUpb_ExtensionIterator_New(PyObject* _ext_dict);
|
||
|
|
||
3 years ago
|
static PyMethodDef PyUpb_ExtensionDict_Methods[] = {
|
||
|
{"_FindExtensionByName", PyUpb_ExtensionDict_FindExtensionByName, METH_O,
|
||
|
"Finds an extension by name."},
|
||
|
{"_FindExtensionByNumber", PyUpb_ExtensionDict_FindExtensionByNumber,
|
||
|
METH_O, "Finds an extension by number."},
|
||
|
{NULL, NULL},
|
||
|
};
|
||
|
|
||
|
static PyType_Slot PyUpb_ExtensionDict_Slots[] = {
|
||
|
{Py_tp_dealloc, PyUpb_ExtensionDict_Dealloc},
|
||
|
{Py_tp_methods, PyUpb_ExtensionDict_Methods},
|
||
|
//{Py_tp_getset, PyUpb_ExtensionDict_Getters},
|
||
|
//{Py_tp_hash, PyObject_HashNotImplemented},
|
||
3 years ago
|
{Py_tp_richcompare, PyUpb_ExtensionDict_RichCompare},
|
||
3 years ago
|
{Py_tp_iter, PyUpb_ExtensionIterator_New},
|
||
3 years ago
|
{Py_sq_contains, PyUpb_ExtensionDict_Contains},
|
||
3 years ago
|
{Py_sq_length, PyUpb_ExtensionDict_Length},
|
||
|
{Py_mp_length, PyUpb_ExtensionDict_Length},
|
||
|
{Py_mp_subscript, PyUpb_ExtensionDict_Subscript},
|
||
|
{Py_mp_ass_subscript, PyUpb_ExtensionDict_AssignSubscript},
|
||
|
{0, NULL}};
|
||
|
|
||
|
static PyType_Spec PyUpb_ExtensionDict_Spec = {
|
||
|
PYUPB_MODULE_NAME ".ExtensionDict", // tp_name
|
||
|
sizeof(PyUpb_ExtensionDict), // tp_basicsize
|
||
|
0, // tp_itemsize
|
||
|
Py_TPFLAGS_DEFAULT, // tp_flags
|
||
|
PyUpb_ExtensionDict_Slots,
|
||
|
};
|
||
|
|
||
|
// -----------------------------------------------------------------------------
|
||
|
// ExtensionIterator
|
||
|
// -----------------------------------------------------------------------------
|
||
|
|
||
|
typedef struct {
|
||
3 years ago
|
PyObject_HEAD;
|
||
3 years ago
|
PyObject* msg;
|
||
|
size_t iter;
|
||
3 years ago
|
} PyUpb_ExtensionIterator;
|
||
|
|
||
3 years ago
|
static PyObject* PyUpb_ExtensionIterator_New(PyObject* _ext_dict) {
|
||
|
PyUpb_ExtensionDict* ext_dict = (PyUpb_ExtensionDict*)_ext_dict;
|
||
|
PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
|
||
|
PyUpb_ExtensionIterator* iter =
|
||
|
(void*)PyType_GenericAlloc(state->extension_iterator_type, 0);
|
||
3 years ago
|
if (!iter) return NULL;
|
||
3 years ago
|
iter->msg = ext_dict->msg;
|
||
3 years ago
|
iter->iter = kUpb_Message_Begin;
|
||
3 years ago
|
Py_INCREF(iter->msg);
|
||
|
return &iter->ob_base;
|
||
|
}
|
||
|
|
||
|
static void PyUpb_ExtensionIterator_Dealloc(void* _self) {
|
||
|
PyUpb_ExtensionIterator* self = (PyUpb_ExtensionIterator*)_self;
|
||
3 years ago
|
Py_DECREF(self->msg);
|
||
3 years ago
|
PyUpb_Dealloc(_self);
|
||
|
}
|
||
|
|
||
|
PyObject* PyUpb_ExtensionIterator_IterNext(PyObject* _self) {
|
||
|
PyUpb_ExtensionIterator* self = (PyUpb_ExtensionIterator*)_self;
|
||
3 years ago
|
upb_Message* msg = PyUpb_Message_GetIfReified(self->msg);
|
||
3 years ago
|
if (!msg) return NULL;
|
||
3 years ago
|
const upb_MessageDef* m = PyUpb_Message_GetMsgdef(self->msg);
|
||
3 years ago
|
const upb_DefPool* symtab = upb_FileDef_Pool(upb_MessageDef_File(m));
|
||
3 years ago
|
while (true) {
|
||
3 years ago
|
const upb_FieldDef* f;
|
||
|
upb_MessageValue val;
|
||
|
if (!upb_Message_Next(msg, m, symtab, &f, &val, &self->iter)) return NULL;
|
||
|
if (upb_FieldDef_IsExtension(f)) return PyUpb_FieldDescriptor_Get(f);
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
|
|
||
|
static PyType_Slot PyUpb_ExtensionIterator_Slots[] = {
|
||
|
{Py_tp_dealloc, PyUpb_ExtensionIterator_Dealloc},
|
||
|
{Py_tp_iter, PyObject_SelfIter},
|
||
3 years ago
|
{Py_tp_iternext, PyUpb_ExtensionIterator_IterNext},
|
||
3 years ago
|
{0, NULL}};
|
||
|
|
||
|
static PyType_Spec PyUpb_ExtensionIterator_Spec = {
|
||
|
PYUPB_MODULE_NAME ".ExtensionIterator", // tp_name
|
||
3 years ago
|
sizeof(PyUpb_ExtensionIterator), // tp_basicsize
|
||
3 years ago
|
0, // tp_itemsize
|
||
|
Py_TPFLAGS_DEFAULT, // tp_flags
|
||
|
PyUpb_ExtensionIterator_Slots,
|
||
|
};
|
||
|
|
||
|
// -----------------------------------------------------------------------------
|
||
|
// Top Level
|
||
|
// -----------------------------------------------------------------------------
|
||
|
|
||
|
bool PyUpb_InitExtensionDict(PyObject* m) {
|
||
|
PyUpb_ModuleState* s = PyUpb_ModuleState_GetFromModule(m);
|
||
|
|
||
|
s->extension_dict_type = PyUpb_AddClass(m, &PyUpb_ExtensionDict_Spec);
|
||
|
s->extension_iterator_type = PyUpb_AddClass(m, &PyUpb_ExtensionIterator_Spec);
|
||
|
|
||
|
return s->extension_dict_type && s->extension_iterator_type;
|
||
|
}
|