GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-118761: Expose more core interpreter types in ``_types`` by AA-Turner · Pull Request #132103 · python/cpython

Conversation

@AA-Turner

@AA-Turner AA-Turner commented Apr 5, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This was discussed in #131969 (cc @ZeroIntensity), but deferred to get the smaller change in. This PR adds all of the core interpreter types currently exposed in the types module to _typesmodule.c. We then change the definition of types.py to prefer these, but if the _types module is not available, to use the pure-Python fallbacks.

Strictly, this isn't needed, as _types is a built-in module. I think it might be helpful to keep the pure Python definitions for other implementations (PyPy, etc), and perhaps as a quick reminder of what each type looks like. Open to thoughts on this, as it would clearly be simpler to replace everything with from _types import *.

I've initially categorised this under #118761, as this does provide a ~15% speed-up for me (1627µs to 1403µs).

A

  • Issue: Improve import time of various stdlib modules #118761

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AA-Turner
Expose more core types in _types
737216d
@AA-Turner AA-Turner added performance Performance or resource usage skip news labels Apr 5, 2025
@AA-Turner
AA-Turner requested review from ZeroIntensity and picnixz April 5, 2025 00:24
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 5, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Apr 5, 2025
Improve import time of various stdlib modules #118761
Closed
@AA-Turner AA-Turner mentioned this pull request Apr 5, 2025
gh-109599: Expose CapsuleType via the _types module #131969
Merged
XuehaiPan
XuehaiPan reviewed Apr 5, 2025
View reviewed changes
Comment thread Lib/types.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

picnixz
picnixz reviewed Apr 5, 2025
View reviewed changes
Comment thread Modules/_typesmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_typesmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/types.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

XuehaiPan
XuehaiPan reviewed Apr 5, 2025
View reviewed changes
Comment thread Lib/types.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AA-Turner
Use a macro
0c3c8be
ZeroIntensity
ZeroIntensity reviewed Apr 5, 2025
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

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.

It might be worth adding a _pytypes module to hold the pure-Python fallbacks.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AA-Turner
Test that types are identical
51cd97b
@AA-Turner

AA-Turner commented Apr 5, 2025

Copy link
Copy Markdown
Member Author

It might be worth adding a _pytypes module to hold the pure-Python fallbacks.

As in the following?

try:
    from _types import *
except ImportError:
    from _pytypes import *

Could you elaborate on the benefits?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

picnixz
picnixz reviewed Apr 5, 2025
View reviewed changes
Comment thread Modules/_typesmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_typesmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Apr 5, 2025

Copy link
Copy Markdown
Member

Could you elaborate on the benefits?

One benefit is that it reduces by one the indentation level and it's easier to read/modify I think. Also, we do it for _pydecimal so why not doing it for _pytypes?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AA-Turner @picnixz
fix typo
84c2285
Co-authored-by: Bénédikt Tran <[email protected]>
@ZeroIntensity

ZeroIntensity commented Apr 5, 2025

Copy link
Copy Markdown
Member

Could you elaborate on the benefits?

Yeah, what Bénédikt said. It seems cleaner to encapsulate each implementation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Apr 5, 2025
View reviewed changes
Comment thread Modules/_typesmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AA-Turner

AA-Turner commented Apr 5, 2025

Copy link
Copy Markdown
Member Author

Also, we do it for _pydecimal so why not doing it for _pytypes?

_pydecimal, _pyio, etc are Python implementations of the C accelarator modules (see PEP-0399), whereas types still contains quite a lot only implemented in Python. See e.g. collections and functools which use extension modules, but don't have a _py<mod> helper module.

A

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AA-Turner added 2 commits April 5, 2025 16:50
@AA-Turner
Align comments
fd19ac6
@AA-Turner
Ensure EXPORT_STATIC_TYPE is used with static types
74aac08
@ZeroIntensity

ZeroIntensity commented Apr 5, 2025

Copy link
Copy Markdown
Member

I don't have much of a preference, it's just prettier. Are we planning to implement the other functions in C?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity reviewed Apr 5, 2025
View reviewed changes
Comment thread Modules/_typesmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AA-Turner
Update comments
0fbbf0e
@AA-Turner

AA-Turner commented Apr 5, 2025

Copy link
Copy Markdown
Member Author

Are we planning to implement the other functions in C?

I'm not personally, though Yury left a TODO comment by _GeneratorWrapper about implementing it in C. I'd probably prefer to keep the simpler functions in Python, but there might be benefits I haven't considered to converting to C.

A

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ZeroIntensity
ZeroIntensity approved these changes Apr 5, 2025
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

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.

LGTM. Let's do a _pytypes module in a follow-up if we decide to implement the rest of the functions in C.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
AA-Turner added 2 commits April 5, 2025 18:20
@AA-Turner
Tweak test_names()
3dd8e04
@AA-Turner
style
de81f97
picnixz
picnixz reviewed Apr 5, 2025
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

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.

Just some cosmetic suggesetions. Concerning the spaces before the comments, I don't think we now need to have some column-aligned comments (it's not even clear that they are aligned in the first place).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/types.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/types.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_types.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AA-Turner @picnixz
whitespace
816d2bc
Co-authored-by: Bénédikt Tran <[email protected]>
picnixz
picnixz approved these changes Apr 5, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Apr 5, 2025
@AA-Turner
AA-Turner enabled auto-merge (squash) April 5, 2025 17:56
@AA-Turner
AA-Turner merged commit 1755157 into python:main Apr 5, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 5, 2025
@AA-Turner
AA-Turner deleted the more-_types branch April 5, 2025 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@picnixz picnixz picnixz approved these changes
@ZeroIntensity ZeroIntensity ZeroIntensity approved these changes
+1 more reviewer
@XuehaiPan XuehaiPan XuehaiPan left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

performance Performance or resource usage skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@AA-Turner @picnixz @ZeroIntensity @XuehaiPan
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.