Skip to content

Commit 7e3c3f7

Browse files
committed
[GR-21590] Update imports
PullRequest: graalpython/3443
2 parents f753cf7 + 2581f8f commit 7e3c3f7

File tree

7 files changed

+144
-29
lines changed

7 files changed

+144
-29
lines changed

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "overlay": "1ff9147f4afb583004082346d424996122690425" }
1+
{ "overlay": "e0a9866e03c0a3d7e14140ed9c7c86a7d09caa3f" }

graalpython/com.oracle.graal.python.cext/src/tupleobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ PyObject **
13611361
PyTruffleTuple_GetItems(PyObject *op)
13621362
{
13631363
#ifdef GRAALVM_PYTHON_LLVM_MANAGED
1364-
return GraalPy_get_PyTupleObject_ob_item(op);
1364+
return GraalPy_get_PyTupleObject_ob_item((PyTupleObject*) op);
13651365
#else /* GRAALVM_PYTHON_LLVM_MANAGED */
13661366
PyObject *res;
13671367
PyObject **ob_item;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/foreign/ForeignObjectBuiltins.java

Lines changed: 122 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
8080

8181
import java.math.BigInteger;
82-
import java.util.Arrays;
8382
import java.util.List;
8483

8584
import com.oracle.graal.python.PythonLanguage;
@@ -105,6 +104,7 @@
105104
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotLen.LenBuiltinNode;
106105
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotSetAttr.SetAttrBuiltinNode;
107106
import com.oracle.graal.python.builtins.objects.type.slots.TpSlotSizeArgFun.SqItemBuiltinNode;
107+
import com.oracle.graal.python.lib.PyNumberAddNode;
108108
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
109109
import com.oracle.graal.python.lib.PyObjectReprAsTruffleStringNode;
110110
import com.oracle.graal.python.lib.PyObjectRichCompareBool;
@@ -279,6 +279,8 @@ static int len(Object self,
279279
}
280280
}
281281

282+
// TODO: remove once all dunder methods are converted to slots and to
283+
// NormalizeForeignForBinopNode
282284
abstract static class ForeignBinaryNode extends BinaryOpBuiltinNode {
283285
@Child private BinaryOpNode op;
284286
protected final boolean reverse;
@@ -408,36 +410,134 @@ public static PNotImplemented doGeneric(Object left, Object right) {
408410
}
409411
}
410412

411-
@Slot(value = SlotKind.nb_add, isComplex = true)
412-
@GenerateNodeFactory
413-
abstract static class AddNode extends ForeignBinaryNode {
414-
AddNode() {
415-
super(BinaryArithmetic.Add.create(), false);
413+
@GenerateInline
414+
@GenerateCached(false)
415+
abstract static class NormalizeForeignForBinopNode extends Node {
416+
public abstract Object execute(Node inliningTarget, Object value, boolean doArray);
417+
418+
@Specialization(guards = {"lib.isBoolean(obj)"})
419+
Object doBool(Object obj, @SuppressWarnings("unused") boolean doArray,
420+
@Shared @CachedLibrary(limit = "3") InteropLibrary lib,
421+
@Shared @Cached(inline = false) GilNode gil) {
422+
gil.release(true);
423+
try {
424+
return lib.asBoolean(obj);
425+
} catch (UnsupportedMessageException e) {
426+
throw CompilerDirectives.shouldNotReachHere(e);
427+
} finally {
428+
gil.acquire();
429+
}
416430
}
417431

418-
AddNode(boolean reverse) {
419-
super(BinaryArithmetic.Add.create(), reverse);
432+
@Specialization(guards = "lib.fitsInLong(obj)")
433+
Object doLong(Object obj, @SuppressWarnings("unused") boolean doArray,
434+
@Shared @CachedLibrary(limit = "3") InteropLibrary lib,
435+
@Shared @Cached(inline = false) GilNode gil) {
436+
assert !lib.isBoolean(obj);
437+
gil.release(true);
438+
try {
439+
return lib.asLong(obj);
440+
} catch (UnsupportedMessageException e) {
441+
throw CompilerDirectives.shouldNotReachHere(e);
442+
} finally {
443+
gil.acquire();
444+
}
420445
}
421446

422-
@Specialization(insertBefore = "doGeneric", guards = {"lib.hasArrayElements(left)", "lib.hasArrayElements(right)"})
423-
static Object doForeignArray(Object left, Object right,
424-
@Cached PythonObjectFactory factory,
425-
@CachedLibrary(limit = "3") InteropLibrary lib,
426-
@Cached PForeignToPTypeNode convert,
427-
@Cached GilNode gil) {
447+
@Specialization(guards = {"!lib.fitsInLong(obj)", "lib.fitsInBigInteger(obj)"})
448+
Object doBigInt(Object obj, @SuppressWarnings("unused") boolean doArray,
449+
@Shared @CachedLibrary(limit = "3") InteropLibrary lib,
450+
@Shared @Cached(inline = false) GilNode gil,
451+
@Shared @Cached(inline = false) PythonObjectFactory factory) {
452+
assert !lib.isBoolean(obj);
428453
gil.release(true);
429454
try {
430-
Object[] unpackedLeft = unpackForeignArray(left, lib, convert);
431-
Object[] unpackedRight = unpackForeignArray(right, lib, convert);
432-
if (unpackedLeft != null && unpackedRight != null) {
433-
Object[] result = Arrays.copyOf(unpackedLeft, unpackedLeft.length + unpackedRight.length);
434-
System.arraycopy(unpackedRight, 0, result, unpackedLeft.length, unpackedRight.length);
435-
return factory.createList(result);
436-
}
455+
return factory.createInt(lib.asBigInteger(obj));
456+
} catch (UnsupportedMessageException e) {
457+
throw CompilerDirectives.shouldNotReachHere(e);
437458
} finally {
438459
gil.acquire();
439460
}
440-
return PNotImplemented.NOT_IMPLEMENTED;
461+
}
462+
463+
@Specialization(guards = {"!lib.fitsInLong(obj)", "!lib.fitsInBigInteger(obj)", "lib.fitsInDouble(obj)"})
464+
Object doDouble(Object obj, @SuppressWarnings("unused") boolean doArray,
465+
@Shared @CachedLibrary(limit = "3") InteropLibrary lib,
466+
@Shared @Cached(inline = false) GilNode gil) {
467+
assert !lib.isBoolean(obj);
468+
gil.release(true);
469+
try {
470+
return lib.asDouble(obj);
471+
} catch (UnsupportedMessageException e) {
472+
throw CompilerDirectives.shouldNotReachHere(e);
473+
} finally {
474+
gil.acquire();
475+
}
476+
}
477+
478+
@Specialization(guards = {"!lib.fitsInLong(obj)", "!lib.fitsInBigInteger(obj)", "!lib.fitsInDouble(obj)", "lib.isString(obj)"})
479+
Object doString(Object obj, @SuppressWarnings("unused") boolean doArray,
480+
@Shared @CachedLibrary(limit = "3") InteropLibrary lib,
481+
@Cached(inline = false) TruffleString.SwitchEncodingNode switchEncodingNode,
482+
@Shared @Cached(inline = false) GilNode gil) {
483+
assert !lib.isBoolean(obj);
484+
gil.release(true);
485+
try {
486+
return switchEncodingNode.execute(lib.asTruffleString(obj), TS_ENCODING);
487+
} catch (UnsupportedMessageException e) {
488+
throw CompilerDirectives.shouldNotReachHere(e);
489+
} finally {
490+
gil.acquire();
491+
}
492+
}
493+
494+
@Specialization(guards = {"doArray", "!lib.isBoolean(obj)", "!lib.fitsInLong(obj)", "!lib.fitsInBigInteger(obj)", //
495+
"!lib.fitsInDouble(obj)", "!lib.isString(obj)", "lib.hasArrayElements(obj)"})
496+
Object doArray(Object obj, @SuppressWarnings("unused") boolean doArray,
497+
@Shared @CachedLibrary(limit = "3") InteropLibrary lib,
498+
@Shared @Cached(inline = false) GilNode gil,
499+
@Cached(inline = false) PForeignToPTypeNode convert,
500+
@Shared @Cached(inline = false) PythonObjectFactory factory) {
501+
gil.release(true);
502+
try {
503+
return factory.createTuple(unpackForeignArray(obj, lib, convert));
504+
} finally {
505+
gil.acquire();
506+
}
507+
}
508+
509+
@Fallback
510+
@SuppressWarnings("unused")
511+
public static Object doGeneric(Object left, boolean doArray) {
512+
return null;
513+
}
514+
}
515+
516+
@Slot(value = SlotKind.nb_add, isComplex = true)
517+
@GenerateNodeFactory
518+
abstract static class AddNode extends BinaryOpBuiltinNode {
519+
@Specialization
520+
static Object doIt(VirtualFrame frame, Object left, Object right,
521+
@Bind("this") Node inliningTarget,
522+
@Cached IsForeignObjectNode isForeignLeft,
523+
@Cached IsForeignObjectNode isForeignRight,
524+
@Cached NormalizeForeignForBinopNode normalizeLeft,
525+
@Cached NormalizeForeignForBinopNode normalizeRight,
526+
@Cached PyNumberAddNode addNode) {
527+
boolean leftIsForeign = isForeignLeft.execute(inliningTarget, left);
528+
boolean rightIsForeign = isForeignRight.execute(inliningTarget, right);
529+
if (!leftIsForeign && !rightIsForeign) {
530+
return PNotImplemented.NOT_IMPLEMENTED;
531+
}
532+
533+
Object newLeft = normalizeLeft.execute(inliningTarget, left, true);
534+
Object newRight = normalizeRight.execute(inliningTarget, right, true);
535+
assert newLeft == null || !IsForeignObjectNode.executeUncached(newLeft) : newLeft;
536+
assert newRight == null || !IsForeignObjectNode.executeUncached(newRight) : newRight;
537+
if (newLeft == null || newRight == null) {
538+
return PNotImplemented.NOT_IMPLEMENTED;
539+
}
540+
return addNode.execute(frame, inliningTarget, newLeft, newRight);
441541
}
442542
}
443543

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,14 @@ static BigInteger op(BigInteger left, BigInteger right) {
454454
return left.add(right);
455455
}
456456

457+
static boolean isNotImplemented(Object x) {
458+
return !(x instanceof Long || x instanceof Integer || x instanceof Boolean || x instanceof PInt);
459+
}
460+
461+
// There is a Truffle bug (GR-57305) that constructs a wrong fallback guard in the presence
462+
// of implicit casts, so we cannot use @Fallback for now
457463
@SuppressWarnings("unused")
458-
@Fallback
464+
@Specialization(guards = {"isNotImplemented(left) || isNotImplemented(right)"})
459465
static PNotImplemented doGeneric(Object left, Object right) {
460466
return PNotImplemented.NOT_IMPLEMENTED;
461467
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ public Object execute(VirtualFrame frame, Object self, Object other) {
178178
}
179179
}
180180

181+
/**
182+
* Slots representing "reversible" binary operations on the Python side do not have two
183+
* versions, instead they should be able to handle the situation when "left" operand is not the
184+
* object that "owns" the slot. See also {@link com.oracle.graal.python.lib.CallBinaryOp1Node}.
185+
*/
181186
@GenerateInline(value = false, inherit = true)
182187
public abstract static class BinaryOpBuiltinNode extends PythonBinaryBuiltinNode {
183188
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/CallBinaryOp1Node.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ static Object doIt(VirtualFrame frame, Node inliningTarget, Object v, Object cla
105105
slotW = null;
106106
}
107107
}
108+
// Note: we call slotW with v as the receiver. This appears to be the semantics of
109+
// CPython reversible binop slots. This is supposed to allow the slot to handle
110+
// the reversible case, if the slot does not want to handle it, it should detect that
111+
// the first receiver argument is not of the right type and just return NotImplemented.
108112
if (slotV != null) {
109113
if (slotW != null && isSubtypeNode.execute(frame, classW, classV)) {
110114
assert !sameTypes;

mx.graalpython/suite.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,31 @@
4545
},
4646
{
4747
"name": "sdk",
48-
"version": "2d0500c2cfb47b733d539f180bfdace11dac595e",
48+
"version": "a596f1b0a9bd97dabcb314fa0500e6d4547ead1d",
4949
"subdir": True,
5050
"urls": [
5151
{"url": "https://github.com/oracle/graal", "kind": "git"},
5252
]
5353
},
5454
{
5555
"name": "tools",
56-
"version": "2d0500c2cfb47b733d539f180bfdace11dac595e",
56+
"version": "a596f1b0a9bd97dabcb314fa0500e6d4547ead1d",
5757
"subdir": True,
5858
"urls": [
5959
{"url": "https://github.com/oracle/graal", "kind": "git"},
6060
],
6161
},
6262
{
6363
"name": "sulong",
64-
"version": "2d0500c2cfb47b733d539f180bfdace11dac595e",
64+
"version": "a596f1b0a9bd97dabcb314fa0500e6d4547ead1d",
6565
"subdir": True,
6666
"urls": [
6767
{"url": "https://github.com/oracle/graal", "kind": "git"},
6868
]
6969
},
7070
{
7171
"name": "regex",
72-
"version": "2d0500c2cfb47b733d539f180bfdace11dac595e",
72+
"version": "a596f1b0a9bd97dabcb314fa0500e6d4547ead1d",
7373
"subdir": True,
7474
"urls": [
7575
{"url": "https://github.com/oracle/graal", "kind": "git"},

0 commit comments

Comments
 (0)