Skip to content
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
3 changes: 1 addition & 2 deletions graalpython/com.oracle.graal.python.cext/src/floatobject.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
/* Copyright (c) 2018, 2026, Oracle and/or its affiliates.
* Copyright (C) 1996-2017 Python Software Foundation
*
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
Expand Down Expand Up @@ -2689,7 +2689,6 @@ GraalPyPrivate_Float_SubtypeNew(PyTypeObject *type, double x)
{
PyObject* newobj = type->tp_alloc(type, 0);
if (newobj == NULL) {
Py_DECREF(newobj);
return NULL;
}
((PyFloatObject *)newobj)->ob_fval = x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -245,6 +245,25 @@ class ManagedSubclass(NativeFloatSubclass):
f = ManagedSubclass(1.0)
assert is_native_object(f)

def test_alloc_failure(self):
"""A native float subtype must propagate tp_alloc failure instead of crashing on NULL."""
FailingAllocFloatSubclass = CPyExtType(
'FailingAllocFloatSubclass',
r'''
static PyObject* fail_alloc(PyTypeObject *type, Py_ssize_t nitems) {
PyErr_NoMemory();
return NULL;
}
''',
struct_base='PyFloatObject base;',
tp_base='&PyFloat_Type',
tp_new='0',
tp_alloc='fail_alloc',
tp_free='0',
)
with self.assertRaises(MemoryError):
FailingAllocFloatSubclass(1.0)

def test_methods(self):
f = NativeFloatSubclass(1.1)
zero = NativeFloatSubclass(0.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,18 @@ public abstract static class FloatSubtypeNew extends Node {
static Object doGeneric(Object object, double arg,
@Bind Node inliningTarget,
@Cached PythonToNativeNode toNativeNode,
@Cached NativeToPythonTransferNode toJavaNode) {
@Cached NativeToPythonTransferNode toJavaNode,
@Cached PyObjectCheckFunctionResultNode checkFunctionResultNode) {
assert TypeNodes.NeedsNativeAllocationNode.executeUncached(object);
NativeFunctionPointer callable = CApiContext.getNativeSymbol(inliningTarget, NativeCAPISymbol.FUN_FLOAT_SUBTYPE_NEW);
long result;
try {
long result = ExternalFunctionInvoker.invokeFLOAT_SUBTYPE_NEW(callable.getAddress(), toNativeNode.executeLong(object), arg);
return toJavaNode.execute(result);
result = ExternalFunctionInvoker.invokeFLOAT_SUBTYPE_NEW(callable.getAddress(), toNativeNode.executeLong(object), arg);
} catch (Throwable e) {
throw CompilerDirectives.shouldNotReachHere(e);
}
return checkFunctionResultNode.execute(PythonContext.get(inliningTarget), NativeCAPISymbol.FUN_FLOAT_SUBTYPE_NEW.getTsName(),
toJavaNode.execute(result));
}
}

Expand Down
Loading