Skip to content

Commit

Permalink
[GR-45621] Promote File#path and File#to_path to IO#path and IO#to_pa…
Browse files Browse the repository at this point in the history
…th, added path: optional param to IO#new

PullRequest: truffleruby/4041
  • Loading branch information
andrykonchin committed Oct 30, 2023
2 parents 7d90021 + 8389acd commit e2b5200
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Compatibility:
* Add `Exception#detailed_message` method (#3257, @andrykonchin).
* Fix `rb_enc_vsprintf` and force String encoding instead of converting it (@andrykonchin).
* Add `rb_gc_mark_movable` function (@andrykonchin).
* Promote `File#path` and `File#to_path` to `IO#path` and `IO#to_path` and make IO#new accept an optional `path:` keyword argument (#3039, @moste00)

Performance:

Expand Down
1 change: 0 additions & 1 deletion spec/tags/core/io/path_tags.txt

This file was deleted.

13 changes: 1 addition & 12 deletions src/main/ruby/truffleruby/core/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@
class File < IO
include Enumerable

class FileError < Exception; end # rubocop:disable Lint/InheritException
class NoFileError < FileError; end
class UnableToStat < FileError; end
class PermissionError < FileError; end

# these will be necessary when we run on Windows
DOSISH = false # Primitive.as_boolean(RUBY_PLATFORM =~ /mswin/)
CASEFOLD_FILESYSTEM = DOSISH
Expand Down Expand Up @@ -1161,14 +1156,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
super(fd, mode, **options)
super(fd, mode, **options, path: path)
end
end

Expand Down Expand Up @@ -1208,10 +1201,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
8 changes: 7 additions & 1 deletion src/main/ruby/truffleruby/core/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,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 @@ -853,6 +853,7 @@ def initialize(fd, mode = nil, **options)

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

##
Expand Down Expand Up @@ -1217,6 +1218,11 @@ 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
7 changes: 6 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,15 @@ def self.normalize_options(mode, perm, options, default_mode = nil)
external, internal = encoding.split(':', 2)
end
end

path = options[:path]
unless Primitive.nil? path
path = StringValue(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 e2b5200

Please sign in to comment.