Skip to content

gh-151912: Fix segfault in type() with NULL tp_new metaclasses#151916

Open
Santhosh-I wants to merge 4 commits into
python:mainfrom
Santhosh-I:main
Open

gh-151912: Fix segfault in type() with NULL tp_new metaclasses#151916
Santhosh-I wants to merge 4 commits into
python:mainfrom
Santhosh-I:main

Conversation

@Santhosh-I

Copy link
Copy Markdown

Summary

Fix a crash in type(...) when the provided metaclass has tp_new == NULL.

Currently, type(name, bases, namespace) can reach a code path that calls:

type = winner->tp_new(winner, ctx->args, ctx->kwds);

without verifying that winner->tp_new is non-NULL. For metaclasses that are valid base classes at the C level but cannot be instantiated from Python (tp_new == NULL), this results in a segmentation fault.

This change adds validation to reject such metaclasses and raise a Python exception instead.

Bug

For example, on NumPy main:

type("name", metaclass_instance, {})

where metaclass_instance == np.dtype, can eventually dereference a NULL tp_new pointer.

The expected behavior is consistent with:

type(metaclass_instance)()

which raises:

TypeError: cannot create 'numpy._DTypeMeta' instances

Fix

  • Check whether the selected metaclass (winner) has a valid tp_new.
  • Raise TypeError when tp_new == NULL instead of attempting to call it.
  • Prevent a NULL function pointer dereference and resulting segfault.

Issue

Closes gh-151912. #151912

@Santhosh-I Santhosh-I requested a review from markshannon as a code owner June 22, 2026 10:06
Copilot AI review requested due to automatic review settings June 22, 2026 10:06
@bedevere-app

bedevere-app Bot commented Jun 22, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Jun 22, 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.

Pull request overview

Fixes a crash in CPython’s type(name, bases, namespace) code path when metaclass resolution selects a metaclass whose tp_new slot is NULL (i.e., not instantiable), by raising a TypeError instead of calling through a NULL function pointer.

Changes:

  • Add a winner->tp_new == NULL guard before delegating to a non-type_new metaclass constructor.
  • Raise TypeError: cannot create '<metaclass>' instances rather than segfaulting.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Objects/typeobject.c
Comment on lines +5036 to +5038
PyErr_Format(PyExc_TypeError,
"cannot create '%.400s' instances",
winner->tp_name);
Comment thread Objects/typeobject.c
Comment on lines +5034 to 5043
/* Check if tp_new is NULL (cannot instantiate this type) */
if (winner->tp_new == NULL) {
PyErr_Format(PyExc_TypeError,
"cannot create '%.400s' instances",
winner->tp_name);
return -1;
}
/* Pass it to the winner */
*type = winner->tp_new(winner, ctx->args, ctx->kwds);
if (*type == NULL) {
@StanFromIreland StanFromIreland changed the title Fix segfault in type() with NULL tp_new metaclasses gh-151912: Fix segfault in type() with NULL tp_new metaclasses Jun 22, 2026
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.

MetaClass tp_new == NULL needs to be rejected in Python for type(...)

2 participants