Skip to content

Commit 27451b5

Browse files
committed
Implement rb_syserr_new c function
It's already defined in cext.rb just not exposed in C.
1 parent 21ced61 commit 27451b5

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
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 (#3175, @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/optional/capi/exception_spec.rb

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

103+
describe "rb_syserr_new" do
104+
it "returns system error with nil message" 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+
exception.message.should include("custom message")
114+
exception.class.should == Errno::ENOENT
115+
exception.should.is_a?(SystemCallError)
116+
end
117+
end
118+
103119
describe "rb_syserr_new_str" do
104120
it "returns system error with nil message" do
105121
const = Errno::ENOENT

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ 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+
char *cstr;
41+
if(msg != Qnil) {
42+
cstr = StringValuePtr(msg);
43+
}
44+
int n = NUM2INT(num);
45+
return rb_syserr_new(n, cstr);
46+
}
47+
3948
VALUE exception_spec_rb_syserr_new_str(VALUE self, VALUE num, VALUE msg) {
4049
int n = NUM2INT(num);
4150
return rb_syserr_new_str(n, msg);
@@ -55,6 +64,7 @@ void Init_exception_spec(void) {
5564
rb_define_method(cls, "rb_exc_new3", exception_spec_rb_exc_new3, 1);
5665
rb_define_method(cls, "rb_exc_raise", exception_spec_rb_exc_raise, 1);
5766
rb_define_method(cls, "rb_set_errinfo", exception_spec_rb_set_errinfo, 1);
67+
rb_define_method(cls, "rb_syserr_new", exception_spec_rb_syserr_new, 2);
5868
rb_define_method(cls, "rb_syserr_new_str", exception_spec_rb_syserr_new_str, 2);
5969
rb_define_method(cls, "rb_make_exception", exception_spec_rb_make_exception, 1);
6070
}

src/main/c/cext/exception.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ void rb_sys_fail(const char *message) {
7272
rb_syserr_fail(n, message);
7373
}
7474

75+
VALUE rb_syserr_new(int n, const char *mesg) {
76+
VALUE arg = mesg ? rb_str_new_cstr(mesg) : Qnil;
77+
return rb_syserr_new_str(n, arg);
78+
}
79+
7580
VALUE rb_syserr_new_str(int n, VALUE mesg) {
7681
return RUBY_CEXT_INVOKE("rb_syserr_new", INT2FIX(n), mesg);
7782
}

0 commit comments

Comments
 (0)