-
Notifications
You must be signed in to change notification settings - Fork 15.6k
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
Python generated code is no longer compatible with Cython since 3.20 #10800
Comments
What is the use case for this? What is the expected behavior if you let Cython compile the generated code? |
The expected behavior is to have the exact same behavior as without the Cython pass. Cython is usually used to improve performances, but it can also be used for obfuscation and protecting the source code. |
I see. That seems reasonable. We reference _globals = globals()
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'proto.a_pb2', _globals)
if _descriptor._USE_C_DESCRIPTORS == False:
DESCRIPTOR._options = None
_globals["_FOO"]._serialized_start=17 We could use I would worry some about the performance, except that |
Yes that would work well! |
Are you working on this @haberman ? I can try to make a PR for it otherwise |
I am not working on this currently. PRs welcome. |
Replace access to variables created in globals() by explicit access to the globals() array. This avoids static analysis of the code to error out on those unknown variables, and make the files cythonable. Fixes #10800 Closes #11011 FUTURE_COPYBARA_INTEGRATE_REVIEW=#11011 from vthib:10800-cython-compatibility 705da40 PiperOrigin-RevId: 493055381
Version: 4.21.8
Language: Python
Before the 3.20 update, the generated code was statically defining all the messages, and could be given to cython without issues.
Since the 3.20 update, those messages are dynamically generated, leading to a cython error.
For example, given this proto file
The codegen gives this:
$ protoc --python_out=gen proto/a.proto
This can no longer be compiled by cython:
Cython does not dynamically check variables from modifications of
globals()
, and thinks the variable is not set.This isn't really a regression since I suppose you do not support Cython. However, a small change could make this work with native python and cython:
Instead of generating this:
_FOO._serialized_start=17
generating this would fix the issue:
globals()["_FOO"]._serialized_start=17
This is not an easy fix to do with some post processing of the generated files with regexes and seds and stuff like this, but shouldn't be too hard to do in the code generator I suppose.
Would you be OK with such a change? That would be really useful for cython users. I can try to make this change if needed.
The text was updated successfully, but these errors were encountered: