From ad8dae7587f0afd369d467a051b23a68cc3703ac Mon Sep 17 00:00:00 2001 From: apnadkarni Date: Thu, 2 Mar 2023 06:29:10 +0000 Subject: Bug [e778e3f804]. Fix error message for invalid profile name. --- generic/tclEncoding.c | 28 +++++++++++++++++++--------- tests/encoding.test | 8 ++++++++ tests/ioCmd.test | 4 ++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/generic/tclEncoding.c b/generic/tclEncoding.c index 264ca96..3842f2f 100644 --- a/generic/tclEncoding.c +++ b/generic/tclEncoding.c @@ -188,15 +188,16 @@ static Tcl_Encoding systemEncoding = NULL; Tcl_Encoding tclIdentityEncoding = NULL; /* - * Names of encoding profiles and corresponding integer values + * Names of encoding profiles and corresponding integer values. + * Keep alphabetical order for error messages. */ static struct TclEncodingProfiles { const char *name; int value; } encodingProfiles[] = { - {"tcl8", TCL_ENCODING_PROFILE_TCL8}, - {"strict", TCL_ENCODING_PROFILE_STRICT}, {"replace", TCL_ENCODING_PROFILE_REPLACE}, + {"strict", TCL_ENCODING_PROFILE_STRICT}, + {"tcl8", TCL_ENCODING_PROFILE_TCL8}, }; #define PROFILE_STRICT(flags_) \ ((TCL_ENCODING_PROFILE_GET(flags_) == TCL_ENCODING_PROFILE_STRICT) \ @@ -4395,19 +4396,28 @@ TclEncodingProfileNameToId( int *profilePtr) /* Output */ { size_t i; + size_t numProfiles = sizeof(encodingProfiles) / sizeof(encodingProfiles[0]); - for (i = 0; i < sizeof(encodingProfiles) / sizeof(encodingProfiles[0]); ++i) { + for (i = 0; i < numProfiles; ++i) { if (!strcmp(profileName, encodingProfiles[i].name)) { *profilePtr = encodingProfiles[i].value; return TCL_OK; } } if (interp) { - Tcl_SetObjResult( - interp, - Tcl_ObjPrintf( - "bad profile \"%s\". Must be \"tcl8\" or \"strict\".", - profileName)); + Tcl_Obj *errorObj; + /* This code assumes at least two profiles :-) */ + errorObj = + Tcl_ObjPrintf("bad profile name \"%s\": must be", + profileName); + for (i = 0; i < (numProfiles - 1); ++i) { + Tcl_AppendStringsToObj( + errorObj, " ", encodingProfiles[i].name, ",", NULL); + } + Tcl_AppendStringsToObj( + errorObj, " or ", encodingProfiles[numProfiles-1].name, NULL); + + Tcl_SetObjResult(interp, errorObj); Tcl_SetErrorCode( interp, "TCL", "ENCODING", "PROFILE", profileName, NULL); } diff --git a/tests/encoding.test b/tests/encoding.test index 215b5c8..8044c8c 100644 --- a/tests/encoding.test +++ b/tests/encoding.test @@ -106,6 +106,14 @@ test encoding-3.2 {Tcl_GetEncodingName, non-null} -setup { } -cleanup { fconfigure stdout -encoding $old } -result {jis0208} +test encoding-3.3 {fconfigure -encodingprofile} -setup { + set old [fconfigure stdout -encodingprofile] +} -body { + fconfigure stdout -encodingprofile replace + fconfigure stdout -encodingprofile +} -cleanup { + fconfigure stdout -encodingprofile $old +} -result replace test encoding-4.1 {Tcl_GetEncodingNames} -constraints {testencoding} -setup { cd [makeDirectory tmp] diff --git a/tests/ioCmd.test b/tests/ioCmd.test index 9e28569..a1ec571 100644 --- a/tests/ioCmd.test +++ b/tests/ioCmd.test @@ -369,6 +369,10 @@ test iocmd-8.20 {fconfigure command / win console channel} -constraints {nonPort # TODO: Test parsing of serial channel options (nonPortable, since requires an # open channel to work with). +test iocmd-8.21 {fconfigure -encodingprofile badprofile} -body { + fconfigure stdin -encodingprofile froboz +} -returnCodes error -result {bad profile name "froboz": must be replace, strict, or tcl8} + test iocmd-9.1 {eof command} { list [catch {eof} msg] $msg $::errorCode } {1 {wrong # args: should be "eof channelId"} {TCL WRONGARGS}} -- cgit v0.12