Skip to content

Commit

Permalink
fix python code generation compatibility with Cython
Browse files Browse the repository at this point in the history
This is a follow-up to
protocolbuffers#11011

The generation is still not compatible with Cython when maps are used.

For example, this protobuf file:

```
syntax = "proto3";

message Foo {
    map<string, string> bar = 1;
}
```

Will generate:

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

  DESCRIPTOR._options = None
  _FOO_BARENTRY._options = None
  _FOO_BARENTRY._serialized_options = b'8\001'
  _globals['_FOO']._serialized_start=11
  _globals['_FOO']._serialized_end=88
  _globals['_FOO_BARENTRY']._serialized_start=46
  _globals['_FOO_BARENTRY']._serialized_end=88
```

The `_FOO_BARENTRY` variable is not defined anywhere and confuses
cython. We can see the `_globals` used below, it is simply missing for
the first two lines using `_FOO_BARENTRY`.
  • Loading branch information
vthib committed Aug 18, 2023
1 parent 9f45862 commit 8c37e01
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/python/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1199,8 +1199,8 @@ void PrintDescriptorOptionsFixingCode(absl::string_view descriptor,
// Reset the _options to None thus DescriptorBase.GetOptions() can
// parse _options again after extensions are registered.
printer->Print(
"$descriptor$._options = None\n"
"$descriptor$._serialized_options = $serialized_value$\n",
"_globals['$descriptor$']._options = None\n"
"_globals['$descriptor$']._serialized_options = $serialized_value$\n",
"descriptor", descriptor, "serialized_value", options);
}
} // namespace
Expand Down

0 comments on commit 8c37e01

Please sign in to comment.