Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use disallow instantiation flag for classes without the __new__ method #4568

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pytests/tests/test_pyclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,16 @@ def __new__(cls):


@pytest.mark.parametrize(
"cls", [pyclasses.ClassWithoutConstructor, ClassWithoutConstructor]
"cls, exc_message",
[
(
pyclasses.ClassWithoutConstructor,
"cannot create 'builtins.ClassWithoutConstructor' instances",
),
(ClassWithoutConstructor, "No constructor defined for ClassWithoutConstructor"),
Copy link
Member

Choose a reason for hiding this comment

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

This type is defined just above; we can change the message to match the expected Rust one to simplify the test. (Then there's just the differences across versions.)

],
)
def test_no_constructor_defined_propagates_cause(cls: Type):
def test_no_constructor_defined_propagates_cause(cls: Type, exc_message: str):
original_error = ValueError("Original message")
with pytest.raises(Exception) as exc_info:
try:
Expand All @@ -82,9 +89,7 @@ def test_no_constructor_defined_propagates_cause(cls: Type):
cls() # should raise TypeError("No constructor defined for ...")

assert exc_info.type is TypeError
assert exc_info.value.args == (
"No constructor defined for ClassWithoutConstructor",
)
assert exc_info.value.args == (exc_message,)
Copy link
Member

Choose a reason for hiding this comment

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

This test will be run on all of Python 3.7 through 3.13, so I think the parameterized test is a good start but this will need to account for the variety between versions too.

assert exc_info.value.__context__ is original_error


Expand Down
12 changes: 10 additions & 2 deletions src/pyclass/create_type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,15 @@ impl PyTypeBuilder {
unsafe { self.push_slot(ffi::Py_tp_base, self.tp_base) }

if !self.has_new {
// Safety: This is the correct slot type for Py_tp_new
unsafe { self.push_slot(ffi::Py_tp_new, no_constructor_defined as *mut c_void) }
#[cfg(not(Py_3_10))]
{
// Safety: This is the correct slot type for Py_tp_new
unsafe { self.push_slot(ffi::Py_tp_new, no_constructor_defined as *mut c_void) }
}
#[cfg(Py_3_10)]
{
self.class_flags |= ffi::Py_TPFLAGS_DISALLOW_INSTANTIATION;
}
}

let tp_dealloc = if self.has_traverse || unsafe { ffi::PyType_IS_GC(self.tp_base) == 1 } {
Expand Down Expand Up @@ -523,6 +530,7 @@ fn bpo_45315_workaround(py: Python<'_>, class_name: CString) {
}

/// Default new implementation
#[cfg(not(Py_3_10))]
unsafe extern "C" fn no_constructor_defined(
subtype: *mut ffi::PyTypeObject,
_args: *mut ffi::PyObject,
Expand Down
Loading