OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 23 matching lines...) Expand all Loading... |
34 #include <frameobject.h> | 34 #include <frameobject.h> |
35 #include <string> | 35 #include <string> |
36 | 36 |
37 #include <google/protobuf/io/coded_stream.h> | 37 #include <google/protobuf/io/coded_stream.h> |
38 #include <google/protobuf/descriptor.pb.h> | 38 #include <google/protobuf/descriptor.pb.h> |
39 #include <google/protobuf/dynamic_message.h> | 39 #include <google/protobuf/dynamic_message.h> |
40 #include <google/protobuf/pyext/descriptor.h> | 40 #include <google/protobuf/pyext/descriptor.h> |
41 #include <google/protobuf/pyext/descriptor_containers.h> | 41 #include <google/protobuf/pyext/descriptor_containers.h> |
42 #include <google/protobuf/pyext/descriptor_pool.h> | 42 #include <google/protobuf/pyext/descriptor_pool.h> |
43 #include <google/protobuf/pyext/message.h> | 43 #include <google/protobuf/pyext/message.h> |
| 44 #include <google/protobuf/pyext/message_factory.h> |
44 #include <google/protobuf/pyext/scoped_pyobject_ptr.h> | 45 #include <google/protobuf/pyext/scoped_pyobject_ptr.h> |
45 | 46 |
46 #if PY_MAJOR_VERSION >= 3 | 47 #if PY_MAJOR_VERSION >= 3 |
47 #define PyString_FromStringAndSize PyUnicode_FromStringAndSize | 48 #define PyString_FromStringAndSize PyUnicode_FromStringAndSize |
48 #define PyString_Check PyUnicode_Check | 49 #define PyString_Check PyUnicode_Check |
49 #define PyString_InternFromString PyUnicode_InternFromString | 50 #define PyString_InternFromString PyUnicode_InternFromString |
50 #define PyInt_FromLong PyLong_FromLong | 51 #define PyInt_FromLong PyLong_FromLong |
51 #define PyInt_FromSize_t PyLong_FromSize_t | 52 #define PyInt_FromSize_t PyLong_FromSize_t |
52 #if PY_VERSION_HEX < 0x03030000 | 53 #if PY_VERSION_HEX < 0x03030000 |
53 #error "Python 3.0 - 3.2 are not supported." | 54 #error "Python 3.0 - 3.2 are not supported." |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 return descriptor; | 166 return descriptor; |
166 } | 167 } |
167 template<> | 168 template<> |
168 const FileDescriptor* GetFileDescriptor(const EnumValueDescriptor* descriptor) { | 169 const FileDescriptor* GetFileDescriptor(const EnumValueDescriptor* descriptor) { |
169 return descriptor->type()->file(); | 170 return descriptor->type()->file(); |
170 } | 171 } |
171 template<> | 172 template<> |
172 const FileDescriptor* GetFileDescriptor(const OneofDescriptor* descriptor) { | 173 const FileDescriptor* GetFileDescriptor(const OneofDescriptor* descriptor) { |
173 return descriptor->containing_type()->file(); | 174 return descriptor->containing_type()->file(); |
174 } | 175 } |
| 176 template<> |
| 177 const FileDescriptor* GetFileDescriptor(const MethodDescriptor* descriptor) { |
| 178 return descriptor->service()->file(); |
| 179 } |
175 | 180 |
176 // Converts options into a Python protobuf, and cache the result. | 181 // Converts options into a Python protobuf, and cache the result. |
177 // | 182 // |
178 // This is a bit tricky because options can contain extension fields defined in | 183 // This is a bit tricky because options can contain extension fields defined in |
179 // the same proto file. In this case the options parsed from the serialized_pb | 184 // the same proto file. In this case the options parsed from the serialized_pb |
180 // have unkown fields, and we need to parse them again. | 185 // have unknown fields, and we need to parse them again. |
181 // | 186 // |
182 // Always returns a new reference. | 187 // Always returns a new reference. |
183 template<class DescriptorClass> | 188 template<class DescriptorClass> |
184 static PyObject* GetOrBuildOptions(const DescriptorClass *descriptor) { | 189 static PyObject* GetOrBuildOptions(const DescriptorClass *descriptor) { |
185 // Options (and their extensions) are completely resolved in the proto file | 190 // Options (and their extensions) are completely resolved in the proto file |
186 // containing the descriptor. | 191 // containing the descriptor. |
187 PyDescriptorPool* pool = GetDescriptorPool_FromPool( | 192 PyDescriptorPool* pool = GetDescriptorPool_FromPool( |
188 GetFileDescriptor(descriptor)->pool()); | 193 GetFileDescriptor(descriptor)->pool()); |
189 | 194 |
190 hash_map<const void*, PyObject*>* descriptor_options = | 195 hash_map<const void*, PyObject*>* descriptor_options = |
191 pool->descriptor_options; | 196 pool->descriptor_options; |
192 // First search in the cache. | 197 // First search in the cache. |
193 if (descriptor_options->find(descriptor) != descriptor_options->end()) { | 198 if (descriptor_options->find(descriptor) != descriptor_options->end()) { |
194 PyObject *value = (*descriptor_options)[descriptor]; | 199 PyObject *value = (*descriptor_options)[descriptor]; |
195 Py_INCREF(value); | 200 Py_INCREF(value); |
196 return value; | 201 return value; |
197 } | 202 } |
198 | 203 |
199 // Build the Options object: get its Python class, and make a copy of the C++ | 204 // Build the Options object: get its Python class, and make a copy of the C++ |
200 // read-only instance. | 205 // read-only instance. |
201 const Message& options(descriptor->options()); | 206 const Message& options(descriptor->options()); |
202 const Descriptor *message_type = options.GetDescriptor(); | 207 const Descriptor *message_type = options.GetDescriptor(); |
203 CMessageClass* message_class( | 208 PyMessageFactory* message_factory = pool->py_message_factory; |
204 cdescriptor_pool::GetMessageClass(pool, message_type)); | 209 CMessageClass* message_class = message_factory::GetMessageClass( |
| 210 message_factory, message_type); |
205 if (message_class == NULL) { | 211 if (message_class == NULL) { |
206 // The Options message was not found in the current DescriptorPool. | 212 // The Options message was not found in the current DescriptorPool. |
207 // In this case, there cannot be extensions to these options, and we can | 213 // This means that the pool cannot contain any extensions to the Options |
208 // try to use the basic pool instead. | 214 // message either, so falling back to the basic pool we can only increase |
| 215 // the chances of successfully parsing the options. |
209 PyErr_Clear(); | 216 PyErr_Clear(); |
210 message_class = cdescriptor_pool::GetMessageClass( | 217 pool = GetDefaultDescriptorPool(); |
211 GetDefaultDescriptorPool(), message_type); | 218 message_factory = pool->py_message_factory; |
| 219 message_class = message_factory::GetMessageClass( |
| 220 message_factory, message_type); |
212 } | 221 } |
213 if (message_class == NULL) { | 222 if (message_class == NULL) { |
214 PyErr_Format(PyExc_TypeError, "Could not retrieve class for Options: %s", | 223 PyErr_Format(PyExc_TypeError, "Could not retrieve class for Options: %s", |
215 message_type->full_name().c_str()); | 224 message_type->full_name().c_str()); |
216 return NULL; | 225 return NULL; |
217 } | 226 } |
218 ScopedPyObjectPtr value( | 227 ScopedPyObjectPtr value( |
219 PyEval_CallObject(message_class->AsPyObject(), NULL)); | 228 PyEval_CallObject(message_class->AsPyObject(), NULL)); |
220 if (value == NULL) { | 229 if (value == NULL) { |
221 return NULL; | 230 return NULL; |
222 } | 231 } |
223 if (!PyObject_TypeCheck(value.get(), &CMessage_Type)) { | 232 if (!PyObject_TypeCheck(value.get(), &CMessage_Type)) { |
224 PyErr_Format(PyExc_TypeError, "Invalid class for %s: %s", | 233 PyErr_Format(PyExc_TypeError, "Invalid class for %s: %s", |
225 message_type->full_name().c_str(), | 234 message_type->full_name().c_str(), |
226 Py_TYPE(value.get())->tp_name); | 235 Py_TYPE(value.get())->tp_name); |
227 return NULL; | 236 return NULL; |
228 } | 237 } |
229 CMessage* cmsg = reinterpret_cast<CMessage*>(value.get()); | 238 CMessage* cmsg = reinterpret_cast<CMessage*>(value.get()); |
230 | 239 |
231 const Reflection* reflection = options.GetReflection(); | 240 const Reflection* reflection = options.GetReflection(); |
232 const UnknownFieldSet& unknown_fields(reflection->GetUnknownFields(options)); | 241 const UnknownFieldSet& unknown_fields(reflection->GetUnknownFields(options)); |
233 if (unknown_fields.empty()) { | 242 if (unknown_fields.empty()) { |
234 cmsg->message->CopyFrom(options); | 243 cmsg->message->CopyFrom(options); |
235 } else { | 244 } else { |
236 // Reparse options string! XXX call cmessage::MergeFromString | 245 // Reparse options string! XXX call cmessage::MergeFromString |
237 string serialized; | 246 string serialized; |
238 options.SerializeToString(&serialized); | 247 options.SerializeToString(&serialized); |
239 io::CodedInputStream input( | 248 io::CodedInputStream input( |
240 reinterpret_cast<const uint8*>(serialized.c_str()), serialized.size()); | 249 reinterpret_cast<const uint8*>(serialized.c_str()), serialized.size()); |
241 input.SetExtensionRegistry(pool->pool, pool->message_factory); | 250 input.SetExtensionRegistry(pool->pool, message_factory->message_factory); |
242 bool success = cmsg->message->MergePartialFromCodedStream(&input); | 251 bool success = cmsg->message->MergePartialFromCodedStream(&input); |
243 if (!success) { | 252 if (!success) { |
244 PyErr_Format(PyExc_ValueError, "Error parsing Options message"); | 253 PyErr_Format(PyExc_ValueError, "Error parsing Options message"); |
245 return NULL; | 254 return NULL; |
246 } | 255 } |
247 } | 256 } |
248 | 257 |
249 // Cache the result. | 258 // Cache the result. |
250 Py_INCREF(value.get()); | 259 Py_INCREF(value.get()); |
251 (*pool->descriptor_options)[descriptor] = value.get(); | 260 (*descriptor_options)[descriptor] = value.get(); |
252 | 261 |
253 return value.release(); | 262 return value.release(); |
254 } | 263 } |
255 | 264 |
256 // Copy the C++ descriptor to a Python message. | 265 // Copy the C++ descriptor to a Python message. |
257 // The Python message is an instance of descriptor_pb2.DescriptorProto | 266 // The Python message is an instance of descriptor_pb2.DescriptorProto |
258 // or similar. | 267 // or similar. |
259 template<class DescriptorProtoClass, class DescriptorClass> | 268 template<class DescriptorProtoClass, class DescriptorClass> |
260 static PyObject* CopyToPythonProto(const DescriptorClass *descriptor, | 269 static PyObject* CopyToPythonProto(const DescriptorClass *descriptor, |
261 PyObject *target) { | 270 PyObject *target) { |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 static PyObject* GetFile(PyBaseDescriptor *self, void *closure) { | 436 static PyObject* GetFile(PyBaseDescriptor *self, void *closure) { |
428 return PyFileDescriptor_FromDescriptor(_GetDescriptor(self)->file()); | 437 return PyFileDescriptor_FromDescriptor(_GetDescriptor(self)->file()); |
429 } | 438 } |
430 | 439 |
431 static PyObject* GetConcreteClass(PyBaseDescriptor* self, void *closure) { | 440 static PyObject* GetConcreteClass(PyBaseDescriptor* self, void *closure) { |
432 // Retuns the canonical class for the given descriptor. | 441 // Retuns the canonical class for the given descriptor. |
433 // This is the class that was registered with the primary descriptor pool | 442 // This is the class that was registered with the primary descriptor pool |
434 // which contains this descriptor. | 443 // which contains this descriptor. |
435 // This might not be the one you expect! For example the returned object does | 444 // This might not be the one you expect! For example the returned object does |
436 // not know about extensions defined in a custom pool. | 445 // not know about extensions defined in a custom pool. |
437 CMessageClass* concrete_class(cdescriptor_pool::GetMessageClass( | 446 CMessageClass* concrete_class(message_factory::GetMessageClass( |
438 GetDescriptorPool_FromPool(_GetDescriptor(self)->file()->pool()), | 447 GetDescriptorPool_FromPool( |
| 448 _GetDescriptor(self)->file()->pool())->py_message_factory, |
439 _GetDescriptor(self))); | 449 _GetDescriptor(self))); |
440 Py_XINCREF(concrete_class); | 450 Py_XINCREF(concrete_class); |
441 return concrete_class->AsPyObject(); | 451 return concrete_class->AsPyObject(); |
442 } | 452 } |
443 | 453 |
444 static PyObject* GetFieldsByName(PyBaseDescriptor* self, void *closure) { | 454 static PyObject* GetFieldsByName(PyBaseDescriptor* self, void *closure) { |
445 return NewMessageFieldsByName(_GetDescriptor(self)); | 455 return NewMessageFieldsByName(_GetDescriptor(self)); |
446 } | 456 } |
447 | 457 |
448 static PyObject* GetFieldsByCamelcaseName(PyBaseDescriptor* self, | 458 static PyObject* GetFieldsByCamelcaseName(PyBaseDescriptor* self, |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
687 } | 697 } |
688 | 698 |
689 static PyObject* GetName(PyBaseDescriptor *self, void *closure) { | 699 static PyObject* GetName(PyBaseDescriptor *self, void *closure) { |
690 return PyString_FromCppString(_GetDescriptor(self)->name()); | 700 return PyString_FromCppString(_GetDescriptor(self)->name()); |
691 } | 701 } |
692 | 702 |
693 static PyObject* GetCamelcaseName(PyBaseDescriptor* self, void *closure) { | 703 static PyObject* GetCamelcaseName(PyBaseDescriptor* self, void *closure) { |
694 return PyString_FromCppString(_GetDescriptor(self)->camelcase_name()); | 704 return PyString_FromCppString(_GetDescriptor(self)->camelcase_name()); |
695 } | 705 } |
696 | 706 |
| 707 static PyObject* GetJsonName(PyBaseDescriptor* self, void *closure) { |
| 708 return PyString_FromCppString(_GetDescriptor(self)->json_name()); |
| 709 } |
| 710 |
697 static PyObject* GetType(PyBaseDescriptor *self, void *closure) { | 711 static PyObject* GetType(PyBaseDescriptor *self, void *closure) { |
698 return PyInt_FromLong(_GetDescriptor(self)->type()); | 712 return PyInt_FromLong(_GetDescriptor(self)->type()); |
699 } | 713 } |
700 | 714 |
701 static PyObject* GetCppType(PyBaseDescriptor *self, void *closure) { | 715 static PyObject* GetCppType(PyBaseDescriptor *self, void *closure) { |
702 return PyInt_FromLong(_GetDescriptor(self)->cpp_type()); | 716 return PyInt_FromLong(_GetDescriptor(self)->cpp_type()); |
703 } | 717 } |
704 | 718 |
705 static PyObject* GetLabel(PyBaseDescriptor *self, void *closure) { | 719 static PyObject* GetLabel(PyBaseDescriptor *self, void *closure) { |
706 return PyInt_FromLong(_GetDescriptor(self)->label()); | 720 return PyInt_FromLong(_GetDescriptor(self)->label()); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
876 static int SetOptions(PyBaseDescriptor *self, PyObject *value, | 890 static int SetOptions(PyBaseDescriptor *self, PyObject *value, |
877 void *closure) { | 891 void *closure) { |
878 return CheckCalledFromGeneratedFile("_options"); | 892 return CheckCalledFromGeneratedFile("_options"); |
879 } | 893 } |
880 | 894 |
881 | 895 |
882 static PyGetSetDef Getters[] = { | 896 static PyGetSetDef Getters[] = { |
883 { "full_name", (getter)GetFullName, NULL, "Full name"}, | 897 { "full_name", (getter)GetFullName, NULL, "Full name"}, |
884 { "name", (getter)GetName, NULL, "Unqualified name"}, | 898 { "name", (getter)GetName, NULL, "Unqualified name"}, |
885 { "camelcase_name", (getter)GetCamelcaseName, NULL, "Camelcase name"}, | 899 { "camelcase_name", (getter)GetCamelcaseName, NULL, "Camelcase name"}, |
| 900 { "json_name", (getter)GetJsonName, NULL, "Json name"}, |
886 { "type", (getter)GetType, NULL, "C++ Type"}, | 901 { "type", (getter)GetType, NULL, "C++ Type"}, |
887 { "cpp_type", (getter)GetCppType, NULL, "C++ Type"}, | 902 { "cpp_type", (getter)GetCppType, NULL, "C++ Type"}, |
888 { "label", (getter)GetLabel, NULL, "Label"}, | 903 { "label", (getter)GetLabel, NULL, "Label"}, |
889 { "number", (getter)GetNumber, NULL, "Number"}, | 904 { "number", (getter)GetNumber, NULL, "Number"}, |
890 { "index", (getter)GetIndex, NULL, "Index"}, | 905 { "index", (getter)GetIndex, NULL, "Index"}, |
891 { "default_value", (getter)GetDefaultValue, NULL, "Default Value"}, | 906 { "default_value", (getter)GetDefaultValue, NULL, "Default Value"}, |
892 { "has_default_value", (getter)HasDefaultValue}, | 907 { "has_default_value", (getter)HasDefaultValue}, |
893 { "is_extension", (getter)IsExtension, NULL, "ID"}, | 908 { "is_extension", (getter)IsExtension, NULL, "ID"}, |
894 { "id", (getter)GetID, NULL, "ID"}, | 909 { "id", (getter)GetID, NULL, "ID"}, |
895 { "_cdescriptor", (getter)GetCDescriptor, NULL, "HAACK REMOVE ME"}, | 910 { "_cdescriptor", (getter)GetCDescriptor, NULL, "HAACK REMOVE ME"}, |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1084 0, // tp_setattro | 1099 0, // tp_setattro |
1085 0, // tp_as_buffer | 1100 0, // tp_as_buffer |
1086 Py_TPFLAGS_DEFAULT, // tp_flags | 1101 Py_TPFLAGS_DEFAULT, // tp_flags |
1087 "A Enum Descriptor", // tp_doc | 1102 "A Enum Descriptor", // tp_doc |
1088 0, // tp_traverse | 1103 0, // tp_traverse |
1089 0, // tp_clear | 1104 0, // tp_clear |
1090 0, // tp_richcompare | 1105 0, // tp_richcompare |
1091 0, // tp_weaklistoffset | 1106 0, // tp_weaklistoffset |
1092 0, // tp_iter | 1107 0, // tp_iter |
1093 0, // tp_iternext | 1108 0, // tp_iternext |
1094 enum_descriptor::Methods, // tp_getset | 1109 enum_descriptor::Methods, // tp_methods |
1095 0, // tp_members | 1110 0, // tp_members |
1096 enum_descriptor::Getters, // tp_getset | 1111 enum_descriptor::Getters, // tp_getset |
1097 &descriptor::PyBaseDescriptor_Type, // tp_base | 1112 &descriptor::PyBaseDescriptor_Type, // tp_base |
1098 }; | 1113 }; |
1099 | 1114 |
1100 PyObject* PyEnumDescriptor_FromDescriptor( | 1115 PyObject* PyEnumDescriptor_FromDescriptor( |
1101 const EnumDescriptor* enum_descriptor) { | 1116 const EnumDescriptor* enum_descriptor) { |
1102 return descriptor::NewInternedDescriptor( | 1117 return descriptor::NewInternedDescriptor( |
1103 &PyEnumDescriptor_Type, enum_descriptor, NULL); | 1118 &PyEnumDescriptor_Type, enum_descriptor, NULL); |
1104 } | 1119 } |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1268 } | 1283 } |
1269 | 1284 |
1270 static PyObject* GetEnumTypesByName(PyFileDescriptor* self, void *closure) { | 1285 static PyObject* GetEnumTypesByName(PyFileDescriptor* self, void *closure) { |
1271 return NewFileEnumTypesByName(_GetDescriptor(self)); | 1286 return NewFileEnumTypesByName(_GetDescriptor(self)); |
1272 } | 1287 } |
1273 | 1288 |
1274 static PyObject* GetExtensionsByName(PyFileDescriptor* self, void *closure) { | 1289 static PyObject* GetExtensionsByName(PyFileDescriptor* self, void *closure) { |
1275 return NewFileExtensionsByName(_GetDescriptor(self)); | 1290 return NewFileExtensionsByName(_GetDescriptor(self)); |
1276 } | 1291 } |
1277 | 1292 |
| 1293 static PyObject* GetServicesByName(PyFileDescriptor* self, void *closure) { |
| 1294 return NewFileServicesByName(_GetDescriptor(self)); |
| 1295 } |
| 1296 |
1278 static PyObject* GetDependencies(PyFileDescriptor* self, void *closure) { | 1297 static PyObject* GetDependencies(PyFileDescriptor* self, void *closure) { |
1279 return NewFileDependencies(_GetDescriptor(self)); | 1298 return NewFileDependencies(_GetDescriptor(self)); |
1280 } | 1299 } |
1281 | 1300 |
1282 static PyObject* GetPublicDependencies(PyFileDescriptor* self, void *closure) { | 1301 static PyObject* GetPublicDependencies(PyFileDescriptor* self, void *closure) { |
1283 return NewFilePublicDependencies(_GetDescriptor(self)); | 1302 return NewFilePublicDependencies(_GetDescriptor(self)); |
1284 } | 1303 } |
1285 | 1304 |
1286 static PyObject* GetHasOptions(PyFileDescriptor *self, void *closure) { | 1305 static PyObject* GetHasOptions(PyFileDescriptor *self, void *closure) { |
1287 const FileOptions& options(_GetDescriptor(self)->options()); | 1306 const FileOptions& options(_GetDescriptor(self)->options()); |
(...skipping 29 matching lines...) Expand all Loading... |
1317 static PyGetSetDef Getters[] = { | 1336 static PyGetSetDef Getters[] = { |
1318 { "pool", (getter)GetPool, NULL, "pool"}, | 1337 { "pool", (getter)GetPool, NULL, "pool"}, |
1319 { "name", (getter)GetName, NULL, "name"}, | 1338 { "name", (getter)GetName, NULL, "name"}, |
1320 { "package", (getter)GetPackage, NULL, "package"}, | 1339 { "package", (getter)GetPackage, NULL, "package"}, |
1321 { "serialized_pb", (getter)GetSerializedPb}, | 1340 { "serialized_pb", (getter)GetSerializedPb}, |
1322 { "message_types_by_name", (getter)GetMessageTypesByName, NULL, | 1341 { "message_types_by_name", (getter)GetMessageTypesByName, NULL, |
1323 "Messages by name"}, | 1342 "Messages by name"}, |
1324 { "enum_types_by_name", (getter)GetEnumTypesByName, NULL, "Enums by name"}, | 1343 { "enum_types_by_name", (getter)GetEnumTypesByName, NULL, "Enums by name"}, |
1325 { "extensions_by_name", (getter)GetExtensionsByName, NULL, | 1344 { "extensions_by_name", (getter)GetExtensionsByName, NULL, |
1326 "Extensions by name"}, | 1345 "Extensions by name"}, |
| 1346 { "services_by_name", (getter)GetServicesByName, NULL, "Services by name"}, |
1327 { "dependencies", (getter)GetDependencies, NULL, "Dependencies"}, | 1347 { "dependencies", (getter)GetDependencies, NULL, "Dependencies"}, |
1328 { "public_dependencies", (getter)GetPublicDependencies, NULL, "Dependencies"}, | 1348 { "public_dependencies", (getter)GetPublicDependencies, NULL, "Dependencies"}, |
1329 | 1349 |
1330 { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"}, | 1350 { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"}, |
1331 { "_options", (getter)NULL, (setter)SetOptions, "Options"}, | 1351 { "_options", (getter)NULL, (setter)SetOptions, "Options"}, |
1332 { "syntax", (getter)GetSyntax, (setter)NULL, "Syntax"}, | 1352 { "syntax", (getter)GetSyntax, (setter)NULL, "Syntax"}, |
1333 {NULL} | 1353 {NULL} |
1334 }; | 1354 }; |
1335 | 1355 |
1336 static PyMethodDef Methods[] = { | 1356 static PyMethodDef Methods[] = { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1445 static PyObject* GetContainingType(PyBaseDescriptor *self, void *closure) { | 1465 static PyObject* GetContainingType(PyBaseDescriptor *self, void *closure) { |
1446 const Descriptor* containing_type = | 1466 const Descriptor* containing_type = |
1447 _GetDescriptor(self)->containing_type(); | 1467 _GetDescriptor(self)->containing_type(); |
1448 if (containing_type) { | 1468 if (containing_type) { |
1449 return PyMessageDescriptor_FromDescriptor(containing_type); | 1469 return PyMessageDescriptor_FromDescriptor(containing_type); |
1450 } else { | 1470 } else { |
1451 Py_RETURN_NONE; | 1471 Py_RETURN_NONE; |
1452 } | 1472 } |
1453 } | 1473 } |
1454 | 1474 |
| 1475 static PyObject* GetHasOptions(PyBaseDescriptor *self, void *closure) { |
| 1476 const OneofOptions& options(_GetDescriptor(self)->options()); |
| 1477 if (&options != &OneofOptions::default_instance()) { |
| 1478 Py_RETURN_TRUE; |
| 1479 } else { |
| 1480 Py_RETURN_FALSE; |
| 1481 } |
| 1482 } |
| 1483 static int SetHasOptions(PyBaseDescriptor *self, PyObject *value, |
| 1484 void *closure) { |
| 1485 return CheckCalledFromGeneratedFile("has_options"); |
| 1486 } |
| 1487 |
| 1488 static PyObject* GetOptions(PyBaseDescriptor *self) { |
| 1489 return GetOrBuildOptions(_GetDescriptor(self)); |
| 1490 } |
| 1491 |
| 1492 static int SetOptions(PyBaseDescriptor *self, PyObject *value, |
| 1493 void *closure) { |
| 1494 return CheckCalledFromGeneratedFile("_options"); |
| 1495 } |
| 1496 |
1455 static PyGetSetDef Getters[] = { | 1497 static PyGetSetDef Getters[] = { |
1456 { "name", (getter)GetName, NULL, "Name"}, | 1498 { "name", (getter)GetName, NULL, "Name"}, |
1457 { "full_name", (getter)GetFullName, NULL, "Full name"}, | 1499 { "full_name", (getter)GetFullName, NULL, "Full name"}, |
1458 { "index", (getter)GetIndex, NULL, "Index"}, | 1500 { "index", (getter)GetIndex, NULL, "Index"}, |
1459 | 1501 |
1460 { "containing_type", (getter)GetContainingType, NULL, "Containing type"}, | 1502 { "containing_type", (getter)GetContainingType, NULL, "Containing type"}, |
| 1503 { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"}, |
| 1504 { "_options", (getter)NULL, (setter)SetOptions, "Options"}, |
1461 { "fields", (getter)GetFields, NULL, "Fields"}, | 1505 { "fields", (getter)GetFields, NULL, "Fields"}, |
1462 {NULL} | 1506 {NULL} |
1463 }; | 1507 }; |
1464 | 1508 |
| 1509 static PyMethodDef Methods[] = { |
| 1510 { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS }, |
| 1511 {NULL} |
| 1512 }; |
| 1513 |
1465 } // namespace oneof_descriptor | 1514 } // namespace oneof_descriptor |
1466 | 1515 |
1467 PyTypeObject PyOneofDescriptor_Type = { | 1516 PyTypeObject PyOneofDescriptor_Type = { |
1468 PyVarObject_HEAD_INIT(&PyType_Type, 0) | 1517 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1469 FULL_MODULE_NAME ".OneofDescriptor", // tp_name | 1518 FULL_MODULE_NAME ".OneofDescriptor", // tp_name |
1470 sizeof(PyBaseDescriptor), // tp_basicsize | 1519 sizeof(PyBaseDescriptor), // tp_basicsize |
1471 0, // tp_itemsize | 1520 0, // tp_itemsize |
1472 0, // tp_dealloc | 1521 0, // tp_dealloc |
1473 0, // tp_print | 1522 0, // tp_print |
1474 0, // tp_getattr | 1523 0, // tp_getattr |
(...skipping 10 matching lines...) Expand all Loading... |
1485 0, // tp_setattro | 1534 0, // tp_setattro |
1486 0, // tp_as_buffer | 1535 0, // tp_as_buffer |
1487 Py_TPFLAGS_DEFAULT, // tp_flags | 1536 Py_TPFLAGS_DEFAULT, // tp_flags |
1488 "A Oneof Descriptor", // tp_doc | 1537 "A Oneof Descriptor", // tp_doc |
1489 0, // tp_traverse | 1538 0, // tp_traverse |
1490 0, // tp_clear | 1539 0, // tp_clear |
1491 0, // tp_richcompare | 1540 0, // tp_richcompare |
1492 0, // tp_weaklistoffset | 1541 0, // tp_weaklistoffset |
1493 0, // tp_iter | 1542 0, // tp_iter |
1494 0, // tp_iternext | 1543 0, // tp_iternext |
1495 0, // tp_methods | 1544 oneof_descriptor::Methods, // tp_methods |
1496 0, // tp_members | 1545 0, // tp_members |
1497 oneof_descriptor::Getters, // tp_getset | 1546 oneof_descriptor::Getters, // tp_getset |
1498 &descriptor::PyBaseDescriptor_Type, // tp_base | 1547 &descriptor::PyBaseDescriptor_Type, // tp_base |
1499 }; | 1548 }; |
1500 | 1549 |
1501 PyObject* PyOneofDescriptor_FromDescriptor( | 1550 PyObject* PyOneofDescriptor_FromDescriptor( |
1502 const OneofDescriptor* oneof_descriptor) { | 1551 const OneofDescriptor* oneof_descriptor) { |
1503 return descriptor::NewInternedDescriptor( | 1552 return descriptor::NewInternedDescriptor( |
1504 &PyOneofDescriptor_Type, oneof_descriptor, NULL); | 1553 &PyOneofDescriptor_Type, oneof_descriptor, NULL); |
1505 } | 1554 } |
1506 | 1555 |
| 1556 namespace service_descriptor { |
| 1557 |
| 1558 // Unchecked accessor to the C++ pointer. |
| 1559 static const ServiceDescriptor* _GetDescriptor( |
| 1560 PyBaseDescriptor *self) { |
| 1561 return reinterpret_cast<const ServiceDescriptor*>(self->descriptor); |
| 1562 } |
| 1563 |
| 1564 static PyObject* GetName(PyBaseDescriptor* self, void *closure) { |
| 1565 return PyString_FromCppString(_GetDescriptor(self)->name()); |
| 1566 } |
| 1567 |
| 1568 static PyObject* GetFullName(PyBaseDescriptor* self, void *closure) { |
| 1569 return PyString_FromCppString(_GetDescriptor(self)->full_name()); |
| 1570 } |
| 1571 |
| 1572 static PyObject* GetIndex(PyBaseDescriptor *self, void *closure) { |
| 1573 return PyInt_FromLong(_GetDescriptor(self)->index()); |
| 1574 } |
| 1575 |
| 1576 static PyObject* GetMethods(PyBaseDescriptor* self, void *closure) { |
| 1577 return NewServiceMethodsSeq(_GetDescriptor(self)); |
| 1578 } |
| 1579 |
| 1580 static PyObject* GetMethodsByName(PyBaseDescriptor* self, void *closure) { |
| 1581 return NewServiceMethodsByName(_GetDescriptor(self)); |
| 1582 } |
| 1583 |
| 1584 static PyObject* FindMethodByName(PyBaseDescriptor *self, PyObject* arg) { |
| 1585 Py_ssize_t name_size; |
| 1586 char* name; |
| 1587 if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) { |
| 1588 return NULL; |
| 1589 } |
| 1590 |
| 1591 const MethodDescriptor* method_descriptor = |
| 1592 _GetDescriptor(self)->FindMethodByName(string(name, name_size)); |
| 1593 if (method_descriptor == NULL) { |
| 1594 PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name); |
| 1595 return NULL; |
| 1596 } |
| 1597 |
| 1598 return PyMethodDescriptor_FromDescriptor(method_descriptor); |
| 1599 } |
| 1600 |
| 1601 static PyObject* GetOptions(PyBaseDescriptor *self) { |
| 1602 return GetOrBuildOptions(_GetDescriptor(self)); |
| 1603 } |
| 1604 |
| 1605 static PyObject* CopyToProto(PyBaseDescriptor *self, PyObject *target) { |
| 1606 return CopyToPythonProto<ServiceDescriptorProto>(_GetDescriptor(self), |
| 1607 target); |
| 1608 } |
| 1609 |
| 1610 static PyGetSetDef Getters[] = { |
| 1611 { "name", (getter)GetName, NULL, "Name", NULL}, |
| 1612 { "full_name", (getter)GetFullName, NULL, "Full name", NULL}, |
| 1613 { "index", (getter)GetIndex, NULL, "Index", NULL}, |
| 1614 |
| 1615 { "methods", (getter)GetMethods, NULL, "Methods", NULL}, |
| 1616 { "methods_by_name", (getter)GetMethodsByName, NULL, "Methods by name", NULL}, |
| 1617 {NULL} |
| 1618 }; |
| 1619 |
| 1620 static PyMethodDef Methods[] = { |
| 1621 { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS }, |
| 1622 { "CopyToProto", (PyCFunction)CopyToProto, METH_O, }, |
| 1623 { "FindMethodByName", (PyCFunction)FindMethodByName, METH_O }, |
| 1624 {NULL} |
| 1625 }; |
| 1626 |
| 1627 } // namespace service_descriptor |
| 1628 |
| 1629 PyTypeObject PyServiceDescriptor_Type = { |
| 1630 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1631 FULL_MODULE_NAME ".ServiceDescriptor", // tp_name |
| 1632 sizeof(PyBaseDescriptor), // tp_basicsize |
| 1633 0, // tp_itemsize |
| 1634 0, // tp_dealloc |
| 1635 0, // tp_print |
| 1636 0, // tp_getattr |
| 1637 0, // tp_setattr |
| 1638 0, // tp_compare |
| 1639 0, // tp_repr |
| 1640 0, // tp_as_number |
| 1641 0, // tp_as_sequence |
| 1642 0, // tp_as_mapping |
| 1643 0, // tp_hash |
| 1644 0, // tp_call |
| 1645 0, // tp_str |
| 1646 0, // tp_getattro |
| 1647 0, // tp_setattro |
| 1648 0, // tp_as_buffer |
| 1649 Py_TPFLAGS_DEFAULT, // tp_flags |
| 1650 "A Service Descriptor", // tp_doc |
| 1651 0, // tp_traverse |
| 1652 0, // tp_clear |
| 1653 0, // tp_richcompare |
| 1654 0, // tp_weaklistoffset |
| 1655 0, // tp_iter |
| 1656 0, // tp_iternext |
| 1657 service_descriptor::Methods, // tp_methods |
| 1658 0, // tp_members |
| 1659 service_descriptor::Getters, // tp_getset |
| 1660 &descriptor::PyBaseDescriptor_Type, // tp_base |
| 1661 }; |
| 1662 |
| 1663 PyObject* PyServiceDescriptor_FromDescriptor( |
| 1664 const ServiceDescriptor* service_descriptor) { |
| 1665 return descriptor::NewInternedDescriptor( |
| 1666 &PyServiceDescriptor_Type, service_descriptor, NULL); |
| 1667 } |
| 1668 |
| 1669 namespace method_descriptor { |
| 1670 |
| 1671 // Unchecked accessor to the C++ pointer. |
| 1672 static const MethodDescriptor* _GetDescriptor( |
| 1673 PyBaseDescriptor *self) { |
| 1674 return reinterpret_cast<const MethodDescriptor*>(self->descriptor); |
| 1675 } |
| 1676 |
| 1677 static PyObject* GetName(PyBaseDescriptor* self, void *closure) { |
| 1678 return PyString_FromCppString(_GetDescriptor(self)->name()); |
| 1679 } |
| 1680 |
| 1681 static PyObject* GetFullName(PyBaseDescriptor* self, void *closure) { |
| 1682 return PyString_FromCppString(_GetDescriptor(self)->full_name()); |
| 1683 } |
| 1684 |
| 1685 static PyObject* GetIndex(PyBaseDescriptor *self, void *closure) { |
| 1686 return PyInt_FromLong(_GetDescriptor(self)->index()); |
| 1687 } |
| 1688 |
| 1689 static PyObject* GetContainingService(PyBaseDescriptor *self, void *closure) { |
| 1690 const ServiceDescriptor* containing_service = |
| 1691 _GetDescriptor(self)->service(); |
| 1692 return PyServiceDescriptor_FromDescriptor(containing_service); |
| 1693 } |
| 1694 |
| 1695 static PyObject* GetInputType(PyBaseDescriptor *self, void *closure) { |
| 1696 const Descriptor* input_type = _GetDescriptor(self)->input_type(); |
| 1697 return PyMessageDescriptor_FromDescriptor(input_type); |
| 1698 } |
| 1699 |
| 1700 static PyObject* GetOutputType(PyBaseDescriptor *self, void *closure) { |
| 1701 const Descriptor* output_type = _GetDescriptor(self)->output_type(); |
| 1702 return PyMessageDescriptor_FromDescriptor(output_type); |
| 1703 } |
| 1704 |
| 1705 static PyObject* GetOptions(PyBaseDescriptor *self) { |
| 1706 return GetOrBuildOptions(_GetDescriptor(self)); |
| 1707 } |
| 1708 |
| 1709 static PyObject* CopyToProto(PyBaseDescriptor *self, PyObject *target) { |
| 1710 return CopyToPythonProto<MethodDescriptorProto>(_GetDescriptor(self), target); |
| 1711 } |
| 1712 |
| 1713 static PyGetSetDef Getters[] = { |
| 1714 { "name", (getter)GetName, NULL, "Name", NULL}, |
| 1715 { "full_name", (getter)GetFullName, NULL, "Full name", NULL}, |
| 1716 { "index", (getter)GetIndex, NULL, "Index", NULL}, |
| 1717 { "containing_service", (getter)GetContainingService, NULL, |
| 1718 "Containing service", NULL}, |
| 1719 { "input_type", (getter)GetInputType, NULL, "Input type", NULL}, |
| 1720 { "output_type", (getter)GetOutputType, NULL, "Output type", NULL}, |
| 1721 {NULL} |
| 1722 }; |
| 1723 |
| 1724 static PyMethodDef Methods[] = { |
| 1725 { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS, }, |
| 1726 { "CopyToProto", (PyCFunction)CopyToProto, METH_O, }, |
| 1727 {NULL} |
| 1728 }; |
| 1729 |
| 1730 } // namespace method_descriptor |
| 1731 |
| 1732 PyTypeObject PyMethodDescriptor_Type = { |
| 1733 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1734 FULL_MODULE_NAME ".MethodDescriptor", // tp_name |
| 1735 sizeof(PyBaseDescriptor), // tp_basicsize |
| 1736 0, // tp_itemsize |
| 1737 0, // tp_dealloc |
| 1738 0, // tp_print |
| 1739 0, // tp_getattr |
| 1740 0, // tp_setattr |
| 1741 0, // tp_compare |
| 1742 0, // tp_repr |
| 1743 0, // tp_as_number |
| 1744 0, // tp_as_sequence |
| 1745 0, // tp_as_mapping |
| 1746 0, // tp_hash |
| 1747 0, // tp_call |
| 1748 0, // tp_str |
| 1749 0, // tp_getattro |
| 1750 0, // tp_setattro |
| 1751 0, // tp_as_buffer |
| 1752 Py_TPFLAGS_DEFAULT, // tp_flags |
| 1753 "A Method Descriptor", // tp_doc |
| 1754 0, // tp_traverse |
| 1755 0, // tp_clear |
| 1756 0, // tp_richcompare |
| 1757 0, // tp_weaklistoffset |
| 1758 0, // tp_iter |
| 1759 0, // tp_iternext |
| 1760 method_descriptor::Methods, // tp_methods |
| 1761 0, // tp_members |
| 1762 method_descriptor::Getters, // tp_getset |
| 1763 &descriptor::PyBaseDescriptor_Type, // tp_base |
| 1764 }; |
| 1765 |
| 1766 PyObject* PyMethodDescriptor_FromDescriptor( |
| 1767 const MethodDescriptor* method_descriptor) { |
| 1768 return descriptor::NewInternedDescriptor( |
| 1769 &PyMethodDescriptor_Type, method_descriptor, NULL); |
| 1770 } |
| 1771 |
1507 // Add a enum values to a type dictionary. | 1772 // Add a enum values to a type dictionary. |
1508 static bool AddEnumValues(PyTypeObject *type, | 1773 static bool AddEnumValues(PyTypeObject *type, |
1509 const EnumDescriptor* enum_descriptor) { | 1774 const EnumDescriptor* enum_descriptor) { |
1510 for (int i = 0; i < enum_descriptor->value_count(); ++i) { | 1775 for (int i = 0; i < enum_descriptor->value_count(); ++i) { |
1511 const EnumValueDescriptor* value = enum_descriptor->value(i); | 1776 const EnumValueDescriptor* value = enum_descriptor->value(i); |
1512 ScopedPyObjectPtr obj(PyInt_FromLong(value->number())); | 1777 ScopedPyObjectPtr obj(PyInt_FromLong(value->number())); |
1513 if (obj == NULL) { | 1778 if (obj == NULL) { |
1514 return false; | 1779 return false; |
1515 } | 1780 } |
1516 if (PyDict_SetItemString(type->tp_dict, value->name().c_str(), obj.get()) < | 1781 if (PyDict_SetItemString(type->tp_dict, value->name().c_str(), obj.get()) < |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1566 | 1831 |
1567 if (PyType_Ready(&PyEnumValueDescriptor_Type) < 0) | 1832 if (PyType_Ready(&PyEnumValueDescriptor_Type) < 0) |
1568 return false; | 1833 return false; |
1569 | 1834 |
1570 if (PyType_Ready(&PyFileDescriptor_Type) < 0) | 1835 if (PyType_Ready(&PyFileDescriptor_Type) < 0) |
1571 return false; | 1836 return false; |
1572 | 1837 |
1573 if (PyType_Ready(&PyOneofDescriptor_Type) < 0) | 1838 if (PyType_Ready(&PyOneofDescriptor_Type) < 0) |
1574 return false; | 1839 return false; |
1575 | 1840 |
| 1841 if (PyType_Ready(&PyServiceDescriptor_Type) < 0) |
| 1842 return false; |
| 1843 |
| 1844 if (PyType_Ready(&PyMethodDescriptor_Type) < 0) |
| 1845 return false; |
| 1846 |
1576 if (!InitDescriptorMappingTypes()) | 1847 if (!InitDescriptorMappingTypes()) |
1577 return false; | 1848 return false; |
1578 | 1849 |
1579 return true; | 1850 return true; |
1580 } | 1851 } |
1581 | 1852 |
1582 } // namespace python | 1853 } // namespace python |
1583 } // namespace protobuf | 1854 } // namespace protobuf |
1584 } // namespace google | 1855 } // namespace google |
OLD | NEW |