Skip to content

Commit b927eb7

Browse files
committed
[GR-25521] Fix regression for c-magic-iter.
PullRequest: graalpython/1186
2 parents ec956ea + 002387b commit b927eb7

File tree

4 files changed

+59
-67
lines changed

4 files changed

+59
-67
lines changed

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,22 @@
5555
import com.oracle.graal.python.builtins.PythonBuiltins;
5656
import com.oracle.graal.python.builtins.objects.PNone;
5757
import com.oracle.graal.python.builtins.objects.dict.PDict;
58+
import com.oracle.graal.python.builtins.objects.function.PArguments;
59+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5860
import com.oracle.graal.python.nodes.ErrorMessages;
61+
import com.oracle.graal.python.nodes.PGuards;
5962
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
63+
import com.oracle.graal.python.nodes.util.CannotCastException;
64+
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
6065
import com.oracle.graal.python.runtime.exception.PythonErrorType;
6166
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6267
import com.oracle.truffle.api.TruffleOptions;
63-
import com.oracle.truffle.api.dsl.Fallback;
68+
import com.oracle.truffle.api.dsl.Cached;
6469
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6570
import com.oracle.truffle.api.dsl.NodeFactory;
6671
import com.oracle.truffle.api.dsl.Specialization;
72+
import com.oracle.truffle.api.frame.VirtualFrame;
73+
import com.oracle.truffle.api.library.CachedLibrary;
6774

6875
@CoreFunctions(defineModule = "_locale")
6976
public class LocaleModuleBuiltins extends PythonBuiltins {
@@ -222,9 +229,9 @@ public PDict localeconv() {
222229
public abstract static class SetLocaleNode extends PythonBuiltinNode {
223230

224231
@SuppressWarnings("fallthrough")
225-
@Specialization(guards = {"category >= 0", "category <= 6"})
232+
@Specialization(guards = "isValidCategory(category)")
226233
@TruffleBoundary
227-
public Object setLocale(int category, @SuppressWarnings("unused") PNone posixLocaleID) {
234+
Object doWithoutLocaleID(int category, @SuppressWarnings("unused") PNone posixLocaleID) {
228235
Locale defaultLocale;
229236
Locale.Category displayCategory = null;
230237
Locale.Category formatCategory = null;
@@ -258,10 +265,10 @@ public Object setLocale(int category, @SuppressWarnings("unused") PNone posixLoc
258265
return toPosix(defaultLocale);
259266
}
260267

261-
@SuppressWarnings("fallthrough")
262-
@Specialization(guards = {"category >= 0", "category <= 6"})
268+
@Specialization(guards = "isValidCategory(category)")
263269
@TruffleBoundary
264-
public Object setLocale(int category, String posixLocaleID) {
270+
@SuppressWarnings("fallthrough")
271+
Object doWithLocaleID(int category, String posixLocaleID) {
265272
Locale.Category displayCategory = null;
266273
Locale.Category formatCategory = null;
267274
if (!TruffleOptions.AOT) {
@@ -303,9 +310,34 @@ public Object setLocale(int category, String posixLocaleID) {
303310
return toPosix(newLocale);
304311
}
305312

306-
@Fallback
307-
public Object setLocale(@SuppressWarnings("unused") Object category, @SuppressWarnings("unused") Object locale) {
308-
throw raise(PythonErrorType.ValueError, ErrorMessages.INVALID_LOCALE_CATEGORY);
313+
@Specialization(replaces = {"doWithoutLocaleID", "doWithLocaleID"}, limit = "3")
314+
Object doGeneric(VirtualFrame frame, Object category, Object posixLocaleID,
315+
@CachedLibrary("category") PythonObjectLibrary categoryLib,
316+
@Cached CastToJavaStringNode castToJavaStringNode) {
317+
318+
long l = categoryLib.asJavaLongWithState(category, PArguments.getThreadState(frame));
319+
if (!isValidCategory(l)) {
320+
throw raise(PythonErrorType.ValueError, ErrorMessages.INVALID_LOCALE_CATEGORY);
321+
}
322+
323+
String posixLocaleIDStr = null;
324+
// may be NONE or NO_VALUE
325+
if (!PGuards.isPNone(posixLocaleID)) {
326+
try {
327+
posixLocaleIDStr = castToJavaStringNode.execute(posixLocaleID);
328+
} catch (CannotCastException e) {
329+
// fall through
330+
}
331+
}
332+
333+
if (posixLocaleIDStr != null) {
334+
return doWithLocaleID((int) l, posixLocaleIDStr);
335+
}
336+
return doWithoutLocaleID((int) l, PNone.NONE);
337+
}
338+
339+
static boolean isValidCategory(long l) {
340+
return 0 <= l && l <= 6;
309341
}
310342
}
311343
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CExtNodes.java

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
import com.oracle.truffle.api.dsl.GeneratedBy;
150150
import com.oracle.truffle.api.dsl.ImportStatic;
151151
import com.oracle.truffle.api.dsl.NodeFactory;
152+
import com.oracle.truffle.api.dsl.ReportPolymorphism;
152153
import com.oracle.truffle.api.dsl.Specialization;
153154
import com.oracle.truffle.api.dsl.TypeSystemReference;
154155
import com.oracle.truffle.api.frame.Frame;
@@ -317,6 +318,7 @@ public static FromNativeSubclassNode create() {
317318

318319
// -----------------------------------------------------------------------------------------------------------------
319320
@GenerateUncached
321+
@ReportPolymorphism
320322
@ImportStatic({PGuards.class, CApiGuards.class})
321323
public abstract static class ToSulongNode extends CExtToNativeNode {
322324

@@ -351,20 +353,11 @@ static PrimitiveNativeWrapper doIntegerSmall(@SuppressWarnings("unused") CExtCon
351353
return PrimitiveNativeWrapper.createInt(i);
352354
}
353355

354-
@Specialization(guards = "!isSmallInteger(i)", replaces = "doIntegerSmall")
356+
@Specialization(guards = "!isSmallInteger(i)")
355357
static PrimitiveNativeWrapper doInteger(@SuppressWarnings("unused") CExtContext cextContext, int i) {
356358
return PrimitiveNativeWrapper.createInt(i);
357359
}
358360

359-
@Specialization(replaces = {"doIntegerSmall", "doInteger"})
360-
static PrimitiveNativeWrapper doIntegerGeneric(@SuppressWarnings("unused") CExtContext cextContext, int i,
361-
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
362-
if (CApiGuards.isSmallInteger(i)) {
363-
return doIntegerSmall(cextContext, i, contextRef);
364-
}
365-
return PrimitiveNativeWrapper.createInt(i);
366-
}
367-
368361
@Specialization(guards = "isSmallLong(l)")
369362
static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContext cextContext, long l,
370363
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
@@ -375,20 +368,11 @@ static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContex
375368
return PrimitiveNativeWrapper.createLong(l);
376369
}
377370

378-
@Specialization(guards = "!isSmallLong(l)", replaces = "doLongSmall")
371+
@Specialization(guards = "!isSmallLong(l)")
379372
static PrimitiveNativeWrapper doLong(@SuppressWarnings("unused") CExtContext cextContext, long l) {
380373
return PrimitiveNativeWrapper.createLong(l);
381374
}
382375

383-
@Specialization(replaces = {"doLongSmall", "doLong"})
384-
static PrimitiveNativeWrapper doLongGeneric(@SuppressWarnings("unused") CExtContext cextContext, long l,
385-
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
386-
if (CApiGuards.isSmallLong(l)) {
387-
return doLongSmall(cextContext, l, contextRef);
388-
}
389-
return PrimitiveNativeWrapper.createLong(l);
390-
}
391-
392376
@Specialization(guards = "!isNaN(d)")
393377
static Object doDouble(@SuppressWarnings("unused") CExtContext cextContext, double d) {
394378
return PrimitiveNativeWrapper.createDouble(d);
@@ -548,6 +532,7 @@ public static ToSulongNode getUncached() {
548532
*/
549533
@GenerateUncached
550534
@ImportStatic({PGuards.class, CApiGuards.class})
535+
@ReportPolymorphism
551536
public abstract static class ToNewRefNode extends CExtToNativeNode {
552537

553538
public final Object executeInt(int i) {
@@ -597,20 +582,11 @@ static PrimitiveNativeWrapper doIntegerSmall(@SuppressWarnings("unused") CExtCon
597582
return PrimitiveNativeWrapper.createInt(i);
598583
}
599584

600-
@Specialization(guards = "!isSmallInteger(i)", replaces = "doIntegerSmall")
585+
@Specialization(guards = "!isSmallInteger(i)")
601586
static PrimitiveNativeWrapper doInteger(@SuppressWarnings("unused") CExtContext cextContext, int i) {
602587
return PrimitiveNativeWrapper.createInt(i);
603588
}
604589

605-
@Specialization(replaces = {"doIntegerSmall", "doInteger"})
606-
static PrimitiveNativeWrapper doIntegerGeneric(CExtContext cextContext, int i,
607-
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
608-
if (CApiGuards.isSmallInteger(i)) {
609-
return doIntegerSmall(cextContext, i, contextRef);
610-
}
611-
return PrimitiveNativeWrapper.createInt(i);
612-
}
613-
614590
@Specialization(guards = "isSmallLong(l)")
615591
static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContext cextContext, long l,
616592
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
@@ -623,20 +599,11 @@ static PrimitiveNativeWrapper doLongSmall(@SuppressWarnings("unused") CExtContex
623599
return PrimitiveNativeWrapper.createLong(l);
624600
}
625601

626-
@Specialization(guards = "!isSmallLong(l)", replaces = "doLongSmall")
602+
@Specialization(guards = "!isSmallLong(l)")
627603
static PrimitiveNativeWrapper doLong(@SuppressWarnings("unused") CExtContext cextContext, long l) {
628604
return PrimitiveNativeWrapper.createLong(l);
629605
}
630606

631-
@Specialization(replaces = {"doLongSmall", "doLong"})
632-
static PrimitiveNativeWrapper doLongGeneric(CExtContext cextContext, long l,
633-
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
634-
if (CApiGuards.isSmallLong(l)) {
635-
return doLongSmall(cextContext, l, contextRef);
636-
}
637-
return PrimitiveNativeWrapper.createLong(l);
638-
}
639-
640607
@Specialization(guards = "!isNaN(d)")
641608
static Object doDouble(CExtContext cextContext, double d) {
642609
return ToSulongNode.doDouble(cextContext, d);
@@ -811,34 +778,22 @@ static PrimitiveNativeWrapper doIntegerSmall(CExtContext cextContext, int i,
811778
return ToNewRefNode.doIntegerSmall(cextContext, i, contextRef);
812779
}
813780

814-
@Specialization(guards = "!isSmallInteger(i)", replaces = "doIntegerSmall")
781+
@Specialization(guards = "!isSmallInteger(i)")
815782
static PrimitiveNativeWrapper doInteger(CExtContext cextContext, int i) {
816783
return ToNewRefNode.doInteger(cextContext, i);
817784
}
818785

819-
@Specialization(replaces = {"doIntegerSmall", "doInteger"})
820-
static PrimitiveNativeWrapper doIntegerGeneric(CExtContext cextContext, int i,
821-
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
822-
return ToNewRefNode.doIntegerGeneric(cextContext, i, contextRef);
823-
}
824-
825786
@Specialization(guards = "isSmallLong(l)")
826787
static PrimitiveNativeWrapper doLongSmall(CExtContext cextContext, long l,
827788
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
828789
return ToNewRefNode.doLongSmall(cextContext, l, contextRef);
829790
}
830791

831-
@Specialization(guards = "!isSmallLong(l)", replaces = "doLongSmall")
792+
@Specialization(guards = "!isSmallLong(l)")
832793
static PrimitiveNativeWrapper doLong(@SuppressWarnings("unused") CExtContext cextContext, long l) {
833794
return ToNewRefNode.doLong(cextContext, l);
834795
}
835796

836-
@Specialization(replaces = {"doLongSmall", "doLong"})
837-
static PrimitiveNativeWrapper doLongGeneric(CExtContext cextContext, long l,
838-
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
839-
return ToNewRefNode.doLongGeneric(cextContext, l, contextRef);
840-
}
841-
842797
@Specialization(guards = "!isNaN(d)")
843798
static Object doDouble(CExtContext cextContext, double d) {
844799
return ToSulongNode.doDouble(cextContext, d);
@@ -958,12 +913,14 @@ static byte doByteNativeWrapper(@SuppressWarnings("unused") CExtContext cextCont
958913
}
959914

960915
@Specialization(guards = {"object.isInt()", "mayUsePrimitive(isPointerNode, object)"}, limit = "1")
961-
static int doIntNativeWrappe(@SuppressWarnings("unused") CExtContext cextContext, PrimitiveNativeWrapper object,
916+
static int doIntNativeWrapper(@SuppressWarnings("unused") CExtContext cextContext, PrimitiveNativeWrapper object,
962917
@Shared("isPointerNode") @Cached @SuppressWarnings("unused") IsPointerNode isPointerNode) {
963918
return object.getInt();
964919
}
965920

966-
@Specialization(guards = {"object.isLong()", "mayUsePrimitive(isPointerNode, object)"}, limit = "1")
921+
@Specialization(guards = {"object.isInt() || object.isLong()", "mayUsePrimitive(isPointerNode, object)"}, //
922+
limit = "1", //
923+
replaces = "doIntNativeWrapper")
967924
static long doLongNativeWrapper(@SuppressWarnings("unused") CExtContext cextContext, PrimitiveNativeWrapper object,
968925
@Shared("isPointerNode") @Cached @SuppressWarnings("unused") IsPointerNode isPointerNode) {
969926
return object.getLong();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PythonNativeWrapper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.oracle.truffle.api.interop.TruffleObject;
5151
import com.oracle.truffle.api.library.ExportLibrary;
5252
import com.oracle.truffle.api.library.ExportMessage;
53+
import com.oracle.truffle.api.profiles.ConditionProfile;
5354

5455
@ExportLibrary(PythonNativeWrapperLibrary.class)
5556
public abstract class PythonNativeWrapper implements TruffleObject {
@@ -186,8 +187,9 @@ protected static boolean isCachedNative(@SuppressWarnings("unused") PythonNative
186187
}
187188

188189
@Specialization(replaces = "isCachedNative")
189-
protected static boolean isNative(PythonNativeWrapper wrapper) {
190-
if (wrapper.nativePointer != null) {
190+
protected static boolean isNative(PythonNativeWrapper wrapper,
191+
@Cached ConditionProfile hasNativePointerProfile) {
192+
if (hasNativePointerProfile.profile(wrapper.nativePointer != null)) {
191193
Assumption handleValidAssumption = wrapper.getHandleValidAssumption();
192194
// If an assumption exists, it must be valid
193195
return handleValidAssumption == null || isValid(handleValidAssumption);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/frame/PFrame.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ public PFrame(@SuppressWarnings("unused") Object threadState, PCode code, Python
181181
this.location = code.getRootNode();
182182
this.inClassScope = code.getRootNode() instanceof ClassBodyRootNode;
183183
this.line = code.getRootNode() == null ? code.getFirstLineNo() : -2;
184+
this.arguments = frameArgs;
184185

185186
localsDict = locals;
186187
}

0 commit comments

Comments
 (0)