diff --git a/CHANGELOG.md b/CHANGELOG.md index f88284ceced1..c5c71a66eb6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/spec/tags/core/io/path_tags.txt b/spec/tags/core/io/path_tags.txt deleted file mode 100644 index 2c0103ed2f90..000000000000 --- a/spec/tags/core/io/path_tags.txt +++ /dev/null @@ -1 +0,0 @@ -fails:IO#path returns the path of the file associated with the IO object diff --git a/src/main/ruby/truffleruby/core/file.rb b/src/main/ruby/truffleruby/core/file.rb index 67f270a38200..6b642e2ef3df 100644 --- a/src/main/ruby/truffleruby/core/file.rb +++ b/src/main/ruby/truffleruby/core/file.rb @@ -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 @@ -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 @@ -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 diff --git a/src/main/ruby/truffleruby/core/io.rb b/src/main/ruby/truffleruby/core/io.rb index ca300fbf6293..1489c97a5ead 100644 --- a/src/main/ruby/truffleruby/core/io.rb +++ b/src/main/ruby/truffleruby/core/io.rb @@ -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) @@ -853,6 +853,7 @@ def initialize(fd, mode = nil, **options) @autoclose = autoclose_tmp @pipe = false + @path = path end ## @@ -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 diff --git a/src/main/ruby/truffleruby/core/truffle/io_operations.rb b/src/main/ruby/truffleruby/core/truffle/io_operations.rb index e49f89aa7421..ee01aeace6c1 100644 --- a/src/main/ruby/truffleruby/core/truffle/io_operations.rb +++ b/src/main/ruby/truffleruby/core/truffle/io_operations.rb @@ -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