You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ah, still not sure, sorry. There's some subtlety here since it only occurs with nested functions (maybe with anything nested that can take a docstring?).
You're of course welcome to look into it, especially if you'd like to see more of the compiler, and if stuck we'll figure something out :)
The reason why this happens is that cpython forwards .__doc__ to just the first element of co_consts, which is either the docstring or None if there isn't one. We manually set .__doc__ = ... on the function object, but should probably switch to the other way of doing it.
The relevant file that handles this is compiler/codegen/src/compile.rs. It needs some tweaking to stuff __doc__ in the consts if it exists instead of generating the load and store for the __doc__.
You could start for functions (compile_function) and then the process should be similar to the other cases.
Activity
evanrittenhouse commentedon Mar 4, 2023
@DimitrisJim how difficult do you think this would be?
DimitrisJim commentedon Mar 7, 2023
Ah, still not sure, sorry. There's some subtlety here since it only occurs with nested functions (maybe with anything nested that can take a docstring?).
You're of course welcome to look into it, especially if you'd like to see more of the compiler, and if stuck we'll figure something out :)
evanrittenhouse commentedon Mar 8, 2023
Sure! I'd love to get onto the compiler side. Do you have a recommendation for where I should look?
coolreader18 commentedon Mar 9, 2023
The reason why this happens is that cpython forwards
.__doc__to just the first element ofco_consts, which is either the docstring or None if there isn't one. We manually set.__doc__ = ...on the function object, but should probably switch to the other way of doing it.DimitrisJim commentedon Mar 9, 2023
What Noa said, but in bytecode form, for a given function:
What RustPython generates:
While for CPython:
notice the missing instructions for
__doc__.The relevant file that handles this is
compiler/codegen/src/compile.rs. It needs some tweaking to stuff__doc__in the consts if it exists instead of generating the load and store for the__doc__.You could start for functions (
compile_function) and then the process should be similar to the other cases.Add regression test for RustPython#4505
Merge pull request #6417 from ShaharNaveh/regr-test-doc-leak