forked from jruby/jruby
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Direct port of SignalException from MRI
Fixes jruby#3954
- Loading branch information
Showing
5 changed files
with
141 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |