Skip to content

Commit 2ef8c34

Browse files
msimacektimfel
authored andcommitted
Fix argument handling in int.to_bytes
1 parent bc9e409 commit 2ef8c34

File tree

2 files changed

+22
-149
lines changed

2 files changed

+22
-149
lines changed

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import static com.oracle.graal.python.nodes.StringLiterals.T_EXT_PYD;
5252
import static com.oracle.graal.python.nodes.StringLiterals.T_EXT_SO;
5353
import static com.oracle.graal.python.nodes.StringLiterals.T_HPY_SUFFIX;
54-
import static com.oracle.graal.python.nodes.StringLiterals.T_LITTLE;
5554
import static com.oracle.graal.python.nodes.StringLiterals.T_NAME;
5655
import static com.oracle.graal.python.nodes.StringLiterals.T_SITE;
5756
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ImportError;
@@ -92,7 +91,6 @@
9291
import com.oracle.graal.python.builtins.objects.code.CodeNodes;
9392
import com.oracle.graal.python.builtins.objects.code.PCode;
9493
import com.oracle.graal.python.builtins.objects.function.PArguments;
95-
import com.oracle.graal.python.builtins.objects.ints.IntBuiltins;
9694
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
9795
import com.oracle.graal.python.builtins.objects.module.FrozenModules;
9896
import com.oracle.graal.python.builtins.objects.module.PythonFrozenModule;
@@ -128,12 +126,12 @@
128126
import com.oracle.truffle.api.dsl.Cached;
129127
import com.oracle.truffle.api.dsl.Fallback;
130128
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
131-
import com.oracle.truffle.api.dsl.NeverDefault;
132129
import com.oracle.truffle.api.dsl.NodeFactory;
133130
import com.oracle.truffle.api.dsl.Specialization;
134131
import com.oracle.truffle.api.frame.VirtualFrame;
135132
import com.oracle.truffle.api.interop.InteropLibrary;
136133
import com.oracle.truffle.api.library.CachedLibrary;
134+
import com.oracle.truffle.api.memory.ByteArraySupport;
137135
import com.oracle.truffle.api.nodes.Node;
138136
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
139137
import com.oracle.truffle.api.strings.TruffleString;
@@ -254,31 +252,26 @@ public boolean run() {
254252
@GenerateNodeFactory
255253
public abstract static class GetMagic extends PythonBuiltinNode {
256254
static final int MAGIC_NUMBER = 21000 + Compiler.BYTECODE_VERSION * 10;
257-
258-
@Child private IntBuiltins.ToBytesNode toBytesNode = IntBuiltins.ToBytesNode.create();
259-
@Child private PythonBufferAccessLibrary bufferLib = PythonBufferAccessLibrary.getFactory().createDispatched(1);
255+
static final byte[] MAGIC_NUMBER_BYTES = new byte[4];
256+
static {
257+
ByteArraySupport.littleEndian().putInt(MAGIC_NUMBER_BYTES, 0, MAGIC_NUMBER);
258+
MAGIC_NUMBER_BYTES[2] = '\r';
259+
MAGIC_NUMBER_BYTES[3] = '\n';
260+
}
260261

261262
@Specialization(guards = "isSingleContext()")
262-
public PBytes runCachedSingleContext(@SuppressWarnings("unused") VirtualFrame frame,
263-
@Cached(value = "getMagicNumberPBytes(frame)", weak = true) PBytes magicBytes) {
263+
public PBytes runCachedSingleContext(
264+
@Cached(value = "getMagicNumberPBytes()", weak = true) PBytes magicBytes) {
264265
return magicBytes;
265266
}
266267

267268
@Specialization(replaces = "runCachedSingleContext")
268-
public PBytes run(@SuppressWarnings("unused") VirtualFrame frame,
269-
@Cached(value = "getMagicNumberBytes(frame)", dimensions = 1) byte[] magicBytes) {
270-
return factory().createBytes(magicBytes);
271-
}
272-
273-
protected PBytes getMagicNumberPBytes(VirtualFrame frame) {
274-
return factory().createBytes(getMagicNumberBytes(frame));
269+
public PBytes run() {
270+
return factory().createBytes(MAGIC_NUMBER_BYTES);
275271
}
276272

277-
@NeverDefault
278-
protected byte[] getMagicNumberBytes(VirtualFrame frame) {
279-
PBytes magic = toBytesNode.execute(frame, MAGIC_NUMBER, 2, T_LITTLE, false);
280-
byte[] magicBytes = bufferLib.getInternalOrCopiedByteArray(magic);
281-
return new byte[]{magicBytes[0], magicBytes[1], '\r', '\n'};
273+
protected PBytes getMagicNumberPBytes() {
274+
return factory().createBytes(MAGIC_NUMBER_BYTES);
282275
}
283276
}
284277

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

Lines changed: 9 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@
120120
import com.oracle.graal.python.builtins.objects.ints.IntBuiltinsClinicProviders.FormatNodeClinicProviderGen;
121121
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
122122
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
123-
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
124123
import com.oracle.graal.python.lib.PyNumberFloatNode;
125124
import com.oracle.graal.python.lib.PyObjectHashNode;
126125
import com.oracle.graal.python.nodes.ErrorMessages;
127-
import com.oracle.graal.python.nodes.PGuards;
128126
import com.oracle.graal.python.nodes.PRaiseNode;
129127
import com.oracle.graal.python.nodes.SpecialMethodNames;
130128
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
@@ -2395,17 +2393,14 @@ static PNotImplemented doGeneric(Object a, Object b) {
23952393
}
23962394

23972395
// to_bytes
2398-
@Builtin(name = "to_bytes", minNumOfPositionalArgs = 3, parameterNames = {"self", "bytecount", "byteorder", "signed"})
2396+
@Builtin(name = "to_bytes", minNumOfPositionalArgs = 3, parameterNames = {"$self", "length", "byteorder"}, keywordOnlyNames = {"signed"})
2397+
@ArgumentClinic(name = "length", conversion = ClinicConversion.Index)
2398+
@ArgumentClinic(name = "byteorder", conversion = ClinicConversion.TString)
2399+
@ArgumentClinic(name = "signed", conversion = ClinicConversion.Boolean, defaultValue = "false")
23992400
@GenerateNodeFactory
24002401
@SuppressWarnings("unused")
24012402
@TypeSystemReference(PythonArithmeticTypes.class)
2402-
public abstract static class ToBytesNode extends PythonBuiltinNode {
2403-
2404-
public abstract PBytes execute(VirtualFrame frame, Object self, Object byteCount, Object StringOrder, Object signed);
2405-
2406-
// used for obtaining int, which will be the size of created array
2407-
@Child private ToBytesNode recursiveNode;
2408-
@Child private PyNumberAsSizeNode asSizeNode;
2403+
public abstract static class ToBytesNode extends PythonClinicBuiltinNode {
24092404

24102405
@TruffleBoundary
24112406
private boolean isBigEndian(TruffleString order) {
@@ -2418,20 +2413,11 @@ private boolean isBigEndian(TruffleString order) {
24182413
throw raise(PythonErrorType.ValueError, ErrorMessages.BYTEORDER_MUST_BE_LITTLE_OR_BIG);
24192414
}
24202415

2421-
@Specialization
2422-
PBytes fromLong(long self, int byteCount, TruffleString byteorder, PNone signed,
2423-
@Bind("this") Node inliningTarget,
2424-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2425-
@Shared @Cached InlinedConditionProfile negativeNumberProfile,
2426-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2427-
return fromLong(self, byteCount, byteorder, false, inliningTarget, negativeByteCountProfile, negativeNumberProfile, overflowProfile);
2428-
}
2429-
24302416
@Specialization
24312417
PBytes fromLong(long self, int byteCount, TruffleString byteorder, boolean signed,
24322418
@Bind("this") Node inliningTarget,
24332419
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2434-
@Shared @Cached InlinedConditionProfile negativeNumberProfile,
2420+
@Cached InlinedConditionProfile negativeNumberProfile,
24352421
@Shared @Cached InlinedConditionProfile overflowProfile) {
24362422
if (negativeByteCountProfile.profile(inliningTarget, byteCount < 0)) {
24372423
throw raise(PythonErrorType.ValueError, ErrorMessages.MESSAGE_LENGTH_ARGUMENT);
@@ -2489,52 +2475,6 @@ public static byte[] fromLong(long self, int byteCount, boolean isBigEndian, boo
24892475
return bytes;
24902476
}
24912477

2492-
@Specialization
2493-
PBytes fromLongLong(VirtualFrame frame, long self, long byteCount, TruffleString byteorder, PNone signed,
2494-
@Bind("this") Node inliningTarget,
2495-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2496-
@Shared @Cached InlinedConditionProfile negativeNumberProfile,
2497-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2498-
return fromLongLong(frame, self, byteCount, byteorder, false, inliningTarget, negativeByteCountProfile, negativeNumberProfile, overflowProfile);
2499-
}
2500-
2501-
@Specialization
2502-
PBytes fromLongLong(VirtualFrame frame, long self, long byteCount, TruffleString byteorder, boolean signed,
2503-
@Bind("this") Node inliningTarget,
2504-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2505-
@Shared @Cached InlinedConditionProfile negativeNumberProfile,
2506-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2507-
int count = asSize(frame, byteCount);
2508-
return fromLong(self, count, byteorder, signed, inliningTarget, negativeByteCountProfile, negativeNumberProfile, overflowProfile);
2509-
}
2510-
2511-
@Specialization
2512-
PBytes fromLongPInt(VirtualFrame frame, long self, PInt byteCount, TruffleString byteorder, PNone signed,
2513-
@Bind("this") Node inliningTarget,
2514-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2515-
@Shared @Cached InlinedConditionProfile negativeNumberProfile,
2516-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2517-
return fromLongPInt(frame, self, byteCount, byteorder, false, inliningTarget, negativeByteCountProfile, negativeNumberProfile, overflowProfile);
2518-
}
2519-
2520-
@Specialization
2521-
PBytes fromLongPInt(VirtualFrame frame, long self, PInt byteCount, TruffleString byteorder, boolean signed,
2522-
@Bind("this") Node inliningTarget,
2523-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2524-
@Shared @Cached InlinedConditionProfile negativeNumberProfile,
2525-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2526-
int count = asSize(frame, byteCount);
2527-
return fromLong(self, count, byteorder, signed, inliningTarget, negativeByteCountProfile, negativeNumberProfile, overflowProfile);
2528-
}
2529-
2530-
@Specialization
2531-
PBytes fromPIntInt(PInt self, int byteCount, TruffleString byteorder, PNone signed,
2532-
@Bind("this") Node inliningTarget,
2533-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2534-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2535-
return fromPIntInt(self, byteCount, byteorder, false, inliningTarget, negativeByteCountProfile, overflowProfile);
2536-
}
2537-
25382478
@TruffleBoundary
25392479
private static byte getSingByte(BigInteger value, boolean signed,
25402480
PRaiseNode raise) {
@@ -2625,69 +2565,9 @@ public static byte[] fromBigInteger(PInt self, int byteCount, boolean isBigEndia
26252565
}
26262566
}
26272567

2628-
@Specialization
2629-
PBytes fromPIntLong(VirtualFrame frame, PInt self, long byteCount, TruffleString byteorder, PNone signed,
2630-
@Bind("this") Node inliningTarget,
2631-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2632-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2633-
return fromPIntLong(frame, self, byteCount, byteorder, false, inliningTarget, negativeByteCountProfile, overflowProfile);
2634-
}
2635-
2636-
@Specialization
2637-
PBytes fromPIntLong(VirtualFrame frame, PInt self, long byteCount, TruffleString byteorder, boolean signed,
2638-
@Bind("this") Node inliningTarget,
2639-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2640-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2641-
int count = asSize(frame, byteCount);
2642-
return fromPIntInt(self, count, byteorder, signed, inliningTarget, negativeByteCountProfile, overflowProfile);
2643-
}
2644-
2645-
@Specialization
2646-
PBytes fromPIntPInt(VirtualFrame frame, PInt self, PInt byteCount, TruffleString byteorder, PNone signed,
2647-
@Bind("this") Node inliningTarget,
2648-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2649-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2650-
return fromPIntPInt(frame, self, byteCount, byteorder, false, inliningTarget, negativeByteCountProfile, overflowProfile);
2651-
}
2652-
2653-
@Specialization
2654-
PBytes fromPIntPInt(VirtualFrame frame, PInt self, PInt byteCount, TruffleString byteorder, boolean signed,
2655-
@Bind("this") Node inliningTarget,
2656-
@Shared @Cached InlinedConditionProfile negativeByteCountProfile,
2657-
@Shared @Cached InlinedConditionProfile overflowProfile) {
2658-
int count = asSize(frame, byteCount);
2659-
return fromPIntInt(self, count, byteorder, signed, inliningTarget, negativeByteCountProfile, overflowProfile);
2660-
}
2661-
2662-
static boolean isNumber(Object value) {
2663-
return value instanceof Integer || value instanceof Long || value instanceof PInt;
2664-
}
2665-
2666-
@Fallback
2667-
PBytes general(VirtualFrame frame, Object self, Object byteCount, Object byteorder, Object oSigned) {
2668-
int count = asSize(frame, byteCount);
2669-
if (!PGuards.isString(byteorder)) {
2670-
throw raise(PythonErrorType.TypeError, ErrorMessages.ARG_D_MUST_BE_S_NOT_P, "to_bytes()", 2, "str", byteorder);
2671-
}
2672-
boolean signed = oSigned instanceof Boolean && (boolean) oSigned;
2673-
if (recursiveNode == null) {
2674-
CompilerDirectives.transferToInterpreterAndInvalidate();
2675-
recursiveNode = insert(create());
2676-
}
2677-
return recursiveNode.execute(frame, self, count, byteorder, signed);
2678-
}
2679-
2680-
private int asSize(VirtualFrame frame, Object object) {
2681-
if (asSizeNode == null) {
2682-
CompilerDirectives.transferToInterpreterAndInvalidate();
2683-
asSizeNode = insert(PyNumberAsSizeNode.create());
2684-
}
2685-
return asSizeNode.executeExact(frame, object);
2686-
}
2687-
2688-
@NeverDefault
2689-
public static ToBytesNode create() {
2690-
return IntBuiltinsFactory.ToBytesNodeFactory.create(null);
2568+
@Override
2569+
protected ArgumentClinicProvider getArgumentClinic() {
2570+
return IntBuiltinsClinicProviders.ToBytesNodeClinicProviderGen.INSTANCE;
26912571
}
26922572
}
26932573

0 commit comments

Comments
 (0)