Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix python code generation compatibility with Cython #13593

Closed
wants to merge 1 commit into from

Conversation

vthib
Copy link
Contributor

@vthib vthib commented Aug 18, 2023

This is a follow-up to #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.

If possible, i would love to have this fix in the next 24 release, instead of having to wait for the 25 release.

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`.
@vthib vthib requested a review from a team as a code owner August 18, 2023 09:59
@vthib vthib requested review from anandolee and removed request for a team August 18, 2023 09:59
@anandolee
Copy link
Contributor

Thank you for the PR. As it changes generated code, I may need to run a wide tests in google internal and submit there

copybara-service bot pushed a commit that referenced this pull request Aug 28, 2023
copied from #13593

PiperOrigin-RevId: 560774287
@anandolee anandolee requested review from deannagarcia and removed request for deannagarcia August 28, 2023 18:49
@anandolee anandolee self-assigned this Aug 28, 2023
@anandolee
Copy link
Contributor

anandolee commented Aug 28, 2023

Turns out tests can not pass. Example error:

_globals['_ANY.fields_by_name['type_url']']._options = None
SyntaxError: invalid syntax. Perhaps you forgot a comma?

I will update the fix

copybara-service bot pushed a commit that referenced this pull request Aug 28, 2023
raised by #13593

PiperOrigin-RevId: 560774287
copybara-service bot pushed a commit that referenced this pull request Aug 29, 2023
raised by #13593

PiperOrigin-RevId: 560774287
copybara-service bot pushed a commit that referenced this pull request Aug 29, 2023
raised by #13593

PiperOrigin-RevId: 561077681
@anandolee
Copy link
Contributor

should be fixed with e65d051

@anandolee anandolee closed this Aug 29, 2023
@vthib vthib deleted the fix-cython-compatibility branch August 29, 2023 18:30
@vthib
Copy link
Contributor Author

vthib commented Aug 29, 2023

Thanks!

@vthib
Copy link
Contributor Author

vthib commented Aug 30, 2023

@anandolee Is it possible to backport this fix into the next 24.X release?

@vthib
Copy link
Contributor Author

vthib commented Sep 8, 2023

@anandolee Sorry for the bother, I saw the new release 24.3 release but it does not contain this fix. Is it possible to backport it for the next 24.X release please?

@vthib
Copy link
Contributor Author

vthib commented Sep 21, 2023

@anandolee Can I do a PR to backport this fix into the 24.X branch?

@vthib
Copy link
Contributor Author

vthib commented Sep 28, 2023

I pushed a PR request for the backport here: #14240

vthib pushed a commit to vthib/protobuf that referenced this pull request Sep 28, 2023
@hothanhloc68
Copy link

This is a follow-up to #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.

If possible, i would love to have this fix in the next 24 release, instead of having to wait for the 25 release.

@hothanhloc68
Copy link

This is a follow-up to #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.

If possible, i would love to have this fix in the next 24 release, instead of having to wait for the 25 release.

@hothanhloc68
Copy link

Turns out tests can not pass. Example error:

_globals['_ANY.fields_by_name['type_url']']._options = None
SyntaxError: invalid syntax. Perhaps you forgot a comma?

I will update the fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants