Skip to content

Commit

Permalink
[GR-18163] T_FILE should be for IO instances, not just File (#2662)
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/3366
  • Loading branch information
eregon committed May 27, 2022
2 parents b8867bd + 0b46762 commit 7dae01e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Compatibility:
* Implement specializations for immutable ruby objects for ObjectSpace methods (@bjfish).
* Use `$PAGER` for `--help` and `--help*`, similar to CRuby (#2542, @Strech).
* Ensure all headers are warnings-free (#2662, @eregon).
* All `IO` instances should have `T_FILE` as their `rb_type()`, not only `File` instances (#2662, @eregon).

Performance:

Expand Down
2 changes: 1 addition & 1 deletion lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def rb_tr_find_type(value)
T_HASH
when Struct
T_STRUCT
when File
when IO
T_FILE
when Complex
T_COMPLEX
Expand Down
8 changes: 8 additions & 0 deletions spec/ruby/optional/capi/ext/object_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ static VALUE so_is_rb_type_p_data(VALUE self, VALUE obj) {
return Qfalse;
}

static VALUE so_is_rb_type_p_file(VALUE self, VALUE obj) {
if(rb_type_p(obj, T_FILE)) {
return Qtrue;
}
return Qfalse;
}

static VALUE so_is_builtin_type_object(VALUE self, VALUE obj) {
if(BUILTIN_TYPE(obj) == T_OBJECT) {
return Qtrue;
Expand Down Expand Up @@ -478,6 +485,7 @@ void Init_object_spec(void) {
rb_define_method(cls, "rb_is_rb_type_p_module", so_is_rb_type_p_module, 1);
rb_define_method(cls, "rb_is_rb_type_p_class", so_is_rb_type_p_class, 1);
rb_define_method(cls, "rb_is_rb_type_p_data", so_is_rb_type_p_data, 1);
rb_define_method(cls, "rb_is_rb_type_p_file", so_is_rb_type_p_file, 1);
rb_define_method(cls, "rb_is_builtin_type_object", so_is_builtin_type_object, 1);
rb_define_method(cls, "rb_is_builtin_type_array", so_is_builtin_type_array, 1);
rb_define_method(cls, "rb_is_builtin_type_module", so_is_builtin_type_module, 1);
Expand Down
15 changes: 15 additions & 0 deletions spec/ruby/optional/capi/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,21 @@ class DescArray < Array
@o.rb_is_type_class(ObjectTest).should == true
@o.rb_is_type_data(Time.now).should == true
end

it "returns T_FILE for instances of IO and subclasses" do
STDERR.class.should == IO
@o.rb_is_rb_type_p_file(STDERR).should == true

File.open(__FILE__) do |f|
f.class.should == File
@o.rb_is_rb_type_p_file(f).should == true
end

require 'socket'
TCPServer.open(0) do |s|
@o.rb_is_rb_type_p_file(s).should == true
end
end
end

describe "rb_check_type" do
Expand Down

0 comments on commit 7dae01e

Please sign in to comment.