Skip to content

Commit 2c927a9

Browse files
committed
[GR-20057] CoerceToDoubleNode, CoerceToJavaLongNode, CoerceToIntegerNode.
PullRequest: graalpython/971
2 parents 2c5c5e4 + e21acd9 commit 2c927a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1281
-939
lines changed

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7171
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7272
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
73-
import com.oracle.graal.python.nodes.util.CoerceToIntegerNode;
7473
import com.oracle.graal.python.runtime.PythonCore;
7574
import com.oracle.graal.python.runtime.exception.PException;
7675
import com.oracle.truffle.api.CompilerDirectives;
@@ -226,7 +225,6 @@ private ReadAttributeFromObjectNode getAttrNode() {
226225
abstract static class B2aBase64Node extends PythonBinaryBuiltinNode {
227226

228227
@Child private SequenceStorageNodes.ToByteArrayNode toByteArray;
229-
@Child private CoerceToIntegerNode castToIntNode;
230228
@Child private B2aBase64Node recursiveNode;
231229

232230
private SequenceStorageNodes.ToByteArrayNode getToByteArrayNode() {
@@ -237,16 +235,6 @@ private SequenceStorageNodes.ToByteArrayNode getToByteArrayNode() {
237235
return toByteArray;
238236
}
239237

240-
private CoerceToIntegerNode getCastToIntNode() {
241-
if (castToIntNode == null) {
242-
CompilerDirectives.transferToInterpreterAndInvalidate();
243-
castToIntNode = insert(CoerceToIntegerNode.create(val -> {
244-
throw raise(PythonBuiltinClassType.TypeError, "an integer is required (got type %p)", val);
245-
}));
246-
}
247-
return castToIntNode;
248-
}
249-
250238
private B2aBase64Node getRecursiveNode() {
251239
if (recursiveNode == null) {
252240
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -279,14 +267,16 @@ PBytes b2aBytesLike(PIBytesLike data, PInt newline) {
279267
return b2a(getToByteArrayNode().execute(data.getSequenceStorage()), !newline.isZero());
280268
}
281269

282-
@Specialization
283-
PBytes b2aBytesLike(VirtualFrame frame, PIBytesLike data, Object newline) {
284-
return (PBytes) getRecursiveNode().execute(frame, data, getCastToIntNode().execute(newline));
270+
@Specialization(limit = "1")
271+
PBytes b2aBytesLike(VirtualFrame frame, PIBytesLike data, Object newline,
272+
@CachedLibrary("newline") PythonObjectLibrary lib) {
273+
return (PBytes) getRecursiveNode().execute(frame, data, asPInt(newline, lib));
285274
}
286275

287276
@Specialization(guards = "isNoValue(newline)")
288-
PBytes b2aArray(VirtualFrame frame, PArray data, @SuppressWarnings("unused") PNone newline) {
289-
return b2aArray(frame, data, 1);
277+
PBytes b2aArray(VirtualFrame frame, PArray data, @SuppressWarnings("unused") PNone newline,
278+
@CachedLibrary(limit = "1") PythonObjectLibrary lib) {
279+
return b2aArray(frame, data, 1, lib);
290280
}
291281

292282
@Specialization
@@ -299,9 +289,10 @@ PBytes b2aArray(PArray data, PInt newline) {
299289
return b2a(getToByteArrayNode().execute(data.getSequenceStorage()), !newline.isZero());
300290
}
301291

302-
@Specialization
303-
PBytes b2aArray(VirtualFrame frame, PArray data, Object newline) {
304-
return (PBytes) getRecursiveNode().execute(frame, data, getCastToIntNode().execute(newline));
292+
@Specialization(limit = "1")
293+
PBytes b2aArray(VirtualFrame frame, PArray data, Object newline,
294+
@CachedLibrary("newline") PythonObjectLibrary lib) {
295+
return (PBytes) getRecursiveNode().execute(frame, data, asPInt(newline, lib));
305296
}
306297

307298
@Specialization(guards = "isNoValue(newline)")
@@ -329,15 +320,23 @@ PBytes b2aMmeory(VirtualFrame frame, PMemoryView data, PInt newline,
329320
return b2aMemory(frame, data, newline.isZero() ? 0 : 1, toBytesNode, isBytesProfile);
330321
}
331322

332-
@Specialization
333-
PBytes b2aMmeory(VirtualFrame frame, PMemoryView data, Object newline) {
334-
return (PBytes) getRecursiveNode().execute(frame, data, getCastToIntNode().execute(newline));
323+
@Specialization(limit = "1")
324+
PBytes b2aMmeory(VirtualFrame frame, PMemoryView data, Object newline,
325+
@CachedLibrary("newline") PythonObjectLibrary lib) {
326+
return (PBytes) getRecursiveNode().execute(frame, data, asPInt(newline, lib));
335327
}
336328

337329
@Fallback
338330
PBytes b2sGeneral(Object data, @SuppressWarnings("unused") Object newline) {
339331
throw raise(PythonBuiltinClassType.TypeError, "a bytes-like object is required, not '%p'", data);
340332
}
333+
334+
private Object asPInt(Object obj, PythonObjectLibrary lib) {
335+
if (lib.canBePInt(obj)) {
336+
return lib.asPInt(obj);
337+
}
338+
throw raise(PythonBuiltinClassType.TypeError, "an integer is required (got type %p)", obj);
339+
}
341340
}
342341

343342
@Builtin(name = "b2a_hex", minNumOfPositionalArgs = 1)

0 commit comments

Comments
 (0)