protoc 23.4 Windows x64 Python strange generated file

197 views
Skip to first unread message

Nikita Ig

unread,
Aug 1, 2023, 7:35:32 AM8/1/23
to Protocol Buffers
Hello. I'm a rookie in protocol buffers and I'm trying to repeat example from documentation.

Step 1. Installed protoc
Step 2. Created test.proto file consisted this code:
syntax = "proto3";

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 results_per_page = 3;
}
Step 3. I ran protoc with help command:
protoc.exe --proto_path=${PWD}/protobuf --python_out=${PWD}/protobuf/ ${PWD}/protobuf/test.proto

Step 4. I got test_pb2.py file consisted this code:
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: test.proto
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import symbol_database as _symbol_database
from google.protobuf.internal import builder as _builder
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()




DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\ntest.proto\"M\n\rSearchRequest\x12\r\n\x05query\x18\x01 \x01(\t\x12\x13\n\x0bpage_number\x18\x02 \x01(\x05\x12\x18\n\x10results_per_page\x18\x03 \x01(\x05\x62\x06proto3')

_globals = globals()
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'test_pb2', _globals)
if _descriptor._USE_C_DESCRIPTORS == False:

  DESCRIPTOR._options = None
  _globals['_SEARCHREQUEST']._serialized_start=14
  _globals['_SEARCHREQUEST']._serialized_end=91
# @@protoc_insertion_point(module_scope)

But documentation said about other format file for python generated file and I have no idea what I need to import in my producer for create message object. Generated file didn't contain any classes or something other entity for import.

Thanks for any help.

Nikita Ig

unread,
Aug 1, 2023, 9:04:49 AM8/1/23
to Protocol Buffers
I installed protoc to my ubuntu and got version libprotoc 3.12.4
I reproduce all my steps and got typical example of pb2.py file consisted all data

# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: test.proto

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
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()




DESCRIPTOR = _descriptor.FileDescriptor(
  name='test.proto',
  package='',
  syntax='proto3',
  serialized_options=None,
  create_key=_descriptor._internal_create_key,
  serialized_pb=b'\n\ntest.proto\"M\n\rSearchRequest\x12\r\n\x05query\x18\x01 \x01(\t\x12\x13\n\x0bpage_number\x18\x02 \x01(\x05\x12\x18\n\x10results_per_page\x18\x03 \x01(\x05\x62\x06proto3'
)




_SEARCHREQUEST = _descriptor.Descriptor(
  name='SearchRequest',
  full_name='SearchRequest',
  filename=None,
  file=DESCRIPTOR,
  containing_type=None,
  create_key=_descriptor._internal_create_key,
  fields=[
    _descriptor.FieldDescriptor(
      name='query', full_name='SearchRequest.query', index=0,
      number=1, type=9, cpp_type=9, label=1,
      has_default_value=False, default_value=b"".decode('utf-8'),
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      serialized_options=None, file=DESCRIPTOR,  create_key=_descriptor._internal_create_key),
    _descriptor.FieldDescriptor(
      name='page_number', full_name='SearchRequest.page_number', index=1,
      number=2, 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,
      serialized_options=None, file=DESCRIPTOR,  create_key=_descriptor._internal_create_key),
    _descriptor.FieldDescriptor(
      name='results_per_page', full_name='SearchRequest.results_per_page', 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,
      serialized_options=None, file=DESCRIPTOR,  create_key=_descriptor._internal_create_key),
  ],
  extensions=[
  ],
  nested_types=[],
  enum_types=[
  ],
  serialized_options=None,
  is_extendable=False,
  syntax='proto3',
  extension_ranges=[],
  oneofs=[
  ],
  serialized_start=14,
  serialized_end=91,
)

DESCRIPTOR.message_types_by_name['SearchRequest'] = _SEARCHREQUEST
_sym_db.RegisterFileDescriptor(DESCRIPTOR)

SearchRequest = _reflection.GeneratedProtocolMessageType('SearchRequest', (_message.Message,), {
  'DESCRIPTOR' : _SEARCHREQUEST,
  '__module__' : 'test_pb2'
  # @@protoc_insertion_point(class_scope:SearchRequest)
  })
_sym_db.RegisterMessage(SearchRequest)


# @@protoc_insertion_point(module_scope)


And I have a question  - what did I wrong in my first attempt? Is it a new version of protoc? or is it a problem in my Windows? Or anything else?

Nikita Ig

unread,
Aug 1, 2023, 10:32:49 AM8/1/23
to Protocol Buffers

Oh, I'm so sorry. I can understand my problems. I suppose that conversation may close and delete.

Charlie Tian

unread,
Sep 15, 2023, 7:39:08 AM9/15/23
to Protocol Buffers

I got this problem too! Can you explain why your first attemp generate "wrong" file ?

Nikita Ig

unread,
Sep 15, 2023, 12:06:55 PM9/15/23
to Protocol Buffers
As I understand it's a new version of protoc. And now all fields for protobuff message dynamically adding at runtime. You can run your program and set breakpoint. You could find all fields for message. Pycharm shows errors about this field, but now it's a dynamical object and you'll get correct message.

Derek Perez

unread,
Sep 15, 2023, 1:08:45 PM9/15/23
to Nikita Ig, Protocol Buffers
you can also set --pyi_out to get the interface/type definitions I believe!

- D

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/28077f2b-cfa3-4d6f-8f88-4515607033b7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages