Skip to content

Commit

Permalink
Direct port of SignalException from MRI
Browse files Browse the repository at this point in the history
  • Loading branch information
etehtsea committed Sep 1, 2016
1 parent 90413a8 commit cba615c
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 15 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/org/jruby/Ruby.java
Expand Up @@ -1610,8 +1610,10 @@ private void initExceptions() {
ioError = defineClassIfAllowed("IOError", standardError);
scriptError = defineClassIfAllowed("ScriptError", exceptionClass);
rangeError = defineClassIfAllowed("RangeError", standardError);
signalException = defineClassIfAllowed("SignalException", exceptionClass);

if (profile.allowClass("SignalException")) {
signalException = RubySignalException.createSignalExceptionClass(this, exceptionClass);
}
if (profile.allowClass("NameError")) {
nameError = RubyNameError.createNameErrorClass(this, standardError);
nameErrorMessage = RubyNameError.createNameErrorMessageClass(this, nameError);
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/org/jruby/RubySignal.java
Expand Up @@ -148,7 +148,15 @@ public static String signo2signm(long no) {
}
return null;
}


// MRI: signm2signo
public static long signm2signo(String nm) {
for (Signal s : Signal.values()) {
if (s.name().substring(3).equals(nm)) return s.longValue();
}
return 0;
}

private static final Set<String> RUBY_18_SIGNALS;
static {
RUBY_18_SIGNALS = new HashSet<String>();
Expand Down
128 changes: 128 additions & 0 deletions core/src/main/java/org/jruby/RubySignalException.java
@@ -0,0 +1,128 @@
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/

package org.jruby;

import static jnr.constants.platform.Signal.NSIG;

import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import static org.jruby.runtime.Visibility.PRIVATE;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.RubyBasicObject;
import org.jruby.RubySignal;
import org.jruby.util.TypeConverter;

@JRubyClass(name="SignalException", parent="Exception")
public class RubySignalException extends RubyException {
private static final ObjectAllocator SIGNAL_EXCEPTION_ALLOCATOR = new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
return new RubySignalException(runtime, klass);
}
};

protected RubySignalException(Ruby runtime, RubyClass exceptionClass) {
super(runtime, exceptionClass);
}

static RubyClass createSignalExceptionClass(Ruby runtime, RubyClass exceptionClass) {
RubyClass signalExceptionClass = runtime.defineClass("SignalException", exceptionClass, SIGNAL_EXCEPTION_ALLOCATOR);
signalExceptionClass.defineAnnotatedMethods(RubySignalException.class);

return signalExceptionClass;
}

@JRubyMethod(optional = 2, visibility = PRIVATE)
public IRubyObject initialize(IRubyObject[] args, Block block) {
final Ruby runtime = getRuntime();
int argnum = 1;
IRubyObject sig = runtime.getCurrentContext().nil;
long _signo;
int argc = args.length;

if (argc > 0) {
sig = TypeConverter.checkIntegerType(runtime, args[0], "to_int");

if (sig.isNil()) {
sig = args[0];
} else {
argnum = 2;
}
}

Arity.checkArgumentCount(runtime, args, 1, argnum);

if (argnum == 2) {
_signo = sig.convertToInteger().getLongValue();
if (_signo < 0 || _signo > NSIG.longValue()) {
throw runtime.newArgumentError("invalid signal number (" + _signo + ")");
}

if (argc > 1) {
sig = args[1];
} else {
sig = runtime.newString("SIG" + RubySignal.signo2signm(_signo));
}
} else {
String signm = sig.toString();
signm = signm.startsWith("SIG") ? signm.substring("SIG".length()) : signm;
_signo = RubySignal.signm2signo(signm);

if (_signo == 0) {
throw runtime.newArgumentError("unsupported name " + sig);
}

sig = runtime.newString("SIG" + signm);
}

super.initialize(new IRubyObject[]{sig}, block);
this.signo = runtime.newFixnum(_signo);

return this;
}

@JRubyMethod
public IRubyObject signo(ThreadContext context) {
assert signo != null;

if (signo == RubyBasicObject.UNDEF) return context.nil;

return signo;
}

@JRubyMethod(name = {"message","signm"})
@Override
public IRubyObject message(ThreadContext context) {
return super.message(context);
}

private IRubyObject signo;
}
11 changes: 0 additions & 11 deletions spec/tags/ruby/core/exception/signal_exception_tags.txt
@@ -1,12 +1 @@
fails:SignalException.new takes a signal number as the first argument
fails:SignalException.new raises an exception with an invalid signal number
fails:SignalException.new takes a signal name without SIG prefix as the first argument
fails:SignalException.new takes a signal name with SIG prefix as the first argument
fails:SignalException.new raises an exception with an invalid signal name
fails:SignalException.new takes a signal symbol without SIG prefix as the first argument
fails:SignalException.new takes a signal symbol with SIG prefix as the first argument
fails:SignalException.new raises an exception with an invalid signal name
fails:SignalException.new takes an optional message argument with a signal number
fails:SignalException.new raises an exception for an optional argument with a signal name
critical(crash):rescueing SignalException raises a SignalException when sent a signal
fails:rescueing SignalException raises a SignalException when sent a signal
3 changes: 1 addition & 2 deletions test/mri/excludes/TestSignal.rb
@@ -1,11 +1,10 @@
exclude :test_exit_action, "kills test run"
exclude :test_hup_me, "kills test run"
exclude :test_kill_immediately_before_termination, "kills test run"
exclude :test_signal2, "SignalException needs to be its own class which provides a #signo accessor"
exclude :test_trap, "SignalException needs to be its own class which provides a #signo accessor"
exclude :test_signal_exception, "SignalException needs to be its own class which enforces args"
exclude :test_signame, "needs investigation - OutOfMemoryError"
exclude :test_trap_uncatchable_KILL, "uses SIGKILL which is already in use on the JVM"
exclude :test_trap_uncatchable_STOP, "uses SIGSTOP which is already in use on the JVM"
exclude :test_trap_puts, "fails due to use of INT. Works when the test uses SIGUSR2"
exclude :test_trap_system_default, "Test works, but JRuby's trap warning violates its stderr expectations"
exclude :test_trap_system_default, "Test works, but JRuby's trap warning violates its stderr expectations"

0 comments on commit cba615c

Please sign in to comment.