Skip to content

gh-90763: Modernise xx template module initialisation - #93078

Merged
erlend-aasland merged 3 commits into
python:mainfrom
erlend-aasland:xxmodule-refs
Jun 10, 2022
Merged

erlend-aasland merged 3 commits into
python:mainfrom
erlend-aasland:xxmodule-refs

Conversation

@erlend-aasland

@erlend-aasland erlend-aasland commented May 22, 2022 •

Copy link
Copy Markdown
Contributor

xxmodule:

  • remove incorrect decref of module object if module init fails
  • modernise API usage; use PyModule_AddType iso. PyModule_AddObject

xxlimited_35:

  • remove incorrect decref of module object if module init fails
  • check return of PyModule_AddObject
  • fix module name in type objects

Use C APIs such as PyModule_AddType instead of PyModule_AddObject.
Also remove incorrect module decrefs if module fails to initialise.
@erlend-aasland

Copy link
Copy Markdown
Contributor Author

@corona10, would you mind reviewing this? It's been a while since I've done the module init ref count dances :)

I note that xxlimited_35 creates two objects from the same type spec. I'm not sure if there's a point in doing that.

@erlend-aasland
erlend-aasland requested a review from encukou May 22, 2022 19:52
@erlend-aasland erlend-aasland linked an issue May 22, 2022 that may be closed by this pull request

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall looks good to me with nit comment.

Comment thread Modules/xxlimited_35.c Resolved
Comment thread Modules/xxlimited_35.c Resolved
@erlend-aasland
erlend-aasland requested a review from corona10 May 23, 2022 05:25

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@erlend-aasland

ghost commented May 23, 2022

Copy link
Copy Markdown
Contributor Author

Thanks, Dong-hee! I'll hold the merge a couple of days to give Petr (or Victor) a chance to review.

@encukou

ghost commented May 23, 2022

Copy link
Copy Markdown
Member

Looks good, thanks!
If it was me, instead of create_and_add_type I'd write out the two calls each time: it's a historical module so I'd keep changes to minimal bugfixes. It's OK to showcase how clunky the old API is :)
But that's a minor nit, feel free to merge as is.

@erlend-aasland

ghost commented May 23, 2022

Copy link
Copy Markdown
Contributor Author

Looks good, thanks!
If it was me, instead of create_and_add_type I'd write out the two calls each time: it's a historical module so I'd keep changes to minimal bugfixes. It's OK to showcase how clunky the old API is :)
But that's a minor nit, feel free to merge as is.

I had to ponder a little bit about this; I agree that it's worth it keeping the changes minimal for these modules :)

Comment thread Modules/xxlimited_35.c Resolved
Comment thread Modules/xxlimited_35.c Resolved
Comment thread Modules/xxmodule.c Resolved
Comment thread Modules/xxmodule.c Resolved
@vstinner

ghost commented May 23, 2022

Copy link
Copy Markdown
Member

The point of this module is to show that static types and global variables still work.

The file starts with:

Use this file as a template to start implementing a module that also declares object types.

IMO it should be a show case of the best practice to write a C extension in 2022. Otherwise, people will continue to copy/paste this old code and will propagate static types and other legacy code to their project :-(

@vstinner

ghost commented May 23, 2022

Copy link
Copy Markdown
Member

If you want to test that the old way to write a C extension, with static types and global variables, still work, IMO it should be another module than this "template".

@erlend-aasland

ghost commented May 23, 2022 •

Copy link
Copy Markdown
Contributor Author

If you want to test that the old way to write a C extension, with static types and global variables, still work, IMO it should be another module than this "template".

We could adjust the wording in the top of the file, and also rename it to something like xxlegacy.c. But I think this warrants a broader discussion :)

IMO it should be a show case of the best practice to write a C extension in 2022. Otherwise, people will continue to copy/paste this old code and will propagate static types and other legacy code to their project :-(

That's what the xxlimited.c module is :)

@encukou

ghost commented May 24, 2022

Copy link
Copy Markdown
Member

xxlimited shows the new way. The historical comment in xxmodule could be changed, but IMO the module name isn't misleading enough to rename it.

Anyway, the more important thing to do here is converting the tutorial. That will, in my opinion, make the new way official :)
But I don't want to do that until PEP 687 is accepted, and more PEP 630 open issues are solved. (FWIW: Metaclasses and Per-class scope look like they could go in anearly 3.12 alpha, Type checking should hopefully go in 3.12 as well, and lossless conversion isn't blocking for me.)

But yes, all that is out of scope of this PR.

@erlend-aasland

ghost commented Jun 9, 2022

Copy link
Copy Markdown
Contributor Author

FYI, I plan to merge this tomorrow.

@erlend-aasland
erlend-aasland merged commit a87c9b5 into python:main Jun 10, 2022
@erlend-aasland
erlend-aasland deleted the xxmodule-refs branch June 10, 2022 10:39

ghost left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

Comment thread Modules/xxmodule.c Resolved
@vstinner

ghost commented Jun 10, 2022

Copy link
Copy Markdown
Member

I'm fine with backporting the whole PR. Most changes are related to the bugfix.

@miss-islington

ghost commented Jun 10, 2022

Copy link
Copy Markdown
Contributor

Thanks @erlend-aasland for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@miss-islington

ghost commented Jun 10, 2022

Copy link
Copy Markdown
Contributor

Thanks @erlend-aasland for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

@miss-islington

ghost commented Jun 10, 2022

Copy link
Copy Markdown
Contributor

Sorry @erlend-aasland, I had trouble checking out the 3.11 backport branch.
Please backport using cherry_picker on command line.
cherry_picker a87c9b538fbfc42883417c4d5e69f1a5922690e3 3.11

@bedevere-bot

ghost commented Jun 10, 2022

Copy link
Copy Markdown

GH-93679 is a backport of this pull request to the 3.10 branch.

@bedevere-bot

ghost commented Jun 10, 2022

Copy link
Copy Markdown

GH-93681 is a backport of this pull request to the 3.11 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Py_XDECREF() module on fail in Py_mod_exec

6 participants