blob: f081df842d3a8a147d1ca30ecb4cb8058bcc36ac [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// Author: petar@google.com (Petar Petrov)
32
33#ifndef GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__
34#define GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__
35
36#include <Python.h>
37
38#include <google/protobuf/descriptor.h>
39
40namespace google {
41namespace protobuf {
42namespace python {
43
44extern PyTypeObject PyMessageDescriptor_Type;
45extern PyTypeObject PyFieldDescriptor_Type;
46extern PyTypeObject PyEnumDescriptor_Type;
47extern PyTypeObject PyEnumValueDescriptor_Type;
48extern PyTypeObject PyFileDescriptor_Type;
49extern PyTypeObject PyOneofDescriptor_Type;
Austin Schuh40c16522018-10-28 20:27:54 -070050extern PyTypeObject PyServiceDescriptor_Type;
51extern PyTypeObject PyMethodDescriptor_Type;
Brian Silverman9c614bc2016-02-15 20:20:02 -050052
53// Wraps a Descriptor in a Python object.
54// The C++ pointer is usually borrowed from the global DescriptorPool.
55// In any case, it must stay alive as long as the Python object.
56// Returns a new reference.
57PyObject* PyMessageDescriptor_FromDescriptor(const Descriptor* descriptor);
58PyObject* PyFieldDescriptor_FromDescriptor(const FieldDescriptor* descriptor);
59PyObject* PyEnumDescriptor_FromDescriptor(const EnumDescriptor* descriptor);
60PyObject* PyEnumValueDescriptor_FromDescriptor(
61 const EnumValueDescriptor* descriptor);
62PyObject* PyOneofDescriptor_FromDescriptor(const OneofDescriptor* descriptor);
63PyObject* PyFileDescriptor_FromDescriptor(
64 const FileDescriptor* file_descriptor);
Austin Schuh40c16522018-10-28 20:27:54 -070065PyObject* PyServiceDescriptor_FromDescriptor(
66 const ServiceDescriptor* descriptor);
67PyObject* PyMethodDescriptor_FromDescriptor(
68 const MethodDescriptor* descriptor);
Brian Silverman9c614bc2016-02-15 20:20:02 -050069
70// Alternate constructor of PyFileDescriptor, used when we already have a
71// serialized FileDescriptorProto that can be cached.
72// Returns a new reference.
73PyObject* PyFileDescriptor_FromDescriptorWithSerializedPb(
74 const FileDescriptor* file_descriptor, PyObject* serialized_pb);
75
76// Return the C++ descriptor pointer.
77// This function checks the parameter type; on error, return NULL with a Python
78// exception set.
79const Descriptor* PyMessageDescriptor_AsDescriptor(PyObject* obj);
80const FieldDescriptor* PyFieldDescriptor_AsDescriptor(PyObject* obj);
81const EnumDescriptor* PyEnumDescriptor_AsDescriptor(PyObject* obj);
82const FileDescriptor* PyFileDescriptor_AsDescriptor(PyObject* obj);
Austin Schuh40c16522018-10-28 20:27:54 -070083const ServiceDescriptor* PyServiceDescriptor_AsDescriptor(PyObject* obj);
84const MethodDescriptor* PyMethodDescriptor_AsDescriptor(PyObject* obj);
Brian Silverman9c614bc2016-02-15 20:20:02 -050085
86// Returns the raw C++ pointer.
87const void* PyDescriptor_AsVoidPtr(PyObject* obj);
88
89// Check that the calling Python code is the global scope of a _pb2.py module.
90// This function is used to support the current code generated by the proto
91// compiler, which insists on modifying descriptors after they have been
92// created.
93//
94// stacklevel indicates which Python frame should be the _pb2.py module.
95//
96// Don't use this function outside descriptor classes.
97bool _CalledFromGeneratedFile(int stacklevel);
98
99bool InitDescriptor();
100
101} // namespace python
102} // namespace protobuf
103
104} // namespace google
105#endif // GOOGLE_PROTOBUF_PYTHON_CPP_DESCRIPTOR_H__