Skip to content

Commit 2cb20be

Browse files
committed
[GR-53369] Fixes for optuna
PullRequest: graalpython/3310
2 parents 0bae7f7 + eb02ed8 commit 2cb20be

File tree

8 files changed

+676
-71
lines changed

8 files changed

+676
-71
lines changed

graalpython/com.oracle.graal.python.test.integration/src/com/oracle/graal/python/test/integration/basic/AttributeTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -143,9 +143,11 @@ public void delMagicAttribute() {
143143

144144
@Test
145145
public void complexHasDoc() {
146-
PythonTests.assertPrints("complex(real[, imag]) -> complex number\n\n" +
147-
"Create a complex number from a real part and an optional imaginary part.\n" +
148-
"This is equivalent to (real + imag*1j) where imag defaults to 0.\n",
146+
PythonTests.assertPrints("""
147+
Create a complex number from a real part and an optional imaginary part.
148+
149+
This is equivalent to (real + imag*1j) where imag defaults to 0.
150+
""",
149151
"print(complex.__doc__)");
150152
}
151153
}

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_tp_slots.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939
import sys
40-
from . import CPyExtType, CPyExtHeapType
40+
from . import CPyExtType, CPyExtHeapType, compile_module_from_string
4141

4242
SlotsGetter = CPyExtType("SlotsGetter",
4343
"""
@@ -245,3 +245,43 @@ class MyClassWithDescr2:
245245
# assert SlotsGetter.get_tp_descr_get(HackySlotsWithManaged()) == SlotsGetter.get_tp_attro(ManagedAttrO())
246246
finally:
247247
sys.modules.pop("test_incompatible_slots_assignment_managed", None)
248+
249+
250+
def test_type_not_ready():
251+
module = compile_module_from_string("""
252+
#define PY_SSIZE_T_CLEAN
253+
#include <Python.h>
254+
255+
static PyObject* my_getattro(PyObject* self, PyObject* key) {
256+
return Py_NewRef(key);
257+
}
258+
259+
static PyTypeObject NotReadyType = {
260+
PyVarObject_HEAD_INIT(NULL, 0)
261+
.tp_name = "NotReadyType",
262+
.tp_basicsize = sizeof(PyObject),
263+
.tp_dealloc = (destructor)PyObject_Del,
264+
.tp_getattro = my_getattro
265+
};
266+
267+
static PyObject* create_not_ready(PyObject* module, PyObject* unused) {
268+
return PyObject_New(PyObject, &NotReadyType);
269+
}
270+
271+
static PyMethodDef module_methods[] = {
272+
{"create_not_ready", _PyCFunction_CAST(create_not_ready), METH_NOARGS, ""},
273+
{NULL}
274+
};
275+
276+
static PyModuleDef NotReadyTypeModule = {
277+
PyModuleDef_HEAD_INIT, "NotReadyType", "", -1, module_methods
278+
};
279+
280+
PyMODINIT_FUNC
281+
PyInit_NotReadyType(void)
282+
{
283+
return PyModule_Create(&NotReadyTypeModule);
284+
}
285+
""", "NotReadyType")
286+
not_ready = module.create_not_ready()
287+
assert not_ready.foo == 'foo'

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 238 additions & 56 deletions
Large diffs are not rendered by default.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ Object doNonWrapper(long pointer, boolean stealing,
15601560
Object ref = lookup.get();
15611561
if (createNativeProfile.profile(inliningTarget, ref == null)) {
15621562
LOGGER.fine(() -> "re-creating collected PythonAbstractNativeObject reference" + Long.toHexString(pointer));
1563-
return createAbstractNativeObject(nativeContext, pointer, stealing, pointer);
1563+
return createAbstractNativeObject(nativeContext, new NativePointer(pointer), stealing, pointer);
15641564
}
15651565
if (isNativeWrapperProfile.profile(inliningTarget, ref instanceof PythonNativeWrapper)) {
15661566
wrapper = (PythonNativeWrapper) ref;
@@ -1572,7 +1572,7 @@ Object doNonWrapper(long pointer, boolean stealing,
15721572
return result;
15731573
}
15741574
} else {
1575-
return createAbstractNativeObject(nativeContext, pointer, stealing, pointer);
1575+
return createAbstractNativeObject(nativeContext, new NativePointer(pointer), stealing, pointer);
15761576
}
15771577
}
15781578
return NativeToPythonNode.handleWrapper(inliningTarget, wrapperProfile, stealing, wrapper);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/ListBuiltins.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import static com.oracle.graal.python.nodes.BuiltinNames.J_APPEND;
2929
import static com.oracle.graal.python.nodes.BuiltinNames.J_EXTEND;
30-
import static com.oracle.graal.python.nodes.SpecialAttributeNames.T___DOC__;
3130
import static com.oracle.graal.python.nodes.SpecialMethodNames.J_SORT;
3231
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___ADD__;
3332
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___CLASS_GETITEM__;
@@ -146,11 +145,6 @@ public final class ListBuiltins extends PythonBuiltins {
146145
@Override
147146
public void initialize(Python3Core core) {
148147
super.initialize(core);
149-
addBuiltinConstant(T___DOC__, //
150-
"Built-in mutable sequence.\n" + //
151-
"\n" + //
152-
"If no argument is given, the constructor creates a new empty list.\n" + //
153-
"The argument must be an iterable if specified.");
154148
this.addBuiltinConstant(T___HASH__, PNone.NONE);
155149
}
156150

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TpSlots.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
104104
import com.oracle.graal.python.util.InlineWeakValueProfile;
105105
import com.oracle.truffle.api.CompilerAsserts;
106+
import com.oracle.truffle.api.CompilerDirectives;
106107
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
107108
import com.oracle.truffle.api.TruffleLogger;
108109
import com.oracle.truffle.api.dsl.Cached;
@@ -115,6 +116,7 @@
115116
import com.oracle.truffle.api.interop.InteropLibrary;
116117
import com.oracle.truffle.api.interop.UnsupportedMessageException;
117118
import com.oracle.truffle.api.nodes.Node;
119+
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
118120
import com.oracle.truffle.api.strings.TruffleString;
119121

120122
/**
@@ -954,8 +956,27 @@ static TpSlots doManaged(PythonManagedClass klass) {
954956
}
955957

956958
@Specialization
957-
static TpSlots doNative(PythonAbstractNativeObject nativeKlass) {
958-
return nativeKlass.getTpSlots();
959+
static TpSlots doNative(Node inliningTarget, PythonAbstractNativeObject nativeKlass,
960+
@Cached InlinedBranchProfile slotsNotInitializedProfile) {
961+
TpSlots tpSlots = nativeKlass.getTpSlots();
962+
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, tpSlots == null)) {
963+
/*
964+
* This happens when we try to get slots of a type that didn't go through
965+
* PyType_Ready yet. Specifically, numpy has a "fortran" type (defined in
966+
* `fortranobject.c`) that they never ready and just expect it to work because it's
967+
* simple. So just do the minimum to make the slots available.
968+
*/
969+
slotsNotInitializedProfile.enter(inliningTarget);
970+
tpSlots = initializeNativeSlots(nativeKlass);
971+
}
972+
return tpSlots;
973+
}
974+
975+
@TruffleBoundary
976+
private static TpSlots initializeNativeSlots(PythonAbstractNativeObject nativeKlass) {
977+
TpSlots tpSlots = TpSlots.fromNative(nativeKlass, PythonContext.get(null));
978+
nativeKlass.setTpSlots(tpSlots);
979+
return tpSlots;
959980
}
960981
}
961982

0 commit comments

Comments
 (0)