diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2020-02-29 21:42:18 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2020-02-29 21:42:18 (GMT) |
commit | 520e0c9938ef5229a94dd4d50005f141db548e69 (patch) | |
tree | 8d9204fce9d09230622a141a301e1ef225fb8784 | |
parent | 4cca9ff9e3ec22ef897c6160c3520c6b7ba58271 (diff) | |
download | tcl-520e0c9938ef5229a94dd4d50005f141db548e69.zip tcl-520e0c9938ef5229a94dd4d50005f141db548e69.tar.gz tcl-520e0c9938ef5229a94dd4d50005f141db548e69.tar.bz2 |
Better error-message in case of "index out of range", mentioning the actual index which was out of range. Also, use the same error-code, because the underlying cause is all the same.
-rw-r--r-- | generic/tclCmdIL.c | 8 | ||||
-rw-r--r-- | generic/tclListObj.c | 25 | ||||
-rw-r--r-- | tests/cmdIL.test | 14 | ||||
-rw-r--r-- | tests/lpop.test | 8 | ||||
-rw-r--r-- | tests/lsearch.test | 10 | ||||
-rw-r--r-- | tests/lset.test | 40 | ||||
-rw-r--r-- | tests/lsetComp.test | 4 |
7 files changed, 54 insertions, 55 deletions
diff --git a/generic/tclCmdIL.c b/generic/tclCmdIL.c index 07b41ac..08948ea 100644 --- a/generic/tclCmdIL.c +++ b/generic/tclCmdIL.c @@ -3421,8 +3421,8 @@ Tcl_LsearchObjCmd( } if (encoded == (int)TCL_INDEX_NONE) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "index \"%s\" cannot select an element " - "from any list", TclGetString(indices[j]))); + "index \"%s\" out of range", + TclGetString(indices[j]))); Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX" "OUTOFRANGE", NULL); result = TCL_ERROR; @@ -4142,8 +4142,8 @@ Tcl_LsortObjCmd( if ((result == TCL_OK) && (encoded == (int)TCL_INDEX_NONE)) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "index \"%s\" cannot select an element " - "from any list", TclGetString(indexv[j]))); + "index \"%s\" out of range", + TclGetString(indexv[j]))); Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX" "OUTOFRANGE", NULL); result = TCL_ERROR; diff --git a/generic/tclListObj.c b/generic/tclListObj.c index bae6452..5de2eac 100644 --- a/generic/tclListObj.c +++ b/generic/tclListObj.c @@ -1608,11 +1608,10 @@ TclLsetFlat( || (valuePtr == NULL && index >= elemCount)) { /* ...the index points outside the sublist. */ if (interp != NULL) { - Tcl_SetObjResult(interp, - Tcl_NewStringObj("list index out of range", -1)); - Tcl_SetErrorCode(interp, "TCL", "OPERATION", - valuePtr == NULL ? "LPOP" : "LSET", - "BADINDEX", NULL); + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "index \"%s\" out of range", Tcl_GetString(indexArray[-1]))); + Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX" + "OUTOFRANGE", NULL); } result = TCL_ERROR; break; @@ -1803,10 +1802,10 @@ TclListObjSetElement( (void) Tcl_GetStringFromObj(listPtr, &length); if (length == 0) { if (interp != NULL) { - Tcl_SetObjResult(interp, - Tcl_NewStringObj("list index out of range", -1)); - Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSET", - "BADINDEX", NULL); + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "index \"%d\" out of range", index)); + Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX", + "OUTOFRANGE", NULL); } return TCL_ERROR; } @@ -1825,10 +1824,10 @@ TclListObjSetElement( if (index<0 || index>=elemCount) { if (interp != NULL) { - Tcl_SetObjResult(interp, - Tcl_NewStringObj("list index out of range", -1)); - Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSET", "BADINDEX", - NULL); + Tcl_SetObjResult(interp, Tcl_ObjPrintf( + "index \"%d\" out of range", index)); + Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX", + "OUTOFRANGE", NULL); } return TCL_ERROR; } diff --git a/tests/cmdIL.test b/tests/cmdIL.test index 05b5040..fe72d94 100644 --- a/tests/cmdIL.test +++ b/tests/cmdIL.test @@ -163,10 +163,10 @@ test cmdIL-1.40 {Tcl_LsortObjCmd procedure, Bug 8e1e31eac0fd6b6c} { } [list \0 \x7f \x80 \uffff \U01ffff] test cmdIL-1.41 {lsort -stride and -index} -body { lsort -stride 2 -index -2 {a 2 b 1} -} -returnCodes error -result {index "-2" cannot select an element from any list} +} -returnCodes error -result {index "-2" out of range} test cmdIL-1.42 {lsort -stride and-index} -body { lsort -stride 2 -index -1-1 {a 2 b 1} -} -returnCodes error -result {index "-1-1" cannot select an element from any list} +} -returnCodes error -result {index "-1-1" out of range} # Can't think of any good tests for the MergeSort and MergeLists procedures, # except a bunch of random lists to sort. @@ -228,10 +228,10 @@ test cmdIL-3.5.1 {SortCompare procedure, -index option (out of range, calculated } -returnCodes error -result {element 4 missing from sublist "1 . c"} test cmdIL-3.5.2 {SortCompare procedure, -index option (out of range, calculated index)} -body { lsort -index -1-1 {{1 . c} {2 . b} {3 . a}} -} -returnCodes error -result {index "-1-1" cannot select an element from any list} +} -returnCodes error -result {index "-1-1" out of range} test cmdIL-3.5.3 {SortCompare procedure, -index option (out of range, calculated index)} -body { lsort -index -2 {{1 . c} {2 . b} {3 . a}} -} -returnCodes error -result {index "-2" cannot select an element from any list} +} -returnCodes error -result {index "-2" out of range} test cmdIL-3.5.4 {SortCompare procedure, -index option (out of range, calculated index)} -body { lsort -index end-4 {{1 . c} {2 . b} {3 . a}} } -returnCodes error -result {element end-4 missing from sublist "1 . c"} @@ -243,13 +243,13 @@ test cmdIL-3.5.6 {SortCompare procedure, -index option} { } {a \{} test cmdIL-3.5.7 {SortCompare procedure, -index option (out of range, calculated index)} -body { lsort -index end--1 {{1 . c} {2 . b} {3 . a}} -} -returnCodes error -result {index "end--1" cannot select an element from any list} +} -returnCodes error -result {index "end--1" out of range} test cmdIL-3.5.8 {SortCompare procedure, -index option (out of range, calculated index)} -body { lsort -index end+1 {{1 . c} {2 . b} {3 . a}} -} -returnCodes error -result {index "end+1" cannot select an element from any list} +} -returnCodes error -result {index "end+1" out of range} test cmdIL-3.5.9 {SortCompare procedure, -index option (out of range, calculated index)} -body { lsort -index end+2 {{1 . c} {2 . b} {3 . a}} -} -returnCodes error -result {index "end+2" cannot select an element from any list} +} -returnCodes error -result {index "end+2" out of range} test cmdIL-3.5.10 {SortCompare procedure, -index option (out of range, calculated index)} -body { lsort -index 0 {{}} } -returnCodes error -result {element 0 missing from sublist ""} diff --git a/tests/lpop.test b/tests/lpop.test index 089299b..89b651c 100644 --- a/tests/lpop.test +++ b/tests/lpop.test @@ -29,15 +29,15 @@ test lpop-1.3 {error conditions} -returnCodes error -body { test lpop-1.4 {error conditions} -returnCodes error -body { set no "x y" lpop no -1 -} -result {list index out of range} +} -result {index "-1" out of range} test lpop-1.5 {error conditions} -returnCodes error -body { set no "x y z" lpop no 3 -} -result {list index out of range} ;#-errorCode {TCL OPERATION LPOP BADINDEX} +} -result {index "3" out of range} ;#-errorCode {TCL OPERATION LPOP BADINDEX} test lpop-1.6 {error conditions} -returnCodes error -body { set no "x y" lpop no end+1 -} -result {list index out of range} +} -result {index "end+1" out of range} test lpop-1.7 {error conditions} -returnCodes error -body { set no "x y" lpop no {} @@ -45,7 +45,7 @@ test lpop-1.7 {error conditions} -returnCodes error -body { test lpop-1.8 {error conditions} -returnCodes error -body { set no "x y" lpop no 0 0 0 0 1 -} -result {list index out of range} +} -result {index "1" out of range} test lpop-1.9 {error conditions} -returnCodes error -body { set no "x y" lpop no {1 0} diff --git a/tests/lsearch.test b/tests/lsearch.test index e401581..b188924 100644 --- a/tests/lsearch.test +++ b/tests/lsearch.test @@ -432,19 +432,19 @@ test lsearch-17.11 {lsearch -index option, empty argument} { } [lsearch [list \{] \{] test lsearch-17.12 {lsearch -index option, encoding aliasing} -body { lsearch -index -2 a a -} -returnCodes error -result {index "-2" cannot select an element from any list} +} -returnCodes error -result {index "-2" out of range} test lsearch-17.13 {lsearch -index option, encoding aliasing} -body { lsearch -index -1-1 a a -} -returnCodes error -result {index "-1-1" cannot select an element from any list} +} -returnCodes error -result {index "-1-1" out of range} test lsearch-17.14 {lsearch -index option, encoding aliasing} -body { lsearch -index end--1 a a -} -returnCodes error -result {index "end--1" cannot select an element from any list} +} -returnCodes error -result {index "end--1" out of range} test lsearch-17.15 {lsearch -index option, encoding aliasing} -body { lsearch -index end+1 a a -} -returnCodes error -result {index "end+1" cannot select an element from any list} +} -returnCodes error -result {index "end+1" out of range} test lsearch-17.16 {lsearch -index option, encoding aliasing} -body { lsearch -index end+2 a a -} -returnCodes error -result {index "end+2" cannot select an element from any list} +} -returnCodes error -result {index "end+2" out of range} test lsearch-18.1 {lsearch -index option, list as index basic functionality} { diff --git a/tests/lset.test b/tests/lset.test index 1c1300b..b1ed110 100644 --- a/tests/lset.test +++ b/tests/lset.test @@ -97,31 +97,31 @@ test lset-4.3 {lset, not compiled, 3 args, index out of range} testevalex { list [catch { testevalex {lset a [list -1] w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "-1" out of range}} test lset-4.4 {lset, not compiled, 3 args, index out of range} testevalex { set a {x y z} list [catch { testevalex {lset a [list 4] w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "4" out of range}} test lset-4.5a {lset, not compiled, 3 args, index out of range} testevalex { set a {x y z} list [catch { testevalex {lset a [list end--2] w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "end--2" out of range}} test lset-4.5b {lset, not compiled, 3 args, index out of range} testevalex { set a {x y z} list [catch { testevalex {lset a [list end+2] w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "end+2" out of range}} test lset-4.6 {lset, not compiled, 3 args, index out of range} testevalex { set a {x y z} list [catch { testevalex {lset a [list end-3] w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "end-3" out of range}} test lset-4.7 {lset, not compiled, 3 args, not a list} testevalex { set a "x \{" list [catch { @@ -139,31 +139,31 @@ test lset-4.9 {lset, not compiled, 3 args, index out of range} testevalex { list [catch { testevalex {lset a -1 w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "-1" out of range}} test lset-4.10 {lset, not compiled, 3 args, index out of range} testevalex { set a {x y z} list [catch { testevalex {lset a 4 w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "4" out of range}} test lset-4.11a {lset, not compiled, 3 args, index out of range} testevalex { set a {x y z} list [catch { testevalex {lset a end--2 w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "end--2" out of range}} test lset-4.11 {lset, not compiled, 3 args, index out of range} testevalex { set a {x y z} list [catch { testevalex {lset a end+2 w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "end+2" out of range}} test lset-4.12 {lset, not compiled, 3 args, index out of range} testevalex { set a {x y z} list [catch { testevalex {lset a end-3 w} } msg] $msg -} {1 {list index out of range}} +} {1 {index "end-3" out of range}} test lset-5.1 {lset, not compiled, 3 args, can't set variable} testevalex { list [catch { @@ -281,43 +281,43 @@ test lset-8.4 {lset, not compiled, bad second index} testevalex { test lset-8.5 {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a 2 -1 h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "-1" out of range}} test lset-8.6 {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a {2 -1} h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "-1" out of range}} test lset-8.7 {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a 2 3 h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "3" out of range}} test lset-8.8 {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a {2 3} h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "3" out of range}} test lset-8.9a {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a 2 end--2 h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "end--2" out of range}} test lset-8.9b {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a 2 end+2 h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "end+2" out of range}} test lset-8.10a {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a {2 end--2} h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "end--2" out of range}} test lset-8.10b {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a {2 end+2} h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "end+2" out of range}} test lset-8.11 {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a 2 end-2 h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "end-2" out of range}} test lset-8.12 {lset, not compiled, second index out of range} testevalex { set a {{b c} {d e} {f g}} list [catch {testevalex {lset a {2 end-2} h}} msg] $msg -} {1 {list index out of range}} +} {1 {index "end-2" out of range}} test lset-9.1 {lset, not compiled, entire variable} testevalex { set a x diff --git a/tests/lsetComp.test b/tests/lsetComp.test index 6330de4..32bfd5f 100644 --- a/tests/lsetComp.test +++ b/tests/lsetComp.test @@ -219,7 +219,7 @@ test lsetComp-2.8 {lset, compiled, list of args, error } { set x { {1 2} {3 4} } lset x {1 5} 5 } -} "1 {list index out of range}" +} {1 {index "5" out of range}} test lsetComp-2.9 {lset, compiled, list of args, error - is string preserved} { set ::x { { 1 2 } { 3 4 } } @@ -412,7 +412,7 @@ test lsetComp-3.8 {lset, compiled, flat args, error } { set x { {1 2} {3 4} } lset x 1 5 5 } -} "1 {list index out of range}" +} {1 {index "5" out of range}} test lsetComp-3.9 {lset, compiled, flat args, error - is string preserved} { set ::x { { 1 2 } { 3 4 } } |