Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-59866] Follow up for PR "Add missing io-related functions" #3765

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cext/include/truffleruby/truffleruby-abi-version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
// $RUBY_VERSION must be the same as TruffleRuby.LANGUAGE_VERSION.
// $ABI_NUMBER starts at 1 and is incremented for every ABI-incompatible change.

#define TRUFFLERUBY_ABI_VERSION "3.3.5.9"
#define TRUFFLERUBY_ABI_VERSION "3.3.5.11"

#endif
10 changes: 2 additions & 8 deletions lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2379,17 +2379,11 @@ def rb_io_path(io)
end

def rb_io_open_descriptor(klass, fd, mode, path, timeout, internal_encoding, external_encoding, flags, options)
return klass.allocate if klass != IO and klass != File

# Translate Ruby-specific modes (`FMODE_`) to corresponding platform-specific file open flags (`O_`).
# Translate platform-specific file open flags (`O_`) to corresponding Ruby-specific modes (`FMODE_`).
# Ruby interface accepts `FMODE_` flags, but C API functions accept `O_` flags.
mode = Truffle::IOOperations.translate_omode_to_fmode(mode)

klass.new(fd, mode, **options, internal_encoding: internal_encoding, external_encoding: external_encoding, path: path, flags: flags, skip_mode_enforcing: true)
end

def rb_io_closed_p(io)
io.closed?
klass.new(fd, mode, **options, internal_encoding: internal_encoding, external_encoding: external_encoding, path: path, flags: flags)
end

def rb_tr_io_pointer(io)
Expand Down
14 changes: 6 additions & 8 deletions spec/ruby/optional/capi/ext/io_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,14 @@ static VALUE io_spec_rb_io_closed_p(VALUE self, VALUE io) {
}

static VALUE io_spec_rb_io_open_descriptor(VALUE self, VALUE klass, VALUE descriptor, VALUE mode, VALUE path, VALUE timeout, VALUE internal_encoding, VALUE external_encoding, VALUE ecflags, VALUE ecopts) {
struct rb_io_encoding *io_encoding;
struct rb_io_encoding io_encoding;

io_encoding = (struct rb_io_encoding *) malloc(sizeof(struct rb_io_encoding));
io_encoding.enc = rb_to_encoding(internal_encoding);
io_encoding.enc2 = rb_to_encoding(external_encoding);
io_encoding.ecflags = FIX2INT(ecflags);
io_encoding.ecopts = ecopts;

io_encoding->enc = rb_to_encoding(internal_encoding);
io_encoding->enc2 = rb_to_encoding(external_encoding);
io_encoding->ecflags = FIX2INT(ecflags);
io_encoding->ecopts = ecopts;

return rb_io_open_descriptor(klass, FIX2INT(descriptor), FIX2INT(mode), path, timeout, io_encoding);
return rb_io_open_descriptor(klass, FIX2INT(descriptor), FIX2INT(mode), path, timeout, &io_encoding);
}

static VALUE io_spec_rb_io_open_descriptor_without_encoding(VALUE self, VALUE klass, VALUE descriptor, VALUE mode, VALUE path, VALUE timeout) {
Expand Down
1 change: 1 addition & 0 deletions spec/tags/core/file/new_tags.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
fails:File.new does not use the given block and warns to use File::open
fails:File.new can't alter mode or permissions when opening a file
1 change: 1 addition & 0 deletions spec/tags/core/io/for_fd_tags.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
fails:IO.for_fd ignores the :encoding option when the :external_encoding option is present
fails:IO.for_fd ignores the :encoding option when the :internal_encoding option is present
fails:IO.for_fd raises an Errno::EINVAL if the new mode is not compatible with the descriptor's current mode
1 change: 1 addition & 0 deletions spec/tags/core/io/new_tags.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
fails:IO.new ignores the :encoding option when the :external_encoding option is present
fails:IO.new ignores the :encoding option when the :internal_encoding option is present
fails:IO.new raises an Errno::EINVAL if the new mode is not compatible with the descriptor's current mode
1 change: 1 addition & 0 deletions spec/tags/core/io/open_tags.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fails:IO.open propagates an exception raised by #close that is a StandardError
fails:IO.open ignores the :encoding option when the :external_encoding option is present
fails:IO.open ignores the :encoding option when the :internal_encoding option is present
fails:IO.open raises an Errno::EINVAL if the new mode is not compatible with the descriptor's current mode
2 changes: 1 addition & 1 deletion src/main/c/cext/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ VALUE rb_io_open_descriptor(VALUE klass, int descriptor, int mode, VALUE path, V
}

VALUE rb_io_closed_p(VALUE io) {
return RUBY_CEXT_INVOKE("rb_io_closed_p", io);
return RUBY_INVOKE(io, "closed?");
}

static RFile_and_rb_io_t* get_RFile_and_rb_io_t(VALUE io) {
Expand Down
23 changes: 8 additions & 15 deletions src/main/ruby/truffleruby/core/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -816,25 +816,18 @@ def self.sysopen(path, mode = nil, perm = nil)
# the underlying descriptor allows it.
#
# The +sync+ attribute will also be set.
#
# The +skip_mode_enforcing+ parameter is needed for implementing some C-functions and allows
# to bypass enforcing a specified file mode to be the same as current mode of a file.
def self.setup(io, fd, mode, sync, skip_mode_enforcing = false)
if !Truffle::Boot.preinitializing? && Truffle::POSIX::NATIVE
cur_mode = Truffle::POSIX.fcntl(fd, F_GETFL, 0)
Errno.handle if cur_mode < 0
cur_mode &= ACCMODE
end
def self.setup(io, fd, mode, sync)
raise Errno::EBADF if fd < 0

if mode
mode = Truffle::IOOperations.parse_mode(mode)
mode &= ACCMODE

if cur_mode and (cur_mode == RDONLY or cur_mode == WRONLY) and mode != cur_mode && !skip_mode_enforcing
raise Errno::EINVAL, "Invalid mode #{cur_mode} for existing descriptor #{fd} (expected #{mode})"
end
elsif !Truffle::Boot.preinitializing? && Truffle::POSIX::NATIVE
mode = Truffle::POSIX.fcntl(fd, F_GETFL, 0)
Errno.handle if mode < 0
mode &= ACCMODE
else
mode = cur_mode or raise 'No mode given for IO'
raise 'No mode given for IO'
end

# Close old descriptor if there was already one associated
Expand Down Expand Up @@ -865,7 +858,7 @@ def initialize(fd, mode = nil, **options)

fd = Truffle::Type.coerce_to(fd, Integer, :to_int)
sync = fd == 2 # stderr is always unbuffered, see setvbuf(3)
IO.setup(self, fd, mode, sync, options[:skip_mode_enforcing]) # :skip_mode_enforcing is a TruffleRuby specific option used internally only
IO.setup(self, fd, mode, sync)

binmode if binary
set_encoding external, internal
Expand Down
1 change: 1 addition & 0 deletions test/mri/excludes/TestIO.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
exclude :test_foreach, "expected: /IO process creation with a leading '\\|'/"
exclude :test_gets_chomp_rs_nil, "<\"a\\n\" + \"\\n\" + \"b\\n\" + \"\\n\"> expected but was <\"a\\n\" + \"\\n\" + \"b\\n\">."
exclude :test_gets_limit_extra_arg, "TypeError expected but nothing was raised."
exclude :test_initialize, "TruffleRuby intentionally does not check actual mode: Errno::EINVAL expected but nothing was raised."
exclude :test_invalid_advise, "Expected Exception(NotImplementedError) was raised, but the message doesn't match. Expected /:Normal/ to match \"Unsupported advice: Normal\"."
exclude :test_new_with_block, "expected: /IO入出力/"
exclude :test_open_pipe, "expected: /Kernel#open with a leading '\\|'/"
Expand Down
Loading