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-130453: pygettext: Extend support for specifying custom keywords#130463

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
serhiy-storchaka merged 10 commits intopython:mainfromtomasr8:pygettext-keywordspec
Feb 25, 2025

Conversation

@tomasr8
Copy link
Member

@tomasr8tomasr8 commentedFeb 22, 2025
edited by bedevere-appbot
Loading

This addresses the first point in#130453

It is now possible to use the fullkeyword spec syntax (except fort, that will be added later) to specify keywords:

./python Tools/i18n/pygettext.py --keyword=foo:1./python Tools/i18n/pygettext.py --keyword=foo:1,2./python Tools/i18n/pygettext.py --keyword=foo:1c,2./python Tools/i18n/pygettext.py --keyword=foo:1c,2,3

I tried to match the behaviour of xgettext and babel but neither seem to do much validation for the keyword specs.
xgettext, for instance, does not allowfoo:1c,2c (context specified twice) norfoo:1,1c (msgid andmsgctxt have the same index) but it does (weirdly) allowfoo:1,1 (same index formsgid andmsgid_plural), whereas it outright crashes with a double free forfoo:1,1,2c.

This PR properly validates the keyword specs in order to be consistent and provide helpful error messages to the user.

Feedback welcome!

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I do not think that it was necessary to go so far with detecting errors and generating error reports. Garbage in -- garbage out. The parsing code could be 2 or 3 times smaller without this. But if you already implemented this, it is fine.

LGTM.

raiseValueError(f'Invalid keyword spec{spec!r}: '
'msgctxt cannot appear without msgid')

returnname, {v:kfork,vinresult.items()}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Would it be simpler to buildresult in that form from the beginning?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I did that ind861c84, let me know if you like it better like that

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It was just a question. I am fine with both variants.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I just wanted to let you see the difference :) I don't have a strong preference either, let's stick with the current version, then?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Actually, I tried implementing some followup work on top of this PR (support for thet specifier, multiple keywords with the same funcname) and it's better to use the original representation because the diff in the followup PRs will be smaller.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

So I did the right thing by letting the PR lie down for two days. 😄

tomasr8 reacted with laugh emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, good call 🙂 And thanks for your super thorough reviews! It's really appreciated

try:
options.keywords=dict(parse_spec(spec)forspecinoptions.keywords)
exceptValueErrorase:
raiseSystemExit(e)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Other errors causeprint() +sys.exit(1).

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Indeed, though I believeraise SystemExit(s) is functionally equivalent toprint(..., file=sys.stderr) +sys.exit(1) and since it's shorter and the intent is clearer, I thought I'd start using that instead.

Though if you prefer to be consistent, I can change it toprint+sys.exit?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, it just for consistency (also,sys.exit() allows to set different return codes, but this is not used here).

tomasr8 reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Updated in18d29cb to useprint+sys.exit

@tomasr8
Copy link
MemberAuthor

I do not think that it was necessary to go so far with detecting errors and generating error reports. Garbage in -- garbage out. The parsing code could be 2 or 3 times smaller without this. But if you already implemented this, it is fine.

Honestly, if you prefer it without the detailed error messages, I am fine with removing them. Just let me know!

My thinking for adding them was that most people using this will not be that familiar with the syntax and for them, it's better to show a descriptive error rather than fail silently, but as I said, if you prefer to have simpler code, that's also ok!

try:
options.keywords=dict(parse_spec(spec)forspecinoptions.keywords)
exceptValueErrorase:
raiseSystemExit(e)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, it just for consistency (also,sys.exit() allows to set different return codes, but this is not used here).

tomasr8 reacted with thumbs up emoji
raiseValueError(f'Invalid keyword spec{spec!r}: '
'msgctxt cannot appear without msgid')

returnname, {v:kfork,vinresult.items()}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It was just a question. I am fine with both variants.

@serhiy-storchakaserhiy-storchaka merged commit44213bc intopython:mainFeb 25, 2025
39 checks passed
@tomasr8tomasr8 deleted the pygettext-keywordspec branchFebruary 25, 2025 10:38
seehwan pushed a commit to seehwan/cpython that referenced this pull requestApr 16, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@serhiy-storchakaserhiy-storchakaAwaiting requested review from serhiy-storchaka

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@tomasr8@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp