Custom field options in Python

781 views
Skip to first unread message

Jan Koriťák

unread,
Sep 11, 2012, 9:07:57 AM9/11/12
to prot...@googlegroups.com
Hello,

We would like to use Custom options for message fields in Python.

"As of this writing (version 2.0.3), custom options are not accessible in Python."

I would like to ask if there was any progress since then or can it be expected in near future ?

I have actually tried to use the custom options, and I could only get it working after hacking the generated Python file by moving the line with call to "RegisterExtension" right after  definition of the Extension field. So maybe we just need to tweak protoc a bit.

My test proto:
import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
  optional string comment = 50002;
}

message MyMessage {
  optional string foo = 1 [(comment) = "Not particularly useful field"];
}

Python console:
In [1]: import test_pb2

In [2]: test_pb2.MyMessage.DESCRIPTOR.fields[0].GetOptions().Extensions[test_pb2.comment]
Out[2]: u''

Hacked test_pb2.py:
...
COMMENT_FIELD_NUMBER = 50002
comment = descriptor.FieldDescriptor(
  name='comment', full_name='comment', index=0,
  number=50002, type=9, cpp_type=9, label=1,
  has_default_value=False, default_value=unicode("", "utf-8"),
  message_type=None, enum_type=None, containing_type=None,
  is_extension=True, extension_scope=None,
  options=None)

# MOVED FROM THE END OF FILE
google.protobuf.descriptor_pb2.FieldOptions.RegisterExtension(comment)
...

Python console:

In [1]: import test_pb2

In [2]: test_pb2.MyMessage.DESCRIPTOR.fields[0].GetOptions().Extensions[test_pb2.comment]
Out[2]: u'Not particularly useful field'

Best regards,
Jan Koriťák

Jason Hsueh

unread,
Sep 11, 2012, 4:49:48 PM9/11/12
to Jan Koriťák, Protocol Buffers
This has also been implemented internally and will be included in the next release.


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/b4pQsNMoEzYJ.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.

Reply all
Reply to author
Forward
0 new messages