|
55 | 55 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
56 | 56 | import com.oracle.graal.python.builtins.objects.PNone;
|
57 | 57 | 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; |
58 | 60 | import com.oracle.graal.python.nodes.ErrorMessages;
|
| 61 | +import com.oracle.graal.python.nodes.PGuards; |
59 | 62 | 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; |
60 | 65 | import com.oracle.graal.python.runtime.exception.PythonErrorType;
|
61 | 66 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
62 | 67 | import com.oracle.truffle.api.TruffleOptions;
|
63 |
| -import com.oracle.truffle.api.dsl.Fallback; |
| 68 | +import com.oracle.truffle.api.dsl.Cached; |
64 | 69 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
65 | 70 | import com.oracle.truffle.api.dsl.NodeFactory;
|
66 | 71 | import com.oracle.truffle.api.dsl.Specialization;
|
| 72 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 73 | +import com.oracle.truffle.api.library.CachedLibrary; |
67 | 74 |
|
68 | 75 | @CoreFunctions(defineModule = "_locale")
|
69 | 76 | public class LocaleModuleBuiltins extends PythonBuiltins {
|
@@ -222,9 +229,9 @@ public PDict localeconv() {
|
222 | 229 | public abstract static class SetLocaleNode extends PythonBuiltinNode {
|
223 | 230 |
|
224 | 231 | @SuppressWarnings("fallthrough")
|
225 |
| - @Specialization(guards = {"category >= 0", "category <= 6"}) |
| 232 | + @Specialization(guards = "isValidCategory(category)") |
226 | 233 | @TruffleBoundary
|
227 |
| - public Object setLocale(int category, @SuppressWarnings("unused") PNone posixLocaleID) { |
| 234 | + Object doWithoutLocaleID(int category, @SuppressWarnings("unused") PNone posixLocaleID) { |
228 | 235 | Locale defaultLocale;
|
229 | 236 | Locale.Category displayCategory = null;
|
230 | 237 | Locale.Category formatCategory = null;
|
@@ -258,10 +265,10 @@ public Object setLocale(int category, @SuppressWarnings("unused") PNone posixLoc
|
258 | 265 | return toPosix(defaultLocale);
|
259 | 266 | }
|
260 | 267 |
|
261 |
| - @SuppressWarnings("fallthrough") |
262 |
| - @Specialization(guards = {"category >= 0", "category <= 6"}) |
| 268 | + @Specialization(guards = "isValidCategory(category)") |
263 | 269 | @TruffleBoundary
|
264 |
| - public Object setLocale(int category, String posixLocaleID) { |
| 270 | + @SuppressWarnings("fallthrough") |
| 271 | + Object doWithLocaleID(int category, String posixLocaleID) { |
265 | 272 | Locale.Category displayCategory = null;
|
266 | 273 | Locale.Category formatCategory = null;
|
267 | 274 | if (!TruffleOptions.AOT) {
|
@@ -303,9 +310,34 @@ public Object setLocale(int category, String posixLocaleID) {
|
303 | 310 | return toPosix(newLocale);
|
304 | 311 | }
|
305 | 312 |
|
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; |
309 | 341 | }
|
310 | 342 | }
|
311 | 343 | }
|
0 commit comments