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> | |
45 #include <google/protobuf/pyext/scoped_pyobject_ptr.h> | 44 #include <google/protobuf/pyext/scoped_pyobject_ptr.h> |
46 | 45 |
47 #if PY_MAJOR_VERSION >= 3 | 46 #if PY_MAJOR_VERSION >= 3 |
48 #define PyString_FromStringAndSize PyUnicode_FromStringAndSize | 47 #define PyString_FromStringAndSize PyUnicode_FromStringAndSize |
49 #define PyString_Check PyUnicode_Check | 48 #define PyString_Check PyUnicode_Check |
50 #define PyString_InternFromString PyUnicode_InternFromString | 49 #define PyString_InternFromString PyUnicode_InternFromString |
51 #define PyInt_FromLong PyLong_FromLong | 50 #define PyInt_FromLong PyLong_FromLong |
52 #define PyInt_FromSize_t PyLong_FromSize_t | 51 #define PyInt_FromSize_t PyLong_FromSize_t |
53 #if PY_VERSION_HEX < 0x03030000 | 52 #if PY_VERSION_HEX < 0x03030000 |
54 #error "Python 3.0 - 3.2 are not supported." | 53 #error "Python 3.0 - 3.2 are not supported." |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 return descriptor; | 165 return descriptor; |
167 } | 166 } |
168 template<> | 167 template<> |
169 const FileDescriptor* GetFileDescriptor(const EnumValueDescriptor* descriptor) { | 168 const FileDescriptor* GetFileDescriptor(const EnumValueDescriptor* descriptor) { |
170 return descriptor->type()->file(); | 169 return descriptor->type()->file(); |
171 } | 170 } |
172 template<> | 171 template<> |
173 const FileDescriptor* GetFileDescriptor(const OneofDescriptor* descriptor) { | 172 const FileDescriptor* GetFileDescriptor(const OneofDescriptor* descriptor) { |
174 return descriptor->containing_type()->file(); | 173 return descriptor->containing_type()->file(); |
175 } | 174 } |
176 template<> | |
177 const FileDescriptor* GetFileDescriptor(const MethodDescriptor* descriptor) { | |
178 return descriptor->service()->file(); | |
179 } | |
180 | 175 |
181 // Converts options into a Python protobuf, and cache the result. | 176 // Converts options into a Python protobuf, and cache the result. |
182 // | 177 // |
183 // This is a bit tricky because options can contain extension fields defined in | 178 // This is a bit tricky because options can contain extension fields defined in |
184 // the same proto file. In this case the options parsed from the serialized_pb | 179 // the same proto file. In this case the options parsed from the serialized_pb |
185 // have unknown fields, and we need to parse them again. | 180 // have unkown fields, and we need to parse them again. |
186 // | 181 // |
187 // Always returns a new reference. | 182 // Always returns a new reference. |
188 template<class DescriptorClass> | 183 template<class DescriptorClass> |
189 static PyObject* GetOrBuildOptions(const DescriptorClass *descriptor) { | 184 static PyObject* GetOrBuildOptions(const DescriptorClass *descriptor) { |
190 // Options (and their extensions) are completely resolved in the proto file | 185 // Options (and their extensions) are completely resolved in the proto file |
191 // containing the descriptor. | 186 // containing the descriptor. |
192 PyDescriptorPool* pool = GetDescriptorPool_FromPool( | 187 PyDescriptorPool* pool = GetDescriptorPool_FromPool( |
193 GetFileDescriptor(descriptor)->pool()); | 188 GetFileDescriptor(descriptor)->pool()); |
194 | 189 |
195 hash_map<const void*, PyObject*>* descriptor_options = | 190 hash_map<const void*, PyObject*>* descriptor_options = |
196 pool->descriptor_options; | 191 pool->descriptor_options; |
197 // First search in the cache. | 192 // First search in the cache. |
198 if (descriptor_options->find(descriptor) != descriptor_options->end()) { | 193 if (descriptor_options->find(descriptor) != descriptor_options->end()) { |
199 PyObject *value = (*descriptor_options)[descriptor]; | 194 PyObject *value = (*descriptor_options)[descriptor]; |
200 Py_INCREF(value); | 195 Py_INCREF(value); |
201 return value; | 196 return value; |
202 } | 197 } |
203 | 198 |
204 // Build the Options object: get its Python class, and make a copy of the C++ | 199 // Build the Options object: get its Python class, and make a copy of the C++ |
205 // read-only instance. | 200 // read-only instance. |
206 const Message& options(descriptor->options()); | 201 const Message& options(descriptor->options()); |
207 const Descriptor *message_type = options.GetDescriptor(); | 202 const Descriptor *message_type = options.GetDescriptor(); |
208 PyMessageFactory* message_factory = pool->py_message_factory; | 203 CMessageClass* message_class( |
209 CMessageClass* message_class = message_factory::GetMessageClass( | 204 cdescriptor_pool::GetMessageClass(pool, message_type)); |
210 message_factory, message_type); | |
211 if (message_class == NULL) { | 205 if (message_class == NULL) { |
212 // The Options message was not found in the current DescriptorPool. | 206 // The Options message was not found in the current DescriptorPool. |
213 // This means that the pool cannot contain any extensions to the Options | 207 // In this case, there cannot be extensions to these options, and we can |
214 // message either, so falling back to the basic pool we can only increase | 208 // try to use the basic pool instead. |
215 // the chances of successfully parsing the options. | |
216 PyErr_Clear(); | 209 PyErr_Clear(); |
217 pool = GetDefaultDescriptorPool(); | 210 message_class = cdescriptor_pool::GetMessageClass( |
218 message_factory = pool->py_message_factory; | 211 GetDefaultDescriptorPool(), message_type); |
219 message_class = message_factory::GetMessageClass( | |
220 message_factory, message_type); | |
221 } | 212 } |
222 if (message_class == NULL) { | 213 if (message_class == NULL) { |
223 PyErr_Format(PyExc_TypeError, "Could not retrieve class for Options: %s", | 214 PyErr_Format(PyExc_TypeError, "Could not retrieve class for Options: %s", |
224 message_type->full_name().c_str()); | 215 message_type->full_name().c_str()); |
225 return NULL; | 216 return NULL; |
226 } | 217 } |
227 ScopedPyObjectPtr value( | 218 ScopedPyObjectPtr value( |
228 PyEval_CallObject(message_class->AsPyObject(), NULL)); | 219 PyEval_CallObject(message_class->AsPyObject(), NULL)); |
229 if (value == NULL) { | 220 if (value == NULL) { |
230 return NULL; | 221 return NULL; |
231 } | 222 } |
232 if (!PyObject_TypeCheck(value.get(), &CMessage_Type)) { | 223 if (!PyObject_TypeCheck(value.get(), &CMessage_Type)) { |
233 PyErr_Format(PyExc_TypeError, "Invalid class for %s: %s", | 224 PyErr_Format(PyExc_TypeError, "Invalid class for %s: %s", |
234 message_type->full_name().c_str(), | 225 message_type->full_name().c_str(), |
235 Py_TYPE(value.get())->tp_name); | 226 Py_TYPE(value.get())->tp_name); |
236 return NULL; | 227 return NULL; |
237 } | 228 } |
238 CMessage* cmsg = reinterpret_cast<CMessage*>(value.get()); | 229 CMessage* cmsg = reinterpret_cast<CMessage*>(value.get()); |
239 | 230 |
240 const Reflection* reflection = options.GetReflection(); | 231 const Reflection* reflection = options.GetReflection(); |
241 const UnknownFieldSet& unknown_fields(reflection->GetUnknownFields(options)); | 232 const UnknownFieldSet& unknown_fields(reflection->GetUnknownFields(options)); |
242 if (unknown_fields.empty()) { | 233 if (unknown_fields.empty()) { |
243 cmsg->message->CopyFrom(options); | 234 cmsg->message->CopyFrom(options); |
244 } else { | 235 } else { |
245 // Reparse options string! XXX call cmessage::MergeFromString | 236 // Reparse options string! XXX call cmessage::MergeFromString |
246 string serialized; | 237 string serialized; |
247 options.SerializeToString(&serialized); | 238 options.SerializeToString(&serialized); |
248 io::CodedInputStream input( | 239 io::CodedInputStream input( |
249 reinterpret_cast<const uint8*>(serialized.c_str()), serialized.size()); | 240 reinterpret_cast<const uint8*>(serialized.c_str()), serialized.size()); |
250 input.SetExtensionRegistry(pool->pool, message_factory->message_factory); | 241 input.SetExtensionRegistry(pool->pool, pool->message_factory); |
251 bool success = cmsg->message->MergePartialFromCodedStream(&input); | 242 bool success = cmsg->message->MergePartialFromCodedStream(&input); |
252 if (!success) { | 243 if (!success) { |
253 PyErr_Format(PyExc_ValueError, "Error parsing Options message"); | 244 PyErr_Format(PyExc_ValueError, "Error parsing Options message"); |
254 return NULL; | 245 return NULL; |
255 } | 246 } |
256 } | 247 } |
257 | 248 |
258 // Cache the result. | 249 // Cache the result. |
259 Py_INCREF(value.get()); | 250 Py_INCREF(value.get()); |
260 (*descriptor_options)[descriptor] = value.get(); | 251 (*pool->descriptor_options)[descriptor] = value.get(); |
261 | 252 |
262 return value.release(); | 253 return value.release(); |
263 } | 254 } |
264 | 255 |
265 // Copy the C++ descriptor to a Python message. | 256 // Copy the C++ descriptor to a Python message. |
266 // The Python message is an instance of descriptor_pb2.DescriptorProto | 257 // The Python message is an instance of descriptor_pb2.DescriptorProto |
267 // or similar. | 258 // or similar. |
268 template<class DescriptorProtoClass, class DescriptorClass> | 259 template<class DescriptorProtoClass, class DescriptorClass> |
269 static PyObject* CopyToPythonProto(const DescriptorClass *descriptor, | 260 static PyObject* CopyToPythonProto(const DescriptorClass *descriptor, |
270 PyObject *target) { | 261 PyObject *target) { |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 static PyObject* GetFile(PyBaseDescriptor *self, void *closure) { | 427 static PyObject* GetFile(PyBaseDescriptor *self, void *closure) { |
437 return PyFileDescriptor_FromDescriptor(_GetDescriptor(self)->file()); | 428 return PyFileDescriptor_FromDescriptor(_GetDescriptor(self)->file()); |
438 } | 429 } |
439 | 430 |
440 static PyObject* GetConcreteClass(PyBaseDescriptor* self, void *closure) { | 431 static PyObject* GetConcreteClass(PyBaseDescriptor* self, void *closure) { |
441 // Retuns the canonical class for the given descriptor. | 432 // Retuns the canonical class for the given descriptor. |
442 // This is the class that was registered with the primary descriptor pool | 433 // This is the class that was registered with the primary descriptor pool |
443 // which contains this descriptor. | 434 // which contains this descriptor. |
444 // This might not be the one you expect! For example the returned object does | 435 // This might not be the one you expect! For example the returned object does |
445 // not know about extensions defined in a custom pool. | 436 // not know about extensions defined in a custom pool. |
446 CMessageClass* concrete_class(message_factory::GetMessageClass( | 437 CMessageClass* concrete_class(cdescriptor_pool::GetMessageClass( |
447 GetDescriptorPool_FromPool( | 438 GetDescriptorPool_FromPool(_GetDescriptor(self)->file()->pool()), |
448 _GetDescriptor(self)->file()->pool())->py_message_factory, | |
449 _GetDescriptor(self))); | 439 _GetDescriptor(self))); |
450 Py_XINCREF(concrete_class); | 440 Py_XINCREF(concrete_class); |
451 return concrete_class->AsPyObject(); | 441 return concrete_class->AsPyObject(); |
452 } | 442 } |
453 | 443 |
454 static PyObject* GetFieldsByName(PyBaseDescriptor* self, void *closure) { | 444 static PyObject* GetFieldsByName(PyBaseDescriptor* self, void *closure) { |
455 return NewMessageFieldsByName(_GetDescriptor(self)); | 445 return NewMessageFieldsByName(_GetDescriptor(self)); |
456 } | 446 } |
457 | 447 |
458 static PyObject* GetFieldsByCamelcaseName(PyBaseDescriptor* self, | 448 static PyObject* GetFieldsByCamelcaseName(PyBaseDescriptor* self, |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 } | 687 } |
698 | 688 |
699 static PyObject* GetName(PyBaseDescriptor *self, void *closure) { | 689 static PyObject* GetName(PyBaseDescriptor *self, void *closure) { |
700 return PyString_FromCppString(_GetDescriptor(self)->name()); | 690 return PyString_FromCppString(_GetDescriptor(self)->name()); |
701 } | 691 } |
702 | 692 |
703 static PyObject* GetCamelcaseName(PyBaseDescriptor* self, void *closure) { | 693 static PyObject* GetCamelcaseName(PyBaseDescriptor* self, void *closure) { |
704 return PyString_FromCppString(_GetDescriptor(self)->camelcase_name()); | 694 return PyString_FromCppString(_GetDescriptor(self)->camelcase_name()); |
705 } | 695 } |
706 | 696 |
707 static PyObject* GetJsonName(PyBaseDescriptor* self, void *closure) { | |
708 return PyString_FromCppString(_GetDescriptor(self)->json_name()); | |
709 } | |
710 | |
711 static PyObject* GetType(PyBaseDescriptor *self, void *closure) { | 697 static PyObject* GetType(PyBaseDescriptor *self, void *closure) { |
712 return PyInt_FromLong(_GetDescriptor(self)->type()); | 698 return PyInt_FromLong(_GetDescriptor(self)->type()); |
713 } | 699 } |
714 | 700 |
715 static PyObject* GetCppType(PyBaseDescriptor *self, void *closure) { | 701 static PyObject* GetCppType(PyBaseDescriptor *self, void *closure) { |
716 return PyInt_FromLong(_GetDescriptor(self)->cpp_type()); | 702 return PyInt_FromLong(_GetDescriptor(self)->cpp_type()); |
717 } | 703 } |
718 | 704 |
719 static PyObject* GetLabel(PyBaseDescriptor *self, void *closure) { | 705 static PyObject* GetLabel(PyBaseDescriptor *self, void *closure) { |
720 return PyInt_FromLong(_GetDescriptor(self)->label()); | 706 return PyInt_FromLong(_GetDescriptor(self)->label()); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
890 static int SetOptions(PyBaseDescriptor *self, PyObject *value, | 876 static int SetOptions(PyBaseDescriptor *self, PyObject *value, |
891 void *closure) { | 877 void *closure) { |
892 return CheckCalledFromGeneratedFile("_options"); | 878 return CheckCalledFromGeneratedFile("_options"); |
893 } | 879 } |
894 | 880 |
895 | 881 |
896 static PyGetSetDef Getters[] = { | 882 static PyGetSetDef Getters[] = { |
897 { "full_name", (getter)GetFullName, NULL, "Full name"}, | 883 { "full_name", (getter)GetFullName, NULL, "Full name"}, |
898 { "name", (getter)GetName, NULL, "Unqualified name"}, | 884 { "name", (getter)GetName, NULL, "Unqualified name"}, |
899 { "camelcase_name", (getter)GetCamelcaseName, NULL, "Camelcase name"}, | 885 { "camelcase_name", (getter)GetCamelcaseName, NULL, "Camelcase name"}, |
900 { "json_name", (getter)GetJsonName, NULL, "Json name"}, | |
901 { "type", (getter)GetType, NULL, "C++ Type"}, | 886 { "type", (getter)GetType, NULL, "C++ Type"}, |
902 { "cpp_type", (getter)GetCppType, NULL, "C++ Type"}, | 887 { "cpp_type", (getter)GetCppType, NULL, "C++ Type"}, |
903 { "label", (getter)GetLabel, NULL, "Label"}, | 888 { "label", (getter)GetLabel, NULL, "Label"}, |
904 { "number", (getter)GetNumber, NULL, "Number"}, | 889 { "number", (getter)GetNumber, NULL, "Number"}, |
905 { "index", (getter)GetIndex, NULL, "Index"}, | 890 { "index", (getter)GetIndex, NULL, "Index"}, |
906 { "default_value", (getter)GetDefaultValue, NULL, "Default Value"}, | 891 { "default_value", (getter)GetDefaultValue, NULL, "Default Value"}, |
907 { "has_default_value", (getter)HasDefaultValue}, | 892 { "has_default_value", (getter)HasDefaultValue}, |
908 { "is_extension", (getter)IsExtension, NULL, "ID"}, | 893 { "is_extension", (getter)IsExtension, NULL, "ID"}, |
909 { "id", (getter)GetID, NULL, "ID"}, | 894 { "id", (getter)GetID, NULL, "ID"}, |
910 { "_cdescriptor", (getter)GetCDescriptor, NULL, "HAACK REMOVE ME"}, | 895 { "_cdescriptor", (getter)GetCDescriptor, NULL, "HAACK REMOVE ME"}, |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1099 0, // tp_setattro | 1084 0, // tp_setattro |
1100 0, // tp_as_buffer | 1085 0, // tp_as_buffer |
1101 Py_TPFLAGS_DEFAULT, // tp_flags | 1086 Py_TPFLAGS_DEFAULT, // tp_flags |
1102 "A Enum Descriptor", // tp_doc | 1087 "A Enum Descriptor", // tp_doc |
1103 0, // tp_traverse | 1088 0, // tp_traverse |
1104 0, // tp_clear | 1089 0, // tp_clear |
1105 0, // tp_richcompare | 1090 0, // tp_richcompare |
1106 0, // tp_weaklistoffset | 1091 0, // tp_weaklistoffset |
1107 0, // tp_iter | 1092 0, // tp_iter |
1108 0, // tp_iternext | 1093 0, // tp_iternext |
1109 enum_descriptor::Methods, // tp_methods | 1094 enum_descriptor::Methods, // tp_getset |
1110 0, // tp_members | 1095 0, // tp_members |
1111 enum_descriptor::Getters, // tp_getset | 1096 enum_descriptor::Getters, // tp_getset |
1112 &descriptor::PyBaseDescriptor_Type, // tp_base | 1097 &descriptor::PyBaseDescriptor_Type, // tp_base |
1113 }; | 1098 }; |
1114 | 1099 |
1115 PyObject* PyEnumDescriptor_FromDescriptor( | 1100 PyObject* PyEnumDescriptor_FromDescriptor( |
1116 const EnumDescriptor* enum_descriptor) { | 1101 const EnumDescriptor* enum_descriptor) { |
1117 return descriptor::NewInternedDescriptor( | 1102 return descriptor::NewInternedDescriptor( |
1118 &PyEnumDescriptor_Type, enum_descriptor, NULL); | 1103 &PyEnumDescriptor_Type, enum_descriptor, NULL); |
1119 } | 1104 } |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1283 } | 1268 } |
1284 | 1269 |
1285 static PyObject* GetEnumTypesByName(PyFileDescriptor* self, void *closure) { | 1270 static PyObject* GetEnumTypesByName(PyFileDescriptor* self, void *closure) { |
1286 return NewFileEnumTypesByName(_GetDescriptor(self)); | 1271 return NewFileEnumTypesByName(_GetDescriptor(self)); |
1287 } | 1272 } |
1288 | 1273 |
1289 static PyObject* GetExtensionsByName(PyFileDescriptor* self, void *closure) { | 1274 static PyObject* GetExtensionsByName(PyFileDescriptor* self, void *closure) { |
1290 return NewFileExtensionsByName(_GetDescriptor(self)); | 1275 return NewFileExtensionsByName(_GetDescriptor(self)); |
1291 } | 1276 } |
1292 | 1277 |
1293 static PyObject* GetServicesByName(PyFileDescriptor* self, void *closure) { | |
1294 return NewFileServicesByName(_GetDescriptor(self)); | |
1295 } | |
1296 | |
1297 static PyObject* GetDependencies(PyFileDescriptor* self, void *closure) { | 1278 static PyObject* GetDependencies(PyFileDescriptor* self, void *closure) { |
1298 return NewFileDependencies(_GetDescriptor(self)); | 1279 return NewFileDependencies(_GetDescriptor(self)); |
1299 } | 1280 } |
1300 | 1281 |
1301 static PyObject* GetPublicDependencies(PyFileDescriptor* self, void *closure) { | 1282 static PyObject* GetPublicDependencies(PyFileDescriptor* self, void *closure) { |
1302 return NewFilePublicDependencies(_GetDescriptor(self)); | 1283 return NewFilePublicDependencies(_GetDescriptor(self)); |
1303 } | 1284 } |
1304 | 1285 |
1305 static PyObject* GetHasOptions(PyFileDescriptor *self, void *closure) { | 1286 static PyObject* GetHasOptions(PyFileDescriptor *self, void *closure) { |
1306 const FileOptions& options(_GetDescriptor(self)->options()); | 1287 const FileOptions& options(_GetDescriptor(self)->options()); |
(...skipping 29 matching lines...) Expand all Loading... |
1336 static PyGetSetDef Getters[] = { | 1317 static PyGetSetDef Getters[] = { |
1337 { "pool", (getter)GetPool, NULL, "pool"}, | 1318 { "pool", (getter)GetPool, NULL, "pool"}, |
1338 { "name", (getter)GetName, NULL, "name"}, | 1319 { "name", (getter)GetName, NULL, "name"}, |
1339 { "package", (getter)GetPackage, NULL, "package"}, | 1320 { "package", (getter)GetPackage, NULL, "package"}, |
1340 { "serialized_pb", (getter)GetSerializedPb}, | 1321 { "serialized_pb", (getter)GetSerializedPb}, |
1341 { "message_types_by_name", (getter)GetMessageTypesByName, NULL, | 1322 { "message_types_by_name", (getter)GetMessageTypesByName, NULL, |
1342 "Messages by name"}, | 1323 "Messages by name"}, |
1343 { "enum_types_by_name", (getter)GetEnumTypesByName, NULL, "Enums by name"}, | 1324 { "enum_types_by_name", (getter)GetEnumTypesByName, NULL, "Enums by name"}, |
1344 { "extensions_by_name", (getter)GetExtensionsByName, NULL, | 1325 { "extensions_by_name", (getter)GetExtensionsByName, NULL, |
1345 "Extensions by name"}, | 1326 "Extensions by name"}, |
1346 { "services_by_name", (getter)GetServicesByName, NULL, "Services by name"}, | |
1347 { "dependencies", (getter)GetDependencies, NULL, "Dependencies"}, | 1327 { "dependencies", (getter)GetDependencies, NULL, "Dependencies"}, |
1348 { "public_dependencies", (getter)GetPublicDependencies, NULL, "Dependencies"}, | 1328 { "public_dependencies", (getter)GetPublicDependencies, NULL, "Dependencies"}, |
1349 | 1329 |
1350 { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"}, | 1330 { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"}, |
1351 { "_options", (getter)NULL, (setter)SetOptions, "Options"}, | 1331 { "_options", (getter)NULL, (setter)SetOptions, "Options"}, |
1352 { "syntax", (getter)GetSyntax, (setter)NULL, "Syntax"}, | 1332 { "syntax", (getter)GetSyntax, (setter)NULL, "Syntax"}, |
1353 {NULL} | 1333 {NULL} |
1354 }; | 1334 }; |
1355 | 1335 |
1356 static PyMethodDef Methods[] = { | 1336 static PyMethodDef Methods[] = { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1465 static PyObject* GetContainingType(PyBaseDescriptor *self, void *closure) { | 1445 static PyObject* GetContainingType(PyBaseDescriptor *self, void *closure) { |
1466 const Descriptor* containing_type = | 1446 const Descriptor* containing_type = |
1467 _GetDescriptor(self)->containing_type(); | 1447 _GetDescriptor(self)->containing_type(); |
1468 if (containing_type) { | 1448 if (containing_type) { |
1469 return PyMessageDescriptor_FromDescriptor(containing_type); | 1449 return PyMessageDescriptor_FromDescriptor(containing_type); |
1470 } else { | 1450 } else { |
1471 Py_RETURN_NONE; | 1451 Py_RETURN_NONE; |
1472 } | 1452 } |
1473 } | 1453 } |
1474 | 1454 |
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 | |
1497 static PyGetSetDef Getters[] = { | 1455 static PyGetSetDef Getters[] = { |
1498 { "name", (getter)GetName, NULL, "Name"}, | 1456 { "name", (getter)GetName, NULL, "Name"}, |
1499 { "full_name", (getter)GetFullName, NULL, "Full name"}, | 1457 { "full_name", (getter)GetFullName, NULL, "Full name"}, |
1500 { "index", (getter)GetIndex, NULL, "Index"}, | 1458 { "index", (getter)GetIndex, NULL, "Index"}, |
1501 | 1459 |
1502 { "containing_type", (getter)GetContainingType, NULL, "Containing type"}, | 1460 { "containing_type", (getter)GetContainingType, NULL, "Containing type"}, |
1503 { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"}, | |
1504 { "_options", (getter)NULL, (setter)SetOptions, "Options"}, | |
1505 { "fields", (getter)GetFields, NULL, "Fields"}, | 1461 { "fields", (getter)GetFields, NULL, "Fields"}, |
1506 {NULL} | 1462 {NULL} |
1507 }; | 1463 }; |
1508 | 1464 |
1509 static PyMethodDef Methods[] = { | |
1510 { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS }, | |
1511 {NULL} | |
1512 }; | |
1513 | |
1514 } // namespace oneof_descriptor | 1465 } // namespace oneof_descriptor |
1515 | 1466 |
1516 PyTypeObject PyOneofDescriptor_Type = { | 1467 PyTypeObject PyOneofDescriptor_Type = { |
1517 PyVarObject_HEAD_INIT(&PyType_Type, 0) | 1468 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
1518 FULL_MODULE_NAME ".OneofDescriptor", // tp_name | 1469 FULL_MODULE_NAME ".OneofDescriptor", // tp_name |
1519 sizeof(PyBaseDescriptor), // tp_basicsize | 1470 sizeof(PyBaseDescriptor), // tp_basicsize |
1520 0, // tp_itemsize | 1471 0, // tp_itemsize |
1521 0, // tp_dealloc | 1472 0, // tp_dealloc |
1522 0, // tp_print | 1473 0, // tp_print |
1523 0, // tp_getattr | 1474 0, // tp_getattr |
(...skipping 10 matching lines...) Expand all Loading... |
1534 0, // tp_setattro | 1485 0, // tp_setattro |
1535 0, // tp_as_buffer | 1486 0, // tp_as_buffer |
1536 Py_TPFLAGS_DEFAULT, // tp_flags | 1487 Py_TPFLAGS_DEFAULT, // tp_flags |
1537 "A Oneof Descriptor", // tp_doc | 1488 "A Oneof Descriptor", // tp_doc |
1538 0, // tp_traverse | 1489 0, // tp_traverse |
1539 0, // tp_clear | 1490 0, // tp_clear |
1540 0, // tp_richcompare | 1491 0, // tp_richcompare |
1541 0, // tp_weaklistoffset | 1492 0, // tp_weaklistoffset |
1542 0, // tp_iter | 1493 0, // tp_iter |
1543 0, // tp_iternext | 1494 0, // tp_iternext |
1544 oneof_descriptor::Methods, // tp_methods | 1495 0, // tp_methods |
1545 0, // tp_members | 1496 0, // tp_members |
1546 oneof_descriptor::Getters, // tp_getset | 1497 oneof_descriptor::Getters, // tp_getset |
1547 &descriptor::PyBaseDescriptor_Type, // tp_base | 1498 &descriptor::PyBaseDescriptor_Type, // tp_base |
1548 }; | 1499 }; |
1549 | 1500 |
1550 PyObject* PyOneofDescriptor_FromDescriptor( | 1501 PyObject* PyOneofDescriptor_FromDescriptor( |
1551 const OneofDescriptor* oneof_descriptor) { | 1502 const OneofDescriptor* oneof_descriptor) { |
1552 return descriptor::NewInternedDescriptor( | 1503 return descriptor::NewInternedDescriptor( |
1553 &PyOneofDescriptor_Type, oneof_descriptor, NULL); | 1504 &PyOneofDescriptor_Type, oneof_descriptor, NULL); |
1554 } | 1505 } |
1555 | 1506 |
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 | |
1772 // Add a enum values to a type dictionary. | 1507 // Add a enum values to a type dictionary. |
1773 static bool AddEnumValues(PyTypeObject *type, | 1508 static bool AddEnumValues(PyTypeObject *type, |
1774 const EnumDescriptor* enum_descriptor) { | 1509 const EnumDescriptor* enum_descriptor) { |
1775 for (int i = 0; i < enum_descriptor->value_count(); ++i) { | 1510 for (int i = 0; i < enum_descriptor->value_count(); ++i) { |
1776 const EnumValueDescriptor* value = enum_descriptor->value(i); | 1511 const EnumValueDescriptor* value = enum_descriptor->value(i); |
1777 ScopedPyObjectPtr obj(PyInt_FromLong(value->number())); | 1512 ScopedPyObjectPtr obj(PyInt_FromLong(value->number())); |
1778 if (obj == NULL) { | 1513 if (obj == NULL) { |
1779 return false; | 1514 return false; |
1780 } | 1515 } |
1781 if (PyDict_SetItemString(type->tp_dict, value->name().c_str(), obj.get()) < | 1516 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... |
1831 | 1566 |
1832 if (PyType_Ready(&PyEnumValueDescriptor_Type) < 0) | 1567 if (PyType_Ready(&PyEnumValueDescriptor_Type) < 0) |
1833 return false; | 1568 return false; |
1834 | 1569 |
1835 if (PyType_Ready(&PyFileDescriptor_Type) < 0) | 1570 if (PyType_Ready(&PyFileDescriptor_Type) < 0) |
1836 return false; | 1571 return false; |
1837 | 1572 |
1838 if (PyType_Ready(&PyOneofDescriptor_Type) < 0) | 1573 if (PyType_Ready(&PyOneofDescriptor_Type) < 0) |
1839 return false; | 1574 return false; |
1840 | 1575 |
1841 if (PyType_Ready(&PyServiceDescriptor_Type) < 0) | |
1842 return false; | |
1843 | |
1844 if (PyType_Ready(&PyMethodDescriptor_Type) < 0) | |
1845 return false; | |
1846 | |
1847 if (!InitDescriptorMappingTypes()) | 1576 if (!InitDescriptorMappingTypes()) |
1848 return false; | 1577 return false; |
1849 | 1578 |
1850 return true; | 1579 return true; |
1851 } | 1580 } |
1852 | 1581 |
1853 } // namespace python | 1582 } // namespace python |
1854 } // namespace protobuf | 1583 } // namespace protobuf |
1855 } // namespace google | 1584 } // namespace google |
OLD | NEW |