Skip to content

Commit e2b5200

Browse files
committed
[GR-45621] Promote File#path and File#to_path to IO#path and IO#to_path, added path: optional param to IO#new
PullRequest: truffleruby/4041
2 parents 7d90021 + 8389acd commit e2b5200

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Compatibility:
1414
* Add `Exception#detailed_message` method (#3257, @andrykonchin).
1515
* Fix `rb_enc_vsprintf` and force String encoding instead of converting it (@andrykonchin).
1616
* Add `rb_gc_mark_movable` function (@andrykonchin).
17+
* 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)
1718

1819
Performance:
1920

spec/tags/core/io/path_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/ruby/truffleruby/core/file.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@
3737
class File < IO
3838
include Enumerable
3939

40-
class FileError < Exception; end # rubocop:disable Lint/InheritException
41-
class NoFileError < FileError; end
42-
class UnableToStat < FileError; end
43-
class PermissionError < FileError; end
44-
4540
# these will be necessary when we run on Windows
4641
DOSISH = false # Primitive.as_boolean(RUBY_PLATFORM =~ /mswin/)
4742
CASEFOLD_FILESYSTEM = DOSISH
@@ -1161,14 +1156,12 @@ class << self
11611156
def initialize(path_or_fd, mode = nil, perm = nil, **options)
11621157
if Primitive.is_a?(path_or_fd, Integer)
11631158
super(path_or_fd, mode, **options)
1164-
@path = nil
11651159
else
11661160
path = Truffle::Type.coerce_to_path path_or_fd
11671161
nmode, _binary, _external, _internal, _autoclose, perm = Truffle::IOOperations.normalize_options(mode, perm, options)
11681162
fd = IO.sysopen(path, nmode, perm)
11691163

1170-
@path = path
1171-
super(fd, mode, **options)
1164+
super(fd, mode, **options, path: path)
11721165
end
11731166
end
11741167

@@ -1208,10 +1201,6 @@ def stat
12081201
Stat.fstat Primitive.io_fd(self)
12091202
end
12101203

1211-
def path
1212-
@path.dup
1213-
end
1214-
alias_method :to_path, :path
12151204

12161205
def truncate(length)
12171206
length = Truffle::Type.coerce_to length, Integer, :to_int

src/main/ruby/truffleruby/core/io.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ def initialize(fd, mode = nil, **options)
842842
@external = nil
843843
@pid = nil
844844

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

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

854854
@autoclose = autoclose_tmp
855855
@pipe = false
856+
@path = path
856857
end
857858

858859
##
@@ -1217,6 +1218,11 @@ def prepare_read_string(str)
12171218
@lineno += 1
12181219
end
12191220

1221+
def path
1222+
@path.dup
1223+
end
1224+
alias_method :to_path, :path
1225+
12201226
##
12211227
# Return a string describing this IO object.
12221228
def inspect

src/main/ruby/truffleruby/core/truffle/io_operations.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,15 @@ def self.normalize_options(mode, perm, options, default_mode = nil)
543543
external, internal = encoding.split(':', 2)
544544
end
545545
end
546+
547+
path = options[:path]
548+
unless Primitive.nil? path
549+
path = StringValue(path)
550+
end
546551
end
547552
external = Encoding::BINARY if binary and !external and !internal
548553
perm ||= 0666
549-
[mode, binary, external, internal, autoclose, perm]
554+
[mode, binary, external, internal, autoclose, perm, path]
550555
end
551556
end
552557
end

0 commit comments

Comments
 (0)