Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
gh-126072: Set docstring attribute for module and class#126231
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Misc/NEWS.d/next/Core_and_Builtins/2024-10-31-21-49-00.gh-issue-126072.o9k8Ns.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
1dc125e to8622d78Comparexuantengh commentedNov 3, 2024
Just kindly to ask any further updates needed? I'm not sure whether it's proper to re-request review in CPython PR workflow. |
iritkatriel left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Excuse formatting, reviewing on my phone.
Uh oh!
There was an error while loading.Please reload this page.
Python/codegen.c Outdated
| PyObject*docstring=_PyAST_GetDocString(body); | ||
| assert(OPTIMIZATION_LEVEL(c)<2||docstring==NULL); | ||
| if (docstring) { | ||
| assert(ste->ste_has_docstring); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Is it still true that GetDocstring returns non-null only when ste_has_docstring is true?
Perhaps it should be
if(ste_has_docstring) assert(GetDocstring)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Currently I think they are equivalent, i.e.,ste_has_docstring == 1 iff. there is a docstring during symbol table generation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
And when you runpython -OO?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Currently when running with-OO, the first docstring is removed and the subsequent docstring is converted intoJoinedStr in the AST stage. For_PyAST_GetDocString, it will not identify the convertedJoinedStr as docstring, soste_has_docstring will not be set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Add a test for docstring optimization under-OO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Currently when running with
-OO, the first docstring is removed and the subsequent docstring is converted intoJoinedStrin the AST stage. For_PyAST_GetDocString, it will not identify the convertedJoinedStras docstring, soste_has_docstringwill not be set.
Right, but we want to remove that hack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Should we do it in this PR?
Uh oh!
There was an error while loading.Please reload this page.
d9c97a6 to7db42a1Comparexuantengh commentedNov 4, 2024
@iritkatriel Hi, I've encountered an issue when trying to remove f-string conversion in deffunc():"not "+"a docstring"
|
iritkatriel commentedNov 5, 2024
I thought the idea is that it uses |
xuantengh commentedNov 6, 2024
Sorry I'm a little confused now. 😂 If we remove the f-string conversion in the Lines 1846 to 1848 in83ba8c2
The reason is If there is something I misunderstood, please correct me, thanks! |
JelleZijlstra commentedNov 6, 2024
I guess the problem is that we perform AST optimization before we build the symtable, so we have to keep doing the f-string conversion to avoid breaking the symtable. Maybe we can instead run the symtablebefore ast optimization, so we can get rid of the f-string trick in the AST optimizer. |
xuantengh commentedNov 6, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Yeah that's the problem. That's say, we cannot convey the information from AST construction to symtable generation phase.
Theoratically it's feasible, but I'm afraid that there are side-effects and the scope is beyond this PR. |
iritkatriel commentedNov 6, 2024
My proposal (in#126072 (comment)) was to move the docstring removal to the symtable, so it's all in one place. |
xuantengh commentedNov 6, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
But I think the problem is still not addressed, we've lost some information after the AST optimization (the concatenated string has been optimized as constant string). Even though the docstring removal is put together with symtable, it still fails to distinguish in the above case. |
75bfe7b to0f06e57Compare2194f4f to390caaaComparexuantengh commentedNov 8, 2024
Revert to the version without swapping AST optimization and symtable generation. The docstring removal in |
iritkatriel commentedNov 8, 2024
No, this would mean we optimise less when we should be optimising more. I think we should leave symtable after ast_opt and try my suggestion to just move the docstring handling from ast_opt to symtable. |
xuantengh commentedNov 8, 2024
As mentioned above, what blocks us doing so is if we leave the docstring handling to symtable, for the following code: defwith_const_expression():"aa"*4 The optimized AST will be: Module(body=[FunctionDef(name='with_const_expression',args=arguments(),body=[Expr(value=Constant(value='aaaaaaaa'))])]) Then in the symtable stage, it cannot distinguish whether the constant string |
iritkatriel commentedNov 8, 2024
Ok, I see. I think we should just forget about that for this PR. Can you revert all the changes related to removing the hack, and make this PR just about about module and class change? |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
6ec8865 intopython:mainUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
This is the ongoging work of#126101, where we introduced a new attribute
ste_has_docstringin the symbol table entry. This PR aims to set this attribute for modules and classes whenever there are docstrings for them. Meanwhile, it also preventsNonebeing added toco_constsfor lambda, annotation and type alias.