Added python protobuf
This commit is contained in:
1 parent
029b9216b9
commit
2282ec752b
163 files changed
+63512
-1
No files matched your search
@@ -0,0 +1,6 @@
|
||||
This is the 'v2' C++ implementation for python proto2.
|
||||
|
||||
It is active when:
|
||||
|
||||
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp
|
||||
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION=2
|
||||
@@ -0,0 +1,4 @@
|
||||
try:
|
||||
__import__('pkg_resources').declare_namespace(__name__)
|
||||
except ImportError:
|
||||
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
|
||||
@@ -0,0 +1,65 @@
|
||||
# Protocol Buffers - Google's data interchange format
|
||||
# Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
"""Protocol message implementation hooks for C++ implementation.
|
||||
|
||||
Contains helper functions used to create protocol message classes from
|
||||
Descriptor objects at runtime backed by the protocol buffer C++ API.
|
||||
"""
|
||||
|
||||
__author__ = 'tibell@google.com (Johan Tibell)'
|
||||
|
||||
from google.protobuf.pyext import _message
|
||||
|
||||
|
||||
class GeneratedProtocolMessageType(_message.MessageMeta):
|
||||
|
||||
"""Metaclass for protocol message classes created at runtime from Descriptors.
|
||||
|
||||
The protocol compiler currently uses this metaclass to create protocol
|
||||
message classes at runtime. Clients can also manually create their own
|
||||
classes at runtime, as in this example:
|
||||
|
||||
mydescriptor = Descriptor(.....)
|
||||
factory = symbol_database.Default()
|
||||
factory.pool.AddDescriptor(mydescriptor)
|
||||
MyProtoClass = factory.GetPrototype(mydescriptor)
|
||||
myproto_instance = MyProtoClass()
|
||||
myproto.foo_field = 23
|
||||
...
|
||||
|
||||
The above example will not work for nested types. If you wish to include them,
|
||||
use reflection.MakeClass() instead of manually instantiating the class in
|
||||
order to create the appropriate class structure.
|
||||
"""
|
||||
|
||||
# Must be consistent with the protocol-compiler code in
|
||||
# proto2/compiler/internal/generator.*.
|
||||
_DESCRIPTOR_KEY = 'DESCRIPTOR'
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,105 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: petar@google.com (Petar Petrov)
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <google/protobuf/descriptor.h>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
extern PyTypeObject PyMessageDescriptor_Type;
|
||||
extern PyTypeObject PyFieldDescriptor_Type;
|
||||
extern PyTypeObject PyEnumDescriptor_Type;
|
||||
extern PyTypeObject PyEnumValueDescriptor_Type;
|
||||
extern PyTypeObject PyFileDescriptor_Type;
|
||||
extern PyTypeObject PyOneofDescriptor_Type;
|
||||
extern PyTypeObject PyServiceDescriptor_Type;
|
||||
extern PyTypeObject PyMethodDescriptor_Type;
|
||||
|
||||
// Wraps a Descriptor in a Python object.
|
||||
// The C++ pointer is usually borrowed from the global DescriptorPool.
|
||||
// In any case, it must stay alive as long as the Python object.
|
||||
// Returns a new reference.
|
||||
PyObject* PyMessageDescriptor_FromDescriptor(const Descriptor* descriptor);
|
||||
PyObject* PyFieldDescriptor_FromDescriptor(const FieldDescriptor* descriptor);
|
||||
PyObject* PyEnumDescriptor_FromDescriptor(const EnumDescriptor* descriptor);
|
||||
PyObject* PyEnumValueDescriptor_FromDescriptor(
|
||||
const EnumValueDescriptor* descriptor);
|
||||
PyObject* PyOneofDescriptor_FromDescriptor(const OneofDescriptor* descriptor);
|
||||
PyObject* PyFileDescriptor_FromDescriptor(
|
||||
const FileDescriptor* file_descriptor);
|
||||
PyObject* PyServiceDescriptor_FromDescriptor(
|
||||
const ServiceDescriptor* descriptor);
|
||||
PyObject* PyMethodDescriptor_FromDescriptor(
|
||||
const MethodDescriptor* descriptor);
|
||||
|
||||
// Alternate constructor of PyFileDescriptor, used when we already have a
|
||||
// serialized FileDescriptorProto that can be cached.
|
||||
// Returns a new reference.
|
||||
PyObject* PyFileDescriptor_FromDescriptorWithSerializedPb(
|
||||
const FileDescriptor* file_descriptor, PyObject* serialized_pb);
|
||||
|
||||
// Return the C++ descriptor pointer.
|
||||
// This function checks the parameter type; on error, return NULL with a Python
|
||||
// exception set.
|
||||
const Descriptor* PyMessageDescriptor_AsDescriptor(PyObject* obj);
|
||||
const FieldDescriptor* PyFieldDescriptor_AsDescriptor(PyObject* obj);
|
||||
const EnumDescriptor* PyEnumDescriptor_AsDescriptor(PyObject* obj);
|
||||
const FileDescriptor* PyFileDescriptor_AsDescriptor(PyObject* obj);
|
||||
const ServiceDescriptor* PyServiceDescriptor_AsDescriptor(PyObject* obj);
|
||||
const MethodDescriptor* PyMethodDescriptor_AsDescriptor(PyObject* obj);
|
||||
|
||||
// Returns the raw C++ pointer.
|
||||
const void* PyDescriptor_AsVoidPtr(PyObject* obj);
|
||||
|
||||
// Check that the calling Python code is the global scope of a _pb2.py module.
|
||||
// This function is used to support the current code generated by the proto
|
||||
// compiler, which insists on modifying descriptors after they have been
|
||||
// created.
|
||||
//
|
||||
// stacklevel indicates which Python frame should be the _pb2.py module.
|
||||
//
|
||||
// Don't use this function outside descriptor classes.
|
||||
bool _CalledFromGeneratedFile(int stacklevel);
|
||||
|
||||
bool InitDescriptor();
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,109 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_CONTAINERS_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_CONTAINERS_H__
|
||||
|
||||
// Mappings and Sequences of descriptors.
|
||||
// They implement containers like fields_by_name, EnumDescriptor.values...
|
||||
// See descriptor_containers.cc for more description.
|
||||
#include <Python.h>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
|
||||
class Descriptor;
|
||||
class FileDescriptor;
|
||||
class EnumDescriptor;
|
||||
class OneofDescriptor;
|
||||
class ServiceDescriptor;
|
||||
|
||||
namespace python {
|
||||
|
||||
// Initialize the various types and objects.
|
||||
bool InitDescriptorMappingTypes();
|
||||
|
||||
// Each function below returns a Mapping, or a Sequence of descriptors.
|
||||
// They all return a new reference.
|
||||
|
||||
namespace message_descriptor {
|
||||
PyObject* NewMessageFieldsByName(const Descriptor* descriptor);
|
||||
PyObject* NewMessageFieldsByCamelcaseName(const Descriptor* descriptor);
|
||||
PyObject* NewMessageFieldsByNumber(const Descriptor* descriptor);
|
||||
PyObject* NewMessageFieldsSeq(const Descriptor* descriptor);
|
||||
|
||||
PyObject* NewMessageNestedTypesSeq(const Descriptor* descriptor);
|
||||
PyObject* NewMessageNestedTypesByName(const Descriptor* descriptor);
|
||||
|
||||
PyObject* NewMessageEnumsByName(const Descriptor* descriptor);
|
||||
PyObject* NewMessageEnumsSeq(const Descriptor* descriptor);
|
||||
PyObject* NewMessageEnumValuesByName(const Descriptor* descriptor);
|
||||
|
||||
PyObject* NewMessageExtensionsByName(const Descriptor* descriptor);
|
||||
PyObject* NewMessageExtensionsSeq(const Descriptor* descriptor);
|
||||
|
||||
PyObject* NewMessageOneofsByName(const Descriptor* descriptor);
|
||||
PyObject* NewMessageOneofsSeq(const Descriptor* descriptor);
|
||||
} // namespace message_descriptor
|
||||
|
||||
namespace enum_descriptor {
|
||||
PyObject* NewEnumValuesByName(const EnumDescriptor* descriptor);
|
||||
PyObject* NewEnumValuesByNumber(const EnumDescriptor* descriptor);
|
||||
PyObject* NewEnumValuesSeq(const EnumDescriptor* descriptor);
|
||||
} // namespace enum_descriptor
|
||||
|
||||
namespace oneof_descriptor {
|
||||
PyObject* NewOneofFieldsSeq(const OneofDescriptor* descriptor);
|
||||
} // namespace oneof_descriptor
|
||||
|
||||
namespace file_descriptor {
|
||||
PyObject* NewFileMessageTypesByName(const FileDescriptor* descriptor);
|
||||
|
||||
PyObject* NewFileEnumTypesByName(const FileDescriptor* descriptor);
|
||||
|
||||
PyObject* NewFileExtensionsByName(const FileDescriptor* descriptor);
|
||||
|
||||
PyObject* NewFileServicesByName(const FileDescriptor* descriptor);
|
||||
|
||||
PyObject* NewFileDependencies(const FileDescriptor* descriptor);
|
||||
PyObject* NewFilePublicDependencies(const FileDescriptor* descriptor);
|
||||
} // namespace file_descriptor
|
||||
|
||||
namespace service_descriptor {
|
||||
PyObject* NewServiceMethodsSeq(const ServiceDescriptor* descriptor);
|
||||
PyObject* NewServiceMethodsByName(const ServiceDescriptor* descriptor);
|
||||
} // namespace service_descriptor
|
||||
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_CONTAINERS_H__
|
||||
@@ -0,0 +1,148 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// This file defines a C++ DescriptorDatabase, which wraps a Python Database
|
||||
// and delegate all its operations to Python methods.
|
||||
|
||||
#include <google/protobuf/pyext/descriptor_database.h>
|
||||
|
||||
#include <google/protobuf/stubs/logging.h>
|
||||
#include <google/protobuf/stubs/common.h>
|
||||
#include <google/protobuf/descriptor.pb.h>
|
||||
#include <google/protobuf/pyext/message.h>
|
||||
#include <google/protobuf/pyext/scoped_pyobject_ptr.h>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
PyDescriptorDatabase::PyDescriptorDatabase(PyObject* py_database)
|
||||
: py_database_(py_database) {
|
||||
Py_INCREF(py_database_);
|
||||
}
|
||||
|
||||
PyDescriptorDatabase::~PyDescriptorDatabase() { Py_DECREF(py_database_); }
|
||||
|
||||
// Convert a Python object to a FileDescriptorProto pointer.
|
||||
// Handles all kinds of Python errors, which are simply logged.
|
||||
static bool GetFileDescriptorProto(PyObject* py_descriptor,
|
||||
FileDescriptorProto* output) {
|
||||
if (py_descriptor == NULL) {
|
||||
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
|
||||
// Expected error: item was simply not found.
|
||||
PyErr_Clear();
|
||||
} else {
|
||||
GOOGLE_LOG(ERROR) << "DescriptorDatabase method raised an error";
|
||||
PyErr_Print();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (py_descriptor == Py_None) {
|
||||
return false;
|
||||
}
|
||||
const Descriptor* filedescriptor_descriptor =
|
||||
FileDescriptorProto::default_instance().GetDescriptor();
|
||||
CMessage* message = reinterpret_cast<CMessage*>(py_descriptor);
|
||||
if (PyObject_TypeCheck(py_descriptor, &CMessage_Type) &&
|
||||
message->message->GetDescriptor() == filedescriptor_descriptor) {
|
||||
// Fast path: Just use the pointer.
|
||||
FileDescriptorProto* file_proto =
|
||||
static_cast<FileDescriptorProto*>(message->message);
|
||||
*output = *file_proto;
|
||||
return true;
|
||||
} else {
|
||||
// Slow path: serialize the message. This allows to use databases which
|
||||
// use a different implementation of FileDescriptorProto.
|
||||
ScopedPyObjectPtr serialized_pb(
|
||||
PyObject_CallMethod(py_descriptor, "SerializeToString", NULL));
|
||||
if (serialized_pb == NULL) {
|
||||
GOOGLE_LOG(ERROR)
|
||||
<< "DescriptorDatabase method did not return a FileDescriptorProto";
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
char* str;
|
||||
Py_ssize_t len;
|
||||
if (PyBytes_AsStringAndSize(serialized_pb.get(), &str, &len) < 0) {
|
||||
GOOGLE_LOG(ERROR)
|
||||
<< "DescriptorDatabase method did not return a FileDescriptorProto";
|
||||
PyErr_Print();
|
||||
return false;
|
||||
}
|
||||
FileDescriptorProto file_proto;
|
||||
if (!file_proto.ParseFromArray(str, len)) {
|
||||
GOOGLE_LOG(ERROR)
|
||||
<< "DescriptorDatabase method did not return a FileDescriptorProto";
|
||||
return false;
|
||||
}
|
||||
*output = file_proto;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Find a file by file name.
|
||||
bool PyDescriptorDatabase::FindFileByName(const string& filename,
|
||||
FileDescriptorProto* output) {
|
||||
ScopedPyObjectPtr py_descriptor(PyObject_CallMethod(
|
||||
py_database_, "FindFileByName", "s#", filename.c_str(), filename.size()));
|
||||
return GetFileDescriptorProto(py_descriptor.get(), output);
|
||||
}
|
||||
|
||||
// Find the file that declares the given fully-qualified symbol name.
|
||||
bool PyDescriptorDatabase::FindFileContainingSymbol(
|
||||
const string& symbol_name, FileDescriptorProto* output) {
|
||||
ScopedPyObjectPtr py_descriptor(
|
||||
PyObject_CallMethod(py_database_, "FindFileContainingSymbol", "s#",
|
||||
symbol_name.c_str(), symbol_name.size()));
|
||||
return GetFileDescriptorProto(py_descriptor.get(), output);
|
||||
}
|
||||
|
||||
// Find the file which defines an extension extending the given message type
|
||||
// with the given field number.
|
||||
// Python DescriptorDatabases are not required to implement this method.
|
||||
bool PyDescriptorDatabase::FindFileContainingExtension(
|
||||
const string& containing_type, int field_number,
|
||||
FileDescriptorProto* output) {
|
||||
ScopedPyObjectPtr py_method(
|
||||
PyObject_GetAttrString(py_database_, "FindFileContainingExtension"));
|
||||
if (py_method == NULL) {
|
||||
// This method is not implemented, returns without error.
|
||||
PyErr_Clear();
|
||||
return false;
|
||||
}
|
||||
ScopedPyObjectPtr py_descriptor(
|
||||
PyObject_CallFunction(py_method.get(), "s#i", containing_type.c_str(),
|
||||
containing_type.size(), field_number));
|
||||
return GetFileDescriptorProto(py_descriptor.get(), output);
|
||||
}
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
@@ -0,0 +1,75 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_DATABASE_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_DATABASE_H__
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <google/protobuf/descriptor_database.h>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
class PyDescriptorDatabase : public DescriptorDatabase {
|
||||
public:
|
||||
explicit PyDescriptorDatabase(PyObject* py_database);
|
||||
~PyDescriptorDatabase();
|
||||
|
||||
// Implement the abstract interface. All these functions fill the output
|
||||
// with a copy of FileDescriptorProto.
|
||||
|
||||
// Find a file by file name.
|
||||
bool FindFileByName(const string& filename,
|
||||
FileDescriptorProto* output);
|
||||
|
||||
// Find the file that declares the given fully-qualified symbol name.
|
||||
bool FindFileContainingSymbol(const string& symbol_name,
|
||||
FileDescriptorProto* output);
|
||||
|
||||
// Find the file which defines an extension extending the given message type
|
||||
// with the given field number.
|
||||
// Containing_type must be a fully-qualified type name.
|
||||
// Python objects are not required to implement this method.
|
||||
bool FindFileContainingExtension(const string& containing_type,
|
||||
int field_number,
|
||||
FileDescriptorProto* output);
|
||||
|
||||
private:
|
||||
// The python object that implements the database. The reference is owned.
|
||||
PyObject* py_database_;
|
||||
};
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_DATABASE_H__
|
||||
@@ -0,0 +1,679 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Implements the DescriptorPool, which collects all descriptors.
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <google/protobuf/descriptor.pb.h>
|
||||
#include <google/protobuf/pyext/descriptor.h>
|
||||
#include <google/protobuf/pyext/descriptor_database.h>
|
||||
#include <google/protobuf/pyext/descriptor_pool.h>
|
||||
#include <google/protobuf/pyext/message.h>
|
||||
#include <google/protobuf/pyext/message_factory.h>
|
||||
#include <google/protobuf/pyext/scoped_pyobject_ptr.h>
|
||||
#include <google/protobuf/stubs/hash.h>
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#define PyString_FromStringAndSize PyUnicode_FromStringAndSize
|
||||
#if PY_VERSION_HEX < 0x03030000
|
||||
#error "Python 3.0 - 3.2 are not supported."
|
||||
#endif
|
||||
#define PyString_AsStringAndSize(ob, charpp, sizep) \
|
||||
(PyUnicode_Check(ob)? \
|
||||
((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
|
||||
PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
|
||||
#endif
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
// A map to cache Python Pools per C++ pointer.
|
||||
// Pointers are not owned here, and belong to the PyDescriptorPool.
|
||||
static hash_map<const DescriptorPool*, PyDescriptorPool*> descriptor_pool_map;
|
||||
|
||||
namespace cdescriptor_pool {
|
||||
|
||||
// Create a Python DescriptorPool object, but does not fill the "pool"
|
||||
// attribute.
|
||||
static PyDescriptorPool* _CreateDescriptorPool() {
|
||||
PyDescriptorPool* cpool = PyObject_New(
|
||||
PyDescriptorPool, &PyDescriptorPool_Type);
|
||||
if (cpool == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cpool->underlay = NULL;
|
||||
cpool->database = NULL;
|
||||
|
||||
cpool->descriptor_options =
|
||||
new hash_map<const void*, PyObject *>();
|
||||
|
||||
cpool->py_message_factory = message_factory::NewMessageFactory(
|
||||
&PyMessageFactory_Type, cpool);
|
||||
if (cpool->py_message_factory == NULL) {
|
||||
Py_DECREF(cpool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return cpool;
|
||||
}
|
||||
|
||||
// Create a Python DescriptorPool, using the given pool as an underlay:
|
||||
// new messages will be added to a custom pool, not to the underlay.
|
||||
//
|
||||
// Ownership of the underlay is not transferred, its pointer should
|
||||
// stay alive.
|
||||
static PyDescriptorPool* PyDescriptorPool_NewWithUnderlay(
|
||||
const DescriptorPool* underlay) {
|
||||
PyDescriptorPool* cpool = _CreateDescriptorPool();
|
||||
if (cpool == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
cpool->pool = new DescriptorPool(underlay);
|
||||
cpool->underlay = underlay;
|
||||
|
||||
if (!descriptor_pool_map.insert(
|
||||
std::make_pair(cpool->pool, cpool)).second) {
|
||||
// Should never happen -- would indicate an internal error / bug.
|
||||
PyErr_SetString(PyExc_ValueError, "DescriptorPool already registered");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return cpool;
|
||||
}
|
||||
|
||||
static PyDescriptorPool* PyDescriptorPool_NewWithDatabase(
|
||||
DescriptorDatabase* database) {
|
||||
PyDescriptorPool* cpool = _CreateDescriptorPool();
|
||||
if (cpool == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (database != NULL) {
|
||||
cpool->pool = new DescriptorPool(database);
|
||||
cpool->database = database;
|
||||
} else {
|
||||
cpool->pool = new DescriptorPool();
|
||||
}
|
||||
|
||||
if (!descriptor_pool_map.insert(std::make_pair(cpool->pool, cpool)).second) {
|
||||
// Should never happen -- would indicate an internal error / bug.
|
||||
PyErr_SetString(PyExc_ValueError, "DescriptorPool already registered");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return cpool;
|
||||
}
|
||||
|
||||
// The public DescriptorPool constructor.
|
||||
static PyObject* New(PyTypeObject* type,
|
||||
PyObject* args, PyObject* kwargs) {
|
||||
static char* kwlist[] = {"descriptor_db", 0};
|
||||
PyObject* py_database = NULL;
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &py_database)) {
|
||||
return NULL;
|
||||
}
|
||||
DescriptorDatabase* database = NULL;
|
||||
if (py_database && py_database != Py_None) {
|
||||
database = new PyDescriptorDatabase(py_database);
|
||||
}
|
||||
return reinterpret_cast<PyObject*>(
|
||||
PyDescriptorPool_NewWithDatabase(database));
|
||||
}
|
||||
|
||||
static void Dealloc(PyDescriptorPool* self) {
|
||||
descriptor_pool_map.erase(self->pool);
|
||||
Py_CLEAR(self->py_message_factory);
|
||||
for (hash_map<const void*, PyObject*>::iterator it =
|
||||
self->descriptor_options->begin();
|
||||
it != self->descriptor_options->end(); ++it) {
|
||||
Py_DECREF(it->second);
|
||||
}
|
||||
delete self->descriptor_options;
|
||||
delete self->database;
|
||||
delete self->pool;
|
||||
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
|
||||
}
|
||||
|
||||
PyObject* FindMessageByName(PyDescriptorPool* self, PyObject* arg) {
|
||||
Py_ssize_t name_size;
|
||||
char* name;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const Descriptor* message_descriptor =
|
||||
self->pool->FindMessageTypeByName(string(name, name_size));
|
||||
|
||||
if (message_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find message %.200s", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyMessageDescriptor_FromDescriptor(message_descriptor);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PyObject* FindFileByName(PyDescriptorPool* self, PyObject* arg) {
|
||||
Py_ssize_t name_size;
|
||||
char* name;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const FileDescriptor* file_descriptor =
|
||||
self->pool->FindFileByName(string(name, name_size));
|
||||
if (file_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find file %.200s", name);
|
||||
return NULL;
|
||||
}
|
||||
return PyFileDescriptor_FromDescriptor(file_descriptor);
|
||||
}
|
||||
|
||||
PyObject* FindFieldByName(PyDescriptorPool* self, PyObject* arg) {
|
||||
Py_ssize_t name_size;
|
||||
char* name;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const FieldDescriptor* field_descriptor =
|
||||
self->pool->FindFieldByName(string(name, name_size));
|
||||
if (field_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find field %.200s",
|
||||
name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyFieldDescriptor_FromDescriptor(field_descriptor);
|
||||
}
|
||||
|
||||
PyObject* FindExtensionByName(PyDescriptorPool* self, PyObject* arg) {
|
||||
Py_ssize_t name_size;
|
||||
char* name;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const FieldDescriptor* field_descriptor =
|
||||
self->pool->FindExtensionByName(string(name, name_size));
|
||||
if (field_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find extension field %.200s", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyFieldDescriptor_FromDescriptor(field_descriptor);
|
||||
}
|
||||
|
||||
PyObject* FindEnumTypeByName(PyDescriptorPool* self, PyObject* arg) {
|
||||
Py_ssize_t name_size;
|
||||
char* name;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const EnumDescriptor* enum_descriptor =
|
||||
self->pool->FindEnumTypeByName(string(name, name_size));
|
||||
if (enum_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find enum %.200s", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyEnumDescriptor_FromDescriptor(enum_descriptor);
|
||||
}
|
||||
|
||||
PyObject* FindOneofByName(PyDescriptorPool* self, PyObject* arg) {
|
||||
Py_ssize_t name_size;
|
||||
char* name;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const OneofDescriptor* oneof_descriptor =
|
||||
self->pool->FindOneofByName(string(name, name_size));
|
||||
if (oneof_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find oneof %.200s", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyOneofDescriptor_FromDescriptor(oneof_descriptor);
|
||||
}
|
||||
|
||||
PyObject* FindServiceByName(PyDescriptorPool* self, PyObject* arg) {
|
||||
Py_ssize_t name_size;
|
||||
char* name;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const ServiceDescriptor* service_descriptor =
|
||||
self->pool->FindServiceByName(string(name, name_size));
|
||||
if (service_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find service %.200s", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyServiceDescriptor_FromDescriptor(service_descriptor);
|
||||
}
|
||||
|
||||
PyObject* FindMethodByName(PyDescriptorPool* self, PyObject* arg) {
|
||||
Py_ssize_t name_size;
|
||||
char* name;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const MethodDescriptor* method_descriptor =
|
||||
self->pool->FindMethodByName(string(name, name_size));
|
||||
if (method_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyMethodDescriptor_FromDescriptor(method_descriptor);
|
||||
}
|
||||
|
||||
PyObject* FindFileContainingSymbol(PyDescriptorPool* self, PyObject* arg) {
|
||||
Py_ssize_t name_size;
|
||||
char* name;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const FileDescriptor* file_descriptor =
|
||||
self->pool->FindFileContainingSymbol(string(name, name_size));
|
||||
if (file_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find symbol %.200s", name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyFileDescriptor_FromDescriptor(file_descriptor);
|
||||
}
|
||||
|
||||
PyObject* FindExtensionByNumber(PyDescriptorPool* self, PyObject* args) {
|
||||
PyObject* message_descriptor;
|
||||
int number;
|
||||
if (!PyArg_ParseTuple(args, "Oi", &message_descriptor, &number)) {
|
||||
return NULL;
|
||||
}
|
||||
const Descriptor* descriptor = PyMessageDescriptor_AsDescriptor(
|
||||
message_descriptor);
|
||||
if (descriptor == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const FieldDescriptor* extension_descriptor =
|
||||
self->pool->FindExtensionByNumber(descriptor, number);
|
||||
if (extension_descriptor == NULL) {
|
||||
PyErr_Format(PyExc_KeyError, "Couldn't find extension %d", number);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyFieldDescriptor_FromDescriptor(extension_descriptor);
|
||||
}
|
||||
|
||||
PyObject* FindAllExtensions(PyDescriptorPool* self, PyObject* arg) {
|
||||
const Descriptor* descriptor = PyMessageDescriptor_AsDescriptor(arg);
|
||||
if (descriptor == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
std::vector<const FieldDescriptor*> extensions;
|
||||
self->pool->FindAllExtensions(descriptor, &extensions);
|
||||
|
||||
ScopedPyObjectPtr result(PyList_New(extensions.size()));
|
||||
if (result == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
for (int i = 0; i < extensions.size(); i++) {
|
||||
PyObject* extension = PyFieldDescriptor_FromDescriptor(extensions[i]);
|
||||
if (extension == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyList_SET_ITEM(result.get(), i, extension); // Steals the reference.
|
||||
}
|
||||
return result.release();
|
||||
}
|
||||
|
||||
// These functions should not exist -- the only valid way to create
|
||||
// descriptors is to call Add() or AddSerializedFile().
|
||||
// But these AddDescriptor() functions were created in Python and some people
|
||||
// call them, so we support them for now for compatibility.
|
||||
// However we do check that the existing descriptor already exists in the pool,
|
||||
// which appears to always be true for existing calls -- but then why do people
|
||||
// call a function that will just be a no-op?
|
||||
// TODO(amauryfa): Need to investigate further.
|
||||
|
||||
PyObject* AddFileDescriptor(PyDescriptorPool* self, PyObject* descriptor) {
|
||||
const FileDescriptor* file_descriptor =
|
||||
PyFileDescriptor_AsDescriptor(descriptor);
|
||||
if (!file_descriptor) {
|
||||
return NULL;
|
||||
}
|
||||
if (file_descriptor !=
|
||||
self->pool->FindFileByName(file_descriptor->name())) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"The file descriptor %s does not belong to this pool",
|
||||
file_descriptor->name().c_str());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* AddDescriptor(PyDescriptorPool* self, PyObject* descriptor) {
|
||||
const Descriptor* message_descriptor =
|
||||
PyMessageDescriptor_AsDescriptor(descriptor);
|
||||
if (!message_descriptor) {
|
||||
return NULL;
|
||||
}
|
||||
if (message_descriptor !=
|
||||
self->pool->FindMessageTypeByName(message_descriptor->full_name())) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"The message descriptor %s does not belong to this pool",
|
||||
message_descriptor->full_name().c_str());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* AddEnumDescriptor(PyDescriptorPool* self, PyObject* descriptor) {
|
||||
const EnumDescriptor* enum_descriptor =
|
||||
PyEnumDescriptor_AsDescriptor(descriptor);
|
||||
if (!enum_descriptor) {
|
||||
return NULL;
|
||||
}
|
||||
if (enum_descriptor !=
|
||||
self->pool->FindEnumTypeByName(enum_descriptor->full_name())) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"The enum descriptor %s does not belong to this pool",
|
||||
enum_descriptor->full_name().c_str());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* AddExtensionDescriptor(PyDescriptorPool* self, PyObject* descriptor) {
|
||||
const FieldDescriptor* extension_descriptor =
|
||||
PyFieldDescriptor_AsDescriptor(descriptor);
|
||||
if (!extension_descriptor) {
|
||||
return NULL;
|
||||
}
|
||||
if (extension_descriptor !=
|
||||
self->pool->FindExtensionByName(extension_descriptor->full_name())) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"The extension descriptor %s does not belong to this pool",
|
||||
extension_descriptor->full_name().c_str());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* AddServiceDescriptor(PyDescriptorPool* self, PyObject* descriptor) {
|
||||
const ServiceDescriptor* service_descriptor =
|
||||
PyServiceDescriptor_AsDescriptor(descriptor);
|
||||
if (!service_descriptor) {
|
||||
return NULL;
|
||||
}
|
||||
if (service_descriptor !=
|
||||
self->pool->FindServiceByName(service_descriptor->full_name())) {
|
||||
PyErr_Format(PyExc_ValueError,
|
||||
"The service descriptor %s does not belong to this pool",
|
||||
service_descriptor->full_name().c_str());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// The code below loads new Descriptors from a serialized FileDescriptorProto.
|
||||
|
||||
// Collects errors that occur during proto file building to allow them to be
|
||||
// propagated in the python exception instead of only living in ERROR logs.
|
||||
class BuildFileErrorCollector : public DescriptorPool::ErrorCollector {
|
||||
public:
|
||||
BuildFileErrorCollector() : error_message(""), had_errors(false) {}
|
||||
|
||||
void AddError(const string& filename, const string& element_name,
|
||||
const Message* descriptor, ErrorLocation location,
|
||||
const string& message) {
|
||||
// Replicates the logging behavior that happens in the C++ implementation
|
||||
// when an error collector is not passed in.
|
||||
if (!had_errors) {
|
||||
error_message +=
|
||||
("Invalid proto descriptor for file \"" + filename + "\":\n");
|
||||
had_errors = true;
|
||||
}
|
||||
// As this only happens on failure and will result in the program not
|
||||
// running at all, no effort is made to optimize this string manipulation.
|
||||
error_message += (" " + element_name + ": " + message + "\n");
|
||||
}
|
||||
|
||||
string error_message;
|
||||
bool had_errors;
|
||||
};
|
||||
|
||||
PyObject* AddSerializedFile(PyDescriptorPool* self, PyObject* serialized_pb) {
|
||||
char* message_type;
|
||||
Py_ssize_t message_len;
|
||||
|
||||
if (self->database != NULL) {
|
||||
PyErr_SetString(
|
||||
PyExc_ValueError,
|
||||
"Cannot call Add on a DescriptorPool that uses a DescriptorDatabase. "
|
||||
"Add your file to the underlying database.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (PyBytes_AsStringAndSize(serialized_pb, &message_type, &message_len) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FileDescriptorProto file_proto;
|
||||
if (!file_proto.ParseFromArray(message_type, message_len)) {
|
||||
PyErr_SetString(PyExc_TypeError, "Couldn't parse file content!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// If the file was already part of a C++ library, all its descriptors are in
|
||||
// the underlying pool. No need to do anything else.
|
||||
const FileDescriptor* generated_file = NULL;
|
||||
if (self->underlay) {
|
||||
generated_file = self->underlay->FindFileByName(file_proto.name());
|
||||
}
|
||||
if (generated_file != NULL) {
|
||||
return PyFileDescriptor_FromDescriptorWithSerializedPb(
|
||||
generated_file, serialized_pb);
|
||||
}
|
||||
|
||||
BuildFileErrorCollector error_collector;
|
||||
const FileDescriptor* descriptor =
|
||||
self->pool->BuildFileCollectingErrors(file_proto,
|
||||
&error_collector);
|
||||
if (descriptor == NULL) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"Couldn't build proto file into descriptor pool!\n%s",
|
||||
error_collector.error_message.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyFileDescriptor_FromDescriptorWithSerializedPb(
|
||||
descriptor, serialized_pb);
|
||||
}
|
||||
|
||||
PyObject* Add(PyDescriptorPool* self, PyObject* file_descriptor_proto) {
|
||||
ScopedPyObjectPtr serialized_pb(
|
||||
PyObject_CallMethod(file_descriptor_proto, "SerializeToString", NULL));
|
||||
if (serialized_pb == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return AddSerializedFile(self, serialized_pb.get());
|
||||
}
|
||||
|
||||
static PyMethodDef Methods[] = {
|
||||
{ "Add", (PyCFunction)Add, METH_O,
|
||||
"Adds the FileDescriptorProto and its types to this pool." },
|
||||
{ "AddSerializedFile", (PyCFunction)AddSerializedFile, METH_O,
|
||||
"Adds a serialized FileDescriptorProto to this pool." },
|
||||
|
||||
// TODO(amauryfa): Understand why the Python implementation differs from
|
||||
// this one, ask users to use another API and deprecate these functions.
|
||||
{ "AddFileDescriptor", (PyCFunction)AddFileDescriptor, METH_O,
|
||||
"No-op. Add() must have been called before." },
|
||||
{ "AddDescriptor", (PyCFunction)AddDescriptor, METH_O,
|
||||
"No-op. Add() must have been called before." },
|
||||
{ "AddEnumDescriptor", (PyCFunction)AddEnumDescriptor, METH_O,
|
||||
"No-op. Add() must have been called before." },
|
||||
{ "AddExtensionDescriptor", (PyCFunction)AddExtensionDescriptor, METH_O,
|
||||
"No-op. Add() must have been called before." },
|
||||
{ "AddServiceDescriptor", (PyCFunction)AddServiceDescriptor, METH_O,
|
||||
"No-op. Add() must have been called before." },
|
||||
|
||||
{ "FindFileByName", (PyCFunction)FindFileByName, METH_O,
|
||||
"Searches for a file descriptor by its .proto name." },
|
||||
{ "FindMessageTypeByName", (PyCFunction)FindMessageByName, METH_O,
|
||||
"Searches for a message descriptor by full name." },
|
||||
{ "FindFieldByName", (PyCFunction)FindFieldByName, METH_O,
|
||||
"Searches for a field descriptor by full name." },
|
||||
{ "FindExtensionByName", (PyCFunction)FindExtensionByName, METH_O,
|
||||
"Searches for extension descriptor by full name." },
|
||||
{ "FindEnumTypeByName", (PyCFunction)FindEnumTypeByName, METH_O,
|
||||
"Searches for enum type descriptor by full name." },
|
||||
{ "FindOneofByName", (PyCFunction)FindOneofByName, METH_O,
|
||||
"Searches for oneof descriptor by full name." },
|
||||
{ "FindServiceByName", (PyCFunction)FindServiceByName, METH_O,
|
||||
"Searches for service descriptor by full name." },
|
||||
{ "FindMethodByName", (PyCFunction)FindMethodByName, METH_O,
|
||||
"Searches for method descriptor by full name." },
|
||||
|
||||
{ "FindFileContainingSymbol", (PyCFunction)FindFileContainingSymbol, METH_O,
|
||||
"Gets the FileDescriptor containing the specified symbol." },
|
||||
{ "FindExtensionByNumber", (PyCFunction)FindExtensionByNumber, METH_VARARGS,
|
||||
"Gets the extension descriptor for the given number." },
|
||||
{ "FindAllExtensions", (PyCFunction)FindAllExtensions, METH_O,
|
||||
"Gets all known extensions of the given message descriptor." },
|
||||
{NULL}
|
||||
};
|
||||
|
||||
} // namespace cdescriptor_pool
|
||||
|
||||
PyTypeObject PyDescriptorPool_Type = {
|
||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||
FULL_MODULE_NAME ".DescriptorPool", // tp_name
|
||||
sizeof(PyDescriptorPool), // tp_basicsize
|
||||
0, // tp_itemsize
|
||||
(destructor)cdescriptor_pool::Dealloc, // tp_dealloc
|
||||
0, // tp_print
|
||||
0, // tp_getattr
|
||||
0, // tp_setattr
|
||||
0, // tp_compare
|
||||
0, // tp_repr
|
||||
0, // tp_as_number
|
||||
0, // tp_as_sequence
|
||||
0, // tp_as_mapping
|
||||
0, // tp_hash
|
||||
0, // tp_call
|
||||
0, // tp_str
|
||||
0, // tp_getattro
|
||||
0, // tp_setattro
|
||||
0, // tp_as_buffer
|
||||
Py_TPFLAGS_DEFAULT, // tp_flags
|
||||
"A Descriptor Pool", // tp_doc
|
||||
0, // tp_traverse
|
||||
0, // tp_clear
|
||||
0, // tp_richcompare
|
||||
0, // tp_weaklistoffset
|
||||
0, // tp_iter
|
||||
0, // tp_iternext
|
||||
cdescriptor_pool::Methods, // tp_methods
|
||||
0, // tp_members
|
||||
0, // tp_getset
|
||||
0, // tp_base
|
||||
0, // tp_dict
|
||||
0, // tp_descr_get
|
||||
0, // tp_descr_set
|
||||
0, // tp_dictoffset
|
||||
0, // tp_init
|
||||
0, // tp_alloc
|
||||
cdescriptor_pool::New, // tp_new
|
||||
PyObject_Del, // tp_free
|
||||
};
|
||||
|
||||
// This is the DescriptorPool which contains all the definitions from the
|
||||
// generated _pb2.py modules.
|
||||
static PyDescriptorPool* python_generated_pool = NULL;
|
||||
|
||||
bool InitDescriptorPool() {
|
||||
if (PyType_Ready(&PyDescriptorPool_Type) < 0)
|
||||
return false;
|
||||
|
||||
// The Pool of messages declared in Python libraries.
|
||||
// generated_pool() contains all messages already linked in C++ libraries, and
|
||||
// is used as underlay.
|
||||
python_generated_pool = cdescriptor_pool::PyDescriptorPool_NewWithUnderlay(
|
||||
DescriptorPool::generated_pool());
|
||||
if (python_generated_pool == NULL) {
|
||||
return false;
|
||||
}
|
||||
// Register this pool to be found for C++-generated descriptors.
|
||||
descriptor_pool_map.insert(
|
||||
std::make_pair(DescriptorPool::generated_pool(),
|
||||
python_generated_pool));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// The default DescriptorPool used everywhere in this module.
|
||||
// Today it's the python_generated_pool.
|
||||
// TODO(amauryfa): Remove all usages of this function: the pool should be
|
||||
// derived from the context.
|
||||
PyDescriptorPool* GetDefaultDescriptorPool() {
|
||||
return python_generated_pool;
|
||||
}
|
||||
|
||||
PyDescriptorPool* GetDescriptorPool_FromPool(const DescriptorPool* pool) {
|
||||
// Fast path for standard descriptors.
|
||||
if (pool == python_generated_pool->pool ||
|
||||
pool == DescriptorPool::generated_pool()) {
|
||||
return python_generated_pool;
|
||||
}
|
||||
hash_map<const DescriptorPool*, PyDescriptorPool*>::iterator it =
|
||||
descriptor_pool_map.find(pool);
|
||||
if (it == descriptor_pool_map.end()) {
|
||||
PyErr_SetString(PyExc_KeyError, "Unknown descriptor pool");
|
||||
return NULL;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
@@ -0,0 +1,145 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <google/protobuf/stubs/hash.h>
|
||||
#include <google/protobuf/descriptor.h>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
struct PyMessageFactory;
|
||||
|
||||
// The (meta) type of all Messages classes.
|
||||
struct CMessageClass;
|
||||
|
||||
// Wraps operations to the global DescriptorPool which contains information
|
||||
// about all messages and fields.
|
||||
//
|
||||
// There is normally one pool per process. We make it a Python object only
|
||||
// because it contains many Python references.
|
||||
// TODO(amauryfa): See whether such objects can appear in reference cycles, and
|
||||
// consider adding support for the cyclic GC.
|
||||
//
|
||||
// "Methods" that interacts with this DescriptorPool are in the cdescriptor_pool
|
||||
// namespace.
|
||||
typedef struct PyDescriptorPool {
|
||||
PyObject_HEAD
|
||||
|
||||
// The C++ pool containing Descriptors.
|
||||
DescriptorPool* pool;
|
||||
|
||||
// The C++ pool acting as an underlay. Can be NULL.
|
||||
// This pointer is not owned and must stay alive.
|
||||
const DescriptorPool* underlay;
|
||||
|
||||
// The C++ descriptor database used to fetch unknown protos. Can be NULL.
|
||||
// This pointer is owned.
|
||||
const DescriptorDatabase* database;
|
||||
|
||||
// The preferred MessageFactory to be used by descriptors.
|
||||
// TODO(amauryfa): Don't create the Factory from the DescriptorPool, but
|
||||
// use the one passed while creating message classes. And remove this member.
|
||||
PyMessageFactory* py_message_factory;
|
||||
|
||||
// Cache the options for any kind of descriptor.
|
||||
// Descriptor pointers are owned by the DescriptorPool above.
|
||||
// Python objects are owned by the map.
|
||||
hash_map<const void*, PyObject*>* descriptor_options;
|
||||
} PyDescriptorPool;
|
||||
|
||||
|
||||
extern PyTypeObject PyDescriptorPool_Type;
|
||||
|
||||
namespace cdescriptor_pool {
|
||||
|
||||
|
||||
// Looks up a message by name.
|
||||
// Returns a message Descriptor, or NULL if not found.
|
||||
const Descriptor* FindMessageTypeByName(PyDescriptorPool* self,
|
||||
const string& name);
|
||||
|
||||
// The functions below are also exposed as methods of the DescriptorPool type.
|
||||
|
||||
// Looks up a message by name. Returns a PyMessageDescriptor corresponding to
|
||||
// the field on success, or NULL on failure.
|
||||
//
|
||||
// Returns a new reference.
|
||||
PyObject* FindMessageByName(PyDescriptorPool* self, PyObject* name);
|
||||
|
||||
// Looks up a field by name. Returns a PyFieldDescriptor corresponding to
|
||||
// the field on success, or NULL on failure.
|
||||
//
|
||||
// Returns a new reference.
|
||||
PyObject* FindFieldByName(PyDescriptorPool* self, PyObject* name);
|
||||
|
||||
// Looks up an extension by name. Returns a PyFieldDescriptor corresponding
|
||||
// to the field on success, or NULL on failure.
|
||||
//
|
||||
// Returns a new reference.
|
||||
PyObject* FindExtensionByName(PyDescriptorPool* self, PyObject* arg);
|
||||
|
||||
// Looks up an enum type by name. Returns a PyEnumDescriptor corresponding
|
||||
// to the field on success, or NULL on failure.
|
||||
//
|
||||
// Returns a new reference.
|
||||
PyObject* FindEnumTypeByName(PyDescriptorPool* self, PyObject* arg);
|
||||
|
||||
// Looks up a oneof by name. Returns a COneofDescriptor corresponding
|
||||
// to the oneof on success, or NULL on failure.
|
||||
//
|
||||
// Returns a new reference.
|
||||
PyObject* FindOneofByName(PyDescriptorPool* self, PyObject* arg);
|
||||
|
||||
} // namespace cdescriptor_pool
|
||||
|
||||
// Retrieve the global descriptor pool owned by the _message module.
|
||||
// This is the one used by pb2.py generated modules.
|
||||
// Returns a *borrowed* reference.
|
||||
// "Default" pool used to register messages from _pb2.py modules.
|
||||
PyDescriptorPool* GetDefaultDescriptorPool();
|
||||
|
||||
// Retrieve the python descriptor pool owning a C++ descriptor pool.
|
||||
// Returns a *borrowed* reference.
|
||||
PyDescriptorPool* GetDescriptorPool_FromPool(const DescriptorPool* pool);
|
||||
|
||||
// Initialize objects used by this module.
|
||||
bool InitDescriptorPool();
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_POOL_H__
|
||||
@@ -0,0 +1,306 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: anuraag@google.com (Anuraag Agrawal)
|
||||
// Author: tibell@google.com (Johan Tibell)
|
||||
|
||||
#include <google/protobuf/pyext/extension_dict.h>
|
||||
|
||||
#include <google/protobuf/stubs/logging.h>
|
||||
#include <google/protobuf/stubs/common.h>
|
||||
#include <google/protobuf/descriptor.h>
|
||||
#include <google/protobuf/dynamic_message.h>
|
||||
#include <google/protobuf/message.h>
|
||||
#include <google/protobuf/descriptor.pb.h>
|
||||
#include <google/protobuf/pyext/descriptor.h>
|
||||
#include <google/protobuf/pyext/message.h>
|
||||
#include <google/protobuf/pyext/message_factory.h>
|
||||
#include <google/protobuf/pyext/repeated_composite_container.h>
|
||||
#include <google/protobuf/pyext/repeated_scalar_container.h>
|
||||
#include <google/protobuf/pyext/scoped_pyobject_ptr.h>
|
||||
#include <google/protobuf/stubs/shared_ptr.h>
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#if PY_VERSION_HEX < 0x03030000
|
||||
#error "Python 3.0 - 3.2 are not supported."
|
||||
#endif
|
||||
#define PyString_AsStringAndSize(ob, charpp, sizep) \
|
||||
(PyUnicode_Check(ob)? \
|
||||
((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
|
||||
PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
|
||||
#endif
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
namespace extension_dict {
|
||||
|
||||
PyObject* len(ExtensionDict* self) {
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
return PyLong_FromLong(PyDict_Size(self->values));
|
||||
#else
|
||||
return PyInt_FromLong(PyDict_Size(self->values));
|
||||
#endif
|
||||
}
|
||||
|
||||
PyObject* subscript(ExtensionDict* self, PyObject* key) {
|
||||
const FieldDescriptor* descriptor = cmessage::GetExtensionDescriptor(key);
|
||||
if (descriptor == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (!CheckFieldBelongsToMessage(descriptor, self->message)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (descriptor->label() != FieldDescriptor::LABEL_REPEATED &&
|
||||
descriptor->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
|
||||
return cmessage::InternalGetScalar(self->message, descriptor);
|
||||
}
|
||||
|
||||
PyObject* value = PyDict_GetItem(self->values, key);
|
||||
if (value != NULL) {
|
||||
Py_INCREF(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
if (self->parent == NULL) {
|
||||
// We are in "detached" state. Don't allow further modifications.
|
||||
// TODO(amauryfa): Support adding non-scalars to a detached extension dict.
|
||||
// This probably requires to store the type of the main message.
|
||||
PyErr_SetObject(PyExc_KeyError, key);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (descriptor->label() != FieldDescriptor::LABEL_REPEATED &&
|
||||
descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
|
||||
// TODO(plabatut): consider building the class on the fly!
|
||||
PyObject* sub_message = cmessage::InternalGetSubMessage(
|
||||
self->parent, descriptor);
|
||||
if (sub_message == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyDict_SetItem(self->values, key, sub_message);
|
||||
return sub_message;
|
||||
}
|
||||
|
||||
if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) {
|
||||
if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
|
||||
// On the fly message class creation is needed to support the following
|
||||
// situation:
|
||||
// 1- add FileDescriptor to the pool that contains extensions of a message
|
||||
// defined by another proto file. Do not create any message classes.
|
||||
// 2- instantiate an extended message, and access the extension using
|
||||
// the field descriptor.
|
||||
// 3- the extension submessage fails to be returned, because no class has
|
||||
// been created.
|
||||
// It happens when deserializing text proto format, or when enumerating
|
||||
// fields of a deserialized message.
|
||||
CMessageClass* message_class = message_factory::GetOrCreateMessageClass(
|
||||
cmessage::GetFactoryForMessage(self->parent),
|
||||
descriptor->message_type());
|
||||
ScopedPyObjectPtr message_class_handler(
|
||||
reinterpret_cast<PyObject*>(message_class));
|
||||
if (message_class == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyObject* py_container = repeated_composite_container::NewContainer(
|
||||
self->parent, descriptor, message_class);
|
||||
if (py_container == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyDict_SetItem(self->values, key, py_container);
|
||||
return py_container;
|
||||
} else {
|
||||
PyObject* py_container = repeated_scalar_container::NewContainer(
|
||||
self->parent, descriptor);
|
||||
if (py_container == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyDict_SetItem(self->values, key, py_container);
|
||||
return py_container;
|
||||
}
|
||||
}
|
||||
PyErr_SetString(PyExc_ValueError, "control reached unexpected line");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ass_subscript(ExtensionDict* self, PyObject* key, PyObject* value) {
|
||||
const FieldDescriptor* descriptor = cmessage::GetExtensionDescriptor(key);
|
||||
if (descriptor == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (!CheckFieldBelongsToMessage(descriptor, self->message)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (descriptor->label() != FieldDescriptor::LABEL_OPTIONAL ||
|
||||
descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
|
||||
PyErr_SetString(PyExc_TypeError, "Extension is repeated and/or composite "
|
||||
"type");
|
||||
return -1;
|
||||
}
|
||||
if (self->parent) {
|
||||
cmessage::AssureWritable(self->parent);
|
||||
if (cmessage::InternalSetScalar(self->parent, descriptor, value) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// TODO(tibell): We shouldn't write scalars to the cache.
|
||||
PyDict_SetItem(self->values, key, value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* _FindExtensionByName(ExtensionDict* self, PyObject* arg) {
|
||||
char* name;
|
||||
Py_ssize_t name_size;
|
||||
if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyDescriptorPool* pool = cmessage::GetFactoryForMessage(self->parent)->pool;
|
||||
const FieldDescriptor* message_extension =
|
||||
pool->pool->FindExtensionByName(string(name, name_size));
|
||||
if (message_extension == NULL) {
|
||||
// Is is the name of a message set extension?
|
||||
const Descriptor* message_descriptor = pool->pool->FindMessageTypeByName(
|
||||
string(name, name_size));
|
||||
if (message_descriptor && message_descriptor->extension_count() > 0) {
|
||||
const FieldDescriptor* extension = message_descriptor->extension(0);
|
||||
if (extension->is_extension() &&
|
||||
extension->containing_type()->options().message_set_wire_format() &&
|
||||
extension->type() == FieldDescriptor::TYPE_MESSAGE &&
|
||||
extension->label() == FieldDescriptor::LABEL_OPTIONAL) {
|
||||
message_extension = extension;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (message_extension == NULL) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
return PyFieldDescriptor_FromDescriptor(message_extension);
|
||||
}
|
||||
|
||||
PyObject* _FindExtensionByNumber(ExtensionDict* self, PyObject* arg) {
|
||||
int64 number = PyLong_AsLong(arg);
|
||||
if (number == -1 && PyErr_Occurred()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyDescriptorPool* pool = cmessage::GetFactoryForMessage(self->parent)->pool;
|
||||
const FieldDescriptor* message_extension = pool->pool->FindExtensionByNumber(
|
||||
self->parent->message->GetDescriptor(), number);
|
||||
if (message_extension == NULL) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
return PyFieldDescriptor_FromDescriptor(message_extension);
|
||||
}
|
||||
|
||||
ExtensionDict* NewExtensionDict(CMessage *parent) {
|
||||
ExtensionDict* self = reinterpret_cast<ExtensionDict*>(
|
||||
PyType_GenericAlloc(&ExtensionDict_Type, 0));
|
||||
if (self == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
self->parent = parent; // Store a borrowed reference.
|
||||
self->message = parent->message;
|
||||
self->owner = parent->owner;
|
||||
self->values = PyDict_New();
|
||||
return self;
|
||||
}
|
||||
|
||||
void dealloc(ExtensionDict* self) {
|
||||
Py_CLEAR(self->values);
|
||||
self->owner.reset();
|
||||
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
|
||||
}
|
||||
|
||||
static PyMappingMethods MpMethods = {
|
||||
(lenfunc)len, /* mp_length */
|
||||
(binaryfunc)subscript, /* mp_subscript */
|
||||
(objobjargproc)ass_subscript,/* mp_ass_subscript */
|
||||
};
|
||||
|
||||
#define EDMETHOD(name, args, doc) { #name, (PyCFunction)name, args, doc }
|
||||
static PyMethodDef Methods[] = {
|
||||
EDMETHOD(_FindExtensionByName, METH_O,
|
||||
"Finds an extension by name."),
|
||||
EDMETHOD(_FindExtensionByNumber, METH_O,
|
||||
"Finds an extension by field number."),
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
} // namespace extension_dict
|
||||
|
||||
PyTypeObject ExtensionDict_Type = {
|
||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||
FULL_MODULE_NAME ".ExtensionDict", // tp_name
|
||||
sizeof(ExtensionDict), // tp_basicsize
|
||||
0, // tp_itemsize
|
||||
(destructor)extension_dict::dealloc, // tp_dealloc
|
||||
0, // tp_print
|
||||
0, // tp_getattr
|
||||
0, // tp_setattr
|
||||
0, // tp_compare
|
||||
0, // tp_repr
|
||||
0, // tp_as_number
|
||||
0, // tp_as_sequence
|
||||
&extension_dict::MpMethods, // tp_as_mapping
|
||||
PyObject_HashNotImplemented, // tp_hash
|
||||
0, // tp_call
|
||||
0, // tp_str
|
||||
0, // tp_getattro
|
||||
0, // tp_setattro
|
||||
0, // tp_as_buffer
|
||||
Py_TPFLAGS_DEFAULT, // tp_flags
|
||||
"An extension dict", // tp_doc
|
||||
0, // tp_traverse
|
||||
0, // tp_clear
|
||||
0, // tp_richcompare
|
||||
0, // tp_weaklistoffset
|
||||
0, // tp_iter
|
||||
0, // tp_iternext
|
||||
extension_dict::Methods, // tp_methods
|
||||
0, // tp_members
|
||||
0, // tp_getset
|
||||
0, // tp_base
|
||||
0, // tp_dict
|
||||
0, // tp_descr_get
|
||||
0, // tp_descr_set
|
||||
0, // tp_dictoffset
|
||||
0, // tp_init
|
||||
};
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
@@ -0,0 +1,94 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: anuraag@google.com (Anuraag Agrawal)
|
||||
// Author: tibell@google.com (Johan Tibell)
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_H__
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <memory>
|
||||
#ifndef _SHARED_PTR_H
|
||||
#include <google/protobuf/stubs/shared_ptr.h>
|
||||
#endif
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
|
||||
class Message;
|
||||
class FieldDescriptor;
|
||||
|
||||
#ifdef _SHARED_PTR_H
|
||||
using std::shared_ptr;
|
||||
#else
|
||||
using internal::shared_ptr;
|
||||
#endif
|
||||
|
||||
namespace python {
|
||||
|
||||
struct CMessage;
|
||||
|
||||
typedef struct ExtensionDict {
|
||||
PyObject_HEAD;
|
||||
|
||||
// This is the top-level C++ Message object that owns the whole
|
||||
// proto tree. Every Python container class holds a
|
||||
// reference to it in order to keep it alive as long as there's a
|
||||
// Python object that references any part of the tree.
|
||||
shared_ptr<Message> owner;
|
||||
|
||||
// Weak reference to parent message. Used to make sure
|
||||
// the parent is writable when an extension field is modified.
|
||||
CMessage* parent;
|
||||
|
||||
// Pointer to the C++ Message that this ExtensionDict extends.
|
||||
// Not owned by us.
|
||||
Message* message;
|
||||
|
||||
// A dict of child messages, indexed by Extension descriptors.
|
||||
// Similar to CMessage::composite_fields.
|
||||
PyObject* values;
|
||||
} ExtensionDict;
|
||||
|
||||
extern PyTypeObject ExtensionDict_Type;
|
||||
|
||||
namespace extension_dict {
|
||||
|
||||
// Builds an Extensions dict for a specific message.
|
||||
ExtensionDict* NewExtensionDict(CMessage *parent);
|
||||
|
||||
} // namespace extension_dict
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_EXTENSION_DICT_H__
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,136 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <memory>
|
||||
#ifndef _SHARED_PTR_H
|
||||
#include <google/protobuf/stubs/shared_ptr.h>
|
||||
#endif
|
||||
|
||||
#include <google/protobuf/descriptor.h>
|
||||
#include <google/protobuf/message.h>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
|
||||
class Message;
|
||||
|
||||
#ifdef _SHARED_PTR_H
|
||||
using std::shared_ptr;
|
||||
#else
|
||||
using internal::shared_ptr;
|
||||
#endif
|
||||
|
||||
namespace python {
|
||||
|
||||
struct CMessage;
|
||||
struct CMessageClass;
|
||||
|
||||
// This struct is used directly for ScalarMap, and is the base class of
|
||||
// MessageMapContainer, which is used for MessageMap.
|
||||
struct MapContainer {
|
||||
PyObject_HEAD;
|
||||
|
||||
// This is the top-level C++ Message object that owns the whole
|
||||
// proto tree. Every Python MapContainer holds a
|
||||
// reference to it in order to keep it alive as long as there's a
|
||||
// Python object that references any part of the tree.
|
||||
shared_ptr<Message> owner;
|
||||
|
||||
// Pointer to the C++ Message that contains this container. The
|
||||
// MapContainer does not own this pointer.
|
||||
const Message* message;
|
||||
|
||||
// Use to get a mutable message when necessary.
|
||||
Message* GetMutableMessage();
|
||||
|
||||
// Weak reference to a parent CMessage object (i.e. may be NULL.)
|
||||
//
|
||||
// Used to make sure all ancestors are also mutable when first
|
||||
// modifying the container.
|
||||
CMessage* parent;
|
||||
|
||||
// Pointer to the parent's descriptor that describes this
|
||||
// field. Used together with the parent's message when making a
|
||||
// default message instance mutable.
|
||||
// The pointer is owned by the global DescriptorPool.
|
||||
const FieldDescriptor* parent_field_descriptor;
|
||||
const FieldDescriptor* key_field_descriptor;
|
||||
const FieldDescriptor* value_field_descriptor;
|
||||
|
||||
// We bump this whenever we perform a mutation, to invalidate existing
|
||||
// iterators.
|
||||
uint64 version;
|
||||
|
||||
// Releases the messages in the container to a new message.
|
||||
//
|
||||
// Returns 0 on success, -1 on failure.
|
||||
int Release();
|
||||
|
||||
// Set the owner field of self and any children of self.
|
||||
void SetOwner(const shared_ptr<Message>& new_owner) {
|
||||
owner = new_owner;
|
||||
}
|
||||
};
|
||||
|
||||
struct MessageMapContainer : public MapContainer {
|
||||
// The type used to create new child messages.
|
||||
CMessageClass* message_class;
|
||||
|
||||
// A dict mapping Message* -> CMessage.
|
||||
PyObject* message_dict;
|
||||
};
|
||||
|
||||
bool InitMapContainers();
|
||||
|
||||
extern PyTypeObject* MessageMapContainer_Type;
|
||||
extern PyTypeObject* ScalarMapContainer_Type;
|
||||
extern PyTypeObject MapIterator_Type; // Both map types use the same iterator.
|
||||
|
||||
// Builds a MapContainer object, from a parent message and a
|
||||
// field descriptor.
|
||||
extern PyObject* NewScalarMapContainer(
|
||||
CMessage* parent, const FieldDescriptor* parent_field_descriptor);
|
||||
|
||||
// Builds a MessageMap object, from a parent message and a
|
||||
// field descriptor.
|
||||
extern PyObject* NewMessageMapContainer(
|
||||
CMessage* parent, const FieldDescriptor* parent_field_descriptor,
|
||||
CMessageClass* message_class);
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_MAP_CONTAINER_H__
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,354 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: anuraag@google.com (Anuraag Agrawal)
|
||||
// Author: tibell@google.com (Johan Tibell)
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_H__
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <memory>
|
||||
#ifndef _SHARED_PTR_H
|
||||
#include <google/protobuf/stubs/shared_ptr.h>
|
||||
#endif
|
||||
#include <string>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
|
||||
class Message;
|
||||
class Reflection;
|
||||
class FieldDescriptor;
|
||||
class Descriptor;
|
||||
class DescriptorPool;
|
||||
class MessageFactory;
|
||||
|
||||
#ifdef _SHARED_PTR_H
|
||||
using std::shared_ptr;
|
||||
using std::string;
|
||||
#else
|
||||
using internal::shared_ptr;
|
||||
#endif
|
||||
|
||||
namespace python {
|
||||
|
||||
struct ExtensionDict;
|
||||
struct PyMessageFactory;
|
||||
|
||||
typedef struct CMessage {
|
||||
PyObject_HEAD;
|
||||
|
||||
// This is the top-level C++ Message object that owns the whole
|
||||
// proto tree. Every Python CMessage holds a reference to it in
|
||||
// order to keep it alive as long as there's a Python object that
|
||||
// references any part of the tree.
|
||||
shared_ptr<Message> owner;
|
||||
|
||||
// Weak reference to a parent CMessage object. This is NULL for any top-level
|
||||
// message and is set for any child message (i.e. a child submessage or a
|
||||
// part of a repeated composite field).
|
||||
//
|
||||
// Used to make sure all ancestors are also mutable when first modifying
|
||||
// a child submessage (in other words, turning a default message instance
|
||||
// into a mutable one).
|
||||
//
|
||||
// If a submessage is released (becomes a new top-level message), this field
|
||||
// MUST be set to NULL. The parent may get deallocated and further attempts
|
||||
// to use this pointer will result in a crash.
|
||||
struct CMessage* parent;
|
||||
|
||||
// Pointer to the parent's descriptor that describes this submessage.
|
||||
// Used together with the parent's message when making a default message
|
||||
// instance mutable.
|
||||
// The pointer is owned by the global DescriptorPool.
|
||||
const FieldDescriptor* parent_field_descriptor;
|
||||
|
||||
// Pointer to the C++ Message object for this CMessage. The
|
||||
// CMessage does not own this pointer.
|
||||
Message* message;
|
||||
|
||||
// Indicates this submessage is pointing to a default instance of a message.
|
||||
// Submessages are always first created as read only messages and are then
|
||||
// made writable, at which point this field is set to false.
|
||||
bool read_only;
|
||||
|
||||
// A reference to a Python dictionary containing CMessage,
|
||||
// RepeatedCompositeContainer, and RepeatedScalarContainer
|
||||
// objects. Used as a cache to make sure we don't have to make a
|
||||
// Python wrapper for the C++ Message objects on every access, or
|
||||
// deal with the synchronization nightmare that could create.
|
||||
PyObject* composite_fields;
|
||||
|
||||
// A reference to the dictionary containing the message's extensions.
|
||||
// Similar to composite_fields, acting as a cache, but also contains the
|
||||
// required extension dict logic.
|
||||
ExtensionDict* extensions;
|
||||
|
||||
// Implements the "weakref" protocol for this object.
|
||||
PyObject* weakreflist;
|
||||
} CMessage;
|
||||
|
||||
extern PyTypeObject CMessageClass_Type;
|
||||
extern PyTypeObject CMessage_Type;
|
||||
|
||||
|
||||
// The (meta) type of all Messages classes.
|
||||
// It allows us to cache some C++ pointers in the class object itself, they are
|
||||
// faster to extract than from the type's dictionary.
|
||||
|
||||
struct CMessageClass {
|
||||
// This is how CPython subclasses C structures: the base structure must be
|
||||
// the first member of the object.
|
||||
PyHeapTypeObject super;
|
||||
|
||||
// C++ descriptor of this message.
|
||||
const Descriptor* message_descriptor;
|
||||
|
||||
// Owned reference, used to keep the pointer above alive.
|
||||
PyObject* py_message_descriptor;
|
||||
|
||||
// The Python MessageFactory used to create the class. It is needed to resolve
|
||||
// fields descriptors, including extensions fields; its C++ MessageFactory is
|
||||
// used to instantiate submessages.
|
||||
// We own the reference, because it's important to keep the factory alive.
|
||||
PyMessageFactory* py_message_factory;
|
||||
|
||||
PyObject* AsPyObject() {
|
||||
return reinterpret_cast<PyObject*>(this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
namespace cmessage {
|
||||
|
||||
// Internal function to create a new empty Message Python object, but with empty
|
||||
// pointers to the C++ objects.
|
||||
// The caller must fill self->message, self->owner and eventually self->parent.
|
||||
CMessage* NewEmptyMessage(CMessageClass* type);
|
||||
|
||||
// Retrieves the C++ descriptor of a Python Extension descriptor.
|
||||
// On error, return NULL with an exception set.
|
||||
const FieldDescriptor* GetExtensionDescriptor(PyObject* extension);
|
||||
|
||||
// Initializes a new CMessage instance for a submessage. Only called once per
|
||||
// submessage as the result is cached in composite_fields.
|
||||
//
|
||||
// Corresponds to reflection api method GetMessage.
|
||||
PyObject* InternalGetSubMessage(
|
||||
CMessage* self, const FieldDescriptor* field_descriptor);
|
||||
|
||||
// Deletes a range of C++ submessages in a repeated field (following a
|
||||
// removal in a RepeatedCompositeContainer).
|
||||
//
|
||||
// Releases messages to the provided cmessage_list if it is not NULL rather
|
||||
// than just removing them from the underlying proto. This cmessage_list must
|
||||
// have a CMessage for each underlying submessage. The CMessages referred to
|
||||
// by slice will be removed from cmessage_list by this function.
|
||||
//
|
||||
// Corresponds to reflection api method RemoveLast.
|
||||
int InternalDeleteRepeatedField(CMessage* self,
|
||||
const FieldDescriptor* field_descriptor,
|
||||
PyObject* slice, PyObject* cmessage_list);
|
||||
|
||||
// Sets the specified scalar value to the message.
|
||||
int InternalSetScalar(CMessage* self,
|
||||
const FieldDescriptor* field_descriptor,
|
||||
PyObject* value);
|
||||
|
||||
// Sets the specified scalar value to the message. Requires it is not a Oneof.
|
||||
int InternalSetNonOneofScalar(Message* message,
|
||||
const FieldDescriptor* field_descriptor,
|
||||
PyObject* arg);
|
||||
|
||||
// Retrieves the specified scalar value from the message.
|
||||
//
|
||||
// Returns a new python reference.
|
||||
PyObject* InternalGetScalar(const Message* message,
|
||||
const FieldDescriptor* field_descriptor);
|
||||
|
||||
// Clears the message, removing all contained data. Extension dictionary and
|
||||
// submessages are released first if there are remaining external references.
|
||||
//
|
||||
// Corresponds to message api method Clear.
|
||||
PyObject* Clear(CMessage* self);
|
||||
|
||||
// Clears the data described by the given descriptor. Used to clear extensions
|
||||
// (which don't have names). Extension release is handled by ExtensionDict
|
||||
// class, not this function.
|
||||
// TODO(anuraag): Try to make this discrepancy in release semantics with
|
||||
// ClearField less confusing.
|
||||
//
|
||||
// Corresponds to reflection api method ClearField.
|
||||
PyObject* ClearFieldByDescriptor(
|
||||
CMessage* self, const FieldDescriptor* descriptor);
|
||||
|
||||
// Clears the data for the given field name. The message is released if there
|
||||
// are any external references.
|
||||
//
|
||||
// Corresponds to reflection api method ClearField.
|
||||
PyObject* ClearField(CMessage* self, PyObject* arg);
|
||||
|
||||
// Checks if the message has the field described by the descriptor. Used for
|
||||
// extensions (which have no name).
|
||||
//
|
||||
// Corresponds to reflection api method HasField
|
||||
PyObject* HasFieldByDescriptor(
|
||||
CMessage* self, const FieldDescriptor* field_descriptor);
|
||||
|
||||
// Checks if the message has the named field.
|
||||
//
|
||||
// Corresponds to reflection api method HasField.
|
||||
PyObject* HasField(CMessage* self, PyObject* arg);
|
||||
|
||||
// Initializes values of fields on a newly constructed message.
|
||||
// Note that positional arguments are disallowed: 'args' must be NULL or the
|
||||
// empty tuple.
|
||||
int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs);
|
||||
|
||||
PyObject* MergeFrom(CMessage* self, PyObject* arg);
|
||||
|
||||
// This method does not do anything beyond checking that no other extension
|
||||
// has been registered with the same field number on this class.
|
||||
PyObject* RegisterExtension(PyObject* cls, PyObject* extension_handle);
|
||||
|
||||
// Retrieves an attribute named 'name' from CMessage 'self'. Returns
|
||||
// the attribute value on success, or NULL on failure.
|
||||
//
|
||||
// Returns a new reference.
|
||||
PyObject* GetAttr(CMessage* self, PyObject* name);
|
||||
|
||||
// Set the value of the attribute named 'name', for CMessage 'self',
|
||||
// to the value 'value'. Returns -1 on failure.
|
||||
int SetAttr(CMessage* self, PyObject* name, PyObject* value);
|
||||
|
||||
PyObject* FindInitializationErrors(CMessage* self);
|
||||
|
||||
// Set the owner field of self and any children of self, recursively.
|
||||
// Used when self is being released and thus has a new owner (the
|
||||
// released Message.)
|
||||
int SetOwner(CMessage* self, const shared_ptr<Message>& new_owner);
|
||||
|
||||
int AssureWritable(CMessage* self);
|
||||
|
||||
// Returns the message factory for the given message.
|
||||
// This is equivalent to message.MESSAGE_FACTORY
|
||||
//
|
||||
// The returned factory is suitable for finding fields and building submessages,
|
||||
// even in the case of extensions.
|
||||
// Returns a *borrowed* reference, and never fails because we pass a CMessage.
|
||||
PyMessageFactory* GetFactoryForMessage(CMessage* message);
|
||||
|
||||
PyObject* SetAllowOversizeProtos(PyObject* m, PyObject* arg);
|
||||
|
||||
} // namespace cmessage
|
||||
|
||||
|
||||
/* Is 64bit */
|
||||
#define IS_64BIT (SIZEOF_LONG == 8)
|
||||
|
||||
#define FIELD_IS_REPEATED(field_descriptor) \
|
||||
((field_descriptor)->label() == FieldDescriptor::LABEL_REPEATED)
|
||||
|
||||
#define GOOGLE_CHECK_GET_INT32(arg, value, err) \
|
||||
int32 value; \
|
||||
if (!CheckAndGetInteger(arg, &value)) { \
|
||||
return err; \
|
||||
}
|
||||
|
||||
#define GOOGLE_CHECK_GET_INT64(arg, value, err) \
|
||||
int64 value; \
|
||||
if (!CheckAndGetInteger(arg, &value)) { \
|
||||
return err; \
|
||||
}
|
||||
|
||||
#define GOOGLE_CHECK_GET_UINT32(arg, value, err) \
|
||||
uint32 value; \
|
||||
if (!CheckAndGetInteger(arg, &value)) { \
|
||||
return err; \
|
||||
}
|
||||
|
||||
#define GOOGLE_CHECK_GET_UINT64(arg, value, err) \
|
||||
uint64 value; \
|
||||
if (!CheckAndGetInteger(arg, &value)) { \
|
||||
return err; \
|
||||
}
|
||||
|
||||
#define GOOGLE_CHECK_GET_FLOAT(arg, value, err) \
|
||||
float value; \
|
||||
if (!CheckAndGetFloat(arg, &value)) { \
|
||||
return err; \
|
||||
} \
|
||||
|
||||
#define GOOGLE_CHECK_GET_DOUBLE(arg, value, err) \
|
||||
double value; \
|
||||
if (!CheckAndGetDouble(arg, &value)) { \
|
||||
return err; \
|
||||
}
|
||||
|
||||
#define GOOGLE_CHECK_GET_BOOL(arg, value, err) \
|
||||
bool value; \
|
||||
if (!CheckAndGetBool(arg, &value)) { \
|
||||
return err; \
|
||||
}
|
||||
|
||||
|
||||
#define FULL_MODULE_NAME "google.protobuf.pyext._message"
|
||||
|
||||
void FormatTypeError(PyObject* arg, char* expected_types);
|
||||
template<class T>
|
||||
bool CheckAndGetInteger(PyObject* arg, T* value);
|
||||
bool CheckAndGetDouble(PyObject* arg, double* value);
|
||||
bool CheckAndGetFloat(PyObject* arg, float* value);
|
||||
bool CheckAndGetBool(PyObject* arg, bool* value);
|
||||
PyObject* CheckString(PyObject* arg, const FieldDescriptor* descriptor);
|
||||
bool CheckAndSetString(
|
||||
PyObject* arg, Message* message,
|
||||
const FieldDescriptor* descriptor,
|
||||
const Reflection* reflection,
|
||||
bool append,
|
||||
int index);
|
||||
PyObject* ToStringObject(const FieldDescriptor* descriptor, string value);
|
||||
|
||||
// Check if the passed field descriptor belongs to the given message.
|
||||
// If not, return false and set a Python exception (a KeyError)
|
||||
bool CheckFieldBelongsToMessage(const FieldDescriptor* field_descriptor,
|
||||
const Message* message);
|
||||
|
||||
extern PyObject* PickleError_class;
|
||||
|
||||
bool InitProto2MessageModule(PyObject *m);
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_H__
|
||||
@@ -0,0 +1,280 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <google/protobuf/dynamic_message.h>
|
||||
#include <google/protobuf/pyext/descriptor.h>
|
||||
#include <google/protobuf/pyext/message.h>
|
||||
#include <google/protobuf/pyext/message_factory.h>
|
||||
#include <google/protobuf/pyext/scoped_pyobject_ptr.h>
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#if PY_VERSION_HEX < 0x03030000
|
||||
#error "Python 3.0 - 3.2 are not supported."
|
||||
#endif
|
||||
#define PyString_AsStringAndSize(ob, charpp, sizep) \
|
||||
(PyUnicode_Check(ob)? \
|
||||
((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
|
||||
PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
|
||||
#endif
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
namespace message_factory {
|
||||
|
||||
PyMessageFactory* NewMessageFactory(PyTypeObject* type, PyDescriptorPool* pool) {
|
||||
PyMessageFactory* factory = reinterpret_cast<PyMessageFactory*>(
|
||||
PyType_GenericAlloc(type, 0));
|
||||
if (factory == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DynamicMessageFactory* message_factory = new DynamicMessageFactory();
|
||||
// This option might be the default some day.
|
||||
message_factory->SetDelegateToGeneratedFactory(true);
|
||||
factory->message_factory = message_factory;
|
||||
|
||||
factory->pool = pool;
|
||||
// TODO(amauryfa): When the MessageFactory is not created from the
|
||||
// DescriptorPool this reference should be owned, not borrowed.
|
||||
// Py_INCREF(pool);
|
||||
|
||||
factory->classes_by_descriptor = new PyMessageFactory::ClassesByMessageMap();
|
||||
|
||||
return factory;
|
||||
}
|
||||
|
||||
PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
|
||||
static char* kwlist[] = {"pool", 0};
|
||||
PyObject* pool = NULL;
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &pool)) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr owned_pool;
|
||||
if (pool == NULL || pool == Py_None) {
|
||||
owned_pool.reset(PyObject_CallFunction(
|
||||
reinterpret_cast<PyObject*>(&PyDescriptorPool_Type), NULL));
|
||||
if (owned_pool == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
pool = owned_pool.get();
|
||||
} else {
|
||||
if (!PyObject_TypeCheck(pool, &PyDescriptorPool_Type)) {
|
||||
PyErr_Format(PyExc_TypeError, "Expected a DescriptorPool, got %s",
|
||||
pool->ob_type->tp_name);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return reinterpret_cast<PyObject*>(
|
||||
NewMessageFactory(type, reinterpret_cast<PyDescriptorPool*>(pool)));
|
||||
}
|
||||
|
||||
static void Dealloc(PyMessageFactory* self) {
|
||||
// TODO(amauryfa): When the MessageFactory is not created from the
|
||||
// DescriptorPool this reference should be owned, not borrowed.
|
||||
// Py_CLEAR(self->pool);
|
||||
typedef PyMessageFactory::ClassesByMessageMap::iterator iterator;
|
||||
for (iterator it = self->classes_by_descriptor->begin();
|
||||
it != self->classes_by_descriptor->end(); ++it) {
|
||||
Py_DECREF(it->second);
|
||||
}
|
||||
delete self->classes_by_descriptor;
|
||||
delete self->message_factory;
|
||||
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
|
||||
}
|
||||
|
||||
// Add a message class to our database.
|
||||
int RegisterMessageClass(PyMessageFactory* self,
|
||||
const Descriptor* message_descriptor,
|
||||
CMessageClass* message_class) {
|
||||
Py_INCREF(message_class);
|
||||
typedef PyMessageFactory::ClassesByMessageMap::iterator iterator;
|
||||
std::pair<iterator, bool> ret = self->classes_by_descriptor->insert(
|
||||
std::make_pair(message_descriptor, message_class));
|
||||
if (!ret.second) {
|
||||
// Update case: DECREF the previous value.
|
||||
Py_DECREF(ret.first->second);
|
||||
ret.first->second = message_class;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
CMessageClass* GetOrCreateMessageClass(PyMessageFactory* self,
|
||||
const Descriptor* descriptor) {
|
||||
// This is the same implementation as MessageFactory.GetPrototype().
|
||||
ScopedPyObjectPtr py_descriptor(
|
||||
PyMessageDescriptor_FromDescriptor(descriptor));
|
||||
if (py_descriptor == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
// Do not create a MessageClass that already exists.
|
||||
hash_map<const Descriptor*, CMessageClass*>::iterator it =
|
||||
self->classes_by_descriptor->find(descriptor);
|
||||
if (it != self->classes_by_descriptor->end()) {
|
||||
Py_INCREF(it->second);
|
||||
return it->second;
|
||||
}
|
||||
// Create a new message class.
|
||||
ScopedPyObjectPtr args(Py_BuildValue(
|
||||
"s(){sOsOsO}", descriptor->name().c_str(),
|
||||
"DESCRIPTOR", py_descriptor.get(),
|
||||
"__module__", Py_None,
|
||||
"message_factory", self));
|
||||
if (args == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr message_class(PyObject_CallObject(
|
||||
reinterpret_cast<PyObject*>(&CMessageClass_Type), args.get()));
|
||||
if (message_class == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
// Create messages class for the messages used by the fields, and registers
|
||||
// all extensions for these messages during the recursion.
|
||||
for (int field_idx = 0; field_idx < descriptor->field_count(); field_idx++) {
|
||||
const Descriptor* sub_descriptor =
|
||||
descriptor->field(field_idx)->message_type();
|
||||
// It is NULL if the field type is not a message.
|
||||
if (sub_descriptor != NULL) {
|
||||
CMessageClass* result = GetOrCreateMessageClass(self, sub_descriptor);
|
||||
if (result == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(result);
|
||||
}
|
||||
}
|
||||
|
||||
// Register extensions defined in this message.
|
||||
for (int ext_idx = 0 ; ext_idx < descriptor->extension_count() ; ext_idx++) {
|
||||
const FieldDescriptor* extension = descriptor->extension(ext_idx);
|
||||
ScopedPyObjectPtr py_extended_class(
|
||||
GetOrCreateMessageClass(self, extension->containing_type())
|
||||
->AsPyObject());
|
||||
if (py_extended_class == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr py_extension(PyFieldDescriptor_FromDescriptor(extension));
|
||||
if (py_extension == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr result(cmessage::RegisterExtension(
|
||||
py_extended_class.get(), py_extension.get()));
|
||||
if (result == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return reinterpret_cast<CMessageClass*>(message_class.release());
|
||||
}
|
||||
|
||||
// Retrieve the message class added to our database.
|
||||
CMessageClass* GetMessageClass(PyMessageFactory* self,
|
||||
const Descriptor* message_descriptor) {
|
||||
typedef PyMessageFactory::ClassesByMessageMap::iterator iterator;
|
||||
iterator ret = self->classes_by_descriptor->find(message_descriptor);
|
||||
if (ret == self->classes_by_descriptor->end()) {
|
||||
PyErr_Format(PyExc_TypeError, "No message class registered for '%s'",
|
||||
message_descriptor->full_name().c_str());
|
||||
return NULL;
|
||||
} else {
|
||||
return ret->second;
|
||||
}
|
||||
}
|
||||
|
||||
static PyMethodDef Methods[] = {
|
||||
{NULL}};
|
||||
|
||||
static PyObject* GetPool(PyMessageFactory* self, void* closure) {
|
||||
Py_INCREF(self->pool);
|
||||
return reinterpret_cast<PyObject*>(self->pool);
|
||||
}
|
||||
|
||||
static PyGetSetDef Getters[] = {
|
||||
{"pool", (getter)GetPool, NULL, "DescriptorPool"},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
} // namespace message_factory
|
||||
|
||||
PyTypeObject PyMessageFactory_Type = {
|
||||
PyVarObject_HEAD_INIT(&PyType_Type, 0) FULL_MODULE_NAME
|
||||
".MessageFactory", // tp_name
|
||||
sizeof(PyMessageFactory), // tp_basicsize
|
||||
0, // tp_itemsize
|
||||
(destructor)message_factory::Dealloc, // tp_dealloc
|
||||
0, // tp_print
|
||||
0, // tp_getattr
|
||||
0, // tp_setattr
|
||||
0, // tp_compare
|
||||
0, // tp_repr
|
||||
0, // tp_as_number
|
||||
0, // tp_as_sequence
|
||||
0, // tp_as_mapping
|
||||
0, // tp_hash
|
||||
0, // tp_call
|
||||
0, // tp_str
|
||||
0, // tp_getattro
|
||||
0, // tp_setattro
|
||||
0, // tp_as_buffer
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags
|
||||
"A static Message Factory", // tp_doc
|
||||
0, // tp_traverse
|
||||
0, // tp_clear
|
||||
0, // tp_richcompare
|
||||
0, // tp_weaklistoffset
|
||||
0, // tp_iter
|
||||
0, // tp_iternext
|
||||
message_factory::Methods, // tp_methods
|
||||
0, // tp_members
|
||||
message_factory::Getters, // tp_getset
|
||||
0, // tp_base
|
||||
0, // tp_dict
|
||||
0, // tp_descr_get
|
||||
0, // tp_descr_set
|
||||
0, // tp_dictoffset
|
||||
0, // tp_init
|
||||
0, // tp_alloc
|
||||
message_factory::New, // tp_new
|
||||
PyObject_Del, // tp_free
|
||||
};
|
||||
|
||||
bool InitMessageFactory() {
|
||||
if (PyType_Ready(&PyMessageFactory_Type) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
@@ -0,0 +1,103 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_FACTORY_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_FACTORY_H__
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <google/protobuf/stubs/hash.h>
|
||||
#include <google/protobuf/descriptor.h>
|
||||
#include <google/protobuf/pyext/descriptor_pool.h>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
class MessageFactory;
|
||||
|
||||
namespace python {
|
||||
|
||||
// The (meta) type of all Messages classes.
|
||||
struct CMessageClass;
|
||||
|
||||
struct PyMessageFactory {
|
||||
PyObject_HEAD
|
||||
|
||||
// DynamicMessageFactory used to create C++ instances of messages.
|
||||
// This object cache the descriptors that were used, so the DescriptorPool
|
||||
// needs to get rid of it before it can delete itself.
|
||||
//
|
||||
// Note: A C++ MessageFactory is different from the PyMessageFactory.
|
||||
// The C++ one creates messages, when the Python one creates classes.
|
||||
MessageFactory* message_factory;
|
||||
|
||||
// borrowed reference to a Python DescriptorPool.
|
||||
// TODO(amauryfa): invert the dependency: the MessageFactory owns the
|
||||
// DescriptorPool, not the opposite.
|
||||
PyDescriptorPool* pool;
|
||||
|
||||
// Make our own mapping to retrieve Python classes from C++ descriptors.
|
||||
//
|
||||
// Descriptor pointers stored here are owned by the DescriptorPool above.
|
||||
// Python references to classes are owned by this PyDescriptorPool.
|
||||
typedef hash_map<const Descriptor*, CMessageClass*> ClassesByMessageMap;
|
||||
ClassesByMessageMap* classes_by_descriptor;
|
||||
};
|
||||
|
||||
extern PyTypeObject PyMessageFactory_Type;
|
||||
|
||||
namespace message_factory {
|
||||
|
||||
// Creates a new MessageFactory instance.
|
||||
PyMessageFactory* NewMessageFactory(PyTypeObject* type, PyDescriptorPool* pool);
|
||||
|
||||
// Registers a new Python class for the given message descriptor.
|
||||
// On error, returns -1 with a Python exception set.
|
||||
int RegisterMessageClass(PyMessageFactory* self,
|
||||
const Descriptor* message_descriptor,
|
||||
CMessageClass* message_class);
|
||||
// Retrieves the Python class registered with the given message descriptor, or
|
||||
// fail with a TypeError. Returns a *borrowed* reference.
|
||||
CMessageClass* GetMessageClass(PyMessageFactory* self,
|
||||
const Descriptor* message_descriptor);
|
||||
// Retrieves the Python class registered with the given message descriptor.
|
||||
// The class is created if not done yet. Returns a *new* reference.
|
||||
CMessageClass* GetOrCreateMessageClass(PyMessageFactory* self,
|
||||
const Descriptor* message_descriptor);
|
||||
} // namespace message_factory
|
||||
|
||||
// Initialize objects used by this module.
|
||||
// On error, returns false with a Python exception set.
|
||||
bool InitMessageFactory();
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_FACTORY_H__
|
||||
@@ -0,0 +1,88 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
#include <google/protobuf/pyext/message.h>
|
||||
|
||||
static const char module_docstring[] =
|
||||
"python-proto2 is a module that can be used to enhance proto2 Python API\n"
|
||||
"performance.\n"
|
||||
"\n"
|
||||
"It provides access to the protocol buffers C++ reflection API that\n"
|
||||
"implements the basic protocol buffer functions.";
|
||||
|
||||
static PyMethodDef ModuleMethods[] = {
|
||||
{"SetAllowOversizeProtos",
|
||||
(PyCFunction)google::protobuf::python::cmessage::SetAllowOversizeProtos,
|
||||
METH_O, "Enable/disable oversize proto parsing."},
|
||||
{ NULL, NULL}
|
||||
};
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
static struct PyModuleDef _module = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"_message",
|
||||
module_docstring,
|
||||
-1,
|
||||
ModuleMethods, /* m_methods */
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
#define INITFUNC PyInit__message
|
||||
#define INITFUNC_ERRORVAL NULL
|
||||
#else // Python 2
|
||||
#define INITFUNC init_message
|
||||
#define INITFUNC_ERRORVAL
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
PyMODINIT_FUNC INITFUNC(void) {
|
||||
PyObject* m;
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
m = PyModule_Create(&_module);
|
||||
#else
|
||||
m = Py_InitModule3("_message", ModuleMethods,
|
||||
module_docstring);
|
||||
#endif
|
||||
if (m == NULL) {
|
||||
return INITFUNC_ERRORVAL;
|
||||
}
|
||||
|
||||
if (!google::protobuf::python::InitProto2MessageModule(m)) {
|
||||
Py_DECREF(m);
|
||||
return INITFUNC_ERRORVAL;
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
syntax = "proto2";
|
||||
|
||||
import "google/protobuf/internal/cpp/proto1_api_test.proto";
|
||||
|
||||
package google.protobuf.python.internal;
|
||||
|
||||
message TestNestedProto1APIMessage {
|
||||
optional int32 a = 1;
|
||||
optional TestMessage.NestedMessage b = 2;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: tibell@google.com (Johan Tibell)
|
||||
//
|
||||
// These message definitions are used to exercises known corner cases
|
||||
// in the C++ implementation of the Python API.
|
||||
|
||||
syntax = "proto2";
|
||||
|
||||
package google.protobuf.python.internal;
|
||||
|
||||
// Protos optimized for SPEED use a strict superset of the generated code
|
||||
// of equivalent ones optimized for CODE_SIZE, so we should optimize all our
|
||||
// tests for speed unless explicitly testing code size optimization.
|
||||
option optimize_for = SPEED;
|
||||
|
||||
message TestAllTypes {
|
||||
message NestedMessage {
|
||||
optional int32 bb = 1;
|
||||
optional ForeignMessage cc = 2;
|
||||
}
|
||||
|
||||
repeated NestedMessage repeated_nested_message = 1;
|
||||
optional NestedMessage optional_nested_message = 2;
|
||||
optional int32 optional_int32 = 3;
|
||||
}
|
||||
|
||||
message ForeignMessage {
|
||||
optional int32 c = 1;
|
||||
repeated int32 d = 2;
|
||||
}
|
||||
|
||||
message TestAllExtensions {
|
||||
extensions 1 to max;
|
||||
}
|
||||
|
||||
extend TestAllExtensions {
|
||||
optional TestAllTypes.NestedMessage optional_nested_message_extension = 1;
|
||||
repeated TestAllTypes.NestedMessage repeated_nested_message_extension = 2;
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: google/protobuf/pyext/python.proto
|
||||
|
||||
import sys
|
||||
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
|
||||
from google.protobuf import descriptor as _descriptor
|
||||
from google.protobuf import message as _message
|
||||
from google.protobuf import reflection as _reflection
|
||||
from google.protobuf import symbol_database as _symbol_database
|
||||
from google.protobuf import descriptor_pb2
|
||||
# @@protoc_insertion_point(imports)
|
||||
|
||||
_sym_db = _symbol_database.Default()
|
||||
|
||||
|
||||
|
||||
|
||||
DESCRIPTOR = _descriptor.FileDescriptor(
|
||||
name='google/protobuf/pyext/python.proto',
|
||||
package='google.protobuf.python.internal',
|
||||
syntax='proto2',
|
||||
serialized_pb=_b('\n\"google/protobuf/pyext/python.proto\x12\x1fgoogle.protobuf.python.internal\"\xbc\x02\n\x0cTestAllTypes\x12\\\n\x17repeated_nested_message\x18\x01 \x03(\x0b\x32;.google.protobuf.python.internal.TestAllTypes.NestedMessage\x12\\\n\x17optional_nested_message\x18\x02 \x01(\x0b\x32;.google.protobuf.python.internal.TestAllTypes.NestedMessage\x12\x16\n\x0eoptional_int32\x18\x03 \x01(\x05\x1aX\n\rNestedMessage\x12\n\n\x02\x62\x62\x18\x01 \x01(\x05\x12;\n\x02\x63\x63\x18\x02 \x01(\x0b\x32/.google.protobuf.python.internal.ForeignMessage\"&\n\x0e\x46oreignMessage\x12\t\n\x01\x63\x18\x01 \x01(\x05\x12\t\n\x01\x64\x18\x02 \x03(\x05\"\x1d\n\x11TestAllExtensions*\x08\x08\x01\x10\x80\x80\x80\x80\x02:\x9a\x01\n!optional_nested_message_extension\x12\x32.google.protobuf.python.internal.TestAllExtensions\x18\x01 \x01(\x0b\x32;.google.protobuf.python.internal.TestAllTypes.NestedMessage:\x9a\x01\n!repeated_nested_message_extension\x12\x32.google.protobuf.python.internal.TestAllExtensions\x18\x02 \x03(\x0b\x32;.google.protobuf.python.internal.TestAllTypes.NestedMessageB\x02H\x01')
|
||||
)
|
||||
|
||||
|
||||
OPTIONAL_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER = 1
|
||||
optional_nested_message_extension = _descriptor.FieldDescriptor(
|
||||
name='optional_nested_message_extension', full_name='google.protobuf.python.internal.optional_nested_message_extension', index=0,
|
||||
number=1, type=11, cpp_type=10, label=1,
|
||||
has_default_value=False, default_value=None,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=True, extension_scope=None,
|
||||
options=None)
|
||||
REPEATED_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER = 2
|
||||
repeated_nested_message_extension = _descriptor.FieldDescriptor(
|
||||
name='repeated_nested_message_extension', full_name='google.protobuf.python.internal.repeated_nested_message_extension', index=1,
|
||||
number=2, type=11, cpp_type=10, label=3,
|
||||
has_default_value=False, default_value=[],
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=True, extension_scope=None,
|
||||
options=None)
|
||||
|
||||
|
||||
_TESTALLTYPES_NESTEDMESSAGE = _descriptor.Descriptor(
|
||||
name='NestedMessage',
|
||||
full_name='google.protobuf.python.internal.TestAllTypes.NestedMessage',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
containing_type=None,
|
||||
fields=[
|
||||
_descriptor.FieldDescriptor(
|
||||
name='bb', full_name='google.protobuf.python.internal.TestAllTypes.NestedMessage.bb', index=0,
|
||||
number=1, type=5, cpp_type=1, label=1,
|
||||
has_default_value=False, default_value=0,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='cc', full_name='google.protobuf.python.internal.TestAllTypes.NestedMessage.cc', index=1,
|
||||
number=2, type=11, cpp_type=10, label=1,
|
||||
has_default_value=False, default_value=None,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
],
|
||||
extensions=[
|
||||
],
|
||||
nested_types=[],
|
||||
enum_types=[
|
||||
],
|
||||
options=None,
|
||||
is_extendable=False,
|
||||
syntax='proto2',
|
||||
extension_ranges=[],
|
||||
oneofs=[
|
||||
],
|
||||
serialized_start=300,
|
||||
serialized_end=388,
|
||||
)
|
||||
|
||||
_TESTALLTYPES = _descriptor.Descriptor(
|
||||
name='TestAllTypes',
|
||||
full_name='google.protobuf.python.internal.TestAllTypes',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
containing_type=None,
|
||||
fields=[
|
||||
_descriptor.FieldDescriptor(
|
||||
name='repeated_nested_message', full_name='google.protobuf.python.internal.TestAllTypes.repeated_nested_message', index=0,
|
||||
number=1, type=11, cpp_type=10, label=3,
|
||||
has_default_value=False, default_value=[],
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='optional_nested_message', full_name='google.protobuf.python.internal.TestAllTypes.optional_nested_message', index=1,
|
||||
number=2, type=11, cpp_type=10, label=1,
|
||||
has_default_value=False, default_value=None,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='optional_int32', full_name='google.protobuf.python.internal.TestAllTypes.optional_int32', index=2,
|
||||
number=3, type=5, cpp_type=1, label=1,
|
||||
has_default_value=False, default_value=0,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
],
|
||||
extensions=[
|
||||
],
|
||||
nested_types=[_TESTALLTYPES_NESTEDMESSAGE, ],
|
||||
enum_types=[
|
||||
],
|
||||
options=None,
|
||||
is_extendable=False,
|
||||
syntax='proto2',
|
||||
extension_ranges=[],
|
||||
oneofs=[
|
||||
],
|
||||
serialized_start=72,
|
||||
serialized_end=388,
|
||||
)
|
||||
|
||||
|
||||
_FOREIGNMESSAGE = _descriptor.Descriptor(
|
||||
name='ForeignMessage',
|
||||
full_name='google.protobuf.python.internal.ForeignMessage',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
containing_type=None,
|
||||
fields=[
|
||||
_descriptor.FieldDescriptor(
|
||||
name='c', full_name='google.protobuf.python.internal.ForeignMessage.c', index=0,
|
||||
number=1, type=5, cpp_type=1, label=1,
|
||||
has_default_value=False, default_value=0,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='d', full_name='google.protobuf.python.internal.ForeignMessage.d', index=1,
|
||||
number=2, type=5, cpp_type=1, label=3,
|
||||
has_default_value=False, default_value=[],
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
],
|
||||
extensions=[
|
||||
],
|
||||
nested_types=[],
|
||||
enum_types=[
|
||||
],
|
||||
options=None,
|
||||
is_extendable=False,
|
||||
syntax='proto2',
|
||||
extension_ranges=[],
|
||||
oneofs=[
|
||||
],
|
||||
serialized_start=390,
|
||||
serialized_end=428,
|
||||
)
|
||||
|
||||
|
||||
_TESTALLEXTENSIONS = _descriptor.Descriptor(
|
||||
name='TestAllExtensions',
|
||||
full_name='google.protobuf.python.internal.TestAllExtensions',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
containing_type=None,
|
||||
fields=[
|
||||
],
|
||||
extensions=[
|
||||
],
|
||||
nested_types=[],
|
||||
enum_types=[
|
||||
],
|
||||
options=None,
|
||||
is_extendable=True,
|
||||
syntax='proto2',
|
||||
extension_ranges=[(1, 536870912), ],
|
||||
oneofs=[
|
||||
],
|
||||
serialized_start=430,
|
||||
serialized_end=459,
|
||||
)
|
||||
|
||||
_TESTALLTYPES_NESTEDMESSAGE.fields_by_name['cc'].message_type = _FOREIGNMESSAGE
|
||||
_TESTALLTYPES_NESTEDMESSAGE.containing_type = _TESTALLTYPES
|
||||
_TESTALLTYPES.fields_by_name['repeated_nested_message'].message_type = _TESTALLTYPES_NESTEDMESSAGE
|
||||
_TESTALLTYPES.fields_by_name['optional_nested_message'].message_type = _TESTALLTYPES_NESTEDMESSAGE
|
||||
DESCRIPTOR.message_types_by_name['TestAllTypes'] = _TESTALLTYPES
|
||||
DESCRIPTOR.message_types_by_name['ForeignMessage'] = _FOREIGNMESSAGE
|
||||
DESCRIPTOR.message_types_by_name['TestAllExtensions'] = _TESTALLEXTENSIONS
|
||||
DESCRIPTOR.extensions_by_name['optional_nested_message_extension'] = optional_nested_message_extension
|
||||
DESCRIPTOR.extensions_by_name['repeated_nested_message_extension'] = repeated_nested_message_extension
|
||||
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
|
||||
|
||||
TestAllTypes = _reflection.GeneratedProtocolMessageType('TestAllTypes', (_message.Message,), dict(
|
||||
|
||||
NestedMessage = _reflection.GeneratedProtocolMessageType('NestedMessage', (_message.Message,), dict(
|
||||
DESCRIPTOR = _TESTALLTYPES_NESTEDMESSAGE,
|
||||
__module__ = 'google.protobuf.pyext.python_pb2'
|
||||
# @@protoc_insertion_point(class_scope:google.protobuf.python.internal.TestAllTypes.NestedMessage)
|
||||
))
|
||||
,
|
||||
DESCRIPTOR = _TESTALLTYPES,
|
||||
__module__ = 'google.protobuf.pyext.python_pb2'
|
||||
# @@protoc_insertion_point(class_scope:google.protobuf.python.internal.TestAllTypes)
|
||||
))
|
||||
_sym_db.RegisterMessage(TestAllTypes)
|
||||
_sym_db.RegisterMessage(TestAllTypes.NestedMessage)
|
||||
|
||||
ForeignMessage = _reflection.GeneratedProtocolMessageType('ForeignMessage', (_message.Message,), dict(
|
||||
DESCRIPTOR = _FOREIGNMESSAGE,
|
||||
__module__ = 'google.protobuf.pyext.python_pb2'
|
||||
# @@protoc_insertion_point(class_scope:google.protobuf.python.internal.ForeignMessage)
|
||||
))
|
||||
_sym_db.RegisterMessage(ForeignMessage)
|
||||
|
||||
TestAllExtensions = _reflection.GeneratedProtocolMessageType('TestAllExtensions', (_message.Message,), dict(
|
||||
DESCRIPTOR = _TESTALLEXTENSIONS,
|
||||
__module__ = 'google.protobuf.pyext.python_pb2'
|
||||
# @@protoc_insertion_point(class_scope:google.protobuf.python.internal.TestAllExtensions)
|
||||
))
|
||||
_sym_db.RegisterMessage(TestAllExtensions)
|
||||
|
||||
optional_nested_message_extension.message_type = _TESTALLTYPES_NESTEDMESSAGE
|
||||
TestAllExtensions.RegisterExtension(optional_nested_message_extension)
|
||||
repeated_nested_message_extension.message_type = _TESTALLTYPES_NESTEDMESSAGE
|
||||
TestAllExtensions.RegisterExtension(repeated_nested_message_extension)
|
||||
|
||||
DESCRIPTOR.has_options = True
|
||||
DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), _b('H\001'))
|
||||
# @@protoc_insertion_point(module_scope)
|
||||
@@ -0,0 +1,641 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: anuraag@google.com (Anuraag Agrawal)
|
||||
// Author: tibell@google.com (Johan Tibell)
|
||||
|
||||
#include <google/protobuf/pyext/repeated_composite_container.h>
|
||||
|
||||
#include <memory>
|
||||
#ifndef _SHARED_PTR_H
|
||||
#include <google/protobuf/stubs/shared_ptr.h>
|
||||
#endif
|
||||
|
||||
#include <google/protobuf/stubs/logging.h>
|
||||
#include <google/protobuf/stubs/common.h>
|
||||
#include <google/protobuf/descriptor.h>
|
||||
#include <google/protobuf/dynamic_message.h>
|
||||
#include <google/protobuf/message.h>
|
||||
#include <google/protobuf/pyext/descriptor.h>
|
||||
#include <google/protobuf/pyext/descriptor_pool.h>
|
||||
#include <google/protobuf/pyext/message.h>
|
||||
#include <google/protobuf/pyext/scoped_pyobject_ptr.h>
|
||||
#include <google/protobuf/reflection.h>
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#define PyInt_Check PyLong_Check
|
||||
#define PyInt_AsLong PyLong_AsLong
|
||||
#define PyInt_FromLong PyLong_FromLong
|
||||
#endif
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
namespace repeated_composite_container {
|
||||
|
||||
// TODO(tibell): We might also want to check:
|
||||
// GOOGLE_CHECK_NOTNULL((self)->owner.get());
|
||||
#define GOOGLE_CHECK_ATTACHED(self) \
|
||||
do { \
|
||||
GOOGLE_CHECK_NOTNULL((self)->message); \
|
||||
GOOGLE_CHECK_NOTNULL((self)->parent_field_descriptor); \
|
||||
} while (0);
|
||||
|
||||
#define GOOGLE_CHECK_RELEASED(self) \
|
||||
do { \
|
||||
GOOGLE_CHECK((self)->owner.get() == NULL); \
|
||||
GOOGLE_CHECK((self)->message == NULL); \
|
||||
GOOGLE_CHECK((self)->parent_field_descriptor == NULL); \
|
||||
GOOGLE_CHECK((self)->parent == NULL); \
|
||||
} while (0);
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// len()
|
||||
|
||||
static Py_ssize_t Length(RepeatedCompositeContainer* self) {
|
||||
Message* message = self->message;
|
||||
if (message != NULL) {
|
||||
return message->GetReflection()->FieldSize(*message,
|
||||
self->parent_field_descriptor);
|
||||
} else {
|
||||
// The container has been released (i.e. by a call to Clear() or
|
||||
// ClearField() on the parent) and thus there's no message.
|
||||
return PyList_GET_SIZE(self->child_messages);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns 0 if successful; returns -1 and sets an exception if
|
||||
// unsuccessful.
|
||||
static int UpdateChildMessages(RepeatedCompositeContainer* self) {
|
||||
if (self->message == NULL)
|
||||
return 0;
|
||||
|
||||
// A MergeFrom on a parent message could have caused extra messages to be
|
||||
// added in the underlying protobuf so add them to our list. They can never
|
||||
// be removed in such a way so there's no need to worry about that.
|
||||
Py_ssize_t message_length = Length(self);
|
||||
Py_ssize_t child_length = PyList_GET_SIZE(self->child_messages);
|
||||
Message* message = self->message;
|
||||
const Reflection* reflection = message->GetReflection();
|
||||
for (Py_ssize_t i = child_length; i < message_length; ++i) {
|
||||
const Message& sub_message = reflection->GetRepeatedMessage(
|
||||
*(self->message), self->parent_field_descriptor, i);
|
||||
CMessage* cmsg = cmessage::NewEmptyMessage(self->child_message_class);
|
||||
ScopedPyObjectPtr py_cmsg(reinterpret_cast<PyObject*>(cmsg));
|
||||
if (cmsg == NULL) {
|
||||
return -1;
|
||||
}
|
||||
cmsg->owner = self->owner;
|
||||
cmsg->message = const_cast<Message*>(&sub_message);
|
||||
cmsg->parent = self->parent;
|
||||
if (PyList_Append(self->child_messages, py_cmsg.get()) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// add()
|
||||
|
||||
static PyObject* AddToAttached(RepeatedCompositeContainer* self,
|
||||
PyObject* args,
|
||||
PyObject* kwargs) {
|
||||
GOOGLE_CHECK_ATTACHED(self);
|
||||
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (cmessage::AssureWritable(self->parent) == -1)
|
||||
return NULL;
|
||||
Message* message = self->message;
|
||||
Message* sub_message =
|
||||
message->GetReflection()->AddMessage(message,
|
||||
self->parent_field_descriptor);
|
||||
CMessage* cmsg = cmessage::NewEmptyMessage(self->child_message_class);
|
||||
if (cmsg == NULL)
|
||||
return NULL;
|
||||
|
||||
cmsg->owner = self->owner;
|
||||
cmsg->message = sub_message;
|
||||
cmsg->parent = self->parent;
|
||||
if (cmessage::InitAttributes(cmsg, args, kwargs) < 0) {
|
||||
Py_DECREF(cmsg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject* py_cmsg = reinterpret_cast<PyObject*>(cmsg);
|
||||
if (PyList_Append(self->child_messages, py_cmsg) < 0) {
|
||||
Py_DECREF(py_cmsg);
|
||||
return NULL;
|
||||
}
|
||||
return py_cmsg;
|
||||
}
|
||||
|
||||
static PyObject* AddToReleased(RepeatedCompositeContainer* self,
|
||||
PyObject* args,
|
||||
PyObject* kwargs) {
|
||||
GOOGLE_CHECK_RELEASED(self);
|
||||
|
||||
// Create a new Message detached from the rest.
|
||||
PyObject* py_cmsg = PyEval_CallObjectWithKeywords(
|
||||
self->child_message_class->AsPyObject(), args, kwargs);
|
||||
if (py_cmsg == NULL)
|
||||
return NULL;
|
||||
|
||||
if (PyList_Append(self->child_messages, py_cmsg) < 0) {
|
||||
Py_DECREF(py_cmsg);
|
||||
return NULL;
|
||||
}
|
||||
return py_cmsg;
|
||||
}
|
||||
|
||||
PyObject* Add(RepeatedCompositeContainer* self,
|
||||
PyObject* args,
|
||||
PyObject* kwargs) {
|
||||
if (self->message == NULL)
|
||||
return AddToReleased(self, args, kwargs);
|
||||
else
|
||||
return AddToAttached(self, args, kwargs);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// extend()
|
||||
|
||||
PyObject* Extend(RepeatedCompositeContainer* self, PyObject* value) {
|
||||
cmessage::AssureWritable(self->parent);
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr iter(PyObject_GetIter(value));
|
||||
if (iter == NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "Value must be iterable");
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr next;
|
||||
while ((next.reset(PyIter_Next(iter.get()))) != NULL) {
|
||||
if (!PyObject_TypeCheck(next.get(), &CMessage_Type)) {
|
||||
PyErr_SetString(PyExc_TypeError, "Not a cmessage");
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr new_message(Add(self, NULL, NULL));
|
||||
if (new_message == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
CMessage* new_cmessage = reinterpret_cast<CMessage*>(new_message.get());
|
||||
if (ScopedPyObjectPtr(cmessage::MergeFrom(new_cmessage, next.get())) ==
|
||||
NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if (PyErr_Occurred()) {
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
PyObject* MergeFrom(RepeatedCompositeContainer* self, PyObject* other) {
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return Extend(self, other);
|
||||
}
|
||||
|
||||
PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* slice) {
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
// Just forward the call to the subscript-handling function of the
|
||||
// list containing the child messages.
|
||||
return PyObject_GetItem(self->child_messages, slice);
|
||||
}
|
||||
|
||||
int AssignSubscript(RepeatedCompositeContainer* self,
|
||||
PyObject* slice,
|
||||
PyObject* value) {
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (value != NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "does not support assignment");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Delete from the underlying Message, if any.
|
||||
if (self->parent != NULL) {
|
||||
if (cmessage::InternalDeleteRepeatedField(self->parent,
|
||||
self->parent_field_descriptor,
|
||||
slice,
|
||||
self->child_messages) < 0) {
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
Py_ssize_t from;
|
||||
Py_ssize_t to;
|
||||
Py_ssize_t step;
|
||||
Py_ssize_t length = Length(self);
|
||||
Py_ssize_t slicelength;
|
||||
if (PySlice_Check(slice)) {
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
if (PySlice_GetIndicesEx(slice,
|
||||
#else
|
||||
if (PySlice_GetIndicesEx(reinterpret_cast<PySliceObject*>(slice),
|
||||
#endif
|
||||
length, &from, &to, &step, &slicelength) == -1) {
|
||||
return -1;
|
||||
}
|
||||
return PySequence_DelSlice(self->child_messages, from, to);
|
||||
} else if (PyInt_Check(slice) || PyLong_Check(slice)) {
|
||||
from = to = PyLong_AsLong(slice);
|
||||
if (from < 0) {
|
||||
from = to = length + from;
|
||||
}
|
||||
return PySequence_DelItem(self->child_messages, from);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static PyObject* Remove(RepeatedCompositeContainer* self, PyObject* value) {
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
Py_ssize_t index = PySequence_Index(self->child_messages, value);
|
||||
if (index == -1) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr py_index(PyLong_FromLong(index));
|
||||
if (AssignSubscript(self, py_index.get(), NULL) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject* RichCompare(RepeatedCompositeContainer* self,
|
||||
PyObject* other,
|
||||
int opid) {
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (!PyObject_TypeCheck(other, &RepeatedCompositeContainer_Type)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"Can only compare repeated composite fields "
|
||||
"against other repeated composite fields.");
|
||||
return NULL;
|
||||
}
|
||||
if (opid == Py_EQ || opid == Py_NE) {
|
||||
// TODO(anuraag): Don't make new lists just for this...
|
||||
ScopedPyObjectPtr full_slice(PySlice_New(NULL, NULL, NULL));
|
||||
if (full_slice == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr list(Subscript(self, full_slice.get()));
|
||||
if (list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr other_list(
|
||||
Subscript(reinterpret_cast<RepeatedCompositeContainer*>(other),
|
||||
full_slice.get()));
|
||||
if (other_list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return PyObject_RichCompare(list.get(), other_list.get(), opid);
|
||||
} else {
|
||||
Py_INCREF(Py_NotImplemented);
|
||||
return Py_NotImplemented;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// sort()
|
||||
|
||||
static void ReorderAttached(RepeatedCompositeContainer* self) {
|
||||
Message* message = self->message;
|
||||
const Reflection* reflection = message->GetReflection();
|
||||
const FieldDescriptor* descriptor = self->parent_field_descriptor;
|
||||
const Py_ssize_t length = Length(self);
|
||||
|
||||
// Since Python protobuf objects are never arena-allocated, adding and
|
||||
// removing message pointers to the underlying array is just updating
|
||||
// pointers.
|
||||
for (Py_ssize_t i = 0; i < length; ++i)
|
||||
reflection->ReleaseLast(message, descriptor);
|
||||
|
||||
for (Py_ssize_t i = 0; i < length; ++i) {
|
||||
CMessage* py_cmsg = reinterpret_cast<CMessage*>(
|
||||
PyList_GET_ITEM(self->child_messages, i));
|
||||
reflection->AddAllocatedMessage(message, descriptor, py_cmsg->message);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns 0 if successful; returns -1 and sets an exception if
|
||||
// unsuccessful.
|
||||
static int SortPythonMessages(RepeatedCompositeContainer* self,
|
||||
PyObject* args,
|
||||
PyObject* kwds) {
|
||||
ScopedPyObjectPtr m(PyObject_GetAttrString(self->child_messages, "sort"));
|
||||
if (m == NULL)
|
||||
return -1;
|
||||
if (ScopedPyObjectPtr(PyObject_Call(m.get(), args, kwds)) == NULL)
|
||||
return -1;
|
||||
if (self->message != NULL) {
|
||||
ReorderAttached(self);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static PyObject* Sort(RepeatedCompositeContainer* self,
|
||||
PyObject* args,
|
||||
PyObject* kwds) {
|
||||
// Support the old sort_function argument for backwards
|
||||
// compatibility.
|
||||
if (kwds != NULL) {
|
||||
PyObject* sort_func = PyDict_GetItemString(kwds, "sort_function");
|
||||
if (sort_func != NULL) {
|
||||
// Must set before deleting as sort_func is a borrowed reference
|
||||
// and kwds might be the only thing keeping it alive.
|
||||
PyDict_SetItemString(kwds, "cmp", sort_func);
|
||||
PyDict_DelItemString(kwds, "sort_function");
|
||||
}
|
||||
}
|
||||
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (SortPythonMessages(self, args, kwds) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
static PyObject* Item(RepeatedCompositeContainer* self, Py_ssize_t index) {
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
Py_ssize_t length = Length(self);
|
||||
if (index < 0) {
|
||||
index = length + index;
|
||||
}
|
||||
PyObject* item = PyList_GetItem(self->child_messages, index);
|
||||
if (item == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
Py_INCREF(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
static PyObject* Pop(RepeatedCompositeContainer* self,
|
||||
PyObject* args) {
|
||||
Py_ssize_t index = -1;
|
||||
if (!PyArg_ParseTuple(args, "|n", &index)) {
|
||||
return NULL;
|
||||
}
|
||||
PyObject* item = Item(self, index);
|
||||
if (item == NULL) {
|
||||
PyErr_Format(PyExc_IndexError,
|
||||
"list index (%zd) out of range",
|
||||
index);
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr py_index(PyLong_FromSsize_t(index));
|
||||
if (AssignSubscript(self, py_index.get(), NULL) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
// Release field of parent message and transfer the ownership to target.
|
||||
void ReleaseLastTo(CMessage* parent,
|
||||
const FieldDescriptor* field,
|
||||
CMessage* target) {
|
||||
GOOGLE_CHECK_NOTNULL(parent);
|
||||
GOOGLE_CHECK_NOTNULL(field);
|
||||
GOOGLE_CHECK_NOTNULL(target);
|
||||
|
||||
shared_ptr<Message> released_message(
|
||||
parent->message->GetReflection()->ReleaseLast(parent->message, field));
|
||||
// TODO(tibell): Deal with proto1.
|
||||
|
||||
target->parent = NULL;
|
||||
target->parent_field_descriptor = NULL;
|
||||
target->message = released_message.get();
|
||||
target->read_only = false;
|
||||
cmessage::SetOwner(target, released_message);
|
||||
}
|
||||
|
||||
// Called to release a container using
|
||||
// ClearField('container_field_name') on the parent.
|
||||
int Release(RepeatedCompositeContainer* self) {
|
||||
if (UpdateChildMessages(self) < 0) {
|
||||
PyErr_WriteUnraisable(PyBytes_FromString("Failed to update released "
|
||||
"messages"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
Message* message = self->message;
|
||||
const FieldDescriptor* field = self->parent_field_descriptor;
|
||||
|
||||
// The reflection API only lets us release the last message in a
|
||||
// repeated field. Therefore we iterate through the children
|
||||
// starting with the last one.
|
||||
const Py_ssize_t size = PyList_GET_SIZE(self->child_messages);
|
||||
GOOGLE_DCHECK_EQ(size, message->GetReflection()->FieldSize(*message, field));
|
||||
for (Py_ssize_t i = size - 1; i >= 0; --i) {
|
||||
CMessage* child_cmessage = reinterpret_cast<CMessage*>(
|
||||
PyList_GET_ITEM(self->child_messages, i));
|
||||
ReleaseLastTo(self->parent, field, child_cmessage);
|
||||
}
|
||||
|
||||
// Detach from containing message.
|
||||
self->parent = NULL;
|
||||
self->parent_field_descriptor = NULL;
|
||||
self->message = NULL;
|
||||
self->owner.reset();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* DeepCopy(RepeatedCompositeContainer* self, PyObject* arg) {
|
||||
ScopedPyObjectPtr cloneObj(
|
||||
PyType_GenericAlloc(&RepeatedCompositeContainer_Type, 0));
|
||||
if (cloneObj == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
RepeatedCompositeContainer* clone =
|
||||
reinterpret_cast<RepeatedCompositeContainer*>(cloneObj.get());
|
||||
|
||||
Message* new_message = self->message->New();
|
||||
clone->parent = NULL;
|
||||
clone->parent_field_descriptor = self->parent_field_descriptor;
|
||||
clone->message = new_message;
|
||||
clone->owner.reset(new_message);
|
||||
Py_INCREF(self->child_message_class);
|
||||
clone->child_message_class = self->child_message_class;
|
||||
clone->child_messages = PyList_New(0);
|
||||
|
||||
new_message->GetReflection()
|
||||
->GetMutableRepeatedFieldRef<Message>(new_message,
|
||||
self->parent_field_descriptor)
|
||||
.MergeFrom(self->message->GetReflection()->GetRepeatedFieldRef<Message>(
|
||||
*self->message, self->parent_field_descriptor));
|
||||
return cloneObj.release();
|
||||
}
|
||||
|
||||
int SetOwner(RepeatedCompositeContainer* self,
|
||||
const shared_ptr<Message>& new_owner) {
|
||||
GOOGLE_CHECK_ATTACHED(self);
|
||||
|
||||
self->owner = new_owner;
|
||||
const Py_ssize_t n = PyList_GET_SIZE(self->child_messages);
|
||||
for (Py_ssize_t i = 0; i < n; ++i) {
|
||||
PyObject* msg = PyList_GET_ITEM(self->child_messages, i);
|
||||
if (cmessage::SetOwner(reinterpret_cast<CMessage*>(msg), new_owner) == -1) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The private constructor of RepeatedCompositeContainer objects.
|
||||
PyObject *NewContainer(
|
||||
CMessage* parent,
|
||||
const FieldDescriptor* parent_field_descriptor,
|
||||
CMessageClass* concrete_class) {
|
||||
if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RepeatedCompositeContainer* self =
|
||||
reinterpret_cast<RepeatedCompositeContainer*>(
|
||||
PyType_GenericAlloc(&RepeatedCompositeContainer_Type, 0));
|
||||
if (self == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
self->message = parent->message;
|
||||
self->parent = parent;
|
||||
self->parent_field_descriptor = parent_field_descriptor;
|
||||
self->owner = parent->owner;
|
||||
Py_INCREF(concrete_class);
|
||||
self->child_message_class = concrete_class;
|
||||
self->child_messages = PyList_New(0);
|
||||
|
||||
return reinterpret_cast<PyObject*>(self);
|
||||
}
|
||||
|
||||
static void Dealloc(RepeatedCompositeContainer* self) {
|
||||
Py_CLEAR(self->child_messages);
|
||||
Py_CLEAR(self->child_message_class);
|
||||
// TODO(tibell): Do we need to call delete on these objects to make
|
||||
// sure their destructors are called?
|
||||
self->owner.reset();
|
||||
|
||||
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
|
||||
}
|
||||
|
||||
static PySequenceMethods SqMethods = {
|
||||
(lenfunc)Length, /* sq_length */
|
||||
0, /* sq_concat */
|
||||
0, /* sq_repeat */
|
||||
(ssizeargfunc)Item /* sq_item */
|
||||
};
|
||||
|
||||
static PyMappingMethods MpMethods = {
|
||||
(lenfunc)Length, /* mp_length */
|
||||
(binaryfunc)Subscript, /* mp_subscript */
|
||||
(objobjargproc)AssignSubscript,/* mp_ass_subscript */
|
||||
};
|
||||
|
||||
static PyMethodDef Methods[] = {
|
||||
{ "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS,
|
||||
"Makes a deep copy of the class." },
|
||||
{ "add", (PyCFunction) Add, METH_VARARGS | METH_KEYWORDS,
|
||||
"Adds an object to the repeated container." },
|
||||
{ "extend", (PyCFunction) Extend, METH_O,
|
||||
"Adds objects to the repeated container." },
|
||||
{ "pop", (PyCFunction)Pop, METH_VARARGS,
|
||||
"Removes an object from the repeated container and returns it." },
|
||||
{ "remove", (PyCFunction) Remove, METH_O,
|
||||
"Removes an object from the repeated container." },
|
||||
{ "sort", (PyCFunction) Sort, METH_VARARGS | METH_KEYWORDS,
|
||||
"Sorts the repeated container." },
|
||||
{ "MergeFrom", (PyCFunction) MergeFrom, METH_O,
|
||||
"Adds objects to the repeated container." },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
} // namespace repeated_composite_container
|
||||
|
||||
PyTypeObject RepeatedCompositeContainer_Type = {
|
||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||
FULL_MODULE_NAME ".RepeatedCompositeContainer", // tp_name
|
||||
sizeof(RepeatedCompositeContainer), // tp_basicsize
|
||||
0, // tp_itemsize
|
||||
(destructor)repeated_composite_container::Dealloc, // tp_dealloc
|
||||
0, // tp_print
|
||||
0, // tp_getattr
|
||||
0, // tp_setattr
|
||||
0, // tp_compare
|
||||
0, // tp_repr
|
||||
0, // tp_as_number
|
||||
&repeated_composite_container::SqMethods, // tp_as_sequence
|
||||
&repeated_composite_container::MpMethods, // tp_as_mapping
|
||||
PyObject_HashNotImplemented, // tp_hash
|
||||
0, // tp_call
|
||||
0, // tp_str
|
||||
0, // tp_getattro
|
||||
0, // tp_setattro
|
||||
0, // tp_as_buffer
|
||||
Py_TPFLAGS_DEFAULT, // tp_flags
|
||||
"A Repeated scalar container", // tp_doc
|
||||
0, // tp_traverse
|
||||
0, // tp_clear
|
||||
(richcmpfunc)repeated_composite_container::RichCompare, // tp_richcompare
|
||||
0, // tp_weaklistoffset
|
||||
0, // tp_iter
|
||||
0, // tp_iternext
|
||||
repeated_composite_container::Methods, // tp_methods
|
||||
0, // tp_members
|
||||
0, // tp_getset
|
||||
0, // tp_base
|
||||
0, // tp_dict
|
||||
0, // tp_descr_get
|
||||
0, // tp_descr_set
|
||||
0, // tp_dictoffset
|
||||
0, // tp_init
|
||||
};
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
@@ -0,0 +1,179 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: anuraag@google.com (Anuraag Agrawal)
|
||||
// Author: tibell@google.com (Johan Tibell)
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <memory>
|
||||
#ifndef _SHARED_PTR_H
|
||||
#include <google/protobuf/stubs/shared_ptr.h>
|
||||
#endif
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
|
||||
class FieldDescriptor;
|
||||
class Message;
|
||||
|
||||
#ifdef _SHARED_PTR_H
|
||||
using std::shared_ptr;
|
||||
#else
|
||||
using internal::shared_ptr;
|
||||
#endif
|
||||
|
||||
namespace python {
|
||||
|
||||
struct CMessage;
|
||||
struct CMessageClass;
|
||||
|
||||
// A RepeatedCompositeContainer can be in one of two states: attached
|
||||
// or released.
|
||||
//
|
||||
// When in the attached state all modifications to the container are
|
||||
// done both on the 'message' and on the 'child_messages'
|
||||
// list. In this state all Messages referred to by the children in
|
||||
// 'child_messages' are owner by the 'owner'.
|
||||
//
|
||||
// When in the released state 'message', 'owner', 'parent', and
|
||||
// 'parent_field_descriptor' are NULL.
|
||||
typedef struct RepeatedCompositeContainer {
|
||||
PyObject_HEAD;
|
||||
|
||||
// This is the top-level C++ Message object that owns the whole
|
||||
// proto tree. Every Python RepeatedCompositeContainer holds a
|
||||
// reference to it in order to keep it alive as long as there's a
|
||||
// Python object that references any part of the tree.
|
||||
shared_ptr<Message> owner;
|
||||
|
||||
// Weak reference to parent object. May be NULL. Used to make sure
|
||||
// the parent is writable before modifying the
|
||||
// RepeatedCompositeContainer.
|
||||
CMessage* parent;
|
||||
|
||||
// A descriptor used to modify the underlying 'message'.
|
||||
// The pointer is owned by the global DescriptorPool.
|
||||
const FieldDescriptor* parent_field_descriptor;
|
||||
|
||||
// Pointer to the C++ Message that contains this container. The
|
||||
// RepeatedCompositeContainer does not own this pointer.
|
||||
//
|
||||
// If NULL, this message has been released from its parent (by
|
||||
// calling Clear() or ClearField() on the parent.
|
||||
Message* message;
|
||||
|
||||
// The type used to create new child messages.
|
||||
CMessageClass* child_message_class;
|
||||
|
||||
// A list of child messages.
|
||||
PyObject* child_messages;
|
||||
} RepeatedCompositeContainer;
|
||||
|
||||
extern PyTypeObject RepeatedCompositeContainer_Type;
|
||||
|
||||
namespace repeated_composite_container {
|
||||
|
||||
// Builds a RepeatedCompositeContainer object, from a parent message and a
|
||||
// field descriptor.
|
||||
PyObject *NewContainer(
|
||||
CMessage* parent,
|
||||
const FieldDescriptor* parent_field_descriptor,
|
||||
CMessageClass *child_message_class);
|
||||
|
||||
// Appends a new CMessage to the container and returns it. The
|
||||
// CMessage is initialized using the content of kwargs.
|
||||
//
|
||||
// Returns a new reference if successful; returns NULL and sets an
|
||||
// exception if unsuccessful.
|
||||
PyObject* Add(RepeatedCompositeContainer* self,
|
||||
PyObject* args,
|
||||
PyObject* kwargs);
|
||||
|
||||
// Appends all the CMessages in the input iterator to the container.
|
||||
//
|
||||
// Returns None if successful; returns NULL and sets an exception if
|
||||
// unsuccessful.
|
||||
PyObject* Extend(RepeatedCompositeContainer* self, PyObject* value);
|
||||
|
||||
// Appends a new message to the container for each message in the
|
||||
// input iterator, merging each data element in. Equivalent to extend.
|
||||
//
|
||||
// Returns None if successful; returns NULL and sets an exception if
|
||||
// unsuccessful.
|
||||
PyObject* MergeFrom(RepeatedCompositeContainer* self, PyObject* other);
|
||||
|
||||
// Accesses messages in the container.
|
||||
//
|
||||
// Returns a new reference to the message for an integer parameter.
|
||||
// Returns a new reference to a list of messages for a slice.
|
||||
PyObject* Subscript(RepeatedCompositeContainer* self, PyObject* slice);
|
||||
|
||||
// Deletes items from the container (cannot be used for assignment).
|
||||
//
|
||||
// Returns 0 on success, -1 on failure.
|
||||
int AssignSubscript(RepeatedCompositeContainer* self,
|
||||
PyObject* slice,
|
||||
PyObject* value);
|
||||
|
||||
// Releases the messages in the container to the given message.
|
||||
//
|
||||
// Returns 0 on success, -1 on failure.
|
||||
int ReleaseToMessage(RepeatedCompositeContainer* self, Message* new_message);
|
||||
|
||||
// Releases the messages in the container to a new message.
|
||||
//
|
||||
// Returns 0 on success, -1 on failure.
|
||||
int Release(RepeatedCompositeContainer* self);
|
||||
|
||||
// Returns 0 on success, -1 on failure.
|
||||
int SetOwner(RepeatedCompositeContainer* self,
|
||||
const shared_ptr<Message>& new_owner);
|
||||
|
||||
// Removes the last element of the repeated message field 'field' on
|
||||
// the Message 'parent', and transfers the ownership of the released
|
||||
// Message to 'target'.
|
||||
//
|
||||
// Corresponds to reflection api method ReleaseMessage.
|
||||
void ReleaseLastTo(CMessage* parent,
|
||||
const FieldDescriptor* field,
|
||||
CMessage* target);
|
||||
|
||||
} // namespace repeated_composite_container
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_COMPOSITE_CONTAINER_H__
|
||||
@@ -0,0 +1,812 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: anuraag@google.com (Anuraag Agrawal)
|
||||
// Author: tibell@google.com (Johan Tibell)
|
||||
|
||||
#include <google/protobuf/pyext/repeated_scalar_container.h>
|
||||
|
||||
#include <memory>
|
||||
#ifndef _SHARED_PTR_H
|
||||
#include <google/protobuf/stubs/shared_ptr.h>
|
||||
#endif
|
||||
|
||||
#include <google/protobuf/stubs/common.h>
|
||||
#include <google/protobuf/stubs/logging.h>
|
||||
#include <google/protobuf/descriptor.h>
|
||||
#include <google/protobuf/dynamic_message.h>
|
||||
#include <google/protobuf/message.h>
|
||||
#include <google/protobuf/pyext/descriptor.h>
|
||||
#include <google/protobuf/pyext/descriptor_pool.h>
|
||||
#include <google/protobuf/pyext/message.h>
|
||||
#include <google/protobuf/pyext/scoped_pyobject_ptr.h>
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
#define PyInt_FromLong PyLong_FromLong
|
||||
#if PY_VERSION_HEX < 0x03030000
|
||||
#error "Python 3.0 - 3.2 are not supported."
|
||||
#else
|
||||
#define PyString_AsString(ob) \
|
||||
(PyUnicode_Check(ob)? PyUnicode_AsUTF8(ob): PyBytes_AsString(ob))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
namespace repeated_scalar_container {
|
||||
|
||||
static int InternalAssignRepeatedField(
|
||||
RepeatedScalarContainer* self, PyObject* list) {
|
||||
self->message->GetReflection()->ClearField(self->message,
|
||||
self->parent_field_descriptor);
|
||||
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(list); ++i) {
|
||||
PyObject* value = PyList_GET_ITEM(list, i);
|
||||
if (ScopedPyObjectPtr(Append(self, value)) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static Py_ssize_t Len(RepeatedScalarContainer* self) {
|
||||
Message* message = self->message;
|
||||
return message->GetReflection()->FieldSize(*message,
|
||||
self->parent_field_descriptor);
|
||||
}
|
||||
|
||||
static int AssignItem(RepeatedScalarContainer* self,
|
||||
Py_ssize_t index,
|
||||
PyObject* arg) {
|
||||
cmessage::AssureWritable(self->parent);
|
||||
Message* message = self->message;
|
||||
const FieldDescriptor* field_descriptor = self->parent_field_descriptor;
|
||||
|
||||
const Reflection* reflection = message->GetReflection();
|
||||
int field_size = reflection->FieldSize(*message, field_descriptor);
|
||||
if (index < 0) {
|
||||
index = field_size + index;
|
||||
}
|
||||
if (index < 0 || index >= field_size) {
|
||||
PyErr_Format(PyExc_IndexError,
|
||||
"list assignment index (%d) out of range",
|
||||
static_cast<int>(index));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (arg == NULL) {
|
||||
ScopedPyObjectPtr py_index(PyLong_FromLong(index));
|
||||
return cmessage::InternalDeleteRepeatedField(self->parent, field_descriptor,
|
||||
py_index.get(), NULL);
|
||||
}
|
||||
|
||||
if (PySequence_Check(arg) && !(PyBytes_Check(arg) || PyUnicode_Check(arg))) {
|
||||
PyErr_SetString(PyExc_TypeError, "Value must be scalar");
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (field_descriptor->cpp_type()) {
|
||||
case FieldDescriptor::CPPTYPE_INT32: {
|
||||
GOOGLE_CHECK_GET_INT32(arg, value, -1);
|
||||
reflection->SetRepeatedInt32(message, field_descriptor, index, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_INT64: {
|
||||
GOOGLE_CHECK_GET_INT64(arg, value, -1);
|
||||
reflection->SetRepeatedInt64(message, field_descriptor, index, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_UINT32: {
|
||||
GOOGLE_CHECK_GET_UINT32(arg, value, -1);
|
||||
reflection->SetRepeatedUInt32(message, field_descriptor, index, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_UINT64: {
|
||||
GOOGLE_CHECK_GET_UINT64(arg, value, -1);
|
||||
reflection->SetRepeatedUInt64(message, field_descriptor, index, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_FLOAT: {
|
||||
GOOGLE_CHECK_GET_FLOAT(arg, value, -1);
|
||||
reflection->SetRepeatedFloat(message, field_descriptor, index, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_DOUBLE: {
|
||||
GOOGLE_CHECK_GET_DOUBLE(arg, value, -1);
|
||||
reflection->SetRepeatedDouble(message, field_descriptor, index, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_BOOL: {
|
||||
GOOGLE_CHECK_GET_BOOL(arg, value, -1);
|
||||
reflection->SetRepeatedBool(message, field_descriptor, index, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_STRING: {
|
||||
if (!CheckAndSetString(
|
||||
arg, message, field_descriptor, reflection, false, index)) {
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_ENUM: {
|
||||
GOOGLE_CHECK_GET_INT32(arg, value, -1);
|
||||
if (reflection->SupportsUnknownEnumValues()) {
|
||||
reflection->SetRepeatedEnumValue(message, field_descriptor, index,
|
||||
value);
|
||||
} else {
|
||||
const EnumDescriptor* enum_descriptor = field_descriptor->enum_type();
|
||||
const EnumValueDescriptor* enum_value =
|
||||
enum_descriptor->FindValueByNumber(value);
|
||||
if (enum_value != NULL) {
|
||||
reflection->SetRepeatedEnum(message, field_descriptor, index,
|
||||
enum_value);
|
||||
} else {
|
||||
ScopedPyObjectPtr s(PyObject_Str(arg));
|
||||
if (s != NULL) {
|
||||
PyErr_Format(PyExc_ValueError, "Unknown enum value: %s",
|
||||
PyString_AsString(s.get()));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
PyErr_Format(
|
||||
PyExc_SystemError, "Adding value to a field of unknown type %d",
|
||||
field_descriptor->cpp_type());
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static PyObject* Item(RepeatedScalarContainer* self, Py_ssize_t index) {
|
||||
Message* message = self->message;
|
||||
const FieldDescriptor* field_descriptor = self->parent_field_descriptor;
|
||||
const Reflection* reflection = message->GetReflection();
|
||||
|
||||
int field_size = reflection->FieldSize(*message, field_descriptor);
|
||||
if (index < 0) {
|
||||
index = field_size + index;
|
||||
}
|
||||
if (index < 0 || index >= field_size) {
|
||||
PyErr_Format(PyExc_IndexError,
|
||||
"list index (%zd) out of range",
|
||||
index);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject* result = NULL;
|
||||
switch (field_descriptor->cpp_type()) {
|
||||
case FieldDescriptor::CPPTYPE_INT32: {
|
||||
int32 value = reflection->GetRepeatedInt32(
|
||||
*message, field_descriptor, index);
|
||||
result = PyInt_FromLong(value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_INT64: {
|
||||
int64 value = reflection->GetRepeatedInt64(
|
||||
*message, field_descriptor, index);
|
||||
result = PyLong_FromLongLong(value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_UINT32: {
|
||||
uint32 value = reflection->GetRepeatedUInt32(
|
||||
*message, field_descriptor, index);
|
||||
result = PyLong_FromLongLong(value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_UINT64: {
|
||||
uint64 value = reflection->GetRepeatedUInt64(
|
||||
*message, field_descriptor, index);
|
||||
result = PyLong_FromUnsignedLongLong(value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_FLOAT: {
|
||||
float value = reflection->GetRepeatedFloat(
|
||||
*message, field_descriptor, index);
|
||||
result = PyFloat_FromDouble(value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_DOUBLE: {
|
||||
double value = reflection->GetRepeatedDouble(
|
||||
*message, field_descriptor, index);
|
||||
result = PyFloat_FromDouble(value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_BOOL: {
|
||||
bool value = reflection->GetRepeatedBool(
|
||||
*message, field_descriptor, index);
|
||||
result = PyBool_FromLong(value ? 1 : 0);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_ENUM: {
|
||||
const EnumValueDescriptor* enum_value =
|
||||
message->GetReflection()->GetRepeatedEnum(
|
||||
*message, field_descriptor, index);
|
||||
result = PyInt_FromLong(enum_value->number());
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_STRING: {
|
||||
string value = reflection->GetRepeatedString(
|
||||
*message, field_descriptor, index);
|
||||
result = ToStringObject(field_descriptor, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_MESSAGE: {
|
||||
PyObject* py_cmsg = PyObject_CallObject(reinterpret_cast<PyObject*>(
|
||||
&CMessage_Type), NULL);
|
||||
if (py_cmsg == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
CMessage* cmsg = reinterpret_cast<CMessage*>(py_cmsg);
|
||||
const Message& msg = reflection->GetRepeatedMessage(
|
||||
*message, field_descriptor, index);
|
||||
cmsg->owner = self->owner;
|
||||
cmsg->parent = self->parent;
|
||||
cmsg->message = const_cast<Message*>(&msg);
|
||||
cmsg->read_only = false;
|
||||
result = reinterpret_cast<PyObject*>(py_cmsg);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
PyErr_Format(
|
||||
PyExc_SystemError,
|
||||
"Getting value from a repeated field of unknown type %d",
|
||||
field_descriptor->cpp_type());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static PyObject* Subscript(RepeatedScalarContainer* self, PyObject* slice) {
|
||||
Py_ssize_t from;
|
||||
Py_ssize_t to;
|
||||
Py_ssize_t step;
|
||||
Py_ssize_t length;
|
||||
Py_ssize_t slicelength;
|
||||
bool return_list = false;
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
if (PyInt_Check(slice)) {
|
||||
from = to = PyInt_AsLong(slice);
|
||||
} else // NOLINT
|
||||
#endif
|
||||
if (PyLong_Check(slice)) {
|
||||
from = to = PyLong_AsLong(slice);
|
||||
} else if (PySlice_Check(slice)) {
|
||||
length = Len(self);
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
if (PySlice_GetIndicesEx(slice,
|
||||
#else
|
||||
if (PySlice_GetIndicesEx(reinterpret_cast<PySliceObject*>(slice),
|
||||
#endif
|
||||
length, &from, &to, &step, &slicelength) == -1) {
|
||||
return NULL;
|
||||
}
|
||||
return_list = true;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "list indices must be integers");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!return_list) {
|
||||
return Item(self, from);
|
||||
}
|
||||
|
||||
PyObject* list = PyList_New(0);
|
||||
if (list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (from <= to) {
|
||||
if (step < 0) {
|
||||
return list;
|
||||
}
|
||||
for (Py_ssize_t index = from; index < to; index += step) {
|
||||
if (index < 0 || index >= length) {
|
||||
break;
|
||||
}
|
||||
ScopedPyObjectPtr s(Item(self, index));
|
||||
PyList_Append(list, s.get());
|
||||
}
|
||||
} else {
|
||||
if (step > 0) {
|
||||
return list;
|
||||
}
|
||||
for (Py_ssize_t index = from; index > to; index += step) {
|
||||
if (index < 0 || index >= length) {
|
||||
break;
|
||||
}
|
||||
ScopedPyObjectPtr s(Item(self, index));
|
||||
PyList_Append(list, s.get());
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
PyObject* Append(RepeatedScalarContainer* self, PyObject* item) {
|
||||
cmessage::AssureWritable(self->parent);
|
||||
Message* message = self->message;
|
||||
const FieldDescriptor* field_descriptor = self->parent_field_descriptor;
|
||||
|
||||
const Reflection* reflection = message->GetReflection();
|
||||
switch (field_descriptor->cpp_type()) {
|
||||
case FieldDescriptor::CPPTYPE_INT32: {
|
||||
GOOGLE_CHECK_GET_INT32(item, value, NULL);
|
||||
reflection->AddInt32(message, field_descriptor, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_INT64: {
|
||||
GOOGLE_CHECK_GET_INT64(item, value, NULL);
|
||||
reflection->AddInt64(message, field_descriptor, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_UINT32: {
|
||||
GOOGLE_CHECK_GET_UINT32(item, value, NULL);
|
||||
reflection->AddUInt32(message, field_descriptor, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_UINT64: {
|
||||
GOOGLE_CHECK_GET_UINT64(item, value, NULL);
|
||||
reflection->AddUInt64(message, field_descriptor, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_FLOAT: {
|
||||
GOOGLE_CHECK_GET_FLOAT(item, value, NULL);
|
||||
reflection->AddFloat(message, field_descriptor, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_DOUBLE: {
|
||||
GOOGLE_CHECK_GET_DOUBLE(item, value, NULL);
|
||||
reflection->AddDouble(message, field_descriptor, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_BOOL: {
|
||||
GOOGLE_CHECK_GET_BOOL(item, value, NULL);
|
||||
reflection->AddBool(message, field_descriptor, value);
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_STRING: {
|
||||
if (!CheckAndSetString(
|
||||
item, message, field_descriptor, reflection, true, -1)) {
|
||||
return NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FieldDescriptor::CPPTYPE_ENUM: {
|
||||
GOOGLE_CHECK_GET_INT32(item, value, NULL);
|
||||
if (reflection->SupportsUnknownEnumValues()) {
|
||||
reflection->AddEnumValue(message, field_descriptor, value);
|
||||
} else {
|
||||
const EnumDescriptor* enum_descriptor = field_descriptor->enum_type();
|
||||
const EnumValueDescriptor* enum_value =
|
||||
enum_descriptor->FindValueByNumber(value);
|
||||
if (enum_value != NULL) {
|
||||
reflection->AddEnum(message, field_descriptor, enum_value);
|
||||
} else {
|
||||
ScopedPyObjectPtr s(PyObject_Str(item));
|
||||
if (s != NULL) {
|
||||
PyErr_Format(PyExc_ValueError, "Unknown enum value: %s",
|
||||
PyString_AsString(s.get()));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
PyErr_Format(
|
||||
PyExc_SystemError, "Adding value to a field of unknown type %d",
|
||||
field_descriptor->cpp_type());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static int AssSubscript(RepeatedScalarContainer* self,
|
||||
PyObject* slice,
|
||||
PyObject* value) {
|
||||
Py_ssize_t from;
|
||||
Py_ssize_t to;
|
||||
Py_ssize_t step;
|
||||
Py_ssize_t length;
|
||||
Py_ssize_t slicelength;
|
||||
bool create_list = false;
|
||||
|
||||
cmessage::AssureWritable(self->parent);
|
||||
Message* message = self->message;
|
||||
const FieldDescriptor* field_descriptor =
|
||||
self->parent_field_descriptor;
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
if (PyInt_Check(slice)) {
|
||||
from = to = PyInt_AsLong(slice);
|
||||
} else
|
||||
#endif
|
||||
if (PyLong_Check(slice)) {
|
||||
from = to = PyLong_AsLong(slice);
|
||||
} else if (PySlice_Check(slice)) {
|
||||
const Reflection* reflection = message->GetReflection();
|
||||
length = reflection->FieldSize(*message, field_descriptor);
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
if (PySlice_GetIndicesEx(slice,
|
||||
#else
|
||||
if (PySlice_GetIndicesEx(reinterpret_cast<PySliceObject*>(slice),
|
||||
#endif
|
||||
length, &from, &to, &step, &slicelength) == -1) {
|
||||
return -1;
|
||||
}
|
||||
create_list = true;
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "list indices must be integers");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (value == NULL) {
|
||||
return cmessage::InternalDeleteRepeatedField(
|
||||
self->parent, field_descriptor, slice, NULL);
|
||||
}
|
||||
|
||||
if (!create_list) {
|
||||
return AssignItem(self, from, value);
|
||||
}
|
||||
|
||||
ScopedPyObjectPtr full_slice(PySlice_New(NULL, NULL, NULL));
|
||||
if (full_slice == NULL) {
|
||||
return -1;
|
||||
}
|
||||
ScopedPyObjectPtr new_list(Subscript(self, full_slice.get()));
|
||||
if (new_list == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (PySequence_SetSlice(new_list.get(), from, to, value) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return InternalAssignRepeatedField(self, new_list.get());
|
||||
}
|
||||
|
||||
PyObject* Extend(RepeatedScalarContainer* self, PyObject* value) {
|
||||
cmessage::AssureWritable(self->parent);
|
||||
|
||||
// TODO(ptucker): Deprecate this behavior. b/18413862
|
||||
if (value == Py_None) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
if ((Py_TYPE(value)->tp_as_sequence == NULL) && PyObject_Not(value)) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
ScopedPyObjectPtr iter(PyObject_GetIter(value));
|
||||
if (iter == NULL) {
|
||||
PyErr_SetString(PyExc_TypeError, "Value must be iterable");
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr next;
|
||||
while ((next.reset(PyIter_Next(iter.get()))) != NULL) {
|
||||
if (ScopedPyObjectPtr(Append(self, next.get())) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if (PyErr_Occurred()) {
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject* Insert(RepeatedScalarContainer* self, PyObject* args) {
|
||||
Py_ssize_t index;
|
||||
PyObject* value;
|
||||
if (!PyArg_ParseTuple(args, "lO", &index, &value)) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr full_slice(PySlice_New(NULL, NULL, NULL));
|
||||
ScopedPyObjectPtr new_list(Subscript(self, full_slice.get()));
|
||||
if (PyList_Insert(new_list.get(), index, value) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
int ret = InternalAssignRepeatedField(self, new_list.get());
|
||||
if (ret < 0) {
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject* Remove(RepeatedScalarContainer* self, PyObject* value) {
|
||||
Py_ssize_t match_index = -1;
|
||||
for (Py_ssize_t i = 0; i < Len(self); ++i) {
|
||||
ScopedPyObjectPtr elem(Item(self, i));
|
||||
if (PyObject_RichCompareBool(elem.get(), value, Py_EQ)) {
|
||||
match_index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (match_index == -1) {
|
||||
PyErr_SetString(PyExc_ValueError, "remove(x): x not in container");
|
||||
return NULL;
|
||||
}
|
||||
if (AssignItem(self, match_index, NULL) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject* RichCompare(RepeatedScalarContainer* self,
|
||||
PyObject* other,
|
||||
int opid) {
|
||||
if (opid != Py_EQ && opid != Py_NE) {
|
||||
Py_INCREF(Py_NotImplemented);
|
||||
return Py_NotImplemented;
|
||||
}
|
||||
|
||||
// Copy the contents of this repeated scalar container, and other if it is
|
||||
// also a repeated scalar container, into Python lists so we can delegate
|
||||
// to the list's compare method.
|
||||
|
||||
ScopedPyObjectPtr full_slice(PySlice_New(NULL, NULL, NULL));
|
||||
if (full_slice == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ScopedPyObjectPtr other_list_deleter;
|
||||
if (PyObject_TypeCheck(other, &RepeatedScalarContainer_Type)) {
|
||||
other_list_deleter.reset(Subscript(
|
||||
reinterpret_cast<RepeatedScalarContainer*>(other), full_slice.get()));
|
||||
other = other_list_deleter.get();
|
||||
}
|
||||
|
||||
ScopedPyObjectPtr list(Subscript(self, full_slice.get()));
|
||||
if (list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return PyObject_RichCompare(list.get(), other, opid);
|
||||
}
|
||||
|
||||
PyObject* Reduce(RepeatedScalarContainer* unused_self) {
|
||||
PyErr_Format(
|
||||
PickleError_class,
|
||||
"can't pickle repeated message fields, convert to list first");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static PyObject* Sort(RepeatedScalarContainer* self,
|
||||
PyObject* args,
|
||||
PyObject* kwds) {
|
||||
// Support the old sort_function argument for backwards
|
||||
// compatibility.
|
||||
if (kwds != NULL) {
|
||||
PyObject* sort_func = PyDict_GetItemString(kwds, "sort_function");
|
||||
if (sort_func != NULL) {
|
||||
// Must set before deleting as sort_func is a borrowed reference
|
||||
// and kwds might be the only thing keeping it alive.
|
||||
if (PyDict_SetItemString(kwds, "cmp", sort_func) == -1)
|
||||
return NULL;
|
||||
if (PyDict_DelItemString(kwds, "sort_function") == -1)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
ScopedPyObjectPtr full_slice(PySlice_New(NULL, NULL, NULL));
|
||||
if (full_slice == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr list(Subscript(self, full_slice.get()));
|
||||
if (list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr m(PyObject_GetAttrString(list.get(), "sort"));
|
||||
if (m == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ScopedPyObjectPtr res(PyObject_Call(m.get(), args, kwds));
|
||||
if (res == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
int ret = InternalAssignRepeatedField(self, list.get());
|
||||
if (ret < 0) {
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject* Pop(RepeatedScalarContainer* self,
|
||||
PyObject* args) {
|
||||
Py_ssize_t index = -1;
|
||||
if (!PyArg_ParseTuple(args, "|n", &index)) {
|
||||
return NULL;
|
||||
}
|
||||
PyObject* item = Item(self, index);
|
||||
if (item == NULL) {
|
||||
PyErr_Format(PyExc_IndexError,
|
||||
"list index (%zd) out of range",
|
||||
index);
|
||||
return NULL;
|
||||
}
|
||||
if (AssignItem(self, index, NULL) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
// The private constructor of RepeatedScalarContainer objects.
|
||||
PyObject *NewContainer(
|
||||
CMessage* parent, const FieldDescriptor* parent_field_descriptor) {
|
||||
if (!CheckFieldBelongsToMessage(parent_field_descriptor, parent->message)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RepeatedScalarContainer* self = reinterpret_cast<RepeatedScalarContainer*>(
|
||||
PyType_GenericAlloc(&RepeatedScalarContainer_Type, 0));
|
||||
if (self == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
self->message = parent->message;
|
||||
self->parent = parent;
|
||||
self->parent_field_descriptor = parent_field_descriptor;
|
||||
self->owner = parent->owner;
|
||||
|
||||
return reinterpret_cast<PyObject*>(self);
|
||||
}
|
||||
|
||||
// Initializes the underlying Message object of "to" so it becomes a new parent
|
||||
// repeated scalar, and copies all the values from "from" to it. A child scalar
|
||||
// container can be released by passing it as both from and to (e.g. making it
|
||||
// the recipient of the new parent message and copying the values from itself).
|
||||
static int InitializeAndCopyToParentContainer(
|
||||
RepeatedScalarContainer* from,
|
||||
RepeatedScalarContainer* to) {
|
||||
ScopedPyObjectPtr full_slice(PySlice_New(NULL, NULL, NULL));
|
||||
if (full_slice == NULL) {
|
||||
return -1;
|
||||
}
|
||||
ScopedPyObjectPtr values(Subscript(from, full_slice.get()));
|
||||
if (values == NULL) {
|
||||
return -1;
|
||||
}
|
||||
Message* new_message = from->message->New();
|
||||
to->parent = NULL;
|
||||
to->parent_field_descriptor = from->parent_field_descriptor;
|
||||
to->message = new_message;
|
||||
to->owner.reset(new_message);
|
||||
if (InternalAssignRepeatedField(to, values.get()) < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Release(RepeatedScalarContainer* self) {
|
||||
return InitializeAndCopyToParentContainer(self, self);
|
||||
}
|
||||
|
||||
PyObject* DeepCopy(RepeatedScalarContainer* self, PyObject* arg) {
|
||||
RepeatedScalarContainer* clone = reinterpret_cast<RepeatedScalarContainer*>(
|
||||
PyType_GenericAlloc(&RepeatedScalarContainer_Type, 0));
|
||||
if (clone == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (InitializeAndCopyToParentContainer(self, clone) < 0) {
|
||||
Py_DECREF(clone);
|
||||
return NULL;
|
||||
}
|
||||
return reinterpret_cast<PyObject*>(clone);
|
||||
}
|
||||
|
||||
static void Dealloc(RepeatedScalarContainer* self) {
|
||||
self->owner.reset();
|
||||
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
|
||||
}
|
||||
|
||||
void SetOwner(RepeatedScalarContainer* self,
|
||||
const shared_ptr<Message>& new_owner) {
|
||||
self->owner = new_owner;
|
||||
}
|
||||
|
||||
static PySequenceMethods SqMethods = {
|
||||
(lenfunc)Len, /* sq_length */
|
||||
0, /* sq_concat */
|
||||
0, /* sq_repeat */
|
||||
(ssizeargfunc)Item, /* sq_item */
|
||||
0, /* sq_slice */
|
||||
(ssizeobjargproc)AssignItem /* sq_ass_item */
|
||||
};
|
||||
|
||||
static PyMappingMethods MpMethods = {
|
||||
(lenfunc)Len, /* mp_length */
|
||||
(binaryfunc)Subscript, /* mp_subscript */
|
||||
(objobjargproc)AssSubscript, /* mp_ass_subscript */
|
||||
};
|
||||
|
||||
static PyMethodDef Methods[] = {
|
||||
{ "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS,
|
||||
"Makes a deep copy of the class." },
|
||||
{ "__reduce__", (PyCFunction)Reduce, METH_NOARGS,
|
||||
"Outputs picklable representation of the repeated field." },
|
||||
{ "append", (PyCFunction)Append, METH_O,
|
||||
"Appends an object to the repeated container." },
|
||||
{ "extend", (PyCFunction)Extend, METH_O,
|
||||
"Appends objects to the repeated container." },
|
||||
{ "insert", (PyCFunction)Insert, METH_VARARGS,
|
||||
"Appends objects to the repeated container." },
|
||||
{ "pop", (PyCFunction)Pop, METH_VARARGS,
|
||||
"Removes an object from the repeated container and returns it." },
|
||||
{ "remove", (PyCFunction)Remove, METH_O,
|
||||
"Removes an object from the repeated container." },
|
||||
{ "sort", (PyCFunction)Sort, METH_VARARGS | METH_KEYWORDS,
|
||||
"Sorts the repeated container."},
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
} // namespace repeated_scalar_container
|
||||
|
||||
PyTypeObject RepeatedScalarContainer_Type = {
|
||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||
FULL_MODULE_NAME ".RepeatedScalarContainer", // tp_name
|
||||
sizeof(RepeatedScalarContainer), // tp_basicsize
|
||||
0, // tp_itemsize
|
||||
(destructor)repeated_scalar_container::Dealloc, // tp_dealloc
|
||||
0, // tp_print
|
||||
0, // tp_getattr
|
||||
0, // tp_setattr
|
||||
0, // tp_compare
|
||||
0, // tp_repr
|
||||
0, // tp_as_number
|
||||
&repeated_scalar_container::SqMethods, // tp_as_sequence
|
||||
&repeated_scalar_container::MpMethods, // tp_as_mapping
|
||||
PyObject_HashNotImplemented, // tp_hash
|
||||
0, // tp_call
|
||||
0, // tp_str
|
||||
0, // tp_getattro
|
||||
0, // tp_setattro
|
||||
0, // tp_as_buffer
|
||||
Py_TPFLAGS_DEFAULT, // tp_flags
|
||||
"A Repeated scalar container", // tp_doc
|
||||
0, // tp_traverse
|
||||
0, // tp_clear
|
||||
(richcmpfunc)repeated_scalar_container::RichCompare, // tp_richcompare
|
||||
0, // tp_weaklistoffset
|
||||
0, // tp_iter
|
||||
0, // tp_iternext
|
||||
repeated_scalar_container::Methods, // tp_methods
|
||||
0, // tp_members
|
||||
0, // tp_getset
|
||||
0, // tp_base
|
||||
0, // tp_dict
|
||||
0, // tp_descr_get
|
||||
0, // tp_descr_set
|
||||
0, // tp_dictoffset
|
||||
0, // tp_init
|
||||
};
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
@@ -0,0 +1,122 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: anuraag@google.com (Anuraag Agrawal)
|
||||
// Author: tibell@google.com (Johan Tibell)
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <memory>
|
||||
#ifndef _SHARED_PTR_H
|
||||
#include <google/protobuf/stubs/shared_ptr.h>
|
||||
#endif
|
||||
|
||||
#include <google/protobuf/descriptor.h>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
|
||||
class Message;
|
||||
|
||||
#ifdef _SHARED_PTR_H
|
||||
using std::shared_ptr;
|
||||
#else
|
||||
using internal::shared_ptr;
|
||||
#endif
|
||||
|
||||
namespace python {
|
||||
|
||||
struct CMessage;
|
||||
|
||||
typedef struct RepeatedScalarContainer {
|
||||
PyObject_HEAD;
|
||||
|
||||
// This is the top-level C++ Message object that owns the whole
|
||||
// proto tree. Every Python RepeatedScalarContainer holds a
|
||||
// reference to it in order to keep it alive as long as there's a
|
||||
// Python object that references any part of the tree.
|
||||
shared_ptr<Message> owner;
|
||||
|
||||
// Pointer to the C++ Message that contains this container. The
|
||||
// RepeatedScalarContainer does not own this pointer.
|
||||
Message* message;
|
||||
|
||||
// Weak reference to a parent CMessage object (i.e. may be NULL.)
|
||||
//
|
||||
// Used to make sure all ancestors are also mutable when first
|
||||
// modifying the container.
|
||||
CMessage* parent;
|
||||
|
||||
// Pointer to the parent's descriptor that describes this
|
||||
// field. Used together with the parent's message when making a
|
||||
// default message instance mutable.
|
||||
// The pointer is owned by the global DescriptorPool.
|
||||
const FieldDescriptor* parent_field_descriptor;
|
||||
} RepeatedScalarContainer;
|
||||
|
||||
extern PyTypeObject RepeatedScalarContainer_Type;
|
||||
|
||||
namespace repeated_scalar_container {
|
||||
|
||||
// Builds a RepeatedScalarContainer object, from a parent message and a
|
||||
// field descriptor.
|
||||
extern PyObject *NewContainer(
|
||||
CMessage* parent, const FieldDescriptor* parent_field_descriptor);
|
||||
|
||||
// Appends the scalar 'item' to the end of the container 'self'.
|
||||
//
|
||||
// Returns None if successful; returns NULL and sets an exception if
|
||||
// unsuccessful.
|
||||
PyObject* Append(RepeatedScalarContainer* self, PyObject* item);
|
||||
|
||||
// Releases the messages in the container to a new message.
|
||||
//
|
||||
// Returns 0 on success, -1 on failure.
|
||||
int Release(RepeatedScalarContainer* self);
|
||||
|
||||
// Appends all the elements in the input iterator to the container.
|
||||
//
|
||||
// Returns None if successful; returns NULL and sets an exception if
|
||||
// unsuccessful.
|
||||
PyObject* Extend(RepeatedScalarContainer* self, PyObject* value);
|
||||
|
||||
// Set the owner field of self and any children of self.
|
||||
void SetOwner(RepeatedScalarContainer* self,
|
||||
const shared_ptr<Message>& new_owner);
|
||||
|
||||
} // namespace repeated_scalar_container
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_REPEATED_SCALAR_CONTAINER_H__
|
||||
@@ -0,0 +1,164 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_SAFE_NUMERICS_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_SAFE_NUMERICS_H__
|
||||
// Copied from chromium with only changes to the namespace.
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <google/protobuf/stubs/logging.h>
|
||||
#include <google/protobuf/stubs/common.h>
|
||||
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
template <bool SameSize, bool DestLarger,
|
||||
bool DestIsSigned, bool SourceIsSigned>
|
||||
struct IsValidNumericCastImpl;
|
||||
|
||||
#define BASE_NUMERIC_CAST_CASE_SPECIALIZATION(A, B, C, D, Code) \
|
||||
template <> struct IsValidNumericCastImpl<A, B, C, D> { \
|
||||
template <class Source, class DestBounds> static inline bool Test( \
|
||||
Source source, DestBounds min, DestBounds max) { \
|
||||
return Code; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define BASE_NUMERIC_CAST_CASE_SAME_SIZE(DestSigned, SourceSigned, Code) \
|
||||
BASE_NUMERIC_CAST_CASE_SPECIALIZATION( \
|
||||
true, true, DestSigned, SourceSigned, Code); \
|
||||
BASE_NUMERIC_CAST_CASE_SPECIALIZATION( \
|
||||
true, false, DestSigned, SourceSigned, Code)
|
||||
|
||||
#define BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(DestSigned, SourceSigned, Code) \
|
||||
BASE_NUMERIC_CAST_CASE_SPECIALIZATION( \
|
||||
false, false, DestSigned, SourceSigned, Code); \
|
||||
|
||||
#define BASE_NUMERIC_CAST_CASE_DEST_LARGER(DestSigned, SourceSigned, Code) \
|
||||
BASE_NUMERIC_CAST_CASE_SPECIALIZATION( \
|
||||
false, true, DestSigned, SourceSigned, Code); \
|
||||
|
||||
// The three top level cases are:
|
||||
// - Same size
|
||||
// - Source larger
|
||||
// - Dest larger
|
||||
// And for each of those three cases, we handle the 4 different possibilities
|
||||
// of signed and unsigned. This gives 12 cases to handle, which we enumerate
|
||||
// below.
|
||||
//
|
||||
// The last argument in each of the macros is the actual comparison code. It
|
||||
// has three arguments available, source (the value), and min/max which are
|
||||
// the ranges of the destination.
|
||||
|
||||
|
||||
// These are the cases where both types have the same size.
|
||||
|
||||
// Both signed.
|
||||
BASE_NUMERIC_CAST_CASE_SAME_SIZE(true, true, true);
|
||||
// Both unsigned.
|
||||
BASE_NUMERIC_CAST_CASE_SAME_SIZE(false, false, true);
|
||||
// Dest unsigned, Source signed.
|
||||
BASE_NUMERIC_CAST_CASE_SAME_SIZE(false, true, source >= 0);
|
||||
// Dest signed, Source unsigned.
|
||||
// This cast is OK because Dest's max must be less than Source's.
|
||||
BASE_NUMERIC_CAST_CASE_SAME_SIZE(true, false,
|
||||
source <= static_cast<Source>(max));
|
||||
|
||||
|
||||
// These are the cases where Source is larger.
|
||||
|
||||
// Both unsigned.
|
||||
BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(false, false, source <= max);
|
||||
// Both signed.
|
||||
BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(true, true,
|
||||
source >= min && source <= max);
|
||||
// Dest is unsigned, Source is signed.
|
||||
BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(false, true,
|
||||
source >= 0 && source <= max);
|
||||
// Dest is signed, Source is unsigned.
|
||||
// This cast is OK because Dest's max must be less than Source's.
|
||||
BASE_NUMERIC_CAST_CASE_SOURCE_LARGER(true, false,
|
||||
source <= static_cast<Source>(max));
|
||||
|
||||
|
||||
// These are the cases where Dest is larger.
|
||||
|
||||
// Both unsigned.
|
||||
BASE_NUMERIC_CAST_CASE_DEST_LARGER(false, false, true);
|
||||
// Both signed.
|
||||
BASE_NUMERIC_CAST_CASE_DEST_LARGER(true, true, true);
|
||||
// Dest is unsigned, Source is signed.
|
||||
BASE_NUMERIC_CAST_CASE_DEST_LARGER(false, true, source >= 0);
|
||||
// Dest is signed, Source is unsigned.
|
||||
BASE_NUMERIC_CAST_CASE_DEST_LARGER(true, false, true);
|
||||
|
||||
#undef BASE_NUMERIC_CAST_CASE_SPECIALIZATION
|
||||
#undef BASE_NUMERIC_CAST_CASE_SAME_SIZE
|
||||
#undef BASE_NUMERIC_CAST_CASE_SOURCE_LARGER
|
||||
#undef BASE_NUMERIC_CAST_CASE_DEST_LARGER
|
||||
|
||||
|
||||
// The main test for whether the conversion will under or overflow.
|
||||
template <class Dest, class Source>
|
||||
inline bool IsValidNumericCast(Source source) {
|
||||
typedef std::numeric_limits<Source> SourceLimits;
|
||||
typedef std::numeric_limits<Dest> DestLimits;
|
||||
GOOGLE_COMPILE_ASSERT(SourceLimits::is_specialized, argument_must_be_numeric);
|
||||
GOOGLE_COMPILE_ASSERT(SourceLimits::is_integer, argument_must_be_integral);
|
||||
GOOGLE_COMPILE_ASSERT(DestLimits::is_specialized, result_must_be_numeric);
|
||||
GOOGLE_COMPILE_ASSERT(DestLimits::is_integer, result_must_be_integral);
|
||||
|
||||
return IsValidNumericCastImpl<
|
||||
sizeof(Dest) == sizeof(Source),
|
||||
(sizeof(Dest) > sizeof(Source)),
|
||||
DestLimits::is_signed,
|
||||
SourceLimits::is_signed>::Test(
|
||||
source,
|
||||
DestLimits::min(),
|
||||
DestLimits::max());
|
||||
}
|
||||
|
||||
// checked_numeric_cast<> is analogous to static_cast<> for numeric types,
|
||||
// except that it CHECKs that the specified numeric conversion will not
|
||||
// overflow or underflow. Floating point arguments are not currently allowed
|
||||
// (this is COMPILE_ASSERTd), though this could be supported if necessary.
|
||||
template <class Dest, class Source>
|
||||
inline Dest checked_numeric_cast(Source source) {
|
||||
GOOGLE_CHECK(IsValidNumericCast<Dest>(source));
|
||||
return static_cast<Dest>(source);
|
||||
}
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_SAFE_NUMERICS_H__
|
||||
@@ -0,0 +1,105 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. 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 Inc. 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.
|
||||
|
||||
// Author: tibell@google.com (Johan Tibell)
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_SCOPED_PYOBJECT_PTR_H__
|
||||
#define GOOGLE_PROTOBUF_PYTHON_CPP_SCOPED_PYOBJECT_PTR_H__
|
||||
|
||||
#include <google/protobuf/stubs/common.h>
|
||||
|
||||
#include <Python.h>
|
||||
namespace google {
|
||||
namespace protobuf {
|
||||
namespace python {
|
||||
|
||||
// Owns a python object and decrements the reference count on destruction.
|
||||
// This class is not threadsafe.
|
||||
template <typename PyObjectStruct>
|
||||
class ScopedPythonPtr {
|
||||
public:
|
||||
// Takes the ownership of the specified object to ScopedPythonPtr.
|
||||
// The reference count of the specified py_object is not incremented.
|
||||
explicit ScopedPythonPtr(PyObjectStruct* py_object = NULL)
|
||||
: ptr_(py_object) {}
|
||||
|
||||
// If a PyObject is owned, decrement its reference count.
|
||||
~ScopedPythonPtr() { Py_XDECREF(ptr_); }
|
||||
|
||||
// Deletes the current owned object, if any.
|
||||
// Then takes ownership of a new object without incrementing the reference
|
||||
// count.
|
||||
// This function must be called with a reference that you own.
|
||||
// this->reset(this->get()) is wrong!
|
||||
// this->reset(this->release()) is OK.
|
||||
PyObjectStruct* reset(PyObjectStruct* p = NULL) {
|
||||
Py_XDECREF(ptr_);
|
||||
ptr_ = p;
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
// Releases ownership of the object without decrementing the reference count.
|
||||
// The caller now owns the returned reference.
|
||||
PyObjectStruct* release() {
|
||||
PyObject* p = ptr_;
|
||||
ptr_ = NULL;
|
||||
return p;
|
||||
}
|
||||
|
||||
PyObjectStruct* operator->() const {
|
||||
assert(ptr_ != NULL);
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
PyObjectStruct* get() const { return ptr_; }
|
||||
|
||||
PyObject* as_pyobject() const { return reinterpret_cast<PyObject*>(ptr_); }
|
||||
|
||||
// Increments the reference count fo the current object.
|
||||
// Should not be called when no object is held.
|
||||
void inc() const { Py_INCREF(ptr_); }
|
||||
|
||||
// True when a ScopedPyObjectPtr and a raw pointer refer to the same object.
|
||||
// Comparison operators are non reflexive.
|
||||
bool operator==(const PyObjectStruct* p) const { return ptr_ == p; }
|
||||
bool operator!=(const PyObjectStruct* p) const { return ptr_ != p; }
|
||||
|
||||
private:
|
||||
PyObjectStruct* ptr_;
|
||||
|
||||
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ScopedPythonPtr);
|
||||
};
|
||||
|
||||
typedef ScopedPythonPtr<PyObject> ScopedPyObjectPtr;
|
||||
|
||||
} // namespace python
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
#endif // GOOGLE_PROTOBUF_PYTHON_CPP_SCOPED_PYOBJECT_PTR_H__
|
||||
Reference in new issue
Block a user