Skip to content

Commit

Permalink
Promoted File#path and File#to_path to IO#path and IO#to_path, added …
Browse files Browse the repository at this point in the history
…path: optional param to IO#new
  • Loading branch information
moste00 committed Oct 6, 2023
1 parent b4a05b5 commit f1a6eef
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
1 change: 0 additions & 1 deletion spec/tags/core/io/path_tags.txt

This file was deleted.

12 changes: 5 additions & 7 deletions src/main/ruby/truffleruby/core/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
class File < IO
include Enumerable

class FileError < Exception; end # rubocop:disable Lint/InheritException
class FileError < RuntimeError; end

class NoFileError < FileError; end

class UnableToStat < FileError; end

class PermissionError < FileError; end

# these will be necessary when we run on Windows
Expand Down Expand Up @@ -1161,13 +1164,12 @@ class << self
def initialize(path_or_fd, mode = nil, perm = nil, **options)
if Primitive.is_a?(path_or_fd, Integer)
super(path_or_fd, mode, **options)
@path = nil
else
path = Truffle::Type.coerce_to_path path_or_fd
nmode, _binary, _external, _internal, _autoclose, perm = Truffle::IOOperations.normalize_options(mode, perm, options)
fd = IO.sysopen(path, nmode, perm)

@path = path
options[:path] = path
super(fd, mode, **options)
end
end
Expand Down Expand Up @@ -1208,10 +1210,6 @@ def stat
Stat.fstat Primitive.io_fd(self)
end

def path
@path.dup
end
alias_method :to_path, :path

def truncate(length)
length = Truffle::Type.coerce_to length, Integer, :to_int
Expand Down
9 changes: 8 additions & 1 deletion src/main/ruby/truffleruby/core/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ def initialize(fd, mode = nil, **options)
@external = nil
@pid = nil

mode, binary, external, internal, autoclose_tmp, _perm = Truffle::IOOperations.normalize_options(mode, nil, options)
mode, binary, external, internal, autoclose_tmp, _perm, path = Truffle::IOOperations.normalize_options(mode, nil, options)

fd = Truffle::Type.coerce_to(fd, Integer, :to_int)
sync = fd == 2 # stderr is always unbuffered, see setvbuf(3)
Expand All @@ -857,6 +857,7 @@ def initialize(fd, mode = nil, **options)

@autoclose = autoclose_tmp
@pipe = false
@path = path
end

##
Expand Down Expand Up @@ -1221,6 +1222,12 @@ def prepare_read_string(str)
@lineno += 1
end

def path
@path.dup
end

alias_method :to_path, :path

##
# Return a string describing this IO object.
def inspect
Expand Down
6 changes: 5 additions & 1 deletion src/main/ruby/truffleruby/core/truffle/io_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,14 @@ def self.normalize_options(mode, perm, options, default_mode = nil)
external, internal = encoding.split(':', 2)
end
end

unless Primitive.nil? options[:path]
path = StringValue(options[:path])
end
end
external = Encoding::BINARY if binary and !external and !internal
perm ||= 0666
[mode, binary, external, internal, autoclose, perm]
[mode, binary, external, internal, autoclose, perm, path]
end
end
end

0 comments on commit f1a6eef

Please sign in to comment.