@@ -128,7 +128,7 @@ class File < IO::FileDescriptor
128
128
# This constructor is for constructors to be able to initialize a `File` with
129
129
# a *path* and *fd*. The *blocking* param is informational and must reflect
130
130
# the non/blocking state of the underlying fd.
131
- private def initialize (@path , fd : Int , mode = " " , blocking = true , encoding = nil , invalid = nil )
131
+ private def initialize (@path , fd : Int , mode = " " , blocking = nil , encoding = nil , invalid = nil )
132
132
super (handle: fd)
133
133
system_init(mode, blocking)
134
134
set_encoding(encoding, invalid: invalid) if encoding
@@ -156,19 +156,12 @@ class File < IO::FileDescriptor
156
156
# Line endings are preserved on all platforms. The `b` mode flag has no
157
157
# effect; it is provided only for POSIX compatibility.
158
158
#
159
- # *blocking* is set to `true` by default because system event queues (e.g.
160
- # epoll, kqueue) will always report the file descriptor of regular disk files
161
- # as ready.
162
- #
163
- # *blocking* must be set to `false` on POSIX targets when the file to open
164
- # isn't a regular file but a character device (e.g. `/dev/tty`) or fifo. These
165
- # files depend on another process or thread to also be reading or writing, and
166
- # system event queues will properly report readiness.
167
- #
168
- # *blocking* may also be set to `nil` in which case the blocking or
169
- # non-blocking flag will be determined automatically, at the expense of an
170
- # additional syscall.
171
- def self.new (filename : Path | String , mode = " r" , perm = DEFAULT_CREATE_PERMISSIONS , encoding = nil , invalid = nil , blocking = true )
159
+ # NOTE: The *blocking* arg is deprecated since Crystal 1.17. It used to be
160
+ # true by default to denote a regular disk file (always ready in system event
161
+ # loops) and could be set to false when the file was known to be a fifo, pipe,
162
+ # or character device (for example `/dev/tty`). Event loops now properly
163
+ # handle this and there are no reasons to change the blocking mode anymore.
164
+ def self.new (filename : Path | String , mode = " r" , perm = DEFAULT_CREATE_PERMISSIONS , encoding = nil , invalid = nil , blocking = nil )
172
165
filename = filename.to_s
173
166
fd, blocking = Crystal ::System ::File .open(filename, mode, perm: perm, blocking: blocking)
174
167
new(filename, fd, mode, blocking, encoding, invalid)
@@ -505,7 +498,7 @@ class File < IO::FileDescriptor
505
498
# permissions may be set using the *perm* parameter.
506
499
#
507
500
# See `self.new` for what *mode* can be.
508
- def self.open (filename : Path | String , mode = " r" , perm = DEFAULT_CREATE_PERMISSIONS , encoding = nil , invalid = nil , blocking = true ) : self
501
+ def self.open (filename : Path | String , mode = " r" , perm = DEFAULT_CREATE_PERMISSIONS , encoding = nil , invalid = nil , blocking = nil ) : self
509
502
new filename, mode, perm, encoding, invalid, blocking
510
503
end
511
504
@@ -514,7 +507,7 @@ class File < IO::FileDescriptor
514
507
# file as an argument, the file will be automatically closed when the block returns.
515
508
#
516
509
# See `self.new` for what *mode* can be.
517
- def self.open (filename : Path | String , mode = " r" , perm = DEFAULT_CREATE_PERMISSIONS , encoding = nil , invalid = nil , blocking = true , & )
510
+ def self.open (filename : Path | String , mode = " r" , perm = DEFAULT_CREATE_PERMISSIONS , encoding = nil , invalid = nil , blocking = nil , & )
518
511
file = new filename, mode, perm, encoding, invalid, blocking
519
512
begin
520
513
yield file
@@ -529,7 +522,7 @@ class File < IO::FileDescriptor
529
522
# File.write("bar", "foo")
530
523
# File.read("bar") # => "foo"
531
524
# ```
532
- def self.read (filename : Path | String , encoding = nil , invalid = nil , blocking = true ) : String
525
+ def self.read (filename : Path | String , encoding = nil , invalid = nil , blocking = nil ) : String
533
526
open(filename, " r" , blocking: blocking) do |file |
534
527
if encoding
535
528
file.set_encoding(encoding, invalid: invalid)
@@ -558,7 +551,7 @@ class File < IO::FileDescriptor
558
551
# end
559
552
# array # => ["foo", "bar"]
560
553
# ```
561
- def self.each_line (filename : Path | String , encoding = nil , invalid = nil , chomp = true , blocking = true , & )
554
+ def self.each_line (filename : Path | String , encoding = nil , invalid = nil , chomp = true , blocking = nil , & )
562
555
open(filename, " r" , encoding: encoding, invalid: invalid, blocking: blocking) do |file |
563
556
file.each_line(chomp: chomp) do |line |
564
557
yield line
@@ -572,7 +565,7 @@ class File < IO::FileDescriptor
572
565
# File.write("foobar", "foo\nbar")
573
566
# File.read_lines("foobar") # => ["foo", "bar"]
574
567
# ```
575
- def self.read_lines (filename : Path | String , encoding = nil , invalid = nil , chomp = true , blocking = true ) : Array (String )
568
+ def self.read_lines (filename : Path | String , encoding = nil , invalid = nil , chomp = true , blocking = nil ) : Array (String )
576
569
lines = [] of String
577
570
each_line(filename, encoding: encoding, invalid: invalid, chomp: chomp, blocking: blocking) do |line |
578
571
lines << line
@@ -597,7 +590,7 @@ class File < IO::FileDescriptor
597
590
# (the result of invoking `to_s` on *content*).
598
591
#
599
592
# See `self.new` for what *mode* can be.
600
- def self.write (filename : Path | String , content, perm = DEFAULT_CREATE_PERMISSIONS , encoding = nil , invalid = nil , mode = " w" , blocking = true )
593
+ def self.write (filename : Path | String , content, perm = DEFAULT_CREATE_PERMISSIONS , encoding = nil , invalid = nil , mode = " w" , blocking = nil )
601
594
open(filename, mode, perm, encoding: encoding, invalid: invalid, blocking: blocking) do |file |
602
595
case content
603
596
when Bytes
0 commit comments