Skip to content

Commit bbdb412

Browse files
committed
bunch of smaller python error related fixes in TimeModuleBuiltins
1 parent f05db68 commit bbdb412

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import com.oracle.truffle.api.TruffleOptions;
7878
import com.oracle.truffle.api.dsl.Cached;
7979
import com.oracle.truffle.api.dsl.Cached.Shared;
80+
import com.oracle.truffle.api.dsl.Fallback;
8081
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
8182
import com.oracle.truffle.api.dsl.NodeFactory;
8283
import com.oracle.truffle.api.dsl.Specialization;
@@ -521,8 +522,8 @@ protected static int[] checkStructtime(PTuple time,
521522
CastToJavaIntExactNode toJavaIntExact,
522523
PRaiseNode raise) {
523524
Object[] otime = getInternalObjectArrayNode.execute(time.getSequenceStorage());
524-
if (lenNode.execute(time.getSequenceStorage()) < 9) {
525-
throw raise.raise(TypeError, ErrorMessages.FUNC_TAKES_AT_LEAST_D_ARGS, 9, otime.length);
525+
if (lenNode.execute(time.getSequenceStorage()) != 9) {
526+
throw raise.raise(TypeError, ErrorMessages.S_ILLEGAL_TIME_TUPLE_ARG, "asctime()");
526527
}
527528
int[] date = new int[9];
528529
for (int i = 0; i < 9; i++) {
@@ -531,7 +532,7 @@ protected static int[] checkStructtime(PTuple time,
531532

532533
// This is specific to java
533534
if (date[TM_YEAR] < Year.MIN_VALUE || date[TM_YEAR] > Year.MAX_VALUE) {
534-
throw raise.raise(ValueError, "year out of range");
535+
throw raise.raise(OverflowError, "year out of range");
535536
}
536537

537538
if (date[TM_MON] == 0) {
@@ -817,6 +818,9 @@ private static String format(String format, int[] date) {
817818

818819
@Specialization
819820
public String formatTime(String format, @SuppressWarnings("unused") PNone time) {
821+
if (format.indexOf(0) > -1) {
822+
throw raise(PythonBuiltinClassType.ValueError, ErrorMessages.EMBEDDED_NULL_CHARACTER);
823+
}
820824
return format(format, getIntLocalTimeStruct((long) timeSeconds()));
821825
}
822826

@@ -826,6 +830,9 @@ public String formatTime(String format, PTuple time,
826830
@Cached SequenceStorageNodes.LenNode lenNode,
827831
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
828832
@Cached CastToJavaIntExactNode castToInt) {
833+
if (format.indexOf(0) > -1) {
834+
throw raise(PythonBuiltinClassType.ValueError, ErrorMessages.EMBEDDED_NULL_CHARACTER);
835+
}
829836
int[] date = checkStructtime(time, getArray, lenNode, lib, castToInt, getRaiseNode());
830837
return format(format, date);
831838
}
@@ -921,6 +928,11 @@ public String localtime(PTuple time,
921928
return format(StrfTimeNode.checkStructtime(time, getArray, lenNode, asPIntLib, toJavaIntExact, getRaiseNode()));
922929
}
923930

931+
@Fallback
932+
public Object localtime(@SuppressWarnings("unused") Object time) {
933+
throw raise(TypeError, ErrorMessages.TUPLE_OR_STRUCT_TIME_ARG_REQUIRED);
934+
}
935+
924936
protected static String format(int[] tm) {
925937
return format(CTIME_FORMAT, tm);
926938
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ public abstract class ErrorMessages {
285285
public static final String ILLEGAL_EXPRESSION_FOR_AUGMENTED_ASSIGNEMNT = "illegal expression for augmented assignment";
286286
public static final String ILLEGAL_IP_STRING_PASSED_TO = "illegal IP address string passed to %s";
287287
public static final String ILLEGAL_SOCKET_ADDR_ARG = "%s: illegal sockaddr argument";
288+
public static final String S_ILLEGAL_TIME_TUPLE_ARG = "%s: illegal time tuple argument";
288289
public static final String IMPORT_STAR_ONLY_ALLOWED_AT_MODULE_LEVEL = "import * only allowed at module level";
289290
public static final String INCOMPLETE_FORMAT = "incomplete format";
290291
public static final String INDEX_NOT_INT = "%s: index not int";
@@ -507,6 +508,7 @@ public abstract class ErrorMessages {
507508
public static final String RETURNED_NULL_WO_SETTING_ERROR = "%s returned NULL without setting an error";
508509
public static final String RETURNED_RESULT_WITH_ERROR_SET = "%s returned a result with an error set";
509510
public static final String RETURNED_UNEXPECTE_RET_CODE_EXPECTED_INT_BUT_WAS_S = "%s returned an unexpected return code; expected 'int' but was %s";
511+
public static final String EMBEDDED_NULL_CHARACTER = "embedded null character";
510512
public static final String S_EMBEDDED_NULL_CHARACTER_IN_S = "%sembedded null character in %s";
511513
public static final String S_MUST_BE_S = "%s must be %s";
512514
public static final String S_NOT_SUPPORTED = "%s not supported";

0 commit comments

Comments
 (0)