Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
iritkatriel merged 18 commits intopython:mainfromxuantengh:main
Nov 8, 2024

Conversation

@xuantengh
Copy link
Contributor

@xuantenghxuantengh commentedOct 31, 2024
edited by bedevere-appbot
Loading

This is the ongoging work of#126101, where we introduced a new attributeste_has_docstring in the symbol table entry. This PR aims to set this attribute for modules and classes whenever there are docstrings for them. Meanwhile, it also preventsNone being added toco_consts for lambda, annotation and type alias.

@xuantenghxuantenghforce-pushed themain branch 2 times, most recently from1dc125e to8622d78CompareNovember 2, 2024 13:16
@xuantengh
Copy link
ContributorAuthor

Just kindly to ask any further updates needed? I'm not sure whether it's proper to re-request review in CPython PR workflow.

Copy link
Member

@iritkatrieliritkatriel left a 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.

PyObject*docstring=_PyAST_GetDocString(body);
assert(OPTIMIZATION_LEVEL(c)<2||docstring==NULL);
if (docstring) {
assert(ste->ste_has_docstring);
Copy link
Member

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)?

Copy link
ContributorAuthor

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.

Copy link
Member

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?

Copy link
ContributorAuthor

@xuantenghxuantenghNov 4, 2024
edited
Loading

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.

Copy link
ContributorAuthor

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.

Copy link
Member

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.

Right, but we want to remove that hack.

Copy link
ContributorAuthor

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?

@xuantenghxuantenghforce-pushed themain branch 4 times, most recently fromd9c97a6 to7db42a1CompareNovember 4, 2024 11:56
@xuantengh
Copy link
ContributorAuthor

@iritkatriel Hi, I've encountered an issue when trying to remove f-string conversion inastfold_body. For the following function whenopt=0 or1:

deffunc():"not "+"a docstring"

astfold_body prevents the concatenated string (optimized as const expression statement somewhere during AST optimization I guess) being parsed as docstring by converting it into f-string. But if we remove the logic, the symbol table generation cannot distinguish"not a docstring" is originally a real docstring, or optimized concatenated string.

@iritkatriel
Copy link
Member

@iritkatriel Hi, I've encountered an issue when trying to remove f-string conversion inastfold_body. For the following function whenopt=0 or1:

deffunc():"not "+"a docstring"

astfold_body prevents the concatenated string (optimized as const expression statement somewhere during AST optimization I guess) being parsed as docstring by converting it into f-string. But if we remove the logic, the symbol table generation cannot distinguish"not a docstring" is originally a real docstring, or optimized concatenated string.

I thought the idea is that it usesste_has_docstring to determine whether there is a docstring or not, rather than the type of the first expression.

JelleZijlstra reacted with thumbs up emoji

@xuantengh
Copy link
ContributorAuthor

I thought the idea is that it usesste_has_docstring to determine whether there is a docstring or not, rather than the type of the first expression.

Sorry I'm a little confused now. 😂

If we remove the f-string conversion in theastfold_body function, the_PyAST_GetDocString returns true and setste_has_docstring incorrectly in symbol table generation stage.

cpython/Python/symtable.c

Lines 1846 to 1848 in83ba8c2

if (_PyAST_GetDocString(s->v.FunctionDef.body)) {
new_ste->ste_has_docstring=1;
}

The reason is_PyAST_GetDocString checks whether there is a docstring by inspecting the first expression type. If it's aConstant_kind, then it returns true. If we remove the f-string conversion in AST optimization stage, it cannot distinguish the constant expression is actually a docstring or concatenated string expression. In either cases, it returns true andste_has_docstring will be set.

If there is something I misunderstood, please correct me, thanks!

@JelleZijlstra
Copy link
Member

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
Copy link
ContributorAuthor

xuantengh commentedNov 6, 2024
edited
Loading

I guess the problem is that we perform AST optimization before we build the symtable

Yeah that's the problem. That's say, we cannot convey the information from AST construction to symtable generation phase.

Maybe we can instead run the symtable before ast optimization, so we can get rid of the f-string trick in the AST optimizer.

Theoratically it's feasible, but I'm afraid that there are side-effects and the scope is beyond this PR.

@iritkatriel
Copy link
Member

My proposal (in#126072 (comment)) was to move the docstring removal to the symtable, so it's all in one place.

@xuantengh
Copy link
ContributorAuthor

xuantengh commentedNov 6, 2024
edited
Loading

My proposal (in#126072 (comment)) was to move the docstring removal to the symtable, so it's all in one place.

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.

@xuantenghxuantenghforce-pushed themain branch 2 times, most recently from2194f4f to390caaaCompareNovember 8, 2024 06:40
@xuantengh
Copy link
ContributorAuthor

Revert to the version without swapping AST optimization and symtable generation. The docstring removal inastfold_body can be omitted in cunrrent implementation, but the f-string conversion should be retained. IMHO, removing the f-string conversion will introduce a lot of compromise changes, which even makes the code more complicated.

@iritkatriel
Copy link
Member

Specifically, can we disable the constant string concatenation optimization in AST when opt=1?

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
Copy link
ContributorAuthor

I think we should leave symtable after ast_opt and try my suggestion to just move the docstring handling from ast_opt to symtable.

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'aaaaaaaa' is really a docstring, or the optimized concatenated string. So the compiler will incorrectly setste_has_docstring.

@iritkatriel
Copy link
Member

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?

xuantengh reacted with thumbs up emoji

@iritkatrieliritkatrielenabled auto-merge (squash)November 8, 2024 14:17
@iritkatrieliritkatriel merged commit6ec8865 intopython:mainNov 8, 2024
39 checks passed
picnixz pushed a commit to picnixz/cpython that referenced this pull requestDec 8, 2024
ebonnal pushed a commit to ebonnal/cpython that referenced this pull requestJan 12, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@JelleZijlstraJelleZijlstraJelleZijlstra left review comments

@iritkatrieliritkatrieliritkatriel approved these changes

@carljmcarljmAwaiting requested review from carljmcarljm is a code owner

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

@isidenticalisidenticalAwaiting requested review from isidentical

@Eclips4Eclips4Awaiting requested review from Eclips4

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@xuantengh@iritkatriel@JelleZijlstra

[8]ページ先頭

©2009-2025 Movatter.jp