Skip to content

Commit 222dcf3

Browse files
committed
[GR-19220] Implement rb_syserr_new in C
PullRequest: truffleruby/3930
2 parents 15146ad + b047a27 commit 222dcf3

File tree

20 files changed

+115
-66
lines changed

20 files changed

+115
-66
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Compatibility:
3434
* Add `Module#undefined_instance_methods` (#3039, @itarato).
3535
* Add `Thread.each_caller_location` (#3039, @itarato).
3636
* Add `timeout` argument to `Thread::SizedQueue#push` (#3039, @itarato).
37+
* Add `rb_syserr_new` function (@rwstauner).
3738

3839
Performance:
3940

lib/cext/ABI_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9
1+
10

spec/ruby/core/exception/case_compare_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@
2626
end
2727

2828
it "returns true if receiver is generic and arg is kind of SystemCallError" do
29-
unknown_error_number = Errno.constants.size
3029
e = SystemCallError.new('foo', @example_errno)
3130
SystemCallError.===(e).should == true
3231
end
3332

3433
it "returns false if receiver is generic and arg is not kind of SystemCallError" do
35-
unknown_error_number = Errno.constants.size
3634
e = Object.new
3735
SystemCallError.===(e).should == false
3836
end

spec/ruby/optional/capi/exception_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,40 @@
100100
end
101101
end
102102

103+
describe "rb_syserr_new" do
104+
it "returns system error with default message when passed message is NULL" do
105+
exception = @s.rb_syserr_new(Errno::ENOENT::Errno, nil)
106+
exception.class.should == Errno::ENOENT
107+
exception.message.should include("No such file or directory")
108+
exception.should.is_a?(SystemCallError)
109+
end
110+
111+
it "returns system error with custom message" do
112+
exception = @s.rb_syserr_new(Errno::ENOENT::Errno, "custom message")
113+
114+
exception.message.should include("custom message")
115+
exception.class.should == Errno::ENOENT
116+
exception.should.is_a?(SystemCallError)
117+
end
118+
end
119+
120+
describe "rb_syserr_new_str" do
121+
it "returns system error with default message when passed message is nil" do
122+
exception = @s.rb_syserr_new_str(Errno::ENOENT::Errno, nil)
123+
124+
exception.message.should include("No such file or directory")
125+
exception.class.should == Errno::ENOENT
126+
exception.should.is_a?(SystemCallError)
127+
end
128+
129+
it "returns system error with custom message" do
130+
exception = @s.rb_syserr_new_str(Errno::ENOENT::Errno, "custom message")
131+
exception.message.should include("custom message")
132+
exception.class.should == Errno::ENOENT
133+
exception.should.is_a?(SystemCallError)
134+
end
135+
end
136+
103137
describe "rb_make_exception" do
104138
it "returns a RuntimeError when given a String argument" do
105139
e = @s.rb_make_exception(["Message"])

spec/ruby/optional/capi/ext/class_spec.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static VALUE class_spec_rb_class_new_instance_kw(VALUE self, VALUE args, VALUE k
7272
#endif
7373

7474
static VALUE class_spec_rb_class_real(VALUE self, VALUE object) {
75-
if(rb_type_p(object, T_FIXNUM)) {
75+
if (rb_type_p(object, T_FIXNUM)) {
7676
return INT2FIX(rb_class_real(FIX2INT(object)));
7777
} else {
7878
return rb_class_real(CLASS_OF(object));
@@ -116,19 +116,19 @@ VALUE class_spec_define_attr(VALUE self, VALUE klass, VALUE sym, VALUE read, VAL
116116
}
117117

118118
static VALUE class_spec_rb_define_class(VALUE self, VALUE name, VALUE super) {
119-
if(NIL_P(super)) super = 0;
119+
if (NIL_P(super)) super = 0;
120120
return rb_define_class(RSTRING_PTR(name), super);
121121
}
122122

123123
static VALUE class_spec_rb_define_class_under(VALUE self, VALUE outer,
124124
VALUE name, VALUE super) {
125-
if(NIL_P(super)) super = 0;
125+
if (NIL_P(super)) super = 0;
126126
return rb_define_class_under(outer, RSTRING_PTR(name), super);
127127
}
128128

129129
static VALUE class_spec_rb_define_class_id_under(VALUE self, VALUE outer,
130130
VALUE name, VALUE super) {
131-
if(NIL_P(super)) super = 0;
131+
if (NIL_P(super)) super = 0;
132132
return rb_define_class_id_under(outer, SYM2ID(name), super);
133133
}
134134

spec/ruby/optional/capi/ext/debug_spec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static VALUE rb_debug_inspector_frame_iseq_get_callback(const rb_debug_inspector
4545
return rb_debug_inspector_frame_iseq_get(dc, NUM2LONG((VALUE) ptr));
4646
}
4747

48-
static VALUE debug_spec_callback_data(VALUE self){
48+
static VALUE debug_spec_callback_data(VALUE self) {
4949
return callback_data;
5050
}
5151

spec/ruby/optional/capi/ext/encoding_spec.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static VALUE encoding_spec_MBCLEN_CHARFOUND_P(VALUE self, VALUE obj) {
1212
}
1313

1414
static VALUE encoding_spec_ENC_CODERANGE_ASCIIONLY(VALUE self, VALUE obj) {
15-
if(ENC_CODERANGE_ASCIIONLY(obj)) {
15+
if (ENC_CODERANGE_ASCIIONLY(obj)) {
1616
return Qtrue;
1717
} else {
1818
return Qfalse;
@@ -61,13 +61,13 @@ static VALUE encoding_spec_rb_filesystem_encindex(VALUE self) {
6161

6262
static VALUE encoding_spec_rb_default_internal_encoding(VALUE self) {
6363
rb_encoding* enc = rb_default_internal_encoding();
64-
if(enc == 0) return Qnil;
64+
if (enc == 0) return Qnil;
6565
return rb_str_new2(enc->name);
6666
}
6767

6868
static VALUE encoding_spec_rb_default_external_encoding(VALUE self) {
6969
rb_encoding* enc = rb_default_external_encoding();
70-
if(enc == 0) return Qnil;
70+
if (enc == 0) return Qnil;
7171
return rb_str_new2(enc->name);
7272
}
7373

@@ -86,7 +86,7 @@ static VALUE encoding_spec_rb_enc_associate_index(VALUE self, VALUE obj, VALUE i
8686
static VALUE encoding_spec_rb_enc_compatible(VALUE self, VALUE a, VALUE b) {
8787
rb_encoding* enc = rb_enc_compatible(a, b);
8888

89-
if(!enc) return INT2FIX(0);
89+
if (!enc) return INT2FIX(0);
9090

9191
return rb_enc_from_encoding(enc);
9292
}

spec/ruby/optional/capi/ext/exception_spec.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ VALUE exception_spec_rb_exc_new3(VALUE self, VALUE str) {
2727
}
2828

2929
VALUE exception_spec_rb_exc_raise(VALUE self, VALUE exc) {
30-
if (self != Qundef) rb_exc_raise(exc);
30+
if (self != Qundef) rb_exc_raise(exc);
3131
return Qnil;
3232
}
3333

@@ -36,6 +36,21 @@ VALUE exception_spec_rb_set_errinfo(VALUE self, VALUE exc) {
3636
return Qnil;
3737
}
3838

39+
VALUE exception_spec_rb_syserr_new(VALUE self, VALUE num, VALUE msg) {
40+
int n = NUM2INT(num);
41+
char *cstr = NULL;
42+
43+
if (msg != Qnil) {
44+
cstr = StringValuePtr(msg);
45+
}
46+
47+
return rb_syserr_new(n, cstr);
48+
}
49+
50+
VALUE exception_spec_rb_syserr_new_str(VALUE self, VALUE num, VALUE msg) {
51+
int n = NUM2INT(num);
52+
return rb_syserr_new_str(n, msg);
53+
}
3954

4055
VALUE exception_spec_rb_make_exception(VALUE self, VALUE ary) {
4156
int argc = RARRAY_LENINT(ary);
@@ -51,6 +66,8 @@ void Init_exception_spec(void) {
5166
rb_define_method(cls, "rb_exc_new3", exception_spec_rb_exc_new3, 1);
5267
rb_define_method(cls, "rb_exc_raise", exception_spec_rb_exc_raise, 1);
5368
rb_define_method(cls, "rb_set_errinfo", exception_spec_rb_set_errinfo, 1);
69+
rb_define_method(cls, "rb_syserr_new", exception_spec_rb_syserr_new, 2);
70+
rb_define_method(cls, "rb_syserr_new_str", exception_spec_rb_syserr_new_str, 2);
5471
rb_define_method(cls, "rb_make_exception", exception_spec_rb_make_exception, 1);
5572
}
5673

spec/ruby/optional/capi/ext/gc_spec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static VALUE gc_spec_rb_gc(VALUE self) {
5151
return Qnil;
5252
}
5353

54-
static VALUE gc_spec_rb_gc_latest_gc_info(VALUE self, VALUE hash_or_key){
54+
static VALUE gc_spec_rb_gc_latest_gc_info(VALUE self, VALUE hash_or_key) {
5555
return rb_gc_latest_gc_info(hash_or_key);
5656
}
5757

spec/ruby/optional/capi/ext/integer_spec.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ extern "C" {
66
#endif
77

88
static VALUE integer_spec_rb_integer_pack(VALUE self, VALUE value,
9-
VALUE words, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags)
10-
{
9+
VALUE words, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags) {
1110
int result = rb_integer_pack(value, (void*)RSTRING_PTR(words), FIX2INT(numwords),
1211
FIX2INT(wordsize), FIX2INT(nails), FIX2INT(flags));
1312
return INT2FIX(result);
1413
}
1514

1615
RUBY_EXTERN VALUE rb_int_positive_pow(long x, unsigned long y); /* internal.h, used in ripper */
1716

18-
static VALUE integer_spec_rb_int_positive_pow(VALUE self, VALUE a, VALUE b){
17+
static VALUE integer_spec_rb_int_positive_pow(VALUE self, VALUE a, VALUE b) {
1918
return rb_int_positive_pow(FIX2INT(a), FIX2INT(b));
2019
}
2120

0 commit comments

Comments
 (0)