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 — [mypyc] Fix vtable pointer with inherited dunder new by p-sawicki · Pull Request #20302 · python/mypy

Conversation

@p-sawicki

@p-sawicki p-sawicki commented Nov 26, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Collaborator

Fixes an issue where a subclass would have its vtable pointer set to the base class' vtable when there is a __new__ method defined in the base class. This resulted in the subclass constructor calling the setup function of the base class because mypyc transforms object.__new__ into the setup function.

The fix is to store the pointers to the setup functions in tp_methods of type objects and look them up dynamically when instantiating new objects.

Sorry, something went wrong.

Uh oh!

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

All reactions
@p-sawicki p-sawicki mentioned this pull request Nov 26, 2025
1.19 Release Planning #19964
Closed
@p-sawicki
Fix vtable pointer with inherited dunder new
3e8d126
@p-sawicki
p-sawicki force-pushed the fix-vtable-with-inherited-dunder-new branch from a0ae332 to 3e8d126 Compare November 26, 2025 21:10
@pre-commit-ci
[pre-commit.ci] auto fixes from pre-commit.com hooks
be8f0ff
for more information, see https://pre-commit.ci
ilevkivskyi
ilevkivskyi reviewed Nov 27, 2025
View reviewed changes

@ilevkivskyi ilevkivskyi 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 think this is a viable fix, but will leave up to @JukkaL to double-check.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread mypyc/lib-rt/generic_ops.c Outdated
continue;
}

while (def->ml_name && strcmp(def->ml_name, "__internal_mypyc_setup")) {

@ilevkivskyi ilevkivskyi Nov 27, 2025

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.

This search may be slow, we know that this method should be first if it is there, so maybe only check the first method, and if it has a different name, move to next base?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread mypyc/irbuild/specialize.py
if isinstance(typ_arg, NameExpr) and len(method_args) > 0 and method_args[0] == typ_arg.name:
subtype = builder.accept(expr.args[0])
return builder.add(Call(ir.setup, [subtype], expr.line))
return builder.call_c(setup_object, [subtype], expr.line)

@ilevkivskyi ilevkivskyi Nov 27, 2025

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.

Add a comment here explaining why a dynamic method dispatch is needed here.

Sorry, something went wrong.

Uh oh!

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

All reactions

@JukkaL JukkaL Nov 27, 2025

Copy link
Copy Markdown
Collaborator

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.

Can you add a fast path if class is known to not have subclasses -- we would still be able to use the old direct call, right?

Sorry, something went wrong.

Uh oh!

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

All reactions
@ilevkivskyi ilevkivskyi mentioned this pull request Nov 27, 2025
Various updates to 1.19 changelog #20304
Merged
@p-sawicki
Address review comments
98d7abb
JukkaL
JukkaL reviewed Nov 27, 2025
View reviewed changes
Comment thread mypyc/irbuild/specialize.py
if isinstance(typ_arg, NameExpr) and len(method_args) > 0 and method_args[0] == typ_arg.name:
subtype = builder.accept(expr.args[0])
return builder.add(Call(ir.setup, [subtype], expr.line))
return builder.call_c(setup_object, [subtype], expr.line)

@JukkaL JukkaL Nov 27, 2025

Copy link
Copy Markdown
Collaborator

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.

Can you add a fast path if class is known to not have subclasses -- we would still be able to use the old direct call, right?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread mypyc/lib-rt/generic_ops.c Outdated
}
}
if (!def || !def->ml_name) {
PyErr_SetString(PyExc_LookupError, "Internal error: Unable to find object setup function");

@JukkaL JukkaL Nov 27, 2025

Copy link
Copy Markdown
Collaborator

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.

Add something to the error message that indicates that this error is related to mypyc, and not CPython.

Sorry, something went wrong.

Uh oh!

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

All reactions
p-sawicki added 2 commits November 27, 2025 18:19
@p-sawicki
Address more review comments
499389f
@p-sawicki
Simplify CPy_SetupObject
54dc884
JukkaL
JukkaL reviewed Nov 27, 2025
View reviewed changes
Comment thread mypyc/irbuild/specialize.py Outdated
if isinstance(typ_arg, NameExpr) and len(method_args) > 0 and method_args[0] == typ_arg.name:
subtype = builder.accept(expr.args[0])
return builder.add(Call(ir.setup, [subtype], expr.line))
classes = all_concrete_classes(ir)

@JukkaL JukkaL Nov 27, 2025

Copy link
Copy Markdown
Collaborator

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.

ir.subclasses() seems like the correct way -- we need to use the slow path for an ABC that has only a single concrete subclass, I think?

Sorry, something went wrong.

Uh oh!

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

All reactions

@p-sawicki p-sawicki Nov 27, 2025

Copy link
Copy Markdown
Collaborator Author

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.

ok that makes sense. i didn't add a test for this because for now __new__ cannot be called in an abstract class anyway as it inherits from a python type.

Sorry, something went wrong.

Uh oh!

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

All reactions
@p-sawicki
Call subclasses() instead of all_concrete_classes()
2fd137e
JukkaL
JukkaL approved these changes Nov 27, 2025
View reviewed changes
@p-sawicki
p-sawicki merged commit 03b12a1 into python:master Nov 27, 2025
14 checks passed

Uh oh!

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

@p-sawicki
p-sawicki deleted the fix-vtable-with-inherited-dunder-new branch November 27, 2025 18:21
p-sawicki added a commit that referenced this pull request Nov 28, 2025
@p-sawicki @pre-commit-ci
[mypyc] Fix vtable pointer with inherited dunder new (#20302)
1b94fbb
Fixes an issue where a subclass would have its vtable pointer set to the
base class' vtable when there is a `__new__` method defined in the base
class. This resulted in the subclass constructor calling the setup
function of the base class because mypyc transforms `object.__new__`
into the setup function.

The fix is to store the pointers to the setup functions in `tp_methods`
of type objects and look them up dynamically when instantiating new
objects.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@RiyaPrabhakar-git RiyaPrabhakar-git added this to GC-Content-Calculator Dec 12, 2025
@github-project-automation github-project-automation Bot moved this from Todo to Done in GC-Content-Calculator Dec 12, 2025
@github-project-automation github-project-automation Bot moved this to Todo in GC-Content-Calculator Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ilevkivskyi ilevkivskyi ilevkivskyi left review comments
@JukkaL JukkaL JukkaL approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

3 participants

@p-sawicki @JukkaL @ilevkivskyi
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 01402520 — 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.