Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Tcl 9: [gets] on -strictencoding 1 configured channel

Closed / Fixed154ed7ce564a7b4cc39463d643b251969006c247Created 4 years agoUpdated 4 years ago
Descriptionplain2022-12-22 13:15:31
# Create the data
package require Tcl 9
set fd [open "data.txt" w+]
fconfigure $fd -encoding binary
# This is not valid UTF-8
puts $fd AB\xc0\x40CD\nEFG
close $fd

# Now try to read it with [gets]
set fd [open "data.txt"]
fconfigure $fd -encoding utf-8 -strictencoding 1
# That next line will hang indefinitely
set data [gets $fd]
puts "not reached"
close $fd
puts $data

I'm not sure how this is supposed to work 
but it surely should not work this way.
Referenced by6
  • 5966973d58Merge trunk c8983a2a94: Fix [154ed7ce56]: Tcl 9: [gets] on -strictencoding 1 configured channel. check-in2023-03-24 13:16:26
  • c02747a663Fix [154ed7ce56]: Tcl 9: [gets] on -strictencoding 1 configured channel check-in2023-03-21 08:44:37
  • a338fcd542Proposed fix for [154ed7ce56]: Tcl 9: [gets] on -strictencoding 1 configured channel. Extracted from TIP #657 branch (better keeping bug-fix separate from enhancements) check-in2023-03-20 22:53:33
  • 4cdf4836aaFix for [b8f575aa2398b0e4], and also for [154ed7ce56], Tcl 9: [gets] on -strictencoding 1 configured channel. check-in2023-01-17 20:09:49
  • 8c5d9bcc6eMerge py-b8f575aa23: Fix for [154ed7ce56], Tcl 9: [gets] on -strictencoding 1 configured channel. check-in2023-01-02 23:12:02
  • 003c9e1f2eFix for [154ed7ce56], Tcl 9: [gets] on -strictencoding 1 configured channel. check-in2023-01-02 21:07:02
pointsman Ticket [154ed7ce56]: 3 changes [History diff]2022-12-22 13:27:51
jan.nijtmanswiki2022-12-22 14:45:58

Expected behavior: same as "read".

pointsmanplain2022-12-25 00:14:28
You mean: Instead of the hang gets will return the line read so
far in this situation (without raising error) and will raise
error only on the next gets on the same channel?

Please not. Please consider rasing error in the first place
(and change "read" in the same way).
pooryorickwiki2023-01-02 21:10:21

Fixed in [003c9e1f2e53312b]: [gets] returns an error when a decoding error occurs, and data successfully decoded up until the error is available in the "-result" entry of the return options.

pooryorickwiki2023-01-17 22:09:05

See issue [b8f575aa2398b0e4], where this issue is discussed for both [read] and [gets], and multiple alternative fixes are provided.

pooryorickwiki2023-02-18 01:16:24

The commit that caused this broken behaviour of [gets] was [a106c26dd5e8934f], "TIP #346: Error on Failed String Encodings".

jan.nijtmansmarkdown2023-02-23 15:22:41

From Ashok's document:

6.5.2 Channel read state after errors

Related to the above is the question of the channel state when an exception is raised on a read or gets due to invalid input bytes.

My opinion is that when an exception is raised, the read position should not be changed from the value at the time the read or gets was invoked. This allows the application to then turn off the strict checking (if so desired) and read in the rest of the data. For example,

% set fd [open x.txt r]
file1de34c88158
% fconfigure $fd -encoding utf-8 -strictencoding 1
% gets $fd
a
% gets $fd
error reading "file1de34c88158": illegal byte sequence
% fconfigure $fd -encoding utf-8 -strictencoding 0
% gets $fd
bÀ
% gets $fd
c

An alternate suggestion made in the mailing list was to move the read position to the location of the invalid byte and return the characters successfully decoded so far in the error options dictionary. My personal opinion is returning successfully read data in the error options is unnatural and non-idiomatic.

jan.nijtmanswiki2023-03-21 09:17:23

Fixed [here], as discussed. In blocking mode, the exception is thrown immediately. In non-blocking mode, data read so far is returned.

If an exception is thrown, there is no way to get back the read data so far, that will be handled in a separate TIP.

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