Artifacts Associated With Ticket 3466099fffffffffffffffffffffffffffffffff
Ticket change [438b54e2ec] (rid 116572) by dkf on 2011-12-27 17:31:19:
- assignee initialized to: "nijtmans"
- cmimetype initialized to: "text/plain"
- comment initialized to:
I was reading about the problems that some people are having with Tcl scripts on Windows due to that platform's insistence on putting a byte-order mark at the start of a UTF-8 file. (Arguably wrong, but we're stuck with it.) For reference: https://groups.google.com/group/comp.lang.tcl/browse_frm/thread/cb6fbae11b95fac6/c4211cabc90a8b30?hl=en#c4211cabc90a8b30 I was wondering if the most effective way of dealing with this would be to make Tcl treat a stray BOM as whitespace for the purpose of script parsing? I don't know exactly how practical this is, but it would make the most pressing part of the problem Go Away.
- foundin initialized to: "obsolete: 8.5.11"
- is_private initialized to: "0"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- status initialized to: "Open"
- submitter initialized to: "dkf"
- subsystem initialized to: "45. Parsing and Eval"
- title initialized to: "BOM in Unicode"
- type initialized to: "Bug"
Ticket change [0e934a6d17] (rid 94640) by nijtmans on 2011-12-28 16:26:48:
- icomment:
I think I would modify Tcl_FSEvalFileEx such that when it encounters a BOM as first character (in any of the forms allowed by Unicode), it would switch the encoding accordingly. Then it would work with UTF-16 as well, in both little- and big-endian formst. It will be about the same amount of work.
- login: "nijtmans"
- mimetype: "text/plain"
- username: "nijtmans"
- icomment:
Ticket change [8fc287d2d8] (rid 120327) by dkf on 2011-12-29 01:13:28:
- icomment:
Makes sense. I think we can get away just fine with making Tcl_FSEvalFileEx assume that the file's contents are supposed to be a script and so do a bit more magic than normal. (Theoretically, we also ought to think about doing progressive evaluation of "large" files, say over 1MB. That's for another time.)
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [da6fa50545] (rid 107785) by nijtmans on 2011-12-29 06:32:31:
- icomment:
First attempt implemented in branch bug-3466099 Donal, do you see any negative effects of this? The disadvantage is that any stream which does not contain a BOM will need to seek to the start, and be read again in (possibly) another encoding... Still, I think this is the way I would go. Any feedback is highly appreciated!
- login: "nijtmans"
- mimetype: "text/plain"
- username: "nijtmans"
- icomment:
Ticket change [61a6bf077d] (rid 92279) by nijtmans on 2011-12-29 22:42:40:
- subsystem changed to: "44. UTF-8 Strings"
Ticket change [ccd09fbeb0] (rid 95771) by dgp on 2012-01-09 21:12:14:
- icomment:
I think that when Tcl_FSEvalFileEx() receives a non-NULL value of encodingName, that request ought to be honored. When the caller hasn't made an explicit request, then I can see some value in using BOM contents as a way to make a better guess than blindly using the system encoding.
- login: "dgp"
- mimetype: "text/plain"
- username: "dgp"
- icomment:
Ticket change [85bf9adc57] (rid 131371) by nijtmans on 2012-01-09 22:07:32:
- icomment:
>I think that when Tcl_FSEvalFileEx() >receives a non-NULL value of encodingName, >that request ought to be honored. Agreed, but it's a little bit trickier. If the user explicitly speciefies "utf-8" or "unicode" that should be honored too. Actually, I am thinking about splitting the functionality between Tcl_FSEvalFileEx and the encoding machinery. Currently the encoding "" is synonymous with the system encoding. We could also create an additional encoding with the name "", which reads the first 2/3/4 bytes to see if it is some kind of BOM. If it is, switch to the corresponding encoding (utf-8, utf-16, ....) otherwise go on using default system encoding. Then the only thing left to be done in Tcl_FSEvalFileEx is to strip the BOM.
- login: "nijtmans"
- mimetype: "text/plain"
- username: "nijtmans"
- icomment:
Ticket change [0b56039e32] (rid 113965) by nijtmans on 2012-02-19 22:27:37:
- icomment:
New attempt in bug-3466099 branch (threw away the old one) Tcl_FSEvalFileEx now throws away the BOM when it is the first character in the stream. If the encoding is set correctly (e.g. to UTF-8) this will work on Unix and Windows. Added test case source-2.7 to prove that. Advantage: no seek is needed, as in the previous implementation. So it is harmless for Tcl 8.4/8.5 as well. This could be improved by adding a new encoding named "", which is almost the same as the system encoding. The only difference is that, if the first characters of the stream is a BOM in any of the UTF-8 or UTF-16 forms it will swith to this encoding, otherwise it will behave exactly like the system encoding. I'll leave that for some other day (and for Tcl 8.6 only)
- login: "nijtmans"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- username: "nijtmans"
- icomment:
Ticket change [cdea52d52e] (rid 95969) by dkf on 2012-02-20 20:16:41:
- icomment:
Your test passes and I think it is correctly testing this feature. (I made the test clearer so that it has the file contents setup in the test body; that's clearer if the test fails.)
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [4b7317dcfe] (rid 126712) by nijtmans on 2012-02-20 22:14:30:
- icomment:
Thanks! Yes I agree with your changes.
- login: "nijtmans"
- mimetype: "text/plain"
- username: "nijtmans"
- icomment:
Ticket change [96acf7efef] (rid 106365) by nijtmans on 2012-03-01 05:48:49:
- closedate changed to: "2012-02-29T22:48:49"
- closer changed to: "nijtmans"
- icomment:
Committed to core-8-4-branch, core-8-5-branch and trunk. The situation as described in the above reference, where the Tcl script file started with BOM, now works as expected. I don't thing that putting a BOM as start in an UTF-8 file is wrong, see http://unicode.org/faq/utf_bom.html Q: When a BOM is used, is it only in 16-bit Unicode text? A: No, a BOM can be used as a signature no matter how the Unicode text is transformed: UTF-16, UTF-8, or UTF-32. The exact bytes comprising the BOM will be whatever the Unicode character U+FEFF is converted into by that transformation format. In that form, the BOM serves to indicate both that it is a Unicode file, and which of the formats it is in. Now Tcl conforms to that, which it never did.
- login: "nijtmans"
- mimetype: "text/plain"
- status changed to: "Closed"
- username: "nijtmans"
Ticket change [98df5c8df3] (rid 110682) by nijtmans on 2012-03-02 05:49:02:
- icomment:
Re-opening, because one situation is not handled yet, which can cause problems. On Windows, normally the system encoding is cp1252 (actually cp1250-1258). The previous part only handles the situation that the encoding is set to utf-8 explicitely. However, the BOM indicates that the remaining should be handled as utf-8, regardless of the system encoding. Therefore, I created a new branch bug-3466099, meant as an experiment (again). What it does: If the system encoding is cp125[0-8] or identity and the file starts with a BOM, the BOM is skipped and the encoding is automatically set to utf-8 while reading the remaining of the file This experiment is committed in branch bug-3466099. Remarks more than welcome. Test cases are source-2.8 up to source-2.17 for the different system encodings. Specific question. Is there any other encoding commonly encountered on Windows, which should be handled the same way? Regards, Jan Nijtmans - login: "nijtmans"
- mimetype: "text/plain"
- resolution changed to: "Accepted"
- status changed to: "Open"
- username: "nijtmans"
- icomment:
Ticket change [d83e73431e] (rid 107404) by sebres on 2012-03-02 17:05:15:
- icomment:
The solution with cpBomTable is although good, but theoretically the parameter 'encodingName' could be another single byte encoding such iso8859-X, etc. So the prepared array 'cpBomTable' should be greater and it will be no more practical. I see 2 solution here: 1) read 4 first characters binary, if not BOM convert its to given 'encodingName', set channel encoding to 'encodingName', read further. 2) read 1 first characters in utf-8, if not BOM seek to start, set channel encoding to 'encodingName', read further. I'm trying now the solution number 1.
- login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [cd74ba5c7a] (rid 95894) by sebres on 2012-03-06 21:23:20:
- icomment:
Commit of another solution (1) with auto recognition (without fixed cpBomTable). See http://core.tcl.tk/tcl/info/8da0451f94 Tests: source.test: Total 31 Passed 31 Skipped 0 Failed 0
- login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [b3adff03df] (rid 124895) by sebres on 2012-03-06 21:31:15:
- icomment:
A pity that parameter "encodingName" of the function "Tcl_FSEvalFileEx" is not a Tcl_Obj.
- login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [85eeec6395] (rid 131409) by dkf on 2012-03-07 20:54:02:
- icomment:
What's the problem with “encodingName” being a “const char *”? That's the type of the argument to Tcl_SetChannelOption…
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [3e041c971b] (rid 121191) by sebres on 2012-03-07 21:09:57:
- icomment:
Because of pair Tcl_GetEncoding/Tcl_FreeEncoding and corresponding part of Tcl_SetChannelOption can be extracted/extended with such as "Tcl_SetChannelEncoding"/"Tcl_GetChannelEncoding" or something as "Tcl_SetChannelObjOption". Idea here would be to use the function "Tcl_GetEncodingFromObj"... I'm optimizer ad infinitum :)
- login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [50151d8fe5] (rid 125917) by dkf on 2012-03-07 21:34:48:
- icomment:
But then we'd need to deal with the problem of how to send a Tcl_Obj through the channel API, and that's an API that may cross thread boundaries (which Tcl_Obj values _must not_ due to the way their memory is managed) and it's going to be hard to make it all work with source potentially getting data out of a VFS. Find something else to optimize. Something easy.
- login: "dkf"
- mimetype: "text/plain"
- username: "dkf"
- icomment:
Ticket change [a1ba7a91d7] (rid 109775) by nijtmans on 2012-03-08 03:37:46:
- icomment:
So, now we have 3 attempts in this branch. Thanks! However, I'm not satisfied with any of them, although up to now sebres' one was the most clever. Currently I am thinking about building it in the channel code: something like a channel option "-checkbom true", which has the effect that the channel first waits for the next 3 bytes, and checks if it is the BOM. If so, it outputs utf-8 BOM and switches encoding to utf-8, otherwise continue as before. Advantage: the channel code has access to the binary buffer, so doesn't need to do so much trickery. Disadvantage: A new channel option needs a TIP.
- login: "nijtmans"
- mimetype: "text/plain"
- username: "nijtmans"
- icomment:
Ticket change [bfac3ca3e6] (rid 102283) by nijtmans on 2012-03-08 05:03:48:
- icomment:
So, started a 4th attempt, building it in the channel code. Implemented is the addition of a -checkbom channel flag (name is open for discussion). Actual handling of this flag is not implemented yet, so the source-2.x testcases still fail. A complete implementation should make those testcases pass. Still didn't figure out where the flag handling should be done, but knowing that the implementation should not be difficult any more......
- login: "nijtmans"
- mimetype: "text/plain"
- username: "nijtmans"
- icomment:
Ticket change [f92c31647f] (rid 91657) by sebres on 2012-03-08 16:15:57:
- icomment:
Hi Jan, you have placed Tcl_SetChannelOption "-checkbom" in else scope of if (encodingName != NULL) ... that means the following code does not ignore BOM from utf-8 file "test.tcl": source -encoding utf-8 test.tcl only for this one: source test.tcl
- login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [5ffe1498ed] (rid 115915) by sebres on 2012-03-08 16:48:42:
- icomment:
Have commited revised "source.test"
- login: "sebres"
- mimetype: "text/plain"
- username: "sebres"
- icomment:
Ticket change [1819af9b08] (rid 136995) by jan.nijtmans on 2014-02-21 19:07:10:
- assignee changed to: "jan.nijtmans"
Ticket change [c2079890aa] (rid 192627) by jan.nijtmans on 2021-03-18 13:44:28:
- closedate changed to: "2459292.07255094"
- closer changed to: "jan.nijtmans"
- comment changed to:
I was reading about the problems that some people are having with Tcl scripts on Windows due to that platform's insistence on putting a byte-order mark at the start of a UTF-8 file. (Arguably wrong, but we're stuck with it.) For reference: https://groups.google.com/group/comp.lang.tcl/browse_frm/thread/cb6fbae11b95fac6/c4211cabc90a8b30?hl=en#c4211cabc90a8b30 I was wondering if the most effective way of dealing with this would be to make Tcl treat a stray BOM as whitespace for the purpose of script parsing? I don't know exactly how practical this is, but it would make the most pressing part of the problem Go Away.
- icomment:
This is fixed now in 8.7, thanks to [https://core.tcl-lang.org/tips/doc/trunk/tip/587.md| TIP #587]. Closing.
- login: "jan.nijtmans"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- severity changed to: "Minor"
- status changed to: "Closed"