Debian Codegraph @

tests/io.test

raw
1 # -*- tcl -*-
2 # Functionality covered: operation of all IO commands, and all procedures
3 # defined in generic/tclIO.c.
4 #
5 # This file contains a collection of tests for one or more of the Tcl
6 # built-in commands. Sourcing this file into Tcl runs the tests and
7 # generates output for errors. No output means no errors were found.
8 #
9 # Copyright (c) 1991-1994 The Regents of the University of California.
10 # Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 # Copyright (c) 1998-1999 Scriptics Corporation.
12 #
13 # See the file "license.terms" for information on usage and redistribution
14 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15
16 namespace eval ::tcl::test::io {
17
18 if {"::tcltest" ni [namespace children]} {
19 package require tcltest 2.5
20 namespace import -force ::tcltest::*
21 }
22
23 variable umaskValue
24 variable path
25 variable f
26 variable i
27 variable n
28 variable v
29 variable msg
30 variable expected
31
32 set ::tcltestlib {}
33 catch {
34 ::tcltest::loadTestedCommands
35 package require -exact Tcltest [info patchlevel]
36 set ::tcltestlib [lindex [package ifneeded Tcltest [info patchlevel]] 1]
37 }
38 source [file join [file dirname [info script]] tcltests.tcl]
39
40 testConstraint testbytestring [llength [info commands testbytestring]]
41 testConstraint testchannel [llength [info commands testchannel]]
42 testConstraint testfevent [llength [info commands testfevent]]
43 testConstraint testchannelevent [llength [info commands testchannelevent]]
44 testConstraint testmainthread [llength [info commands testmainthread]]
45 testConstraint testobj [llength [info commands testobj]]
46 testConstraint testservicemode [llength [info commands testservicemode]]
47 # Some things fail under Windows in Continuous Integration systems for subtle
48 # reasons such as CI often running with elevated privileges in a container.
49 testConstraint notWinCI [expr {
50 $::tcl_platform(platform) ne "windows" || ![info exists ::env(CI)]}]
51 testConstraint notOSX [expr {$::tcl_platform(os) ne "Darwin"}]
52 # File permissions broken on wsl without some "exotic" wsl configuration
53 testConstraint notWsl [expr {[llength [array names ::env *WSL*]] == 0}]
54
55 # You need a *very* special environment to do some tests. In
56 # particular, many file systems do not support large-files...
57 testConstraint largefileSupport [expr {$::tcl_platform(os) ne "Darwin"}]
58
59 # some tests can only be run is umask is 2
60 # if "umask" cannot be run, the tests will be skipped.
61 set umaskValue 0
62 testConstraint umask [expr {![catch {set umaskValue [scan [exec /bin/sh -c umask] %o]}]}]
63
64 testConstraint makeFileInHome [expr {![file exists ~/_test_] && [file writable ~]}]
65
66 # set up a long data file for some of the following tests
67
68 set path(longfile) [makeFile {} longfile]
69 set f [open $path(longfile) w]
70 fconfigure $f -eofchar {} -translation lf
71 for { set i 0 } { $i < 100 } { incr i} {
72 puts $f "#123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
73 \#123456789abcdef01
74 \#"
75 }
76 close $f
77
78 set path(cat) [makeFile {
79 set f stdin
80 if {$argv ne ""} {
81 set f [open [lindex $argv 0]]
82 }
83 fconfigure $f -encoding binary -translation lf -blocking 0 -eofchar \x1A
84 fconfigure stdout -encoding binary -translation lf -buffering none
85 fileevent $f readable "foo $f"
86 proc foo {f} {
87 set x [read $f]
88 catch {puts -nonewline $x}
89 if {[eof $f]} {
90 close $f
91 exit 0
92 }
93 }
94 vwait forever
95 } cat]
96
97 set thisScript [file join [pwd] [info script]]
98
99 proc contents {file} {
100 set f [open $file]
101 fconfigure $f -translation binary
102 set a [read $f]
103 close $f
104 return $a
105 }
106
107 test io-1.5 {Tcl_WriteChars: CheckChannelErrors} {emptyTest} {
108 # no test, need to cause an async error.
109 } {}
110 set path(test1) [makeFile {} test1]
111 test io-1.6 {Tcl_WriteChars: WriteBytes} {
112 set f [open $path(test1) w]
113 fconfigure $f -encoding binary
114 puts -nonewline $f "a\u4E4D\x00"
115 close $f
116 contents $path(test1)
117 } "a\x4D\x00"
118 test io-1.7 {Tcl_WriteChars: WriteChars} {
119 set f [open $path(test1) w]
120 fconfigure $f -encoding shiftjis
121 puts -nonewline $f "a\u4E4D\x00"
122 close $f
123 contents $path(test1)
124 } "a\x93\xE1\x00"
125 set path(test2) [makeFile {} test2]
126 test io-1.8 {Tcl_WriteChars: WriteChars} {
127 # This test written for SF bug #506297.
128 #
129 # Executing this test without the fix for the referenced bug
130 # applied to tcl will cause tcl, more specifically WriteChars, to
131 # go into an infinite loop.
132
133 set f [open $path(test2) w]
134 fconfigure $f -encoding iso2022-jp
135 puts -nonewline $f [format %s%c [string repeat " " 4] 12399]
136 close $f
137 contents $path(test2)
138 } " \x1B\$B\$O\x1B(B"
139
140 test io-1.9 {Tcl_WriteChars: WriteChars} {
141 # When closing a channel with an encoding that appends
142 # escape bytes, check for the case where the escape
143 # bytes overflow the current IO buffer. The bytes
144 # should be moved into a new buffer.
145
146 set data "1234567890 [format %c 12399]"
147
148 set sizes [list]
149
150 # With default buffer size
151 set f [open $path(test2) w]
152 fconfigure $f -encoding iso2022-jp
153 puts -nonewline $f $data
154 close $f
155 lappend sizes [file size $path(test2)]
156
157 # With buffer size equal to the length
158 # of the data, the escape bytes would
159 # go into the next buffer.
160
161 set f [open $path(test2) w]
162 fconfigure $f -encoding iso2022-jp -buffersize 16
163 puts -nonewline $f $data
164 close $f
165 lappend sizes [file size $path(test2)]
166
167 # With buffer size that is large enough
168 # to hold 1 byte of escaped data, but
169 # not all 3. This should not write
170 # the escape bytes to the first buffer
171 # and then again to the second buffer.
172
173 set f [open $path(test2) w]
174 fconfigure $f -encoding iso2022-jp -buffersize 17
175 puts -nonewline $f $data
176 close $f
177 lappend sizes [file size $path(test2)]
178
179 # With buffer size that can hold 2 out of
180 # 3 bytes of escaped data.
181
182 set f [open $path(test2) w]
183 fconfigure $f -encoding iso2022-jp -buffersize 18
184 puts -nonewline $f $data
185 close $f
186 lappend sizes [file size $path(test2)]
187
188 # With buffer size that can hold all the
189 # data and escape bytes.
190
191 set f [open $path(test2) w]
192 fconfigure $f -encoding iso2022-jp -buffersize 19
193 puts -nonewline $f $data
194 close $f
195 lappend sizes [file size $path(test2)]
196
197 set sizes
198 } {19 19 19 19 19}
199
200 test io-2.1 {WriteBytes} {
201 # loop until all bytes are written
202
203 set f [open $path(test1) w]
204 fconfigure $f -encoding binary -buffersize 16 -translation crlf
205 puts $f "abcdefghijklmnopqrstuvwxyz"
206 close $f
207 contents $path(test1)
208 } "abcdefghijklmnopqrstuvwxyz\r\n"
209 test io-2.2 {WriteBytes: savedLF > 0} {
210 # After flushing buffer, there was a \n left over from the last
211 # \n -> \r\n expansion. It gets stuck at beginning of this buffer.
212
213 set f [open $path(test1) w]
214 fconfigure $f -encoding binary -buffersize 16 -translation crlf
215 puts -nonewline $f "123456789012345\n12"
216 set x [list [contents $path(test1)]]
217 close $f
218 lappend x [contents $path(test1)]
219 } [list "123456789012345\r" "123456789012345\r\n12"]
220 test io-2.3 {WriteBytes: flush on line} {
221 # Tcl "line" buffering has weird behavior: if current buffer contains
222 # a \n, entire buffer gets flushed. Logical behavior would be to flush
223 # only up to the \n.
224
225 set f [open $path(test1) w]
226 fconfigure $f -encoding binary -buffering line -translation crlf
227 puts -nonewline $f "\n12"
228 set x [contents $path(test1)]
229 close $f
230 set x
231 } "\r\n12"
232 test io-2.4 {WriteBytes: reset sawLF after each buffer} {
233 set f [open $path(test1) w]
234 fconfigure $f -encoding binary -buffering line -translation lf \
235 -buffersize 16
236 puts -nonewline $f "abcdefg\nhijklmnopqrstuvwxyz"
237 set x [list [contents $path(test1)]]
238 close $f
239 lappend x [contents $path(test1)]
240 } [list "abcdefg\nhijklmno" "abcdefg\nhijklmnopqrstuvwxyz"]
241
242 test io-3.1 {WriteChars: compatibility with WriteBytes} {
243 # loop until all bytes are written
244
245 set f [open $path(test1) w]
246 fconfigure $f -encoding ascii -buffersize 16 -translation crlf
247 puts $f "abcdefghijklmnopqrstuvwxyz"
248 close $f
249 contents $path(test1)
250 } "abcdefghijklmnopqrstuvwxyz\r\n"
251 test io-3.2 {WriteChars: compatibility with WriteBytes: savedLF > 0} {
252 # After flushing buffer, there was a \n left over from the last
253 # \n -> \r\n expansion. It gets stuck at beginning of this buffer.
254
255 set f [open $path(test1) w]
256 fconfigure $f -encoding ascii -buffersize 16 -translation crlf
257 puts -nonewline $f "123456789012345\n12"
258 set x [list [contents $path(test1)]]
259 close $f
260 lappend x [contents $path(test1)]
261 } [list "123456789012345\r" "123456789012345\r\n12"]
262 test io-3.3 {WriteChars: compatibility with WriteBytes: flush on line} {
263 # Tcl "line" buffering has weird behavior: if current buffer contains
264 # a \n, entire buffer gets flushed. Logical behavior would be to flush
265 # only up to the \n.
266
267 set f [open $path(test1) w]
268 fconfigure $f -encoding ascii -buffering line -translation crlf
269 puts -nonewline $f "\n12"
270 set x [contents $path(test1)]
271 close $f
272 set x
273 } "\r\n12"
274 test io-3.4 {WriteChars: loop over stage buffer} -body {
275 # stage buffer maps to more than can be queued at once.
276
277 set f [open $path(test1) w]
278 fconfigure $f -encoding jis0208 -buffersize 16
279 puts -nonewline $f "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
280 set x [list [contents $path(test1)]]
281 close $f
282 lappend x [contents $path(test1)]
283 } -cleanup {
284 catch {close $f}
285 } -result [list "!)!)!)!)!)!)!)!)" "!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)"]
286 test io-3.5 {WriteChars: saved != 0} -body {
287 # Bytes produced by UtfToExternal from end of last channel buffer
288 # had to be moved to beginning of next channel buffer to preserve
289 # requested buffersize.
290
291 set f [open $path(test1) w]
292 fconfigure $f -encoding jis0208 -buffersize 17
293 puts -nonewline $f "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
294 set x [list [contents $path(test1)]]
295 close $f
296 lappend x [contents $path(test1)]
297 } -cleanup {
298 catch {close $f}
299 } -result [list "!)!)!)!)!)!)!)!)!" "!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)"]
300 test io-3.6 {WriteChars: (stageRead + dstWrote == 0)} {
301 # One incomplete UTF-8 character at end of staging buffer. Backup
302 # in src to the beginning of that UTF-8 character and try again.
303 #
304 # Translate the first 16 bytes, produce 14 bytes of output, 2 left over
305 # (first two bytes of \uFF21 in UTF-8). Given those two bytes try
306 # translating them again, find that no bytes are read produced, and break
307 # to outer loop where those two bytes will have the remaining 4 bytes
308 # (the last byte of \uFF21 plus the all of \uFF22) appended.
309
310 set f [open $path(test1) w]
311 fconfigure $f -encoding shiftjis -buffersize 16
312 puts -nonewline $f "12345678901234\uFF21\uFF22"
313 set x [list [contents $path(test1)]]
314 close $f
315 lappend x [contents $path(test1)]
316 } [list "12345678901234\x82\x60" "12345678901234\x82\x60\x82\x61"]
317 test io-3.7 {WriteChars: (bufPtr->nextAdded > bufPtr->length)} -body {
318 # When translating UTF-8 to external, the produced bytes went past end
319 # of the channel buffer. This is done purpose -- we then truncate the
320 # bytes at the end of the partial character to preserve the requested
321 # blocksize on flush. The truncated bytes are moved to the beginning
322 # of the next channel buffer.
323
324 set f [open $path(test1) w]
325 fconfigure $f -encoding jis0208 -buffersize 17
326 puts -nonewline $f "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
327 set x [list [contents $path(test1)]]
328 close $f
329 lappend x [contents $path(test1)]
330 } -cleanup {
331 catch {close $f}
332 } -result [list "!)!)!)!)!)!)!)!)!" "!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)"]
333 test io-3.8 {WriteChars: reset sawLF after each buffer} {
334 set f [open $path(test1) w]
335 fconfigure $f -encoding ascii -buffering line -translation lf \
336 -buffersize 16
337 puts -nonewline $f "abcdefg\nhijklmnopqrstuvwxyz"
338 set x [list [contents $path(test1)]]
339 close $f
340 lappend x [contents $path(test1)]
341 } [list "abcdefg\nhijklmno" "abcdefg\nhijklmnopqrstuvwxyz"]
342 test io-3.9 {Write: flush line-buffered channels when crlf is split over two buffers} -body {
343 # https://core.tcl-lang.org/tcllib/tktedit?name=c9d8a52fe
344 set f [open $path(test1) w]
345 fconfigure $f -buffering line -translation crlf -buffersize 8
346 puts $f "1234567"
347 string map {"\r" "<cr>" "\n" "<lf>"} [contents $path(test1)]
348 } -cleanup {
349 close $f
350 } -result "1234567<cr><lf>"
351
352 test io-4.1 {TranslateOutputEOL: lf} {
353 # search for \n
354
355 set f [open $path(test1) w]
356 fconfigure $f -buffering line -translation lf
357 puts $f "abcde"
358 set x [list [contents $path(test1)]]
359 close $f
360 lappend x [contents $path(test1)]
361 } [list "abcde\n" "abcde\n"]
362 test io-4.2 {TranslateOutputEOL: cr} {
363 # search for \n, replace with \r
364
365 set f [open $path(test1) w]
366 fconfigure $f -buffering line -translation cr
367 puts $f "abcde"
368 set x [list [contents $path(test1)]]
369 close $f
370 lappend x [contents $path(test1)]
371 } [list "abcde\r" "abcde\r"]
372 test io-4.3 {TranslateOutputEOL: crlf} {
373 # simple case: search for \n, replace with \r
374
375 set f [open $path(test1) w]
376 fconfigure $f -buffering line -translation crlf
377 puts $f "abcde"
378 set x [list [contents $path(test1)]]
379 close $f
380 lappend x [contents $path(test1)]
381 } [list "abcde\r\n" "abcde\r\n"]
382 test io-4.4 {TranslateOutputEOL: crlf} {
383 # keep storing more bytes in output buffer until output buffer is full.
384 # We have 13 bytes initially that would turn into 18 bytes. Fill
385 # dest buffer while (dstEnd < dstMax).
386
387 set f [open $path(test1) w]
388 fconfigure $f -translation crlf -buffersize 16
389 puts -nonewline $f "1234567\n\n\n\n\nA"
390 set x [list [contents $path(test1)]]
391 close $f
392 lappend x [contents $path(test1)]
393 } [list "1234567\r\n\r\n\r\n\r\n\r" "1234567\r\n\r\n\r\n\r\n\r\nA"]
394 test io-4.5 {TranslateOutputEOL: crlf} {
395 # Check for overflow of the destination buffer
396
397 set f [open $path(test1) w]
398 fconfigure $f -translation crlf -buffersize 12
399 puts -nonewline $f "12345678901\n456789012345678901234"
400 close $f
401 set x [contents $path(test1)]
402 } "12345678901\r\n456789012345678901234"
403
404 test io-5.1 {CheckFlush: not full} {
405 set f [open $path(test1) w]
406 fconfigure $f
407 puts -nonewline $f "12345678901234567890"
408 set x [list [contents $path(test1)]]
409 close $f
410 lappend x [contents $path(test1)]
411 } [list "" "12345678901234567890"]
412 test io-5.2 {CheckFlush: full} {
413 set f [open $path(test1) w]
414 fconfigure $f -buffersize 16
415 puts -nonewline $f "12345678901234567890"
416 set x [list [contents $path(test1)]]
417 close $f
418 lappend x [contents $path(test1)]
419 } [list "1234567890123456" "12345678901234567890"]
420 test io-5.3 {CheckFlush: not line} {
421 set f [open $path(test1) w]
422 fconfigure $f -buffering line
423 puts -nonewline $f "12345678901234567890"
424 set x [list [contents $path(test1)]]
425 close $f
426 lappend x [contents $path(test1)]
427 } [list "" "12345678901234567890"]
428 test io-5.4 {CheckFlush: line} {
429 set f [open $path(test1) w]
430 fconfigure $f -buffering line -translation lf -encoding ascii
431 puts -nonewline $f "1234567890\n1234567890"
432 set x [list [contents $path(test1)]]
433 close $f
434 lappend x [contents $path(test1)]
435 } [list "1234567890\n1234567890" "1234567890\n1234567890"]
436 test io-5.5 {CheckFlush: none} {
437 set f [open $path(test1) w]
438 fconfigure $f -buffering none
439 puts -nonewline $f "1234567890"
440 set x [list [contents $path(test1)]]
441 close $f
442 lappend x [contents $path(test1)]
443 } [list "1234567890" "1234567890"]
444
445 test io-6.1 {Tcl_GetsObj: working} {
446 set f [open $path(test1) w]
447 puts $f "foo\nboo"
448 close $f
449 set f [open $path(test1)]
450 set x [gets $f]
451 close $f
452 set x
453 } {foo}
454 test io-6.2 {Tcl_GetsObj: CheckChannelErrors() != 0} emptyTest {
455 # no test, need to cause an async error.
456 } {}
457 test io-6.3 {Tcl_GetsObj: how many have we used?} {
458 # if (bufPtr != NULL) {oldRemoved = bufPtr->nextRemoved}
459
460 set f [open $path(test1) w]
461 fconfigure $f -translation crlf
462 puts $f "abc\ndefg"
463 close $f
464 set f [open $path(test1)]
465 set x [list [tell $f] [gets $f line] [tell $f] [gets $f line] $line]
466 close $f
467 set x
468 } {0 3 5 4 defg}
469 test io-6.4 {Tcl_GetsObj: encoding == NULL} {
470 set f [open $path(test1) w]
471 fconfigure $f -translation binary
472 puts $f "\x81\u1234\x00"
473 close $f
474 set f [open $path(test1)]
475 fconfigure $f -translation binary
476 set x [list [gets $f line] $line]
477 close $f
478 set x
479 } [list 3 "\x81\x34\x00"]
480 test io-6.5 {Tcl_GetsObj: encoding != NULL} {
481 set f [open $path(test1) w]
482 fconfigure $f -translation binary
483 puts $f "\x88\xEA\x92\x9A"
484 close $f
485 set f [open $path(test1)]
486 fconfigure $f -encoding shiftjis
487 set x [list [gets $f line] $line]
488 close $f
489 set x
490 } [list 2 "\u4E00\u4E01"]
491 set a "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
492 append a $a
493 append a $a
494 test io-6.6 {Tcl_GetsObj: loop test} {
495 # if (dst >= dstEnd)
496
497 set f [open $path(test1) w]
498 puts $f $a
499 puts $f hi
500 close $f
501 set f [open $path(test1)]
502 set x [list [gets $f line] $line]
503 close $f
504 set x
505 } [list 256 $a]
506 test io-6.7 {Tcl_GetsObj: error in input} stdio {
507 # if (FilterInputBytes(chanPtr, &gs) != 0)
508
509 set f [open "|[list [interpreter] $path(cat)]" w+]
510 puts -nonewline $f "hi\nwould"
511 flush $f
512 gets $f
513 fconfigure $f -blocking 0
514 set x [gets $f line]
515 close $f
516 set x
517 } {-1}
518 test io-6.8 {Tcl_GetsObj: remember if EOF is seen} {
519 set f [open $path(test1) w]
520 puts $f "abcdef\x1Aghijk\nwombat"
521 close $f
522 set f [open $path(test1)]
523 fconfigure $f -eofchar \x1A
524 set x [list [gets $f line] $line [gets $f line] $line]
525 close $f
526 set x
527 } {6 abcdef -1 {}}
528 test io-6.9 {Tcl_GetsObj: remember if EOF is seen} {
529 set f [open $path(test1) w]
530 puts $f "abcdefghijk\nwom\u001Abat"
531 close $f
532 set f [open $path(test1)]
533 fconfigure $f -eofchar \x1A
534 set x [list [gets $f line] $line [gets $f line] $line]
535 close $f
536 set x
537 } {11 abcdefghijk 3 wom}
538 # Comprehensive tests
539 test io-6.10 {Tcl_GetsObj: lf mode: no chars} {
540 set f [open $path(test1) w]
541 close $f
542 set f [open $path(test1)]
543 fconfigure $f -translation lf
544 set x [list [gets $f line] $line]
545 close $f
546 set x
547 } {-1 {}}
548 test io-6.11 {Tcl_GetsObj: lf mode: lone \n} {
549 set f [open $path(test1) w]
550 fconfigure $f -translation lf
551 puts -nonewline $f "\n"
552 close $f
553 set f [open $path(test1)]
554 fconfigure $f -translation lf
555 set x [list [gets $f line] $line [gets $f line] $line]
556 close $f
557 set x
558 } {0 {} -1 {}}
559 test io-6.12 {Tcl_GetsObj: lf mode: lone \r} {
560 set f [open $path(test1) w]
561 fconfigure $f -translation lf
562 puts -nonewline $f "\r"
563 close $f
564 set f [open $path(test1)]
565 fconfigure $f -translation lf
566 set x [list [gets $f line] $line [gets $f line] $line]
567 close $f
568 set x
569 } [list 1 "\r" -1 ""]
570 test io-6.13 {Tcl_GetsObj: lf mode: 1 char} {
571 set f [open $path(test1) w]
572 fconfigure $f -translation lf
573 puts -nonewline $f a
574 close $f
575 set f [open $path(test1)]
576 fconfigure $f -translation lf
577 set x [list [gets $f line] $line [gets $f line] $line]
578 close $f
579 set x
580 } {1 a -1 {}}
581 test io-6.14 {Tcl_GetsObj: lf mode: 1 char followed by EOL} {
582 set f [open $path(test1) w]
583 fconfigure $f -translation lf
584 puts -nonewline $f "a\n"
585 close $f
586 set f [open $path(test1)]
587 fconfigure $f -translation lf
588 set x [list [gets $f line] $line [gets $f line] $line]
589 close $f
590 set x
591 } {1 a -1 {}}
592 test io-6.15 {Tcl_GetsObj: lf mode: several chars} {
593 set f [open $path(test1) w]
594 fconfigure $f -translation lf
595 puts -nonewline $f "abcd\nefgh\rijkl\r\nmnop"
596 close $f
597 set f [open $path(test1)]
598 fconfigure $f -translation lf
599 set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line [gets $f line] $line]
600 close $f
601 set x
602 } [list 4 "abcd" 10 "efgh\rijkl\r" 4 "mnop" -1 ""]
603 test io-6.16 {Tcl_GetsObj: cr mode: no chars} {
604 set f [open $path(test1) w]
605 close $f
606 set f [open $path(test1)]
607 fconfigure $f -translation cr
608 set x [list [gets $f line] $line]
609 close $f
610 set x
611 } {-1 {}}
612 test io-6.17 {Tcl_GetsObj: cr mode: lone \n} {
613 set f [open $path(test1) w]
614 fconfigure $f -translation lf
615 puts -nonewline $f "\n"
616 close $f
617 set f [open $path(test1)]
618 fconfigure $f -translation cr
619 set x [list [gets $f line] $line [gets $f line] $line]
620 close $f
621 set x
622 } [list 1 "\n" -1 ""]
623 test io-6.18 {Tcl_GetsObj: cr mode: lone \r} {
624 set f [open $path(test1) w]
625 fconfigure $f -translation lf
626 puts -nonewline $f "\r"
627 close $f
628 set f [open $path(test1)]
629 fconfigure $f -translation cr
630 set x [list [gets $f line] $line [gets $f line] $line]
631 close $f
632 set x
633 } {0 {} -1 {}}
634 test io-6.19 {Tcl_GetsObj: cr mode: 1 char} {
635 set f [open $path(test1) w]
636 fconfigure $f -translation lf
637 puts -nonewline $f a
638 close $f
639 set f [open $path(test1)]
640 fconfigure $f -translation cr
641 set x [list [gets $f line] $line [gets $f line] $line]
642 close $f
643 set x
644 } {1 a -1 {}}
645 test io-6.20 {Tcl_GetsObj: cr mode: 1 char followed by EOL} {
646 set f [open $path(test1) w]
647 fconfigure $f -translation lf
648 puts -nonewline $f "a\r"
649 close $f
650 set f [open $path(test1)]
651 fconfigure $f -translation cr
652 set x [list [gets $f line] $line [gets $f line] $line]
653 close $f
654 set x
655 } {1 a -1 {}}
656 test io-6.21 {Tcl_GetsObj: cr mode: several chars} {
657 set f [open $path(test1) w]
658 fconfigure $f -translation lf
659 puts -nonewline $f "abcd\nefgh\rijkl\r\nmnop"
660 close $f
661 set f [open $path(test1)]
662 fconfigure $f -translation cr
663 set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line [gets $f line] $line]
664 close $f
665 set x
666 } [list 9 "abcd\nefgh" 4 "ijkl" 5 "\nmnop" -1 ""]
667 test io-6.22 {Tcl_GetsObj: crlf mode: no chars} {
668 set f [open $path(test1) w]
669 close $f
670 set f [open $path(test1)]
671 fconfigure $f -translation crlf
672 set x [list [gets $f line] $line]
673 close $f
674 set x
675 } {-1 {}}
676 test io-6.23 {Tcl_GetsObj: crlf mode: lone \n} {
677 set f [open $path(test1) w]
678 fconfigure $f -translation lf
679 puts -nonewline $f "\n"
680 close $f
681 set f [open $path(test1)]
682 fconfigure $f -translation crlf
683 set x [list [gets $f line] $line [gets $f line] $line]
684 close $f
685 set x
686 } [list 1 "\n" -1 ""]
687 test io-6.24 {Tcl_GetsObj: crlf mode: lone \r} {
688 set f [open $path(test1) w]
689 fconfigure $f -translation lf
690 puts -nonewline $f "\r"
691 close $f
692 set f [open $path(test1)]
693 fconfigure $f -translation crlf
694 set x [list [gets $f line] $line [gets $f line] $line]
695 close $f
696 set x
697 } [list 1 "\r" -1 ""]
698 test io-6.25 {Tcl_GetsObj: crlf mode: \r\r} {
699 set f [open $path(test1) w]
700 fconfigure $f -translation lf
701 puts -nonewline $f "\r\r"
702 close $f
703 set f [open $path(test1)]
704 fconfigure $f -translation crlf
705 set x [list [gets $f line] $line [gets $f line] $line]
706 close $f
707 set x
708 } [list 2 "\r\r" -1 ""]
709 test io-6.26 {Tcl_GetsObj: crlf mode: \r\n} {
710 set f [open $path(test1) w]
711 fconfigure $f -translation lf
712 puts -nonewline $f "\r\n"
713 close $f
714 set f [open $path(test1)]
715 fconfigure $f -translation crlf
716 set x [list [gets $f line] $line [gets $f line] $line]
717 close $f
718 set x
719 } [list 0 "" -1 ""]
720 test io-6.27 {Tcl_GetsObj: crlf mode: 1 char} {
721 set f [open $path(test1) w]
722 fconfigure $f -translation lf
723 puts -nonewline $f a
724 close $f
725 set f [open $path(test1)]
726 fconfigure $f -translation crlf
727 set x [list [gets $f line] $line [gets $f line] $line]
728 close $f
729 set x
730 } {1 a -1 {}}
731 test io-6.28 {Tcl_GetsObj: crlf mode: 1 char followed by EOL} {
732 set f [open $path(test1) w]
733 fconfigure $f -translation lf
734 puts -nonewline $f "a\r\n"
735 close $f
736 set f [open $path(test1)]
737 fconfigure $f -translation crlf
738 set x [list [gets $f line] $line [gets $f line] $line]
739 close $f
740 set x
741 } {1 a -1 {}}
742 test io-6.29 {Tcl_GetsObj: crlf mode: several chars} {
743 set f [open $path(test1) w]
744 fconfigure $f -translation lf
745 puts -nonewline $f "abcd\nefgh\rijkl\r\nmnop"
746 close $f
747 set f [open $path(test1)]
748 fconfigure $f -translation crlf
749 set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line]
750 close $f
751 set x
752 } [list 14 "abcd\nefgh\rijkl" 4 "mnop" -1 ""]
753 test io-6.30 {Tcl_GetsObj: crlf mode: buffer exhausted} {testchannel} {
754 # if (eol >= dstEnd)
755
756 set f [open $path(test1) w]
757 fconfigure $f -translation lf
758 puts -nonewline $f "123456789012345\r\nabcdefghijklmnoprstuvwxyz"
759 close $f
760 set f [open $path(test1)]
761 fconfigure $f -translation crlf -buffersize 16
762 set x [list [gets $f line] $line [testchannel inputbuffered $f]]
763 close $f
764 set x
765 } [list 15 "123456789012345" 15]
766 test io-6.31 {Tcl_GetsObj: crlf mode: buffer exhausted, blocked} {stdio testchannel fileevent} {
767 # (FilterInputBytes() != 0)
768
769 set f [open "|[list [interpreter] $path(cat)]" w+]
770 fconfigure $f -translation {crlf lf} -buffering none
771 puts -nonewline $f "bbbbbbbbbbbbbb\r\n123456789012345\r"
772 fconfigure $f -buffersize 16
773 set x [gets $f]
774 fconfigure $f -blocking 0
775 lappend x [gets $f line] $line [fblocked $f] [testchannel inputbuffered $f]
776 close $f
777 set x
778 } [list "bbbbbbbbbbbbbb" -1 "" 1 16]
779 test io-6.32 {Tcl_GetsObj: crlf mode: buffer exhausted, more data} {testchannel} {
780 # not (FilterInputBytes() != 0)
781
782 set f [open $path(test1) w]
783 fconfigure $f -translation lf
784 puts -nonewline $f "123456789012345\r\n123"
785 close $f
786 set f [open $path(test1)]
787 fconfigure $f -translation crlf -buffersize 16
788 set x [list [gets $f line] $line [tell $f] [testchannel inputbuffered $f]]
789 close $f
790 set x
791 } [list 15 "123456789012345" 17 3]
792 test io-6.33 {Tcl_GetsObj: crlf mode: buffer exhausted, at eof} {
793 # eol still equals dstEnd
794
795 set f [open $path(test1) w]
796 fconfigure $f -translation lf
797 puts -nonewline $f "123456789012345\r"
798 close $f
799 set f [open $path(test1)]
800 fconfigure $f -translation crlf -buffersize 16
801 set x [list [gets $f line] $line [eof $f]]
802 close $f
803 set x
804 } [list 16 "123456789012345\r" 1]
805 test io-6.34 {Tcl_GetsObj: crlf mode: buffer exhausted, not followed by \n} {
806 # not (*eol == '\n')
807
808 set f [open $path(test1) w]
809 fconfigure $f -translation lf
810 puts -nonewline $f "123456789012345\rabcd\r\nefg"
811 close $f
812 set f [open $path(test1)]
813 fconfigure $f -translation crlf -buffersize 16
814 set x [list [gets $f line] $line [tell $f]]
815 close $f
816 set x
817 } [list 20 "123456789012345\rabcd" 22]
818 test io-6.35 {Tcl_GetsObj: auto mode: no chars} {
819 set f [open $path(test1) w]
820 close $f
821 set f [open $path(test1)]
822 fconfigure $f -translation auto
823 set x [list [gets $f line] $line]
824 close $f
825 set x
826 } {-1 {}}
827 test io-6.36 {Tcl_GetsObj: auto mode: lone \n} {
828 set f [open $path(test1) w]
829 fconfigure $f -translation lf
830 puts -nonewline $f "\n"
831 close $f
832 set f [open $path(test1)]
833 fconfigure $f -translation auto
834 set x [list [gets $f line] $line [gets $f line] $line]
835 close $f
836 set x
837 } [list 0 "" -1 ""]
838 test io-6.37 {Tcl_GetsObj: auto mode: lone \r} {
839 set f [open $path(test1) w]
840 fconfigure $f -translation lf
841 puts -nonewline $f "\r"
842 close $f
843 set f [open $path(test1)]
844 fconfigure $f -translation auto
845 set x [list [gets $f line] $line [gets $f line] $line]
846 close $f
847 set x
848 } [list 0 "" -1 ""]
849 test io-6.38 {Tcl_GetsObj: auto mode: \r\r} {
850 set f [open $path(test1) w]
851 fconfigure $f -translation lf
852 puts -nonewline $f "\r\r"
853 close $f
854 set f [open $path(test1)]
855 fconfigure $f -translation auto
856 set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line]
857 close $f
858 set x
859 } [list 0 "" 0 "" -1 ""]
860 test io-6.39 {Tcl_GetsObj: auto mode: \r\n} {
861 set f [open $path(test1) w]
862 fconfigure $f -translation lf
863 puts -nonewline $f "\r\n"
864 close $f
865 set f [open $path(test1)]
866 fconfigure $f -translation auto
867 set x [list [gets $f line] $line [gets $f line] $line]
868 close $f
869 set x
870 } [list 0 "" -1 ""]
871 test io-6.40 {Tcl_GetsObj: auto mode: 1 char} {
872 set f [open $path(test1) w]
873 fconfigure $f -translation lf
874 puts -nonewline $f a
875 close $f
876 set f [open $path(test1)]
877 fconfigure $f -translation auto
878 set x [list [gets $f line] $line [gets $f line] $line]
879 close $f
880 set x
881 } {1 a -1 {}}
882 test io-6.41 {Tcl_GetsObj: auto mode: 1 char followed by EOL} {
883 set f [open $path(test1) w]
884 fconfigure $f -translation lf
885 puts -nonewline $f "a\r\n"
886 close $f
887 set f [open $path(test1)]
888 fconfigure $f -translation auto
889 set x [list [gets $f line] $line [gets $f line] $line]
890 close $f
891 set x
892 } {1 a -1 {}}
893 test io-6.42 {Tcl_GetsObj: auto mode: several chars} {
894 set f [open $path(test1) w]
895 fconfigure $f -translation lf
896 puts -nonewline $f "abcd\nefgh\rijkl\r\nmnop"
897 close $f
898 set f [open $path(test1)]
899 fconfigure $f -translation auto
900 set x [list [gets $f line] $line [gets $f line] $line]
901 lappend x [gets $f line] $line [gets $f line] $line [gets $f line] $line
902 close $f
903 set x
904 } [list 4 "abcd" 4 "efgh" 4 "ijkl" 4 "mnop" -1 ""]
905 test io-6.43 {Tcl_GetsObj: input saw cr} {stdio testchannel fileevent} {
906 # if (chanPtr->flags & INPUT_SAW_CR)
907
908 set f [open "|[list [interpreter] $path(cat)]" w+]
909 fconfigure $f -translation {auto lf} -buffering none
910 puts -nonewline $f "bbbbbbbbbbbbbbb\n123456789abcdef\r"
911 fconfigure $f -buffersize 16
912 set x [list [gets $f]]
913 fconfigure $f -blocking 0
914 lappend x [gets $f line] $line [testchannel queuedcr $f]
915 fconfigure $f -blocking 1
916 puts -nonewline $f "\nabcd\refg\x1A"
917 lappend x [gets $f line] $line [testchannel queuedcr $f]
918 lappend x [gets $f line] $line
919 close $f
920 set x
921 } [list "bbbbbbbbbbbbbbb" 15 "123456789abcdef" 1 4 "abcd" 0 3 "efg"]
922 test io-6.44 {Tcl_GetsObj: input saw cr, not followed by cr} {stdio testchannel fileevent} {
923 # not (*eol == '\n')
924
925 set f [open "|[list [interpreter] $path(cat)]" w+]
926 fconfigure $f -translation {auto lf} -buffering none
927 puts -nonewline $f "bbbbbbbbbbbbbbb\n123456789abcdef\r"
928 fconfigure $f -buffersize 16
929 set x [list [gets $f]]
930 fconfigure $f -blocking 0
931 lappend x [gets $f line] $line [testchannel queuedcr $f]
932 fconfigure $f -blocking 1
933 puts -nonewline $f "abcd\refg\x1A"
934 lappend x [gets $f line] $line [testchannel queuedcr $f]
935 lappend x [gets $f line] $line
936 close $f
937 set x
938 } [list "bbbbbbbbbbbbbbb" 15 "123456789abcdef" 1 4 "abcd" 0 3 "efg"]
939 test io-6.45 {Tcl_GetsObj: input saw cr, skip right number of bytes} {stdio testchannel fileevent} {
940 # Tcl_ExternalToUtf()
941
942 set f [open "|[list [interpreter] $path(cat)]" w+]
943 fconfigure $f -translation {auto lf} -buffering none
944 fconfigure $f -encoding unicode
945 puts -nonewline $f "bbbbbbbbbbbbbbb\n123456789abcdef\r"
946 fconfigure $f -buffersize 16
947 gets $f
948 fconfigure $f -blocking 0
949 set x [list [gets $f line] $line [testchannel queuedcr $f]]
950 fconfigure $f -blocking 1
951 puts -nonewline $f "\nabcd\refg"
952 lappend x [gets $f line] $line [testchannel queuedcr $f]
953 close $f
954 set x
955 } [list 15 "123456789abcdef" 1 4 "abcd" 0]
956 test io-6.46 {Tcl_GetsObj: input saw cr, followed by just \n should give eof} {stdio testchannel fileevent} {
957 # memmove()
958
959 set f [open "|[list [interpreter] $path(cat)]" w+]
960 fconfigure $f -translation {auto lf} -buffering none
961 puts -nonewline $f "bbbbbbbbbbbbbbb\n123456789abcdef\r"
962 fconfigure $f -buffersize 16
963 gets $f
964 fconfigure $f -blocking 0
965 set x [list [gets $f line] $line [testchannel queuedcr $f]]
966 fconfigure $f -blocking 1
967 puts -nonewline $f "\n\x1A"
968 lappend x [gets $f line] $line [testchannel queuedcr $f]
969 close $f
970 set x
971 } [list 15 "123456789abcdef" 1 -1 "" 0]
972 test io-6.47 {Tcl_GetsObj: auto mode: \r at end of buffer, peek for \n} {testchannel} {
973 # (eol == dstEnd)
974
975 set f [open $path(test1) w]
976 fconfigure $f -translation lf
977 puts -nonewline $f "123456789012345\r\nabcdefghijklmnopq"
978 close $f
979 set f [open $path(test1)]
980 fconfigure $f -translation auto -buffersize 16
981 set x [list [gets $f] [testchannel inputbuffered $f]]
982 close $f
983 set x
984 } [list "123456789012345" 15]
985 test io-6.48 {Tcl_GetsObj: auto mode: \r at end of buffer, no more avail} {testchannel} {
986 # PeekAhead() did not get any, so (eol >= dstEnd)
987
988 set f [open $path(test1) w]
989 fconfigure $f -translation lf
990 puts -nonewline $f "123456789012345\r"
991 close $f
992 set f [open $path(test1)]
993 fconfigure $f -translation auto -buffersize 16
994 set x [list [gets $f] [testchannel queuedcr $f]]
995 close $f
996 set x
997 } [list "123456789012345" 1]
998 test io-6.49 {Tcl_GetsObj: auto mode: \r followed by \n} {testchannel} {
999 # if (*eol == '\n') {skip++}
1000
1001 set f [open $path(test1) w]
1002 fconfigure $f -translation lf
1003 puts -nonewline $f "123456\r\n78901"
1004 close $f
1005 set f [open $path(test1)]
1006 set x [list [gets $f] [testchannel queuedcr $f] [tell $f] [gets $f]]
1007 close $f
1008 set x
1009 } [list "123456" 0 8 "78901"]
1010 test io-6.50 {Tcl_GetsObj: auto mode: \r not followed by \n} {testchannel} {
1011 # not (*eol == '\n')
1012
1013 set f [open $path(test1) w]
1014 fconfigure $f -translation lf
1015 puts -nonewline $f "123456\r78901"
1016 close $f
1017 set f [open $path(test1)]
1018 set x [list [gets $f] [testchannel queuedcr $f] [tell $f] [gets $f]]
1019 close $f
1020 set x
1021 } [list "123456" 0 7 "78901"]
1022 test io-6.51 {Tcl_GetsObj: auto mode: \n} {
1023 # else if (*eol == '\n') {goto gotoeol;}
1024
1025 set f [open $path(test1) w]
1026 fconfigure $f -translation lf
1027 puts -nonewline $f "123456\n78901"
1028 close $f
1029 set f [open $path(test1)]
1030 set x [list [gets $f] [tell $f] [gets $f]]
1031 close $f
1032 set x
1033 } [list "123456" 7 "78901"]
1034 test io-6.52 {Tcl_GetsObj: saw EOF character} {testchannel} {
1035 # if (eof != NULL)
1036
1037 set f [open $path(test1) w]
1038 fconfigure $f -translation lf
1039 puts -nonewline $f "123456\x1Ak9012345\r"
1040 close $f
1041 set f [open $path(test1)]
1042 fconfigure $f -eofchar \x1A
1043 set x [list [gets $f] [testchannel queuedcr $f] [tell $f] [gets $f]]
1044 close $f
1045 set x
1046 } [list "123456" 0 6 ""]
1047 test io-6.53 {Tcl_GetsObj: device EOF} {
1048 # didn't produce any bytes
1049
1050 set f [open $path(test1) w]
1051 close $f
1052 set f [open $path(test1)]
1053 set x [list [gets $f line] $line [eof $f]]
1054 close $f
1055 set x
1056 } {-1 {} 1}
1057 test io-6.54 {Tcl_GetsObj: device EOF} {
1058 # got some bytes before EOF.
1059
1060 set f [open $path(test1) w]
1061 puts -nonewline $f abc
1062 close $f
1063 set f [open $path(test1)]
1064 set x [list [gets $f line] $line [eof $f]]
1065 close $f
1066 set x
1067 } {3 abc 1}
1068 test io-6.55 {Tcl_GetsObj: overconverted} {
1069 # Tcl_ExternalToUtf(), make sure state updated
1070
1071 set f [open $path(test1) w]
1072 fconfigure $f -encoding iso2022-jp
1073 puts $f "there\u4E00ok\n\u4E01more bytes\nhere"
1074 close $f
1075 set f [open $path(test1)]
1076 fconfigure $f -encoding iso2022-jp
1077 set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line]
1078 close $f
1079 set x
1080 } [list 8 "there\u4E00ok" 11 "\u4E01more bytes" 4 "here"]
1081 test io-6.56 {Tcl_GetsObj: incomplete lines should disable file events} {stdio fileevent} {
1082 update
1083 set f [open "|[list [interpreter] $path(cat)]" w+]
1084 fconfigure $f -buffering none
1085 puts -nonewline $f "foobar"
1086 fconfigure $f -blocking 0
1087 variable x {}
1088 after 500 [namespace code { lappend x timeout }]
1089 fileevent $f readable [namespace code { lappend x [gets $f] }]
1090 vwait [namespace which -variable x]
1091 vwait [namespace which -variable x]
1092 fconfigure $f -blocking 1
1093 puts -nonewline $f "baz\n"
1094 after 500 [namespace code { lappend x timeout }]
1095 fconfigure $f -blocking 0
1096 vwait [namespace which -variable x]
1097 vwait [namespace which -variable x]
1098 close $f
1099 set x
1100 } {{} timeout foobarbaz timeout}
1101
1102 test io-7.1 {FilterInputBytes: split up character at end of buffer} {
1103 # (result == TCL_CONVERT_MULTIBYTE)
1104
1105 set f [open $path(test1) w]
1106 fconfigure $f -encoding shiftjis
1107 puts $f "1234567890123\uFF10\uFF11\uFF12\uFF13\uFF14\nend"
1108 close $f
1109 set f [open $path(test1)]
1110 fconfigure $f -encoding shiftjis -buffersize 16
1111 set x [gets $f]
1112 close $f
1113 set x
1114 } "1234567890123\uFF10\uFF11\uFF12\uFF13\uFF14"
1115 test io-7.2 {FilterInputBytes: split up character in middle of buffer} {
1116 # (bufPtr->nextAdded < bufPtr->bufLength)
1117
1118 set f [open $path(test1) w]
1119 fconfigure $f -encoding binary
1120 puts -nonewline $f "1234567890\n123\x82\x4F\x82\x50\x82"
1121 close $f
1122 set f [open $path(test1)]
1123 fconfigure $f -encoding shiftjis
1124 set x [list [gets $f line] $line [eof $f]]
1125 close $f
1126 set x
1127 } [list 10 "1234567890" 0]
1128 test io-7.3 {FilterInputBytes: split up character at EOF} {testchannel} {
1129 set f [open $path(test1) w]
1130 fconfigure $f -encoding binary
1131 puts -nonewline $f "1234567890123\x82\x4F\x82\x50\x82"
1132 close $f
1133 set f [open $path(test1)]
1134 fconfigure $f -encoding shiftjis
1135 set x [list [gets $f line] $line]
1136 lappend x [tell $f] [testchannel inputbuffered $f] [eof $f]
1137 lappend x [gets $f line] $line
1138 close $f
1139 set x
1140 } [list 16 "1234567890123\uFF10\uFF11\x82" 18 0 1 -1 ""]
1141 test io-7.4 {FilterInputBytes: recover from split up character} {stdio fileevent} {
1142 set f [open "|[list [interpreter] $path(cat)]" w+]
1143 fconfigure $f -encoding binary -buffering none
1144 puts -nonewline $f "1234567890123\x82\x4F\x82\x50\x82"
1145 fconfigure $f -encoding shiftjis -blocking 0
1146 fileevent $f read [namespace code "ready $f"]
1147 variable x {}
1148 proc ready {f} {
1149 variable x
1150 lappend x [gets $f line] $line [fblocked $f]
1151 }
1152 vwait [namespace which -variable x]
1153 fconfigure $f -encoding binary -blocking 1
1154 puts $f "\x51\x82\x52"
1155 fconfigure $f -encoding shiftjis
1156 vwait [namespace which -variable x]
1157 close $f
1158 set x
1159 } [list -1 "" 1 17 "1234567890123\uFF10\uFF11\uFF12\uFF13" 0]
1160
1161 test io-8.1 {PeekAhead: only go to device if no more cached data} {testchannel} {
1162 # (bufPtr->nextPtr == NULL)
1163
1164 set f [open $path(test1) w]
1165 fconfigure $f -encoding ascii -translation lf
1166 puts -nonewline $f "123456789012345\r\n2345678"
1167 close $f
1168 set f [open $path(test1)]
1169 fconfigure $f -encoding ascii -translation auto -buffersize 16
1170 # here
1171 gets $f
1172 set x [testchannel inputbuffered $f]
1173 close $f
1174 set x
1175 } "7"
1176 test io-8.2 {PeekAhead: only go to device if no more cached data} {stdio testchannel fileevent} {
1177 # not (bufPtr->nextPtr == NULL)
1178
1179 set f [open "|[list [interpreter] $path(cat)]" w+]
1180 fconfigure $f -translation lf -encoding ascii -buffering none
1181 puts -nonewline $f "123456789012345\r\nbcdefghijklmnopqrstuvwxyz"
1182 variable x {}
1183 fileevent $f read [namespace code "ready $f"]
1184 proc ready {f} {
1185 variable x
1186 lappend x [gets $f line] $line [testchannel inputbuffered $f]
1187 }
1188 fconfigure $f -encoding unicode -buffersize 16 -blocking 0
1189 vwait [namespace which -variable x]
1190 fconfigure $f -translation auto -encoding ascii -blocking 1
1191 # here
1192 vwait [namespace which -variable x]
1193 close $f
1194 set x
1195 } [list -1 "" 42 15 "123456789012345" 25]
1196 test io-8.3 {PeekAhead: no cached data available} {stdio testchannel fileevent} {
1197 # (bytesLeft == 0)
1198
1199 set f [open "|[list [interpreter] $path(cat)]" w+]
1200 fconfigure $f -translation {auto binary}
1201 puts -nonewline $f "abcdefghijklmno\r"
1202 flush $f
1203 set x [list [gets $f line] $line [testchannel queuedcr $f]]
1204 close $f
1205 set x
1206 } [list 15 "abcdefghijklmno" 1]
1207 set a "123456789012345678901234567890"
1208 append a "123456789012345678901234567890"
1209 append a "1234567890123456789012345678901"
1210 test io-8.4 {PeekAhead: cached data available in this buffer} {
1211 # not (bytesLeft == 0)
1212
1213 set f [open $path(test1) w+]
1214 fconfigure $f -translation binary
1215 puts $f "${a}\r\nabcdef"
1216 close $f
1217 set f [open $path(test1)]
1218 fconfigure $f -encoding binary -translation auto
1219
1220 # "${a}\r" was converted in one operation (because ENCODING_LINESIZE
1221 # is 30). To check if "\n" follows, calls PeekAhead and determines
1222 # that cached data is available in buffer w/o having to call driver.
1223
1224 set x [gets $f]
1225 close $f
1226 set x
1227 } $a
1228 unset a
1229 test io-8.5 {PeekAhead: don't peek if last read was short} {stdio testchannel fileevent} {
1230 # (bufPtr->nextAdded < bufPtr->length)
1231
1232 set f [open "|[list [interpreter] $path(cat)]" w+]
1233 fconfigure $f -translation {auto binary}
1234 puts -nonewline $f "abcdefghijklmno\r"
1235 flush $f
1236 # here
1237 set x [list [gets $f line] $line [testchannel queuedcr $f]]
1238 close $f
1239 set x
1240 } {15 abcdefghijklmno 1}
1241 test io-8.6 {PeekAhead: change to non-blocking mode} {stdio testchannel fileevent} {
1242 # ((chanPtr->flags & CHANNEL_NONBLOCKING) == 0)
1243
1244 set f [open "|[list [interpreter] $path(cat)]" w+]
1245 fconfigure $f -translation {auto binary} -buffersize 16
1246 puts -nonewline $f "abcdefghijklmno\r"
1247 flush $f
1248 # here
1249 set x [list [gets $f line] $line [testchannel queuedcr $f]]
1250 close $f
1251 set x
1252 } {15 abcdefghijklmno 1}
1253 test io-8.7 {PeekAhead: cleanup} {stdio testchannel fileevent} {
1254 # Make sure bytes are removed from buffer.
1255
1256 set f [open "|[list [interpreter] $path(cat)]" w+]
1257 fconfigure $f -translation {auto binary} -buffering none
1258 puts -nonewline $f "abcdefghijklmno\r"
1259 # here
1260 set x [list [gets $f line] $line [testchannel queuedcr $f]]
1261 puts -nonewline $f "\x1A"
1262 lappend x [gets $f line] $line
1263 close $f
1264 set x
1265 } {15 abcdefghijklmno 1 -1 {}}
1266
1267 test io-9.1 {CommonGetsCleanup} emptyTest {
1268 } {}
1269
1270 test io-10.1 {Tcl_ReadChars: CheckChannelErrors} emptyTest {
1271 # no test, need to cause an async error.
1272 } {}
1273 test io-10.2 {Tcl_ReadChars: loop until enough copied} {
1274 # one time
1275 # for (copied = 0; (unsigned) toRead > 0; )
1276
1277 set f [open $path(test1) w]
1278 puts $f abcdefghijklmnop
1279 close $f
1280
1281 set f [open $path(test1)]
1282 set x [read $f 5]
1283 close $f
1284 set x
1285 } {abcde}
1286 test io-10.3 {Tcl_ReadChars: loop until enough copied} {
1287 # multiple times
1288 # for (copied = 0; (unsigned) toRead > 0; )
1289
1290 set f [open $path(test1) w]
1291 puts $f abcdefghijklmnopqrstuvwxyz
1292 close $f
1293
1294 set f [open $path(test1)]
1295 fconfigure $f -buffersize 16
1296 # here
1297 set x [read $f 19]
1298 close $f
1299 set x
1300 } {abcdefghijklmnopqrs}
1301 test io-10.4 {Tcl_ReadChars: no more in channel buffer} {
1302 # (copiedNow < 0)
1303
1304 set f [open $path(test1) w]
1305 puts -nonewline $f abcdefghijkl
1306 close $f
1307
1308 set f [open $path(test1)]
1309 # here
1310 set x [read $f 1000]
1311 close $f
1312 set x
1313 } {abcdefghijkl}
1314 test io-10.5 {Tcl_ReadChars: stop on EOF} {
1315 # (chanPtr->flags & CHANNEL_EOF)
1316
1317 set f [open $path(test1) w]
1318 puts -nonewline $f abcdefghijkl
1319 close $f
1320
1321 set f [open $path(test1)]
1322 # here
1323 set x [read $f 1000]
1324 close $f
1325 set x
1326 } {abcdefghijkl}
1327
1328 test io-11.1 {ReadBytes: want to read a lot} {
1329 # ((unsigned) toRead > (unsigned) srcLen)
1330
1331 set f [open $path(test1) w]
1332 puts -nonewline $f abcdefghijkl
1333 close $f
1334 set f [open $path(test1)]
1335 fconfigure $f -encoding binary
1336 # here
1337 set x [read $f 1000]
1338 close $f
1339 set x
1340 } {abcdefghijkl}
1341 test io-11.2 {ReadBytes: want to read all} {
1342 # ((unsigned) toRead > (unsigned) srcLen)
1343
1344 set f [open $path(test1) w]
1345 puts -nonewline $f abcdefghijkl
1346 close $f
1347 set f [open $path(test1)]
1348 fconfigure $f -encoding binary
1349 # here
1350 set x [read $f]
1351 close $f
1352 set x
1353 } {abcdefghijkl}
1354 test io-11.3 {ReadBytes: allocate more space} {
1355 # (toRead > length - offset - 1)
1356
1357 set f [open $path(test1) w]
1358 puts -nonewline $f abcdefghijklmnopqrstuvwxyz
1359 close $f
1360 set f [open $path(test1)]
1361 fconfigure $f -buffersize 16 -encoding binary
1362 # here
1363 set x [read $f]
1364 close $f
1365 set x
1366 } {abcdefghijklmnopqrstuvwxyz}
1367 test io-11.4 {ReadBytes: EOF char found} {
1368 # (TranslateInputEOL() != 0)
1369
1370 set f [open $path(test1) w]
1371 puts $f abcdefghijklmnopqrstuvwxyz
1372 close $f
1373 set f [open $path(test1)]
1374 fconfigure $f -eofchar m -encoding binary
1375 # here
1376 set x [list [read $f] [eof $f] [read $f] [eof $f]]
1377 close $f
1378 set x
1379 } [list "abcdefghijkl" 1 "" 1]
1380
1381 test io-12.1 {ReadChars: want to read a lot} {
1382 # ((unsigned) toRead > (unsigned) srcLen)
1383
1384 set f [open $path(test1) w]
1385 puts -nonewline $f abcdefghijkl
1386 close $f
1387 set f [open $path(test1)]
1388 # here
1389 set x [read $f 1000]
1390 close $f
1391 set x
1392 } {abcdefghijkl}
1393 test io-12.2 {ReadChars: want to read all} {
1394 # ((unsigned) toRead > (unsigned) srcLen)
1395
1396 set f [open $path(test1) w]
1397 puts -nonewline $f abcdefghijkl
1398 close $f
1399 set f [open $path(test1)]
1400 # here
1401 set x [read $f]
1402 close $f
1403 set x
1404 } {abcdefghijkl}
1405 test io-12.3 {ReadChars: allocate more space} {
1406 # (toRead > length - offset - 1)
1407
1408 set f [open $path(test1) w]
1409 puts -nonewline $f abcdefghijklmnopqrstuvwxyz
1410 close $f
1411 set f [open $path(test1)]
1412 fconfigure $f -buffersize 16
1413 # here
1414 set x [read $f]
1415 close $f
1416 set x
1417 } {abcdefghijklmnopqrstuvwxyz}
1418 test io-12.4 {ReadChars: split-up char} {stdio testchannel fileevent} {
1419 # (srcRead == 0)
1420
1421 set f [open "|[list [interpreter] $path(cat)]" w+]
1422 fconfigure $f -encoding binary -buffering none -buffersize 16
1423 puts -nonewline $f "123456789012345\x96"
1424 fconfigure $f -encoding shiftjis -blocking 0
1425
1426 fileevent $f read [namespace code "ready $f"]
1427 proc ready {f} {
1428 variable x
1429 lappend x [read $f] [testchannel inputbuffered $f]
1430 }
1431 variable x {}
1432
1433 fconfigure $f -encoding shiftjis
1434 vwait [namespace which -variable x]
1435 fconfigure $f -encoding binary -blocking 1
1436 puts -nonewline $f "\x7B"
1437 after 500 ;# Give the cat process time to catch up
1438 fconfigure $f -encoding shiftjis -blocking 0
1439 vwait [namespace which -variable x]
1440 close $f
1441 set x
1442 } [list "123456789012345" 1 "\u672C" 0]
1443 test io-12.5 {ReadChars: fileevents on partial characters} {stdio fileevent} {
1444 set path(test1) [makeFile {
1445 fconfigure stdout -encoding binary -buffering none
1446 gets stdin; puts -nonewline "\xE7"
1447 gets stdin; puts -nonewline "\x89"
1448 gets stdin; puts -nonewline "\xA6"
1449 } test1]
1450 set f [open "|[list [interpreter] $path(test1)]" r+]
1451 fileevent $f readable [namespace code {
1452 lappend x [read $f]
1453 if {[eof $f]} {
1454 lappend x eof
1455 }
1456 }]
1457 puts $f "go1"
1458 flush $f
1459 fconfigure $f -blocking 0 -encoding utf-8
1460 variable x {}
1461 vwait [namespace which -variable x]
1462 after 500 [namespace code { lappend x timeout }]
1463 vwait [namespace which -variable x]
1464 puts $f "go2"
1465 flush $f
1466 vwait [namespace which -variable x]
1467 after 500 [namespace code { lappend x timeout }]
1468 vwait [namespace which -variable x]
1469 puts $f "go3"
1470 flush $f
1471 vwait [namespace which -variable x]
1472 vwait [namespace which -variable x]
1473 lappend x [catch {close $f} msg] $msg
1474 set x
1475 } "{} timeout {} timeout \u7266 {} eof 0 {}"
1476 test io-12.6 {ReadChars: too many chars read} {
1477 proc driver {cmd args} {
1478 variable buffer
1479 variable index
1480 set chan [lindex $args 0]
1481 switch -- $cmd {
1482 initialize {
1483 set index($chan) 0
1484 set buffer($chan) [encoding convertto utf-8 \
1485 [string repeat \uBEEF 20][string repeat . 20]]
1486 return {initialize finalize watch read}
1487 }
1488 finalize {
1489 unset index($chan) buffer($chan)
1490 return
1491 }
1492 watch {}
1493 read {
1494 set n [lindex $args 1]
1495 set new [expr {$index($chan) + $n}]
1496 set result [string range $buffer($chan) $index($chan) $new-1]
1497 set index($chan) $new
1498 return $result
1499 }
1500 }
1501 }
1502 set c [chan create read [namespace which driver]]
1503 chan configure $c -encoding utf-8
1504 while {![eof $c]} {
1505 read $c 15
1506 }
1507 close $c
1508 } {}
1509 test io-12.7 {ReadChars: too many chars read [bc5b790099]} {
1510 proc driver {cmd args} {
1511 variable buffer
1512 variable index
1513 set chan [lindex $args 0]
1514 switch -- $cmd {
1515 initialize {
1516 set index($chan) 0
1517 set buffer($chan) [encoding convertto utf-8 \
1518 [string repeat \uBEEF 10]....\uBEEF]
1519 return {initialize finalize watch read}
1520 }
1521 finalize {
1522 unset index($chan) buffer($chan)
1523 return
1524 }
1525 watch {}
1526 read {
1527 set n [lindex $args 1]
1528 set new [expr {$index($chan) + $n}]
1529 set result [string range $buffer($chan) $index($chan) $new-1]
1530 set index($chan) $new
1531 return $result
1532 }
1533 }
1534 }
1535 set c [chan create read [namespace which driver]]
1536 chan configure $c -encoding utf-8
1537 while {![eof $c]} {
1538 read $c 7
1539 }
1540 close $c
1541 } {}
1542 test io-12.8 {ReadChars: multibyte chars split} {
1543 set f [open $path(test1) w]
1544 fconfigure $f -translation binary
1545 puts -nonewline $f [string repeat a 9]\xC2\xA0
1546 close $f
1547 set f [open $path(test1)]
1548 fconfigure $f -encoding utf-8 -buffersize 10
1549 set in [read $f]
1550 close $f
1551 scan [string index $in end] %c
1552 } 160
1553 test io-12.9 {ReadChars: multibyte chars split} -body {
1554 set f [open $path(test1) w]
1555 fconfigure $f -translation binary
1556 puts -nonewline $f [string repeat a 9]\xC2
1557 close $f
1558 set f [open $path(test1)]
1559 fconfigure $f -encoding utf-8 -buffersize 10
1560 set in [read $f]
1561 close $f
1562 scan [string index $in end] %c
1563 } -cleanup {
1564 catch {close $f}
1565 } -result 194
1566 test io-12.10 {ReadChars: multibyte chars split} -body {
1567 set f [open $path(test1) w]
1568 fconfigure $f -translation binary
1569 puts -nonewline $f [string repeat a 9]\xC2
1570 close $f
1571 set f [open $path(test1)]
1572 fconfigure $f -encoding utf-8 -buffersize 11
1573 set in [read $f]
1574 close $f
1575 scan [string index $in end] %c
1576 } -cleanup {
1577 catch {close $f}
1578 } -result 194
1579
1580 test io-13.1 {TranslateInputEOL: cr mode} {} {
1581 set f [open $path(test1) w]
1582 fconfigure $f -translation lf
1583 puts -nonewline $f "abcd\rdef\r"
1584 close $f
1585 set f [open $path(test1)]
1586 fconfigure $f -translation cr
1587 set x [read $f]
1588 close $f
1589 set x
1590 } "abcd\ndef\n"
1591 test io-13.2 {TranslateInputEOL: crlf mode} {
1592 set f [open $path(test1) w]
1593 fconfigure $f -translation lf
1594 puts -nonewline $f "abcd\r\ndef\r\n"
1595 close $f
1596 set f [open $path(test1)]
1597 fconfigure $f -translation crlf
1598 set x [read $f]
1599 close $f
1600 set x
1601 } "abcd\ndef\n"
1602 test io-13.3 {TranslateInputEOL: crlf mode: naked cr} {
1603 # (src >= srcMax)
1604
1605 set f [open $path(test1) w]
1606 fconfigure $f -translation lf
1607 puts -nonewline $f "abcd\r\ndef\r"
1608 close $f
1609 set f [open $path(test1)]
1610 fconfigure $f -translation crlf
1611 set x [read $f]
1612 close $f
1613 set x
1614 } "abcd\ndef\r"
1615 test io-13.4 {TranslateInputEOL: crlf mode: cr followed by not \n} {
1616 # (src >= srcMax)
1617
1618 set f [open $path(test1) w]
1619 fconfigure $f -translation lf
1620 puts -nonewline $f "abcd\r\ndef\rfgh"
1621 close $f
1622 set f [open $path(test1)]
1623 fconfigure $f -translation crlf
1624 set x [read $f]
1625 close $f
1626 set x
1627 } "abcd\ndef\rfgh"
1628 test io-13.5 {TranslateInputEOL: crlf mode: naked lf} {
1629 # (src >= srcMax)
1630
1631 set f [open $path(test1) w]
1632 fconfigure $f -translation lf
1633 puts -nonewline $f "abcd\r\ndef\nfgh"
1634 close $f
1635 set f [open $path(test1)]
1636 fconfigure $f -translation crlf
1637 set x [read $f]
1638 close $f
1639 set x
1640 } "abcd\ndef\nfgh"
1641 test io-13.6 {TranslateInputEOL: auto mode: saw cr in last segment} {stdio testchannel fileevent} {
1642 # (chanPtr->flags & INPUT_SAW_CR)
1643 # This test may fail on slower machines.
1644
1645 set f [open "|[list [interpreter] $path(cat)]" w+]
1646 fconfigure $f -blocking 0 -buffering none -translation {auto lf}
1647
1648 fileevent $f read [namespace code "ready $f"]
1649 proc ready {f} {
1650 variable x
1651 lappend x [read $f] [testchannel queuedcr $f]
1652 }
1653 variable x {}
1654 variable y {}
1655
1656 puts -nonewline $f "abcdefghj\r"
1657 after 500 [namespace code {set y ok}]
1658 vwait [namespace which -variable y]
1659
1660 puts -nonewline $f "\n01234"
1661 after 500 [namespace code {set y ok}]
1662 vwait [namespace which -variable y]
1663
1664 close $f
1665 set x
1666 } [list "abcdefghj\n" 1 "01234" 0]
1667 test io-13.7 {TranslateInputEOL: auto mode: naked \r} testchannel {
1668 # (src >= srcMax)
1669
1670 set f [open $path(test1) w]
1671 fconfigure $f -translation lf
1672 puts -nonewline $f "abcd\r"
1673 close $f
1674 set f [open $path(test1)]
1675 fconfigure $f -translation auto
1676 set x [list [read $f] [testchannel queuedcr $f]]
1677 close $f
1678 set x
1679 } [list "abcd\n" 1]
1680 test io-13.8 {TranslateInputEOL: auto mode: \r\n} {
1681 # (*src == '\n')
1682
1683 set f [open $path(test1) w]
1684 fconfigure $f -translation lf
1685 puts -nonewline $f "abcd\r\ndef"
1686 close $f
1687 set f [open $path(test1)]
1688 fconfigure $f -translation auto
1689 set x [read $f]
1690 close $f
1691 set x
1692 } "abcd\ndef"
1693 test io-13.8.1 {TranslateInputEOL: auto mode: \r\n} {
1694 set f [open $path(test1) w]
1695 fconfigure $f -translation lf
1696 puts -nonewline $f "abcd\r\ndef"
1697 close $f
1698 set f [open $path(test1)]
1699 fconfigure $f -translation auto
1700 set x {}
1701 lappend x [read $f 5]
1702 lappend x [read $f]
1703 close $f
1704 set x
1705 } [list "abcd\n" "def"]
1706 test io-13.8.2 {TranslateInputEOL: auto mode: \r\n} {
1707 set f [open $path(test1) w]
1708 fconfigure $f -translation lf
1709 puts -nonewline $f "abcd\r\ndef"
1710 close $f
1711 set f [open $path(test1)]
1712 fconfigure $f -translation auto -buffersize 6
1713 set x {}
1714 lappend x [read $f 5]
1715 lappend x [read $f]
1716 close $f
1717 set x
1718 } [list "abcd\n" "def"]
1719 test io-13.8.3 {TranslateInputEOL: auto mode: \r\n} {
1720 set f [open $path(test1) w]
1721 fconfigure $f -translation lf
1722 puts -nonewline $f "abcd\r\n\r\ndef"
1723 close $f
1724 set f [open $path(test1)]
1725 fconfigure $f -translation auto -buffersize 7
1726 set x {}
1727 lappend x [read $f 5]
1728 lappend x [read $f]
1729 close $f
1730 set x
1731 } [list "abcd\n" "\ndef"]
1732 test io-13.9 {TranslateInputEOL: auto mode: \r followed by not \n} {
1733 set f [open $path(test1) w]
1734 fconfigure $f -translation lf
1735 puts -nonewline $f "abcd\rdef"
1736 close $f
1737 set f [open $path(test1)]
1738 fconfigure $f -translation auto
1739 set x [read $f]
1740 close $f
1741 set x
1742 } "abcd\ndef"
1743 test io-13.10 {TranslateInputEOL: auto mode: \n} {
1744 # not (*src == '\r')
1745
1746 set f [open $path(test1) w]
1747 fconfigure $f -translation lf
1748 puts -nonewline $f "abcd\ndef"
1749 close $f
1750 set f [open $path(test1)]
1751 fconfigure $f -translation auto
1752 set x [read $f]
1753 close $f
1754 set x
1755 } "abcd\ndef"
1756 test io-13.11 {TranslateInputEOL: EOF char} {
1757 # (*chanPtr->inEofChar != '\x00')
1758
1759 set f [open $path(test1) w]
1760 fconfigure $f -translation lf
1761 puts -nonewline $f "abcd\ndefgh"
1762 close $f
1763 set f [open $path(test1)]
1764 fconfigure $f -translation auto -eofchar e
1765 set x [read $f]
1766 close $f
1767 set x
1768 } "abcd\nd"
1769 test io-13.12 {TranslateInputEOL: find EOF char in src} {
1770 # (*chanPtr->inEofChar != '\x00')
1771
1772 set f [open $path(test1) w]
1773 fconfigure $f -translation lf
1774 puts -nonewline $f "\r\n\r\n\r\nab\r\n\r\ndef\r\n\r\n\r\n"
1775 close $f
1776 set f [open $path(test1)]
1777 fconfigure $f -translation auto -eofchar e
1778 set x [read $f]
1779 close $f
1780 set x
1781 } "\n\n\nab\n\nd"
1782
1783 # Test standard handle management. The functions tested are
1784 # Tcl_SetStdChannel and Tcl_GetStdChannel. Incidentally we are
1785 # also testing channel table management.
1786
1787 if {[info commands testchannel] ne ""} {
1788 set consoleFileNames [lsort [testchannel open]]
1789 } else {
1790 # just to avoid an error
1791 set consoleFileNames [list]
1792 }
1793
1794 test io-14.1 {Tcl_SetStdChannel and Tcl_GetStdChannel} {testchannel} {
1795 set l ""
1796 lappend l [fconfigure stdin -buffering]
1797 lappend l [fconfigure stdout -buffering]
1798 lappend l [fconfigure stderr -buffering]
1799 lappend l [lsort [testchannel open]]
1800 set l
1801 } [list line line none $consoleFileNames]
1802 test io-14.2 {Tcl_SetStdChannel and Tcl_GetStdChannel} {
1803 interp create x
1804 set l ""
1805 lappend l [x eval {fconfigure stdin -buffering}]
1806 lappend l [x eval {fconfigure stdout -buffering}]
1807 lappend l [x eval {fconfigure stderr -buffering}]
1808 interp delete x
1809 set l
1810 } {line line none}
1811 set path(test3) [makeFile {} test3]
1812 test io-14.3 {Tcl_SetStdChannel & Tcl_GetStdChannel} exec {
1813 set f [open $path(test1) w]
1814 puts -nonewline $f {
1815 close stdin
1816 close stdout
1817 close stderr
1818 set f [}
1819 puts $f [list open $path(test1) r]]
1820 puts $f "set f2 \[[list open $path(test2) w]]"
1821 puts $f "set f3 \[[list open $path(test3) w]]"
1822 puts $f { puts stdout [gets stdin]
1823 puts stdout out
1824 puts stderr err
1825 close $f
1826 close $f2
1827 close $f3
1828 }
1829 close $f
1830 set result [exec [interpreter] $path(test1)]
1831 set f [open $path(test2) r]
1832 set f2 [open $path(test3) r]
1833 lappend result [read $f] [read $f2]
1834 close $f
1835 close $f2
1836 set result
1837 } {{
1838 out
1839 } {err
1840 }}
1841 # This test relies on the fact that stdout is used before stderr
1842 test io-14.4 {Tcl_SetStdChannel & Tcl_GetStdChannel} {exec} {
1843 set f [open $path(test1) w]
1844 puts -nonewline $f { close stdin
1845 close stdout
1846 close stderr
1847 set f [}
1848 puts $f [list open $path(test1) r]]
1849 puts $f "set f2 \[[list open $path(test2) w]]"
1850 puts $f "set f3 \[[list open $path(test3) w]]"
1851 puts $f { puts stdout [gets stdin]
1852 puts stdout $f2
1853 puts stderr $f3
1854 close $f
1855 close $f2
1856 close $f3
1857 }
1858 close $f
1859 set result [exec [interpreter] $path(test1)]
1860 set f [open $path(test2) r]
1861 set f2 [open $path(test3) r]
1862 lappend result [read $f] [read $f2]
1863 close $f
1864 close $f2
1865 set result
1866 } {{ close stdin
1867 stdout
1868 } {stderr
1869 }}
1870 catch {interp delete z}
1871 test io-14.5 {Tcl_GetChannel: stdio name translation} {
1872 interp create z
1873 eof stdin
1874 catch {z eval flush stdin} msg1
1875 catch {z eval close stdin} msg2
1876 catch {z eval flush stdin} msg3
1877 set result [list $msg1 $msg2 $msg3]
1878 interp delete z
1879 set result
1880 } {{channel "stdin" wasn't opened for writing} {} {can not find channel named "stdin"}}
1881 test io-14.6 {Tcl_GetChannel: stdio name translation} {
1882 interp create z
1883 eof stdout
1884 catch {z eval flush stdout} msg1
1885 catch {z eval close stdout} msg2
1886 catch {z eval flush stdout} msg3
1887 set result [list $msg1 $msg2 $msg3]
1888 interp delete z
1889 set result
1890 } {{} {} {can not find channel named "stdout"}}
1891 test io-14.7 {Tcl_GetChannel: stdio name translation} {
1892 interp create z
1893 eof stderr
1894 catch {z eval flush stderr} msg1
1895 catch {z eval close stderr} msg2
1896 catch {z eval flush stderr} msg3
1897 set result [list $msg1 $msg2 $msg3]
1898 interp delete z
1899 set result
1900 } {{} {} {can not find channel named "stderr"}}
1901 set path(script) [makeFile {} script]
1902 test io-14.8 {reuse of stdio special channels} stdio {
1903 file delete $path(script)
1904 file delete $path(test1)
1905 set f [open $path(script) w]
1906 puts -nonewline $f {
1907 close stderr
1908 set f [}
1909 puts $f [list open $path(test1) w]]
1910 puts -nonewline $f {
1911 puts stderr hello
1912 close $f
1913 set f [}
1914 puts $f [list open $path(test1) r]]
1915 puts $f {
1916 puts [gets $f]
1917 }
1918 close $f
1919 set f [open "|[list [interpreter] $path(script)]" r]
1920 set c [gets $f]
1921 close $f
1922 set c
1923 } hello
1924 test io-14.9 {reuse of stdio special channels} {stdio fileevent} {
1925 file delete $path(script)
1926 file delete $path(test1)
1927 set f [open $path(script) w]
1928 puts $f {
1929 array set path [lindex $argv 0]
1930 set f [open $path(test1) w]
1931 puts $f hello
1932 close $f
1933 close stderr
1934 set f [open "|[list [info nameofexecutable] $path(cat) $path(test1)]" r]
1935 puts [gets $f]
1936 }
1937 close $f
1938 set f [open "|[list [interpreter] $path(script) [array get path]]" r]
1939 set c [gets $f]
1940 close $f
1941 # Added delay to give Windows time to stop the spawned process and clean
1942 # up its grip on the file test1. Added delete as proper test cleanup.
1943 # The failing tests were 18.1 and 18.2 as first re-users of file "test1".
1944 after 10000
1945 file delete $path(script)
1946 file delete $path(test1)
1947 set c
1948 } hello
1949
1950 test io-15.1 {Tcl_CreateCloseHandler} emptyTest {
1951 } {}
1952
1953 test io-16.1 {Tcl_DeleteCloseHandler} emptyTest {
1954 } {}
1955
1956 # Test channel table management. The functions tested are
1957 # GetChannelTable, DeleteChannelTable, Tcl_RegisterChannel,
1958 # Tcl_UnregisterChannel, Tcl_GetChannel and Tcl_CreateChannel.
1959 #
1960 # These functions use "eof stdin" to ensure that the standard
1961 # channels are added to the channel table of the interpreter.
1962
1963 test io-17.1 {GetChannelTable, DeleteChannelTable on std handles} {testchannel} {
1964 set l1 [testchannel refcount stdin]
1965 eof stdin
1966 interp create x
1967 set l ""
1968 lappend l [expr {[testchannel refcount stdin] - $l1}]
1969 x eval {eof stdin}
1970 lappend l [expr {[testchannel refcount stdin] - $l1}]
1971 interp delete x
1972 lappend l [expr {[testchannel refcount stdin] - $l1}]
1973 set l
1974 } {0 1 0}
1975 test io-17.2 {GetChannelTable, DeleteChannelTable on std handles} {testchannel} {
1976 set l1 [testchannel refcount stdout]
1977 eof stdin
1978 interp create x
1979 set l ""
1980 lappend l [expr {[testchannel refcount stdout] - $l1}]
1981 x eval {eof stdout}
1982 lappend l [expr {[testchannel refcount stdout] - $l1}]
1983 interp delete x
1984 lappend l [expr {[testchannel refcount stdout] - $l1}]
1985 set l
1986 } {0 1 0}
1987 test io-17.3 {GetChannelTable, DeleteChannelTable on std handles} {testchannel} {
1988 set l1 [testchannel refcount stderr]
1989 eof stdin
1990 interp create x
1991 set l ""
1992 lappend l [expr {[testchannel refcount stderr] - $l1}]
1993 x eval {eof stderr}
1994 lappend l [expr {[testchannel refcount stderr] - $l1}]
1995 interp delete x
1996 lappend l [expr {[testchannel refcount stderr] - $l1}]
1997 set l
1998 } {0 1 0}
1999
2000 test io-18.1 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {testchannel} {
2001 file delete -force $path(test1)
2002 set l ""
2003 set f [open $path(test1) w]
2004 lappend l [lindex [testchannel info $f] 15]
2005 close $f
2006 if {[catch {lindex [testchannel info $f] 15} msg]} {
2007 lappend l $msg
2008 } else {
2009 lappend l "very broken: $f found after being closed"
2010 }
2011 string compare [string tolower $l] \
2012 [list 1 [format "can not find channel named \"%s\"" $f]]
2013 } 0
2014 test io-18.2 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {testchannel} {
2015 file delete -force $path(test1)
2016 set l ""
2017 set f [open $path(test1) w]
2018 lappend l [lindex [testchannel info $f] 15]
2019 interp create x
2020 interp share "" $f x
2021 lappend l [lindex [testchannel info $f] 15]
2022 x eval close $f
2023 lappend l [lindex [testchannel info $f] 15]
2024 interp delete x
2025 lappend l [lindex [testchannel info $f] 15]
2026 close $f
2027 if {[catch {lindex [testchannel info $f] 15} msg]} {
2028 lappend l $msg
2029 } else {
2030 lappend l "very broken: $f found after being closed"
2031 }
2032 string compare [string tolower $l] \
2033 [list 1 2 1 1 [format "can not find channel named \"%s\"" $f]]
2034 } 0
2035 test io-18.3 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {testchannel} {
2036 file delete $path(test1)
2037 set l ""
2038 set f [open $path(test1) w]
2039 lappend l [lindex [testchannel info $f] 15]
2040 interp create x
2041 interp share "" $f x
2042 lappend l [lindex [testchannel info $f] 15]
2043 interp delete x
2044 lappend l [lindex [testchannel info $f] 15]
2045 close $f
2046 if {[catch {lindex [testchannel info $f] 15} msg]} {
2047 lappend l $msg
2048 } else {
2049 lappend l "very broken: $f found after being closed"
2050 }
2051 string compare [string tolower $l] \
2052 [list 1 2 1 [format "can not find channel named \"%s\"" $f]]
2053 } 0
2054
2055 test io-19.1 {Tcl_GetChannel->Tcl_GetStdChannel, standard handles} {
2056 eof stdin
2057 } 0
2058 test io-19.2 {testing Tcl_GetChannel, user opened handle} {
2059 file delete $path(test1)
2060 set f [open $path(test1) w]
2061 set x [eof $f]
2062 close $f
2063 set x
2064 } 0
2065 test io-19.3 {Tcl_GetChannel, channel not found} {
2066 list [catch {eof file34} msg] $msg
2067 } {1 {can not find channel named "file34"}}
2068 test io-19.4 {Tcl_CreateChannel, insertion into channel table} {testchannel} {
2069 file delete $path(test1)
2070 set f [open $path(test1) w]
2071 set l ""
2072 lappend l [eof $f]
2073 close $f
2074 if {[catch {lindex [testchannel info $f] 15} msg]} {
2075 lappend l $msg
2076 } else {
2077 lappend l "very broken: $f found after being closed"
2078 }
2079 string compare [string tolower $l] \
2080 [list 0 [format "can not find channel named \"%s\"" $f]]
2081 } 0
2082
2083 test io-20.1 {Tcl_CreateChannel: initial settings} {
2084 set a [open $path(test2) w]
2085 set old [encoding system]
2086 encoding system ascii
2087 set f [open $path(test1) w]
2088 set x [fconfigure $f -encoding]
2089 close $f
2090 encoding system $old
2091 close $a
2092 set x
2093 } {ascii}
2094 test io-20.2 {Tcl_CreateChannel: initial settings} {win} {
2095 set f [open $path(test1) w+]
2096 set x [list [fconfigure $f -eofchar] [fconfigure $f -translation]]
2097 close $f
2098 set x
2099 } [list [list \x1A ""] {auto crlf}]
2100 test io-20.3 {Tcl_CreateChannel: initial settings} {unix} {
2101 set f [open $path(test1) w+]
2102 set x [list [fconfigure $f -eofchar] [fconfigure $f -translation]]
2103 close $f
2104 set x
2105 } {{{} {}} {auto lf}}
2106 set path(stdout) [makeFile {} stdout]
2107 test io-20.5 {Tcl_CreateChannel: install channel in empty slot} stdio {
2108 set f [open $path(script) w]
2109 puts -nonewline $f {
2110 close stdout
2111 set f1 [}
2112 puts $f [list open $path(stdout) w]]
2113 puts $f {
2114 fconfigure $f1 -buffersize 777
2115 puts stderr [fconfigure stdout -buffersize]
2116 }
2117 close $f
2118 set f [open "|[list [interpreter] $path(script)]"]
2119 catch {close $f} msg
2120 set msg
2121 } {777}
2122
2123 test io-21.1 {CloseChannelsOnExit} emptyTest {
2124 } {}
2125
2126 # Test management of attributes associated with a channel, such as
2127 # its default translation, its name and type, etc. The functions
2128 # tested in this group are Tcl_GetChannelName,
2129 # Tcl_GetChannelType and Tcl_GetChannelFile. Tcl_GetChannelInstanceData
2130 # not tested because files do not use the instance data.
2131
2132 test io-22.1 {Tcl_GetChannelMode} emptyTest {
2133 # Not used anywhere in Tcl.
2134 } {}
2135
2136 test io-23.1 {Tcl_GetChannelName} {testchannel} {
2137 file delete $path(test1)
2138 set f [open $path(test1) w]
2139 set n [testchannel name $f]
2140 close $f
2141 string compare $n $f
2142 } 0
2143
2144 test io-24.1 {Tcl_GetChannelType} {testchannel} {
2145 file delete $path(test1)
2146 set f [open $path(test1) w]
2147 set t [testchannel type $f]
2148 close $f
2149 string compare $t file
2150 } 0
2151
2152 test io-25.1 {Tcl_GetChannelHandle, input} {testchannel} {
2153 set f [open $path(test1) w]
2154 fconfigure $f -translation lf -eofchar {}
2155 puts $f "1234567890\n098765432"
2156 close $f
2157 set f [open $path(test1) r]
2158 gets $f
2159 set l ""
2160 lappend l [testchannel inputbuffered $f]
2161 lappend l [tell $f]
2162 close $f
2163 set l
2164 } {10 11}
2165 test io-25.2 {Tcl_GetChannelHandle, output} {testchannel} {
2166 file delete $path(test1)
2167 set f [open $path(test1) w]
2168 fconfigure $f -translation lf
2169 puts $f hello
2170 set l ""
2171 lappend l [testchannel outputbuffered $f]
2172 lappend l [tell $f]
2173 flush $f
2174 lappend l [testchannel outputbuffered $f]
2175 lappend l [tell $f]
2176 close $f
2177 file delete $path(test1)
2178 set l
2179 } {6 6 0 6}
2180
2181 test io-26.1 {Tcl_GetChannelInstanceData} stdio {
2182 # "pid" command uses Tcl_GetChannelInstanceData
2183 # Don't care what pid is (but must be a number), just want to exercise it.
2184
2185 set f [open "|[list [interpreter] << exit]"]
2186 expr {[pid $f]}
2187 close $f
2188 } {}
2189
2190 # Test flushing. The functions tested here are FlushChannel.
2191
2192 test io-27.1 {FlushChannel, no output buffered} {
2193 file delete $path(test1)
2194 set f [open $path(test1) w]
2195 flush $f
2196 set s [file size $path(test1)]
2197 close $f
2198 set s
2199 } 0
2200 test io-27.2 {FlushChannel, some output buffered} {
2201 file delete $path(test1)
2202 set f [open $path(test1) w]
2203 fconfigure $f -translation lf -eofchar {}
2204 set l ""
2205 puts $f hello
2206 lappend l [file size $path(test1)]
2207 flush $f
2208 lappend l [file size $path(test1)]
2209 close $f
2210 lappend l [file size $path(test1)]
2211 set l
2212 } {0 6 6}
2213 test io-27.3 {FlushChannel, implicit flush on close} {
2214 file delete $path(test1)
2215 set f [open $path(test1) w]
2216 fconfigure $f -translation lf -eofchar {}
2217 set l ""
2218 puts $f hello
2219 lappend l [file size $path(test1)]
2220 close $f
2221 lappend l [file size $path(test1)]
2222 set l
2223 } {0 6}
2224 test io-27.4 {FlushChannel, implicit flush when buffer fills} {
2225 file delete $path(test1)
2226 set f [open $path(test1) w]
2227 fconfigure $f -translation lf -eofchar {}
2228 fconfigure $f -buffersize 60
2229 set l ""
2230 lappend l [file size $path(test1)]
2231 for {set i 0} {$i < 12} {incr i} {
2232 puts $f hello
2233 }
2234 lappend l [file size $path(test1)]
2235 flush $f
2236 lappend l [file size $path(test1)]
2237 close $f
2238 set l
2239 } {0 60 72}
2240 test io-27.5 {FlushChannel, implicit flush when buffer fills and on close} \
2241 {unixOrWin} {
2242 file delete $path(test1)
2243 set f [open $path(test1) w]
2244 fconfigure $f -translation lf -buffersize 60 -eofchar {}
2245 set l ""
2246 lappend l [file size $path(test1)]
2247 for {set i 0} {$i < 12} {incr i} {
2248 puts $f hello
2249 }
2250 lappend l [file size $path(test1)]
2251 close $f
2252 lappend l [file size $path(test1)]
2253 set l
2254 } {0 60 72}
2255 set path(pipe) [makeFile {} pipe]
2256 set path(output) [makeFile {} output]
2257 test io-27.6 {FlushChannel, async flushing, async close} \
2258 {stdio asyncPipeClose notWinCI} {
2259 # This test may fail on old Unix systems (seen on IRIX64 6.5) with
2260 # obsolete gettimeofday() calls. See Tcl Bugs 3530533, 1942197.
2261 file delete $path(pipe)
2262 file delete $path(output)
2263 set f [open $path(pipe) w]
2264 puts $f "set f \[[list open $path(output) w]]"
2265 puts $f {
2266 fconfigure $f -translation lf -buffering none -eofchar {}
2267 while {![eof stdin]} {
2268 after 20
2269 puts -nonewline $f [read stdin 1024]
2270 }
2271 close $f
2272 }
2273 close $f
2274 set x 01234567890123456789012345678901
2275 for {set i 0} {$i < 11} {incr i} {
2276 set x "$x$x"
2277 }
2278 set f [open $path(output) w]
2279 close $f
2280 set f [open "|[list [interpreter] $path(pipe)]" w]
2281 fconfigure $f -blocking off
2282 puts -nonewline $f $x
2283 close $f
2284 set counter 0
2285 while {([file size $path(output)] < 65536) && ($counter < 1000)} {
2286 after 20 [list incr [namespace which -variable counter]]
2287 vwait [namespace which -variable counter]
2288 }
2289 if {$counter == 1000} {
2290 set result "file size only [file size $path(output)]"
2291 } else {
2292 set result ok
2293 }
2294 } ok
2295
2296 # Tests closing a channel. The functions tested are CloseChannel and Tcl_Close.
2297
2298 test io-28.1 {CloseChannel called when all references are dropped} {testchannel} {
2299 file delete $path(test1)
2300 set f [open $path(test1) w]
2301 interp create x
2302 interp share "" $f x
2303 set l ""
2304 lappend l [testchannel refcount $f]
2305 x eval close $f
2306 interp delete x
2307 lappend l [testchannel refcount $f]
2308 close $f
2309 set l
2310 } {2 1}
2311 test io-28.2 {CloseChannel called when all references are dropped} {
2312 file delete $path(test1)
2313 set f [open $path(test1) w]
2314 interp create x
2315 interp share "" $f x
2316 puts -nonewline $f abc
2317 close $f
2318 x eval puts $f def
2319 x eval close $f
2320 interp delete x
2321 set f [open $path(test1) r]
2322 set l [gets $f]
2323 close $f
2324 set l
2325 } abcdef
2326 test io-28.3 {CloseChannel, not called before output queue is empty} \
2327 {stdio asyncPipeClose nonPortable} {
2328 file delete $path(pipe)
2329 file delete $path(output)
2330 set f [open $path(pipe) w]
2331 puts $f {
2332
2333 # Need to not have eof char appended on close, because the other
2334 # side of the pipe already closed, so that writing would cause an
2335 # error "invalid file".
2336
2337 fconfigure stdout -eofchar {}
2338 fconfigure stderr -eofchar {}
2339
2340 set f [open $path(output) w]
2341 fconfigure $f -translation lf -buffering none
2342 for {set x 0} {$x < 20} {incr x} {
2343 after 20
2344 puts -nonewline $f [read stdin 1024]
2345 }
2346 close $f
2347 }
2348 close $f
2349 set x 01234567890123456789012345678901
2350 for {set i 0} {$i < 11} {incr i} {
2351 set x "$x$x"
2352 }
2353 set f [open $path(output) w]
2354 close $f
2355 set f [open "|[list [interpreter] pipe]" r+]
2356 fconfigure $f -blocking off -eofchar {}
2357
2358 puts -nonewline $f $x
2359 close $f
2360 set counter 0
2361 while {([file size $path(output)] < 20480) && ($counter < 1000)} {
2362 after 20 [list incr [namespace which -variable counter]]
2363 vwait [namespace which -variable counter]
2364 }
2365 if {$counter == 1000} {
2366 set result probably_broken
2367 } else {
2368 set result ok
2369 }
2370 } ok
2371 test io-28.4 Tcl_Close testchannel {
2372 file delete $path(test1)
2373 set l {}
2374 lappend l [lsort [testchannel open]]
2375 set f [open $path(test1) w]
2376 lappend l [lsort [testchannel open]]
2377 close $f
2378 lappend l [lsort [testchannel open]]
2379 set x [list $consoleFileNames \
2380 [lsort [list {*}$consoleFileNames $f]] \
2381 $consoleFileNames]
2382 string compare $l $x
2383 } 0
2384 test io-28.5 {Tcl_Close vs standard handles} {stdio unix testchannel} {
2385 file delete $path(script)
2386 set f [open $path(script) w]
2387 puts $f {
2388 close stdin
2389 puts [testchannel open]
2390 }
2391 close $f
2392 set f [open "|[list [interpreter] $path(script)]" r]
2393 set l [gets $f]
2394 close $f
2395 lsort $l
2396 } {file1 file2}
2397
2398 test io-29.1 {Tcl_WriteChars, channel not writable} {
2399 list [catch {puts stdin hello} msg] $msg
2400 } {1 {channel "stdin" wasn't opened for writing}}
2401 test io-29.2 {Tcl_WriteChars, empty string} {
2402 file delete $path(test1)
2403 set f [open $path(test1) w]
2404 fconfigure $f -eofchar {}
2405 puts -nonewline $f ""
2406 close $f
2407 file size $path(test1)
2408 } 0
2409 test io-29.3 {Tcl_WriteChars, nonempty string} {
2410 file delete $path(test1)
2411 set f [open $path(test1) w]
2412 fconfigure $f -eofchar {}
2413 puts -nonewline $f hello
2414 close $f
2415 file size $path(test1)
2416 } 5
2417 test io-29.4 {Tcl_WriteChars, buffering in full buffering mode} {testchannel} {
2418 file delete $path(test1)
2419 set f [open $path(test1) w]
2420 fconfigure $f -translation lf -buffering full -eofchar {}
2421 puts $f hello
2422 set l ""
2423 lappend l [testchannel outputbuffered $f]
2424 lappend l [file size $path(test1)]
2425 flush $f
2426 lappend l [testchannel outputbuffered $f]
2427 lappend l [file size $path(test1)]
2428 close $f
2429 set l
2430 } {6 0 0 6}
2431 test io-29.5 {Tcl_WriteChars, buffering in line buffering mode} {testchannel} {
2432 file delete $path(test1)
2433 set f [open $path(test1) w]
2434 fconfigure $f -translation lf -buffering line -eofchar {}
2435 puts -nonewline $f hello
2436 set l ""
2437 lappend l [testchannel outputbuffered $f]
2438 lappend l [file size $path(test1)]
2439 puts $f hello
2440 lappend l [testchannel outputbuffered $f]
2441 lappend l [file size $path(test1)]
2442 close $f
2443 set l
2444 } {5 0 0 11}
2445 test io-29.6 {Tcl_WriteChars, buffering in no buffering mode} {testchannel} {
2446 file delete $path(test1)
2447 set f [open $path(test1) w]
2448 fconfigure $f -translation lf -buffering none -eofchar {}
2449 puts -nonewline $f hello
2450 set l ""
2451 lappend l [testchannel outputbuffered $f]
2452 lappend l [file size $path(test1)]
2453 puts $f hello
2454 lappend l [testchannel outputbuffered $f]
2455 lappend l [file size $path(test1)]
2456 close $f
2457 set l
2458 } {0 5 0 11}
2459 test io-29.7 {Tcl_Flush, full buffering} {testchannel} {
2460 file delete $path(test1)
2461 set f [open $path(test1) w]
2462 fconfigure $f -translation lf -buffering full -eofchar {}
2463 puts -nonewline $f hello
2464 set l ""
2465 lappend l [testchannel outputbuffered $f]
2466 lappend l [file size $path(test1)]
2467 puts $f hello
2468 lappend l [testchannel outputbuffered $f]
2469 lappend l [file size $path(test1)]
2470 flush $f
2471 lappend l [testchannel outputbuffered $f]
2472 lappend l [file size $path(test1)]
2473 close $f
2474 set l
2475 } {5 0 11 0 0 11}
2476 test io-29.8 {Tcl_Flush, full buffering} {testchannel} {
2477 file delete $path(test1)
2478 set f [open $path(test1) w]
2479 fconfigure $f -translation lf -buffering line
2480 puts -nonewline $f hello
2481 set l ""
2482 lappend l [testchannel outputbuffered $f]
2483 lappend l [file size $path(test1)]
2484 flush $f
2485 lappend l [testchannel outputbuffered $f]
2486 lappend l [file size $path(test1)]
2487 puts $f hello
2488 lappend l [testchannel outputbuffered $f]
2489 lappend l [file size $path(test1)]
2490 flush $f
2491 lappend l [testchannel outputbuffered $f]
2492 lappend l [file size $path(test1)]
2493 close $f
2494 set l
2495 } {5 0 0 5 0 11 0 11}
2496 test io-29.9 {Tcl_Flush, channel not writable} {
2497 list [catch {flush stdin} msg] $msg
2498 } {1 {channel "stdin" wasn't opened for writing}}
2499 test io-29.10 {Tcl_WriteChars, looping and buffering} {
2500 file delete $path(test1)
2501 set f1 [open $path(test1) w]
2502 fconfigure $f1 -translation lf -eofchar {}
2503 set f2 [open $path(longfile) r]
2504 for {set x 0} {$x < 10} {incr x} {
2505 puts $f1 [gets $f2]
2506 }
2507 close $f2
2508 close $f1
2509 file size $path(test1)
2510 } 387
2511 test io-29.11 {Tcl_WriteChars, no newline, implicit flush} {
2512 file delete $path(test1)
2513 set f1 [open $path(test1) w]
2514 fconfigure $f1 -eofchar {}
2515 set f2 [open $path(longfile) r]
2516 for {set x 0} {$x < 10} {incr x} {
2517 puts -nonewline $f1 [gets $f2]
2518 }
2519 close $f1
2520 close $f2
2521 file size $path(test1)
2522 } 377
2523 test io-29.12 {Tcl_WriteChars on a pipe} stdio {
2524 file delete $path(test1)
2525 file delete $path(pipe)
2526 set f1 [open $path(pipe) w]
2527 puts $f1 "set f1 \[[list open $path(longfile) r]]"
2528 puts $f1 {
2529 for {set x 0} {$x < 10} {incr x} {
2530 puts [gets $f1]
2531 }
2532 }
2533 close $f1
2534 set f1 [open "|[list [interpreter] $path(pipe)]" r]
2535 set f2 [open $path(longfile) r]
2536 set y ok
2537 for {set x 0} {$x < 10} {incr x} {
2538 set l1 [gets $f1]
2539 set l2 [gets $f2]
2540 if {"$l1" != "$l2"} {
2541 set y broken
2542 }
2543 }
2544 close $f1
2545 close $f2
2546 set y
2547 } ok
2548 test io-29.13 {Tcl_WriteChars to a pipe, line buffered} stdio {
2549 file delete $path(test1)
2550 file delete $path(pipe)
2551 set f1 [open $path(pipe) w]
2552 puts $f1 {
2553 puts [gets stdin]
2554 puts [gets stdin]
2555 }
2556 close $f1
2557 set y ok
2558 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
2559 fconfigure $f1 -buffering line
2560 set f2 [open $path(longfile) r]
2561 set line [gets $f2]
2562 puts $f1 $line
2563 set backline [gets $f1]
2564 if {"$line" != "$backline"} {
2565 set y broken
2566 }
2567 set line [gets $f2]
2568 puts $f1 $line
2569 set backline [gets $f1]
2570 if {"$line" != "$backline"} {
2571 set y broken
2572 }
2573 close $f1
2574 close $f2
2575 set y
2576 } ok
2577 test io-29.14 {Tcl_WriteChars, buffering and implicit flush at close} {
2578 file delete $path(test3)
2579 set f [open $path(test3) w]
2580 puts -nonewline $f "Text1"
2581 puts -nonewline $f " Text 2"
2582 puts $f " Text 3"
2583 close $f
2584 set f [open $path(test3) r]
2585 set x [gets $f]
2586 close $f
2587 set x
2588 } {Text1 Text 2 Text 3}
2589 test io-29.15 {Tcl_Flush, channel not open for writing} {
2590 file delete $path(test1)
2591 set fd [open $path(test1) w]
2592 close $fd
2593 set fd [open $path(test1) r]
2594 set x [list [catch {flush $fd} msg] $msg]
2595 close $fd
2596 string compare $x \
2597 [list 1 "channel \"$fd\" wasn't opened for writing"]
2598 } 0
2599 test io-29.16 {Tcl_Flush on pipe opened only for reading} stdio {
2600 set fd [open "|[list [interpreter] cat longfile]" r]
2601 set x [list [catch {flush $fd} msg] $msg]
2602 catch {close $fd}
2603 string compare $x \
2604 [list 1 "channel \"$fd\" wasn't opened for writing"]
2605 } 0
2606 test io-29.17 {Tcl_WriteChars buffers, then Tcl_Flush flushes} {
2607 file delete $path(test1)
2608 set f1 [open $path(test1) w]
2609 fconfigure $f1 -translation lf
2610 puts $f1 hello
2611 puts $f1 hello
2612 puts $f1 hello
2613 flush $f1
2614 set x [file size $path(test1)]
2615 close $f1
2616 set x
2617 } 18
2618 test io-29.18 {Tcl_WriteChars and Tcl_Flush intermixed} {
2619 file delete $path(test1)
2620 set x ""
2621 set f1 [open $path(test1) w]
2622 fconfigure $f1 -translation lf
2623 puts $f1 hello
2624 puts $f1 hello
2625 puts $f1 hello
2626 flush $f1
2627 lappend x [file size $path(test1)]
2628 puts $f1 hello
2629 flush $f1
2630 lappend x [file size $path(test1)]
2631 puts $f1 hello
2632 flush $f1
2633 lappend x [file size $path(test1)]
2634 close $f1
2635 set x
2636 } {18 24 30}
2637 test io-29.19 {Explicit and implicit flushes} {
2638 file delete $path(test1)
2639 set f1 [open $path(test1) w]
2640 fconfigure $f1 -translation lf -eofchar {}
2641 set x ""
2642 puts $f1 hello
2643 puts $f1 hello
2644 puts $f1 hello
2645 flush $f1
2646 lappend x [file size $path(test1)]
2647 puts $f1 hello
2648 flush $f1
2649 lappend x [file size $path(test1)]
2650 puts $f1 hello
2651 close $f1
2652 lappend x [file size $path(test1)]
2653 set x
2654 } {18 24 30}
2655 test io-29.20 {Implicit flush when buffer is full} {
2656 file delete $path(test1)
2657 set f1 [open $path(test1) w]
2658 fconfigure $f1 -translation lf -eofchar {}
2659 set line "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
2660 for {set x 0} {$x < 100} {incr x} {
2661 puts $f1 $line
2662 }
2663 set z ""
2664 lappend z [file size $path(test1)]
2665 for {set x 0} {$x < 100} {incr x} {
2666 puts $f1 $line
2667 }
2668 lappend z [file size $path(test1)]
2669 close $f1
2670 lappend z [file size $path(test1)]
2671 set z
2672 } {4096 12288 12600}
2673 test io-29.21 {Tcl_Flush to pipe} stdio {
2674 file delete $path(pipe)
2675 set f1 [open $path(pipe) w]
2676 puts $f1 {set x [read stdin 6]}
2677 puts $f1 {set cnt [string length $x]}
2678 puts $f1 {puts "read $cnt characters"}
2679 close $f1
2680 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
2681 puts $f1 hello
2682 flush $f1
2683 set x [gets $f1]
2684 catch {close $f1}
2685 set x
2686 } "read 6 characters"
2687 test io-29.22 {Tcl_Flush called at other end of pipe} stdio {
2688 file delete $path(pipe)
2689 set f1 [open $path(pipe) w]
2690 puts $f1 {
2691 fconfigure stdout -buffering full
2692 puts hello
2693 puts hello
2694 flush stdout
2695 gets stdin
2696 puts bye
2697 flush stdout
2698 }
2699 close $f1
2700 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
2701 set x ""
2702 lappend x [gets $f1]
2703 lappend x [gets $f1]
2704 puts $f1 hello
2705 flush $f1
2706 lappend x [gets $f1]
2707 close $f1
2708 set x
2709 } {hello hello bye}
2710 test io-29.23 {Tcl_Flush and line buffering at end of pipe} stdio {
2711 file delete $path(pipe)
2712 set f1 [open $path(pipe) w]
2713 puts $f1 {
2714 puts hello
2715 puts hello
2716 gets stdin
2717 puts bye
2718 }
2719 close $f1
2720 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
2721 set x ""
2722 lappend x [gets $f1]
2723 lappend x [gets $f1]
2724 puts $f1 hello
2725 flush $f1
2726 lappend x [gets $f1]
2727 close $f1
2728 set x
2729 } {hello hello bye}
2730 test io-29.24 {Tcl_WriteChars and Tcl_Flush move end of file} {
2731 set f [open $path(test3) w]
2732 puts $f "Line 1"
2733 puts $f "Line 2"
2734 set f2 [open $path(test3)]
2735 set x {}
2736 lappend x [read -nonewline $f2]
2737 close $f2
2738 flush $f
2739 set f2 [open $path(test3)]
2740 lappend x [read -nonewline $f2]
2741 close $f2
2742 close $f
2743 set x
2744 } "{} {Line 1\nLine 2}"
2745 test io-29.25 {Implicit flush with Tcl_Flush to command pipelines} {stdio fileevent} {
2746 file delete $path(test3)
2747 set f [open "|[list [interpreter] $path(cat) | [interpreter] $path(cat) > $path(test3)]" w]
2748 puts $f "Line 1"
2749 puts $f "Line 2"
2750 close $f
2751 after 100
2752 set f [open $path(test3) r]
2753 set x [read $f]
2754 close $f
2755 set x
2756 } "Line 1\nLine 2\n"
2757 test io-29.26 {Tcl_Flush, Tcl_Write on bidirectional pipelines} {stdio unixExecs} {
2758 set f [open "|[list cat -u]" r+]
2759 puts $f "Line1"
2760 flush $f
2761 set x [gets $f]
2762 close $f
2763 set x
2764 } {Line1}
2765 test io-29.27 {Tcl_Flush on closed pipeline} stdio {
2766 file delete $path(pipe)
2767 set f [open $path(pipe) w]
2768 puts $f {exit}
2769 close $f
2770 set f [open "|[list [interpreter] $path(pipe)]" r+]
2771 gets $f
2772 puts $f output
2773 after 50
2774 #
2775 # The flush below will get a SIGPIPE. This is an expected part of
2776 # test and indicates that the test operates correctly. If you run
2777 # this test under a debugger, the signal will by intercepted unless
2778 # you disable the debugger's signal interception.
2779 #
2780 if {[catch {flush $f} msg]} {
2781 set x [list 1 $msg $::errorCode]
2782 catch {close $f}
2783 } else {
2784 if {[catch {close $f} msg]} {
2785 set x [list 1 $msg $::errorCode]
2786 } else {
2787 set x {this was supposed to fail and did not}
2788 }
2789 }
2790 regsub {".*":} $x {"":} x
2791 string tolower $x
2792 } {1 {error flushing "": broken pipe} {posix epipe {broken pipe}}}
2793 test io-29.28 {Tcl_WriteChars, lf mode} {
2794 file delete $path(test1)
2795 set f [open $path(test1) w]
2796 fconfigure $f -translation lf -eofchar {}
2797 puts $f hello\nthere\nand\nhere
2798 flush $f
2799 set s [file size $path(test1)]
2800 close $f
2801 set s
2802 } 21
2803 test io-29.29 {Tcl_WriteChars, cr mode} {
2804 file delete $path(test1)
2805 set f [open $path(test1) w]
2806 fconfigure $f -translation cr -eofchar {}
2807 puts $f hello\nthere\nand\nhere
2808 close $f
2809 file size $path(test1)
2810 } 21
2811 test io-29.30 {Tcl_WriteChars, crlf mode} {
2812 file delete $path(test1)
2813 set f [open $path(test1) w]
2814 fconfigure $f -translation crlf -eofchar {}
2815 puts $f hello\nthere\nand\nhere
2816 close $f
2817 file size $path(test1)
2818 } 25
2819 test io-29.31 {Tcl_WriteChars, background flush} stdio {
2820 # This test may fail on old Unix systems (seen on IRIX64 6.5) with
2821 # obsolete gettimeofday() calls. See Tcl Bugs 3530533, 1942197.
2822 file delete $path(pipe)
2823 file delete $path(output)
2824 set f [open $path(pipe) w]
2825 puts $f "set f \[[list open $path(output) w]]"
2826 puts $f {fconfigure $f -translation lf}
2827 set x [list while {![eof stdin]}]
2828 set x "$x {"
2829 puts $f $x
2830 puts $f { puts -nonewline $f [read stdin 4096]}
2831 puts $f { flush $f}
2832 puts $f "}"
2833 puts $f {close $f}
2834 close $f
2835 set x 01234567890123456789012345678901
2836 for {set i 0} {$i < 11} {incr i} {
2837 set x "$x$x"
2838 }
2839 set f [open $path(output) w]
2840 close $f
2841 set f [open "|[list [interpreter] $path(pipe)]" r+]
2842 fconfigure $f -blocking off
2843 puts -nonewline $f $x
2844 close $f
2845 set counter 0
2846 while {([file size $path(output)] < 65536) && ($counter < 1000)} {
2847 after 10 [list incr [namespace which -variable counter]]
2848 vwait [namespace which -variable counter]
2849 }
2850 if {$counter == 1000} {
2851 set result "file size only [file size $path(output)]"
2852 } else {
2853 set result ok
2854 }
2855 # allow a little time for the background process to close.
2856 # otherwise, the following test fails on the [file delete $path(output)]
2857 # on Windows because a process still has the file open.
2858 after 100 set v 1; vwait v
2859 set result
2860 } ok
2861 test io-29.32 {Tcl_WriteChars, background flush to slow reader} \
2862 {stdio asyncPipeClose notWinCI} {
2863 # This test may fail on old Unix systems (seen on IRIX64 6.5) with
2864 # obsolete gettimeofday() calls. See Tcl Bugs 3530533, 1942197.
2865 file delete $path(pipe)
2866 file delete $path(output)
2867 set f [open $path(pipe) w]
2868 puts $f "set f \[[list open $path(output) w]]"
2869 puts $f {fconfigure $f -translation lf}
2870 set x [list while {![eof stdin]}]
2871 set x "$x \{"
2872 puts $f $x
2873 puts $f { after 20}
2874 puts $f { puts -nonewline $f [read stdin 1024]}
2875 puts $f { flush $f}
2876 puts $f "\}"
2877 puts $f {close $f}
2878 close $f
2879 set x 01234567890123456789012345678901
2880 for {set i 0} {$i < 11} {incr i} {
2881 set x "$x$x"
2882 }
2883 set f [open $path(output) w]
2884 close $f
2885 set f [open "|[list [interpreter] $path(pipe)]" r+]
2886 fconfigure $f -blocking off
2887 puts -nonewline $f $x
2888 close $f
2889 set counter 0
2890 while {([file size $path(output)] < 65536) && ($counter < 1000)} {
2891 after 20 [list incr [namespace which -variable counter]]
2892 vwait [namespace which -variable counter]
2893 }
2894 if {$counter == 1000} {
2895 set result "file size only [file size $path(output)]"
2896 } else {
2897 set result ok
2898 }
2899 } ok
2900 test io-29.33 {Tcl_Flush, implicit flush on exit} {exec} {
2901 set f [open $path(script) w]
2902 puts $f "set f \[[list open $path(test1) w]]"
2903 puts $f {fconfigure $f -translation lf
2904 puts $f hello
2905 puts $f bye
2906 puts $f strange
2907 }
2908 close $f
2909 exec [interpreter] $path(script)
2910 set f [open $path(test1) r]
2911 set r [read $f]
2912 close $f
2913 set r
2914 } "hello\nbye\nstrange\n"
2915 set path(script2) [makeFile {} script2]
2916 test io-29.33b {TIP#398, no implicit flush of nonblocking on exit} {exec} {
2917 set f [open $path(script) w]
2918 puts $f {
2919 fconfigure stdout -blocking 0
2920 puts -nonewline stdout [string repeat A 655360]
2921 flush stdout
2922 }
2923 close $f
2924 set f [open $path(script2) w]
2925 puts $f {after 2000}
2926 close $f
2927 set t1 [clock milliseconds]
2928 set ff [open "|[list [interpreter] $path(script2)]" w]
2929 catch {unset ::env(TCL_FLUSH_NONBLOCKING_ON_EXIT)}
2930 exec [interpreter] $path(script) >@ $ff
2931 set t2 [clock milliseconds]
2932 close $ff
2933 expr {($t2-$t1)/2000 ? $t2-$t1 : 0}
2934 } 0
2935 test io-29.34 {Tcl_Close, async flush on close, using sockets} {socket tempNotMac fileevent} {
2936 variable c 0
2937 variable x running
2938 set l abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
2939 proc writelots {s l} {
2940 for {set i 0} {$i < 9000} {incr i} {
2941 puts $s $l
2942 }
2943 }
2944 proc accept {s a p} {
2945 variable x
2946 fileevent $s readable [namespace code [list readit $s]]
2947 fconfigure $s -blocking off
2948 set x accepted
2949 }
2950 proc readit {s} {
2951 variable c
2952 variable x
2953 set l [gets $s]
2954
2955 if {[eof $s]} {
2956 close $s
2957 set x done
2958 } elseif {([string length $l] > 0) || ![fblocked $s]} {
2959 incr c
2960 }
2961 }
2962 set ss [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
2963 set cs [socket 127.0.0.1 [lindex [fconfigure $ss -sockname] 2]]
2964 vwait [namespace which -variable x]
2965 fconfigure $cs -blocking off
2966 writelots $cs $l
2967 close $cs
2968 close $ss
2969 vwait [namespace which -variable x]
2970 set c
2971 } 9000
2972 test io-29.35 {Tcl_Close vs fileevent vs multiple interpreters} {socket tempNotMac fileevent} {
2973 # On Mac, this test screws up sockets such that subsequent tests using port 2828
2974 # either cause errors or panic().
2975
2976 catch {interp delete x}
2977 catch {interp delete y}
2978 interp create x
2979 interp create y
2980 set s [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
2981 proc accept {s a p} {
2982 puts $s hello
2983 close $s
2984 }
2985 set c [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]]
2986 interp share {} $c x
2987 interp share {} $c y
2988 close $c
2989 x eval {
2990 proc readit {s} {
2991 gets $s
2992 if {[eof $s]} {
2993 close $s
2994 }
2995 }
2996 }
2997 y eval {
2998 proc readit {s} {
2999 gets $s
3000 if {[eof $s]} {
3001 close $s
3002 }
3003 }
3004 }
3005 x eval "fileevent $c readable \{readit $c\}"
3006 y eval "fileevent $c readable \{readit $c\}"
3007 y eval [list close $c]
3008 update
3009 close $s
3010 interp delete x
3011 interp delete y
3012 } ""
3013
3014 test io-29.36.1 {gets on translation auto with "\r" in QA communication mode, possible regression, bug [b3977d199b]} -constraints {
3015 socket tempNotMac fileevent
3016 } -setup {
3017 set s [open "|[list [interpreter] << {
3018 proc accept {so args} {
3019 fconfigure $so -translation binary
3020 puts -nonewline $so "who are you?\r"; flush $so
3021 set a [gets $so]
3022 puts -nonewline $so "really $a?\r"; flush $so
3023 set a [gets $so]
3024 close $so
3025 set ::done $a
3026 }
3027 set s [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
3028 puts [lindex [fconfigure $s -sockname] 2]
3029 foreach c {1 2} {
3030 vwait ::done
3031 puts $::done
3032 }
3033 }]" r]
3034 set c {}
3035 set result {}
3036 } -body {
3037 set port [gets $s]
3038 foreach t {{cr lf} {auto lf}} {
3039 set c [socket 127.0.0.1 $port]
3040 fconfigure $c -buffering line -translation $t
3041 lappend result $t
3042 while {1} {
3043 set q [gets $c]
3044 switch -- $q {
3045 "who are you?" {puts $c "client"}
3046 "really client?" {puts $c "yes"; lappend result $q; break}
3047 default {puts $c "wrong"; lappend result "unexpected input \"$q\""; break}
3048 }
3049 }
3050 lappend result [gets $s]
3051 close $c; set c {}
3052 }
3053 set result
3054 } -cleanup {
3055 close $s
3056 if {$c ne {}} { close $c }
3057 unset -nocomplain s c port t q
3058 } -result [list {cr lf} "really client?" yes {auto lf} "really client?" yes]
3059 test io-29.36.2 {gets on translation auto with "\r\n" in different buffers, bug [b3977d199b]} -constraints {
3060 socket tempNotMac fileevent
3061 } -setup {
3062 set s [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
3063 set c {}
3064 } -body {
3065 set ::cnt 0
3066 proc accept {so args} {
3067 fconfigure $so -translation binary
3068 puts -nonewline $so "1 line\r"
3069 puts -nonewline $so "\n2 li"
3070 flush $so
3071 # now force separate packets
3072 puts -nonewline $so "ne\r"
3073 flush $so
3074 if {$::cnt & 1} {
3075 vwait ::cli; # simulate short delay (so client can process events, just wait for it)
3076 } else {
3077 # we don't have a delay, so client would get the lines as single chunk
3078 }
3079 # we'll try with "\r" and without "\r" (to cover both branches, where "\r" and "eof" causes exit from [gets] by 3rd line)
3080 puts -nonewline $so "\n3 line"
3081 if {!($::cnt % 3)} {
3082 puts -nonewline $so "\r"
3083 }
3084 flush $so
3085 close $so
3086 }
3087 while {$::cnt < 6} { incr ::cnt
3088 set c [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]]
3089 fconfigure $c -blocking 0 -buffering line -translation auto
3090 fileevent $c readable [list apply {c {
3091 if {[gets $c line] >= 0} {
3092 lappend ::cli <$line>
3093 } elseif {[eof $c]} {
3094 set ::done 1
3095 }
3096 }} $c]
3097 vwait ::done
3098 close $c; set c {}
3099 }
3100 set ::cli
3101 } -cleanup {
3102 close $s
3103 if {$c ne {}} { close $c }
3104 unset -nocomplain ::done ::cli ::cnt s c
3105 } -result [lrepeat 6 {<1 line>} {<2 line>} {<3 line>}]
3106
3107 # Test end of line translations. Procedures tested are Tcl_Write, Tcl_Read.
3108
3109 test io-30.1 {Tcl_Write lf, Tcl_Read lf} {
3110 file delete $path(test1)
3111 set f [open $path(test1) w]
3112 fconfigure $f -translation lf
3113 puts $f hello\nthere\nand\nhere
3114 close $f
3115 set f [open $path(test1) r]
3116 fconfigure $f -translation lf
3117 set x [read $f]
3118 close $f
3119 set x
3120 } "hello\nthere\nand\nhere\n"
3121 test io-30.2 {Tcl_Write lf, Tcl_Read cr} {
3122 file delete $path(test1)
3123 set f [open $path(test1) w]
3124 fconfigure $f -translation lf
3125 puts $f hello\nthere\nand\nhere
3126 close $f
3127 set f [open $path(test1) r]
3128 fconfigure $f -translation cr
3129 set x [read $f]
3130 close $f
3131 set x
3132 } "hello\nthere\nand\nhere\n"
3133 test io-30.3 {Tcl_Write lf, Tcl_Read crlf} {
3134 file delete $path(test1)
3135 set f [open $path(test1) w]
3136 fconfigure $f -translation lf
3137 puts $f hello\nthere\nand\nhere
3138 close $f
3139 set f [open $path(test1) r]
3140 fconfigure $f -translation crlf
3141 set x [read $f]
3142 close $f
3143 set x
3144 } "hello\nthere\nand\nhere\n"
3145 test io-30.4 {Tcl_Write cr, Tcl_Read cr} {
3146 file delete $path(test1)
3147 set f [open $path(test1) w]
3148 fconfigure $f -translation cr
3149 puts $f hello\nthere\nand\nhere
3150 close $f
3151 set f [open $path(test1) r]
3152 fconfigure $f -translation cr
3153 set x [read $f]
3154 close $f
3155 set x
3156 } "hello\nthere\nand\nhere\n"
3157 test io-30.5 {Tcl_Write cr, Tcl_Read lf} {
3158 file delete $path(test1)
3159 set f [open $path(test1) w]
3160 fconfigure $f -translation cr
3161 puts $f hello\nthere\nand\nhere
3162 close $f
3163 set f [open $path(test1) r]
3164 fconfigure $f -translation lf
3165 set x [read $f]
3166 close $f
3167 set x
3168 } "hello\rthere\rand\rhere\r"
3169 test io-30.6 {Tcl_Write cr, Tcl_Read crlf} {
3170 file delete $path(test1)
3171 set f [open $path(test1) w]
3172 fconfigure $f -translation cr
3173 puts $f hello\nthere\nand\nhere
3174 close $f
3175 set f [open $path(test1) r]
3176 fconfigure $f -translation crlf
3177 set x [read $f]
3178 close $f
3179 set x
3180 } "hello\rthere\rand\rhere\r"
3181 test io-30.7 {Tcl_Write crlf, Tcl_Read crlf} {
3182 file delete $path(test1)
3183 set f [open $path(test1) w]
3184 fconfigure $f -translation crlf
3185 puts $f hello\nthere\nand\nhere
3186 close $f
3187 set f [open $path(test1) r]
3188 fconfigure $f -translation crlf
3189 set x [read $f]
3190 close $f
3191 set x
3192 } "hello\nthere\nand\nhere\n"
3193 test io-30.8 {Tcl_Write crlf, Tcl_Read lf} {
3194 file delete $path(test1)
3195 set f [open $path(test1) w]
3196 fconfigure $f -translation crlf
3197 puts $f hello\nthere\nand\nhere
3198 close $f
3199 set f [open $path(test1) r]
3200 fconfigure $f -translation lf
3201 set x [read $f]
3202 close $f
3203 set x
3204 } "hello\r\nthere\r\nand\r\nhere\r\n"
3205 test io-30.9 {Tcl_Write crlf, Tcl_Read cr} {
3206 file delete $path(test1)
3207 set f [open $path(test1) w]
3208 fconfigure $f -translation crlf
3209 puts $f hello\nthere\nand\nhere
3210 close $f
3211 set f [open $path(test1) r]
3212 fconfigure $f -translation cr
3213 set x [read $f]
3214 close $f
3215 set x
3216 } "hello\n\nthere\n\nand\n\nhere\n\n"
3217 test io-30.10 {Tcl_Write lf, Tcl_Read auto} {
3218 file delete $path(test1)
3219 set f [open $path(test1) w]
3220 fconfigure $f -translation lf
3221 puts $f hello\nthere\nand\nhere
3222 close $f
3223 set f [open $path(test1) r]
3224 set c [read $f]
3225 set x [fconfigure $f -translation]
3226 close $f
3227 list $c $x
3228 } {{hello
3229 there
3230 and
3231 here
3232 } auto}
3233 test io-30.11 {Tcl_Write cr, Tcl_Read auto} {
3234 file delete $path(test1)
3235 set f [open $path(test1) w]
3236 fconfigure $f -translation cr
3237 puts $f hello\nthere\nand\nhere
3238 close $f
3239 set f [open $path(test1) r]
3240 set c [read $f]
3241 set x [fconfigure $f -translation]
3242 close $f
3243 list $c $x
3244 } {{hello
3245 there
3246 and
3247 here
3248 } auto}
3249 test io-30.12 {Tcl_Write crlf, Tcl_Read auto} {
3250 file delete $path(test1)
3251 set f [open $path(test1) w]
3252 fconfigure $f -translation crlf
3253 puts $f hello\nthere\nand\nhere
3254 close $f
3255 set f [open $path(test1) r]
3256 set c [read $f]
3257 set x [fconfigure $f -translation]
3258 close $f
3259 list $c $x
3260 } {{hello
3261 there
3262 and
3263 here
3264 } auto}
3265 test io-30.13 {Tcl_Write crlf on block boundary, Tcl_Read auto} {
3266 file delete $path(test1)
3267 set f [open $path(test1) w]
3268 fconfigure $f -translation crlf
3269 set line "123456789ABCDE" ;# 14 char plus crlf
3270 puts -nonewline $f x ;# shift crlf across block boundary
3271 for {set i 0} {$i < 700} {incr i} {
3272 puts $f $line
3273 }
3274 close $f
3275 set f [open $path(test1) r]
3276 fconfigure $f -translation auto
3277 set c [read $f]
3278 close $f
3279 string length $c
3280 } [expr {700*15+1}]
3281 test io-30.14 {Tcl_Write crlf on block boundary, Tcl_Read crlf} {
3282 file delete $path(test1)
3283 set f [open $path(test1) w]
3284 fconfigure $f -translation crlf
3285 set line "123456789ABCDE" ;# 14 char plus crlf
3286 puts -nonewline $f x ;# shift crlf across block boundary
3287 for {set i 0} {$i < 700} {incr i} {
3288 puts $f $line
3289 }
3290 close $f
3291 set f [open $path(test1) r]
3292 fconfigure $f -translation crlf
3293 set c [read $f]
3294 close $f
3295 string length $c
3296 } [expr {700*15+1}]
3297 test io-30.15 {Tcl_Write mixed, Tcl_Read auto} {
3298 file delete $path(test1)
3299 set f [open $path(test1) w]
3300 fconfigure $f -translation lf
3301 puts $f hello\nthere\nand\rhere
3302 close $f
3303 set f [open $path(test1) r]
3304 fconfigure $f -translation auto
3305 set c [read $f]
3306 close $f
3307 set c
3308 } {hello
3309 there
3310 and
3311 here
3312 }
3313 test io-30.16 {Tcl_Write ^Z at end, Tcl_Read auto} {
3314 file delete $path(test1)
3315 set f [open $path(test1) w]
3316 fconfigure $f -translation lf
3317 puts -nonewline $f hello\nthere\nand\rhere\n\x1A
3318 close $f
3319 set f [open $path(test1) r]
3320 fconfigure $f -translation auto -eofchar \x1A
3321 set c [read $f]
3322 close $f
3323 set c
3324 } {hello
3325 there
3326 and
3327 here
3328 }
3329 test io-30.17 {Tcl_Write, implicit ^Z at end, Tcl_Read auto} {win} {
3330 file delete $path(test1)
3331 set f [open $path(test1) w]
3332 fconfigure $f -translation lf -eofchar \x1A
3333 puts $f hello\nthere\nand\rhere
3334 close $f
3335 set f [open $path(test1) r]
3336 fconfigure $f -translation auto -eofchar \x1A
3337 set c [read $f]
3338 close $f
3339 set c
3340 } {hello
3341 there
3342 and
3343 here
3344 }
3345 test io-30.18 {Tcl_Write, ^Z in middle, Tcl_Read auto} {
3346 file delete $path(test1)
3347 set f [open $path(test1) w]
3348 fconfigure $f -translation lf
3349 set s [format "abc\ndef\n%cghi\nqrs" 26]
3350 puts $f $s
3351 close $f
3352 set f [open $path(test1) r]
3353 fconfigure $f -translation auto -eofchar \x1A
3354 set l ""
3355 lappend l [gets $f]
3356 lappend l [gets $f]
3357 lappend l [eof $f]
3358 lappend l [gets $f]
3359 lappend l [eof $f]
3360 lappend l [gets $f]
3361 lappend l [eof $f]
3362 close $f
3363 set l
3364 } {abc def 0 {} 1 {} 1}
3365 test io-30.19 {Tcl_Write, ^Z no newline in middle, Tcl_Read auto} {
3366 file delete $path(test1)
3367 set f [open $path(test1) w]
3368 fconfigure $f -translation lf
3369 set s [format "abc\ndef\n%cghi\nqrs" 26]
3370 puts $f $s
3371 close $f
3372 set f [open $path(test1) r]
3373 fconfigure $f -translation auto -eofchar \x1A
3374 set l ""
3375 lappend l [gets $f]
3376 lappend l [gets $f]
3377 lappend l [eof $f]
3378 lappend l [gets $f]
3379 lappend l [eof $f]
3380 lappend l [gets $f]
3381 lappend l [eof $f]
3382 close $f
3383 set l
3384 } {abc def 0 {} 1 {} 1}
3385 test io-30.20 {Tcl_Write, ^Z in middle ignored, Tcl_Read lf} {
3386 file delete $path(test1)
3387 set f [open $path(test1) w]
3388 fconfigure $f -translation lf -eofchar {}
3389 set s [format "abc\ndef\n%cghi\nqrs" 26]
3390 puts $f $s
3391 close $f
3392 set f [open $path(test1) r]
3393 fconfigure $f -translation lf -eofchar {}
3394 set l ""
3395 lappend l [gets $f]
3396 lappend l [gets $f]
3397 lappend l [eof $f]
3398 lappend l [gets $f]
3399 lappend l [eof $f]
3400 lappend l [gets $f]
3401 lappend l [eof $f]
3402 lappend l [gets $f]
3403 lappend l [eof $f]
3404 close $f
3405 set l
3406 } "abc def 0 \x1Aghi 0 qrs 0 {} 1"
3407 test io-30.21 {Tcl_Write, ^Z in middle ignored, Tcl_Read cr} {
3408 file delete $path(test1)
3409 set f [open $path(test1) w]
3410 fconfigure $f -translation lf -eofchar {}
3411 set s [format "abc\ndef\n%cghi\nqrs" 26]
3412 puts $f $s
3413 close $f
3414 set f [open $path(test1) r]
3415 fconfigure $f -translation cr -eofchar {}
3416 set l ""
3417 set x [gets $f]
3418 lappend l [string compare $x "abc\ndef\n\x1Aghi\nqrs\n"]
3419 lappend l [eof $f]
3420 lappend l [gets $f]
3421 lappend l [eof $f]
3422 close $f
3423 set l
3424 } {0 1 {} 1}
3425 test io-30.22 {Tcl_Write, ^Z in middle ignored, Tcl_Read crlf} {
3426 file delete $path(test1)
3427 set f [open $path(test1) w]
3428 fconfigure $f -translation lf -eofchar {}
3429 set s [format "abc\ndef\n%cghi\nqrs" 26]
3430 puts $f $s
3431 close $f
3432 set f [open $path(test1) r]
3433 fconfigure $f -translation crlf -eofchar {}
3434 set l ""
3435 set x [gets $f]
3436 lappend l [string compare $x "abc\ndef\n\x1Aghi\nqrs\n"]
3437 lappend l [eof $f]
3438 lappend l [gets $f]
3439 lappend l [eof $f]
3440 close $f
3441 set l
3442 } {0 1 {} 1}
3443 test io-30.23 {Tcl_Write lf, ^Z in middle, Tcl_Read auto} {
3444 file delete $path(test1)
3445 set f [open $path(test1) w]
3446 fconfigure $f -translation lf
3447 set c [format abc\ndef\n%cqrs\ntuv 26]
3448 puts $f $c
3449 close $f
3450 set f [open $path(test1) r]
3451 fconfigure $f -translation auto -eofchar \x1A
3452 set c [string length [read $f]]
3453 set e [eof $f]
3454 close $f
3455 list $c $e
3456 } {8 1}
3457 test io-30.24 {Tcl_Write lf, ^Z in middle, Tcl_Read lf} {
3458 file delete $path(test1)
3459 set f [open $path(test1) w]
3460 fconfigure $f -translation lf
3461 set c [format abc\ndef\n%cqrs\ntuv 26]
3462 puts $f $c
3463 close $f
3464 set f [open $path(test1) r]
3465 fconfigure $f -translation lf -eofchar \x1A
3466 set c [string length [read $f]]
3467 set e [eof $f]
3468 close $f
3469 list $c $e
3470 } {8 1}
3471 test io-30.25 {Tcl_Write cr, ^Z in middle, Tcl_Read auto} {
3472 file delete $path(test1)
3473 set f [open $path(test1) w]
3474 fconfigure $f -translation cr
3475 set c [format abc\ndef\n%cqrs\ntuv 26]
3476 puts $f $c
3477 close $f
3478 set f [open $path(test1) r]
3479 fconfigure $f -translation auto -eofchar \x1A
3480 set c [string length [read $f]]
3481 set e [eof $f]
3482 close $f
3483 list $c $e
3484 } {8 1}
3485 test io-30.26 {Tcl_Write cr, ^Z in middle, Tcl_Read cr} {
3486 file delete $path(test1)
3487 set f [open $path(test1) w]
3488 fconfigure $f -translation cr
3489 set c [format abc\ndef\n%cqrs\ntuv 26]
3490 puts $f $c
3491 close $f
3492 set f [open $path(test1) r]
3493 fconfigure $f -translation cr -eofchar \x1A
3494 set c [string length [read $f]]
3495 set e [eof $f]
3496 close $f
3497 list $c $e
3498 } {8 1}
3499 test io-30.27 {Tcl_Write crlf, ^Z in middle, Tcl_Read auto} {
3500 file delete $path(test1)
3501 set f [open $path(test1) w]
3502 fconfigure $f -translation crlf
3503 set c [format abc\ndef\n%cqrs\ntuv 26]
3504 puts $f $c
3505 close $f
3506 set f [open $path(test1) r]
3507 fconfigure $f -translation auto -eofchar \x1A
3508 set c [string length [read $f]]
3509 set e [eof $f]
3510 close $f
3511 list $c $e
3512 } {8 1}
3513 test io-30.28 {Tcl_Write crlf, ^Z in middle, Tcl_Read crlf} {
3514 file delete $path(test1)
3515 set f [open $path(test1) w]
3516 fconfigure $f -translation crlf
3517 set c [format abc\ndef\n%cqrs\ntuv 26]
3518 puts $f $c
3519 close $f
3520 set f [open $path(test1) r]
3521 fconfigure $f -translation crlf -eofchar \x1A
3522 set c [string length [read $f]]
3523 set e [eof $f]
3524 close $f
3525 list $c $e
3526 } {8 1}
3527
3528 # Test end of line translations. Functions tested are Tcl_Write and Tcl_Gets.
3529
3530 test io-31.1 {Tcl_Write lf, Tcl_Gets auto} {
3531 file delete $path(test1)
3532 set f [open $path(test1) w]
3533 fconfigure $f -translation lf
3534 puts $f hello\nthere\nand\nhere
3535 close $f
3536 set f [open $path(test1) r]
3537 set l ""
3538 lappend l [gets $f]
3539 lappend l [tell $f]
3540 lappend l [fconfigure $f -translation]
3541 lappend l [gets $f]
3542 lappend l [tell $f]
3543 lappend l [fconfigure $f -translation]
3544 close $f
3545 set l
3546 } {hello 6 auto there 12 auto}
3547 test io-31.2 {Tcl_Write cr, Tcl_Gets auto} {
3548 file delete $path(test1)
3549 set f [open $path(test1) w]
3550 fconfigure $f -translation cr
3551 puts $f hello\nthere\nand\nhere
3552 close $f
3553 set f [open $path(test1) r]
3554 set l ""
3555 lappend l [gets $f]
3556 lappend l [tell $f]
3557 lappend l [fconfigure $f -translation]
3558 lappend l [gets $f]
3559 lappend l [tell $f]
3560 lappend l [fconfigure $f -translation]
3561 close $f
3562 set l
3563 } {hello 6 auto there 12 auto}
3564 test io-31.3 {Tcl_Write crlf, Tcl_Gets auto} {
3565 file delete $path(test1)
3566 set f [open $path(test1) w]
3567 fconfigure $f -translation crlf
3568 puts $f hello\nthere\nand\nhere
3569 close $f
3570 set f [open $path(test1) r]
3571 set l ""
3572 lappend l [gets $f]
3573 lappend l [tell $f]
3574 lappend l [fconfigure $f -translation]
3575 lappend l [gets $f]
3576 lappend l [tell $f]
3577 lappend l [fconfigure $f -translation]
3578 close $f
3579 set l
3580 } {hello 7 auto there 14 auto}
3581 test io-31.4 {Tcl_Write lf, Tcl_Gets lf} {
3582 file delete $path(test1)
3583 set f [open $path(test1) w]
3584 fconfigure $f -translation lf
3585 puts $f hello\nthere\nand\nhere
3586 close $f
3587 set f [open $path(test1) r]
3588 fconfigure $f -translation lf
3589 set l ""
3590 lappend l [gets $f]
3591 lappend l [tell $f]
3592 lappend l [fconfigure $f -translation]
3593 lappend l [gets $f]
3594 lappend l [tell $f]
3595 lappend l [fconfigure $f -translation]
3596 close $f
3597 set l
3598 } {hello 6 lf there 12 lf}
3599 test io-31.5 {Tcl_Write lf, Tcl_Gets cr} {
3600 file delete $path(test1)
3601 set f [open $path(test1) w]
3602 fconfigure $f -translation lf
3603 puts $f hello\nthere\nand\nhere
3604 close $f
3605 set f [open $path(test1) r]
3606 fconfigure $f -translation cr
3607 set l ""
3608 lappend l [string length [gets $f]]
3609 lappend l [tell $f]
3610 lappend l [fconfigure $f -translation]
3611 lappend l [eof $f]
3612 lappend l [gets $f]
3613 lappend l [tell $f]
3614 lappend l [fconfigure $f -translation]
3615 lappend l [eof $f]
3616 close $f
3617 set l
3618 } {21 21 cr 1 {} 21 cr 1}
3619 test io-31.6 {Tcl_Write lf, Tcl_Gets crlf} {
3620 file delete $path(test1)
3621 set f [open $path(test1) w]
3622 fconfigure $f -translation lf
3623 puts $f hello\nthere\nand\nhere
3624 close $f
3625 set f [open $path(test1) r]
3626 fconfigure $f -translation crlf
3627 set l ""
3628 lappend l [string length [gets $f]]
3629 lappend l [tell $f]
3630 lappend l [fconfigure $f -translation]
3631 lappend l [eof $f]
3632 lappend l [gets $f]
3633 lappend l [tell $f]
3634 lappend l [fconfigure $f -translation]
3635 lappend l [eof $f]
3636 close $f
3637 set l
3638 } {21 21 crlf 1 {} 21 crlf 1}
3639 test io-31.7 {Tcl_Write cr, Tcl_Gets cr} {
3640 file delete $path(test1)
3641 set f [open $path(test1) w]
3642 fconfigure $f -translation cr
3643 puts $f hello\nthere\nand\nhere
3644 close $f
3645 set f [open $path(test1) r]
3646 fconfigure $f -translation cr
3647 set l ""
3648 lappend l [gets $f]
3649 lappend l [tell $f]
3650 lappend l [fconfigure $f -translation]
3651 lappend l [eof $f]
3652 lappend l [gets $f]
3653 lappend l [tell $f]
3654 lappend l [fconfigure $f -translation]
3655 lappend l [eof $f]
3656 close $f
3657 set l
3658 } {hello 6 cr 0 there 12 cr 0}
3659 test io-31.8 {Tcl_Write cr, Tcl_Gets lf} {
3660 file delete $path(test1)
3661 set f [open $path(test1) w]
3662 fconfigure $f -translation cr
3663 puts $f hello\nthere\nand\nhere
3664 close $f
3665 set f [open $path(test1) r]
3666 fconfigure $f -translation lf
3667 set l ""
3668 lappend l [string length [gets $f]]
3669 lappend l [tell $f]
3670 lappend l [fconfigure $f -translation]
3671 lappend l [eof $f]
3672 lappend l [gets $f]
3673 lappend l [tell $f]
3674 lappend l [fconfigure $f -translation]
3675 lappend l [eof $f]
3676 close $f
3677 set l
3678 } {21 21 lf 1 {} 21 lf 1}
3679 test io-31.9 {Tcl_Write cr, Tcl_Gets crlf} {
3680 file delete $path(test1)
3681 set f [open $path(test1) w]
3682 fconfigure $f -translation cr
3683 puts $f hello\nthere\nand\nhere
3684 close $f
3685 set f [open $path(test1) r]
3686 fconfigure $f -translation crlf
3687 set l ""
3688 lappend l [string length [gets $f]]
3689 lappend l [tell $f]
3690 lappend l [fconfigure $f -translation]
3691 lappend l [eof $f]
3692 lappend l [gets $f]
3693 lappend l [tell $f]
3694 lappend l [fconfigure $f -translation]
3695 lappend l [eof $f]
3696 close $f
3697 set l
3698 } {21 21 crlf 1 {} 21 crlf 1}
3699 test io-31.10 {Tcl_Write crlf, Tcl_Gets crlf} {
3700 file delete $path(test1)
3701 set f [open $path(test1) w]
3702 fconfigure $f -translation crlf
3703 puts $f hello\nthere\nand\nhere
3704 close $f
3705 set f [open $path(test1) r]
3706 fconfigure $f -translation crlf
3707 set l ""
3708 lappend l [gets $f]
3709 lappend l [tell $f]
3710 lappend l [fconfigure $f -translation]
3711 lappend l [eof $f]
3712 lappend l [gets $f]
3713 lappend l [tell $f]
3714 lappend l [fconfigure $f -translation]
3715 lappend l [eof $f]
3716 close $f
3717 set l
3718 } {hello 7 crlf 0 there 14 crlf 0}
3719 test io-31.11 {Tcl_Write crlf, Tcl_Gets cr} {
3720 file delete $path(test1)
3721 set f [open $path(test1) w]
3722 fconfigure $f -translation crlf
3723 puts $f hello\nthere\nand\nhere
3724 close $f
3725 set f [open $path(test1) r]
3726 fconfigure $f -translation cr
3727 set l ""
3728 lappend l [gets $f]
3729 lappend l [tell $f]
3730 lappend l [fconfigure $f -translation]
3731 lappend l [eof $f]
3732 lappend l [string length [gets $f]]
3733 lappend l [tell $f]
3734 lappend l [fconfigure $f -translation]
3735 lappend l [eof $f]
3736 close $f
3737 set l
3738 } {hello 6 cr 0 6 13 cr 0}
3739 test io-31.12 {Tcl_Write crlf, Tcl_Gets lf} {
3740 file delete $path(test1)
3741 set f [open $path(test1) w]
3742 fconfigure $f -translation crlf
3743 puts $f hello\nthere\nand\nhere
3744 close $f
3745 set f [open $path(test1) r]
3746 fconfigure $f -translation lf
3747 set l ""
3748 lappend l [string length [gets $f]]
3749 lappend l [tell $f]
3750 lappend l [fconfigure $f -translation]
3751 lappend l [eof $f]
3752 lappend l [string length [gets $f]]
3753 lappend l [tell $f]
3754 lappend l [fconfigure $f -translation]
3755 lappend l [eof $f]
3756 close $f
3757 set l
3758 } {6 7 lf 0 6 14 lf 0}
3759 test io-31.13 {binary mode is synonym of lf mode} {
3760 file delete $path(test1)
3761 set f [open $path(test1) w]
3762 fconfigure $f -translation binary
3763 set x [fconfigure $f -translation]
3764 close $f
3765 set x
3766 } lf
3767 #
3768 # Test io-9.14 has been removed because "auto" output translation mode is
3769 # not supported.
3770 #
3771 test io-31.14 {Tcl_Write mixed, Tcl_Gets auto} {
3772 file delete $path(test1)
3773 set f [open $path(test1) w]
3774 fconfigure $f -translation lf
3775 puts $f hello\nthere\rand\r\nhere
3776 close $f
3777 set f [open $path(test1) r]
3778 fconfigure $f -translation auto
3779 set l ""
3780 lappend l [gets $f]
3781 lappend l [gets $f]
3782 lappend l [gets $f]
3783 lappend l [gets $f]
3784 lappend l [eof $f]
3785 lappend l [gets $f]
3786 lappend l [eof $f]
3787 close $f
3788 set l
3789 } {hello there and here 0 {} 1}
3790 test io-31.15 {Tcl_Write mixed, Tcl_Gets auto} {
3791 file delete $path(test1)
3792 set f [open $path(test1) w]
3793 fconfigure $f -translation lf
3794 puts -nonewline $f hello\nthere\rand\r\nhere\r
3795 close $f
3796 set f [open $path(test1) r]
3797 fconfigure $f -translation auto
3798 set l ""
3799 lappend l [gets $f]
3800 lappend l [gets $f]
3801 lappend l [gets $f]
3802 lappend l [gets $f]
3803 lappend l [eof $f]
3804 lappend l [gets $f]
3805 lappend l [eof $f]
3806 close $f
3807 set l
3808 } {hello there and here 0 {} 1}
3809 test io-31.16 {Tcl_Write mixed, Tcl_Gets auto} {
3810 file delete $path(test1)
3811 set f [open $path(test1) w]
3812 fconfigure $f -translation lf
3813 puts -nonewline $f hello\nthere\rand\r\nhere\n
3814 close $f
3815 set f [open $path(test1) r]
3816 set l ""
3817 lappend l [gets $f]
3818 lappend l [gets $f]
3819 lappend l [gets $f]
3820 lappend l [gets $f]
3821 lappend l [eof $f]
3822 lappend l [gets $f]
3823 lappend l [eof $f]
3824 close $f
3825 set l
3826 } {hello there and here 0 {} 1}
3827 test io-31.17 {Tcl_Write mixed, Tcl_Gets auto} {
3828 file delete $path(test1)
3829 set f [open $path(test1) w]
3830 fconfigure $f -translation lf
3831 puts -nonewline $f hello\nthere\rand\r\nhere\r\n
3832 close $f
3833 set f [open $path(test1) r]
3834 fconfigure $f -translation auto
3835 set l ""
3836 lappend l [gets $f]
3837 lappend l [gets $f]
3838 lappend l [gets $f]
3839 lappend l [gets $f]
3840 lappend l [eof $f]
3841 lappend l [gets $f]
3842 lappend l [eof $f]
3843 close $f
3844 set l
3845 } {hello there and here 0 {} 1}
3846 test io-31.18 {Tcl_Write ^Z at end, Tcl_Gets auto} {
3847 file delete $path(test1)
3848 set f [open $path(test1) w]
3849 fconfigure $f -translation lf
3850 set s [format "hello\nthere\nand\rhere\n\%c" 26]
3851 puts $f $s
3852 close $f
3853 set f [open $path(test1) r]
3854 fconfigure $f -translation auto -eofchar \x1A
3855 set l ""
3856 lappend l [gets $f]
3857 lappend l [gets $f]
3858 lappend l [gets $f]
3859 lappend l [gets $f]
3860 lappend l [eof $f]
3861 lappend l [gets $f]
3862 lappend l [eof $f]
3863 close $f
3864 set l
3865 } {hello there and here 0 {} 1}
3866 test io-31.19 {Tcl_Write, implicit ^Z at end, Tcl_Gets auto} {
3867 file delete $path(test1)
3868 set f [open $path(test1) w]
3869 fconfigure $f -translation lf -eofchar \x1A
3870 puts $f hello\nthere\nand\rhere
3871 close $f
3872 set f [open $path(test1) r]
3873 fconfigure $f -translation auto -eofchar \x1A
3874 set l ""
3875 lappend l [gets $f]
3876 lappend l [gets $f]
3877 lappend l [gets $f]
3878 lappend l [gets $f]
3879 lappend l [eof $f]
3880 lappend l [gets $f]
3881 lappend l [eof $f]
3882 close $f
3883 set l
3884 } {hello there and here 0 {} 1}
3885 test io-31.20 {Tcl_Write, ^Z in middle, Tcl_Gets auto, eofChar} {
3886 file delete $path(test1)
3887 set f [open $path(test1) w]
3888 fconfigure $f -translation lf
3889 set s [format "abc\ndef\n%cqrs\ntuv" 26]
3890 puts $f $s
3891 close $f
3892 set f [open $path(test1) r]
3893 fconfigure $f -translation auto -eofchar \x1A
3894 set l ""
3895 lappend l [gets $f]
3896 lappend l [gets $f]
3897 lappend l [eof $f]
3898 lappend l [gets $f]
3899 lappend l [eof $f]
3900 close $f
3901 set l
3902 } {abc def 0 {} 1}
3903 test io-31.21 {Tcl_Write, no newline ^Z in middle, Tcl_Gets auto, eofChar} {
3904 file delete $path(test1)
3905 set f [open $path(test1) w]
3906 fconfigure $f -translation lf
3907 set s [format "abc\ndef\n%cqrs\ntuv" 26]
3908 puts $f $s
3909 close $f
3910 set f [open $path(test1) r]
3911 fconfigure $f -translation auto -eofchar \x1A
3912 set l ""
3913 lappend l [gets $f]
3914 lappend l [gets $f]
3915 lappend l [eof $f]
3916 lappend l [gets $f]
3917 lappend l [eof $f]
3918 close $f
3919 set l
3920 } {abc def 0 {} 1}
3921 test io-31.22 {Tcl_Write, ^Z in middle ignored, Tcl_Gets lf} {
3922 file delete $path(test1)
3923 set f [open $path(test1) w]
3924 fconfigure $f -translation lf -eofchar {}
3925 set s [format "abc\ndef\n%cqrs\ntuv" 26]
3926 puts $f $s
3927 close $f
3928 set f [open $path(test1) r]
3929 fconfigure $f -translation lf -eofchar {}
3930 set l ""
3931 lappend l [gets $f]
3932 lappend l [gets $f]
3933 lappend l [eof $f]
3934 lappend l [gets $f]
3935 lappend l [eof $f]
3936 lappend l [gets $f]
3937 lappend l [eof $f]
3938 lappend l [gets $f]
3939 lappend l [eof $f]
3940 close $f
3941 set l
3942 } "abc def 0 \x1Aqrs 0 tuv 0 {} 1"
3943 test io-31.23 {Tcl_Write, ^Z in middle ignored, Tcl_Gets cr} {
3944 file delete $path(test1)
3945 set f [open $path(test1) w]
3946 fconfigure $f -translation cr -eofchar {}
3947 set s [format "abc\ndef\n%cqrs\ntuv" 26]
3948 puts $f $s
3949 close $f
3950 set f [open $path(test1) r]
3951 fconfigure $f -translation cr -eofchar {}
3952 set l ""
3953 lappend l [gets $f]
3954 lappend l [gets $f]
3955 lappend l [eof $f]
3956 lappend l [gets $f]
3957 lappend l [eof $f]
3958 lappend l [gets $f]
3959 lappend l [eof $f]
3960 lappend l [gets $f]
3961 lappend l [eof $f]
3962 close $f
3963 set l
3964 } "abc def 0 \x1Aqrs 0 tuv 0 {} 1"
3965 test io-31.24 {Tcl_Write, ^Z in middle ignored, Tcl_Gets crlf} {
3966 file delete $path(test1)
3967 set f [open $path(test1) w]
3968 fconfigure $f -translation crlf -eofchar {}
3969 set s [format "abc\ndef\n%cqrs\ntuv" 26]
3970 puts $f $s
3971 close $f
3972 set f [open $path(test1) r]
3973 fconfigure $f -translation crlf -eofchar {}
3974 set l ""
3975 lappend l [gets $f]
3976 lappend l [gets $f]
3977 lappend l [eof $f]
3978 lappend l [gets $f]
3979 lappend l [eof $f]
3980 lappend l [gets $f]
3981 lappend l [eof $f]
3982 lappend l [gets $f]
3983 lappend l [eof $f]
3984 close $f
3985 set l
3986 } "abc def 0 \x1Aqrs 0 tuv 0 {} 1"
3987 test io-31.25 {Tcl_Write lf, ^Z in middle, Tcl_Gets auto} {
3988 file delete $path(test1)
3989 set f [open $path(test1) w]
3990 fconfigure $f -translation lf
3991 set s [format "abc\ndef\n%cqrs\ntuv" 26]
3992 puts $f $s
3993 close $f
3994 set f [open $path(test1) r]
3995 fconfigure $f -translation auto -eofchar \x1A
3996 set l ""
3997 lappend l [gets $f]
3998 lappend l [gets $f]
3999 lappend l [eof $f]
4000 lappend l [gets $f]
4001 lappend l [eof $f]
4002 close $f
4003 set l
4004 } {abc def 0 {} 1}
4005 test io-31.26 {Tcl_Write lf, ^Z in middle, Tcl_Gets lf} {
4006 file delete $path(test1)
4007 set f [open $path(test1) w]
4008 fconfigure $f -translation lf
4009 set s [format "abc\ndef\n%cqrs\ntuv" 26]
4010 puts $f $s
4011 close $f
4012 set f [open $path(test1) r]
4013 fconfigure $f -translation lf -eofchar \x1A
4014 set l ""
4015 lappend l [gets $f]
4016 lappend l [gets $f]
4017 lappend l [eof $f]
4018 lappend l [gets $f]
4019 lappend l [eof $f]
4020 close $f
4021 set l
4022 } {abc def 0 {} 1}
4023 test io-31.27 {Tcl_Write cr, ^Z in middle, Tcl_Gets auto} {
4024 file delete $path(test1)
4025 set f [open $path(test1) w]
4026 fconfigure $f -translation cr -eofchar {}
4027 set s [format "abc\ndef\n%cqrs\ntuv" 26]
4028 puts $f $s
4029 close $f
4030 set f [open $path(test1) r]
4031 fconfigure $f -translation auto -eofchar \x1A
4032 set l ""
4033 lappend l [gets $f]
4034 lappend l [gets $f]
4035 lappend l [eof $f]
4036 lappend l [gets $f]
4037 lappend l [eof $f]
4038 close $f
4039 set l
4040 } {abc def 0 {} 1}
4041 test io-31.28 {Tcl_Write cr, ^Z in middle, Tcl_Gets cr} {
4042 file delete $path(test1)
4043 set f [open $path(test1) w]
4044 fconfigure $f -translation cr -eofchar {}
4045 set s [format "abc\ndef\n%cqrs\ntuv" 26]
4046 puts $f $s
4047 close $f
4048 set f [open $path(test1) r]
4049 fconfigure $f -translation cr -eofchar \x1A
4050 set l ""
4051 lappend l [gets $f]
4052 lappend l [gets $f]
4053 lappend l [eof $f]
4054 lappend l [gets $f]
4055 lappend l [eof $f]
4056 close $f
4057 set l
4058 } {abc def 0 {} 1}
4059 test io-31.29 {Tcl_Write crlf, ^Z in middle, Tcl_Gets auto} {
4060 file delete $path(test1)
4061 set f [open $path(test1) w]
4062 fconfigure $f -translation crlf -eofchar {}
4063 set s [format "abc\ndef\n%cqrs\ntuv" 26]
4064 puts $f $s
4065 close $f
4066 set f [open $path(test1) r]
4067 fconfigure $f -translation auto -eofchar \x1A
4068 set l ""
4069 lappend l [gets $f]
4070 lappend l [gets $f]
4071 lappend l [eof $f]
4072 lappend l [gets $f]
4073 lappend l [eof $f]
4074 close $f
4075 set l
4076 } {abc def 0 {} 1}
4077 test io-31.30 {Tcl_Write crlf, ^Z in middle, Tcl_Gets crlf} {
4078 file delete $path(test1)
4079 set f [open $path(test1) w]
4080 fconfigure $f -translation crlf -eofchar {}
4081 set s [format "abc\ndef\n%cqrs\ntuv" 26]
4082 puts $f $s
4083 close $f
4084 set f [open $path(test1) r]
4085 fconfigure $f -translation crlf -eofchar \x1A
4086 set l ""
4087 lappend l [gets $f]
4088 lappend l [gets $f]
4089 lappend l [eof $f]
4090 lappend l [gets $f]
4091 lappend l [eof $f]
4092 close $f
4093 set l
4094 } {abc def 0 {} 1}
4095 test io-31.31 {Tcl_Write crlf on block boundary, Tcl_Gets crlf} {
4096 file delete $path(test1)
4097 set f [open $path(test1) w]
4098 fconfigure $f -translation crlf
4099 set line "123456789ABCDE" ;# 14 char plus crlf
4100 puts -nonewline $f x ;# shift crlf across block boundary
4101 for {set i 0} {$i < 700} {incr i} {
4102 puts $f $line
4103 }
4104 close $f
4105 set f [open $path(test1) r]
4106 fconfigure $f -translation crlf
4107 set c ""
4108 while {[gets $f line] >= 0} {
4109 append c $line\n
4110 }
4111 close $f
4112 string length $c
4113 } [expr {700*15+1}]
4114 test io-31.32 {Tcl_Write crlf on block boundary, Tcl_Gets auto} {
4115 file delete $path(test1)
4116 set f [open $path(test1) w]
4117 fconfigure $f -translation crlf
4118 set line "123456789ABCDE" ;# 14 char plus crlf
4119 puts -nonewline $f x ;# shift crlf across block boundary
4120 for {set i 0} {$i < 700} {incr i} {
4121 puts $f $line
4122 }
4123 close $f
4124 set f [open $path(test1) r]
4125 fconfigure $f -translation auto
4126 set c ""
4127 while {[gets $f line] >= 0} {
4128 append c $line\n
4129 }
4130 close $f
4131 string length $c
4132 } [expr {700*15+1}]
4133
4134 # Test Tcl_Read and buffering.
4135
4136 test io-32.1 {Tcl_Read, channel not readable} {
4137 list [catch {read stdout} msg] $msg
4138 } {1 {channel "stdout" wasn't opened for reading}}
4139 test io-32.2 {Tcl_Read, zero byte count} {
4140 read stdin 0
4141 } ""
4142 test io-32.3 {Tcl_Read, negative byte count} {
4143 set f [open $path(longfile) r]
4144 set l [list [catch {read $f -1} msg] $msg]
4145 close $f
4146 set l
4147 } {1 {expected non-negative integer but got "-1"}}
4148 test io-32.4 {Tcl_Read, positive byte count} {
4149 set f [open $path(longfile) r]
4150 set x [read $f 1024]
4151 set s [string length $x]
4152 unset x
4153 close $f
4154 set s
4155 } 1024
4156 test io-32.5 {Tcl_Read, multiple buffers} {
4157 set f [open $path(longfile) r]
4158 fconfigure $f -buffersize 100
4159 set x [read $f 1024]
4160 set s [string length $x]
4161 unset x
4162 close $f
4163 set s
4164 } 1024
4165 test io-32.6 {Tcl_Read, very large read} {
4166 set f1 [open $path(longfile) r]
4167 set z [read $f1 1000000]
4168 close $f1
4169 set l [string length $z]
4170 set x ok
4171 set z [file size $path(longfile)]
4172 if {$z != $l} {
4173 set x broken
4174 }
4175 set x
4176 } ok
4177 test io-32.7 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
4178 set f1 [open $path(longfile) r]
4179 fconfigure $f1 -blocking off
4180 set z [read $f1 20]
4181 close $f1
4182 set l [string length $z]
4183 set x ok
4184 if {$l != 20} {
4185 set x broken
4186 }
4187 set x
4188 } ok
4189 test io-32.8 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
4190 set f1 [open $path(longfile) r]
4191 fconfigure $f1 -blocking off
4192 set z [read $f1 1000000]
4193 close $f1
4194 set x ok
4195 set l [string length $z]
4196 set z [file size $path(longfile)]
4197 if {$z != $l} {
4198 set x broken
4199 }
4200 set x
4201 } ok
4202 test io-32.9 {Tcl_Read, read to end of file} {
4203 set f1 [open $path(longfile) r]
4204 set z [read $f1]
4205 close $f1
4206 set l [string length $z]
4207 set x ok
4208 set z [file size $path(longfile)]
4209 if {$z != $l} {
4210 set x broken
4211 }
4212 set x
4213 } ok
4214 test io-32.10 {Tcl_Read from a pipe} stdio {
4215 file delete $path(pipe)
4216 set f1 [open $path(pipe) w]
4217 puts $f1 {puts [gets stdin]}
4218 close $f1
4219 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4220 puts $f1 hello
4221 flush $f1
4222 set x [read $f1]
4223 close $f1
4224 set x
4225 } "hello\n"
4226 test io-32.11 {Tcl_Read from a pipe} stdio {
4227 file delete $path(pipe)
4228 set f1 [open $path(pipe) w]
4229 puts $f1 {puts [gets stdin]}
4230 puts $f1 {puts [gets stdin]}
4231 close $f1
4232 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4233 puts $f1 hello
4234 flush $f1
4235 set x ""
4236 lappend x [read $f1 6]
4237 puts $f1 hello
4238 flush $f1
4239 lappend x [read $f1]
4240 close $f1
4241 set x
4242 } {{hello
4243 } {hello
4244 }}
4245 test io-32.11.1 {Tcl_Read from a pipe} stdio {
4246 file delete $path(pipe)
4247 set f1 [open $path(pipe) w]
4248 puts $f1 {chan configure stdout -translation crlf}
4249 puts $f1 {puts [gets stdin]}
4250 puts $f1 {puts [gets stdin]}
4251 close $f1
4252 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4253 puts $f1 hello
4254 flush $f1
4255 set x ""
4256 lappend x [read $f1 6]
4257 puts $f1 hello
4258 flush $f1
4259 lappend x [read $f1]
4260 close $f1
4261 set x
4262 } {{hello
4263 } {hello
4264 }}
4265 test io-32.11.2 {Tcl_Read from a pipe} stdio {
4266 file delete $path(pipe)
4267 set f1 [open $path(pipe) w]
4268 puts $f1 {chan configure stdout -translation crlf}
4269 puts $f1 {puts [gets stdin]}
4270 puts $f1 {puts [gets stdin]}
4271 close $f1
4272 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4273 puts $f1 hello
4274 flush $f1
4275 set x ""
4276 lappend x [read $f1 6]
4277 puts $f1 hello
4278 flush $f1
4279 lappend x [read $f1]
4280 close $f1
4281 set x
4282 } {{hello
4283 } {hello
4284 }}
4285 test io-32.12 {Tcl_Read, -nonewline} {
4286 file delete $path(test1)
4287 set f1 [open $path(test1) w]
4288 puts $f1 hello
4289 puts $f1 bye
4290 close $f1
4291 set f1 [open $path(test1) r]
4292 set c [read -nonewline $f1]
4293 close $f1
4294 set c
4295 } {hello
4296 bye}
4297 test io-32.13 {Tcl_Read, -nonewline} {
4298 file delete $path(test1)
4299 set f1 [open $path(test1) w]
4300 puts $f1 hello
4301 puts $f1 bye
4302 close $f1
4303 set f1 [open $path(test1) r]
4304 set c [read -nonewline $f1]
4305 close $f1
4306 list [string length $c] $c
4307 } {9 {hello
4308 bye}}
4309 test io-32.14 {Tcl_Read, reading in small chunks} {
4310 file delete $path(test1)
4311 set f [open $path(test1) w]
4312 puts $f "Two lines: this one"
4313 puts $f "and this one"
4314 close $f
4315 set f [open $path(test1)]
4316 set x [list [read $f 1] [read $f 2] [read $f]]
4317 close $f
4318 set x
4319 } {T wo { lines: this one
4320 and this one
4321 }}
4322 test io-32.15 {Tcl_Read, asking for more input than available} {
4323 file delete $path(test1)
4324 set f [open $path(test1) w]
4325 puts $f "Two lines: this one"
4326 puts $f "and this one"
4327 close $f
4328 set f [open $path(test1)]
4329 set x [read $f 100]
4330 close $f
4331 set x
4332 } {Two lines: this one
4333 and this one
4334 }
4335 test io-32.16 {Tcl_Read, read to end of file with -nonewline} {
4336 file delete $path(test1)
4337 set f [open $path(test1) w]
4338 puts $f "Two lines: this one"
4339 puts $f "and this one"
4340 close $f
4341 set f [open $path(test1)]
4342 set x [read -nonewline $f]
4343 close $f
4344 set x
4345 } {Two lines: this one
4346 and this one}
4347
4348 # Test Tcl_Gets.
4349
4350 test io-33.1 {Tcl_Gets, reading what was written} {
4351 file delete $path(test1)
4352 set f1 [open $path(test1) w]
4353 set y "first line"
4354 puts $f1 $y
4355 close $f1
4356 set f1 [open $path(test1) r]
4357 set x [gets $f1]
4358 set z ok
4359 if {"$x" != "$y"} {
4360 set z broken
4361 }
4362 close $f1
4363 set z
4364 } ok
4365 test io-33.2 {Tcl_Gets into variable} {
4366 set f1 [open $path(longfile) r]
4367 set c [gets $f1 x]
4368 set l [string length x]
4369 set z ok
4370 if {$l != $l} {
4371 set z broken
4372 }
4373 close $f1
4374 set z
4375 } ok
4376 test io-33.3 {Tcl_Gets from pipe} stdio {
4377 file delete $path(pipe)
4378 set f1 [open $path(pipe) w]
4379 puts $f1 {puts [gets stdin]}
4380 close $f1
4381 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4382 puts $f1 hello
4383 flush $f1
4384 set x [gets $f1]
4385 close $f1
4386 set z ok
4387 if {"$x" != "hello"} {
4388 set z broken
4389 }
4390 set z
4391 } ok
4392 test io-33.4 {Tcl_Gets with long line} {
4393 file delete $path(test3)
4394 set f [open $path(test3) w]
4395 puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4396 puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4397 puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4398 puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4399 puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4400 close $f
4401 set f [open $path(test3)]
4402 set x [gets $f]
4403 close $f
4404 set x
4405 } {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
4406 set f [open $path(test3) w]
4407 puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4408 puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4409 puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4410 puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4411 puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4412 close $f
4413 test io-33.5 {Tcl_Gets with long line} {
4414 set f [open $path(test3)]
4415 set x [gets $f y]
4416 close $f
4417 list $x $y
4418 } {260 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
4419 test io-33.6 {Tcl_Gets and end of file} {
4420 file delete $path(test3)
4421 set f [open $path(test3) w]
4422 puts -nonewline $f "Test1\nTest2"
4423 close $f
4424 set f [open $path(test3)]
4425 set x {}
4426 set y {}
4427 lappend x [gets $f y] $y
4428 set y {}
4429 lappend x [gets $f y] $y
4430 set y {}
4431 lappend x [gets $f y] $y
4432 close $f
4433 set x
4434 } {5 Test1 5 Test2 -1 {}}
4435 test io-33.7 {Tcl_Gets and bad variable} {
4436 set f [open $path(test3) w]
4437 puts $f "Line 1"
4438 puts $f "Line 2"
4439 close $f
4440 catch {unset x}
4441 set x 24
4442 set f [open $path(test3) r]
4443 set result [list [catch {gets $f x(0)} msg] $msg]
4444 close $f
4445 set result
4446 } {1 {can't set "x(0)": variable isn't array}}
4447 test io-33.8 {Tcl_Gets, exercising double buffering} {
4448 set f [open $path(test3) w]
4449 fconfigure $f -translation lf -eofchar {}
4450 set x ""
4451 for {set y 0} {$y < 99} {incr y} {set x "a$x"}
4452 for {set y 0} {$y < 100} {incr y} {puts $f $x}
4453 close $f
4454 set f [open $path(test3) r]
4455 fconfigure $f -translation lf
4456 for {set y 0} {$y < 100} {incr y} {gets $f}
4457 close $f
4458 set y
4459 } 100
4460 test io-33.9 {Tcl_Gets, exercising double buffering} {
4461 set f [open $path(test3) w]
4462 fconfigure $f -translation lf -eofchar {}
4463 set x ""
4464 for {set y 0} {$y < 99} {incr y} {set x "a$x"}
4465 for {set y 0} {$y < 200} {incr y} {puts $f $x}
4466 close $f
4467 set f [open $path(test3) r]
4468 fconfigure $f -translation lf
4469 for {set y 0} {$y < 200} {incr y} {gets $f}
4470 close $f
4471 set y
4472 } 200
4473 test io-33.10 {Tcl_Gets, exercising double buffering} {
4474 set f [open $path(test3) w]
4475 fconfigure $f -translation lf -eofchar {}
4476 set x ""
4477 for {set y 0} {$y < 99} {incr y} {set x "a$x"}
4478 for {set y 0} {$y < 300} {incr y} {puts $f $x}
4479 close $f
4480 set f [open $path(test3) r]
4481 fconfigure $f -translation lf
4482 for {set y 0} {$y < 300} {incr y} {gets $f}
4483 close $f
4484 set y
4485 } 300
4486 test io-33.11 {TclGetsObjBinary, [10dc6daa37]} -setup {
4487 proc driver {cmd args} {
4488 variable buffer
4489 variable index
4490 set chan [lindex $args 0]
4491 switch -- $cmd {
4492 initialize {
4493 set index($chan) 0
4494 set buffer($chan) .......
4495 return {initialize finalize watch read}
4496 }
4497 finalize {
4498 unset index($chan) buffer($chan)
4499 return
4500 }
4501 watch {}
4502 read {
4503 set n [lindex $args 1]
4504 if {$n > 3} {set n 3}
4505 set new [expr {$index($chan) + $n}]
4506 set result [string range $buffer($chan) $index($chan) $new-1]
4507 set index($chan) $new
4508 return $result
4509 }
4510 }
4511 }
4512 } -body {
4513 set c [chan create read [namespace which driver]]
4514 chan configure $c -translation binary -blocking 0
4515 list [gets $c] [gets $c] [gets $c] [gets $c]
4516 } -cleanup {
4517 close $c
4518 rename driver {}
4519 } -result {{} {} {} .......}
4520 test io-33.12 {Tcl_GetsObj, [10dc6daa37]} -setup {
4521 proc driver {cmd args} {
4522 variable buffer
4523 variable index
4524 set chan [lindex $args 0]
4525 switch -- $cmd {
4526 initialize {
4527 set index($chan) 0
4528 set buffer($chan) .......
4529 return {initialize finalize watch read}
4530 }
4531 finalize {
4532 unset index($chan) buffer($chan)
4533 return
4534 }
4535 watch {}
4536 read {
4537 set n [lindex $args 1]
4538 if {$n > 3} {set n 3}
4539 set new [expr {$index($chan) + $n}]
4540 set result [string range $buffer($chan) $index($chan) $new-1]
4541 set index($chan) $new
4542 return $result
4543 }
4544 }
4545 }
4546 } -body {
4547 set c [chan create read [namespace which driver]]
4548 chan configure $c -blocking 0
4549 list [gets $c] [gets $c] [gets $c] [gets $c]
4550 } -cleanup {
4551 close $c
4552 rename driver {}
4553 } -result {{} {} {} .......}
4554 test io-33.13 {Tcl_GetsObj, [10dc6daa37]} -setup {
4555 proc driver {cmd args} {
4556 variable buffer
4557 variable index
4558 set chan [lindex $args 0]
4559 switch -- $cmd {
4560 initialize {
4561 set index($chan) 0
4562 set buffer($chan) [string repeat \
4563 [string repeat . 64]\n[string repeat . 25] 2]
4564 return {initialize finalize watch read}
4565 }
4566 finalize {
4567 unset index($chan) buffer($chan)
4568 return
4569 }
4570 watch {}
4571 read {
4572 set n [lindex $args 1]
4573 if {$n > 65} {set n 65}
4574 set new [expr {$index($chan) + $n}]
4575 set result [string range $buffer($chan) $index($chan) $new-1]
4576 set index($chan) $new
4577 return $result
4578 }
4579 }
4580 }
4581 } -body {
4582 set c [chan create read [namespace which driver]]
4583 chan configure $c -blocking 0
4584 list [gets $c] [gets $c] [gets $c] [gets $c] [gets $c]
4585 } -cleanup {
4586 close $c
4587 rename driver {}
4588 } -result [list [string repeat . 64] {} [string repeat . 89] \
4589 [string repeat . 25] {}]
4590
4591 # Test Tcl_Seek and Tcl_Tell.
4592
4593 test io-34.1 {Tcl_Seek to current position at start of file} {
4594 set f1 [open $path(longfile) r]
4595 seek $f1 0 current
4596 set c [tell $f1]
4597 close $f1
4598 set c
4599 } 0
4600 test io-34.2 {Tcl_Seek to offset from start} {
4601 file delete $path(test1)
4602 set f1 [open $path(test1) w]
4603 fconfigure $f1 -translation lf -eofchar {}
4604 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4605 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4606 close $f1
4607 set f1 [open $path(test1) r]
4608 seek $f1 10 start
4609 set c [tell $f1]
4610 close $f1
4611 set c
4612 } 10
4613 test io-34.3 {Tcl_Seek to end of file} {
4614 file delete $path(test1)
4615 set f1 [open $path(test1) w]
4616 fconfigure $f1 -translation lf -eofchar {}
4617 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4618 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4619 close $f1
4620 set f1 [open $path(test1) r]
4621 seek $f1 0 end
4622 set c [tell $f1]
4623 close $f1
4624 set c
4625 } 54
4626 test io-34.4 {Tcl_Seek to offset from end of file} {
4627 file delete $path(test1)
4628 set f1 [open $path(test1) w]
4629 fconfigure $f1 -translation lf -eofchar {}
4630 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4631 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4632 close $f1
4633 set f1 [open $path(test1) r]
4634 seek $f1 -10 end
4635 set c [tell $f1]
4636 close $f1
4637 set c
4638 } 44
4639 test io-34.5 {Tcl_Seek to offset from current position} {
4640 file delete $path(test1)
4641 set f1 [open $path(test1) w]
4642 fconfigure $f1 -translation lf -eofchar {}
4643 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4644 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4645 close $f1
4646 set f1 [open $path(test1) r]
4647 seek $f1 10 current
4648 seek $f1 10 current
4649 set c [tell $f1]
4650 close $f1
4651 set c
4652 } 20
4653 test io-34.6 {Tcl_Seek to offset from end of file} {
4654 file delete $path(test1)
4655 set f1 [open $path(test1) w]
4656 fconfigure $f1 -translation lf -eofchar {}
4657 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4658 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4659 close $f1
4660 set f1 [open $path(test1) r]
4661 seek $f1 -10 end
4662 set c [tell $f1]
4663 set r [read $f1]
4664 close $f1
4665 list $c $r
4666 } {44 {rstuvwxyz
4667 }}
4668 test io-34.7 {Tcl_Seek to offset from end of file, then to current position} {
4669 file delete $path(test1)
4670 set f1 [open $path(test1) w]
4671 fconfigure $f1 -translation lf -eofchar {}
4672 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4673 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4674 close $f1
4675 set f1 [open $path(test1) r]
4676 seek $f1 -10 end
4677 set c1 [tell $f1]
4678 set r1 [read $f1 5]
4679 seek $f1 0 current
4680 set c2 [tell $f1]
4681 close $f1
4682 list $c1 $r1 $c2
4683 } {44 rstuv 49}
4684 test io-34.8 {Tcl_Seek on pipes: not supported} stdio {
4685 set f1 [open "|[list [interpreter]]" r+]
4686 set x [list [catch {seek $f1 0 current} msg] $msg]
4687 close $f1
4688 regsub {".*":} $x {"":} x
4689 string tolower $x
4690 } {1 {error during seek on "": invalid argument}}
4691 test io-34.9 {Tcl_Seek, testing buffered input flushing} {
4692 file delete $path(test3)
4693 set f [open $path(test3) w]
4694 fconfigure $f -eofchar {}
4695 puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4696 close $f
4697 set f [open $path(test3) RDWR]
4698 set x [read $f 1]
4699 seek $f 3
4700 lappend x [read $f 1]
4701 seek $f 0 start
4702 lappend x [read $f 1]
4703 seek $f 10 current
4704 lappend x [read $f 1]
4705 seek $f -2 end
4706 lappend x [read $f 1]
4707 seek $f 50 end
4708 lappend x [read $f 1]
4709 seek $f 1
4710 lappend x [read $f 1]
4711 close $f
4712 set x
4713 } {a d a l Y {} b}
4714 set path(test3) [makeFile {} test3]
4715 test io-34.10 {Tcl_Seek testing flushing of buffered input} {
4716 set f [open $path(test3) w]
4717 fconfigure $f -translation lf
4718 puts $f xyz\n123
4719 close $f
4720 set f [open $path(test3) r+]
4721 fconfigure $f -translation lf
4722 set x [gets $f]
4723 seek $f 0 current
4724 puts $f 456
4725 close $f
4726 list $x [viewFile test3]
4727 } "xyz {xyz
4728 456}"
4729 test io-34.11 {Tcl_Seek testing flushing of buffered output} {
4730 set f [open $path(test3) w]
4731 puts $f xyz\n123
4732 close $f
4733 set f [open $path(test3) w+]
4734 puts $f xyzzy
4735 seek $f 2
4736 set x [gets $f]
4737 close $f
4738 list $x [viewFile test3]
4739 } "zzy xyzzy"
4740 test io-34.12 {Tcl_Seek testing combination of write, seek back and read} {
4741 set f [open $path(test3) w]
4742 fconfigure $f -translation lf -eofchar {}
4743 puts $f xyz\n123
4744 close $f
4745 set f [open $path(test3) a+]
4746 fconfigure $f -translation lf -eofchar {}
4747 puts $f xyzzy
4748 flush $f
4749 set x [tell $f]
4750 seek $f -4 cur
4751 set y [gets $f]
4752 close $f
4753 list $x [viewFile test3] $y
4754 } {14 {xyz
4755 123
4756 xyzzy} zzy}
4757 test io-34.13 {Tcl_Tell at start of file} {
4758 file delete $path(test1)
4759 set f1 [open $path(test1) w]
4760 set p [tell $f1]
4761 close $f1
4762 set p
4763 } 0
4764 test io-34.14 {Tcl_Tell after seek to end of file} {
4765 file delete $path(test1)
4766 set f1 [open $path(test1) w]
4767 fconfigure $f1 -translation lf -eofchar {}
4768 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4769 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4770 close $f1
4771 set f1 [open $path(test1) r]
4772 seek $f1 0 end
4773 set c1 [tell $f1]
4774 close $f1
4775 set c1
4776 } 54
4777 test io-34.15 {Tcl_Tell combined with seeking} {
4778 file delete $path(test1)
4779 set f1 [open $path(test1) w]
4780 fconfigure $f1 -translation lf -eofchar {}
4781 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4782 puts $f1 "abcdefghijklmnopqrstuvwxyz"
4783 close $f1
4784 set f1 [open $path(test1) r]
4785 seek $f1 10 start
4786 set c1 [tell $f1]
4787 seek $f1 10 current
4788 set c2 [tell $f1]
4789 close $f1
4790 list $c1 $c2
4791 } {10 20}
4792 test io-34.16 {Tcl_Tell on pipe: always -1} stdio {
4793 set f1 [open "|[list [interpreter]]" r+]
4794 set c [tell $f1]
4795 close $f1
4796 set c
4797 } -1
4798 test io-34.17 {Tcl_Tell on pipe: always -1} stdio {
4799 set f1 [open "|[list [interpreter]]" r+]
4800 puts $f1 {puts hello}
4801 flush $f1
4802 set c [tell $f1]
4803 gets $f1
4804 close $f1
4805 set c
4806 } -1
4807 test io-34.18 {Tcl_Tell combined with seeking and reading} {
4808 file delete $path(test2)
4809 set f [open $path(test2) w]
4810 fconfigure $f -translation lf -eofchar {}
4811 puts -nonewline $f "line1\nline2\nline3\nline4\nline5\n"
4812 close $f
4813 set f [open $path(test2)]
4814 fconfigure $f -translation lf
4815 set x [tell $f]
4816 read $f 3
4817 lappend x [tell $f]
4818 seek $f 2
4819 lappend x [tell $f]
4820 seek $f 10 current
4821 lappend x [tell $f]
4822 seek $f 0 end
4823 lappend x [tell $f]
4824 close $f
4825 set x
4826 } {0 3 2 12 30}
4827 test io-34.19 {Tcl_Tell combined with opening in append mode} {
4828 set f [open $path(test3) w]
4829 fconfigure $f -translation lf -eofchar {}
4830 puts $f "abcdefghijklmnopqrstuvwxyz"
4831 puts $f "abcdefghijklmnopqrstuvwxyz"
4832 close $f
4833 set f [open $path(test3) a]
4834 set c [tell $f]
4835 close $f
4836 set c
4837 } 54
4838 test io-34.20 {Tcl_Tell combined with writing} {
4839 set f [open $path(test3) w]
4840 set l ""
4841 seek $f 29 start
4842 lappend l [tell $f]
4843 puts -nonewline $f a
4844 seek $f 39 start
4845 lappend l [tell $f]
4846 puts -nonewline $f a
4847 lappend l [tell $f]
4848 seek $f 407 end
4849 lappend l [tell $f]
4850 close $f
4851 set l
4852 } {29 39 40 447}
4853 test io-34.21 {Tcl_Seek and Tcl_Tell on large files} {largefileSupport extensive} {
4854 file delete $path(test3)
4855 set f [open $path(test3) w]
4856 fconfigure $f -encoding binary
4857 set l ""
4858 lappend l [tell $f]
4859 puts -nonewline $f abcdef
4860 lappend l [tell $f]
4861 flush $f
4862 lappend l [tell $f]
4863 # 4GB offset!
4864 seek $f 0x100000000
4865 lappend l [tell $f]
4866 puts -nonewline $f abcdef
4867 lappend l [tell $f]
4868 close $f
4869 lappend l [file size $path(test3)]
4870 # truncate...
4871 close [open $path(test3) w]
4872 lappend l [file size $path(test3)]
4873 set l
4874 } {0 6 6 4294967296 4294967302 4294967302 0}
4875
4876 # Test Tcl_Eof
4877
4878 test io-35.1 {Tcl_Eof} {
4879 file delete $path(test1)
4880 set f [open $path(test1) w]
4881 puts $f hello
4882 puts $f hello
4883 close $f
4884 set f [open $path(test1)]
4885 set x [eof $f]
4886 lappend x [eof $f]
4887 gets $f
4888 lappend x [eof $f]
4889 gets $f
4890 lappend x [eof $f]
4891 gets $f
4892 lappend x [eof $f]
4893 lappend x [eof $f]
4894 close $f
4895 set x
4896 } {0 0 0 0 1 1}
4897 test io-35.2 {Tcl_Eof with pipe} stdio {
4898 file delete $path(pipe)
4899 set f1 [open $path(pipe) w]
4900 puts $f1 {gets stdin}
4901 puts $f1 {puts hello}
4902 close $f1
4903 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4904 puts $f1 hello
4905 set x [eof $f1]
4906 flush $f1
4907 lappend x [eof $f1]
4908 gets $f1
4909 lappend x [eof $f1]
4910 gets $f1
4911 lappend x [eof $f1]
4912 close $f1
4913 set x
4914 } {0 0 0 1}
4915 test io-35.3 {Tcl_Eof with pipe} stdio {
4916 file delete $path(pipe)
4917 set f1 [open $path(pipe) w]
4918 puts $f1 {gets stdin}
4919 puts $f1 {puts hello}
4920 close $f1
4921 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4922 puts $f1 hello
4923 set x [eof $f1]
4924 flush $f1
4925 lappend x [eof $f1]
4926 gets $f1
4927 lappend x [eof $f1]
4928 gets $f1
4929 lappend x [eof $f1]
4930 gets $f1
4931 lappend x [eof $f1]
4932 gets $f1
4933 lappend x [eof $f1]
4934 close $f1
4935 set x
4936 } {0 0 0 1 1 1}
4937 test io-35.4 {Tcl_Eof, eof detection on nonblocking file} {nonBlockFiles} {
4938 file delete $path(test1)
4939 set f [open $path(test1) w]
4940 close $f
4941 set f [open $path(test1) r]
4942 fconfigure $f -blocking off
4943 set l ""
4944 lappend l [gets $f]
4945 lappend l [eof $f]
4946 close $f
4947 set l
4948 } {{} 1}
4949 test io-35.5 {Tcl_Eof, eof detection on nonblocking pipe} stdio {
4950 file delete $path(pipe)
4951 set f [open $path(pipe) w]
4952 puts $f {
4953 exit
4954 }
4955 close $f
4956 set f [open "|[list [interpreter] $path(pipe)]" r]
4957 set l ""
4958 lappend l [gets $f]
4959 lappend l [eof $f]
4960 close $f
4961 set l
4962 } {{} 1}
4963 test io-35.6 {Tcl_Eof, eof char, lf write, auto read} {
4964 file delete $path(test1)
4965 set f [open $path(test1) w]
4966 fconfigure $f -translation lf -eofchar \x1A
4967 puts $f abc\ndef
4968 close $f
4969 set s [file size $path(test1)]
4970 set f [open $path(test1) r]
4971 fconfigure $f -translation auto -eofchar \x1A
4972 set l [string length [read $f]]
4973 set e [eof $f]
4974 close $f
4975 list $s $l $e
4976 } {9 8 1}
4977 test io-35.7 {Tcl_Eof, eof char, lf write, lf read} {
4978 file delete $path(test1)
4979 set f [open $path(test1) w]
4980 fconfigure $f -translation lf -eofchar \x1A
4981 puts $f abc\ndef
4982 close $f
4983 set s [file size $path(test1)]
4984 set f [open $path(test1) r]
4985 fconfigure $f -translation lf -eofchar \x1A
4986 set l [string length [read $f]]
4987 set e [eof $f]
4988 close $f
4989 list $s $l $e
4990 } {9 8 1}
4991 test io-35.8 {Tcl_Eof, eof char, cr write, auto read} {
4992 file delete $path(test1)
4993 set f [open $path(test1) w]
4994 fconfigure $f -translation cr -eofchar \x1A
4995 puts $f abc\ndef
4996 close $f
4997 set s [file size $path(test1)]
4998 set f [open $path(test1) r]
4999 fconfigure $f -translation auto -eofchar \x1A
5000 set l [string length [read $f]]
5001 set e [eof $f]
5002 close $f
5003 list $s $l $e
5004 } {9 8 1}
5005 test io-35.9 {Tcl_Eof, eof char, cr write, cr read} {
5006 file delete $path(test1)
5007 set f [open $path(test1) w]
5008 fconfigure $f -translation cr -eofchar \x1A
5009 puts $f abc\ndef
5010 close $f
5011 set s [file size $path(test1)]
5012 set f [open $path(test1) r]
5013 fconfigure $f -translation cr -eofchar \x1A
5014 set l [string length [read $f]]
5015 set e [eof $f]
5016 close $f
5017 list $s $l $e
5018 } {9 8 1}
5019 test io-35.10 {Tcl_Eof, eof char, crlf write, auto read} {
5020 file delete $path(test1)
5021 set f [open $path(test1) w]
5022 fconfigure $f -translation crlf -eofchar \x1A
5023 puts $f abc\ndef
5024 close $f
5025 set s [file size $path(test1)]
5026 set f [open $path(test1) r]
5027 fconfigure $f -translation auto -eofchar \x1A
5028 set l [string length [read $f]]
5029 set e [eof $f]
5030 close $f
5031 list $s $l $e
5032 } {11 8 1}
5033 test io-35.11 {Tcl_Eof, eof char, crlf write, crlf read} {
5034 file delete $path(test1)
5035 set f [open $path(test1) w]
5036 fconfigure $f -translation crlf -eofchar \x1A
5037 puts $f abc\ndef
5038 close $f
5039 set s [file size $path(test1)]
5040 set f [open $path(test1) r]
5041 fconfigure $f -translation crlf -eofchar \x1A
5042 set l [string length [read $f]]
5043 set e [eof $f]
5044 close $f
5045 list $s $l $e
5046 } {11 8 1}
5047 test io-35.12 {Tcl_Eof, eof char in middle, lf write, auto read} {
5048 file delete $path(test1)
5049 set f [open $path(test1) w]
5050 fconfigure $f -translation lf -eofchar {}
5051 set i [format abc\ndef\n%cqrs\nuvw 26]
5052 puts $f $i
5053 close $f
5054 set c [file size $path(test1)]
5055 set f [open $path(test1) r]
5056 fconfigure $f -translation auto -eofchar \x1A
5057 set l [string length [read $f]]
5058 set e [eof $f]
5059 close $f
5060 list $c $l $e
5061 } {17 8 1}
5062 test io-35.13 {Tcl_Eof, eof char in middle, lf write, lf read} {
5063 file delete $path(test1)
5064 set f [open $path(test1) w]
5065 fconfigure $f -translation lf -eofchar {}
5066 set i [format abc\ndef\n%cqrs\nuvw 26]
5067 puts $f $i
5068 close $f
5069 set c [file size $path(test1)]
5070 set f [open $path(test1) r]
5071 fconfigure $f -translation lf -eofchar \x1A
5072 set l [string length [read $f]]
5073 set e [eof $f]
5074 close $f
5075 list $c $l $e
5076 } {17 8 1}
5077 test io-35.14 {Tcl_Eof, eof char in middle, cr write, auto read} {
5078 file delete $path(test1)
5079 set f [open $path(test1) w]
5080 fconfigure $f -translation cr -eofchar {}
5081 set i [format abc\ndef\n%cqrs\nuvw 26]
5082 puts $f $i
5083 close $f
5084 set c [file size $path(test1)]
5085 set f [open $path(test1) r]
5086 fconfigure $f -translation auto -eofchar \x1A
5087 set l [string length [read $f]]
5088 set e [eof $f]
5089 close $f
5090 list $c $l $e
5091 } {17 8 1}
5092 test io-35.15 {Tcl_Eof, eof char in middle, cr write, cr read} {
5093 file delete $path(test1)
5094 set f [open $path(test1) w]
5095 fconfigure $f -translation cr -eofchar {}
5096 set i [format abc\ndef\n%cqrs\nuvw 26]
5097 puts $f $i
5098 close $f
5099 set c [file size $path(test1)]
5100 set f [open $path(test1) r]
5101 fconfigure $f -translation cr -eofchar \x1A
5102 set l [string length [read $f]]
5103 set e [eof $f]
5104 close $f
5105 list $c $l $e
5106 } {17 8 1}
5107 test io-35.16 {Tcl_Eof, eof char in middle, crlf write, auto read} {
5108 file delete $path(test1)
5109 set f [open $path(test1) w]
5110 fconfigure $f -translation crlf -eofchar {}
5111 set i [format abc\ndef\n%cqrs\nuvw 26]
5112 puts $f $i
5113 close $f
5114 set c [file size $path(test1)]
5115 set f [open $path(test1) r]
5116 fconfigure $f -translation auto -eofchar \x1A
5117 set l [string length [read $f]]
5118 set e [eof $f]
5119 close $f
5120 list $c $l $e
5121 } {21 8 1}
5122 test io-35.17 {Tcl_Eof, eof char in middle, crlf write, crlf read} {
5123 file delete $path(test1)
5124 set f [open $path(test1) w]
5125 fconfigure $f -translation crlf -eofchar {}
5126 set i [format abc\ndef\n%cqrs\nuvw 26]
5127 puts $f $i
5128 close $f
5129 set c [file size $path(test1)]
5130 set f [open $path(test1) r]
5131 fconfigure $f -translation crlf -eofchar \x1A
5132 set l [string length [read $f]]
5133 set e [eof $f]
5134 close $f
5135 list $c $l $e
5136 } {21 8 1}
5137 test io-35.18 {Tcl_Eof, eof char, cr write, crlf read} -body {
5138 file delete $path(test1)
5139 set f [open $path(test1) w]
5140 fconfigure $f -translation cr
5141 puts $f abc\ndef
5142 close $f
5143 set s [file size $path(test1)]
5144 set f [open $path(test1) r]
5145 fconfigure $f -translation crlf
5146 set l [string length [set in [read $f]]]
5147 set e [eof $f]
5148 close $f
5149 list $s $l $e [scan [string index $in end] %c]
5150 } -result {8 8 1 13}
5151 test io-35.18a {Tcl_Eof, eof char, cr write, crlf read} -body {
5152 file delete $path(test1)
5153 set f [open $path(test1) w]
5154 fconfigure $f -translation cr -eofchar \x1A
5155 puts $f abc\ndef
5156 close $f
5157 set s [file size $path(test1)]
5158 set f [open $path(test1) r]
5159 fconfigure $f -translation crlf -eofchar \x1A
5160 set l [string length [set in [read $f]]]
5161 set e [eof $f]
5162 close $f
5163 list $s $l $e [scan [string index $in end] %c]
5164 } -result {9 8 1 13}
5165 test io-35.18b {Tcl_Eof, eof char, cr write, crlf read} -body {
5166 file delete $path(test1)
5167 set f [open $path(test1) w]
5168 fconfigure $f -translation cr -eofchar \x1A
5169 puts $f {}
5170 close $f
5171 set s [file size $path(test1)]
5172 set f [open $path(test1) r]
5173 fconfigure $f -translation crlf -eofchar \x1A
5174 set l [string length [set in [read $f]]]
5175 set e [eof $f]
5176 close $f
5177 list $s $l $e [scan [string index $in end] %c]
5178 } -result {2 1 1 13}
5179 test io-35.18c {Tcl_Eof, eof char, cr write, crlf read} -body {
5180 file delete $path(test1)
5181 set f [open $path(test1) w]
5182 fconfigure $f -translation cr
5183 puts $f {}
5184 close $f
5185 set s [file size $path(test1)]
5186 set f [open $path(test1) r]
5187 fconfigure $f -translation crlf
5188 set l [string length [set in [read $f]]]
5189 set e [eof $f]
5190 close $f
5191 list $s $l $e [scan [string index $in end] %c]
5192 } -result {1 1 1 13}
5193 test io-35.19 {Tcl_Eof, eof char in middle, cr write, crlf read} -body {
5194 file delete $path(test1)
5195 set f [open $path(test1) w]
5196 fconfigure $f -translation cr -eofchar {}
5197 set i [format abc\ndef\n%cqrs\nuvw 26]
5198 puts $f $i
5199 close $f
5200 set c [file size $path(test1)]
5201 set f [open $path(test1) r]
5202 fconfigure $f -translation crlf -eofchar \x1A
5203 set l [string length [set in [read $f]]]
5204 set e [eof $f]
5205 close $f
5206 list $c $l $e [scan [string index $in end] %c]
5207 } -result {17 8 1 13}
5208 test io-35.20 {Tcl_Eof, eof char in middle, cr write, crlf read} {
5209 file delete $path(test1)
5210 set f [open $path(test1) w]
5211 fconfigure $f -translation cr -eofchar {}
5212 set i [format \n%cqrsuvw 26]
5213 puts $f $i
5214 close $f
5215 set c [file size $path(test1)]
5216 set f [open $path(test1) r]
5217 fconfigure $f -translation crlf -eofchar \x1A
5218 set l [string length [set in [read $f]]]
5219 set e [eof $f]
5220 close $f
5221 list $c $l $e [scan [string index $in end] %c]
5222 } {9 1 1 13}
5223
5224 # Test Tcl_InputBlocked
5225
5226 test io-36.1 {Tcl_InputBlocked on nonblocking pipe} stdio {
5227 set f1 [open "|[list [interpreter]]" r+]
5228 puts $f1 {puts hello_from_pipe}
5229 flush $f1
5230 gets $f1
5231 fconfigure $f1 -blocking off -buffering full
5232 puts $f1 {puts hello}
5233 set x ""
5234 lappend x [gets $f1]
5235 lappend x [fblocked $f1]
5236 flush $f1
5237 after 200
5238 lappend x [gets $f1]
5239 lappend x [fblocked $f1]
5240 lappend x [gets $f1]
5241 lappend x [fblocked $f1]
5242 close $f1
5243 set x
5244 } {{} 1 hello 0 {} 1}
5245 test io-36.1.1 {Tcl_InputBlocked on nonblocking binary pipe} stdio {
5246 set f1 [open "|[list [interpreter]]" r+]
5247 chan configure $f1 -encoding binary -translation lf -eofchar {}
5248 puts $f1 {
5249 chan configure stdout -encoding binary -translation lf -eofchar {}
5250 puts hello_from_pipe
5251 }
5252 flush $f1
5253 gets $f1
5254 fconfigure $f1 -blocking off -buffering full
5255 puts $f1 {puts hello}
5256 set x ""
5257 lappend x [gets $f1]
5258 lappend x [fblocked $f1]
5259 flush $f1
5260 after 200
5261 lappend x [gets $f1]
5262 lappend x [fblocked $f1]
5263 lappend x [gets $f1]
5264 lappend x [fblocked $f1]
5265 close $f1
5266 set x
5267 } {{} 1 hello 0 {} 1}
5268 test io-36.2 {Tcl_InputBlocked on blocking pipe} stdio {
5269 set f1 [open "|[list [interpreter]]" r+]
5270 fconfigure $f1 -buffering line
5271 puts $f1 {puts hello_from_pipe}
5272 set x ""
5273 lappend x [gets $f1]
5274 lappend x [fblocked $f1]
5275 puts $f1 {exit}
5276 lappend x [gets $f1]
5277 lappend x [fblocked $f1]
5278 lappend x [eof $f1]
5279 close $f1
5280 set x
5281 } {hello_from_pipe 0 {} 0 1}
5282 test io-36.3 {Tcl_InputBlocked vs files, short read} {
5283 file delete $path(test1)
5284 set f [open $path(test1) w]
5285 puts $f abcdefghijklmnop
5286 close $f
5287 set f [open $path(test1) r]
5288 set l ""
5289 lappend l [fblocked $f]
5290 lappend l [read $f 3]
5291 lappend l [fblocked $f]
5292 lappend l [read -nonewline $f]
5293 lappend l [fblocked $f]
5294 lappend l [eof $f]
5295 close $f
5296 set l
5297 } {0 abc 0 defghijklmnop 0 1}
5298 test io-36.4 {Tcl_InputBlocked vs files, event driven read} {fileevent} {
5299 proc in {f} {
5300 variable l
5301 variable x
5302 lappend l [read $f 3]
5303 if {[eof $f]} {lappend l eof; close $f; set x done}
5304 }
5305 file delete $path(test1)
5306 set f [open $path(test1) w]
5307 puts $f abcdefghijklmnop
5308 close $f
5309 set f [open $path(test1) r]
5310 set l ""
5311 fileevent $f readable [namespace code [list in $f]]
5312 variable x
5313 vwait [namespace which -variable x]
5314 set l
5315 } {abc def ghi jkl mno {p
5316 } eof}
5317 test io-36.5 {Tcl_InputBlocked vs files, short read, nonblocking} {nonBlockFiles} {
5318 file delete $path(test1)
5319 set f [open $path(test1) w]
5320 puts $f abcdefghijklmnop
5321 close $f
5322 set f [open $path(test1) r]
5323 fconfigure $f -blocking off
5324 set l ""
5325 lappend l [fblocked $f]
5326 lappend l [read $f 3]
5327 lappend l [fblocked $f]
5328 lappend l [read -nonewline $f]
5329 lappend l [fblocked $f]
5330 lappend l [eof $f]
5331 close $f
5332 set l
5333 } {0 abc 0 defghijklmnop 0 1}
5334 test io-36.6 {Tcl_InputBlocked vs files, event driven read} {nonBlockFiles fileevent} {
5335 proc in {f} {
5336 variable l
5337 variable x
5338 lappend l [read $f 3]
5339 if {[eof $f]} {lappend l eof; close $f; set x done}
5340 }
5341 file delete $path(test1)
5342 set f [open $path(test1) w]
5343 puts $f abcdefghijklmnop
5344 close $f
5345 set f [open $path(test1) r]
5346 fconfigure $f -blocking off
5347 set l ""
5348 fileevent $f readable [namespace code [list in $f]]
5349 variable x
5350 vwait [namespace which -variable x]
5351 set l
5352 } {abc def ghi jkl mno {p
5353 } eof}
5354
5355 # Test Tcl_InputBuffered
5356
5357 test io-37.1 {Tcl_InputBuffered} {testchannel} {
5358 set f [open $path(longfile) r]
5359 fconfigure $f -buffersize 4096
5360 read $f 3
5361 set l ""
5362 lappend l [testchannel inputbuffered $f]
5363 lappend l [tell $f]
5364 close $f
5365 set l
5366 } {4093 3}
5367 test io-37.2 {Tcl_InputBuffered, test input flushing on seek} {testchannel} {
5368 set f [open $path(longfile) r]
5369 fconfigure $f -buffersize 4096
5370 read $f 3
5371 set l ""
5372 lappend l [testchannel inputbuffered $f]
5373 lappend l [tell $f]
5374 seek $f 0 current
5375 lappend l [testchannel inputbuffered $f]
5376 lappend l [tell $f]
5377 close $f
5378 set l
5379 } {4093 3 0 3}
5380
5381 # Test Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize
5382
5383 test io-38.1 {Tcl_GetChannelBufferSize, default buffer size} {
5384 set f [open $path(longfile) r]
5385 set s [fconfigure $f -buffersize]
5386 close $f
5387 set s
5388 } 4096
5389 test io-38.2 {Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize} {
5390 set f [open $path(longfile) r]
5391 set l ""
5392 lappend l [fconfigure $f -buffersize]
5393 fconfigure $f -buffersize 10000
5394 lappend l [fconfigure $f -buffersize]
5395 fconfigure $f -buffersize 1
5396 lappend l [fconfigure $f -buffersize]
5397 fconfigure $f -buffersize -1
5398 lappend l [fconfigure $f -buffersize]
5399 fconfigure $f -buffersize 0
5400 lappend l [fconfigure $f -buffersize]
5401 fconfigure $f -buffersize 100000
5402 lappend l [fconfigure $f -buffersize]
5403 fconfigure $f -buffersize 10000000
5404 lappend l [fconfigure $f -buffersize]
5405 close $f
5406 set l
5407 } {4096 10000 1 1 1 100000 1048576}
5408 test io-38.3 {Tcl_SetChannelBufferSize, changing buffersize between reads} {
5409 # This test crashes the interp if Bug #427196 is not fixed
5410
5411 set chan [open [info script] r]
5412 fconfigure $chan -buffersize 10
5413 set var [read $chan 2]
5414 fconfigure $chan -buffersize 32
5415 append var [read $chan]
5416 close $chan
5417 } {}
5418
5419 # Test Tcl_SetChannelOption, Tcl_GetChannelOption
5420
5421 test io-39.1 {Tcl_GetChannelOption} {
5422 file delete $path(test1)
5423 set f1 [open $path(test1) w]
5424 set x [fconfigure $f1 -blocking]
5425 close $f1
5426 set x
5427 } 1
5428 test io-39.2 {Tcl_GetChannelOption} {
5429 file delete $path(test1)
5430 set f1 [open $path(test1) w]
5431 set x [fconfigure $f1 -buffering]
5432 close $f1
5433 set x
5434 } full
5435 test io-39.3 {Tcl_GetChannelOption} {
5436 file delete $path(test1)
5437 set f1 [open $path(test1) w]
5438 fconfigure $f1 -buffering line
5439 set x [fconfigure $f1 -buffering]
5440 close $f1
5441 set x
5442 } line
5443 test io-39.4 {Tcl_GetChannelOption, Tcl_SetChannelOption} {
5444 file delete $path(test1)
5445 set f1 [open $path(test1) w]
5446 set l ""
5447 lappend l [fconfigure $f1 -buffering]
5448 fconfigure $f1 -buffering line
5449 lappend l [fconfigure $f1 -buffering]
5450 fconfigure $f1 -buffering none
5451 lappend l [fconfigure $f1 -buffering]
5452 fconfigure $f1 -buffering line
5453 lappend l [fconfigure $f1 -buffering]
5454 fconfigure $f1 -buffering full
5455 lappend l [fconfigure $f1 -buffering]
5456 close $f1
5457 set l
5458 } {full line none line full}
5459 test io-39.5 {Tcl_GetChannelOption, invariance} {
5460 file delete $path(test1)
5461 set f1 [open $path(test1) w]
5462 set l ""
5463 lappend l [fconfigure $f1 -buffering]
5464 lappend l [list [catch {fconfigure $f1 -buffering green} msg] $msg]
5465 lappend l [fconfigure $f1 -buffering]
5466 close $f1
5467 set l
5468 } {full {1 {bad value for -buffering: must be one of full, line, or none}} full}
5469 test io-39.6 {Tcl_SetChannelOption, multiple options} {
5470 file delete $path(test1)
5471 set f1 [open $path(test1) w]
5472 fconfigure $f1 -translation lf -buffering line
5473 puts $f1 hello
5474 puts $f1 bye
5475 set x [file size $path(test1)]
5476 close $f1
5477 set x
5478 } 10
5479 test io-39.7 {Tcl_SetChannelOption, buffering, translation} {
5480 file delete $path(test1)
5481 set f1 [open $path(test1) w]
5482 fconfigure $f1 -translation lf
5483 puts $f1 hello
5484 puts $f1 bye
5485 set x ""
5486 fconfigure $f1 -buffering line
5487 lappend x [file size $path(test1)]
5488 puts $f1 really_bye
5489 lappend x [file size $path(test1)]
5490 close $f1
5491 set x
5492 } {0 21}
5493 test io-39.8 {Tcl_SetChannelOption, different buffering options} {
5494 file delete $path(test1)
5495 set f1 [open $path(test1) w]
5496 set l ""
5497 fconfigure $f1 -translation lf -buffering none -eofchar {}
5498 puts -nonewline $f1 hello
5499 lappend l [file size $path(test1)]
5500 puts -nonewline $f1 hello
5501 lappend l [file size $path(test1)]
5502 fconfigure $f1 -buffering full
5503 puts -nonewline $f1 hello
5504 lappend l [file size $path(test1)]
5505 fconfigure $f1 -buffering none
5506 lappend l [file size $path(test1)]
5507 puts -nonewline $f1 hello
5508 lappend l [file size $path(test1)]
5509 close $f1
5510 lappend l [file size $path(test1)]
5511 set l
5512 } {5 10 10 10 20 20}
5513 test io-39.9 {Tcl_SetChannelOption, blocking mode} {nonBlockFiles} {
5514 file delete $path(test1)
5515 set f1 [open $path(test1) w]
5516 close $f1
5517 set f1 [open $path(test1) r]
5518 set x ""
5519 lappend x [fconfigure $f1 -blocking]
5520 fconfigure $f1 -blocking off
5521 lappend x [fconfigure $f1 -blocking]
5522 lappend x [gets $f1]
5523 lappend x [read $f1 1000]
5524 lappend x [fblocked $f1]
5525 lappend x [eof $f1]
5526 close $f1
5527 set x
5528 } {1 0 {} {} 0 1}
5529 test io-39.10 {Tcl_SetChannelOption, blocking mode} stdio {
5530 file delete $path(pipe)
5531 set f1 [open $path(pipe) w]
5532 puts $f1 {
5533 gets stdin
5534 after 100
5535 puts hi
5536 gets stdin
5537 }
5538 close $f1
5539 set x ""
5540 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
5541 fconfigure $f1 -blocking off -buffering line
5542 lappend x [fconfigure $f1 -blocking]
5543 lappend x [gets $f1]
5544 lappend x [fblocked $f1]
5545 fconfigure $f1 -blocking on
5546 puts $f1 hello
5547 fconfigure $f1 -blocking off
5548 lappend x [gets $f1]
5549 lappend x [fblocked $f1]
5550 fconfigure $f1 -blocking on
5551 puts $f1 bye
5552 fconfigure $f1 -blocking off
5553 lappend x [gets $f1]
5554 lappend x [fblocked $f1]
5555 fconfigure $f1 -blocking on
5556 lappend x [fconfigure $f1 -blocking]
5557 lappend x [gets $f1]
5558 lappend x [fblocked $f1]
5559 lappend x [eof $f1]
5560 lappend x [gets $f1]
5561 lappend x [eof $f1]
5562 close $f1
5563 set x
5564 } {0 {} 1 {} 1 {} 1 1 hi 0 0 {} 1}
5565 test io-39.11 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size clipped to lower bound} {
5566 file delete $path(test1)
5567 set f [open $path(test1) w]
5568 fconfigure $f -buffersize -10
5569 set x [fconfigure $f -buffersize]
5570 close $f
5571 set x
5572 } 1
5573 test io-39.12 {Tcl_SetChannelOption, Tcl_GetChannelOption buffer size clipped to upper bound} {
5574 file delete $path(test1)
5575 set f [open $path(test1) w]
5576 fconfigure $f -buffersize 10000000
5577 set x [fconfigure $f -buffersize]
5578 close $f
5579 set x
5580 } 1048576
5581 test io-39.13 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size} {
5582 file delete $path(test1)
5583 set f [open $path(test1) w]
5584 fconfigure $f -buffersize 40000
5585 set x [fconfigure $f -buffersize]
5586 close $f
5587 set x
5588 } 40000
5589 test io-39.14 {Tcl_SetChannelOption: -encoding, binary & utf-8} {
5590 file delete $path(test1)
5591 set f [open $path(test1) w]
5592 fconfigure $f -encoding {}
5593 puts -nonewline $f \xE7\x89\xA6
5594 close $f
5595 set f [open $path(test1) r]
5596 fconfigure $f -encoding utf-8
5597 set x [read $f]
5598 close $f
5599 set x
5600 } \u7266
5601 test io-39.15 {Tcl_SetChannelOption: -encoding, binary & utf-8} {
5602 file delete $path(test1)
5603 set f [open $path(test1) w]
5604 fconfigure $f -encoding binary
5605 puts -nonewline $f \xE7\x89\xA6
5606 close $f
5607 set f [open $path(test1) r]
5608 fconfigure $f -encoding utf-8
5609 set x [read $f]
5610 close $f
5611 set x
5612 } \u7266
5613 test io-39.16 {Tcl_SetChannelOption: -encoding (shortened to "-en"), errors} -body {
5614 file delete $path(test1)
5615 set f [open $path(test1) w]
5616 fconfigure $f -en foobar
5617 } -cleanup {
5618 close $f
5619 } -returnCodes 1 -result {unknown encoding "foobar"}
5620 test io-39.16a {Tcl_SetChannelOption: -encoding (invalid shortening to "-e"), errors} -body {
5621 file delete $path(test1)
5622 set f [open $path(test1) w]
5623 fconfigure $f -e foobar
5624 } -cleanup {
5625 close $f
5626 } -returnCodes 1 -match glob -result {bad option "-e": should be one of *}
5627 test io-39.17 {Tcl_SetChannelOption: -encoding, clearing CHANNEL_NEED_MORE_DATA} {stdio fileevent} {
5628 set f [open "|[list [interpreter] $path(cat)]" r+]
5629 fconfigure $f -encoding binary
5630 puts -nonewline $f "\xE7"
5631 flush $f
5632 fconfigure $f -encoding utf-8 -blocking 0
5633 variable x {}
5634 fileevent $f readable [namespace code { lappend x [read $f] }]
5635 vwait [namespace which -variable x]
5636 after 300 [namespace code { lappend x timeout }]
5637 vwait [namespace which -variable x]
5638 fconfigure $f -encoding utf-8
5639 vwait [namespace which -variable x]
5640 after 300 [namespace code { lappend x timeout }]
5641 vwait [namespace which -variable x]
5642 fconfigure $f -encoding binary
5643 vwait [namespace which -variable x]
5644 after 300 [namespace code { lappend x timeout }]
5645 vwait [namespace which -variable x]
5646 close $f
5647 set x
5648 } "{} timeout {} timeout \xE7 timeout"
5649 test io-39.18 {Tcl_SetChannelOption, setting read mode independently} \
5650 {socket} {
5651 proc accept {s a p} {close $s}
5652 set s1 [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5653 set port [lindex [fconfigure $s1 -sockname] 2]
5654 set s2 [socket 127.0.0.1 $port]
5655 update
5656 fconfigure $s2 -translation {auto lf}
5657 set modes [fconfigure $s2 -translation]
5658 close $s1
5659 close $s2
5660 set modes
5661 } {auto lf}
5662 test io-39.19 {Tcl_SetChannelOption, setting read mode independently} \
5663 {socket} {
5664 proc accept {s a p} {close $s}
5665 set s1 [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5666 set port [lindex [fconfigure $s1 -sockname] 2]
5667 set s2 [socket 127.0.0.1 $port]
5668 update
5669 fconfigure $s2 -translation {auto crlf}
5670 set modes [fconfigure $s2 -translation]
5671 close $s1
5672 close $s2
5673 set modes
5674 } {auto crlf}
5675 test io-39.20 {Tcl_SetChannelOption, setting read mode independently} \
5676 {socket} {
5677 proc accept {s a p} {close $s}
5678 set s1 [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5679 set port [lindex [fconfigure $s1 -sockname] 2]
5680 set s2 [socket 127.0.0.1 $port]
5681 update
5682 fconfigure $s2 -translation {auto cr}
5683 set modes [fconfigure $s2 -translation]
5684 close $s1
5685 close $s2
5686 set modes
5687 } {auto cr}
5688 test io-39.21 {Tcl_SetChannelOption, setting read mode independently} \
5689 {socket} {
5690 proc accept {s a p} {close $s}
5691 set s1 [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5692 set port [lindex [fconfigure $s1 -sockname] 2]
5693 set s2 [socket 127.0.0.1 $port]
5694 update
5695 fconfigure $s2 -translation {auto auto}
5696 set modes [fconfigure $s2 -translation]
5697 close $s1
5698 close $s2
5699 set modes
5700 } {auto crlf}
5701 test io-39.22 {Tcl_SetChannelOption, invariance} {unix} {
5702 file delete $path(test1)
5703 set f1 [open $path(test1) w+]
5704 set l ""
5705 lappend l [fconfigure $f1 -eofchar]
5706 fconfigure $f1 -eofchar {ON GO}
5707 lappend l [fconfigure $f1 -eofchar]
5708 fconfigure $f1 -eofchar D
5709 lappend l [fconfigure $f1 -eofchar]
5710 close $f1
5711 set l
5712 } {{{} {}} {O G} {D D}}
5713 test io-39.22a {Tcl_SetChannelOption, invariance} {
5714 file delete $path(test1)
5715 set f1 [open $path(test1) w+]
5716 set l [list]
5717 fconfigure $f1 -eofchar {ON GO}
5718 lappend l [fconfigure $f1 -eofchar]
5719 fconfigure $f1 -eofchar D
5720 lappend l [fconfigure $f1 -eofchar]
5721 lappend l [list [catch {fconfigure $f1 -eofchar {1 2 3}} msg] $msg]
5722 close $f1
5723 set l
5724 } {{O G} {D D} {1 {bad value for -eofchar: should be a list of zero, one, or two elements}}}
5725 test io-39.23 {Tcl_GetChannelOption, server socket is not readable or
5726 writable, it should still have valid -eofchar and -translation options } {
5727 set l [list]
5728 set sock [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5729 lappend l [fconfigure $sock -eofchar] [fconfigure $sock -translation]
5730 close $sock
5731 set l
5732 } {{{}} auto}
5733 test io-39.24 {Tcl_SetChannelOption, server socket is not readable or
5734 writable so we can't change -eofchar or -translation } {
5735 set l [list]
5736 set sock [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5737 fconfigure $sock -eofchar D -translation lf
5738 lappend l [fconfigure $sock -eofchar] [fconfigure $sock -translation]
5739 close $sock
5740 set l
5741 } {{{}} auto}
5742
5743 test io-40.1 {POSIX open access modes: RDWR} {
5744 file delete $path(test3)
5745 set f [open $path(test3) w]
5746 puts $f xyzzy
5747 close $f
5748 set f [open $path(test3) RDWR]
5749 puts -nonewline $f "ab"
5750 seek $f 0 current
5751 set x [gets $f]
5752 close $f
5753 set f [open $path(test3) r]
5754 lappend x [gets $f]
5755 close $f
5756 set x
5757 } {zzy abzzy}
5758 test io-40.2 {POSIX open access modes: CREAT} {unix notWsl} {
5759 file delete $path(test3)
5760 set f [open $path(test3) {WRONLY CREAT} 0o600]
5761 file stat $path(test3) stats
5762 set x [format "0o%o" [expr {$stats(mode)&0o777}]]
5763 puts $f "line 1"
5764 close $f
5765 set f [open $path(test3) r]
5766 lappend x [gets $f]
5767 close $f
5768 set x
5769 } {0o600 {line 1}}
5770 test io-40.3 {POSIX open access modes: CREAT} {unix umask notWsl} {
5771 # This test only works if your umask is 2, like ouster's.
5772 file delete $path(test3)
5773 set f [open $path(test3) {WRONLY CREAT}]
5774 close $f
5775 file stat $path(test3) stats
5776 format 0o%03o [expr {$stats(mode)&0o777}]
5777 } [format 0o%03o [expr {0o666 & ~ $umaskValue}]]
5778 test io-40.4 {POSIX open access modes: CREAT} {
5779 file delete $path(test3)
5780 set f [open $path(test3) w]
5781 fconfigure $f -eofchar {}
5782 puts $f xyzzy
5783 close $f
5784 set f [open $path(test3) {WRONLY CREAT}]
5785 fconfigure $f -eofchar {}
5786 puts -nonewline $f "ab"
5787 close $f
5788 set f [open $path(test3) r]
5789 set x [gets $f]
5790 close $f
5791 set x
5792 } abzzy
5793 test io-40.5 {POSIX open access modes: APPEND} {
5794 file delete $path(test3)
5795 set f [open $path(test3) w]
5796 fconfigure $f -translation lf -eofchar {}
5797 puts $f xyzzy
5798 close $f
5799 set f [open $path(test3) {WRONLY APPEND}]
5800 fconfigure $f -translation lf
5801 puts $f "new line"
5802 seek $f 0
5803 puts $f "abc"
5804 close $f
5805 set f [open $path(test3) r]
5806 fconfigure $f -translation lf
5807 set x ""
5808 seek $f 6 current
5809 lappend x [gets $f]
5810 lappend x [gets $f]
5811 close $f
5812 set x
5813 } {{new line} abc}
5814 test io-40.6 {POSIX open access modes: EXCL} -match regexp -body {
5815 file delete $path(test3)
5816 set f [open $path(test3) w]
5817 puts $f xyzzy
5818 close $f
5819 open $path(test3) {WRONLY CREAT EXCL}
5820 } -returnCodes error -result {(?i)couldn't open ".*test3": file (already )?exists}
5821 test io-40.7 {POSIX open access modes: EXCL} {
5822 file delete $path(test3)
5823 set f [open $path(test3) {WRONLY CREAT EXCL}]
5824 fconfigure $f -eofchar {}
5825 puts $f "A test line"
5826 close $f
5827 viewFile test3
5828 } {A test line}
5829 test io-40.8 {POSIX open access modes: TRUNC} {
5830 file delete $path(test3)
5831 set f [open $path(test3) w]
5832 puts $f xyzzy
5833 close $f
5834 set f [open $path(test3) {WRONLY TRUNC}]
5835 puts $f abc
5836 close $f
5837 set f [open $path(test3) r]
5838 set x [gets $f]
5839 close $f
5840 set x
5841 } abc
5842 test io-40.9 {POSIX open access modes: NONBLOCK} {nonPortable unix} {
5843 file delete $path(test3)
5844 set f [open $path(test3) {WRONLY NONBLOCK CREAT}]
5845 puts $f "NONBLOCK test"
5846 close $f
5847 set f [open $path(test3) r]
5848 set x [gets $f]
5849 close $f
5850 set x
5851 } {NONBLOCK test}
5852 test io-40.10 {POSIX open access modes: RDONLY} {
5853 set f [open $path(test1) w]
5854 puts $f "two lines: this one"
5855 puts $f "and this"
5856 close $f
5857 set f [open $path(test1) RDONLY]
5858 set x [list [gets $f] [catch {puts $f Test} msg] $msg]
5859 close $f
5860 string compare [string tolower $x] \
5861 [list {two lines: this one} 1 \
5862 [format "channel \"%s\" wasn't opened for writing" $f]]
5863 } 0
5864 test io-40.11 {POSIX open access modes: RDONLY} -match regexp -body {
5865 file delete $path(test3)
5866 open $path(test3) RDONLY
5867 } -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
5868 test io-40.12 {POSIX open access modes: WRONLY} -match regexp -body {
5869 file delete $path(test3)
5870 open $path(test3) WRONLY
5871 } -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
5872 test io-40.13 {POSIX open access modes: WRONLY} {
5873 makeFile xyzzy test3
5874 set f [open $path(test3) WRONLY]
5875 fconfigure $f -eofchar {}
5876 puts -nonewline $f "ab"
5877 seek $f 0 current
5878 set x [list [catch {gets $f} msg] $msg]
5879 close $f
5880 lappend x [viewFile test3]
5881 string compare [string tolower $x] \
5882 [list 1 "channel \"$f\" wasn't opened for reading" abzzy]
5883 } 0
5884 test io-40.14 {POSIX open access modes: RDWR} -match regexp -body {
5885 file delete $path(test3)
5886 open $path(test3) RDWR
5887 } -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
5888 test io-40.15 {POSIX open access modes: RDWR} {
5889 makeFile xyzzy test3
5890 set f [open $path(test3) RDWR]
5891 puts -nonewline $f "ab"
5892 seek $f 0 current
5893 set x [gets $f]
5894 close $f
5895 lappend x [viewFile test3]
5896 } {zzy abzzy}
5897 test io-40.16 {tilde substitution in open} -constraints makeFileInHome -setup {
5898 makeFile {Some text} _test_ ~
5899 } -body {
5900 file exists [file join $::env(HOME) _test_]
5901 } -cleanup {
5902 removeFile _test_ ~
5903 } -result 1
5904 test io-40.17 {tilde substitution in open} {
5905 set home $::env(HOME)
5906 unset ::env(HOME)
5907 set x [list [catch {open ~/foo} msg] $msg]
5908 set ::env(HOME) $home
5909 set x
5910 } {1 {couldn't find HOME environment variable to expand path}}
5911
5912 test io-41.1 {Tcl_FileeventCmd: errors} {fileevent} {
5913 list [catch {fileevent foo} msg] $msg
5914 } {1 {wrong # args: should be "fileevent channelId event ?script?"}}
5915 test io-41.2 {Tcl_FileeventCmd: errors} {fileevent} {
5916 list [catch {fileevent foo bar baz q} msg] $msg
5917 } {1 {wrong # args: should be "fileevent channelId event ?script?"}}
5918 test io-41.3 {Tcl_FileeventCmd: errors} {fileevent} {
5919 list [catch {fileevent gorp readable} msg] $msg
5920 } {1 {can not find channel named "gorp"}}
5921 test io-41.4 {Tcl_FileeventCmd: errors} {fileevent} {
5922 list [catch {fileevent gorp writable} msg] $msg
5923 } {1 {can not find channel named "gorp"}}
5924 test io-41.5 {Tcl_FileeventCmd: errors} {fileevent} {
5925 list [catch {fileevent gorp who-knows} msg] $msg
5926 } {1 {bad event name "who-knows": must be readable or writable}}
5927
5928 #
5929 # Test fileevent on a file
5930 #
5931
5932 set path(foo) [makeFile {} foo]
5933 set f [open $path(foo) w+]
5934
5935 test io-42.1 {Tcl_FileeventCmd: creating, deleting, querying} {fileevent} {
5936 list [fileevent $f readable] [fileevent $f writable]
5937 } {{} {}}
5938 test io-42.2 {Tcl_FileeventCmd: replacing} {fileevent} {
5939 set result {}
5940 fileevent $f r "first script"
5941 lappend result [fileevent $f readable]
5942 fileevent $f r "new script"
5943 lappend result [fileevent $f readable]
5944 fileevent $f r "yet another"
5945 lappend result [fileevent $f readable]
5946 fileevent $f r ""
5947 lappend result [fileevent $f readable]
5948 } {{first script} {new script} {yet another} {}}
5949 test io-42.3 {Tcl_FileeventCmd: replacing, with NULL chars in script} {fileevent} {
5950 set result {}
5951 fileevent $f r "first scr\x00ipt"
5952 lappend result [string length [fileevent $f readable]]
5953 fileevent $f r "new scr\x00ipt"
5954 lappend result [string length [fileevent $f readable]]
5955 fileevent $f r "yet ano\x00ther"
5956 lappend result [string length [fileevent $f readable]]
5957 fileevent $f r ""
5958 lappend result [fileevent $f readable]
5959 } {13 11 12 {}}
5960
5961
5962 test io-43.1 {Tcl_FileeventCmd: creating, deleting, querying} {stdio unixExecs fileevent} {
5963 set result {}
5964 fileevent $f readable "script 1"
5965 lappend result [fileevent $f readable] [fileevent $f writable]
5966 fileevent $f writable "write script"
5967 lappend result [fileevent $f readable] [fileevent $f writable]
5968 fileevent $f readable {}
5969 lappend result [fileevent $f readable] [fileevent $f writable]
5970 fileevent $f writable {}
5971 lappend result [fileevent $f readable] [fileevent $f writable]
5972 } {{script 1} {} {script 1} {write script} {} {write script} {} {}}
5973 test io-43.2 {Tcl_FileeventCmd: deleting when many present} -setup {
5974 set f2 [open "|[list cat -u]" r+]
5975 set f3 [open "|[list cat -u]" r+]
5976 } -constraints {stdio unixExecs fileevent} -body {
5977 set result {}
5978 lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5979 fileevent $f r "read f"
5980 fileevent $f2 r "read f2"
5981 fileevent $f3 r "read f3"
5982 lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5983 fileevent $f2 r {}
5984 lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5985 fileevent $f3 r {}
5986 lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5987 fileevent $f r {}
5988 lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5989 } -cleanup {
5990 catch {close $f2}
5991 catch {close $f3}
5992 } -result {{} {} {} {read f} {read f2} {read f3} {read f} {} {read f3} {read f} {} {} {} {} {}}
5993
5994 test io-44.1 {FileEventProc procedure: normal read event} -setup {
5995 set f2 [open "|[list cat -u]" r+]
5996 set f3 [open "|[list cat -u]" r+]
5997 } -constraints {stdio unixExecs fileevent} -body {
5998 fileevent $f2 readable [namespace code {
5999 set x [gets $f2]; fileevent $f2 readable {}
6000 }]
6001 puts $f2 text; flush $f2
6002 variable x initial
6003 vwait [namespace which -variable x]
6004 set x
6005 } -cleanup {
6006 catch {close $f2}
6007 catch {close $f3}
6008 } -result {text}
6009 test io-44.2 {FileEventProc procedure: error in read event} -constraints {
6010 stdio unixExecs fileevent
6011 } -setup {
6012 set f2 [open "|[list cat -u]" r+]
6013 set f3 [open "|[list cat -u]" r+]
6014 proc myHandler {msg options} {
6015 variable x $msg
6016 }
6017 set handler [interp bgerror {}]
6018 interp bgerror {} [namespace which myHandler]
6019 } -body {
6020 fileevent $f2 readable {error bogus}
6021 puts $f2 text; flush $f2
6022 variable x initial
6023 vwait [namespace which -variable x]
6024 list $x [fileevent $f2 readable]
6025 } -cleanup {
6026 interp bgerror {} $handler
6027 catch {close $f2}
6028 catch {close $f3}
6029 } -result {bogus {}}
6030 test io-44.3 {FileEventProc procedure: normal write event} -setup {
6031 set f2 [open "|[list cat -u]" r+]
6032 set f3 [open "|[list cat -u]" r+]
6033 } -constraints {stdio unixExecs fileevent} -body {
6034 fileevent $f2 writable [namespace code {
6035 lappend x "triggered"
6036 incr count -1
6037 if {$count <= 0} {
6038 fileevent $f2 writable {}
6039 }
6040 }]
6041 variable x initial
6042 set count 3
6043 vwait [namespace which -variable x]
6044 vwait [namespace which -variable x]
6045 vwait [namespace which -variable x]
6046 set x
6047 } -cleanup {
6048 catch {close $f2}
6049 catch {close $f3}
6050 } -result {initial triggered triggered triggered}
6051 test io-44.4 {FileEventProc procedure: eror in write event} -constraints {
6052 stdio unixExecs fileevent
6053 } -setup {
6054 set f2 [open "|[list cat -u]" r+]
6055 set f3 [open "|[list cat -u]" r+]
6056 proc myHandler {msg options} {
6057 variable x $msg
6058 }
6059 set handler [interp bgerror {}]
6060 interp bgerror {} [namespace which myHandler]
6061 } -body {
6062 fileevent $f2 writable {error bad-write}
6063 variable x initial
6064 vwait [namespace which -variable x]
6065 list $x [fileevent $f2 writable]
6066 } -cleanup {
6067 interp bgerror {} $handler
6068 catch {close $f2}
6069 catch {close $f3}
6070 } -result {bad-write {}}
6071 test io-44.5 {FileEventProc procedure: end of file} -constraints {
6072 stdio unixExecs fileevent
6073 } -body {
6074 set f4 [open "|[list [interpreter] $path(cat) << foo]" r]
6075 fileevent $f4 readable [namespace code {
6076 if {[gets $f4 line] < 0} {
6077 lappend x eof
6078 fileevent $f4 readable {}
6079 } else {
6080 lappend x $line
6081 }
6082 }]
6083 variable x initial
6084 vwait [namespace which -variable x]
6085 vwait [namespace which -variable x]
6086 set x
6087 } -cleanup {
6088 close $f4
6089 } -result {initial foo eof}
6090
6091 close $f
6092 makeFile "foo bar" foo
6093
6094 test io-45.1 {DeleteFileEvent, cleanup on close} {fileevent} {
6095 set f [open $path(foo) r]
6096 fileevent $f readable [namespace code {
6097 lappend x "binding triggered: \"[gets $f]\""
6098 fileevent $f readable {}
6099 }]
6100 close $f
6101 set x initial
6102 after 100 [namespace code { set y done }]
6103 variable y
6104 vwait [namespace which -variable y]
6105 set x
6106 } {initial}
6107 test io-45.2 {DeleteFileEvent, cleanup on close} {fileevent} {
6108 set f [open $path(foo) r]
6109 set f2 [open $path(foo) r]
6110 fileevent $f readable [namespace code {
6111 lappend x "f triggered: \"[gets $f]\""
6112 fileevent $f readable {}
6113 }]
6114 fileevent $f2 readable [namespace code {
6115 lappend x "f2 triggered: \"[gets $f2]\""
6116 fileevent $f2 readable {}
6117 }]
6118 close $f
6119 variable x initial
6120 vwait [namespace which -variable x]
6121 close $f2
6122 set x
6123 } {initial {f2 triggered: "foo bar"}}
6124 test io-45.3 {DeleteFileEvent, cleanup on close} {fileevent} {
6125 set f [open $path(foo) r]
6126 set f2 [open $path(foo) r]
6127 set f3 [open $path(foo) r]
6128 fileevent $f readable {f script}
6129 fileevent $f2 readable {f2 script}
6130 fileevent $f3 readable {f3 script}
6131 set x {}
6132 close $f2
6133 lappend x [catch {fileevent $f readable} msg] $msg \
6134 [catch {fileevent $f2 readable}] \
6135 [catch {fileevent $f3 readable} msg] $msg
6136 close $f3
6137 lappend x [catch {fileevent $f readable} msg] $msg \
6138 [catch {fileevent $f2 readable}] \
6139 [catch {fileevent $f3 readable}]
6140 close $f
6141 lappend x [catch {fileevent $f readable}] \
6142 [catch {fileevent $f2 readable}] \
6143 [catch {fileevent $f3 readable}]
6144 } {0 {f script} 1 0 {f3 script} 0 {f script} 1 1 1 1 1}
6145
6146 # Execute these tests only if the "testfevent" command is present.
6147
6148 test io-46.1 {Tcl event loop vs multiple interpreters} {testfevent fileevent notOSX} {
6149 testfevent create
6150 set script "set f \[[list open $path(foo) r]]\n"
6151 append script {
6152 set x "no event"
6153 fileevent $f readable [namespace code {
6154 set x "f triggered: [gets $f]"
6155 fileevent $f readable {}
6156 }]
6157 }
6158 set timer [after 10 lappend x timeout]
6159 testfevent cmd $script
6160 vwait x
6161 after cancel $timer
6162 testfevent cmd {close $f}
6163 list [testfevent cmd {set x}] [testfevent cmd {info commands after}]
6164 } {{f triggered: foo bar} after}
6165 test io-46.2 {Tcl event loop vs multiple interpreters} testfevent {
6166 testfevent create
6167 testfevent cmd {
6168 variable x 0
6169 after 100 {set x triggered}
6170 vwait [namespace which -variable x]
6171 set x
6172 }
6173 } {triggered}
6174 test io-46.3 {Tcl event loop vs multiple interpreters} testfevent {
6175 testfevent create
6176 testfevent cmd {
6177 set x 0
6178 after 10 {lappend x timer}
6179 after 30
6180 set result $x
6181 update idletasks
6182 lappend result $x
6183 update
6184 lappend result $x
6185 }
6186 } {0 0 {0 timer}}
6187
6188 test io-47.1 {fileevent vs multiple interpreters} {testfevent fileevent} {
6189 set f [open $path(foo) r]
6190 set f2 [open $path(foo) r]
6191 set f3 [open $path(foo) r]
6192 fileevent $f readable {script 1}
6193 testfevent create
6194 testfevent share $f2
6195 testfevent cmd "fileevent $f2 readable {script 2}"
6196 fileevent $f3 readable {sript 3}
6197 set x {}
6198 lappend x [fileevent $f2 readable]
6199 testfevent delete
6200 lappend x [fileevent $f readable] [fileevent $f2 readable] \
6201 [fileevent $f3 readable]
6202 close $f
6203 close $f2
6204 close $f3
6205 set x
6206 } {{} {script 1} {} {sript 3}}
6207 test io-47.2 {deleting fileevent on interpreter delete} {testfevent fileevent} {
6208 set f [open $path(foo) r]
6209 set f2 [open $path(foo) r]
6210 set f3 [open $path(foo) r]
6211 set f4 [open $path(foo) r]
6212 fileevent $f readable {script 1}
6213 testfevent create
6214 testfevent share $f2
6215 testfevent share $f3
6216 testfevent cmd "fileevent $f2 readable {script 2}
6217 fileevent $f3 readable {script 3}"
6218 fileevent $f4 readable {script 4}
6219 testfevent delete
6220 set x [list [fileevent $f readable] [fileevent $f2 readable] \
6221 [fileevent $f3 readable] [fileevent $f4 readable]]
6222 close $f
6223 close $f2
6224 close $f3
6225 close $f4
6226 set x
6227 } {{script 1} {} {} {script 4}}
6228 test io-47.3 {deleting fileevent on interpreter delete} {testfevent fileevent} {
6229 set f [open $path(foo) r]
6230 set f2 [open $path(foo) r]
6231 set f3 [open $path(foo) r]
6232 set f4 [open $path(foo) r]
6233 testfevent create
6234 testfevent share $f3
6235 testfevent share $f4
6236 fileevent $f readable {script 1}
6237 fileevent $f2 readable {script 2}
6238 testfevent cmd "fileevent $f3 readable {script 3}
6239 fileevent $f4 readable {script 4}"
6240 testfevent delete
6241 set x [list [fileevent $f readable] [fileevent $f2 readable] \
6242 [fileevent $f3 readable] [fileevent $f4 readable]]
6243 close $f
6244 close $f2
6245 close $f3
6246 close $f4
6247 set x
6248 } {{script 1} {script 2} {} {}}
6249 test io-47.4 {file events on shared files and multiple interpreters} {testfevent fileevent} {
6250 set f [open $path(foo) r]
6251 set f2 [open $path(foo) r]
6252 testfevent create
6253 testfevent share $f
6254 testfevent cmd "fileevent $f readable {script 1}"
6255 fileevent $f readable {script 2}
6256 fileevent $f2 readable {script 3}
6257 set x [list [fileevent $f2 readable] \
6258 [testfevent cmd "fileevent $f readable"] \
6259 [fileevent $f readable]]
6260 testfevent delete
6261 close $f
6262 close $f2
6263 set x
6264 } {{script 3} {script 1} {script 2}}
6265 test io-47.5 {file events on shared files, deleting file events} {testfevent fileevent} {
6266 set f [open $path(foo) r]
6267 testfevent create
6268 testfevent share $f
6269 testfevent cmd "fileevent $f readable {script 1}"
6270 fileevent $f readable {script 2}
6271 testfevent cmd "fileevent $f readable {}"
6272 set x [list [testfevent cmd "fileevent $f readable"] \
6273 [fileevent $f readable]]
6274 testfevent delete
6275 close $f
6276 set x
6277 } {{} {script 2}}
6278 test io-47.6 {file events on shared files, deleting file events} {testfevent fileevent} {
6279 set f [open $path(foo) r]
6280 testfevent create
6281 testfevent share $f
6282 testfevent cmd "fileevent $f readable {script 1}"
6283 fileevent $f readable {script 2}
6284 fileevent $f readable {}
6285 set x [list [testfevent cmd "fileevent $f readable"] \
6286 [fileevent $f readable]]
6287 testfevent delete
6288 close $f
6289 set x
6290 } {{script 1} {}}
6291 unset path(foo)
6292 removeFile foo
6293
6294 set path(bar) [makeFile {} bar]
6295
6296 test io-48.1 {testing readability conditions} {fileevent} {
6297 set f [open $path(bar) w]
6298 puts $f abcdefg
6299 puts $f abcdefg
6300 puts $f abcdefg
6301 puts $f abcdefg
6302 puts $f abcdefg
6303 close $f
6304 set f [open $path(bar) r]
6305 fileevent $f readable [namespace code [list consume $f]]
6306 proc consume {f} {
6307 variable l
6308 variable x
6309 lappend l called
6310 if {[eof $f]} {
6311 close $f
6312 set x done
6313 } else {
6314 gets $f
6315 }
6316 }
6317 set l ""
6318 variable x not_done
6319 vwait [namespace which -variable x]
6320 list $x $l
6321 } {done {called called called called called called called}}
6322 test io-48.2 {testing readability conditions} {nonBlockFiles fileevent} {
6323 set f [open $path(bar) w]
6324 puts $f abcdefg
6325 puts $f abcdefg
6326 puts $f abcdefg
6327 puts $f abcdefg
6328 puts $f abcdefg
6329 close $f
6330 set f [open $path(bar) r]
6331 fileevent $f readable [namespace code [list consume $f]]
6332 fconfigure $f -blocking off
6333 proc consume {f} {
6334 variable x
6335 variable l
6336 lappend l called
6337 if {[eof $f]} {
6338 close $f
6339 set x done
6340 } else {
6341 gets $f
6342 }
6343 }
6344 set l ""
6345 variable x not_done
6346 vwait [namespace which -variable x]
6347 list $x $l
6348 } {done {called called called called called called called}}
6349 set path(my_script) [makeFile {} my_script]
6350 test io-48.3 {testing readability conditions} {stdio unix nonBlockFiles fileevent} {
6351 set f [open $path(bar) w]
6352 puts $f abcdefg
6353 puts $f abcdefg
6354 puts $f abcdefg
6355 puts $f abcdefg
6356 puts $f abcdefg
6357 close $f
6358 set f [open $path(my_script) w]
6359 puts $f {
6360 proc copy_slowly {f} {
6361 while {![eof $f]} {
6362 puts [gets $f]
6363 after 200
6364 }
6365 close $f
6366 }
6367 }
6368 close $f
6369 set f [open "|[list [interpreter]]" r+]
6370 fileevent $f readable [namespace code [list consume $f]]
6371 fconfigure $f -buffering line
6372 fconfigure $f -blocking off
6373 proc consume {f} {
6374 variable l
6375 variable x
6376 if {[eof $f]} {
6377 set x done
6378 } else {
6379 gets $f
6380 lappend l [fblocked $f]
6381 gets $f
6382 lappend l [fblocked $f]
6383 }
6384 }
6385 set l ""
6386 variable x not_done
6387 puts $f [list source $path(my_script)]
6388 puts $f "set f \[[list open $path(bar) r]]"
6389 puts $f {copy_slowly $f}
6390 puts $f {exit}
6391 vwait [namespace which -variable x]
6392 close $f
6393 list $x $l
6394 } {done {0 1 0 1 0 1 0 1 0 1 0 1 0 0}}
6395 unset path(bar)
6396 removeFile bar
6397
6398 test io-48.4 {lf write, testing readability, ^Z termination, auto read mode} {fileevent} {
6399 file delete $path(test1)
6400 set f [open $path(test1) w]
6401 fconfigure $f -translation lf
6402 variable c [format "abc\ndef\n%c" 26]
6403 puts -nonewline $f $c
6404 close $f
6405 proc consume {f} {
6406 variable l
6407 variable c
6408 variable x
6409 if {[eof $f]} {
6410 set x done
6411 close $f
6412 } else {
6413 lappend l [gets $f]
6414 incr c
6415 }
6416 }
6417 set c 0
6418 set l ""
6419 set f [open $path(test1) r]
6420 fconfigure $f -translation auto -eofchar \x1A
6421 fileevent $f readable [namespace code [list consume $f]]
6422 variable x
6423 vwait [namespace which -variable x]
6424 list $c $l
6425 } {3 {abc def {}}}
6426 test io-48.5 {lf write, testing readability, ^Z in middle, auto read mode} {fileevent} {
6427 file delete $path(test1)
6428 set f [open $path(test1) w]
6429 fconfigure $f -translation lf
6430 set c [format "abc\ndef\n%cfoo\nbar\n" 26]
6431 puts -nonewline $f $c
6432 close $f
6433 proc consume {f} {
6434 variable l
6435 variable x
6436 variable c
6437 if {[eof $f]} {
6438 set x done
6439 close $f
6440 } else {
6441 lappend l [gets $f]
6442 incr c
6443 }
6444 }
6445 set c 0
6446 set l ""
6447 set f [open $path(test1) r]
6448 fconfigure $f -translation auto -eofchar \x1A
6449 fileevent $f readable [namespace code [list consume $f]]
6450 variable x
6451 vwait [namespace which -variable x]
6452 list $c $l
6453 } {3 {abc def {}}}
6454 test io-48.6 {cr write, testing readability, ^Z termination, auto read mode} {fileevent} {
6455 file delete $path(test1)
6456 set f [open $path(test1) w]
6457 fconfigure $f -translation cr
6458 set c [format "abc\ndef\n%c" 26]
6459 puts -nonewline $f $c
6460 close $f
6461 proc consume {f} {
6462 variable l
6463 variable x
6464 variable c
6465 if {[eof $f]} {
6466 set x done
6467 close $f
6468 } else {
6469 lappend l [gets $f]
6470 incr c
6471 }
6472 }
6473 set c 0
6474 set l ""
6475 set f [open $path(test1) r]
6476 fconfigure $f -translation auto -eofchar \x1A
6477 fileevent $f readable [namespace code [list consume $f]]
6478 variable x
6479 vwait [namespace which -variable x]
6480 list $c $l
6481 } {3 {abc def {}}}
6482 test io-48.7 {cr write, testing readability, ^Z in middle, auto read mode} {fileevent} {
6483 file delete $path(test1)
6484 set f [open $path(test1) w]
6485 fconfigure $f -translation cr
6486 set c [format "abc\ndef\n%cfoo\nbar\n" 26]
6487 puts -nonewline $f $c
6488 close $f
6489 proc consume {f} {
6490 variable l
6491 variable c
6492 variable x
6493 if {[eof $f]} {
6494 set x done
6495 close $f
6496 } else {
6497 lappend l [gets $f]
6498 incr c
6499 }
6500 }
6501 set c 0
6502 set l ""
6503 set f [open $path(test1) r]
6504 fconfigure $f -translation auto -eofchar \x1A
6505 fileevent $f readable [namespace code [list consume $f]]
6506 variable x
6507 vwait [namespace which -variable x]
6508 list $c $l
6509 } {3 {abc def {}}}
6510 test io-48.8 {crlf write, testing readability, ^Z termination, auto read mode} {fileevent} {
6511 file delete $path(test1)
6512 set f [open $path(test1) w]
6513 fconfigure $f -translation crlf
6514 set c [format "abc\ndef\n%c" 26]
6515 puts -nonewline $f $c
6516 close $f
6517 proc consume {f} {
6518 variable l
6519 variable x
6520 variable c
6521 if {[eof $f]} {
6522 set x done
6523 close $f
6524 } else {
6525 lappend l [gets $f]
6526 incr c
6527 }
6528 }
6529 set c 0
6530 set l ""
6531 set f [open $path(test1) r]
6532 fconfigure $f -translation auto -eofchar \x1A
6533 fileevent $f readable [namespace code [list consume $f]]
6534 variable x
6535 vwait [namespace which -variable x]
6536 list $c $l
6537 } {3 {abc def {}}}
6538 test io-48.9 {crlf write, testing readability, ^Z in middle, auto read mode} {fileevent} {
6539 file delete $path(test1)
6540 set f [open $path(test1) w]
6541 fconfigure $f -translation crlf
6542 set c [format "abc\ndef\n%cfoo\nbar\n" 26]
6543 puts -nonewline $f $c
6544 close $f
6545 proc consume {f} {
6546 variable l
6547 variable c
6548 variable x
6549 if {[eof $f]} {
6550 set x done
6551 close $f
6552 } else {
6553 lappend l [gets $f]
6554 incr c
6555 }
6556 }
6557 set c 0
6558 set l ""
6559 set f [open $path(test1) r]
6560 fconfigure $f -translation auto -eofchar \x1A
6561 fileevent $f readable [namespace code [list consume $f]]
6562 variable x
6563 vwait [namespace which -variable x]
6564 list $c $l
6565 } {3 {abc def {}}}
6566 test io-48.10 {lf write, testing readability, ^Z in middle, lf read mode} {fileevent} {
6567 file delete $path(test1)
6568 set f [open $path(test1) w]
6569 fconfigure $f -translation lf
6570 set c [format "abc\ndef\n%cfoo\nbar\n" 26]
6571 puts -nonewline $f $c
6572 close $f
6573 proc consume {f} {
6574 variable l
6575 variable c
6576 variable x
6577 if {[eof $f]} {
6578 set x done
6579 close $f
6580 } else {
6581 lappend l [gets $f]
6582 incr c
6583 }
6584 }
6585 set c 0
6586 set l ""
6587 set f [open $path(test1) r]
6588 fconfigure $f -translation lf -eofchar \x1A
6589 fileevent $f readable [namespace code [list consume $f]]
6590 variable x
6591 vwait [namespace which -variable x]
6592 list $c $l
6593 } {3 {abc def {}}}
6594 test io-48.11 {lf write, testing readability, ^Z termination, lf read mode} {fileevent} {
6595 file delete $path(test1)
6596 set f [open $path(test1) w]
6597 fconfigure $f -translation lf
6598 set c [format "abc\ndef\n%c" 26]
6599 puts -nonewline $f $c
6600 close $f
6601 proc consume {f} {
6602 variable l
6603 variable x
6604 variable c
6605 if {[eof $f]} {
6606 set x done
6607 close $f
6608 } else {
6609 lappend l [gets $f]
6610 incr c
6611 }
6612 }
6613 set c 0
6614 set l ""
6615 set f [open $path(test1) r]
6616 fconfigure $f -translation lf -eofchar \x1A
6617 fileevent $f readable [namespace code [list consume $f]]
6618 variable x
6619 vwait [namespace which -variable x]
6620 list $c $l
6621 } {3 {abc def {}}}
6622 test io-48.12 {cr write, testing readability, ^Z in middle, cr read mode} {fileevent} {
6623 file delete $path(test1)
6624 set f [open $path(test1) w]
6625 fconfigure $f -translation cr
6626 set c [format "abc\ndef\n%cfoo\nbar\n" 26]
6627 puts -nonewline $f $c
6628 close $f
6629 proc consume {f} {
6630 variable l
6631 variable x
6632 variable c
6633 if {[eof $f]} {
6634 set x done
6635 close $f
6636 } else {
6637 lappend l [gets $f]
6638 incr c
6639 }
6640 }
6641 set c 0
6642 set l ""
6643 set f [open $path(test1) r]
6644 fconfigure $f -translation cr -eofchar \x1A
6645 fileevent $f readable [namespace code [list consume $f]]
6646 variable x
6647 vwait [namespace which -variable x]
6648 list $c $l
6649 } {3 {abc def {}}}
6650 test io-48.13 {cr write, testing readability, ^Z termination, cr read mode} {fileevent} {
6651 file delete $path(test1)
6652 set f [open $path(test1) w]
6653 fconfigure $f -translation cr
6654 set c [format "abc\ndef\n%c" 26]
6655 puts -nonewline $f $c
6656 close $f
6657 proc consume {f} {
6658 variable c
6659 variable x
6660 variable l
6661 if {[eof $f]} {
6662 set x done
6663 close $f
6664 } else {
6665 lappend l [gets $f]
6666 incr c
6667 }
6668 }
6669 set c 0
6670 set l ""
6671 set f [open $path(test1) r]
6672 fconfigure $f -translation cr -eofchar \x1A
6673 fileevent $f readable [namespace code [list consume $f]]
6674 variable x
6675 vwait [namespace which -variable x]
6676 list $c $l
6677 } {3 {abc def {}}}
6678 test io-48.14 {crlf write, testing readability, ^Z in middle, crlf read mode} {fileevent} {
6679 file delete $path(test1)
6680 set f [open $path(test1) w]
6681 fconfigure $f -translation crlf
6682 set c [format "abc\ndef\n%cfoo\nbar\n" 26]
6683 puts -nonewline $f $c
6684 close $f
6685 proc consume {f} {
6686 variable c
6687 variable x
6688 variable l
6689 if {[eof $f]} {
6690 set x done
6691 close $f
6692 } else {
6693 lappend l [gets $f]
6694 incr c
6695 }
6696 }
6697 set c 0
6698 set l ""
6699 set f [open $path(test1) r]
6700 fconfigure $f -translation crlf -eofchar \x1A
6701 fileevent $f readable [namespace code [list consume $f]]
6702 variable x
6703 vwait [namespace which -variable x]
6704 list $c $l
6705 } {3 {abc def {}}}
6706 test io-48.15 {crlf write, testing readability, ^Z termi, crlf read mode} {fileevent} {
6707 file delete $path(test1)
6708 set f [open $path(test1) w]
6709 fconfigure $f -translation crlf
6710 set c [format "abc\ndef\n%c" 26]
6711 puts -nonewline $f $c
6712 close $f
6713 proc consume {f} {
6714 variable c
6715 variable x
6716 variable l
6717 if {[eof $f]} {
6718 set x done
6719 close $f
6720 } else {
6721 lappend l [gets $f]
6722 incr c
6723 }
6724 }
6725 set c 0
6726 set l ""
6727 set f [open $path(test1) r]
6728 fconfigure $f -translation crlf -eofchar \x1A
6729 fileevent $f readable [namespace code [list consume $f]]
6730 variable x
6731 vwait [namespace which -variable x]
6732 list $c $l
6733 } {3 {abc def {}}}
6734
6735 test io-49.1 {testing crlf reading, leftover cr disgorgment} {
6736 file delete $path(test1)
6737 set f [open $path(test1) w]
6738 fconfigure $f -translation lf
6739 puts -nonewline $f "a\rb\rc\r\n"
6740 close $f
6741 set f [open $path(test1) r]
6742 set l ""
6743 lappend l [file size $path(test1)]
6744 fconfigure $f -translation crlf
6745 lappend l [read $f 1]
6746 lappend l [tell $f]
6747 lappend l [read $f 1]
6748 lappend l [tell $f]
6749 lappend l [read $f 1]
6750 lappend l [tell $f]
6751 lappend l [read $f 1]
6752 lappend l [tell $f]
6753 lappend l [read $f 1]
6754 lappend l [tell $f]
6755 lappend l [read $f 1]
6756 lappend l [tell $f]
6757 lappend l [eof $f]
6758 lappend l [read $f 1]
6759 lappend l [eof $f]
6760 close $f
6761 set l
6762 } "7 a 1 [list \r] 2 b 3 [list \r] 4 c 5 {
6763 } 7 0 {} 1"
6764 test io-49.2 {testing crlf reading, leftover cr disgorgment} {
6765 file delete $path(test1)
6766 set f [open $path(test1) w]
6767 fconfigure $f -translation lf
6768 puts -nonewline $f "a\rb\rc\r\n"
6769 close $f
6770 set f [open $path(test1) r]
6771 set l ""
6772 lappend l [file size $path(test1)]
6773 fconfigure $f -translation crlf
6774 lappend l [read $f 2]
6775 lappend l [tell $f]
6776 lappend l [read $f 2]
6777 lappend l [tell $f]
6778 lappend l [read $f 2]
6779 lappend l [tell $f]
6780 lappend l [eof $f]
6781 lappend l [read $f 2]
6782 lappend l [tell $f]
6783 lappend l [eof $f]
6784 close $f
6785 set l
6786 } "7 [list a\r] 2 [list b\r] 4 [list c\n] 7 0 {} 7 1"
6787 test io-49.3 {testing crlf reading, leftover cr disgorgment} {
6788 file delete $path(test1)
6789 set f [open $path(test1) w]
6790 fconfigure $f -translation lf
6791 puts -nonewline $f "a\rb\rc\r\n"
6792 close $f
6793 set f [open $path(test1) r]
6794 set l ""
6795 lappend l [file size $path(test1)]
6796 fconfigure $f -translation crlf
6797 lappend l [read $f 3]
6798 lappend l [tell $f]
6799 lappend l [read $f 3]
6800 lappend l [tell $f]
6801 lappend l [eof $f]
6802 lappend l [read $f 3]
6803 lappend l [tell $f]
6804 lappend l [eof $f]
6805 close $f
6806 set l
6807 } "7 [list a\rb] 3 [list \rc\n] 7 0 {} 7 1"
6808 test io-49.4 {testing crlf reading, leftover cr disgorgment} {
6809 file delete $path(test1)
6810 set f [open $path(test1) w]
6811 fconfigure $f -translation lf
6812 puts -nonewline $f "a\rb\rc\r\n"
6813 close $f
6814 set f [open $path(test1) r]
6815 set l ""
6816 lappend l [file size $path(test1)]
6817 fconfigure $f -translation crlf
6818 lappend l [read $f 3]
6819 lappend l [tell $f]
6820 lappend l [gets $f]
6821 lappend l [tell $f]
6822 lappend l [eof $f]
6823 lappend l [gets $f]
6824 lappend l [tell $f]
6825 lappend l [eof $f]
6826 close $f
6827 set l
6828 } "7 [list a\rb] 3 [list \rc] 7 0 {} 7 1"
6829 test io-49.5 {testing crlf reading, leftover cr disgorgment} {
6830 file delete $path(test1)
6831 set f [open $path(test1) w]
6832 fconfigure $f -translation lf
6833 puts -nonewline $f "a\rb\rc\r\n"
6834 close $f
6835 set f [open $path(test1) r]
6836 set l ""
6837 lappend l [file size $path(test1)]
6838 fconfigure $f -translation crlf
6839 lappend l [set x [gets $f]]
6840 lappend l [tell $f]
6841 lappend l [gets $f]
6842 lappend l [tell $f]
6843 lappend l [eof $f]
6844 close $f
6845 set l
6846 } [list 7 a\rb\rc 7 {} 7 1]
6847
6848 test io-50.1 {testing handler deletion} -constraints {testchannelevent testservicemode} -setup {
6849 file delete $path(test1)
6850 } -body {
6851 set f [open $path(test1) w]
6852 close $f
6853 update
6854 proc delhandler {f} {
6855 variable z
6856 set z called
6857 testchannelevent $f delete 0
6858 }
6859 set z not_called
6860 set timer [after 50 lappend z timeout]
6861 testservicemode 0
6862 set f [open $path(test1) r]
6863 testchannelevent $f add readable [namespace code [list delhandler $f]]
6864 testservicemode 1
6865 vwait z
6866 after cancel $timer
6867 set z
6868 } -cleanup {
6869 close $f
6870 } -result called
6871 test io-50.2 {testing handler deletion with multiple handlers} -constraints {testchannelevent testservicemode} -setup {
6872 file delete $path(test1)
6873 } -body {
6874 set f [open $path(test1) w]
6875 close $f
6876 proc delhandler {f i} {
6877 variable z
6878 lappend z "called delhandler $i"
6879 testchannelevent $f delete 0
6880 }
6881 set z ""
6882 testservicemode 0
6883 set f [open $path(test1) r]
6884 testchannelevent $f add readable [namespace code [list delhandler $f 1]]
6885 testchannelevent $f add readable [namespace code [list delhandler $f 0]]
6886 testservicemode 1
6887 set timer [after 50 lappend z timeout]
6888 vwait z
6889 after cancel $timer
6890 set z
6891 } -cleanup {
6892 close $f
6893 } -result {{called delhandler 0} {called delhandler 1}}
6894 test io-50.3 {testing handler deletion with multiple handlers} -constraints {testchannelevent testservicemode} -setup {
6895 file delete $path(test1)
6896 } -body {
6897 set f [open $path(test1) w]
6898 close $f
6899 set z ""
6900 proc notcalled {f i} {
6901 variable z
6902 lappend z "notcalled was called!! $f $i"
6903 }
6904 proc delhandler {f i} {
6905 variable z
6906 testchannelevent $f delete 1
6907 lappend z "delhandler $i called"
6908 testchannelevent $f delete 0
6909 lappend z "delhandler $i deleted myself"
6910 }
6911 set z ""
6912 testservicemode 0
6913 set f [open $path(test1) r]
6914 testchannelevent $f add readable [namespace code [list notcalled $f 1]]
6915 testchannelevent $f add readable [namespace code [list delhandler $f 0]]
6916 testservicemode 1
6917 set timer [after 50 lappend z timeout]
6918 vwait z
6919 after cancel $timer
6920 set z
6921 } -cleanup {
6922 close $f
6923 } -result {{delhandler 0 called} {delhandler 0 deleted myself}}
6924 test io-50.4 {testing handler deletion vs reentrant calls} -constraints {testchannelevent testservicemode} -setup {
6925 file delete $path(test1)
6926 update
6927 } -body {
6928 set f [open $path(test1) w]
6929 close $f
6930 update
6931 proc delrecursive {f} {
6932 variable z
6933 variable u
6934 if {"$u" == "recursive"} {
6935 testchannelevent $f delete 0
6936 lappend z "delrecursive deleting recursive"
6937 } else {
6938 lappend z "delrecursive calling recursive"
6939 set u recursive
6940 update
6941 }
6942 }
6943 variable u toplevel
6944 variable z ""
6945 testservicemode 0
6946 set f [open $path(test1) r]
6947 testchannelevent $f add readable [namespace code [list delrecursive $f]]
6948 testservicemode 1
6949 set timer [after 50 lappend z timeout]
6950 vwait z
6951 after cancel $timer
6952 set z
6953 } -cleanup {
6954 close $f
6955 } -result {{delrecursive calling recursive} {delrecursive deleting recursive}}
6956 test io-50.5 {testing handler deletion vs reentrant calls} -constraints {testchannelevent testservicemode notOSX} -setup {
6957 file delete $path(test1)
6958 } -body {
6959 set f [open $path(test1) w]
6960 close $f
6961 proc notcalled {f} {
6962 variable z
6963 lappend z "notcalled was called!! $f"
6964 }
6965 proc del {f} {
6966 variable u
6967 variable z
6968 if {"$u" == "recursive"} {
6969 testchannelevent $f delete 1
6970 lappend z "del deleted notcalled"
6971 testchannelevent $f delete 0
6972 lappend z "del deleted myself"
6973 } else {
6974 set u recursive
6975 lappend z "del calling recursive"
6976 set timer [after 50 lappend z timeout]
6977 vwait z
6978 after cancel $timer
6979 lappend z "del after recursive"
6980 }
6981 }
6982 set z ""
6983 set u toplevel
6984 testservicemode 0
6985 set f [open $path(test1) r]
6986 testchannelevent $f add readable [namespace code [list notcalled $f]]
6987 testchannelevent $f add readable [namespace code [list del $f]]
6988 testservicemode 1
6989 set timer [after 50 set z timeout]
6990 vwait z
6991 after cancel $timer
6992 set z
6993 } -cleanup {
6994 close $f
6995 } -result [list {del calling recursive} {del deleted notcalled} \
6996 {del deleted myself} {del after recursive}]
6997 test io-50.6 {testing handler deletion vs reentrant calls} -constraints {testchannelevent testservicemode} -setup {
6998 file delete $path(test1)
6999 } -body {
7000 set f [open $path(test1) w]
7001 close $f
7002 proc first {f} {
7003 variable u
7004 variable z
7005 variable done
7006 if {"$u" == "toplevel"} {
7007 lappend z "first called"
7008 set u first
7009 set timer [after 50 lappend z timeout]
7010 vwait z
7011 after cancel $timer
7012 lappend z "first after toplevel"
7013 set done 1
7014 } else {
7015 lappend z "first called not toplevel"
7016 }
7017 }
7018 proc second {f} {
7019 variable u
7020 variable z
7021 if {"$u" == "first"} {
7022 lappend z "second called, first time"
7023 set u second
7024 testchannelevent $f delete 0
7025 } elseif {"$u" == "second"} {
7026 lappend z "second called, second time"
7027 testchannelevent $f delete 0
7028 } else {
7029 lappend z "second called, cannot happen!"
7030 testchannelevent $f removeall
7031 }
7032 }
7033 set z ""
7034 set u toplevel
7035 set done 0
7036 testservicemode 0
7037 set f [open $path(test1) r]
7038 testchannelevent $f add readable [namespace code [list second $f]]
7039 testchannelevent $f add readable [namespace code [list first $f]]
7040 testservicemode 1
7041 update
7042 if {!$done} {
7043 set timer2 [after 200 set done 1]
7044 vwait done
7045 after cancel $timer2
7046 }
7047 set z
7048 } -cleanup {
7049 close $f
7050 } -result [list {first called} {first called not toplevel} \
7051 {second called, first time} {second called, second time} \
7052 {first after toplevel}]
7053 test io-51.1 {Test old socket deletion on Macintosh} {socket} {
7054 set x 0
7055 set result ""
7056 proc accept {s a p} {
7057 variable x
7058 variable wait
7059 fconfigure $s -blocking off
7060 puts $s "sock[incr x]"
7061 close $s
7062 set wait done
7063 }
7064 set ss [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
7065 set port [lindex [fconfigure $ss -sockname] 2]
7066
7067 variable wait ""
7068 set cs [socket 127.0.0.1 $port]
7069 vwait [namespace which -variable wait]
7070 lappend result [gets $cs]
7071 close $cs
7072
7073 set wait ""
7074 set cs [socket 127.0.0.1 $port]
7075 vwait [namespace which -variable wait]
7076 lappend result [gets $cs]
7077 close $cs
7078
7079 set wait ""
7080 set cs [socket 127.0.0.1 $port]
7081 vwait [namespace which -variable wait]
7082 lappend result [gets $cs]
7083 close $cs
7084
7085 set wait ""
7086 set cs [socket 127.0.0.1 $port]
7087 vwait [namespace which -variable wait]
7088 lappend result [gets $cs]
7089 close $cs
7090 close $ss
7091 set result
7092 } {sock1 sock2 sock3 sock4}
7093
7094 test io-52.1 {TclCopyChannel} {fcopy} {
7095 file delete $path(test1)
7096 set f1 [open $thisScript]
7097 set f2 [open $path(test1) w]
7098 fcopy $f1 $f2 -command { # }
7099 catch { fcopy $f1 $f2 } msg
7100 close $f1
7101 close $f2
7102 string compare $msg "channel \"$f1\" is busy"
7103 } {0}
7104 test io-52.2 {TclCopyChannel} {fcopy} {
7105 file delete $path(test1)
7106 set f1 [open $thisScript]
7107 set f2 [open $path(test1) w]
7108 set f3 [open $thisScript]
7109 fcopy $f1 $f2 -command { # }
7110 catch { fcopy $f3 $f2 } msg
7111 close $f1
7112 close $f2
7113 close $f3
7114 string compare $msg "channel \"$f2\" is busy"
7115 } {0}
7116 test io-52.3 {TclCopyChannel} {fcopy} {
7117 file delete $path(test1)
7118 set f1 [open $thisScript]
7119 set f2 [open $path(test1) w]
7120 fconfigure $f1 -translation lf -blocking 0
7121 fconfigure $f2 -translation cr -blocking 0
7122 set s0 [fcopy $f1 $f2]
7123 set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7124 close $f1
7125 close $f2
7126 set s1 [file size $thisScript]
7127 set s2 [file size $path(test1)]
7128 if {("$s1" == "$s2") && ($s0 == $s1)} {
7129 lappend result ok
7130 }
7131 set result
7132 } {0 0 ok}
7133 test io-52.4 {TclCopyChannel} {fcopy} {
7134 file delete $path(test1)
7135 set f1 [open $thisScript]
7136 set f2 [open $path(test1) w]
7137 fconfigure $f1 -translation lf -blocking 0
7138 fconfigure $f2 -translation cr -blocking 0
7139 fcopy $f1 $f2 -size 40
7140 set result [list [fblocked $f1] [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7141 close $f1
7142 close $f2
7143 lappend result [file size $path(test1)]
7144 } {0 0 0 40}
7145 test io-52.4.1 {TclCopyChannel} {fcopy} {
7146 file delete $path(test1)
7147 set f1 [open $thisScript]
7148 set f2 [open $path(test1) w]
7149 fconfigure $f1 -translation lf -blocking 0 -buffersize 10000000
7150 fconfigure $f2 -translation cr -blocking 0
7151 fcopy $f1 $f2 -size 40
7152 set result [list [fblocked $f1] [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7153 close $f1
7154 close $f2
7155 lappend result [file size $path(test1)]
7156 } {0 0 0 40}
7157 test io-52.5 {TclCopyChannel, all} {fcopy} {
7158 file delete $path(test1)
7159 set f1 [open $thisScript]
7160 set f2 [open $path(test1) w]
7161 fconfigure $f1 -translation lf -blocking 0
7162 fconfigure $f2 -translation lf -blocking 0
7163 fcopy $f1 $f2 -size -1 ;# -1 means 'copy all', same as if no -size specified.
7164 set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7165 close $f1
7166 close $f2
7167 set s1 [file size $thisScript]
7168 set s2 [file size $path(test1)]
7169 if {"$s1" == "$s2"} {
7170 lappend result ok
7171 }
7172 set result
7173 } {0 0 ok}
7174 test io-52.5a {TclCopyChannel, all, other negative value} {fcopy} {
7175 file delete $path(test1)
7176 set f1 [open $thisScript]
7177 set f2 [open $path(test1) w]
7178 fconfigure $f1 -translation lf -blocking 0
7179 fconfigure $f2 -translation lf -blocking 0
7180 fcopy $f1 $f2 -size -2 ;# < 0 behaves like -1, copy all
7181 set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7182 close $f1
7183 close $f2
7184 set s1 [file size $thisScript]
7185 set s2 [file size $path(test1)]
7186 if {"$s1" == "$s2"} {
7187 lappend result ok
7188 }
7189 set result
7190 } {0 0 ok}
7191 test io-52.5b {TclCopyChannel, all, wrap to negative value} {fcopy} {
7192 file delete $path(test1)
7193 set f1 [open $thisScript]
7194 set f2 [open $path(test1) w]
7195 fconfigure $f1 -translation lf -blocking 0
7196 fconfigure $f2 -translation lf -blocking 0
7197 fcopy $f1 $f2 -size 3221176172 ;# Wrapped to < 0, behaves like -1, copy all
7198 set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7199 close $f1
7200 close $f2
7201 set s1 [file size $thisScript]
7202 set s2 [file size $path(test1)]
7203 if {"$s1" == "$s2"} {
7204 lappend result ok
7205 }
7206 set result
7207 } {0 0 ok}
7208 test io-52.6 {TclCopyChannel} {fcopy} {
7209 file delete $path(test1)
7210 set f1 [open $thisScript]
7211 set f2 [open $path(test1) w]
7212 fconfigure $f1 -translation lf -blocking 0
7213 fconfigure $f2 -translation lf -blocking 0
7214 set s0 [fcopy $f1 $f2 -size [expr {[file size $thisScript] + 5}]]
7215 set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7216 close $f1
7217 close $f2
7218 set s1 [file size $thisScript]
7219 set s2 [file size $path(test1)]
7220 if {("$s1" == "$s2") && ($s0 == $s1)} {
7221 lappend result ok
7222 }
7223 set result
7224 } {0 0 ok}
7225 test io-52.7 {TclCopyChannel} {fcopy} {
7226 file delete $path(test1)
7227 set f1 [open $thisScript]
7228 set f2 [open $path(test1) w]
7229 fconfigure $f1 -translation lf -blocking 0
7230 fconfigure $f2 -translation lf -blocking 0
7231 fcopy $f1 $f2
7232 set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7233 set s1 [file size $thisScript]
7234 set s2 [file size $path(test1)]
7235 close $f1
7236 close $f2
7237 if {"$s1" == "$s2"} {
7238 lappend result ok
7239 }
7240 set result
7241 } {0 0 ok}
7242 test io-52.8 {TclCopyChannel} {stdio fcopy} {
7243 file delete $path(test1)
7244 file delete $path(pipe)
7245 set f1 [open $path(pipe) w]
7246 fconfigure $f1 -translation lf
7247 puts $f1 "
7248 puts ready
7249 gets stdin
7250 set f1 \[open [list $thisScript] r\]
7251 fconfigure \$f1 -translation lf
7252 puts \[read \$f1 100\]
7253 close \$f1
7254 "
7255 close $f1
7256 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
7257 fconfigure $f1 -translation lf
7258 gets $f1
7259 puts $f1 ready
7260 flush $f1
7261 set f2 [open $path(test1) w]
7262 fconfigure $f2 -translation lf
7263 set s0 [fcopy $f1 $f2 -size 40]
7264 catch {close $f1}
7265 close $f2
7266 list $s0 [file size $path(test1)]
7267 } {40 40}
7268 # Empty files, to register them with the test facility
7269 set path(kyrillic.txt) [makeFile {} kyrillic.txt]
7270 set path(utf8-fcopy.txt) [makeFile {} utf8-fcopy.txt]
7271 set path(utf8-rp.txt) [makeFile {} utf8-rp.txt]
7272 # Create kyrillic file, use lf translation to avoid os eol issues
7273 set out [open $path(kyrillic.txt) w]
7274 fconfigure $out -encoding koi8-r -translation lf
7275 puts $out "\u0410\u0410"
7276 close $out
7277 test io-52.9 {TclCopyChannel & encodings} {fcopy} {
7278 # Copy kyrillic to UTF-8, using fcopy.
7279
7280 set in [open $path(kyrillic.txt) r]
7281 set out [open $path(utf8-fcopy.txt) w]
7282
7283 fconfigure $in -encoding koi8-r -translation lf
7284 fconfigure $out -encoding utf-8 -translation lf
7285
7286 fcopy $in $out
7287 close $in
7288 close $out
7289
7290 # Do the same again, but differently (read/puts).
7291
7292 set in [open $path(kyrillic.txt) r]
7293 set out [open $path(utf8-rp.txt) w]
7294
7295 fconfigure $in -encoding koi8-r -translation lf
7296 fconfigure $out -encoding utf-8 -translation lf
7297
7298 puts -nonewline $out [read $in]
7299
7300 close $in
7301 close $out
7302
7303 list [file size $path(kyrillic.txt)] \
7304 [file size $path(utf8-fcopy.txt)] \
7305 [file size $path(utf8-rp.txt)]
7306 } {3 5 5}
7307 test io-52.10 {TclCopyChannel & encodings} {fcopy} {
7308 # encoding to binary (=> implies that the
7309 # internal utf-8 is written)
7310
7311 set in [open $path(kyrillic.txt) r]
7312 set out [open $path(utf8-fcopy.txt) w]
7313
7314 fconfigure $in -encoding koi8-r -translation lf
7315 # -translation binary is also -encoding binary
7316 fconfigure $out -translation binary
7317
7318 fcopy $in $out
7319 close $in
7320 close $out
7321
7322 file size $path(utf8-fcopy.txt)
7323 } 5
7324 test io-52.11 {TclCopyChannel & encodings} -setup {
7325 set out [open $path(utf8-fcopy.txt) w]
7326 fconfigure $out -encoding utf-8 -translation lf
7327 puts $out "\u0410\u0410"
7328 close $out
7329 } -constraints {fcopy} -body {
7330 # binary to encoding => the input has to be
7331 # in utf-8 to make sense to the encoder
7332
7333 set in [open $path(utf8-fcopy.txt) r]
7334 set out [open $path(kyrillic.txt) w]
7335
7336 # -translation binary is also -encoding binary
7337 fconfigure $in -translation binary
7338 fconfigure $out -encoding koi8-r -translation lf
7339
7340 fcopy $in $out
7341 close $in
7342 close $out
7343
7344 file size $path(kyrillic.txt)
7345 } -result 3
7346
7347 test io-52.12 {coverage of -translation auto} {
7348 file delete $path(test1) $path(test2)
7349 set out [open $path(test1) wb]
7350 chan configure $out -translation lf
7351 puts -nonewline $out abcdefg\rhijklmn\nopqrstu\r\nvwxyz
7352 close $out
7353 set in [open $path(test1)]
7354 chan configure $in -buffersize 8
7355 set out [open $path(test2) w]
7356 chan configure $out -translation lf
7357 fcopy $in $out
7358 close $in
7359 close $out
7360 file size $path(test2)
7361 } 29
7362 test io-52.13 {coverage of -translation cr} {
7363 file delete $path(test1) $path(test2)
7364 set out [open $path(test1) wb]
7365 chan configure $out -translation lf
7366 puts -nonewline $out abcdefg\rhijklmn\nopqrstu\r\nvwxyz
7367 close $out
7368 set in [open $path(test1)]
7369 chan configure $in -buffersize 8 -translation cr
7370 set out [open $path(test2) w]
7371 chan configure $out -translation lf
7372 fcopy $in $out
7373 close $in
7374 close $out
7375 file size $path(test2)
7376 } 30
7377 test io-52.14 {coverage of -translation crlf} {
7378 file delete $path(test1) $path(test2)
7379 set out [open $path(test1) wb]
7380 chan configure $out -translation lf
7381 puts -nonewline $out abcdefg\rhijklmn\nopqrstu\r\nvwxyz
7382 close $out
7383 set in [open $path(test1)]
7384 chan configure $in -buffersize 8 -translation crlf
7385 set out [open $path(test2) w]
7386 chan configure $out -translation lf
7387 fcopy $in $out
7388 close $in
7389 close $out
7390 file size $path(test2)
7391 } 29
7392 test io-52.14.1 {coverage of -translation crlf} {
7393 file delete $path(test1) $path(test2)
7394 set out [open $path(test1) wb]
7395 chan configure $out -translation lf
7396 puts -nonewline $out abcdefg\rhijklmn\nopqrstu\r\nvwxyz
7397 close $out
7398 set in [open $path(test1)]
7399 chan configure $in -buffersize 8 -translation crlf
7400 set out [open $path(test2) w]
7401 fcopy $in $out -size 2
7402 close $in
7403 close $out
7404 file size $path(test2)
7405 } 2
7406 test io-52.14.2 {coverage of -translation crlf} {
7407 file delete $path(test1) $path(test2)
7408 set out [open $path(test1) wb]
7409 chan configure $out -translation lf
7410 puts -nonewline $out abcdefg\rhijklmn\nopqrstu\r\nvwxyz
7411 close $out
7412 set in [open $path(test1)]
7413 chan configure $in -translation crlf
7414 set out [open $path(test2) w]
7415 fcopy $in $out -size 9
7416 close $in
7417 close $out
7418 file size $path(test2)
7419 } 9
7420 test io-52.15 {coverage of -translation crlf} {
7421 file delete $path(test1) $path(test2)
7422 set out [open $path(test1) wb]
7423 chan configure $out -translation lf
7424 puts -nonewline $out abcdefg\r
7425 close $out
7426 set in [open $path(test1)]
7427 chan configure $in -buffersize 8 -translation crlf
7428 set out [open $path(test2) w]
7429 fcopy $in $out
7430 close $in
7431 close $out
7432 file size $path(test2)
7433 } 8
7434 test io-52.16 {coverage of eofChar handling} {
7435 file delete $path(test1) $path(test2)
7436 set out [open $path(test1) wb]
7437 chan configure $out -translation lf
7438 puts -nonewline $out abcdefg\rhijklmn\nopqrstu\r\nvwxyz
7439 close $out
7440 set in [open $path(test1)]
7441 chan configure $in -buffersize 8 -translation lf -eofchar a
7442 set out [open $path(test2) w]
7443 fcopy $in $out
7444 close $in
7445 close $out
7446 file size $path(test2)
7447 } 0
7448 test io-52.17 {coverage of eofChar handling} {
7449 file delete $path(test1) $path(test2)
7450 set out [open $path(test1) wb]
7451 chan configure $out -translation lf
7452 puts -nonewline $out abcdefg\rhijklmn\nopqrstu\r\nvwxyz
7453 close $out
7454 set in [open $path(test1)]
7455 chan configure $in -buffersize 8 -translation lf -eofchar d
7456 set out [open $path(test2) w]
7457 fcopy $in $out
7458 close $in
7459 close $out
7460 file size $path(test2)
7461 } 3
7462 test io-52.18 {coverage of eofChar handling} {
7463 file delete $path(test1) $path(test2)
7464 set out [open $path(test1) wb]
7465 chan configure $out -translation lf
7466 puts -nonewline $out abcdefg\rhijklmn\nopqrstu\r\nvwxyz
7467 close $out
7468 set in [open $path(test1)]
7469 chan configure $in -buffersize 8 -translation crlf -eofchar h
7470 set out [open $path(test2) w]
7471 fcopy $in $out
7472 close $in
7473 close $out
7474 file size $path(test2)
7475 } 8
7476 test io-52.19 {coverage of eofChar handling} {
7477 file delete $path(test1) $path(test2)
7478 set out [open $path(test1) wb]
7479 chan configure $out -translation lf
7480 puts -nonewline $out abcdefg\rhijklmn\nopqrstu\r\nvwxyz
7481 close $out
7482 set in [open $path(test1)]
7483 chan configure $in -buffersize 10 -translation crlf -eofchar h
7484 set out [open $path(test2) w]
7485 fcopy $in $out
7486 close $in
7487 close $out
7488 file size $path(test2)
7489 } 8
7490
7491 test io-53.1 {CopyData} {fcopy} {
7492 file delete $path(test1)
7493 set f1 [open $thisScript]
7494 set f2 [open $path(test1) w]
7495 fconfigure $f1 -translation lf -blocking 0
7496 fconfigure $f2 -translation cr -blocking 0
7497 fcopy $f1 $f2 -size 0
7498 set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7499 close $f1
7500 close $f2
7501 lappend result [file size $path(test1)]
7502 } {0 0 0}
7503 test io-53.2 {CopyData} {fcopy} {
7504 file delete $path(test1)
7505 set f1 [open $thisScript]
7506 set f2 [open $path(test1) w]
7507 fconfigure $f1 -translation lf -blocking 0
7508 fconfigure $f2 -translation cr -blocking 0
7509 fcopy $f1 $f2 -command [namespace code {set s0}]
7510 set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
7511 variable s0
7512 vwait [namespace which -variable s0]
7513 close $f1
7514 close $f2
7515 set s1 [file size $thisScript]
7516 set s2 [file size $path(test1)]
7517 if {("$s1" == "$s2") && ($s0 == $s1)} {
7518 lappend result ok
7519 }
7520 set result
7521 } {0 0 ok}
7522 test io-53.3 {CopyData: background read underflow} {stdio unix fcopy} {
7523 file delete $path(test1)
7524 file delete $path(pipe)
7525 set f1 [open $path(pipe) w]
7526 puts -nonewline $f1 {
7527 puts ready
7528 flush stdout ;# Don't assume line buffered!
7529 fcopy stdin stdout -command { set x }
7530 vwait x
7531 set f [}
7532 puts $f1 [list open $path(test1) w]]
7533 puts $f1 {
7534 fconfigure $f -translation lf
7535 puts $f "done"
7536 close $f
7537 }
7538 close $f1
7539 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
7540 set result [gets $f1]
7541 puts $f1 line1
7542 flush $f1
7543 lappend result [gets $f1]
7544 puts $f1 line2
7545 flush $f1
7546 lappend result [gets $f1]
7547 close $f1
7548 after 500
7549 set f [open $path(test1)]
7550 lappend result [read $f]
7551 close $f
7552 set result
7553 } "ready line1 line2 {done\n}"
7554 test io-53.4 {CopyData: background write overflow} {stdio fileevent fcopy} {
7555 set big bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n
7556 variable x
7557 for {set x 0} {$x < 12} {incr x} {
7558 append big $big
7559 }
7560 file delete $path(pipe)
7561 set f1 [open $path(pipe) w]
7562 puts $f1 {
7563 puts ready
7564 fcopy stdin stdout -command { set x }
7565 vwait x
7566 }
7567 close $f1
7568 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
7569 set result [gets $f1]
7570 fconfigure $f1 -blocking 0
7571 puts $f1 $big
7572 flush $f1
7573 set result ""
7574 fileevent $f1 read [namespace code {
7575 append result [read $f1 1024]
7576 if {[string length $result] >= [string length $big]+1} {
7577 set x done
7578 }
7579 }]
7580 vwait [namespace which -variable x]
7581 close $f1
7582 set big {}
7583 set x
7584 } done
7585 test io-53.4.1 {Bug 894da183c8} {stdio fcopy} {
7586 set big bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n
7587 variable x
7588 for {set x 0} {$x < 12} {incr x} {
7589 append big $big
7590 }
7591 file delete $path(pipe)
7592 set f1 [open $path(pipe) w]
7593 puts $f1 [list file delete $path(test1)]
7594 puts $f1 {
7595 puts ready
7596 set f [open io-53.4.1 w]
7597 chan configure $f -translation lf
7598 fcopy stdin $f -command { set x }
7599 vwait x
7600 close $f
7601 }
7602 puts $f1 "close \[[list open $path(test1) w]]"
7603 close $f1
7604 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
7605 set result [gets $f1]
7606 fconfigure $f1 -blocking 0 -buffersize 125000 -translation lf
7607 puts $f1 $big
7608 fconfigure $f1 -blocking 1
7609 close $f1
7610 set big {}
7611 while {[catch {glob $path(test1)}]} {after 50}
7612 file delete $path(test1)
7613 set check [file size io-53.4.1]
7614 file delete io-53.4.1
7615 set check
7616 } 266241
7617 set result {}
7618 proc FcopyTestAccept {sock args} {
7619 after 1000 "close $sock"
7620 }
7621 proc FcopyTestDone {bytes {error {}}} {
7622 variable fcopyTestDone
7623 if {[string length $error]} {
7624 set fcopyTestDone 1
7625 } else {
7626 set fcopyTestDone 0
7627 }
7628 }
7629 test io-53.5 {CopyData: error during fcopy} {socket fcopy} {
7630 variable fcopyTestDone
7631 set listen [socket -server [namespace code FcopyTestAccept] -myaddr 127.0.0.1 0]
7632 set in [open $thisScript] ;# 126 K
7633 set out [socket 127.0.0.1 [lindex [fconfigure $listen -sockname] 2]]
7634 catch {unset fcopyTestDone}
7635 close $listen ;# This means the socket open never really succeeds
7636 fcopy $in $out -command [namespace code FcopyTestDone]
7637 variable fcopyTestDone
7638 if {![info exists fcopyTestDone]} {
7639 vwait [namespace which -variable fcopyTestDone] ;# The error occurs here in the b.g.
7640 }
7641 close $in
7642 close $out
7643 set fcopyTestDone ;# 1 for error condition
7644 } 1
7645 test io-53.6 {CopyData: error during fcopy} {stdio fcopy} {
7646 variable fcopyTestDone
7647 file delete $path(pipe)
7648 file delete $path(test1)
7649 catch {unset fcopyTestDone}
7650 set f1 [open $path(pipe) w]
7651 puts $f1 "exit 1"
7652 close $f1
7653 set in [open "|[list [interpreter] $path(pipe)]" r+]
7654 set out [open $path(test1) w]
7655 fcopy $in $out -command [namespace code FcopyTestDone]
7656 variable fcopyTestDone
7657 if {![info exists fcopyTestDone]} {
7658 vwait [namespace which -variable fcopyTestDone]
7659 }
7660 catch {close $in}
7661 close $out
7662 set fcopyTestDone ;# 0 for plain end of file
7663 } {0}
7664 proc doFcopy {in out {bytes 0} {error {}}} {
7665 variable fcopyTestDone
7666 variable fcopyTestCount
7667 incr fcopyTestCount $bytes
7668 if {[string length $error]} {
7669 set fcopyTestDone 1
7670 } elseif {[eof $in]} {
7671 set fcopyTestDone 0
7672 } else {
7673 # Delay next fcopy to wait for size>0 input bytes
7674 after 100 [list fcopy $in $out -size 1000 \
7675 -command [namespace code [list doFcopy $in $out]]]
7676 }
7677 }
7678 test io-53.7 {CopyData: Flooding fcopy from pipe} {stdio fcopy} {
7679 variable fcopyTestDone
7680 file delete $path(pipe)
7681 catch {unset fcopyTestDone}
7682 set fcopyTestCount 0
7683 set f1 [open $path(pipe) w]
7684 puts $f1 {
7685 # Write 10 bytes / 10 msec
7686 proc Write {count} {
7687 puts -nonewline "1234567890"
7688 if {[incr count -1]} {
7689 after 10 [list Write $count]
7690 } else {
7691 set ::ready 1
7692 }
7693 }
7694 fconfigure stdout -buffering none
7695 Write 345 ;# 3450 bytes ~3.45 sec
7696 vwait ready
7697 exit 0
7698 }
7699 close $f1
7700 set in [open "|[list [interpreter] $path(pipe) &]" r+]
7701 set out [open $path(test1) w]
7702 doFcopy $in $out
7703 variable fcopyTestDone
7704 if {![info exists fcopyTestDone]} {
7705 vwait [namespace which -variable fcopyTestDone]
7706 }
7707 catch {close $in}
7708 close $out
7709 # -1=error 0=script error N=number of bytes
7710 expr {($fcopyTestDone == 0) ? $fcopyTestCount : -1}
7711 } {3450}
7712 test io-53.8 {CopyData: async callback and error handling, Bug 1932639} -setup {
7713 # copy progress callback. errors out intentionally
7714 proc ::cmd args {
7715 lappend ::RES "CMD $args"
7716 error !STOP
7717 }
7718 # capture callback error here
7719 proc ::bgerror args {
7720 lappend ::RES "bgerror/OK $args"
7721 set ::forever has-been-reached
7722 return
7723 }
7724 # Files we use for our channels
7725 set foo [makeFile ashgdfashdgfasdhgfasdhgf foo]
7726 set bar [makeFile {} bar]
7727 # Channels to copy between
7728 set f [open $foo r] ; fconfigure $f -translation binary
7729 set g [open $bar w] ; fconfigure $g -translation binary -buffering none
7730 } -constraints {stdio fcopy} -body {
7731 # Record input size, so that result is always defined
7732 lappend ::RES [file size $bar]
7733 # Run the copy. Should not invoke -command now.
7734 fcopy $f $g -size 2 -command ::cmd
7735 # Check that -command was not called synchronously
7736 set sbs [file size $bar]
7737 lappend ::RES [expr {($sbs > 0) ? "sync/FAIL" : "sync/OK"}] $sbs
7738 # Now let the async part happen. Should capture the error in cmd
7739 # via bgerror. If not break the event loop via timer.
7740 set token [after 1000 {
7741 lappend ::RES {bgerror/FAIL timeout}
7742 set ::forever has-been-reached
7743 }]
7744 vwait ::forever
7745 catch {after cancel $token}
7746 # Report
7747 set ::RES
7748 } -cleanup {
7749 close $f
7750 close $g
7751 catch {unset ::RES}
7752 catch {unset ::forever}
7753 rename ::cmd {}
7754 rename ::bgerror {}
7755 removeFile foo
7756 removeFile bar
7757 } -result {0 sync/OK 0 {CMD 2} {bgerror/OK !STOP}}
7758 test io-53.8a {CopyData: async callback and error handling, Bug 1932639, at eof} -setup {
7759 # copy progress callback. errors out intentionally
7760 proc ::cmd args {
7761 lappend ::RES "CMD $args"
7762 set ::forever has-been-reached
7763 return
7764 }
7765 # Files we use for our channels
7766 set foo [makeFile ashgdfashdgfasdhgfasdhgf foo]
7767 set bar [makeFile {} bar]
7768 # Channels to copy between
7769 set f [open $foo r] ; fconfigure $f -translation binary
7770 set g [open $bar w] ; fconfigure $g -translation binary -buffering none
7771 } -constraints {stdio fcopy} -body {
7772 # Initialize and force eof on the input.
7773 seek $f 0 end ; read $f 1
7774 set ::RES [eof $f]
7775 # Run the copy. Should not invoke -command now.
7776 fcopy $f $g -size 2 -command ::cmd
7777 # Check that -command was not called synchronously
7778 lappend ::RES [expr {([llength $::RES] > 1) ? "sync/FAIL" : "sync/OK"}]
7779 # Now let the async part happen. Should capture the eof in cmd
7780 # If not break the event loop via timer.
7781 set token [after 1000 {
7782 lappend ::RES {cmd/FAIL timeout}
7783 set ::forever has-been-reached
7784 }]
7785 vwait ::forever
7786 catch {after cancel $token}
7787 # Report
7788 set ::RES
7789 } -cleanup {
7790 close $f
7791 close $g
7792 catch {unset ::RES}
7793 catch {unset ::forever}
7794 rename ::cmd {}
7795 removeFile foo
7796 removeFile bar
7797 } -result {1 sync/OK {CMD 0}}
7798 test io-53.8b {CopyData: async callback and -size 0} -setup {
7799 # copy progress callback. errors out intentionally
7800 proc ::cmd args {
7801 lappend ::RES "CMD $args"
7802 set ::forever has-been-reached
7803 return
7804 }
7805 # Files we use for our channels
7806 set foo [makeFile ashgdfashdgfasdhgfasdhgf foo]
7807 set bar [makeFile {} bar]
7808 # Channels to copy between
7809 set f [open $foo r] ; fconfigure $f -translation binary
7810 set g [open $bar w] ; fconfigure $g -translation binary -buffering none
7811 } -constraints {stdio fcopy} -body {
7812 set ::RES {}
7813 # Run the copy. Should not invoke -command now.
7814 fcopy $f $g -size 0 -command ::cmd
7815 # Check that -command was not called synchronously
7816 lappend ::RES [expr {([llength $::RES] > 1) ? "sync/FAIL" : "sync/OK"}]
7817 # Now let the async part happen. Should capture the eof in cmd
7818 # If not break the event loop via timer.
7819 set token [after 1000 {
7820 lappend ::RES {cmd/FAIL timeout}
7821 set ::forever has-been-reached
7822 }]
7823 vwait ::forever
7824 catch {after cancel $token}
7825 # Report
7826 set ::RES
7827 } -cleanup {
7828 close $f
7829 close $g
7830 catch {unset ::RES}
7831 catch {unset ::forever}
7832 rename ::cmd {}
7833 removeFile foo
7834 removeFile bar
7835 } -result {sync/OK {CMD 0}}
7836 test io-53.9 {CopyData: -size and event interaction, Bug 780533} -setup {
7837 set out [makeFile {} out]
7838 set err [makeFile {} err]
7839 set pipe [open "|[list [info nameofexecutable] 2> $err]" r+]
7840 fconfigure $pipe -translation binary -buffering line
7841 puts $pipe {
7842 fconfigure stdout -translation binary -buffering line
7843 puts stderr Waiting...
7844 after 1000
7845 foreach x {a b c} {
7846 puts stderr Looping...
7847 puts $x
7848 after 500
7849 }
7850 proc bye args {
7851 if {[gets stdin line]<0} {
7852 puts stderr "CHILD: EOF detected, exiting"
7853 exit
7854 } else {
7855 puts stderr "CHILD: ignoring line: $line"
7856 }
7857 }
7858 puts stderr Now-sleeping-forever
7859 fileevent stdin readable bye
7860 vwait forever
7861 }
7862 proc ::done args {
7863 set ::forever OK
7864 return
7865 }
7866 set ::forever {}
7867 set out [open $out w]
7868 } -constraints {stdio fcopy} -body {
7869 fcopy $pipe $out -size 6 -command ::done
7870 set token [after 5000 {
7871 set ::forever {fcopy hangs}
7872 }]
7873 vwait ::forever
7874 catch {after cancel $token}
7875 set ::forever
7876 } -cleanup {
7877 close $pipe
7878 rename ::done {}
7879 after 1000; # Give Windows time to kill the process
7880 catch {close $out}
7881 catch {removeFile out}
7882 catch {removeFile err}
7883 catch {unset ::forever}
7884 } -result OK
7885 test io-53.10 {Bug 1350564, multi-directional fcopy} -setup {
7886 set err [makeFile {} err]
7887 set pipe [open "|[list [info nameofexecutable] 2> $err]" r+]
7888 fconfigure $pipe -translation binary -buffering line
7889 puts $pipe {
7890 fconfigure stderr -buffering line
7891 # Kill server when pipe closed by invoker.
7892 proc bye args {
7893 if {![eof stdin]} { gets stdin ; return }
7894 puts stderr BYE
7895 exit
7896 }
7897 # Server code. Bi-directional copy between 2 sockets.
7898 proc geof {sok} {
7899 puts stderr DONE/$sok
7900 close $sok
7901 }
7902 proc new {sok args} {
7903 puts stderr NEW/$sok
7904 global l srv
7905 fconfigure $sok -translation binary -buffering none
7906 lappend l $sok
7907 if {[llength $l]==2} {
7908 close $srv
7909 foreach {a b} $l break
7910 fcopy $a $b -command [list geof $a]
7911 fcopy $b $a -command [list geof $b]
7912 puts stderr 2COPY
7913 }
7914 puts stderr ...
7915 }
7916 puts stderr SRV
7917 set l {}
7918 set srv [socket -server new -myaddr 127.0.0.1 0]
7919 set port [lindex [fconfigure $srv -sockname] 2]
7920 puts stderr WAITING
7921 fileevent stdin readable bye
7922 puts "OK $port"
7923 vwait forever
7924 }
7925 # wait for OK from server.
7926 lassign [gets $pipe] ok port
7927 # Now the two clients.
7928 proc ::done {sock} {
7929 if {[eof $sock]} { close $sock ; return }
7930 lappend ::forever [gets $sock]
7931 return
7932 }
7933 set a [socket 127.0.0.1 $port]
7934 set b [socket 127.0.0.1 $port]
7935 fconfigure $a -translation binary -buffering none
7936 fconfigure $b -translation binary -buffering none
7937 fileevent $a readable [list ::done $a]
7938 fileevent $b readable [list ::done $b]
7939 } -constraints {stdio fcopy} -body {
7940 # Now pass data through the server in both directions.
7941 set ::forever {}
7942 puts $a AB
7943 vwait ::forever
7944 puts $b BA
7945 vwait ::forever
7946 set ::forever
7947 } -cleanup {
7948 catch {close $a}
7949 catch {close $b}
7950 close $pipe
7951 rename ::done {}
7952 after 1000 ;# Give Windows time to kill the process
7953 removeFile err
7954 catch {unset ::forever}
7955 } -result {AB BA}
7956 test io-53.11 {Bug 2895565} -setup {
7957 set in [makeFile {} in]
7958 set f [open $in w]
7959 fconfigure $f -encoding utf-8 -translation binary
7960 puts -nonewline $f [string repeat "Ho hum\n" 11]
7961 close $f
7962 set inChan [open $in r]
7963 fconfigure $inChan -translation binary
7964 set out [makeFile {} out]
7965 set outChan [open $out w]
7966 fconfigure $outChan -encoding cp1252 -translation crlf
7967 proc CopyDone {bytes args} {
7968 variable done
7969 if {[llength $args]} {
7970 set done "Error: '[lindex $args 0]' after $bytes bytes copied"
7971 } else {
7972 set done "$bytes bytes copied"
7973 }
7974 }
7975 } -body {
7976 variable done
7977 after 2000 [list set [namespace which -variable done] timeout]
7978 fcopy $inChan $outChan -size 40 -command [namespace which CopyDone]
7979 vwait [namespace which -variable done]
7980 set done
7981 } -cleanup {
7982 close $outChan
7983 close $inChan
7984 removeFile out
7985 removeFile in
7986 } -result {40 bytes copied}
7987 test io-53.12 {CopyData: foreground short reads, aka bug 3096275} {stdio unix fcopy} {
7988 file delete $path(pipe)
7989 set f1 [open $path(pipe) w]
7990 puts -nonewline $f1 {
7991 fconfigure stdin -translation binary -blocking 0
7992 fconfigure stdout -buffering none -translation binary
7993 fcopy stdin stdout
7994 }
7995 close $f1
7996 set f1 [open "|[list [interpreter] $path(pipe)]" r+]
7997 fconfigure $f1 -translation binary -buffering none
7998 puts -nonewline $f1 A
7999 after 2000 {set ::done timeout}
8000 fileevent $f1 readable {set ::done ok}
8001 vwait ::done
8002 set ch [read $f1 1]
8003 close $f1
8004 list $::done $ch
8005 } {ok A}
8006 test io-53.13 {TclCopyChannel: read error reporting} -setup {
8007 proc driver {cmd args} {
8008 variable buffer
8009 variable index
8010 set chan [lindex $args 0]
8011 switch -- $cmd {
8012 initialize {
8013 return {initialize finalize watch read}
8014 }
8015 finalize {
8016 return
8017 }
8018 watch {}
8019 read {
8020 error FAIL
8021 }
8022 }
8023 }
8024 set outFile [makeFile {} out]
8025 } -body {
8026 set in [chan create read [namespace which driver]]
8027 chan configure $in -translation binary
8028 set out [open $outFile wb]
8029 chan copy $in $out
8030 } -cleanup {
8031 catch {close $in}
8032 catch {close $out}
8033 removeFile out
8034 rename driver {}
8035 } -result {error reading "*": *} -returnCodes error -match glob
8036 test io-53.14 {TclCopyChannel: write error reporting} -setup {
8037 proc driver {cmd args} {
8038 variable buffer
8039 variable index
8040 set chan [lindex $args 0]
8041 switch -- $cmd {
8042 initialize {
8043 return {initialize finalize watch write}
8044 }
8045 finalize {
8046 return
8047 }
8048 watch {}
8049 write {
8050 error FAIL
8051 }
8052 }
8053 }
8054 set inFile [makeFile {aaa} in]
8055 } -body {
8056 set in [open $inFile rb]
8057 set out [chan create write [namespace which driver]]
8058 chan configure $out -translation binary
8059 chan copy $in $out
8060 } -cleanup {
8061 catch {close $in}
8062 catch {close $out}
8063 removeFile in
8064 rename driver {}
8065 } -result {error writing "*": *} -returnCodes error -match glob
8066 test io-53.15 {[ed29c4da21] DoRead: fblocked seen as error} -setup {
8067 proc driver {cmd args} {
8068 variable buffer
8069 variable index
8070 variable blocked
8071 set chan [lindex $args 0]
8072 switch -- $cmd {
8073 initialize {
8074 set index($chan) 0
8075 set buffer($chan) [encoding convertto utf-8 \
8076 [string repeat a 100]]
8077 set blocked($chan) 1
8078 return {initialize finalize watch read}
8079 }
8080 finalize {
8081 unset index($chan) buffer($chan) blocked($chan)
8082 return
8083 }
8084 watch {}
8085 read {
8086 if {$blocked($chan)} {
8087 set blocked($chan) [expr {!$blocked($chan)}]
8088 return -code error EAGAIN
8089 }
8090 set n [lindex $args 1]
8091 set new [expr {$index($chan) + $n}]
8092 set result [string range $buffer($chan) $index($chan) $new-1]
8093 set index($chan) $new
8094 return $result
8095 }
8096 }
8097 }
8098 set c [chan create read [namespace which driver]]
8099 chan configure $c -encoding utf-8
8100 set out [makeFile {} out]
8101 set outChan [open $out w]
8102 chan configure $outChan -encoding utf-8
8103 } -body {
8104 chan copy $c $outChan
8105 } -cleanup {
8106 close $outChan
8107 close $c
8108 removeFile out
8109 } -result 100
8110 test io-53.16 {[ed29c4da21] MBRead: fblocked seen as error} -setup {
8111 proc driver {cmd args} {
8112 variable buffer
8113 variable index
8114 variable blocked
8115 set chan [lindex $args 0]
8116 switch -- $cmd {
8117 initialize {
8118 set index($chan) 0
8119 set buffer($chan) [encoding convertto utf-8 \
8120 [string repeat a 100]]
8121 set blocked($chan) 1
8122 return {initialize finalize watch read}
8123 }
8124 finalize {
8125 unset index($chan) buffer($chan) blocked($chan)
8126 return
8127 }
8128 watch {}
8129 read {
8130 if {$blocked($chan)} {
8131 set blocked($chan) [expr {!$blocked($chan)}]
8132 return -code error EAGAIN
8133 }
8134 set n [lindex $args 1]
8135 set new [expr {$index($chan) + $n}]
8136 set result [string range $buffer($chan) $index($chan) $new-1]
8137 set index($chan) $new
8138 return $result
8139 }
8140 }
8141 }
8142 set c [chan create read [namespace which driver]]
8143 chan configure $c -encoding utf-8 -translation lf
8144 set out [makeFile {} out]
8145 set outChan [open $out w]
8146 chan configure $outChan -encoding utf-8 -translation lf
8147 } -body {
8148 chan copy $c $outChan
8149 } -cleanup {
8150 close $outChan
8151 close $c
8152 removeFile out
8153 } -result 100
8154 test io-53.17 {[7c187a3773] MBWrite: proper inQueueTail handling} -setup {
8155 proc driver {cmd args} {
8156 variable buffer
8157 variable index
8158 set chan [lindex $args 0]
8159 switch -- $cmd {
8160 initialize {
8161 set index($chan) 0
8162 set buffer($chan) [encoding convertto utf-8 \
8163 line\n[string repeat a 100]line\n]
8164 return {initialize finalize watch read}
8165 }
8166 finalize {
8167 unset index($chan) buffer($chan)
8168 return
8169 }
8170 watch {}
8171 read {
8172 set n [lindex $args 1]
8173 set new [expr {$index($chan) + $n}]
8174 set result [string range $buffer($chan) $index($chan) $new-1]
8175 set index($chan) $new
8176 return $result
8177 }
8178 }
8179 }
8180 set c [chan create read [namespace which driver]]
8181 chan configure $c -encoding utf-8 -translation lf -buffersize 107
8182 set out [makeFile {} out]
8183 set outChan [open $out w]
8184 chan configure $outChan -encoding utf-8 -translation lf
8185 } -body {
8186 list [gets $c] [chan copy $c $outChan -size 100] [gets $c]
8187 } -cleanup {
8188 close $outChan
8189 close $c
8190 removeFile out
8191 } -result {line 100 line}
8192
8193 test io-54.1 {Recursive channel events} {socket fileevent notWinCI} {
8194 # This test checks to see if file events are delivered during recursive
8195 # event loops when there is buffered data on the channel.
8196
8197 proc accept {s a p} {
8198 variable as
8199 fconfigure $s -translation lf
8200 puts $s "line 1\nline2\nline3"
8201 flush $s
8202 set as $s
8203 }
8204 proc readit {s next} {
8205 variable x
8206 variable result
8207 lappend result $next
8208 if {$next == 1} {
8209 fileevent $s readable [namespace code [list readit $s 2]]
8210 vwait [namespace which -variable x]
8211 }
8212 incr x
8213 }
8214 set ss [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
8215
8216 # We need to delay on some systems until the creation of the
8217 # server socket completes.
8218
8219 set done 0
8220 for {set i 0} {$i < 10} {incr i} {
8221 if {![catch {set cs [socket 127.0.0.1 [lindex [fconfigure $ss -sockname] 2]]}]} {
8222 set done 1
8223 break
8224 }
8225 after 100
8226 }
8227 if {$done == 0} {
8228 close $ss
8229 error "failed to connect to server"
8230 }
8231 variable result {}
8232 variable x 0
8233 variable as
8234 vwait [namespace which -variable as]
8235 fconfigure $cs -translation lf
8236 lappend result [gets $cs]
8237 fconfigure $cs -blocking off
8238 fileevent $cs readable [namespace code [list readit $cs 1]]
8239 set a [after 2000 [namespace code { set x failure }]]
8240 vwait [namespace which -variable x]
8241 after cancel $a
8242 close $as
8243 close $ss
8244 close $cs
8245 list $result $x
8246 } {{{line 1} 1 2} 2}
8247 test io-54.2 {Testing for busy-wait in recursive channel events} {socket fileevent} {
8248 set accept {}
8249 set after {}
8250 variable s [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
8251 proc accept {s a p} {
8252 variable counter
8253 variable accept
8254
8255 set accept $s
8256 set counter 0
8257 fconfigure $s -blocking off -buffering line -translation lf
8258 fileevent $s readable [namespace code "doit $s"]
8259 }
8260 proc doit {s} {
8261 variable counter
8262 variable after
8263
8264 incr counter
8265 set l [gets $s]
8266 if {"$l" eq ""} {
8267 fileevent $s readable [namespace code "doit1 $s"]
8268 set after [after 1000 [namespace code newline]]
8269 }
8270 }
8271 proc doit1 {s} {
8272 variable counter
8273 variable accept
8274
8275 incr counter
8276 set l [gets $s]
8277 close $s
8278 set accept {}
8279 }
8280 proc producer {} {
8281 variable s
8282 variable writer
8283
8284 set writer [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]]
8285 fconfigure $writer -buffering line
8286 puts -nonewline $writer hello
8287 flush $writer
8288 }
8289 proc newline {} {
8290 variable done
8291 variable writer
8292
8293 puts $writer hello
8294 flush $writer
8295 set done 1
8296 }
8297 producer
8298 variable done
8299 vwait [namespace which -variable done]
8300 close $writer
8301 close $s
8302 after cancel $after
8303 if {$accept != {}} {close $accept}
8304 set counter
8305 } 1
8306
8307 set path(fooBar) [makeFile {} fooBar]
8308
8309 test io-55.1 {ChannelEventScriptInvoker: deletion} -constraints {
8310 fileevent
8311 } -setup {
8312 variable x
8313 proc eventScript {fd} {
8314 variable x
8315 close $fd
8316 error "planned error"
8317 set x whoops
8318 }
8319 proc myHandler args {
8320 variable x got_error
8321 }
8322 set handler [interp bgerror {}]
8323 interp bgerror {} [namespace which myHandler]
8324 } -body {
8325 set f [open $path(fooBar) w]
8326 fileevent $f writable [namespace code [list eventScript $f]]
8327 variable x not_done
8328 vwait [namespace which -variable x]
8329 set x
8330 } -cleanup {
8331 interp bgerror {} $handler
8332 } -result {got_error}
8333
8334 test io-56.1 {ChannelTimerProc} {testchannelevent} {
8335 set f [open $path(fooBar) w]
8336 puts $f "this is a test"
8337 close $f
8338 set f [open $path(fooBar) r]
8339 testchannelevent $f add readable [namespace code {
8340 read $f 1
8341 incr x
8342 }]
8343 variable x 0
8344 vwait [namespace which -variable x]
8345 vwait [namespace which -variable x]
8346 set result $x
8347 testchannelevent $f set 0 none
8348 after idle [namespace code {set y done}]
8349 variable y
8350 vwait [namespace which -variable y]
8351 close $f
8352 lappend result $y
8353 } {2 done}
8354
8355 test io-57.1 {buffered data and file events, gets} {fileevent} {
8356 proc accept {sock args} {
8357 variable s2
8358 set s2 $sock
8359 }
8360 set server [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
8361 set s [socket 127.0.0.1 [lindex [fconfigure $server -sockname] 2]]
8362 variable s2
8363 vwait [namespace which -variable s2]
8364 update
8365 fileevent $s2 readable [namespace code {lappend result readable}]
8366 puts $s "12\n34567890"
8367 flush $s
8368 variable result [gets $s2]
8369 after 1000 [namespace code {lappend result timer}]
8370 vwait [namespace which -variable result]
8371 lappend result [gets $s2]
8372 vwait [namespace which -variable result]
8373 close $s
8374 close $s2
8375 close $server
8376 set result
8377 } {12 readable 34567890 timer}
8378 test io-57.2 {buffered data and file events, read} {fileevent} {
8379 proc accept {sock args} {
8380 variable s2
8381 set s2 $sock
8382 }
8383 set server [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
8384 set s [socket 127.0.0.1 [lindex [fconfigure $server -sockname] 2]]
8385 variable s2
8386 vwait [namespace which -variable s2]
8387 update
8388 fileevent $s2 readable [namespace code {lappend result readable}]
8389 puts -nonewline $s "1234567890"
8390 flush $s
8391 variable result [read $s2 1]
8392 after 1000 [namespace code {lappend result timer}]
8393 vwait [namespace which -variable result]
8394 lappend result [read $s2 9]
8395 vwait [namespace which -variable result]
8396 close $s
8397 close $s2
8398 close $server
8399 set result
8400 } {1 readable 234567890 timer}
8401
8402 test io-58.1 {Tcl_NotifyChannel and error when closing} {stdio unixOrWin fileevent} {
8403 set out [open $path(script) w]
8404 puts $out {
8405 puts "normal message from pipe"
8406 puts stderr "error message from pipe"
8407 exit 1
8408 }
8409 proc readit {pipe} {
8410 variable x
8411 variable result
8412 if {[eof $pipe]} {
8413 set x [catch {close $pipe} line]
8414 lappend result catch $line
8415 } else {
8416 gets $pipe line
8417 lappend result gets $line
8418 }
8419 }
8420 close $out
8421 set pipe [open "|[list [interpreter] $path(script)]" r]
8422 fileevent $pipe readable [namespace code [list readit $pipe]]
8423 variable x ""
8424 set result ""
8425 vwait [namespace which -variable x]
8426 list $x $result
8427 } {1 {gets {normal message from pipe} gets {} catch {error message from pipe}}}
8428
8429 test io-59.1 {Thread reference of channels} {testmainthread testchannel} {
8430 # TIP #10
8431 # More complicated tests (like that the reference changes as a
8432 # channel is moved from thread to thread) can be done only in the
8433 # extension which fully implements the moving of channels between
8434 # threads, i.e. 'Threads'.
8435
8436 set f [open $path(longfile) r]
8437 set result [testchannel mthread $f]
8438 close $f
8439 string equal $result [testmainthread]
8440 } {1}
8441
8442 test io-60.1 {writing illegal utf sequences} {fileevent testbytestring} {
8443 # This test will hang in older revisions of the core.
8444
8445 set out [open $path(script) w]
8446 puts $out "catch {load $::tcltestlib Tcltest}"
8447 puts $out {
8448 puts ABC[testbytestring \xE2]
8449 exit 1
8450 }
8451 proc readit {pipe} {
8452 variable x
8453 variable result
8454 if {[eof $pipe]} {
8455 set x [catch {close $pipe} line]
8456 lappend result catch $line
8457 } else {
8458 gets $pipe line
8459 lappend result gets $line
8460 }
8461 }
8462 close $out
8463 set pipe [open "|[list [interpreter] $path(script)]" r]
8464 fileevent $pipe readable [namespace code [list readit $pipe]]
8465 variable x ""
8466 set result ""
8467 vwait [namespace which -variable x]
8468
8469 # cut of the remainder of the error stack, especially the filename
8470 set result [lreplace $result 3 3 [lindex [split [lindex $result 3] \n] 0]]
8471 list $x $result
8472 } {1 {gets ABC catch {error writing "stdout": invalid argument}}}
8473
8474 test io-61.1 {Reset eof state after changing the eof char} -setup {
8475 set datafile [makeFile {} eofchar]
8476 set f [open $datafile w]
8477 fconfigure $f -translation binary
8478 puts -nonewline $f [string repeat "Ho hum\n" 11]
8479 puts $f =
8480 set line [string repeat "Ge gla " 4]
8481 puts -nonewline $f [string repeat [string trimright $line]\n 834]
8482 close $f
8483 } -body {
8484 set f [open $datafile r]
8485 fconfigure $f -eofchar =
8486 set res {}
8487 lappend res [read $f; tell $f]
8488 fconfigure $f -eofchar {}
8489 lappend res [read $f 1]
8490 lappend res [read $f; tell $f]
8491 # Any seek zaps the internals into a good state.
8492 #seek $f 0 start
8493 #seek $f 0 current
8494 #lappend res [read $f; tell $f]
8495 close $f
8496 set res
8497 } -cleanup {
8498 removeFile eofchar
8499 } -result {77 = 23431}
8500
8501
8502 # Test the cutting and splicing of channels, this is incidentally the
8503 # attach/detach facility of package Thread, but __without any
8504 # safeguards__. It can also be used to emulate transfer of channels
8505 # between threads, and is used for that here.
8506
8507 test io-70.0 {Cutting & Splicing channels} {testchannel} {
8508 set f [makeFile {... dummy ...} cutsplice]
8509 set c [open $f r]
8510
8511 set res {}
8512 lappend res [catch {seek $c 0 start}]
8513 testchannel cut $c
8514
8515 lappend res [catch {seek $c 0 start}]
8516 testchannel splice $c
8517
8518 lappend res [catch {seek $c 0 start}]
8519 close $c
8520
8521 removeFile cutsplice
8522
8523 set res
8524 } {0 1 0}
8525
8526
8527 test io-70.1 {Transfer channel} {testchannel thread} {
8528 set f [makeFile {... dummy ...} cutsplice]
8529 set c [open $f r]
8530
8531 set res {}
8532 lappend res [catch {seek $c 0 start}]
8533 testchannel cut $c
8534 lappend res [catch {seek $c 0 start}]
8535
8536 set tid [thread::create -preserved]
8537 thread::send $tid [list set c $c]
8538 thread::send $tid {load {} Tcltest}
8539 lappend res [thread::send $tid {
8540 testchannel splice $c
8541 set res [catch {seek $c 0 start}]
8542 close $c
8543 set res
8544 }]
8545
8546 thread::release $tid
8547 removeFile cutsplice
8548
8549 set res
8550 } {0 1 0}
8551
8552 # ### ### ### ######### ######### #########
8553
8554 foreach {n msg expected} {
8555 0 {} {}
8556 1 {{message only}} {{message only}}
8557 2 {-options x} {-options x}
8558 3 {-options {x y} {the message}} {-options {x y} {the message}}
8559
8560 4 {-code 1 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8561 5 {-code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8562 6 {-code 1 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8563 7 {-code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8564 8 {-code error -level 0 -f ba snarf} {-code error -level 0 -f ba snarf}
8565 9 {-code ok -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8566 10 {-code error -level 5 -f ba snarf} {-code error -level 0 -f ba snarf}
8567 11 {-code ok -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8568 12 {-code boss -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8569 13 {-code boss -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8570 14 {-code 1 -level 0 -f ba} {-code 1 -level 0 -f ba}
8571 15 {-code 0 -level 0 -f ba} {-code 1 -level 0 -f ba}
8572 16 {-code 1 -level 5 -f ba} {-code 1 -level 0 -f ba}
8573 17 {-code 0 -level 5 -f ba} {-code 1 -level 0 -f ba}
8574 18 {-code error -level 0 -f ba} {-code error -level 0 -f ba}
8575 19 {-code ok -level 0 -f ba} {-code 1 -level 0 -f ba}
8576 20 {-code error -level 5 -f ba} {-code error -level 0 -f ba}
8577 21 {-code ok -level 5 -f ba} {-code 1 -level 0 -f ba}
8578 22 {-code boss -level 0 -f ba} {-code 1 -level 0 -f ba}
8579 23 {-code boss -level 5 -f ba} {-code 1 -level 0 -f ba}
8580 24 {-code 1 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8581 25 {-code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8582 26 {-code error -level X -f ba snarf} {-code error -level 0 -f ba snarf}
8583 27 {-code ok -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8584 28 {-code boss -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8585 29 {-code 1 -level X -f ba} {-code 1 -level 0 -f ba}
8586 30 {-code 0 -level X -f ba} {-code 1 -level 0 -f ba}
8587 31 {-code error -level X -f ba} {-code error -level 0 -f ba}
8588 32 {-code ok -level X -f ba} {-code 1 -level 0 -f ba}
8589 33 {-code boss -level X -f ba} {-code 1 -level 0 -f ba}
8590
8591 34 {-code 1 -code 1 -level 0 -f ba snarf} {-code 1 -code 1 -level 0 -f ba snarf}
8592 35 {-code 1 -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8593 36 {-code 1 -code 1 -level 5 -f ba snarf} {-code 1 -code 1 -level 0 -f ba snarf}
8594 37 {-code 1 -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8595 38 {-code 1 -code error -level 0 -f ba snarf} {-code 1 -code error -level 0 -f ba snarf}
8596 39 {-code 1 -code ok -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8597 40 {-code 1 -code error -level 5 -f ba snarf} {-code 1 -code error -level 0 -f ba snarf}
8598 41 {-code 1 -code ok -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8599 42 {-code 1 -code boss -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8600 43 {-code 1 -code boss -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8601 44 {-code 1 -code 1 -level 0 -f ba} {-code 1 -code 1 -level 0 -f ba}
8602 45 {-code 1 -code 0 -level 0 -f ba} {-code 1 -level 0 -f ba}
8603 46 {-code 1 -code 1 -level 5 -f ba} {-code 1 -code 1 -level 0 -f ba}
8604 47 {-code 1 -code 0 -level 5 -f ba} {-code 1 -level 0 -f ba}
8605 48 {-code 1 -code error -level 0 -f ba} {-code 1 -code error -level 0 -f ba}
8606 49 {-code 1 -code ok -level 0 -f ba} {-code 1 -level 0 -f ba}
8607 50 {-code 1 -code error -level 5 -f ba} {-code 1 -code error -level 0 -f ba}
8608 51 {-code 1 -code ok -level 5 -f ba} {-code 1 -level 0 -f ba}
8609 52 {-code 1 -code boss -level 0 -f ba} {-code 1 -level 0 -f ba}
8610 53 {-code 1 -code boss -level 5 -f ba} {-code 1 -level 0 -f ba}
8611 54 {-code 1 -code 1 -level X -f ba snarf} {-code 1 -code 1 -level 0 -f ba snarf}
8612 55 {-code 1 -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8613 56 {-code 1 -code error -level X -f ba snarf} {-code 1 -code error -level 0 -f ba snarf}
8614 57 {-code 1 -code ok -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8615 58 {-code 1 -code boss -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8616 59 {-code 1 -code 1 -level X -f ba} {-code 1 -code 1 -level 0 -f ba}
8617 60 {-code 1 -code 0 -level X -f ba} {-code 1 -level 0 -f ba}
8618 61 {-code 1 -code error -level X -f ba} {-code 1 -code error -level 0 -f ba}
8619 62 {-code 1 -code ok -level X -f ba} {-code 1 -level 0 -f ba}
8620 63 {-code 1 -code boss -level X -f ba} {-code 1 -level 0 -f ba}
8621
8622 64 {-code 0 -code 1 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8623 65 {-code 0 -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8624 66 {-code 0 -code 1 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8625 67 {-code 0 -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8626 68 {-code 0 -code error -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8627 69 {-code 0 -code ok -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8628 70 {-code 0 -code error -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8629 71 {-code 0 -code ok -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8630 72 {-code 0 -code boss -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8631 73 {-code 0 -code boss -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8632 74 {-code 0 -code 1 -level 0 -f ba} {-code 1 -level 0 -f ba}
8633 75 {-code 0 -code 0 -level 0 -f ba} {-code 1 -level 0 -f ba}
8634 76 {-code 0 -code 1 -level 5 -f ba} {-code 1 -level 0 -f ba}
8635 77 {-code 0 -code 0 -level 5 -f ba} {-code 1 -level 0 -f ba}
8636 78 {-code 0 -code error -level 0 -f ba} {-code 1 -level 0 -f ba}
8637 79 {-code 0 -code ok -level 0 -f ba} {-code 1 -level 0 -f ba}
8638 80 {-code 0 -code error -level 5 -f ba} {-code 1 -level 0 -f ba}
8639 81 {-code 0 -code ok -level 5 -f ba} {-code 1 -level 0 -f ba}
8640 82 {-code 0 -code boss -level 0 -f ba} {-code 1 -level 0 -f ba}
8641 83 {-code 0 -code boss -level 5 -f ba} {-code 1 -level 0 -f ba}
8642 84 {-code 0 -code 1 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8643 85 {-code 0 -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8644 86 {-code 0 -code error -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8645 87 {-code 0 -code ok -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8646 88 {-code 0 -code boss -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8647 89 {-code 0 -code 1 -level X -f ba} {-code 1 -level 0 -f ba}
8648 90 {-code 0 -code 0 -level X -f ba} {-code 1 -level 0 -f ba}
8649 91 {-code 0 -code error -level X -f ba} {-code 1 -level 0 -f ba}
8650 92 {-code 0 -code ok -level X -f ba} {-code 1 -level 0 -f ba}
8651 93 {-code 0 -code boss -level X -f ba} {-code 1 -level 0 -f ba}
8652
8653 94 {-code 1 -code 1 -level 0 -f ba snarf} {-code 1 -code 1 -level 0 -f ba snarf}
8654 95 {-code 0 -code 1 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8655 96 {-code 1 -code 1 -level 5 -f ba snarf} {-code 1 -code 1 -level 0 -f ba snarf}
8656 97 {-code 0 -code 1 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8657 98 {-code error -code 1 -level 0 -f ba snarf} {-code error -code 1 -level 0 -f ba snarf}
8658 99 {-code ok -code 1 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8659 a0 {-code error -code 1 -level 5 -f ba snarf} {-code error -code 1 -level 0 -f ba snarf}
8660 a1 {-code ok -code 1 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8661 a2 {-code boss -code 1 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8662 a3 {-code boss -code 1 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8663 a4 {-code 1 -code 1 -level 0 -f ba} {-code 1 -code 1 -level 0 -f ba}
8664 a5 {-code 0 -code 1 -level 0 -f ba} {-code 1 -level 0 -f ba}
8665 a6 {-code 1 -code 1 -level 5 -f ba} {-code 1 -code 1 -level 0 -f ba}
8666 a7 {-code 0 -code 1 -level 5 -f ba} {-code 1 -level 0 -f ba}
8667 a8 {-code error -code 1 -level 0 -f ba} {-code error -code 1 -level 0 -f ba}
8668 a9 {-code ok -code 1 -level 0 -f ba} {-code 1 -level 0 -f ba}
8669 b0 {-code error -code 1 -level 5 -f ba} {-code error -code 1 -level 0 -f ba}
8670 b1 {-code ok -code 1 -level 5 -f ba} {-code 1 -level 0 -f ba}
8671 b2 {-code boss -code 1 -level 0 -f ba} {-code 1 -level 0 -f ba}
8672 b3 {-code boss -code 1 -level 5 -f ba} {-code 1 -level 0 -f ba}
8673 b4 {-code 1 -code 1 -level X -f ba snarf} {-code 1 -code 1 -level 0 -f ba snarf}
8674 b5 {-code 0 -code 1 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8675 b6 {-code error -code 1 -level X -f ba snarf} {-code error -code 1 -level 0 -f ba snarf}
8676 b7 {-code ok -code 1 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8677 b8 {-code boss -code 1 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8678 b9 {-code 1 -code 1 -level X -f ba} {-code 1 -code 1 -level 0 -f ba}
8679 c0 {-code 0 -code 1 -level X -f ba} {-code 1 -level 0 -f ba}
8680 c1 {-code error -code 1 -level X -f ba} {-code error -code 1 -level 0 -f ba}
8681 c2 {-code ok -code 1 -level X -f ba} {-code 1 -level 0 -f ba}
8682 c3 {-code boss -code 1 -level X -f ba} {-code 1 -level 0 -f ba}
8683
8684 c4 {-code 1 -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8685 c5 {-code 0 -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8686 c6 {-code 1 -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8687 c7 {-code 0 -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8688 c8 {-code error -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8689 c9 {-code ok -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8690 d0 {-code error -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8691 d1 {-code ok -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8692 d2 {-code boss -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8693 d3 {-code boss -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
8694 d4 {-code 1 -code 0 -level 0 -f ba} {-code 1 -level 0 -f ba}
8695 d5 {-code 0 -code 0 -level 0 -f ba} {-code 1 -level 0 -f ba}
8696 d6 {-code 1 -code 0 -level 5 -f ba} {-code 1 -level 0 -f ba}
8697 d7 {-code 0 -code 0 -level 5 -f ba} {-code 1 -level 0 -f ba}
8698 d8 {-code error -code 0 -level 0 -f ba} {-code 1 -level 0 -f ba}
8699 d9 {-code ok -code 0 -level 0 -f ba} {-code 1 -level 0 -f ba}
8700 e0 {-code error -code 0 -level 5 -f ba} {-code 1 -level 0 -f ba}
8701 e1 {-code ok -code 0 -level 5 -f ba} {-code 1 -level 0 -f ba}
8702 e2 {-code boss -code 0 -level 0 -f ba} {-code 1 -level 0 -f ba}
8703 e3 {-code boss -code 0 -level 5 -f ba} {-code 1 -level 0 -f ba}
8704 e4 {-code 1 -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8705 e5 {-code 0 -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8706 e6 {-code error -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8707 e7 {-code ok -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8708 e8 {-code boss -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
8709 e9 {-code 1 -code 0 -level X -f ba} {-code 1 -level 0 -f ba}
8710 f0 {-code 0 -code 0 -level X -f ba} {-code 1 -level 0 -f ba}
8711 f1 {-code error -code 0 -level X -f ba} {-code 1 -level 0 -f ba}
8712 f2 {-code ok -code 0 -level X -f ba} {-code 1 -level 0 -f ba}
8713 f3 {-code boss -code 0 -level X -f ba} {-code 1 -level 0 -f ba}
8714 } {
8715 test io-71.$n {Tcl_SetChannelError} {testchannel} {
8716
8717 set f [makeFile {... dummy ...} cutsplice]
8718 set c [open $f r]
8719
8720 set res [testchannel setchannelerror $c [lrange $msg 0 end]]
8721 close $c
8722 removeFile cutsplice
8723
8724 set res
8725 } [lrange $expected 0 end]
8726
8727 test io-72.$n {Tcl_SetChannelErrorInterp} {testchannel} {
8728
8729 set f [makeFile {... dummy ...} cutsplice]
8730 set c [open $f r]
8731
8732 set res [testchannel setchannelerrorinterp $c [lrange $msg 0 end]]
8733 close $c
8734 removeFile cutsplice
8735
8736 set res
8737 } [lrange $expected 0 end]
8738 }
8739
8740 test io-73.1 {channel Tcl_Obj SetChannelFromAny} {} {
8741 # Test for Bug 1847044 - don't spoil type unless we have a valid channel
8742 catch {close [lreplace [list a] 0 end]}
8743 } {1}
8744
8745 test io-73.2 {channel Tcl_Obj SetChannelFromAny, bug 2407783} -setup {
8746 # Invalidate internalrep of 'channel' Tcl_Obj when transiting between interpreters.
8747 set f [open [info script] r]
8748 } -body {
8749 interp create foo
8750 seek $f 0
8751 set code [catch {interp eval foo [list seek $f 0]} msg]
8752 # The string map converts the changing channel handle to a fixed string
8753 list $code [string map [list $f @@] $msg]
8754 } -cleanup {
8755 close $f
8756 } -result {1 {can not find channel named "@@"}}
8757
8758 test io-73.3 {[5adc350683] [gets] after EOF} -setup {
8759 set fn [makeFile {} io-73.3]
8760 set rfd [open $fn r]
8761 set wfd [open $fn a]
8762 chan configure $wfd -buffering line
8763 read $rfd
8764 } -body {
8765 set result [eof $rfd]
8766 puts $wfd "more data"
8767 lappend result [eof $rfd]
8768 lappend result [gets $rfd]
8769 lappend result [eof $rfd]
8770 lappend result [gets $rfd]
8771 lappend result [eof $rfd]
8772 } -cleanup {
8773 close $wfd
8774 close $rfd
8775 removeFile io-73.3
8776 } -result {1 1 {more data} 0 {} 1}
8777
8778 test io-73.4 {[5adc350683] [read] after EOF} -setup {
8779 set fn [makeFile {} io-73.4]
8780 set rfd [open $fn r]
8781 set wfd [open $fn a]
8782 chan configure $wfd -buffering line
8783 read $rfd
8784 } -body {
8785 set result [eof $rfd]
8786 puts $wfd "more data"
8787 lappend result [eof $rfd]
8788 lappend result [read $rfd]
8789 lappend result [eof $rfd]
8790 } -cleanup {
8791 close $wfd
8792 close $rfd
8793 removeFile io-73.4
8794 } -result {1 1 {more data
8795 } 1}
8796
8797 test io-73.5 {effect of eof on encoding end flags} -setup {
8798 set fn [makeFile {} io-73.5]
8799 set rfd [open $fn r]
8800 set wfd [open $fn a]
8801 chan configure $wfd -buffering none -translation binary
8802 chan configure $rfd -buffersize 5 -encoding utf-8
8803 read $rfd
8804 } -body {
8805 set result [eof $rfd]
8806 puts -nonewline $wfd more\u00C2\u00A0data
8807 lappend result [eof $rfd]
8808 lappend result [read $rfd]
8809 lappend result [eof $rfd]
8810 } -cleanup {
8811 close $wfd
8812 close $rfd
8813 removeFile io-73.5
8814 } -result [list 1 1 more\u00A0data 1]
8815
8816 test io-74.1 {[104f2885bb] improper cache validity check} -setup {
8817 set fn [makeFile {} io-74.1]
8818 set rfd [open $fn r]
8819 testobj freeallvars
8820 interp create child
8821 } -constraints testobj -body {
8822 teststringobj set 1 [string range $rfd 0 end]
8823 read [teststringobj get 1]
8824 testobj duplicate 1 2
8825 interp transfer {} $rfd child
8826 catch {read [teststringobj get 1]}
8827 read [teststringobj get 2]
8828 } -cleanup {
8829 interp delete child
8830 testobj freeallvars
8831 removeFile io-74.1
8832 } -returnCodes error -match glob -result {can not find channel named "*"}
8833
8834 # The following tests 75.1 to 75.5 exercise strict or tolerant channel
8835 # encoding.
8836 # TCL 8.6 only offers tolerant channel encoding, what is tested here.
8837 test io-75.1 {multibyte encoding error read results in raw bytes} -setup {
8838 set fn [makeFile {} io-75.1]
8839 set f [open $fn w+]
8840 fconfigure $f -encoding binary
8841 # In UTF-8, a byte 0xCx starts a multibyte sequence and must be followed
8842 # by a byte > 0x7F. This is violated to get an invalid sequence.
8843 puts -nonewline $f A\xC0\x40
8844 flush $f
8845 seek $f 0
8846 fconfigure $f -encoding utf-8 -buffering none
8847 } -body {
8848 set d [read $f]
8849 binary scan $d H* hd
8850 set hd
8851 } -cleanup {
8852 close $f
8853 removeFile io-75.1
8854 } -result 41c040
8855
8856 test io-75.2 {unrepresentable character write passes and is replaced by ?} -setup {
8857 set fn [makeFile {} io-75.2]
8858 set f [open $fn w+]
8859 fconfigure $f -encoding iso8859-1
8860 } -body {
8861 puts -nonewline $f "A\u2022"
8862 flush $f
8863 seek $f 0
8864 read $f
8865 } -cleanup {
8866 close $f
8867 removeFile io-75.2
8868 } -result A?
8869
8870 # Incomplete sequence test.
8871 # This error may IMHO only be detected with the close.
8872 # But the read already returns the incomplete sequence.
8873 test io-75.3 {incomplete multibyte encoding read is ignored} -setup {
8874 set fn [makeFile {} io-75.3]
8875 set f [open $fn w+]
8876 fconfigure $f -encoding binary
8877 puts -nonewline $f "A\xC0"
8878 flush $f
8879 seek $f 0
8880 fconfigure $f -encoding utf-8 -buffering none
8881 } -body {
8882 set d [read $f]
8883 binary scan $d H* hd
8884 set hd
8885 } -cleanup {
8886 close $f
8887 removeFile io-75.3
8888 } -result 41c0
8889
8890 # As utf-8 has a special treatment in multi-byte decoding, also test another
8891 # one.
8892 test io-75.4 {shiftjis encoding error read results in raw bytes} -setup {
8893 set fn [makeFile {} io-75.4]
8894 set f [open $fn w+]
8895 fconfigure $f -encoding binary
8896 # In shiftjis, \x81 starts a two-byte sequence.
8897 # But 2nd byte \xFF is not allowed
8898 puts -nonewline $f A\x81\xFFA
8899 flush $f
8900 seek $f 0
8901 fconfigure $f -encoding shiftjis -buffering none -eofchar "" -translation lf
8902 } -body {
8903 set d [read $f]
8904 binary scan $d H* hd
8905 set hd
8906 } -cleanup {
8907 close $f
8908 removeFile io-75.4
8909 } -result 4181ff41
8910
8911 test io-75.5 {invalid utf-8 encoding read is ignored} -setup {
8912 set fn [makeFile {} io-75.5]
8913 set f [open $fn w+]
8914 fconfigure $f -encoding binary
8915 # \x81 announces a two byte sequence.
8916 puts -nonewline $f A\x81
8917 flush $f
8918 seek $f 0
8919 fconfigure $f -encoding utf-8 -buffering none -eofchar "" -translation lf
8920 } -body {
8921 set d [read $f]
8922 binary scan $d H* hd
8923 set hd
8924 } -cleanup {
8925 close $f
8926 removeFile io-75.5
8927 } -result 4181
8928
8929
8930 # ### ### ### ######### ######### #########
8931
8932 # cleanup
8933 foreach file [list fooBar longfile script script2 output test1 pipe my_script \
8934 test2 test3 cat stdout kyrillic.txt utf8-fcopy.txt utf8-rp.txt] {
8935 removeFile $file
8936 }
8937 cleanupTests
8938 }
8939 namespace delete ::tcl::test::io
8940 return