Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2023-04-15
22:52
Merge trunk [f9cdaa50ad]: First part of fix for [4a7397e0b3]. Not complete yet. check-in: 039abf675c user: pooryorick tags: trunk-encodingdefaultstrict
2023-04-03
16:32 • New ticket [9ca87e6286] Sync fcopy buffers input in ReadChars(). artifact: 33038694c5 user: pooryorick
2023-03-22
13:31
Merge trunk: Put back testcase io-52.20, and re-fix [4a7397e0b3]. check-in: 1076703a04 user: pooryorick tags: unchained
12:02
Merge Trunk 2d036d968c: Proposed fix (and testcases) for [4a7397e0b3]: Tcl 9: fcopy with -strictenco... check-in: 3814dec4b8 user: pooryorick tags: unchained
2023-02-28
13:43 • Closed ticket [4a7397e0b3]: Tcl 9: fcopy with -strictencoding 1 UTF-8 channels</... artifact: 5da15e9145 user: jan.nijtmans
13:33
Put back testcase io-52.20, and re-fix [4a7397e0b3] check-in: 9c0db3d168 user: jan.nijtmans tags: core-8-branch
12:06 • Open ticket [4a7397e0b3]: Tcl 9: fcopy with -strictencoding 1 UTF-8 channels... artifact: d5dc3a86bf user: pooryorick
2023-02-12
01:10 • Ticket [4a7397e0b3]: 3 changes artifact: fae67b50a6 user: jan.nijtmans
00:49
Proposed fix (and testcases) for [4a7397e0b3]: Tcl 9: fcopy with -strictencoding 1 UTF-8 channels check-in: 8b772479c3 user: jan.nijtmans tags: core-8-branch
2023-02-10
20:25 • Pending ticket [4a7397e0b3]: Tcl 9: fcopy with -strictencoding 1 UTF-8 channels<... artifact: 450dea01f5 user: jan.nijtmans
20:22
Proposed fix (and testcases) for [4a7397e0b3]: Tcl 9: fcopy with -strictencoding 1 UTF-8 channels closed check-in: 0f04d39da4 user: jan.nijtmans tags: bug-4a7397e0b3
2023-01-26
15:19 • Ticket [4a7397e0b3] Tcl 9: fcopy with -strictencoding 1 UTF-8 channels statu... artifact: 9503628092 user: pointsman
13:28 • Ticket [4a7397e0b3]: 4 changes artifact: 5c9155e8ed user: oehhar
07:40 • Ticket [4a7397e0b3]: 3 changes artifact: 2b49605281 user: jan.nijtmans
02:13 • Ticket [4a7397e0b3]: 3 changes artifact: b262cebb8f user: pointsman
2023-01-25
19:28 • Ticket [4a7397e0b3]: 4 changes artifact: aaa667d098 user: oehhar
2023-01-24
20:00
First part of fix for [4a7397e0b3]. Not complete yet. check-in: 5d433e183c user: jan.nijtmans tags: core-8-branch
2022-12-24
14:36
First part of fix for [4a7397e0b3]: Take flags into account when deciding to do a binary fcopy or no... check-in: 7dcc1eadb6 user: jan.nijtmans tags: bug-4a7397e0b3
2022-12-22
14:42 • Ticket [4a7397e0b3] Tcl 9: fcopy with -strictencoding 1 UTF-8 channels statu... artifact: 00c9748172 user: jan.nijtmans
13:27 • New ticket [4a7397e0b3]. artifact: f78cf0cc95 user: pointsman

Tcl 9: fcopy with -strictencoding 1 UTF-8 channels

Closed / Fixed4a7397e0b3d60f1aebf50331b748da9493dba07dCreated 4 years agoUpdated 4 years ago
Descriptionplain2022-12-22 13:27:22
package require Tcl 9

# Create the test data
set fd [open "data.txt" w+]
fconfigure $fd -encoding binary
# Not valid UTF-8
puts $fd AB\xc0\x40CD\nEFG
close $fd

set in [open "data.txt"]
set out [open "copy.txt" w+]
fconfigure $in -encoding utf-8 -strictencoding 1
fconfigure $out -encoding utf-8 -strictencoding 1
fcopy $in $out

Again I'm not sure how this is supposed to work
but the result did surprise me.

The script runs to its end without any error.
The created file "copy.txt" is byte indentical with
"data.txt".

Neither the -strictencoding 1 on the in channel
nor the -strictencoding 1 on the out channel seem
to do anything, if the channels are used with fcopy.

At minimum this should be noted either in the fcopy
or the fconfigure documentation (I looked but didn't
found something about this case atm).

Better, I think, would maybe rasing error on the fcopy.
But as written I'm not sure how this is supposed to work.
Referenced by9
  • 039abf675cMerge trunk [f9cdaa50ad]: First part of fix for [4a7397e0b3]. Not complete yet. check-in2023-04-15 22:52:02
  • 33038694c5New ticket [9ca87e6286] Sync fcopy buffers input in ReadChars(). ticket2023-04-03 16:32:22
  • 1076703a04Merge trunk: Put back testcase io-52.20, and re-fix [4a7397e0b3]. check-in2023-03-22 13:31:32
  • 3814dec4b8Merge Trunk 2d036d968c: Proposed fix (and testcases) for [4a7397e0b3]: Tcl 9: fcopy with -strictencoding 1 UTF-8 channels check-in2023-03-22 12:02:44
  • 9c0db3d168Put back testcase io-52.20, and re-fix [4a7397e0b3] check-in2023-02-28 13:33:04
  • 8b772479c3Proposed fix (and testcases) for [4a7397e0b3]: Tcl 9: fcopy with -strictencoding 1 UTF-8 channels check-in2023-02-12 00:49:22
  • 0f04d39da4Proposed fix (and testcases) for [4a7397e0b3]: Tcl 9: fcopy with -strictencoding 1 UTF-8 channels check-in2023-02-10 20:22:07
  • 5d433e183cFirst part of fix for [4a7397e0b3]. Not complete yet. check-in2023-01-24 20:00:22
  • 7dcc1eadb6First part of fix for [4a7397e0b3]: Take flags into account when deciding to do a binary fcopy or not. TODO: Handle generating an exception check-in2022-12-24 14:36:57
jan.nijtmanswiki2022-12-22 14:42:33

Looking here, it's quite understandable why this happens. The fcopy code has a variable "sameEncoding", which prevents the encoding/decoding step when both encodings are the same. That's an optimization which doesn't take the new flags into account

Changing priority to "Cosmetic". If you like, you can submit a patch removing this variable, but the chance introducing a problem is higher than the chance solving anything practical.

More useful (and related): What happens when converting between utf-8 and utf-16 (for example). "fcopy" is not able to throw an exception currently.

oehharwiki2023-01-25 19:28:17

Dear Rolf, Jan,

to my understanding, fcopy works as expected. It is documented to pass data verbatim if the encoding matches. That is what happens.

Take care, Harald

pointsmanplain2023-01-26 02:13:34
I don't read this in the documentation. The only according quote I find is "No conversion is done if both channels are set to encoding “binary” and have matching translations" which seems to me to be something else (binary versus UTF-8 with explicit -strict configured channel).

And the argument "encoding matches" is void if the involved channels are fconfigured to, say, UTF-8 and UTF-16.

Of course every behaviour could be documented "away" - write what it does in the documentation. But's not necessarily a good solution. If I fconfigure my channel with -strict 1 I clearly said that I want to be informed about encoding errors. You can define that away by documentation. But I think this is wrong.

Jan is right, fcopy should throw execption.
jan.nijtmanswiki2023-01-26 07:40:20

> Jan is right, fcopy should throw execption.

If the channel doesn't have -command specified, an exception should be thrown when an encoding error occurs. If -command is specified, the command will be called with 2 arguments

Another tweak: according to the documentation: https://core.tcl-lang.org/tcl/file?name=doc/fcopy.n&ci=f9cdaa50ad760a0c&ln=29-30

That's not 100% correct any more.

For Tcl 8.7, it should become: size is in bytes if the input channel is in binary mode, or if the two channels are using the same encoding and -strict is not specified. Otherwise, size is in characters.

For Tcl 9.0, it should become: size is in bytes if the input channel is in binary mode, and is in characters otherwise.

Agreed?

oehharwiki2023-01-26 13:28:36

Sorry, my comment was just plain wrong, sorry for the nois, Harald

pointsmanplain2023-01-26 15:19:14
> If the channel doesn't have -command specified, an exception should be thrown
> when an encoding error occurs. If -command is specified, the command
> will be called with 2 arguments 

Yes, of course - raise error in case of working not in the background and if working in the background then use the already established error reporting way via second argument to the callback.

> Another tweak: according to the documentation: https://core.tcl-lang.org/tcl/file?name=doc/fcopy.n&ci=f9cdaa50ad760a0c&ln=29-30

Just as note: The chan command documentation should be updated according to all fcopy documentation change.

> For Tcl 9.0, it should become: size is in bytes if the input channel is in binary mode, and is in characters otherwise. 

That would be along the lines of how it it is. To be sure: You mean independent of -strict status of the channels?

I'm not so sure if this is what is needed in practice (say, copy file from server and the size of the file to expect is given (typically) in bytes in the header) but this is cleary outside the scope of this ticket. 

So, I guess: Yes, I agree with respect to Tcl 9.

For 8.7 the proposed rule would even be more complicated as it alread is: -size is interpreted as bytes for binary channels _and_ if the channels have the same encoding and no -strict 1. But that may be indispensable.
jan.nijtmanswiki2023-02-10 20:25:16

Proposed fix [here]

jan.nijtmanswiki2023-02-12 01:10:21

Fixed in [8b772479c3]. More testcases would be welcome, e.g. testing whether the input/output channels after the fcopy have the correct file position: If an exception is thrown, the file position is expected to be exactly on the position where the encoding error occurred. And - in case of an encoding error on input - the characters received so far should be written to the output channel. I think it should work, but this is not thoroughly tested. It's entirely new functionality.

Someone willing to help?

pooryorickwiki2023-02-28 12:06:21

The proposed fix, [8b772479c3587d91], was moved off core-8-branch because it relies on an unapproved fix for [b8f575aa2398b0e4], which broke the semantics of [read]/[gets]. The current issue should be revisited once a proper fix for the other issue is in place.

jan.nijtmanswiki2023-02-28 13:43:39

Fixed again [here]. Testcase io-52.20 should not have been removed. If you really are unable to provide a proper fix, at least it should have been marked "knownBug"!

This fix (using an additional flag) is not the most elegant solution, but - since - you (@nathan) are working on a better fix for "read" (I hope), any fix for "read" should not ruin "fcopy" (neither "gets", but that's another ticket). Feel free to remove the ENCODING_FCOPY flags as soon as you no longer need it. But io-52.20 should continue working as-is.

Log in (anonymous is fine) to add a comment.