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-109599: Expose `CapsuleType` via the `_types` module by AA-Turner · Pull Request #131969 · python/cpython

Conversation

@AA-Turner

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

Uh oh!

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

Copy link
Copy Markdown
Member

Micro-optimisation, but we should avoid re-calculating CapsuleType every time it is accessed in the types module.

A

  • Issue: Expose capsule type in types module #109599

Sorry, something went wrong.

Uh oh!

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

All reactions
@AA-Turner
Cache types.CapsuleType on first compution
81555b8
@AA-Turner AA-Turner added skip issue skip news needs backport to 3.13 only security fixes labels Apr 1, 2025
@AA-Turner
AA-Turner requested a review from pitrou April 1, 2025 08:57
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 1, 2025
@pitrou

pitrou commented Apr 1, 2025

Copy link
Copy Markdown
Member

How about exposing CapsuleType directly, for example as sys._CapsuleType, so that we get rid of the _socket dynamic loading hack?

All reactions

Sorry, something went wrong.

Uh oh!

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

@AA-Turner AA-Turner changed the title Cache types.CapsuleType on first compution gh-109599: Cache types.CapsuleType on first compution Apr 1, 2025
@AA-Turner AA-Turner removed the skip issue label Apr 1, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Apr 1, 2025
Expose capsule type in types module #109599
Closed
@AA-Turner

AA-Turner commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

It feels wrong to expose CapsuleType in sys (I don't believe there are any other types defined on the module?). Perhaps the ideal would be a _types extension module that types could import from?

Obviously that would be a much larger change: I can justify backporting this PR as a bugfix, but not a new module!

A

All reactions

Sorry, something went wrong.

Uh oh!

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

@pitrou

pitrou commented Apr 1, 2025

Copy link
Copy Markdown
Member

A micro-optimization is not a bugfix, so you shouldn't backport this PR in any case :)

The proposal to add a private symbol in sys was just pragmatic, precisely so that we don't create a new private extension module for a single symbol.

AA-Turner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@AA-Turner AA-Turner removed the needs backport to 3.13 only security fixes label Apr 1, 2025
@AA-Turner

AA-Turner commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

True! (removed labels)

Let's see if anyone else has an opinion, otherwise I'll have a look into the sys approach.

A

All reactions

Sorry, something went wrong.

Uh oh!

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

@picnixz

picnixz commented Apr 1, 2025

Copy link
Copy Markdown
Member

I don't think it's the correct place but at the same time the sysmodule says:

Various bits of information used by the interpreter are collected in module 'sys'.

So it could be the correct place, albeit not the most intuitive one. Note that we already do SimpleNamespace = type(sys.implementation). However, be careful that the sys variable is explicitly deleted so you might want to need a re-import in __getattr__ anyway as it's lazy (the reason is that it's to avoid exposing the variable as types._sys).

OTOH, we have a TODO note:

class _GeneratorWrapper:
    # TODO: Implement this in C.

so this might also be an opportunity to have a true _types built-in module.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ZeroIntensity

ZeroIntensity commented Apr 1, 2025

Copy link
Copy Markdown
Member

so this might also be an opportunity to have a true _types built-in module.

Which I implemented a few months ago. I did it for fun to see what kind of speedups it would provide, but didn't think the added complexity was worth upstreaming.

All reactions

Sorry, something went wrong.

Uh oh!

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

AA-Turner added 2 commits April 1, 2025 14:43
@AA-Turner
Add a _types module exposing CapsuleType and SimpleNamespace
f2a2530
@AA-Turner
Use the _types module in types
50c5173
@AA-Turner
AA-Turner requested review from a team, corona10 and erlend-aasland as code owners April 1, 2025 13:44
@AA-Turner

AA-Turner commented Apr 1, 2025

Copy link
Copy Markdown
Member Author

What do you think of this approach? The compexity would come from converting _GeneratorWrapper. The commits I've just pushed simply expose SimpleNamespace and CapsuleType, and nothing further (the full conversion could be explored in a dedicated PR).

A

All reactions

Sorry, something went wrong.

Uh oh!

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

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

Uh oh!

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

@pitrou

pitrou commented Apr 1, 2025

Copy link
Copy Markdown
Member

This approach looks good to me.

AA-Turner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@AA-Turner
Apply configure changes
8468449
@AA-Turner AA-Turner changed the title gh-109599: Cache types.CapsuleType on first compution gh-109599: Expose CapsuleType via the _types module Apr 1, 2025
@AA-Turner
AA-Turner requested a review from picnixz April 1, 2025 14:11
AA-Turner added 3 commits April 1, 2025 15:13
@AA-Turner
fixup! Apply configure changes
816b4af
@AA-Turner
fixup! Apply configure changes
d97b122
@AA-Turner
Remove internal/ prefix
04d9b4e
zooba
zooba reviewed Apr 1, 2025
View reviewed changes

@zooba zooba 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.

I like this approach, though would want someone else's signoff because I'm not as familiar with the Modules/Setup changes. Provided the module is always statically linked, it ought to be fine.

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.

@AA-Turner
Remove types.__all__
9c508ae
`from types import *` will ignore underscore-prefixed names by default, so defining `__all__` doesn't add anything.
@AA-Turner

AA-Turner commented Apr 1, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

From a build perspective I believe it should be configured correctly for static linking, I borrowed the configuration for _typing in large part. A good sign is that '_types' in sys.builtin_module_names is True.

zooba reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@pitrou

pitrou commented Apr 1, 2025

Copy link
Copy Markdown
Member

I like this approach, though would want someone else's signoff because I'm not as familiar with the Modules/Setup changes. Provided the module is always statically linked, it ought to be fine.

cc @gpshead for a quick look perhaps.

All reactions

Sorry, something went wrong.

Uh oh!

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

@AA-Turner
Reinstate __all__ for pydoc
b7f5df1
XuehaiPan
XuehaiPan reviewed Apr 3, 2025
View reviewed changes
Comment thread Lib/types.py Resolved

Uh oh!

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

@ZeroIntensity

ZeroIntensity commented Apr 3, 2025

Copy link
Copy Markdown
Member

@AA-Turner Do you mind if I/someone run the buildbots to make sure that the new module builds properly?

All reactions

Sorry, something went wrong.

Uh oh!

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

@AA-Turner AA-Turner added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 3, 2025
@bedevere-bot

bedevere-bot commented Apr 3, 2025

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @AA-Turner for commit b7f5df1 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F131969%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Apr 3, 2025
@AA-Turner

AA-Turner commented Apr 3, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

The ARM64 macOS failure is in test_idle, and the two ARM64 Windows failures also look unrelated (I think we have known problems on Windows/ARM at present?).

A

zooba and ZeroIntensity reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

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

ZeroIntensity
ZeroIntensity reviewed Apr 4, 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.

Should we add pure-Python fallbacks for when _types isn't available?

On the one hand, it's good for other implementations that want to provide the types module, but on the other hand, types is pretty CPython-specific anyway; it would probably need impl-specific changes, even if it's pure-Python.

LGTM otherwise.

Sorry, something went wrong.

Uh oh!

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

All reactions
@AA-Turner

AA-Turner commented Apr 4, 2025

Copy link
Copy Markdown
Member Author

Especially for CapsuleType it's not possible to have a pure-Python fallback. If other implementations don't want to alter types.py then they could e.g. provide the various names in a _types.py which would be picked up, but they will already have strategies for dealing with the general problem.

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 4, 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.

Sounds good. The implementation looks fine, and unlike most C accelerators, I think _types can actually simplify some of the code, because we don't have to use silly tricks to get the types at runtime.

Thanks for doing this!

Sorry, something went wrong.

Uh oh!

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

All reactions
picnixz
picnixz approved these changes Apr 4, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Apr 4, 2025
@AA-Turner
AA-Turner merged commit 231a50f into python:main Apr 4, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Apr 4, 2025
@AA-Turner
AA-Turner deleted the opt-types-capsule branch April 4, 2025 22:37
@AA-Turner AA-Turner mentioned this pull request Apr 5, 2025
GH-118761: Expose more core interpreter types in _types #132103
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@zooba zooba zooba left review comments
@picnixz picnixz picnixz approved these changes
@ZeroIntensity ZeroIntensity ZeroIntensity approved these changes
@pitrou pitrou Awaiting requested review from pitrou
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner
@corona10 corona10 Awaiting requested review from corona10 corona10 is a code owner
+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

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.

7 participants

@AA-Turner @pitrou @picnixz @ZeroIntensity @bedevere-bot @zooba @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.