Skip to content

gh-162600: Don't specialize calls of abstract classes to CALL_ALLOC_AND_ENTER_INIT - #162883

Open
Robertboy18 wants to merge 1 commit into
python:mainfrom
Robertboy18:gh-162600-abstract-specialization
Open

Robertboy18 wants to merge 1 commit into
python:mainfrom
Robertboy18:gh-162600-abstract-specialization

Conversation

@Robertboy18

@Robertboy18 Robertboy18 commented Nov 30, 2026 •

Copy link
Copy Markdown

This fixes the case from #162600. A class with __abstractmethods__ set directly (without ABCMeta) raises TypeError on the first call, but once the call site specializes to CALL_ALLOC_AND_ENTER_INIT it gets instantiated, because that instruction allocates the object itself and never reaches the abstract check in object_new().

specialize_class_call now skips CALL_ALLOC_AND_ENTER_INIT for a class with Py_TPFLAGS_IS_ABSTRACT. The call site becomes CALL_NON_PY_GENERAL instead, which calls the class the normal way, so object_new() raises. The flag is read after the tp_version lookup, so a class that becomes abstract afterwards still invalidates the cached version. The check only runs when a call site specializes, so CALL_ALLOC_AND_ENTER_INIT itself doesn't change. On the issue, picnixz asked for numbers on a PGO+LTO build (plain classes, dataclasses, pydantic models); I haven't run those yet and will post them here.

The new test in test_opcache covers both orders: a class that's abstract before its call site specializes, and one made abstract afterwards, which then raises and isn't specialized to CALL_ALLOC_AND_ENTER_INIT again after the cooldown.

I used AI tools to help write and review the patch, and checked it myself.

Copilot AI balanced review requested due to automatic review settings November 30, 2026 17:40
@python-cla-bot

python-cla-bot Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

2 participants